從零構建基於以太坊錢包 Parity 聯盟鏈

以太坊 錢包 技術 鏈聞研究院 2018-07-24

鏈聞 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 等。

大綱

  1. Parity 錢包下載安裝
  2. 設置 chain spec
  3. 設置兩個節點
  4. 設置賬號
  5. 啟動 Authority node
  6. 連接兩個節點
  7. 發送交易
  8. 分享給其他節點

一、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/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": "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, 按照步驟創建一個用戶賬號。

file
file
file
file
file
file
file
file
file

到目前為止我們已經完成 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 [email protected]:30300",
"id": 0
}

"enode://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f416f0e7e17fd10619a263c3
[email protected]:30300" 是 node0 的標識。下一步中我們將將它加入到 node1 中以實現兩個節點之間的連接。

2、第二步,將 node0 的 enode 加入 node1 ,Post 下列 JSONs 數據至 node1 (http://localhost:8541 )

{
"jsonrpc":"2.0",
"method":"parity_addReservedPeer",
"params":["enode://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f416f0e7e17fd10619
[email protected]: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。如下圖所示,賬號與賬號之間可以相互轉賬。



file
file
file

八、分享給其他節點

在開發時通常會將 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與我們互動!轉載請註明版權和原文鏈接!

相關推薦

推薦中...