參考:http://www.8btc.com/ethereum-parity
https://wiki.parity.io/Demo-PoA-tutorial
什么情況下可以建立自己測試用的PoA chain?
- 公司內(nèi)網(wǎng)或無對外網(wǎng)絡,無法同步區(qū)塊
- 降低測試時等待區(qū)塊的時間
- 不想碰到testrpc各種雷
Parity POA官方文檔:https://wiki.parity.io/Demo-PoA-tutorial
Parity UI:https://wiki.parity.io/Parity-Wallet
PoA Chain特點有
- 有別于PoW(Proof-of-Work)需要解數(shù)學難題來產(chǎn)生block主胧,PoA是依靠預設好的Authority nodes兜叨,負責產(chǎn)生block航闺。
- 可依照需求設定Authority node數(shù)量。
- 可指定產(chǎn)生block的時間,例如收到交易的5秒后產(chǎn)生block。
- 一般的Ethereum node也可以連接到PoA Chain琳状,正常發(fā)起transactions,contracts等盒齿。
大綱
- Parity錢包下載安裝
- 設置chain spec
- 設置兩個節(jié)點
- 設置賬號(Account)
- 啟動Authority node
- 連接兩個節(jié)點
- 發(fā)送交易
- 分享給其他節(jié)點
一念逞、Parity錢包下載安裝
之前的教程中我們講解了Mist錢包困食、MetaMask、myetherwallet錢包翎承,這篇教程中硕盹,我們系統(tǒng)介紹一下Parity錢包的使用,為下一篇文章中聯(lián)盟鏈搭建做鋪墊审洞。
Parity錢包下載安裝https://parity.io莱睁。
安裝Parity各系統(tǒng)參考:https://github.com/paritytech/parity-ethereum
例:Mac下通過Homebrew來安裝。
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
穩(wěn)定版
brew install parity —stable
- 最新版
brew install parity - 最新開發(fā)版
brew install parity —master - 更新最新版本
brew update && brew upgrade parity
and
brew reinstall parity
4誊酌、查看安裝版本
parity --version
二、設置chain spec
{
"name": "DemoPoA",
"engine": {
"authorityRound": {
"params": {
"stepDuration": "5",
"validators" : {
"list": []
}
}
}
},
"params": {
"gasLimitBoundDivisor": "0x400",
"maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388",
"networkID" : "0x2323",
"eip155Transition": 0,
"validateChainIdTransition": 0,
"eip140Transition": 0,
"eip211Transition": 0,
"eip214Transition": 0,
"eip658Transition": 0
},
"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秒產(chǎn)生一個區(qū)塊露乏。
- validators 設定Authority的地方碧浊,目前先空著,后面創(chuàng)建account之后再回來填入瘟仿。
將上面的文件保存到桌面的一個文件中箱锐,保存為demo-spec.json。
三劳较、設置兩個節(jié)點
在我們這篇文章中驹止,我們在同一臺電腦設置兩個節(jié)點,跟我們講解以太坊私網(wǎng)建立 (2) – 同一臺電腦/不同電腦運行多個節(jié)點時观蜗,如果在同一臺電腦設置兩個節(jié)點臊恋,需要將rpcport和port設置為不同的值,否則就會發(fā)生沖突墓捻,POA chain中也是一樣抖仅,需要將一些參數(shù)設置為不同的值。
- -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 = 8450 - 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 = 8451
[ipc]
disable = true - base_path是指與parity demo-spec.json 同級的目錄路徑
我們將創(chuàng)建三個帳戶:兩個權限和一個用戶帳戶袱院。這里只記錄兩種:
方法1.使用RPC
使用啟動節(jié)點0 parity --config node0.toml屎慢。
RPC可以通過訪問web3瞭稼,parity.js或簡單地使用curl。這將創(chuàng)建第一個權限地址:
curl --data '{"jsonrpc":"2.0","method":"parity_newAccountFromPhrase","params":["node0", "node0"],"id":0}' -H "Content-Type: application/json" -X POST localhost:8540
返回的地址應該是0x00bd138abd70e2f00903268f3db08f2d25677c9e腻惠。
用戶地址:
curl --data '{"jsonrpc":"2.0","method":"parity_newAccountFromPhrase","params":["user", "user"],"id":0}' -H "Content-Type: application/json" -X POST localhost:8540
返回的地址應該是0x004ec07d2329997267ec62b4166639513386f32e环肘。
現(xiàn)在啟動另一個節(jié)點parity --config node1.toml并創(chuàng)建第二個權限帳戶:
curl --data '{"jsonrpc":"2.0","method":"parity_newAccountFromPhrase","params":["node1", "node1"],"id":0}' -H "Content-Type: application/json" -X POST localhost:8541
返回的地址應該是0x00aa39d30f0d20ff03a22ccfc30b7efbfca597c2。
方法2.使用UI
- 使用啟動節(jié)點0 parity --config node0.toml
- 啟動Parity UI并完成初始設置
- 點擊“RESTORE ACCOUNT”
- 使用短語“node0”和密碼“node0”
- 新創(chuàng)建的帳戶應該有地址 0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e
- 現(xiàn)在從另一個短語“用戶”和密碼“用戶”恢復集灌,這將導致帳戶0x004ec07d2329997267ec62b4166639513386f32e
- 使用啟動節(jié)點1 parity --config node1.toml
- 去 localhost:8181
- 再次從短語“node1”和密碼“node1”中恢復悔雹,這將創(chuàng)建帳戶 0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2
3.完成鏈規(guī)范
如果帳戶是根據(jù)短語創(chuàng)建的,我們現(xiàn)在應該進行以下設置:
- 節(jié)點0具有0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e權限帳戶和0x004ec07d2329997267Ec62b4166639513386F32E用戶帳戶
- 節(jié)點1具有0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2權限帳戶
現(xiàn)在可以將帳戶添加到spec文件中欣喧。打開demo-spec.json備份并將我們剛創(chuàng)建的權限添加到"validators"數(shù)組中:
"validators" : {
"list": [
"0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e",
"0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2"
]
}
然后將我們的用戶帳戶添加到創(chuàng)世紀腌零,以便我們有一些平衡發(fā)送:
"0x004ec07d2329997267ec62b4166639513386f32e": { "balance": "10000000000000000000000" }
完整demo-spec.json應該如下所示:
{
"name": "DemoPoA",
"engine": {
"authorityRound": {
"params": {
"stepDuration": "5",
"validators" : {
"list": [
"0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e",
"0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2"
]
}
}
}
},
"params": {
"gasLimitBoundDivisor": "0x400",
"maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388",
"networkID" : "0x2323",
"eip155Transition": 0,
"validateChainIdTransition": 0,
"eip140Transition": 0,
"eip211Transition": 0,
"eip214Transition": 0,
"eip658Transition": 0
},
"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" }
}
}
4.運行授權節(jié)點
現(xiàn)在規(guī)范已經(jīng)完成,我們可以啟動將運行鏈的兩個節(jié)點唆阿。要將節(jié)點作為權限運行益涧,我們需要啟用它來簽署共識消息。
首先驯鳖,讓我們用節(jié)點密碼創(chuàng)建一個文件node.pwds闲询。每行將包含我們在創(chuàng)建權限帳戶時使用的密碼,要存儲在名為node.pwds的文件中的內(nèi)容為:
node0
node1
現(xiàn)在我們可以添加engine-signer到配置文件中浅辙。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 = 8450
[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"]
[ui]
port = 8181
[websockets]
port = 8451
[ipc]
disable = true
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2"
reseal_on_txs = "none"
現(xiàn)在可以啟動這兩個節(jié)點扭弧。節(jié)點0:
parity --config node0.toml
和節(jié)點1:
parity --config node1.toml
5.連接節(jié)點
為了確保節(jié)點彼此連接,我們需要知道它們的enode地址并通知另一個節(jié)點记舆。有三種方法可以獲得enode:
- RPC
- UI(在頁腳中)
- 啟動控制臺輸出
獲得后鸽捻,可以將它們作為引導節(jié)點添加到demo-spec.json保留對等體中。
在這里我們將簡單地使用curl氨淌。獲取節(jié)點0 enode:
curl --data '{"jsonrpc":"2.0","method":"parity_enode","params":[],"id":0}' -H "Content-Type: application/json" -X POST localhost:8540
將“結果”添加到節(jié)點1(enode://RESULT在命令中替換):
curl --data '{"jsonrpc":"2.0","method":"parity_addReservedPeer","params":["enode://RESULT"],"id":0}' -H "Content-Type: application/json" -X POST localhost:8541
現(xiàn)在節(jié)點應該0/1/25 peers在控制臺中指示泊愧,這意味著它們彼此連接。
6.發(fā)送交易
發(fā)送事務的兩種主要方式是RPC和UI盛正。
RPC
從用戶帳戶向權限node0發(fā)送一些令牌:
2b5e3af16b1880000
curl --data '{"jsonrpc":"2.0","method":"personal_sendTransaction","params":[{"from":"0x004ec07d2329997267Ec62b4166639513386F32E","to":"0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e","value":"0xde0b6b3a7640000"}, "user"],"id":0}' -H "Content-Type: application/json" -X POST localhost:8540
提交請求后删咱,應在幾秒鐘后發(fā)出阻止。您可以檢查其他帳戶是否收到了資金:
curl --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", "latest"],"id":1}' -H "Content-Type: application/json" -X POST localhost:8540
我們還可以將一些發(fā)送到另一個節(jié)點上的帳戶:
curl --data '{"jsonrpc":"2.0","method":"personal_sendTransaction","params":[{"from":"0x004ec07d2329997267Ec62b4166639513386F32E","to":"0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2","value":"0xde0b6b3a7640000"}, "user"],"id":0}' -H "Content-Type: application/json" -X POST localhost:8540
并檢查是否收到詢問另一個節(jié)點:
curl --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x00Aa39d30F0D20FF03a22cCfc30B7EfbFca597C2", "latest"],"id":1}' -H "Content-Type: application/json" -X POST localhost:8541
7.進一步發(fā)展
您現(xiàn)在可以創(chuàng)建更多帳戶豪筝,發(fā)送價值痰滋,編寫合同并進行部署。用于開發(fā)和使用以太坊網(wǎng)絡的所有工具也可用于此網(wǎng)絡续崖。
要在多臺計算機上部署Parity敲街,您可能會發(fā)現(xiàn)docker構建很有用。
要添加非授權節(jié)點严望,可以使用以下更簡單的配置:
[parity]
chain = "demo-spec.json"
base_path = "/tmp/parity2"
[network]
port = 30302
[rpc]
port = 8542
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
[ui]
port = 8182
[websockets]
port = 8452
[ipc]
disable = true
然后多艇,帳戶和連接節(jié)點可以與權限節(jié)點相同。為了確保接受事務像吻,權限也可以在usd_per_tx = "0"字段下運行[mining]峻黍。任何提交交易的節(jié)點都可以免費提供复隆。
在不同的機器上運行節(jié)點時,大多數(shù)字段都是冗余的姆涩,因此基本配置只有鏈和可能的RPC apis:
[parity]
chain = "demo-spec.json"
[rpc]
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts”]
或者只是跑parity --chain demo-spec.json挽拂!
-
補充:分享給區(qū)網(wǎng)內(nèi)其他人使用
在開發(fā)時通常會將node跑在server上,讓其他人可以透過JSON RPC port連接上去使用骨饿,此時只要在config里面加入 [interface] 設定即可亏栈。
假設server ip為192.168.1.1,將 node0.toml 修改如下:
[rpc]
port = 8540
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
interface = "192.168.1.1"- 坑點:我在使用時宏赘,使用Vmware虛擬機搭建绒北,parity默認綁定的是本機的IP,導致外部一直不能訪問察署,寫入interface = "0.0.0.0"后解決外部訪問問題镇饮。
*在JSONRPC接口的調(diào)用過程中,如果遇到跨域問題箕母,需要在配置文件中添加cors=all 或者 cors=:
[rpc]
cors = ["all"]**錢包交易轉賬時,可能會遇到交易接口調(diào)用成功俱济,但balance并沒有發(fā)生變化的情況嘶是,你需要用以下命令啟動:
parity --config node0.toml --nat extip:X.X.X.X**如果你需要獲取錢包的操作記錄,你需要給你的錢包開啟此功能蛛碌,在配置文件中添加如下:
[footprint]
tracing = "on"
然后調(diào)用trace_filter接口即可聂喇,但此功能比較占用服務資源,如果不是特別需要蔚携,建議不要開啟希太。