Skip to content

Instantly share code, notes, and snippets.

@firefirer1983
Last active April 8, 2020 09:03
Show Gist options
  • Save firefirer1983/f8074163d84300e89fb9652b537c296e to your computer and use it in GitHub Desktop.
Save firefirer1983/f8074163d84300e89fb9652b537c296e to your computer and use it in GitHub Desktop.
eth私链搭建with docker

eth私链搭建with docker

获取geth image

docker pull ethereum/client-go:v1.8.12

运行geth

docker run -it --rm -v /home/xy/misc/workspace:/workspace --entrypoint /bin/sh ethereum/client-go:v1.8.12

运行geth(指定网络与ip)

docker run -it --rm -v /home/xy/misc/workspace/:/workspace --entrypoint /bin/sh ethereum/client-go:v1.8.12

创建账号4个账号(密码为123456),以写入创世块

准备工作:

docker run -it --rm -v /home/xy/misc/workspace/:/workspace --entrypoint /bin/sh ethereum/client-go:v1.8.12
mkdir -p /workspace/dapp/miner
mkdir -p /workspace/dapp/data

创建账号:

geth -datadir ./data account new
  1. 0x9dbc38e10909caca669ac65a8bcd1f6e29c35c0a
  2. 0xb42e6643c0dc6054caff3a3047a922a2dca7bf77
  3. 0x238b04890c39c0ca6d614e79b54636356d671f89
  4. 0x2e10cec992ca28426e75c300939b1d09195410fb

创建创世区块

alloc下面列举了4个账号及默认余额

/workspace/dapp/data/genesis.json

{
  "config": {
    "chainId": 88,
    "homesteadBlock": 0,
    "eip155Block": 0,
    "eip158Block": 0
  },
  "alloc"      : {
    "0x9dbc38e10909caca669ac65a8bcd1f6e29c35c0a": {"balance": "100000000000000000000"},
    "0xb42e6643c0dc6054caff3a3047a922a2dca7bf77": {"balance": "1000000000000000000"},
    "0x238b04890c39c0ca6d614e79b54636356d671f89": {"balance": "1000000000000000000"},
    "0x2e10cec992ca28426e75c300939b1d09195410fb": {"balance": "1000000000000000000"}
  },
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x400",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000000",
  "mixhash"    :
  "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" :
  "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

创建挖矿主节点

  1. 创建所有矿工的entrypoint脚本

该脚本的功能是让以太坊节点(容器)自动初始化以太坊网络,并且接受一个自动运行脚本作为输入。

/worksapce/dapp/init.sh
#!/bin/sh
geth -datadir ~/data/ init /workspace/dapp/data/genesis.json

if [  $# -lt 1 ]; then 
  exec "/bin/sh"
else
  exec /bin/sh -c "$@"
fi
  1. 创建主节点自动运行脚步
/workspace/dapp/mine.sh
#!/bin/sh
cp -r /workspace/dapp/miner/data/keystore/* ~/data/keystore/
geth -datadir ~/data/ --networkid 88 --rpc --rpcaddr="0.0.0.0" --rpcapi admin,eth,miner,web3,personal,net,txpool --unlock "0x9dbc38e10909caca669ac65a8bcd1f6e29c35c0a" --etherbase "0x9dbc38e10909caca669ac65a8bcd1f6e29c35c0a" console
  1. 创建主节点容器

该命令会创建一个持久化的容器。容器的entrypoint和自动运行脚本指定为我们刚创建的那两个脚本。

docker run -it --name=miner --hostname node -v /home/xy/misc/workspace:/workspace -v /home/xy/misc/workspace/dapp/master:/root/data/geth --entrypoint /workspace/dapp/init.sh ethereum/client-go:v1.8.12 /workspace/dapp/mine.sh

创建从矿工节点

  1. 从矿工节点启动脚本:
/workspace/dapp/node.sh
#!/bin/sh
cp -r /workspace/dapp/miner/data/keystore/* ~/data/keystore/
geth -datadir ~/data/ --networkid 88 console
  1. 启动从矿工
docker run -it --name=node1 --network ethnet --hostname node1 -v /home/xy/misc/workspace:/workspace -v /home/xy/misc/workspace/dapp/slave:/root/data/geth --entrypoint /workspace/dapp/init.sh ethereum/client-go:v1.8.12 /workspace/dapp/node.sh

测试节点RPC

curl 172.16.0.50:8545 -X POST --data '{"id":1,"jsonrpc":"2.0","method":"eth_accounts", "params":[]}' -H "Content-Type: application/json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment