爱币网创始人|从零构建基于以太坊钱包 Parity 联盟链
链闻 ChainNews:
本文先后介绍了联盟链的使用情况和特点,然后从大纲中列出的 8 个步骤从零起教,比较详细的介绍了如何构建 PoA chain。
来源 | 黎跃春区块链博客
作者 | 黎跃春,区块链、高可用架构工程师
什么情况下可以建立自己测试用的 PoA chain?
公司内网或无对外网络,无法同步区块
降低测试时等待区块的时间
不想碰到 testrpc 各种雷
PoA Chain 特点有
有别于 PoW 需要解数学难题来产生 block,PoA 是依靠预设好的 Authority nodes,负责产生 block。
可依照需求设定 Authority node 数量。
可指定产生 block 的时间,例如收到交易的 5 秒后产生 block。
一般的 Ethereum node 也可以连接到 PoA Chain,正常发起 transactions,contracts 等。
大纲
- Parity 钱包下载安装
- 设置 chain spec
- 设置两个节点
- 设置账号
- 启动 Authority node
- 连接两个节点
- 发送交易
- 分享给其他节点
一、Parity 钱包下载安装
之前的教程中我们讲解了 Mist 钱包、MetaMask、myetherwallet 钱包,这篇教程中,我们系统介绍一下 Parity 钱包的使用,为下一篇文章中联盟链搭建做铺垫。
Parity 钱包下载安装 https://parity.io。

如官网所示,The fastest and most secure way of interacting with the Ethereum blockchainParity 是最快最安全的钱包。
打开官网,我们看到有三种安装方式,第一种,直接下载安装,第二种,Brew 安装,第三种,Docker 安装。
在我们案例中,我们通过 Brew 来进行安装。
1、Getting Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2、Adding Parity to your list of Homebrew ‘kegs’
打开终端,输入下面的命令,按 enter。
brew tap paritytech/paritytech
3、Installing Parity
稳定版
brew install parity --stable
最新版
brew install parity
最新开发版
brew install parity --master
更新最新版本
brew update && brew upgrade parity
and
brew reinstall parity
4、查看安装版本
liyuechun:~ yuechunli$ parity --version
Parity
version Parity/v1.8.2-beta-1b6588c-20171025/x86_64-macos/rustc1.21.0
Copyright 2015, 2016, 2017 Parity Technologies (UK) Ltd
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.By Wood/Paronyan/Kotewicz/Drwi?ga/Volf
Habermeier/Czaban/Greeff/Gotchac/Redmannliyuechun:~ yuechunli$
二、设置 chain spec
PoA chain 需要设置一个创世区块。
{
"name": "DemoPoA",
"engine": {
"authorityRound": {
"params": {
"stepDuration": "5",
"validators": {
"list": [```
]
}
}
}```
},
"params": {
"gasLimitBoundDivisor": "0x0400",
"maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388",
"networkID": "0x2323"
},
"genesis": {
"seal": {
"authorityRound": {
"step": "0x0",
"signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000"
}
},
"difficulty": "0x20000",
"gasLimit": "0x5 B8D80"
},
"accounts": {
"0x0000000000000000000000000000000000000001": {
"balance": "1",
"builtin": {
"name": "ecrecover",
"pricing": {
"linear": {
"base": 3000,
"word": 0
}
}
}
},
"0x0000000000000000000000000000000000000002": {
"balance": "1",
"builtin": {
"name": "sha256",
"pricing": {
"linear": {
"base": 60,
"word": 12
}
}
}
},
"0x0000000000000000000000000000000000000003": {
"balance": "1",
"builtin": {
"name": "ripemd160",
"pricing": {
"linear": {
"base": 600,
"word": 120
}
}
}
},
"0x0000000000000000000000000000000000000004": {
"balance": "1",
"builtin": {
"name": "identity",
"pricing": {
"linear": {
"base": 15,
"word": 3
}
}
}
}
}
}
stepDuration 设定成 5 秒产生一个区块。
validators 设定 Authority 的地方,目前先空著,后面创建 account 之后再回来填入。
将上面的文件保存到桌面的一个文件中,保存为 demo-spec.json。
三、设置两个节点
在我们这篇文章中,我们在同一台电脑设置两个节点,跟我们讲解以太坊私网建立 (2) – 同一台电脑/不同电脑运行多个节点时,如果在同一台电脑设置两个节点,需要将 rpcport 和 port 设置为不同的值,否则就会发生冲突,POA chain 中也是一样,需要将一些参数设置为不同的值。
-d:指定存储资料与账号的目录
--dport:指定 Parity 的 network port,可用来让其他 node 连接
--jsonrpc-port:这是 JSON RPC port,使用 web3.js 时会需要
ui-port:Parity 提供的 Web-based UI port
可以用下列指令启动 Parity node。
parity --chain demo-spec.json -d parity0 --port 30300 --ui-port 8180 --jsonrpc-port 8540 --jsonrpc-apis web3\,eth\,net\,personal\,parity\,parity_set\,traces\,rpc\,parity_accounts
除了打一长串的指令外,Parity 也提供更为简洁的 config 档案设定方式,使用 --config 即可引用配置文件。
node0 使用如下配置文件 node0.toml:
[parity]
chain = "demo-spec.json"
base_path = "parity0"
[network]
port = 30300
[rpc]
port = 8540
apis = ["web3"\, "eth"\, "net"\, "personal"\, "parity"\, "parity_set"\, "traces"\, "rpc"\, "parity_accounts"]
[ui]
port = 8180
[websockets]
port = 8456
node1 使用如下配置文件 node1.toml:
[parity]
chain = "demo-spec.json"
base_path = "parity1"
[network]
port = 30301
[rpc]
port = 8541
apis = ["web3"\, "eth"\, "net"\, "personal"\, "parity"\, "parity_set"\, "traces"\, "rpc"\, "parity_accounts"]
[ui]
port = 8181
[websockets]
port = 8457
四、设置账号
我们总共需要设置三个账号,两个 Authority 和一个 user 账号。
** 第一步:** 首先启动 node0 : parity --config node0.toml

打开网页 http://localhost:8180, 按照步骤创建一个用户账号。









到目前为止我们已经完成 node0 的账号设置。
Authority account:0x00 Bd138aBD70e2F00903268F3Db08f2D25677C9e
User account:0x0064 B0999c0142eE99aB0ceC054 BAb53fe0a3EcC
第二步:设置 node1 的账号,启动 parity --config node1.toml。步骤相同,连接到 http://localhost:8181 ,pass phrase 使用 node1。


这样就完成了 node1 的账号设置。
Authority account:0x00F9 B30838ca40c8A53c672840acbDec6fCDb180
第三步:将 Authority account 写入 demo-spec.json 文件
"validators": {
"list": [
"0x00F9 B30838ca40c8A53c672840acbDec6fCDb180",
"0x00 Bd138aBD70e2F00903268F3Db08f2D25677C9e"
]
}
再将 user account 加入 accounts,並给一些 balance,后续可以使用。
"0x0064 B0999c0142eE99aB0ceC054 BAb53fe0a3EcC": {
"balance": "10000000000000000000000"
}
完成后的 demo-spec.json 如下:
{
"name": "DemoPoA",
"engine": {
"authorityRound": {
"params": {
"stepDuration": "5",
"validators": {
"list": [
"0x00F9 B30838ca40c8A53c672840acbDec6fCDb180",
"0x00 Bd138aBD70e2F00903268F3Db08f2D25677C9e"
]
}
}
}
},
"params": {
"gasLimitBoundDivisor": "0x0400",
"maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388",
"networkID": "0x2323"
},
"genesis": {
"seal": {
"authorityRound": {
"step": "0x0",
"signature": "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000"
}
},
"difficulty": "0x20000",
"gasLimit": "0x5 B8D80"
},
"accounts": {
"0x0000000000000000000000000000000000000001": {
"balance": "1",
"builtin": {
"name": "ecrecover",
"pricing": {
"linear": {
"base": 3000,
"word": 0
}
}
}
},
"0x0000000000000000000000000000000000000002": {
"balance": "1",
"builtin": {
"name": "sha256",
"pricing": {
"linear": {
"base": 60,
"word": 12
}
}
}
},
"0x0000000000000000000000000000000000000003": {
"balance": "1",
"builtin": {
"name": "ripemd160",
"pricing": {
"linear": {
"base": 600,
"word": 120
}
}
}
},
"0x0064 B0999c0142eE99aB0ceC054 BAb53fe0a3EcC": {
"balance": "10000000000000000000000"
},
"0x0000000000000000000000000000000000000004": {
"balance": "1",
"builtin": {
"name": "identity",
"pricing": {
"linear": {
"base": 15,
"word": 3
}
}
}
}
}
}
五、启动 Authority node
为了启动 Authority node 来产生区块,我们必须设定负责产生 block 的 signer,分別是 node0 和 node1 account。
1、第一步,创建一个 node.pwds 文件,写入 node0 与 node1 的 password,内容如下:
node0
node1
2、第二步,在 node0.toml 文件中加入 [account] 及 [mining] 设置,如下:
[parity]
chain = "demo-spec.json"
base_path = "parity0"
[network]
port = 30300
[rpc]
port = 8540
apis = ["web3"\, "eth"\, "net"\, "personal"\, "parity"\, "parity_set"\, "traces"\, "rpc"\, "parity_accounts"]
[ui]
port = 8180
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00 Bd138aBD70e2F00903268F3Db08f2D25677C9e"
reseal_on_txs = "none"
3、第三步,在 node1.toml 文件中加入 [account] 及 [mining] 设置,如下:
[parity]
chain = "demo-spec.json"
base_path = "parity1"
[network]
port = 30301
[rpc]
port = 8541
apis = ["web3"\, "eth"\, "net"\, "personal"\, "parity"\, "parity_set"\, "traces"\, "rpc"\, "parity_accounts"]
[ui]
port = 8181
[websockets]
port = 8457
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00F9 B30838ca40c8A53c672840acbDec6fCDb180"
reseal_on_txs = "none"
4、第四步,Step 4 分別启动两个 node
parity --config node0.toml
parity --config node1.toml
六、连接两个节点
使用 Postman 透过 JSON RPC 来测试。
1、第一步,Post 下列 JSON 数据至 http://localhost:8540 以取得 node0 的 enode 资料
{
"jsonrpc":"2.0",
"method":"parity_enode",
"params":[],
"id":0
}

获取到的数据如下:
{
"jsonrpc": "2.0",
"result": "enode://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f416f0e7e17fd10619a263c 360d9324fd2dcd8753c4500fcc54cf84e076b39cd6@192.168.10.101:30300",
"id": 0
}
"enode://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f416f0e7e17fd10619a263c3
60d9324fd2dcd8753c4500fcc54cf84e076b39cd6@192.168.10.101:30300" 是 node0 的标识。下一步中我们将将它加入到 node1 中以实现两个节点之间的连接。
2、第二步,将 node0 的 enode 加入 node1 ,Post 下列 JSONs 数据至 node1 (http://localhost:8541 )
{
"jsonrpc":"2.0",
"method":"parity_addReservedPeer",
"params":["enode://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f416f0e7e17fd10619
a263c360d9324fd2dcd8753c4500fcc54cf84e076b39cd6@192.168.10.101:30300"],
"id":0
}

返回的数据如下,result 为 true,说明连接成功:
{
"jsonrpc": "2.0",
"result": true,
"id": 0
}
再切换到 node1 的终端,会看到下面的数据:
1/25 peers 13 KiB chain 11 KiB db 0 bytes queue 10 KiB sync RPC: 0 conn, 0 req/s, 24 μs

如上图所示,表示连接成功。
七、发送交易
在我们这个案例中,我们一个创建了三个账号,一个用户账号,两个 POA 账号,刚开始的时候我们为用户账号初始化了 10000 ETH。如下图所示,账号与账号之间可以相互转账。





八、分享给其他节点
在开发时通常会将 node 跑在 server 上,让其他人可以通过 JSON RPC port 连接上去使用,此时只要在 config 文件里面加入 [interface] 设置即可。
假设 server ip 为 192.168.1.5,将 node0.toml 修改如下:
[parity]
chain = "demo-spec.json"
base_path = "parity0"
[network]
port = 30300
[rpc]
port = 8540
apis = ["web3"\, "eth"\, "net"\, "personal"\, "parity"\, "parity_set"\, "traces"\, "rpc"\, "parity_accounts"]
interface = "192.168.1.5"
[ui]
port = 8180
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00 Bd138aBD70e2F00903268F3Db08f2D25677C9e"
reseal_on_txs = "none"
node1.toml 修改如下:
[parity]
chain = "demo-spec.json"
base_path = "parity1"
[network]
port = 30301
[rpc]
port = 8541
apis = ["web3"\, "eth"\, "net"\, "personal"\, "parity"\, "parity_set"\, "traces"\, "rpc"\, "parity_accounts"]
interface = "192.168.1.5"
[ui]
port = 8181
[websockets]
port = 8457
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00F9 B30838ca40c8A53c672840acbDec6fCDb180"
reseal_on_txs = "none"
更多精彩内容,关注链闻 ChainNews 公众号(id:chainnewscom),或者来微博@ 链闻 ChainNews 与我们互动!转载请注明版权和原文链接!
- 免责声明
- 世链财经作为开放的信息发布平台,所有资讯仅代表作者个人观点,与世链财经无关。如文章、图片、音频或视频出现侵权、违规及其他不当言论,请提供相关材料,发送到:2785592653@qq.com。
- 风险提示:本站所提供的资讯不代表任何投资暗示。投资有风险,入市须谨慎。
- 世链粉丝群:提供最新热点新闻,空投糖果、红包等福利,微信:juu3644。

链闻研究院



