本文我们使用 docker 搭建以太币的私有链。

网上以讹传讹的正统做法太麻烦, 其实以太坊的 geth 自带私有链的解决方案, 我们基于官方的 docker 镜像: ethereum , 一句命令搞定:

sudo docker run -it -d -p 8545:8545 -p 30303:30303 -v /home/block/geth-private/:/root/ ethereum/client-go --mine --minerthreads 1 --dev --dev.period=10 --rpcaddr 0.0.0.0 --rpc --rpcapi "db,eth,net,web3"

说明:

  1. --dev 指定开发者模式, 即私有链
  2. --dev.period=10 开发者模式私有链的块生成周期. 单位是”秒”
  3. 8545:用于rpc调用, 30303:用于以太区块链数据的广播.
  4. geth attach /tmp/geth.ipc 该命令登录到控制台, 或者使用 --ipcpath 参数指定
  5. 注意: 请设置 –datadir “/your/path” , 如果不设置,重启后数据会丢失

OK, 可以收工了! 一下正统做法可以忽略了

RPC接口测试:

$ curl -H 'Content-Type: application/json' -X POST --data '
   {"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":66}
' localhost:8545 -i

以太坊 WIKI

命令行参数: https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options


正统的方法

下面是网上可以搜到的正统做法, 我们创建一个 dockerfile 文件,内容如下:

FROM ethereum/client-go

RUN echo '{\
  "config": {\
      "chainId": 6,\
      "homesteadBlock": 0,\
      "eip155Block": 0,\
      "eip158Block": 0\
  },\
  "alloc": {\
      "0x0000000000000000000000000000000000000001": {"balance": "9876543210"},\
      "0x0000000000000000000000000000000000000002": {"balance": "12345678901"}\
  },\
  "coinbase"   : "0x0000000000000000000000000000000000000000",\
  "difficulty" : "0x20000",\
  "extraData"  : "",\
  "gasLimit"   : "0x2fefd8",\
  "nonce"      : "0x000000'`date "+%s"`'",\
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",\
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",\
  "timestamp"  : "0x00"\
}' > /root/genesis.json

RUN echo 123456 > /root/eth.pass
RUN /usr/local/bin/geth account new --password /root/eth.pass

RUN /usr/local/bin/geth init /root/genesis.json

ENTRYPOINT [""]
CMD []

说明:’/root/genesis.json’ 用于初始化创世区块, alloc 可以配置初始帐号的余额。 /root/eth.pass 初始化以太币挖矿帐号的密码(123456)。

然后编译,并运行:

 #> sudo docker build . -t test # 编译
 #> sudo docker run -it --rm test geth help # 查看帮助
 #> sudo docker run --name test -it test ps # 创建 test
 #> sudo docker cp test:/root/.ethereum /data/geth-private # copy 私用链初始文件, /data/geth-private 后面 -v 使用该目录
 #> sudo docker run -d -v /data/geth-private/:/root/.ethereum/ --name eth-private -p 8545:8545 -p 30303:30303 ethereum/client-go --mine --minerthreads=1 --maxpeers 0 --rpcaddr 0.0.0.0 --rpc --rpcapi db,eth,net,web3
 #> sudo docker logs -f eth-private # 查看日志

看到如下内容:

INFO [04-23|16:27:10] Generating DAG in progress               epoch=0 percentage=43 elapsed=2m21.566s
INFO [04-23|16:27:13] Generating DAG in progress               epoch=0 percentage=44 elapsed=2m24.482s
INFO [04-23|16:27:15] Generating DAG in progress               epoch=0 percentage=45 elapsed=2m27.229s
INFO [04-23|16:27:18] Generating DAG in progress               epoch=0 percentage=46 elapsed=2m29.877s
INFO [04-23|16:27:21] Generating DAG in progress               epoch=0 percentage=47 elapsed=2m32.656s
INFO [04-23|16:27:24] Generating DAG in progress               epoch=0 percentage=48 elapsed=2m35.921s
INFO [04-23|16:27:27] Generating DAG in progress               epoch=0 percentage=49 elapsed=2m39.304s
..................
INFO [04-23|17:06:08] Generating DAG in progress               epoch=1 percentage=92 elapsed=35m8.843s
INFO [04-23|17:06:28] Generating DAG in progress               epoch=1 percentage=93 elapsed=35m29.399s
INFO [04-23|17:06:51] Generating DAG in progress               epoch=1 percentage=94 elapsed=35m51.768s
INFO [04-23|17:07:15] Generating DAG in progress               epoch=1 percentage=95 elapsed=36m16.324s
INFO [04-23|17:07:38] Generating DAG in progress               epoch=1 percentage=96 elapsed=36m39.318s
INFO [04-23|17:08:02] Generating DAG in progress               epoch=1 percentage=97 elapsed=37m3.001s
INFO [04-23|17:08:26] Generating DAG in progress               epoch=1 percentage=98 elapsed=37m26.534s
INFO [04-23|17:08:48] Generating DAG in progress               epoch=1 percentage=99 elapsed=37m48.782s
INFO [04-23|17:08:48] Generated ethash verification cache      epoch=1 elapsed=37m48.785s
INFO [04-23|17:20:03] Successfully sealed new block            number=1 hash=a4e420…4479e4
INFO [04-23|17:20:04] 🔨 mined potential block                  number=1 hash=a4e420…4479e4
INFO [04-23|17:20:04] Commit new mining work                   number=2 txs=0 uncles=0 elapsed=1.865ms

生成区块需要段时间,根据电脑配置,时间有长短 number=2 表示生成2个块。

查看帮助我们可以使用如下命令:sudo docker exec eth-private geth attach --exec eth

最后清空环境:

 #> sudo docker rm test
 #> sudo docker rmi test

开启挖矿使用 miner.start() 关闭挖矿 miner.stop(), 可以设置一个定时任务 crontab -e 每天09-22点10分钟跑一次:

*/10 9-22 * * * sudo /usr/bin/docker exec eth-private geth attach --exec 'miner.start()' && sleep 1 && sudo /usr/bin/docker exec eth-private geth attach --exec 'miner.stop()'

端口说明: 8545:用于rpc调用, 30303:用于以太区块链数据的广播.

一切 ok 完 (完)