搭建以太坊POA联盟链

本文拟使用Ethereum搭建一个三个节点的联盟链,并采用POA共识方法进行共识。闲话不多说,马上开始:

首先购置三台服务器,我这边选择了阿里云服务器,镜像选择的是Ubuntu16.04。服务器配置与带宽请根据经济条件购置,一般来说1核2G已经够用。

首先安装go-ethereum:

sudo apt-get install software-properties-common

sudo add-apt-repository -y ppa:ethereum/ethereum

sudo apt-get update

sudo apt-get install ethereum

安装完成后可以用

geth version

来查看是否安装成功以及版本。

接着进入机器1的工作目录:

创建签名账户:

geth --datatdir node account new

创建金钱账户:

geth --datadir node account new

下面在机器2和3中同样创建签名账户(POA network需要至少两个签名账户),方法同上。

利用pupeth构建创世区块,执行命令:

pupeth

对应部分的问询分别是:网络名称、共识引擎、出块间隔、签名账户、金钱账户与网络ID。最后导出文件genesis.json,将genesis.json分别拷贝至其他两台机器对应目录中。

分别初始化创世区块:

geth --datadir node init genesis.json

接着利用geth启动区块链网络,对应项请参考geth的命令选项。机器1启动命令为:

geth --datadir node --maxpeers 3 --networkid '9585' --nodiscover --port '30300' --unlock 'd5c8c57d65146ebd3f200d013503409d81a6331e' --password 'node/password.txt' --mine --minerthreads=1 --etherbase=d5c8c57d65146ebd3f200d013503409d81a6331e

机器2和3的启动命令为:

geth --datadir node --maxpeers 3 --networkid '9585' --nodiscover --port '30300' --unlock 'ebed1888b8d4cdba4c13ae56166d7a26c8118a13' --password 'node/password.txt' --mine --minerthreads=1 --etherbase=ebed1888b8d4cdba4c13ae56166d7a26c8118a13 --rpc --rpcaddr '0.0.0.0' --rpcport '8500' --rpccorsdomain '*' --rpcapi 'personal,eth,net,web3'

账户名称自行替换。出于安全性考虑,金钱账户放在机器1中,不开放rpc port。

启动后进入console控制台:

geth attach ipc://root/testnode/node/geth.ipc

查看此节点的enode信息并记录。

使用以上方法对分别启动三台机器后配置static-nodes.json文件用于添加节点,配置完成后如下所示:

[

"enode://9d72cf660257bd9037e1b4c15e614e130a371e41554dac6706be5bccec7496cad43472ae2f1f8ada527a284ad017771b63c739f2ccf34eebd23216df2fd7b6b3@[172.31.118.88]:30300?discport=0",

"enode://d88372a5ee836c32194586bdb2cc902e9fffc104b4125c6d2f5c93abadd6f8befccfd9ded87ee7a00d1bbd4e74c1d36bcddd0360274052404afc9e28afc03397@[172.31.118.89]:30300?discport=0",

"enode://af2e355a232d7fe1a769fb7a2d4275257a4a87f8de4d8b7bf829ea3523edeabeda196d4c520402dd5e8deacc41ef52c45b26fd3dfa5c9382cd4f77b7b4c9d3d7@[172.31.118.90]:30300?discport=0"

]

将此文件分别放入三台机器的node/文件夹中。然后重新使用geth命令分别启动三个节点。

最后发现三台机器完美的运行了POA共识的联盟链网络,接下来我们可以使用各种方式使用区块链来构建自己的DAPP。

最后推荐一种方法使geth后台运行,利用nodejs的pm2工具,将geth命令放在shell中(如start-geth.sh),使用如下方式来快速启动后台运行的geth:

pm2 start start-geth.sh

并利用pm2的命令对进程进行有效的管理。

猜你喜欢

转载自www.cnblogs.com/tzone/p/9289722.html