使用Geth搭建POA私有链及注意事项
初始化私有链
创建工作目录
1 2
| mkdir -p ~/poa-chain cd ~/poa-chain
BASH
|
生成账户
1 2 3 4 5
| mkdir -p ~/chain cd ~/chain geth account new --datadir node1/data geth account new --datadir node2/data geth account new --datadir node3/data
BASH
|
生成引导节点私钥文件
1 2 3 4 5 6 7 8 9 10 11
| cd ~/poa-chain bootnode -genkey boot.key
bootnode -nodekey boot.key -verbosity 9 -addr :30311 > node1.json cat node1.json | grep enode
bootnode -nodekey boot.key -verbosity 9 -addr :30312 > node2.json cat node2.json | grep enode
bootnode -nodekey boot.key -verbosity 9 -addr :30313 > node3.json cat node3.json | grep enode
BASHELL
|
得到的encode值为:
enode://8f6e560cd26313cdfd784c7a1987dcf105272736135d2b0e1186968cea5b520aaa20dece0247a4e3a63a59fcc534c1d4ef2922abbe49c5fe52d6235396a11cc4@127.0.0.1?discport=30311
enode://8f6e560cd26313cdfd784c7a1987dcf105272736135d2b0e1186968cea5b520aaa20dece0247a4e3a63a59fcc534c1d4ef2922abbe49c5fe52d6235396a11cc4@127.0.0.1?discport=30312
enode://8f6e560cd26313cdfd784c7a1987dcf105272736135d2b0e1186968cea5b520aaa20dece0247a4e3a63a59fcc534c1d4ef2922abbe49c5fe52d6235396a11cc4@127.0.0.1?discport=30313
创建创世块文件
创建 genesis.json
文件:
1 2 3 4 5
| cd ~/chain vim genesis.json # 将下面的 genesis.json 复制粘贴 geth init --datadir node1/data genesis.json geth init --datadir node2/data genesis.json geth init --datadir node3/data genesis.json
BASHELL
|
注意: chainId
可以自行设定,genesis.json内容,其中extradata中的用户地址以及账户信息需要修改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| { "config": { "chainId": 12345, "homesteadBlock": 0, "eip150Block": 0, "eip155Block": 0, "eip158Block": 0, "byzantiumBlock": 0, "constantinopleBlock": 0, "petersburgBlock": 0, "istanbulBlock": 0, "berlinBlock": 0, "clique": { "period": 5, "epoch": 300 } }, "difficulty": "1", "gasLimit": "800000000", "extradata": "0x0000000000000000000000000000000000000000000000000000000000000000b2828A107C5dAe6e970d415085755505AE579BFf0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "alloc": { "0xb2828A107C5dAe6e970d415085755505AE579BFf": { "balance": "1000000000000000000000" }, "0xd8A4fE3E505a8B2b87b75CdA5AfFc9F26777F2bE": { "balance": "1000000000000000000000" }, "0x9C5554C7BA22a3Ec09e8e927E67988c817aE6A8B": { "balance": "1000000000000000000000" } } }
JSON
|
启动私有链
分别在node1、node2、node3中创建password.txt 文件,内容为创建节点地址时设置的密码,用于启动命令的签名;
1 2 3 4 5
| geth --datadir data --networkid 12345 --authrpc.port 8551 --http --http.port 8001 --http.api "eth,net,web3,personal,admin,miner" --bootnodes enode://f8ad8bf3b8c40ba6dbff2483885882efad4fa918a9084646e1bf7dea9b89341de2a4e9e76510be898625b5f3abc158bd868d873d33be96c8856fc11b40ef0b1a@127.0.0.1:0?discport=30311 --port 30311 --mine --unlock 0xb2828A107C5dAe6e970d415085755505AE579BFf --password password.txt --miner.etherbase 0xb2828A107C5dAe6e970d415085755505AE579BFf --allow-insecure-unlock --syncmode "full" console
geth --datadir data --networkid 12345 --authrpc.port 8552 --http --http.port 8002 --http.api "eth,net,web3,personal,admin,miner" --bootnodes enode://f8ad8bf3b8c40ba6dbff2483885882efad4fa918a9084646e1bf7dea9b89341de2a4e9e76510be898625b5f3abc158bd868d873d33be96c8856fc11b40ef0b1a@127.0.0.1:0?discport=30311 --port 30312 --mine --unlock 0xd8A4fE3E505a8B2b87b75CdA5AfFc9F26777F2bE --password password.txt --miner.etherbase 0xd8A4fE3E505a8B2b87b75CdA5AfFc9F26777F2bE --allow-insecure-unlock --syncmode "full" console
geth --datadir data --networkid 12345 --authrpc.port 8553 --http --http.port 8003 --http.api "eth,net,web3,personal,admin,miner" --bootnodes enode://f8ad8bf3b8c40ba6dbff2483885882efad4fa918a9084646e1bf7dea9b89341de2a4e9e76510be898625b5f3abc158bd868d873d33be96c8856fc11b40ef0b1a@127.0.0.1:0?discport=30311 --port 30313 --mine --unlock 0x9C5554C7BA22a3Ec09e8e927E67988c817aE6A8B --password password.txt --miner.etherbase 0x9C5554C7BA22a3Ec09e8e927E67988c817aE6A8B --allow-insecure-unlock --syncmode "full" console
BASH
|
参数说明:
--networkid
:私有链的 ID,需要与 genesis.json
中的 chainId
一致。
--http
:开启 HTTP-RPC。
--http.addr "0.0.0.0"
:监听所有地址。
--http.api "eth,net,web3,personal"
:开放 API。
--allow-insecure-unlock
:允许解锁账户。
--nodiscover
:关闭自动发现其他节点。
--unlock "0xYourGeneratedAddress"
:解锁用于挖矿的账户。
--password
:提供账户密码。
--mine
:启动挖矿。
添加到出块节点列表
clique.propose(“0xd8A4fE3E505a8B2b87b75CdA5AfFc9F26777F2bE”,true)
clique.propose(“0x9C5554C7BA22a3Ec09e8e927E67988c817aE6A8B”,true)
clique.getSigners()
连接 Geth 控制台
1
| geth attach node1/data/geth.ipc
BASH
|
进入控制台后,可以使用 JavaScript 命令交互,例如:
1 2
| eth.accounts eth.blockNumber
JS
|