本文阐述在一台windows电脑上,搭建基于PoA共识算法的以太坊私链(3个挖矿节点),步骤如下:
假设3个节点的数据目录分别是:
节点1 e:\work\test\1_poa_data
节点2 e:\work\test\2_poa_data
节点3 e:\work\test\3_poa_data\
分别运行下面的生成账号命令为3个节点生成3个矿工账号:
geth --datadir e:\work\test\1_poa_data\ account new
geth --datadir e:\work\test\2_poa_data\ account new
geth --datadir e:\work\test\3_poa_data\ account new
假设生成的3个矿工账号分别是:
0xe4697cBB8955526C703A4441f219cf0A04447a9B
0x815261DC4186502eC0D8CCFEf163785e1617b5A8
0x2E6C69224AC3F618b77c0c170dC26979be84b1dB
E:\work\test>puppeth
+-----------------------------------------------------------+
| Welcome to puppeth, your Ethereum private network manager |
| |
| This tool lets you create a new Ethereum network down to |
| the genesis block, bootnodes, miners and ethstats servers |
| without the hassle that it would normally entail. |
| |
| Puppeth uses SSH to dial in to remote servers, and builds |
| its network components out of Docker containers using the |
| docker-compose toolset. |
+-----------------------------------------------------------+
Please specify a network name to administer (no spaces or hyphens, please)
> 123
Sweet, you can set this via --network=123 next time!
[32mINFO [0m[07-03|16:09:55] Administering Ethereum network [32mname[0m=123
[33mWARN [0m[07-03|16:09:55] No previous configurations found [33mpath[0m=.puppeth\\123
What would you like to do? (default = stats)
1. Show network stats
2. Configure new genesis
3. Track new remote server
4. Deploy network components
> 2
Which consensus engine to use? (default = clique)
1. Ethash - proof-of-work
2. Clique - proof-of-authority
> 2
How many seconds should blocks take? (default = 15)
> 5
Which accounts are allowed to seal? (mandatory at least one)
> 0xe4697cBB8955526C703A4441f219cf0A04447a9B
> 0x815261DC4186502eC0D8CCFEf163785e1617b5A8
> 0x2E6C69224AC3F618b77c0c170dC26979be84b1dB
> 0x
Which accounts should be pre-funded? (advisable at least one)
> 0x2E6C69224AC3F618b77c0c170dC26979be84b1dB
> 0x
Specify your chain/network ID if you want an explicit one (default = random)
> 888
[32mINFO [0m[07-03|16:12:17] Configured new genesis block
What would you like to do? (default = stats)
1. Show network stats
2. Manage existing genesis
3. Track new remote server
4. Deploy network components
> 2
1. Modify existing fork rules
2. Export genesis configuration
3. Remove genesis configuration
> 2
Which file to save the genesis into? (default = 123.json)
> poa_123.json
[32mINFO [0m[07-03|16:13:15] Exported existing genesis block
What would you like to do? (default = stats)
1. Show network stats
2. Manage existing genesis
3. Track new remote server
4. Deploy network components
>
最后一步直接按Ctrl+C退出即可
节点1
geth --datadir e:\work\test\1_poa_data init e:\work\test\1_poa_data\poa_123.json
节点2
geth --datadir e:\work\test\2_poa_data init e:\work\test\2_poa_data\poa_123.json
节点3
geth --datadir e:\work\test\3_poa_data init e:\work\test\3_poa_data\poa_123.json
节点1
geth --datadir e:\work\test\1_poa_data --networkid 222 --rpc --rpcaddr 0.0.0.0 --rpcport 8545 --port 30303 --allow-insecure-unlock --unlock "0xe4697cBB8955526C703A4441f219cf0A04447a9B" --mine --miner.etherbase "0xe4697cBB8955526C703A4441f219cf0A04447a9B" --miner.threads 1 --rpccorsdomain="*" --rpccorsdomain="*" --rpcvhosts "192.168.1.1"
节点2
geth --datadir e:\work\test\2_poa_data --networkid 222 --rpc --rpcaddr 0.0.0.0 --rpcport 8546 --port 30304 --allow-insecure-unlock --unlock "0x815261DC4186502eC0D8CCFEf163785e1617b5A8" --mine --miner.etherbase "0x815261DC4186502eC0D8CCFEf163785e1617b5A8" --miner.threads 1 --rpccorsdomain="*" --rpccorsdomain="*" --rpcvhosts "192.168.1.1" --ipcpath e:\work\test\2_poa_data\geth.ipc
节点3
geth --datadir e:\work\test\3_poa_data --networkid 222 --rpc --rpcaddr 0.0.0.0 --rpcport 8547 --port 30305 --allow-insecure-unlock --unlock "0x2E6C69224AC3F618b77c0c170dC26979be84b1dB" --mine --miner.etherbase "0x2E6C69224AC3F618b77c0c170dC26979be84b1dB" --miner.threads 1 --rpccorsdomain="*" --rpccorsdomain="*" --rpcvhosts "192.168.1.1" --ipcpath e:\work\test\3_poa_data\geth.ipc
需要注意的地方:
a)因为3个节点都运行在同一台电脑上,所以每个节点的port和rpcport都要配置为不相同的,否则会造成启动节点时报错。
b)每个节点都需要生成geth.ipc。不通过如果 --ipcpath 参数来指定不同位置的geth.ipc,有可能3个节点缺省的geth.ipc文件都是同一个位置上的geth.ipc,从而造成3个节点对同一个geth.ipc文件的读写冲突,造成启动节点时报错
在步骤(4)中,3个节点启动后是互不相通的,因为大家都不知道对方的存在。可以通过使用命令admin.addPeer添加节点或添加配置静态节点文件这2种方式来添加节点。
由于这条私链配置了3个矿工,每个区块至少要经过一半以上的矿工确认后才能够写入区块链。通过添加其它的节点,自身节点才能和其它2个节点进行通信,完成区块的确认。
自身节点的peer信息可通过admin.nodeInfo命令获取。见命令结果中的红色部分
admin.nodeInfo
{
enode: “enode://30097bc3d33863483b111da76b88d9864e9125887ee6fd0c54520d1348a8aa5547940df8f623ecd7fb02765bf214fd4b59496ceda46231d9e5fd20ffe41af116@121.32.15.2:30303?discport=35403”,
enr: “enr:-KG4QE83sXBDE07D6-6ubF2__RqJxVAdbi0iOLhWtpTGdVZManjh1sC3x9kd69gxAkAQo8cpdmc9H5W7mIp7XqaIrSuCAV6DZXRox8aEMZMU8oCCaWSCdjSCaXCEeSCR8olzZWNwMjU2azGhAjAJe8PTOGNIOxEdp2uI2YZOkSWIfub9DFRSDRNIqKpVg3RjcIJ2X4N1ZHCCikuEdWRwNoJ2Xw”,
id: “dc590a024899bff1a9e4da35ab4f0313221f8b3ac27bca0c4674a77593055c1b”,
ip: “121.32.15.2”,
listenAddr: “[::]:30303”,
name: “Geth/v1.9.12-stable/windows-amd64/go1.14.1”,
ports: {
discovery: 35403,
listener: 30303
},
protocols: {
eth: {
config: {
byzantiumBlock: 4,
chainId: 222,
clique: {…},
eip150Block: 2,
eip150Hash: “0x0000000000000000000000000000000000000000000000000000000000000000”,
eip155Block: 3,
eip158Block: 3,
homesteadBlock: 1
},
difficulty: 41998,
genesis: “0xe2c25fa41550fc09adf645a73e4afc94b0a17650d0126bc1b85f1cc9c4dc381d”,
head: “0x47cb2ccd4882b55302b48d5e1916908cb4d47cd1b3c8f80ea4652b0e0abeea72”,
network: 888
}
}
}
a)使用命令admin.addPeer来添加节点
admin.addPeer(“enode://30097bc3d33863483b111da76b88d9864e9125887ee6fd0c54520d1348a8aa5547940df8f623ecd7fb02765bf214fd4b59496ceda46231d9e5fd20ffe41af116@127.0.0.1:30303”)
由于是在本机操作,可以把公网ip 121.32.15.2 替换为127.0.0.1
添加完节点后,可通过admin.peers命令来查看当前节点和其它哪些节点联系上
admin.peers
[{
caps: [“eth/63”, “eth/64”, “eth/65”],
enode: “enode://79d1c8e09ba615dfb104b8bcb9f6bdeab9ec4e81eaeb40b3f37c0ac8cafc902f083f23511cfc7576f75be8a463cb59a9a38e9eeb284a623b26cd7ea23dd28b95@127.0.0.1:62418”,
id: “617b2ec1edeee6ef48794e1b805bd362b65ad897d98387f876d212e598b5a61f”,
name: “Geth/v1.9.12-stable/windows-amd64/go1.14.1”,
network: {
inbound: true,
localAddress: “127.0.0.1:30303”,
remoteAddress: “127.0.0.1:62418”,
static: false,
trusted: false
},
protocols: {
eth: {
difficulty: 42254,
head: “0x3d366885c6fbce40a2b6056c20c2cbbe731b25b97b8ad5f59a4f4077cb429e77”,
version: 65
}
}
}]
b)使用添加配置静态节点文件来添加节点
静态节点配置文件名为static-nodes.json,内容格式如下:
[
“enode://30097bc3d33863483b111da76b88d9864e9125887ee6fd0c54520d1348a8aa5547940df8f623ecd7fb02765bf214fd4b59496ceda46231d9e5fd20ffe41af116@127.0.0.1:30303?discport=0”,
“enode://3c159695675054b63c006b88328692a167a696a2757556c388d1c458973610f61047645150f2e16fd579062144e5889ffa9b78d8f1352b5942f5660179c9b9f3@127.0.0.1:30304?discport=0”,
“enode://9b7d4f199df47d518156d86639fae5c5b313199981f0e761c45da3bee7956832e765201921e47e0035f43d9d6346b28d035078eb778e1e46999c02cd8b7c3f55@127.0.0.1:30305?discport=0”
]
在3个节点的data目录下放置static-nodes.json配置文件,重新启动节点后,自身节点就能和其它2个节点进行通信
由于配置了3个矿工,至少要有2个矿工正常运行才能产生区块。
我的csdn:https://blog.csdn.net/powervip
我的知乎: https://www.zhihu.com/people/powervip
我的腾讯微云网盘:https://share.weiyun.com/5qT0TvG
如果你觉得这篇文章写得还可以,请帮忙点个赞,谢谢!
你的鼓励,我的动力!