体验篇 - 部署以太坊私有链 (PoA)

本地部署一套以太坊私链,使用适用于联盟链的PoA共识
如果用默认的PoW共识,请看这篇: 体验篇 - 部署以太坊私链 (PoW)

(1)bootnode

在poa目录下建立三个文件夹,分别是bootdir,node1,node2三个文件夹。

mkdir poa
cd poa
mkdir bootdir node1 node2

在bootdir文件夹中创建bootnode节点用来做p2p网络路由。

./bootnode --genkey boot.key #创建名为boot.key的key文件。
./bootnode --nodekey boot.key #启动bootnode路由节点。

#回显信息如下, 之后启动node节点时会用到
UDP listener up                          self=enode://bde0739c7d9f213054575574d6e8e1df21147066a666bc939f29634dac947ad5c715a990d878264fe91a83c9fa0495911c824fe43329570c8d0510c8fa3243f2@[::]:30301

#或者 后台运行
nohup bootnode -nodekey boot.key -verbosity 9 -addr :30301 2>> bootnode_output.log &

(2)创建账户

在node1 node2 目录下创建账户 (密码 123456)

./geth account new
0xbac5564cc4f7528ebb6150270ee63b3fa1641b17
0x8a6673dc85e544bce6d4876f63cfcdc4e5c8cc08

(3)产生创世文件

使用puppeth来产生创世文件. puppeth是geth自带的程序,可以引导用户创建geth的创世文件。
运行puppeth在poa目录产生genesis.json文件

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)
> poa
 
Sweet, you can set this via --network=poa next time!
 
INFO [06-23|17:10:34] Administering Ethereum network           name=poa
INFO [06-23|17:10:34] No remote machines to gather stats from 
 
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)
> 2
 
Which accounts are allowed to seal? (mandatory at least one)
> 0xd71ad920f80e6a1e06689a720f24b335f22d557e
> 0x8bbc43acd355be0cecc61872e13e0a0e53c700b7
> 0x
 
Which accounts should be pre-funded? (advisable at least one)
> 0x0xd71ad920f80e6a1e06689a720f24b335f22d557e
ERROR[06-23|17:11:33] Invalid address length, please retry 
> 0xd71ad920f80e6a1e06689a720f24b335f22d557e
> 0x
 
Specify your chain/network ID if you want an explicit one (default = random)
> 1500
INFO [06-23|17:12:05] 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 = poa.json)
> genesis.json
INFO [06-23|17:12:27] 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

(4)初始化并启动节点

# Init node1 & node2
echo "Init node1 & node2."
geth --datadir node1 init genesis.json
geth --datadir node2 init genesis.json

sleep 1s

# Startup node1
echo "Startup node1."
nohup geth --datadir node1/ --syncmode 'full' --port 30311 --rpc --rpcaddr 0.0.0.0 --rpcport 8501 \
--rpcapi 'personal,db,eth,net,web3,txpool,miner' --rpccorsdomain "*" --ws --wsaddr 0.0.0.0 --wsport 8601 --wsapi 'personal,db,eth,net,web3,txpool,miner' --wsorigins "*" \
--bootnodes 'enode://bde0739c7d9f213054575574d6e8e1df21147066a666bc939f29634dac947ad5c715a990d878264fe91a83c9fa0495911c824fe43329570c8d0510c8fa3243f2@127.0.0.1:30301' \
--networkid 1515 --gasprice '1' -unlock '0xbac5564cc4f7528ebb6150270ee63b3fa1641b17' --password node1/password.txt --mine \
2>>node1/eth_output.log &

sleep 1s

# Startup node2
echo "Startup node2."
nohup geth --datadir node2/ --syncmode 'full' --port 30312 --rpc --rpcaddr 'localhost' --rpcport 8502 \
 --rpcapi 'personal,db,eth,net,web3,txpool,miner' --ws --wsaddr 0.0.0.0 --wsport 8602 --wsapi 'personal,db,eth,net,web3,txpool,miner' --wsorigins "*" \
 --bootnodes 'enode://bde0739c7d9f213054575574d6e8e1df21147066a666bc939f29634dac947ad5c715a990d878264fe91a83c9fa0495911c824fe43329570c8d0510c8fa3243f2@127.0.0.1:30301' \
 --networkid 1515 --gasprice '1' -unlock '0x8a6673dc85e544bce6d4876f63cfcdc4e5c8cc08' --password node2/password.txt --mine \
 2>>node2/eth_output.log &

# Jobs command
# jobs
# kill %1
# kill %2
# kill %3

踩坑记录

(1)启动节点挖矿时,需要带上标志–syncmode “full”,否则汇报错误:
Clique : Discarded bad propagated block #1 when syncing

(2)启动节点挖矿时,需要在启动时带上 --unlock “账号”,否则会在挖矿一定时间后会报挖矿失败,需要解锁的错误。

参考

  • http://www.cnblogs.com/xd502djj/p/3612131.html
  • https://blog.csdn.net/fengyifei11228/article/details/5737371
  • http://www.cnblogs.com/mfryf/archive/2012/05/09/2491322.html

往期精彩回顾:
区块链知识系列
密码学系列
零知识证明系列
共识系列
公链系列
比特币系列
以太坊系列
EOS系列
Filecoin系列
联盟链系列
Fabric系列
智能合约系列
Token系列

你可能感兴趣的:(体验篇,#,以太坊系列,联盟链系列,区块链,以太坊,ethereum,PoA,联盟链)