PoA私链搭建
go-ethereum自带puppeth工具, 可以方便地部署支持PoA的以太坊私链,以本地部署2节点的以太坊私有链为例, 部署步骤如下:
- geth1.6版本后自带puppeth工具(编译geth时用
make all
) - 创建两个文件夹,用来保存2个节点数据, 创建后进入到testnet文件夹
testnet
|-- node1
|-- node2
- 创建2个账户作为signer,生成的2个地址保存为变量:
{ADDR1} 和 {ADDR2}
$ geth --datadir node1 account new
$ geth --datadir node2 account new
- 通过puppeth创建genesis文件
$ puppeth
Please specify a network name to administer (no spaces, please)
> testnet
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)
> 10
Which accounts are allowed to seal? (mandatory at least one)
(addresses from the account creation above, replace with your own)
> 0x{ADDR1}
> 0x{ADDR2}
> 0x<ENTER>
Which accounts should be pre-funded? (advisable at least one)
> 0x{ADDR1}
> 0x{ADDR2}
> 0x<ENTER>
Specify your chain/network ID if you want an explicit one (default = random)
> <ENTER>
Anything fun to embed into the genesis block? (max 32 bytes)
> <ENTER>
What would you like to do? (default = stats)
1. Show network stats
2. Save existing genesis
3. Track new remote server
4. Deploy network components
> 2
Which file to save the genesis into? (default = testnet.json)
> <ENTER>
What would you like to do? (default = stats)
1. Show network stats
2. Save existing genesis
3. Track new remote server
4. Deploy network components
> <CTRL-C>
- 启动节点, 在2个终端上分别执行:
serv1$ geth --datadir node1 init testnet.json
serv1$ geth --datadir node1 --port 3000 --syncmode "full" console
serv2$ geth --datadir node2 init testnet.json
serv2$ geth --datadir node2 --port 3002 --syncmode "full" console
启动后也可以通过attach到geth节点
console1$ geth attach ipc:node1/geth.ipc
console2$ geth attach ipc:node2/geth.ipc
- 连接2个节点
console2> admin.nodeInfo.enode
{ENODE-URL}
console1> admin.addPeer({ENODE-URL})
console1> admin.nodeInfo.enode
{ENODE-URL}
console2> admin.addPeer({ENODE-URL})
在客户端上 net.peerCount
应该可以看到数量是1
如果2个节点部署在2个主机上,那么ENODE-URL中的[::]需要替换为对应主机的IP地址
- 启动"挖矿"
> personal.unlockAccount(eth.coinbase)
> eth.defaultAccount = eth.coinbase
> miner.start()
一切顺利的话,就开始定时出块.
注意:
- 出块时间如果配置为0, 代表没有固定的出块时间, 只有当有新交易产生时才触发出块.