从无到有建立Ethereum联盟链

开发Ethereum Dapp的过程中,需要一个测试用的chain,之前是使用testrpc或直接连到Ethereum testnet (Ropsten),不过testrpc有各种坑需要处理,而Ropsten testnet则是需要同步与等待区块,相当的烦人。前几天看到Parity 1.5版以后支持建立Proof of Authority Chains,可以直接解决上述的问题,果然一试成主顾。

若你有下列困扰,可以建立自己测试用的PoA chain

  • 公司内网或无对外网路,无法同步区块
  • 降低测试时等待区块的时间
  • 不想碰到testrpc各种坑

PoA Chain特点有

  • 有别于PoW (Proof-of-Work)需要解数学难题来产生block,PoA是依靠预设好的Authority nodes,负责产生block。
  • 可依照需求设定Authority node数量。
  • 可指定产生block的时间,例如收到交易的5秒后产生block。
  • 一般的Ethereum node也可以连接到PoA Chain,正常发起transactions, contracts等。
  • 这篇教学文基本上是照著Parity Demo PoA 教程来撰写。

Parity是一个注重效能的Ethereum Client端软体

1. 安装Parity (1.5.0以上版本)

请自行到Parity官网安装,支持Ubutnu, OSX, Docker, Windows

2. 设定chain spec

PoA chain需要设定一个创世区块

{"name": "DemoPoA","engine": {"authorityRound": {"params": {"gasLimitBoundDivisor": "0x400","stepDuration": "5","validators" : {"list": []}}}},"params": {"maximumExtraDataSize": "0x20","minGasLimit": "0x1388","networkID" : "0x2323"},"genesis": {"seal": {"authorityRound": {"step": "0x0","signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}},"difficulty": "0x20000","gasLimit": "0x5B8D80"},"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

3. 设定两个node

此教学会在同一台机器上跑两个node,因此有些Parity原生的设定参数会有衝突,两个node需要分别设定不同值。

  • -d 指定储存资料与帐号的目录
  • --dport 指定Parity的network port,可用来让其他node连接
  • --jsonrpc-port 这是JSON RPC port,使用web3.js时会需要
  • ui-port Parity提供的Web-based UI port
  • dapps-port Parity Dapps使用的port

可以用下列指令启动Parity node

parity --chain demo-spec.json -d /tmp/parity0 --port 30300 --jsonrpc-port 8540 --ui-port 8180 --dapps-port 8080 --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 = "/tmp/parity0"
[network]
port = 30300
[rpc]
port = 8540
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
[ui]
port = 8180
[dapps]
port = 8080

node1 使用如下设定档 node1.toml

[parity]
chain = "demo-spec.json"
base_path = "/tmp/parity1"
[network]
port = 30301
[rpc]
port = 8541
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
[ui]
port = 8181
[dapps]
port = 8081

4. 设定帐号(Account)

我们总共要开3个帐号:2个Authority跟1个user帐号。

步骤1、 首先启动 node0 : parity --config node0.toml

接著开启网页 http://localhost:8180

我不知道怎么略过WELCOME操作,所以先跟著指示随便建立一个account,然后再删除XD

接著新增一个user account,使用Recover account from recovery phrase功能,为了示范的一致性,使用 user 当作pass phrase

选择第二项

输入user

完成User account

新增Authority account,一样使用Recover account from recovery phrase功能,为了示范的一致性,使用 node0 当作pass phrase

选择第二项

输入node0

最后要看到User与Node0 (Authority) account

这样就完成 node0 的帐号设定

  • Authority account: 0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e
  • User account: 0x004ec07d2329997267Ec62b4166639513386F32E

步骤2、 再来设定 node1 的帐号,启动 parity --config node1.toml ,步骤相同,连接到 http://localhost:8181 ,pass phrase使用 node1

启动Parity node1

输入node1

完成Authority account

这样就完成 node1 的帐号设定

  • Authority account: 0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2

步骤3、 将Authority account写入 demo-spec.json 档案

"validators" : {"list": ["0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e","0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2"]
}

再将user account加入accounts,并给一些balance,后续可以使用

"0x004ec07d2329997267Ec62b4166639513386F32E": { "balance": "10000000000000000000000" }

完成后的demo-spec.json如下

{"name": "DemoPoA","engine": {"authorityRound": {"params": {"gasLimitBoundDivisor": "0x400","stepDuration": "5","validators" : {"list": ["0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e","0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2"]}}}},"params": {"maximumExtraDataSize": "0x20","minGasLimit": "0x1388","networkID" : "0x2323"},"genesis": {"seal": {"authorityRound": {"step": "0x0","signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}},"difficulty": "0x20000","gasLimit": "0x5B8D80"},"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 } } } },"0x004ec07d2329997267Ec62b4166639513386F32E": { "balance": "10000000000000000000000" }}
}

5. 启动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 = "/tmp/parity0"
[network]
port = 30300
[rpc]
port = 8540
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
[ui]
port = 8180
[dapps]
port = 8080
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
reseal_on_txs = "none"

步骤3、node1.toml 也一样,如下

[parity]
chain = "demo-spec.json"
base_path = "/tmp/parity1"
[network]
port = 30301
[rpc]
port = 8541
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
[ui]
port = 8181
[dapps]
port = 8081
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2"
reseal_on_txs = "none"

步骤4、分别启动两个node

parity --config node0.toml

parity --config node1.toml

6、连接两个node

使用Postman透过JSON RPC来测试

步骤1、Post下列JSON资料至 http://localhost:8540 以取得 node0 的enode资料

{"jsonrpc":"2.0","method":"parity_enode","params":[],"id":0
}

取得enode连接资讯

步骤2、将 node0 的enode加入 node1 ,Post下列JSON资料至node1 (http://localhost:8541 )

{"jsonrpc":"2.0","method":"parity_addReservedPeer","params":["enode://6c4f53fc8536553c8f151516b7ee17f4b0719d21abe8fdd273588419cf467e3deafb414cd8efa331e4ad55fd7c2820a303a160895129e142a4306e7c3367d67c@172.20.160.80:30300"],"id":0
}

你的IP地址会不一样,172.20.160.80

成功加入

最后到 node1 的console画面,会看到 0/1/25 peers,就表示已经连接上。

7. 发送transaction

通过Parity提供的web-based UI可以很容易发送transaction,这边就不赘述了。

补充:分享给区网内其他人使用

在开发时通常会将node跑在server上,让其他人可以透过JSON RPC port连接上去使用,此时只要在config里面加入 [interface] 设定即可。

假设server ip为192.168.1.1,将 node0.toml 修改如下:

[parity]
chain = "demo-spec.json"
base_path = "/tmp/parity0"
[network]
port = 30300
[rpc]
port = 8540
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
interface = "192.168.1.1"
[ui]
port = 8180
[dapps]
port = 8080
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
reseal_on_txs = "none"

同样 node1.toml 修改如下:

[parity]
chain = "demo-spec.json"
base_path = "/tmp/parity1"
[network]
port = 30301
[rpc]
port = 8541
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
interface = "192.168.1.1"
[ui]
port = 8181
[dapps]
port = 8081
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2"
reseal_on_txs = "none"

使用 Parity 建立Proof-of-Authority (PoA) Ethereum Chain相关推荐

  1. 5分钟快速从无到有建立Ethereum联盟链

    5分钟快速从无到有建立Ethereum联盟链 开发Ethereum Dapp的过程中,需要一个测试用的chain,之前是使用testrpc或直接连到Ethereum testnet (Ropsten) ...

  2. 区块链中的共识机制以及共识算法

    什么是共识 共识,从字面上来看,是在某方面达成一致.打个比方,一个部门最近来了几位新同事,部门主管为了欢迎新同事的加入,以能让新同事融入到新环境,提议下班后聚个餐,对于聚餐了,大伙对此举双手赞成,毕竟 ...

  3. 使用Node.js部署智能合約(Smart Contract)

    從智能合約原始檔.編譯.部署,一氣呵成 我想大部分的人應該都是為了寫智能合約(Smart Contract)而選擇使用Ethereum,在開發應用程式(Dapp)時,若能透過程式碼自動部署智能合約,就 ...

  4. 《Performance Overhead of Atomic Crosschain Transactions》

    摘要: Abstract-Atomic Crosschain Transaction technology allows composable programming across permissio ...

  5. 主流区块链共识算法大全

    1. 引言 主要参考资料见: Lear With Whiteboard 2023年4月博客 All Major Blockchain Consensus Algorithms Explained 视频 ...

  6. Clique PoA consensus 建立Private chain

    Clique PoA consensus 建立Private chain Ethereum Proof of Authority Proof of Authority 是直接指定某些节点有记帐权,其他 ...

  7. 搭建以太坊私有链(PoA)--CentOS7.6 on 阿里云ECS、macOS Catalina on Mac、Ubuntu20.04 on Raspberry Pi 4B

    使用Geth搭建以太坊PoA私有链 搭建以太坊私有链(PoA) 1 阿里云ECS上的操作步骤 1.1 准备环境 1.2 安装Geth及Tools 1.3 创建以太坊私有链的数据文件夹 1.4 用pup ...

  8. 以太坊区块链同步_以太坊69:如何在10分钟内建立完全同步的区块链节点

    以太坊区块链同步 by Lukas Lukac 卢卡斯·卢卡奇(Lukas Lukac) Ethereu M 69:如何在10分钟内建立完全同步的区块链节点 (Ethereum 69: how to ...

  9. 重磅!美图技术团队发布开源 ethereum dpos 实现

    作者:美图技术团队 链接:https://zhuanlan.zhihu.com/p/38013479 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 导语:目前以太坊 ...

  10. 共识算法 —— PoA

    定义 PoA的全称是"Proof Of Authority" 权威证明,(网上有些文章全称写得是"Proof Of Activity",个人感觉明显不对,大家自 ...

最新文章

  1. AWS 中国宁夏和北京区正式上线 Amazon SageMaker,中国用户终于能用到新工具和功能!
  2. android绘制高亮区域,实现高亮某行的RecyclerView效果
  3. 【宜搭客户说】宜搭帮助教育局搭建教育管理平台
  4. 小球弹起次数及高度(python)
  5. 程序员造轮子的正确姿势
  6. 财经数据提取器上线拉
  7. python获取同音字
  8. 中国联通5G-NR 900MHz基站设备技术白皮书(2022)
  9. 【英语魔法俱乐部——读书笔记】 3 高级句型-简化从句倒装句(Reduced Clauses、Inverted Sentences) 【完结】...
  10. java中编写一个学生抽奖活动_用java做的抽奖程序
  11. 自己做量化交易软件(36)小白量化实战9--小白量化回测面板设计
  12. openshift开源_使用OpenShift Origin降低开源贡献的壁垒
  13. 最新web/java/jsp实现发送手机短信验证码和邮箱验证码的注册登录功能(详细)
  14. java 毕业设计源码-基于Vue助农扶贫电商平台ssm电子商务网站
  15. 求二叉树根节点到叶节点的所有路径
  16. 华为实习笔试2020.4.15
  17. Pygame学习笔记11:三角函数及Tank Battle游戏
  18. 考研数学公式Day2:对1/(a+bcosx)的积分
  19. macOS实现视频转音频以及音频拼接
  20. 5种常用翻译软件测试实验

热门文章

  1. 清华学霸尹成Python教程
  2. 如何用CSS实现div元素高度相对于整个屏幕100%
  3. ESP8266烧录选项中的QIO 和 DIO解释
  4. MacOS清理DNS缓存的终端代码推荐
  5. Python3之数据结构
  6. echarts饼图自动动画_echarts实时旋转饼图效果特效
  7. 2022-2028年中国新疆旅游行业发展动态及投资前景分析报告
  8. rows是横着的还是cols_rows和cols到底哪个是列哪个是行
  9. 基于安卓的高清语音技术亮相中国国际通信展览会
  10. 把Wordpress集成到zen-cart里方法 各种修改 经典机制