本文参考自:https://hackernoon.com/setup-your-own-private-proof-of-authority-ethereum-network-with-geth-9a0a3750cda8(记录一下方便以后看,文章有省略,具体看原文)
1.创建目录
mkdir devnet
cd devnet
mkdir node1 node2
2.创建账户
geth --datadir node1/ account new
geth --datadir node2/account new
把两个节点的地址和密码都分别写在文件里,以后后面使用.
echo 'node1_addr' >> account.txt
echo 'node2_addr' >> account.txt
echo 'password1' >> node1/password.txt
echo 'password2' >> node2/password.txt
3.创建genesis.json文件
使用geth自带的工具puppeth来创建:
devnet$ puppeth
根据提示填写信息(后面操作中可以更改里面的信息)
Please specify a network name to administer (no spaces, please)
> devnet
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 // for example
Which accounts are allowed to seal? (mandatory at least one)
> 0x87366ef81db496edd0ea2055ca605e8686eec1e6 //copy paste from account.txt :)
> 0x08a58f09194e403d02a1928a7bf78646cfc260b0
Which accounts should be pre-funded? (advisable at least one)
> 0x87366ef81db496edd0ea2055ca605e8686eec1e6 // free ethers !
> 0x08a58f09194e403d02a1928a7bf78646cfc260b0
Specify your chain/network ID if you want an explicit one (default = random)
> 1515 // for example. Do not use anything from 1 to 10
Anything fun to embed into the genesis block? (max 32 bytes)
>
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
> 2
Which file to save the genesis into? (default = devnet.json)
> genesis.json
INFO [01-23|15:16:17] 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
> ^C // ctrl+C to quit puppeth
注:PoA是没有挖矿奖励的,所以在初始化的时候要给账户存一些余额.
4.初始化节点
geth datadir node1/ init genesis.json
geth datadir node2/ init genesis.json
5.创建一个bootnode
bootnode的作用是路由功能,可以发现网络中的节点.
初始化:
devnet$ bootnode -genkey boot.key
6.开始运行bootnode服务:
devnet$ bootnode -nodekey boot.key -verbosity 9 -addr :30310
INFO [02-07|22:44:09] UDP listener up self=enode://3ec4fef2d726c2c01f16f0a0030f15dd5a81e274067af2b2157cafbf76aa79fa9c0be52c6664e80cc5b08162ede53279bd70ee10d024fe86613b0b09e1106c40@[::]:30310
7.运行节点:
启动node1:
devnet$ geth --datadir node1/ --syncmode 'full' --port 30311 --rpc --rpcaddr 'localhost' --rpcport 8501 --rpcapi 'personal,db,eth,net,web3,txpool,miner,net' --bootnodes 'enode://3ec4fef2d726c2c01f16f0a0030f15dd5a81e274067af2b2157cafbf76aa79fa9c0be52c6664e80cc5b08162ede53279bd70ee10d024fe86613b0b09e1106c40@127.0.0.1:30310' --networkid 1515 --gasprice '1' -unlock '0x87366ef81db496edd0ea2055ca605e8686eec1e6' --password node1/password.txt --mine
启动node2:
devnet$ geth --datadir node2/ --syncmode 'full' --port 30312 --rpc --rpcaddr 'localhost' --rpcport 8502 --rpcapi 'personal,db,eth,net,web3,txpool,miner,net' --bootnodes 'enode://3ec4fef2d726c2c01f16f0a0030f15dd5a81e274067af2b2157cafbf76aa79fa9c0be52c6664e80cc5b08162ede53279bd70ee10d024fe86613b0b09e1106c40@127.0.0.1:30310' --networkid 1515 --gasprice '0' --unlock '0x08a58f09194e403d02a1928a7bf78646cfc260b0' --password node2/password.txt --mine
(具体参数含义见原文,还是挺详细的)
bootnode, node1, node2的运行截图分别如下(从原文直接粘过来的,在我的电脑也运行成功了,界面差不多)
到此为止,已经可以启动了PoA共识的私有链.原文中还有一些后续的操作和扩展,具体参见原文吧.(另,膜拜原文大神)
----------------------------------------------------------------------------------------------------------------------------------
(如果只是想搭建一个私有链或者联盟链,不想使用以太坊pow挖矿的话,可以使用quorum(摩根大通在修改以太坊某些特性,做成一个联盟链),可以看一下这一篇