安装推荐方式,启用bootnode节点作为引导节点,使各节点进行正常P2P通讯,而在生产环境下,不推荐使用bootnode,而直接使用eth节点作为引导节点,
bootnode方式
基本环境
版本:
go-ethereum 1.10.17
环境:
windows/linux
网络:
如有三个主机节点,节点1、节点2 、节点3
进入主机节点1,创建必要文件,然后再向其他节点分发。
下载最新源码
进入go-ethereum目录
编译源码,需要程序bootnode、geth、puppeth三个程序:
go build -mod=mod -o geth -gcflags="all = -N -l" github.com/ethereum/go-ethereum/cmd/geth/ go build -mod=mod -o bootnode -gcflags="all = -N -l" github.com/ethereum/go-ethereum/cmd/bootnode/ go build -mod=mod -o puppeth -gcflags="all = -N -l" github.com/ethereum/go-ethereum/cmd/puppeth/
创建目录及生成秘钥
在私链根目录/xxx/privateEthereum下执行
mkdir bootdir mkdir boot1 mkdir boot2 bootnode --genkey bootdir/boot.key bootnode --nodekey=bootdir/boot.key
log其中显示enode信息,包含bootnode的id及ip端口
enode://7751fdcd42670376131f1f1855553d689d237edd294c03bf8598769c39b75bd6fcb6def09cfcec1e01bcbf06e592e8cc079c8f56a84f46fd9efe317e4de5df19@127.0.0.1:0?discport=30301
创建账户
分别创建两个节点的目录
cd boot1 geth --datadir ./ account new (password:123) 0x8f92c1a97ca9483ae580fab9d5fd35dfd723b638 cd boot2 geth --datadir ./ account new (password:123) 0x2cc31711590e729bf4bc38c42d1e9fe6ed82a3d4
在boot上级目录运行puppeth
直接运行“puppeth”用以生成创世配置模版:
此处,需要注意的是"which accounts are allowed to seal"设置项,若只设置1个账户,则该账户可以直接开始挖矿,若要加入其他账户,则需要在控制台使用命令clique.propose("待加入账户地址",true)进行激活,才能使用该新账户挖矿。
复制配置文件
cp 1818.json ./boot1/ cp 1818.json ./boot2/
两个区块链节点初始化文件
cd boot1/ geth -datadir boot1\ init 1818.json cd boot2/ geth -datadir boot2\ init 1818.json
先在主机节点1启动bootnode
(首次启动其他节点时需要,后面断掉就无所谓了)
bootnode --genkey=boot.key bootnode --nodekey=boot.key -verbosity 5
-verbosity参数为输出log日志等级, -verbosity 5,可以输出最详细的信息。
启动私链boot1区块链节点
windows环境下,节点1启动命令运行控制台:
geth --networkid 1818 --datadir boot1\ --ipcdisable --port 30303 --http --http.port 8545 --http.addr="0.0.0.0" --http.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --http.corsdomain "" --ws --ws.addr="0.0.0.0" --ws.port 8546 --ws.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --ws.origins "" --unlock 0 --password password.txt --bootnodes enode://7751fdcd42670376131f1f1855553d689d237edd294c03bf8598769c39b75bd6fcb6def09cfcec1e01bcbf06e592e8cc079c8f56a84f46fd9efe317e4de5df19@10.10.36.83:30301 --syncmode "full" --nousb --allow-insecure-unlock --mine --miner.threads=1 --miner.etherbase 0x8f92c1a97ca9483ae580fab9d5fd35dfd723b638 console
若是在linux下运行,启动命令:
./geth --networkid 1818 --datadir ./node1/ --ipcdisable --port 30303 --http --http.port 8545 --http.addr="0.0.0.0" --http.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --http.corsdomain "" --ws --ws.addr="0.0.0.0" --ws.port 8546 --ws.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --ws.origins "" --unlock 0 --password password.txt --bootnodes enode://d4fd1cdd857a19129c31cc6a1a5e3840ad720f2e3570a7641b5ecf50e82325bcafaa1f4abc67c151ea5c9ff02a4b6f444d4fc436779179d8b65e0148b133d4d6@172.16.3.61:30301 --syncmode "full" --nousb --allow-insecure-unlock --mine --miner.threads=1 --miner.etherbase a3a81a180131a90cffa90df5d9d50b486e731233 console
注意参数--datadir ./node1/ ,不同系统下斜杠或反斜杠的不同,否则识别不到数据目录,默认启动主网,会自动生成DAG,以及clique库缺少等问题,造成不必要麻烦。
也可以采用nohup后台启动挖矿的方式:
nohup "启动命令" >> run_nohup.log 2>&1 &
注意,使用“>>”追加日志的方式,可以方便后续进行日志清理
一种清理日志的方式:cat /dev/null > /path/.../run_nohup.log
nohup ./geth --networkid 1818 --datadir boot1\ --port 30303 --http --http.port 8545 --http.addr="0.0.0.0" --http.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --http.corsdomain "" --ws --ws.addr="0.0.0.0" --ws.port 8546 --ws.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --ws.origins "" --unlock 0 --password password.txt --bootnodes enode://7751fdcd42670376131f1f1855553d689d237edd294c03bf8598769c39b75bd6fcb6def09cfcec1e01bcbf06e592e8cc079c8f56a84f46fd9efe317e4de5df19@127.0.0.1:0?discport=30301 --syncmode "full" --nousb --allow-insecure-unlock --mine --miner.threads=1 --miner.etherbase 0x8f92c1a97ca9483ae580fab9d5fd35dfd723b638 > boot1/ethRunning.log 2>&1 &
注:
- --mine和--etherbase就能够在启动的同时进行挖矿,此时参数--miner.etherbas一定要指明,才知道收益矿工是谁,因为其默认为“0”;--password后面参数为密码文件的路径。
- 若没有./geth目录则重新初始化:
cd boot2/ geth --datadir .../privateEthereum/boot2/ init 1818.json
此时可看到,节点1已经开始出块。
启动私链boot2区块链节点
geth --networkid 1818 --datadir boot2\ --ipcdisable --port 40303 --http --http.port 9545 --http.addr="0.0.0.0" --http.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --http.corsdomain "" --ws --ws.addr="0.0.0.0" --ws.port 9546 --ws.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --ws.origins "" --unlock 0 --password password.txt --bootnodes enode://7751fdcd42670376131f1f1855553d689d237edd294c03bf8598769c39b75bd6fcb6def09cfcec1e01bcbf06e592e8cc079c8f56a84f46fd9efe317e4de5df19@10.10.36.83:30301 --syncmode "full" --nousb --allow-insecure-unlock --mine --miner.threads=1 --miner.etherbase 0x2cc31711590e729bf4bc38c42d1e9fe6ed82a3d4 console
通过nohup直接启动后台启动挖矿的方式:
nohup ./geth --networkid 1818 --datadir boot2\ --ipcdisable --port 40303 --http --http.port 9545 --http.addr="0.0.0.0" --http.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --http.corsdomain "" --ws --ws.addr="0.0.0.0" --ws.port 9546 --ws.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --ws.origins "" --unlock 0 --password password.txt --bootnodes enode://7751fdcd42670376131f1f1855553d689d237edd294c03bf8598769c39b75bd6fcb6def09cfcec1e01bcbf06e592e8cc079c8f56a84f46fd9efe317e4de5df19@10.10.36.83:30301 --syncmode "full" --nousb --allow-insecure-unlock --mine --miner.threads=1 --miner.etherbase 0x2cc31711590e729bf4bc38c42d1e9fe6ed82a3d4 >> boot2/ethRunning.log 2>&1 &
注意:
- 正常启动挖矿后,控制台命令“net”查看节点连接数
- 若不在同一主机节点中,修改enode后IP为真实地址
- 若账户余额显示为0,说明初始化未成功,初始化成功后即使不挖矿也可正常显示余额
- 若产生以下错误的解决思路:
WARN [08-14|13:33:20.770] System clock seems off by 2m45.277120101s, which can prevent network connectivity
WARN [08-14|13:33:20.770] Please enable network time synchronisation in system settings.
linux下可用如下方法解决:
timedatectl set-ntp true sudo systemctl start ntpd sudo systemctl enable ntpd
新增加区块链节点node3:
(1)创建keytore
cd boot3 geth --datadir ./ account new (password:123) 0x08aEF83902aFAFd1792FA56aDbF9167049fdCb67
(2) 初始化:
cd boot3/ geth --datadir .../privateEthereum/boot3/ init 1818.json
(3)在节点1启动前确保bootnode 处于启动
bootnode --nodekey=boot.key -verbosity 5
(4) 启动geth
geth --networkid 1818 --datadir boot3\ --ipcdisable --port 50303 --http --http.port 10545 --http.addr="0.0.0.0" --http.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --http.corsdomain "" --ws --ws.addr="0.0.0.0" --ws.port 10546 --ws.api "web3,personal,admin,net,eth,miner,rpc,txpool,clique" --ws.origins "" --unlock 0 --password password.txt --bootnodes enode://7751fdcd42670376131f1f1855553d689d237edd294c03bf8598769c39b75bd6fcb6def09cfcec1e01bcbf06e592e8cc079c8f56a84f46fd9efe317e4de5df19@10.10.36.83:30301 --syncmode "full" --nousb --allow-insecure-unlock --mine --miner.threads=1 --miner.etherbase 0x08aef83902afafd1792fa56adbf9167049fdcb67console
(5)提名挖矿
现在共有3个节点,所以需要一半以上的节点提名节点3的根账号,它才能被加入验证人列表。在boot1节点和boot2节点提名boot3的根账号:
clique.propose("0x08aef83902afafd1792fa56adbf9167049fdcb67",true)
注:
net显示peerCount为0,发现ntpd挂掉,使用:sudo systemctl restart ntpd,发现已成功连接其他节点,并开始同步区块。
无bootnode组网方式
该方式下,除不使用bootnode外,其余账户创建、数据目录初始化以及账户授权等步骤均与上面方式一样。
初始节点启动
geth --datadir .\ --networkid 1818 --ipcdisable --port 30303 --nat extip:10.10.36.83 --allow-insecure-unlock --unlock 0 --password password.txt --mine --miner.threads=1 --miner.etherbase 0x8f92c1a97ca9483ae580fab9d5fd35dfd723b638 console
如显示该初始节点enode为:
enode://d7a6686e3d56d1f72bbf2ea469a515fc8594f2b52db3204a7505e2e7471aeadef020a265af9a7dce72d989377d2cccdca2a0c9b8dd4e084f6ae1faf473a17913@10.10.36.83:30303
其他节点启动
--bootnodes后加入初始节点enode
geth --datadir .\ --networkid 1818 --ipcdisable --port 30305 --nat extip:10.10.36.83 --allow-insecure-unlock --unlock 0 --password password.txt --mine --miner.threads=1 --miner.etherbase 2cc31711590e729bf4bc38c42d1e9fe6ed82a3d4 --bootnodes "enode://d7a6686e3d56d1f72bbf2ea469a515fc8594f2b52db3204a7505e2e7471aeadef020a265af9a7dce72d989377d2cccdca2a0c9b8dd4e084f6ae1faf473a17913@10.10.36.83:30303" console
注意:
如无法发现p2p节点,看看是否是网络问题, 尤其在内网中测试部署时需注意,可使用--nat extip:<内网IP>。
如要metamask进行远程连接,需要注意两个geth参数,--http.corsdomain,--http.vhosts 限制外部访问的域名和主机名,不考虑安全性的前提下可全设置为"*",这样便可通过域名+端口的形式直接访问节点。