從零構建基於以太坊(Ethereum)錢包Parity聯盟鏈
什麼情況下可以建立自己測試用的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錢包下載安裝
- 設定chain spec
- 設定兩個節點
- 設定賬號(Account)
- 啟動Authority node
- 連線兩個節點
- 傳送交易
- 分享給其他節點
一、Parity錢包下載安裝
之前的教程中我們講解了Mist錢包、MetaMask、myetherwallet錢包,這篇教程中,我們系統介紹一下Parity錢包的使用,為下一篇文章中聯盟鏈搭建做鋪墊。
如官網所示,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/Redmann
liyuechun:~ 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": "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
。
三、設定兩個節點
在我們這篇文章中,我們在同一臺電腦設定兩個節點,跟我們講解以太坊私網建立 (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"]
cors = ["*"]
[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"]
cors = ["*"]
[ui]
port = 8181
[websockets]
port = 8457
四、設定賬號(Account)
我們總共需要設定三個賬號,兩個Authority
和一個user
賬號。
第一步:首先啟動node0
: parity --config node0.toml
- 新增
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
"validators": {
"list": [
"0x00F9B30838ca40c8A53c672840acbDec6fCDb180",
"0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
]
}
再將user account
加入accounts
,並給一些balance
,後續可以使用。
"0x0064B0999c0142eE99aB0ceC054BAb53fe0a3EcC": {
"balance": "10000000000000000000000"
}
完成後的demo-spec.json
如下:
{
"name": "DemoPoA",
"engine": {
"authorityRound": {
"params": {
"stepDuration": "5",
"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": "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
}
}
}
},
"0x0064B0999c0142eE99aB0ceC054BAb53fe0a3EcC": {
"balance": "10000000000000000000000"
},
"0x0000000000000000000000000000000000000004": {
"balance": "1",
"builtin": {
"name": "identity",
"pricing": {
"linear": {
"base": 15,
"word": 3
}
}
}
}
}
}
為了啟動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"]
cors = ["*"]
[ui]
port = 8180
[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 = 30301
[rpc]
port = 8541
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
cors = ["*"]
[ui]
port = 8181
[websockets]
port = 8457
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00F9B30838ca40c8A53c672840acbDec6fCDb180"
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://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f4[email protected]192.168.10.101:30300",
"id": 0
}
"enode://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f4[email protected]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://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f4[email protected]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"]
cors = ["*"]
interface = "192.168.1.5"
[ui]
port = 8180
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
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"
cors = ["*"]
[ui]
port = 8181
[websockets]
port = 8457
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00F9B30838ca40c8A53c672840acbDec6fCDb180"
reseal_on_txs = "none"
技術交流
區塊鏈技術交流QQ群:348924182
「區塊鏈部落」官方公眾號