从零构建以太坊(Ethereum)智能合约到项目实战——第21章 搭建联盟链
P78 、1-内容介绍
什么情况下建立自己测试用的PoA chain?
- 公司内网或无对外网络,无法同步区块
- 降低测试时等待区块的时间
- 不想碰到testrpc各种雷
PoA chain特点有
- 有别于PoW(Proof-of-Work)需要解数学难题来产生block,PoA是依靠预设好的Authority nodes,负责产生block。
- 可依照需求设定Authority nodes数量。
- 可指定产生block的时间,例如收到交易的5秒后产生block。
- 一般的Ethereum node也可以连接到PoA chain,正常发起transactions,contracts等。
大纲
1、Parity钱包下载安装
2、设置chain spec
3、设置两个节点
4、设置账号(Account)
5、启动Authority node
6、连接两个节点
7、发送交易
8、分享给其它节点
P79 、2-Parity钱包下载安装
钱包有:Mist钱包、Metamask、myetherwallet钱包。
Parity钱包下载安装:https://www.parity.io/ethereum/
https://github.com/paritytech/parity-ethereum/releases/tag/v2.2.9
ubuntu安装parit教程:https://github.com/paritytech/parity-ethereum
https://wiki.parity.io/Setup
后来摸索着步骤:
1、ubuntu安装Homebrew:https://blog.csdn.net/Cocoa_vip/article/details/74395285
步骤:
将如下代码粘贴到终端,如果没有安装ruby,使用apt-get install ruby安装:
ruby -e "$(wget -O- https://raw.github.com/Homebrew/linuxbrew/go/install)"
2、增加parity到你的Homebrew ‘keys’中,首先ubuntu安装好 linuxbrew-wrapper,
apt install linuxbrew-wrapper
brew tap paritytech/paritytech
3、安装parity
- 稳定版
brew install parity --stable
- 最新版(推荐)
brew install parity
- 最新开发板
brew install parity --master
- 更新最新版本
brew update && brew upgrade parity
或者
brew reinstall parity
4、查看安装版本
parity --version
https://www.cnblogs.com/sumingk/articles/9097996.html
P80 、3-demo-spec 配置文件
PoA chain 需要设置一个创世区块。
{
"name": "DemoPoA",
"engine": {
"authorityRound": {
"params": {
"stepDuration": "",
"validators": {
"list": [ ]
}
}
}
},
"params": {
"gasLimitBoundDivisor": "0x0400",
"maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388",
"networkID": "0x2323"
},
"genesis": {
"seal": {
"authorityRound": {
"step": "0x0",
"signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}
},
"difficulty": "0x20000",
"gasLimit": "0x5B8D80"
},
"accounts": {
"0x0000000000000000000000000000000000000001": {
"balance": "",
"builtin": {
"name": "ecrecover",
"pricing": {
"linear": {
"base": ,
"word":
}
}
}
},
"0x0000000000000000000000000000000000000002": {
"balance": "",
"builtin": {
"name": "sha256",
"pricing": {
"linear": {
"base": ,
"word":
}
}
}
},
"0x0000000000000000000000000000000000000003": {
"balance": "",
"builtin": {
"name": "ripemd160",
"pricing": {
"linear": {
"base": ,
"word":
}
}
}
},
"0x0000000000000000000000000000000000000004": {
"balance": "",
"builtin": {
"name": "identity",
"pricing": {
"linear": {
"base": ,
"word":
}
}
}
}
}
}
- stepDuration 设定成5秒产生一个区块。
- validators 设定 Authority 的地方,目前先空着,后面创建 account 之后再回来填入。
将上面的文件保存到桌面的一个文件中,保存为demo-spec.json。
P81 、4-POA Node创建的两种方法
我们在同一台电脑设置两个节点,在私链建立中,如果在同一台电脑设置两个节点,需要将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 --ui-port --jsonrpc-port --jsonrpc-apis web3,eth,net,personal,parity,parity_set,traces,rpc,parity_accounts
浏览器输入:
localhost:
访问联盟链的UI界面。
除了打一长串的指令外,Parity也提供更为简洁的config档案设定方式,使用--config即可引用配置文件。
- node0 使用如下配置 node0.toml:
[parity]
chain = "demo-spec.json"
base_path = "parity0"
[network]
port =
[rpc]
port =
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
cors = ["*"]
[ui]
port =
[websockets]
port =
- node1 使用如下配置 node1.toml:
[parity]
chain = "demo-spec.json"
base_path = "parity1"
[network]
port =
[rpc]
port =
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
cors = ["*"]
[ui]
port =
[websockets]
port =
P82 、5-node0 & node1 配置以及账号创建
我们总共需要设置三个账号,两个Authority 和一个 user 账号。
第一步:首先启动node0
: parity --config node0.toml
打开网页http://localhost:8180,按照步骤创建一个用户账号。
- 新增
Authority account
,使用Restore
功能,为了示范一致性,我们使用node0
当作pass phrase
。
到目前为止我们已经完成node0
的账号设置。
- Authority account:
0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e
- User account:
0x0064B0999c0142eE99aB0ceC054BAb53fe0a3EcC
第二步:设置node1
的账号,启动parity --config node1.toml
。步骤相同,连接到 http://localhost:8181 ,pass phrase
使用 node1
。
这样就完成了node1
的账号设置。
- Authority account:
0x00F9B30838ca40c8A53c672840acbDec6fCDb180
第三步:将Authority account 写入 demo-spec.json 文件
"validators": {
"list": [
"0x00F9B30838ca40c8A53c672840acbDec6fCDb180",
"0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
]
}
再将user account
加入accounts
,並给一些balance
,后续可以使用。
"0x0064B0999c0142eE99aB0ceC054BAb53fe0a3EcC": {
"balance": ""
}
完成后的demo-spec.json
如下:
{
"name": "DemoPoA",
"engine": {
"authorityRound": {
"params": {
"stepDuration": "",
"validators": {
"list": [
"0x00F9B30838ca40c8A53c672840acbDec6fCDb180",
"0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
]
}
}
}
},
"params": {
"gasLimitBoundDivisor": "0x0400",
"maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388",
"networkID": "0x2323"
},
"genesis": {
"seal": {
"authorityRound": {
"step": "0x0",
"signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}
},
"difficulty": "0x20000",
"gasLimit": "0x5B8D80"
},
"accounts": {
"0x0000000000000000000000000000000000000001": {
"balance": "",
"builtin": {
"name": "ecrecover",
"pricing": {
"linear": {
"base": ,
"word":
}
}
}
},
"0x0000000000000000000000000000000000000002": {
"balance": "",
"builtin": {
"name": "sha256",
"pricing": {
"linear": {
"base": ,
"word":
}
}
}
},
"0x0000000000000000000000000000000000000003": {
"balance": "",
"builtin": {
"name": "ripemd160",
"pricing": {
"linear": {
"base": ,
"word":
}
}
}
},
"0x0064B0999c0142eE99aB0ceC054BAb53fe0a3EcC": {
"balance": "10000000000000000000000"
},
"0x0000000000000000000000000000000000000004": {
"balance": "",
"builtin": {
"name": "identity",
"pricing": {
"linear": {
"base": ,
"word":
}
}
}
}
}
}
五、启动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 =
[rpc]
port =
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
cors = ["*"]
[ui]
port =
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
reseal_on_txs = "none"
3、第三步,在node1.toml文件中加入[account]及[mining]设置,如下:
[parity]
chain = "demo-spec.json"
base_path = "parity1"
[network]
port =
[rpc]
port =
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
cors = ["*"]
[ui]
port =
[websockets]
port =
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00F9B30838ca40c8A53c672840acbDec6fCDb180"
reseal_on_txs = "none"
4、第四步,Step 4 分別启动两个node
parity --config node0.toml
parity --config node1.toml
P83 、6-多节点连接、交易、互通
六、连接两个节点
使用Postman
透过JSON RPC
来测试。
1、第一步,Post下列JSON数据至 http://localhost:8540 以取得 node0 的enode资料
{
"jsonrpc":"2.0",
"method":"parity_enode",
"params":[],
"id":
}
获取到的数据如下:
{
"jsonrpc": "2.0",
"result": "enode://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f416f0e7e17fd10619a263c360d9324fd2dcd8753c4500fcc54cf84e076b39cd6@192.168.10.101:30300",
"id":
}
"enode://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f416f0e7e17fd10619a263c360d9324fd2dcd8753c4500fcc54cf84e076b39cd6@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://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f416f0e7e17fd10619a263c360d9324fd2dcd8753c4500fcc54cf84e076b39cd6@192.168.10.101:30300"],
"id":
}
返回的数据如下,result
为true,说明连接成功:
{
"jsonrpc": "2.0",
"result": true,
"id":
}
再切换到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 =
[rpc]
port =
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
interface = "192.168.1.5"
[ui]
port =
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
reseal_on_txs = "none"
node1.toml
修改如下:
[parity]
chain = "demo-spec.json"
base_path = "parity1"
[network]
port =
[rpc]
port =
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
interface = "192.168.1.5"
[ui]
port =
[websockets]
port =
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00F9B30838ca40c8A53c672840acbDec6fCDb180"
reseal_on_txs = "none"
从零构建以太坊(Ethereum)智能合约到项目实战——第21章 搭建联盟链的更多相关文章
- 从零构建以太坊(Ethereum)智能合约到项目实战——第20章 搭建自己的私有链网络
P75 .1-以太坊私网建立 .合约编译.部署完全教程(1) 使用此博文进行安装配置:https://blog.csdn.net/w88193363/article/details/79402074 ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——第24章 IPFS + 区块链
P93 .1-IPFS环境配置P94 .2-IPFS+P .IPNS+P .个人博客搭建 - 如何在IPFS新增一个文件P95 .3-IPFS+P .IPNS+P .个人博客搭建 - 通过ipfs创建 ...
- 区块链入门到实战(27)之以太坊(Ethereum) – 智能合约开发
智能合约的优点 与传统合同相比,智能合约有一些显著优点: 不需要中间人 费用低 代码就是规则 区块链网络中有多个备份,不用担心丢失 避免人工错误 无需信任,就可履行协议 匿名履行协议 以太坊(Ethe ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——第23章 从零构建和部署去中心化投票App,decentralization Voting Dapp
P90 .1-从零构建和部署去中心化投票App-01 P91 .2-从零构建和部署去中心化投票App-02 P92 .3-从零构建和部署去中心化投票App-03 参考博文:http://liyuech ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——第22章 玩转truffle framework 、Web3.js 框架
P84 .1-玩转truffle framework.Web3.js 框架 内容介绍 truffle官方网站:https://truffleframework.com/ P85 .2-truffle ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——第25章 Embark FrameWork
P109 .1-Embark Framework 开发入门篇P110 .2-Embark Framework 去中心化存储 (IPFS)
- 零门槛,包教会。让你在5分钟内使用以太坊ERC20智能合约发行属于自己的空气币
前言 目前区块链是互联网中最最火的风口,没有之一.我周围的很多朋友也加入了“炒币”行列,但很不幸,几乎都被“割韭菜”了.而经过我的几天研究,发现,如果自己要发行一种空气币,简直太简单了.只需要下面几个 ...
- 以太坊solidity智能合约-生成随机数
Solidity随机数生成 在以太坊的只能合约中,没有提供像其他面向对象编程一样的生成随机数的工具类或方法.其实,所谓的随机数也是伪随机的,没有哪一种语言能够真正的生成随机数. 对于solidity来 ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——学习笔记10
P57 .1-Solidity Types - 玩转 Solidity 数组 (Arrays) 学习目标 1.掌握Arrays的可变不可变的创建 2.深度理解可变数组和不可变数组之间的区别 3.二维数 ...
随机推荐
- 优化mysql
数据库设计和表创建时就要考虑性能 sql的编写需要注意优化 分区 分表 分库 1.数据库设计和表创建时就要考虑性能 mysql数据库本身高度灵活,造成性能不足,严重依赖开发人员能力.也就是说开发人员能 ...
- 67课 for循环1-为什么需要for循环
# include <stdio.h> int main (void) { int i; ;.//sum代表和的意思 ; i<=; ++i)//第八行代码 sum = sum + i ...
- 金中宝POS
金中宝POS机刷卡到账时间:9:00-22:00 现代金控自选商户POS机注意事项 选择: 1 消费 T+1到账(下一个工作日到账) 选择: 2 订单支付 是实时到账! 一.金中宝POS机秒到时间:2 ...
- CSS - div居中在屏幕中(水平居中 + 垂直居中)
方法一代码 <div> <h1>404 Not Found.</h1> </div> <style> div { text-align: c ...
- Java web 会话技术 cookie与session
一.会话 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 会话过程中要解决的一些问题 每个用户在使用浏览器与服务器进行会话的过程 ...
- java 生成/解读 二维码
package com.rails.util; import com.swetake.util.Qrcode; import jp.sourceforge.qrcode.QRCodeDecoder; ...
- 【IO多路复用】
" 目录 一.IO模型介绍 二.阻塞IO(blocking IO) 三.非阻塞IO(non-blocking IO) 四.多路复用IO(IO multiplexing) 五.异步IO(Asy ...
- 巧用命令行工具 redis-cli
我们天天都在使用 Redis 内置的命令行工具 redis-cli,久而久之以为它就是一个简单的交互式 Redis 数据结构手工操作程序,但是它背后强大的功能绝大多数同学可能闻所未闻.本节我们一起来挖 ...
- iOS 实现 摇一摇
摇一摇功能:调用了系统自带加速器,当设备摇动时,系统会 计算出加速器的加速值,然后告诉设备是否发生摇动手势,系统只会运动开始和结束时通知你,并不会在运动发生的整个过程中始终向你报告每一次运动.例如,你 ...
- fiddler的代理设置
fiddler通过监听系统的8888端口实现对系统浏览器发出的http报文进行截获监听,因此要使fiddler能够监听到浏览器数据,需要对浏览器设置代理端口 浏览器 <--8888端口代理--& ...