近来有意进军区块链行业,偷偷的学习使用Solidity开发以太坊Ethereum智能合约,瞎蒙乱撞的使用了据说是当下最流行的Truffle框架,参考其在线教程 Truffle Doc 步步探索,跑通了第一个合约:MetaCoin,期间碰到一些小坑,记录和分享一下。
OS:Windows 7 ,有点老旧啊。
1、使用命令【truffle unbox metacoin】,使用【truffle init】也一样, 时发生错误【RequestError: Error: read ECONNRESET】,如下所示。
C:\Users\Gary\Documents\Dapps\MetaCoin>truffle unbox metacoin
Starting unbox...
=================
√ Preparing to download box
× Downloading
Unbox failed!
× Downloading
Unbox failed!
RequestError: Error: read ECONNRESET
at new RequestError (C:\Users\Gary\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\request-promise-core\lib\errors.js:14:1)
at Request.plumbing.callback (C:\Users\Gary\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\request-promise-core\lib\plumbing.js:87:1)
at Request.RP$callback [as _callback] (C:\Users\Gary\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\request-promise-core\lib\plumbing.js:46:1)
at self.callback (C:\Users\Gary\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\request\request.js:185:1)
at Request.emit (events.js:311:20)
at Request.onRequestError (C:\Users\Gary\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\request\request.js:881:1)
at ClientRequest.emit (events.js:311:20)
at TLSSocket.socketErrorListener (_http_client.js:426:9)
at TLSSocket.emit (events.js:311:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Truffle v5.1.14 (core: 5.1.14)
Node v12.16.1
2、一通搜索,查找到真凶是网络原因,请在hosts文件添加gitbub相关域名。找到hosts(所在目录:C:\windows\System32\drivers\etc\hosts),在其中添加下面的内容:
# GitHub Start
192.30.255.112 gist.github.com
192.30.255.112 github.com
192.30.255.112 www.github.com
151.101.56.133 avatars0.githubusercontent.com
151.101.56.133 avatars1.githubusercontent.com
151.101.56.133 avatars2.githubusercontent.com
151.101.56.133 avatars3.githubusercontent.com
151.101.56.133 avatars4.githubusercontent.com
151.101.56.133 avatars5.githubusercontent.com
151.101.56.133 avatars6.githubusercontent.com
151.101.56.133 avatars7.githubusercontent.com
151.101.56.133 avatars8.githubusercontent.com
151.101.56.133 camo.githubusercontent.com
151.101.56.133 cloud.githubusercontent.com
151.101.56.133 gist.githubusercontent.com
151.101.56.133 marketplace-screenshots.githubusercontent.com
151.101.56.133 raw.githubusercontent.com
151.101.56.133 repository-images.githubusercontent.com
151.101.56.133 user-images.githubusercontent.com
# GitHub End
3、重新运行命令【truffle unbox metacoin】,完美解决,嘚瑟嘚瑟!如果还是不能成功,可能是网络不太稳定,请多试几次,或者喝一杯拿铁再试试,哈哈。
C:\Users\Gary\Documents\Dapps\MetaCoin>truffle unbox metacoin
Starting unbox...
=================
√ Preparing to download box
√ Downloading
√ cleaning up temporary files
√ Setting up box
Unbox successful, sweet!
Commands:
Compile contracts: truffle compile
Migrate contracts: truffle migrate
Test contracts: truffle test