1、搭建私鏈所需環境node
操做系統:ubuntu16.04,開虛擬機的話要至少4G,不然會影響測試挖礦時的速度git
軟件:github
geth客戶端web
Mist和Ethereum Wallet:https://github.com/ethereum/mist/releases/json
2、安裝geth客戶端ubuntu
sudo apt-get update sudo apt-get install software-properties-common sudo add-apt-repository -y ppa:ethereum/ethereum sudo add-apt-repository -y ppa:ethereum/ethereum-dev sudo apt-get update sudo apt-get install ethereum
安裝完成後,輸入geth -h,若是有列出信息則說明安裝成功,可查看列出信息,方便後續的操做api
3、建立創始塊數組
(1)建立以太坊初始區塊文件 genesis.json服務器
做爲區塊鏈,鏈子總要有個頭,因此須要建立一個創世塊做爲頭部,纔好往下添加,創世文件名稱命名爲genesis.json,數據目錄存放在/home/cll/privateDemo/data1,genesis.json存放在/home/cll/ privateDemo/:網絡
cat > genesis.json
輸入:
{ "config": { "chainId": 411, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 }, "nonce": "0x0000000000000033", "timestamp": "0x0", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "gasLimit": "0x8000000", "difficulty": "0x100", "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase":"0x0000000000000000000000000000000000000000", "alloc": { "0x1C83C95473e1e93A2C8560c73976dAFA9C3f0a79": {"balance":"1000000"} } }
查看:
cat genesis.json
部分字段解釋:
chainId : 以太坊區塊鏈網絡Id,ethereum主鏈是1,私有鏈只用不要與主鏈衝突便可。
alloc : 預留帳戶,以下
coinbase: 曠工帳戶
difficulty: 挖礦難度
extraData:至關於備註
gasLimit:最小消耗gas
nonce : 64位隨機數,用於挖礦,注意他和mixhash的設置須要知足以太坊黃皮書中的要求
parentHash : 上一個區塊的Hash值,由於是創世塊,沒有在它前面的,因此是0
timestamp : 時間戳
(2)初始化區塊節點
geth --datadir data1 init genesis.json
(3)啓動geth客戶端節點
geth --identity "data1" --rpc --rpcport 8000 --rpccorsdomain "*" --datadir "data1" --port 30303 --rpcapi "db,eth,net,web3" --networkid 999 console 2>>data1/geth.log
geth經常使用屬性:
--Identity : 節點身份標識,起個名字
--datadir : 指定節點存在位置,「data1」
--rpc : 啓用http-rpc服務器
--rpcapi : 基於http-rpc提供的api接口。eth,net,web3,db...
--rpcaddr : http-rpc服務器接口地址:默認「127.0.0.1」
--rpcport : http-rpc 端口(多節點時,不要重複)
--port : 節點端口號(多節點時,不要重複)
--networkid : 網絡標識符 隨便指定一個id(確保多節點是統一網絡,保持一致)
(4)geth經常使用命令
#建立帳戶 personal.newAccount("123456") #獲取帳戶數組 eth.accounts #解鎖帳戶,轉帳時可以使用 personal.unlockAccount(eth.accounts[0], "123456") #節點主帳戶 eth.coinbase #查看帳戶餘額 eth.getBalance(eth.accounts[0]) #啓動,結束挖礦,寫區塊 miner.start(), miner.stop() #查看區塊數量 eth.blockNumber
4、配置多節點服務
(1)再初始化一個節點(與上一個節點使用同一個genesis.json)
(privateDemo目錄)
geth --datadir data2 init genesis.json
(2)啓動新節點
(privateDemo目錄)
geth --identity "data2" --rpc --rpcport 8001 --rpccorsdomain "*" --datadir "data2" --port 30306 --rpcapi "db,eth,net,web3" --networkid 999 console 2>>data2/geth.log
注:若是在同一臺電腦上這兩個端口號與以前的節點不能相同
(3)查看:新節點enode信息
admin.nodeInfo.enode
(4)主節點添加新節點,使兩節點連在同一個私有鏈上
在data1終端下輸入新節點data2的enode信息
例如:
admin.addpeer("enode://d4f64272de882d2e2ccefc6466c6580ddecd253f5c9d87f977ac3881cbea7b141c07681ea605c53af5815cbfc25b5138b9ddb07be61b757850a55b7197939ba4@127.0.0.1:30306")
(5)檢查兩節點是否已鏈接
首先使用命令admin.nodeInfo,查看結果是否爲空[],或者net.peerCount,查看是否爲0(這裏有點問題,有時候因爲某些緣由,可能網絡不穩定,添加完後是[]和0,有時候又顯示有同伴);另外在其中一個節點啓動挖礦命令 miner.start(),看另外一個節點是否也同步,可使用命令行跟蹤(data1目錄):tail -f geth.log。
4、Mist鏈接私鏈
(1)以rpc方式打開訪問ipc接口方式打開mist錢包
「(mist應用所在路徑)」 –rpc 「(節點geth.ipc所在目錄)」
例如:
「/home/cll/ethereum/mist/mist」 –rpc 「/home/cll/privateDemo/data1/geth.ipc」
(2)只讀的http方式打開錢包。不能轉帳,不能部署合約
「/home/cll/ethereum/mist/mist」 –rpc http://127.0.0.1:8000
5、在Mist上部署智能合約
點擊CONTRACTS->點擊DEPLOY NEW CONTRACT->選擇帳戶,編寫合約內容->最底下點擊DEPLOY。成功提交後,等待挖礦確認,確認的合約會顯示在CONTRACTS頁面,點擊合約便可進行相關操做。