geth搭建私链

主要工作

参考go-ethereum官网的Private Networks文档搭建了一个私有链,并总结出几个脚本,可以半自动化地实现geth网络的搭建,脚本已上传至github仓库DLCCB

setup.sh

  • 这一步使用了以下几个工具
    • geth命令,用于生成初始两个节点的账户,使用创世块配置文件对两个账户进行初始化
    • puppeth 用于生成创世块的配置文件,这个命令是交互式的,编写了一个puppeth.txt作为其输入,默认生成一个基于pow的区块链
    • bootnode 用于生成启动bootnode
  • 这一步使用了以下几个linux命令
    • sed 非交互式的文本编辑器,用于读取生成的账户的区块链地址,写入puppeth.txt中,使得puppeth工具能为初始的两个节点分配一定的以太币
    • awk,用于对文本的处理
setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mkdir node1 node2
geth --datadir node1 account new < password.txt
geth --datadir node2 account new < password.txt

sed -i "5i$(cat node1/keystore/UTC--* | awk '{split($0, arr, "\""); print arr[4]}')" puppeth.txt
sed -i "5i$(cat node2/keystore/UTC--* | awk '{split($0, arr, "\""); print arr[4]}')" puppeth.txt
puppeth < puppeth.txt
sed -i "5d" puppeth.txt
sed -i "5d" puppeth.txt

geth init --datadir node1 tianer.json
geth init --datadir node2 tianer.json

cat password.txt | head -n 1 | tee node1/password.txt
cat password.txt | head -n 1 | tee node2/password.txt

bootnode -genkey boot.key
bootnode -nodekey boot.key -addr :30305

两个输入文件的内容

阅读更多