1. 程式人生 > 資訊 >壹號本公佈 T 系列平板新品,搭載英特爾 12 代酷睿

壹號本公佈 T 系列平板新品,搭載英特爾 12 代酷睿

摘要: 這是一篇演示如何使用一臺伺服器,執行多個以太坊客戶端,並且將節點互聯,完成兩個節點之間的轉賬操作。

內容摘要

這一部關於區塊鏈開發及運維的電子書。

為什麼會寫區塊鏈電子書?因為2018年是區塊鏈年。

這本電子書是否會出版(紙質圖書)? 不會,因為網際網路技術更迭太快,紙質書籍的內容無法實時更新,一本書動輒百元,很快就成為垃圾,你會發現目前市面的上區塊鏈書籍至少是一年前寫的,內容已經過時,很多例子無法正確執行。所以我不會出版,電子書的內容會追逐技術發展,及時跟進軟體版本的升級,做到內容最新,至少是主流。

這本電子書與其他區塊鏈書籍有什麼不同?市面上大部分割槽塊鏈書籍都是用2/3去講區塊鏈原理,只要不到 1/3 的乾貨,乾貨不夠理論來湊,通篇將理論或是大談特談區塊鏈行業,這些內容更多是頭腦風暴,展望區塊鏈,均無法落地實施。本書與那些書籍完全不同,不講理論和原理,面向應用落地,注重例子,均是乾貨。

電子書更新頻率?每天都會有新內容加入,更新頻率最遲不會超過一週,更新內容請關注 https://github.com/netkiller/netkiller.github.io/commits/master

本文采用碎片化寫作,原文會不定期更新,請儘量閱讀原文。

http://www.netkiller.cn/blockchain/index.html

2.3. 單機多例項演示

在沒有條件安裝虛擬機器也沒有多臺伺服器的情況下我們可以使用一臺伺服器執行多個例項的方法也可以實現多個節點執行環境。

cd ~mkdir -p ethereumcd ethereummkdir data{1,2}

建立檔案 genesis.json

{  "nonce": "0x0000000000000042",  "difficulty": "0x020000",  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",  "coinbase": "0x0000000000000000000000000000000000000000",  "timestamp": "0x00",  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",  "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",  "gasLimit": "0x4c4b40",  "config": {      "chainId": 15,      "homesteadBlock": 0,      "eip155Block": 0,      "eip158Block": 0
  },  "alloc": { }
}

2.3.1. 例項一

geth --datadir ~/ethereum/data1 init genesis.json
geth --datadir="~/ethereum/data1" --networkid 123456 --port 30301 --rpc --rpcaddr="0.0.0.0" --rpccorsdomain "*" --rpcport 8541

啟動後終端輸出

neo@netkiller ~/ethereum % geth --datadir ~/ethereum/data1 init genesis.jsonWARN [02-02|22:09:56] No etherbase set and no accounts found as default INFO [02-02|22:09:56] Allocated cache and file handles         database=/home/neo/ethereum/data1/geth/chaindata cache=16 handles=16INFO [02-02|22:09:56] Writing custom genesis block INFO [02-02|22:09:56] Successfully wrote genesis state         database=chaindata                               hash=611596…424d04
INFO [02-02|22:09:56] Allocated cache and file handles         database=/home/neo/ethereum/data1/geth/lightchaindata cache=16 handles=16INFO [02-02|22:09:57] Writing custom genesis block INFO [02-02|22:09:57] Successfully wrote genesis state         database=lightchaindata                               hash=611596…424d04

neo@netkiller ~ % geth --datadir="~/ethereum/data1" --networkid 123456 --port 30301 --rpc --rpcaddr="0.0.0.0" --rpccorsdomain "*" -rpcport 8541             WARN [02-02|22:36:02] No etherbase set and no accounts found as default INFO [02-02|22:36:02] Starting peer-to-peer node               instance=Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1INFO [02-02|22:36:02] Allocated cache and file handles         database=/home/neo/ethereum/data1/geth/chaindata cache=128 handles=1024INFO [02-02|22:36:02] Initialised chain configuration          config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Engine: unknown}"INFO [02-02|22:36:02] Disk storage enabled for ethash caches   dir=/home/neo/ethereum/data1/geth/ethash count=3INFO [02-02|22:36:02] Disk storage enabled for ethash DAGs     dir=/home/neo/.ethash                    count=2INFO [02-02|22:36:02] Initialising Ethereum protocol           versions="[63 62]" network=123456INFO [02-02|22:36:02] Loaded most recent local header          number=0 hash=611596…424d04 td=131072INFO [02-02|22:36:02] Loaded most recent local full block      number=0 hash=611596…424d04 td=131072INFO [02-02|22:36:02] Loaded most recent local fast block      number=0 hash=611596…424d04 td=131072INFO [02-02|22:36:02] Loaded local transaction journal         transactions=0 dropped=0INFO [02-02|22:36:02] Regenerated local transaction journal    transactions=0 accounts=0INFO [02-02|22:36:02] Starting P2P networking 
INFO [02-02|22:36:05] UDP listener up                          self=enode://53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062@14.103.209.119:30301INFO [02-02|22:36:05] HTTP endpoint opened: http://0.0.0.0:8541 INFO [02-02|22:36:05] IPC endpoint opened: /home/neo/ethereum/data1/geth.ipc 
INFO [02-02|22:36:05] RLPx listener up                         self=enode://53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062@14.103.209.119:30301INFO [02-02|22:36:05] Mapped network port                      proto=udp extport=30301 intport=30301 interface="UPNP IGDv1-IP1"INFO [02-02|22:36:07] Mapped network port                      proto=tcp extport=30301 intport=30301 interface="UPNP IGDv1-IP1"

2.3.2. 例項二

geth --datadir ~/ethereum/data2 init genesis.json
geth --datadir="~/ethereum/data2" --networkid 123456 --port 30302 --rpc --rpcaddr="0.0.0.0" --rpccorsdomain "*" --rpcport 8542

啟動後控制檯輸出與例項一類似

2.3.3. 新增節點

開啟一個新終端視窗,執行下面命令檢視節點一的 enode 字串

geth --exec 'admin.nodeInfo.enode' attach ethereum/data1/geth.ipc"enode://53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062@[::]:30301?discport=0"

進入節點二,並連線到節點一。

neo@netkiller ~ % geth attach ethereum/data2/geth.ipc
Welcome to the Geth JavaScript console!instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0> admin.addPeer("enode://53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062@[::]:30302")true> admin.addPeer("enode://53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062@[::]:30301")true

檢視節點

> admin.peers
[{    caps: ["eth/63"],    id: "53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062",    name: "Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1",    network: {      localAddress: "[::1]:51250",      remoteAddress: "[::1]:30301"
    },    protocols: {      eth: {        difficulty: 131072,        head: "0x611596e7979cd4e7ca1531260fa706093a5492ecbdf58f20a39545397e424d04",        version: 63
      }
    }
}]

至此,節點已經新增完畢。

> exit

退出

2.3.4. 節點測試

這裡我們實現兩個節點間的以太幣轉賬。

現在兩個節點上都沒有任何賬號

neo@netkiller ~ % geth --exec 'personal.listAccounts' attach ethereum/data1/geth.ipc                                        
[]
neo@netkiller ~ % geth --exec 'personal.listAccounts' attach ethereum/data2/geth.ipc
[]

在兩個節點上分別建立兩個賬號,一個是礦工賬號,另一個是普通賬號。

neo@netkiller ~ % geth --exec 'personal.newAccount("abc123")' attach ethereum/data1/geth.ipc"0x5ad227e8d7e460713c78eebbe558473571edae72"neo@netkiller ~ % geth --exec 'personal.newAccount("abc123")' attach ethereum/data1/geth.ipc"0x3e822e05ee975e02be3f15f32b0fddced8d5bdd0"neo@netkiller ~ % geth --exec 'personal.listAccounts' attach ethereum/data1/geth.ipc 
["0x5ad227e8d7e460713c78eebbe558473571edae72", "0x3e822e05ee975e02be3f15f32b0fddced8d5bdd0"]



neo@netkiller ~ % geth --exec 'personal.newAccount("abc123")' attach ethereum/data2/geth.ipc"0xa6df3e3c141e27726f4aeb21a5dab2e5c76c9565"neo@netkiller ~ % geth --exec 'personal.newAccount("abc123")' attach ethereum/data2/geth.ipc"0xa66c7b8b1c26856d284a0b962385babe02caa51d"neo@netkiller ~ % geth --exec 'personal.listAccounts' attach ethereum/data2/geth.ipc 
["0xa6df3e3c141e27726f4aeb21a5dab2e5c76c9565", "0xa66c7b8b1c26856d284a0b962385babe02caa51d"]

啟動挖礦

geth --exec 'miner.start(1)' attach ethereum/data1/geth.ipc 
geth --exec 'miner.start(1)' attach ethereum/data2/geth.ipc

如果正常執行,兩個節點上的礦工賬號都會有一定的以太幣。而普通賬號額度應該為零。

neo@netkiller ~ % geth --exec 'eth.getBalance(eth.accounts[0])' attach ethereum/data1/geth.ipc299438256000000000000neo@netkiller ~ % geth --exec 'eth.getBalance(eth.accounts[1])' attach ethereum/data1/geth.ipc0neo@netkiller ~ % geth --exec 'eth.getBalance(eth.accounts[1])' attach ethereum/data1/geth.ipc298000056000000000000neo@netkiller ~ % geth --exec 'eth.getBalance(eth.accounts[1])' attach ethereum/data2/geth.ipc0

我們嘗試從節點一礦工賬號向節點二上的普通使用者轉賬。

neo@netkiller ~ % geth attach ethereum/data1/geth.ipc                               
Welcome to the Geth JavaScript console!instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1coinbase: 0x5ad227e8d7e460713c78eebbe558473571edae72at block: 144 (Fri, 02 Feb 2018 23:24:35 HST) datadir: /home/neo/ethereum/data1 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0> personal.listAccounts
["0x5ad227e8d7e460713c78eebbe558473571edae72"]

> personal.unlockAccount(eth.accounts[0], "abc123")true> eth.sendTransaction({from: "0x5ad227e8d7e460713c78eebbe558473571edae72", to: "0xa66c7b8b1c26856d284a0b962385babe02caa51d", value: web3.toWei(1, "ether")})"0x87c059d0769c8a74499ddd08c04a10f23b7681651615a28098d73ec63a943684"> eth.pendingTransactions
[{    blockHash: null,    blockNumber: null,    from: "0x5ad227e8d7e460713c78eebbe558473571edae72",    gas: 90000,    gasPrice: 18000000000,    hash: "0x87c059d0769c8a74499ddd08c04a10f23b7681651615a28098d73ec63a943684",    input: "0x",    nonce: 2,    r: "0xce004f964f268a00e90cadd4e8a685131aa34f37144f7e2e47dc7fe4ec784e55",    s: "0x412209c18513a28422e62c4bdb85a223f190e133cf71990a87c570a3a53ae093",    to: "0xa66c7b8b1c26856d284a0b962385babe02caa51d",    transactionIndex: 0,    v: "0x41",    value: 1000000000000000000}]

稍後一會,當使用 eth.pendingTransactions 檢視掛起交易為空的時候,表示已經處理完畢。這時退出控制檯。

> eth.pendingTransactions[]> exit

現在檢視節點二上的第二個普通賬號餘額

neo@netkiller ~ % geth --exec 'eth.getBalance(eth.accounts[1])' attach ethereum/data2/geth.ipc1000000000000000000

轉賬成功

現在我們從節點二上的普通使用者向節點一上的普通使用者轉賬。

neo@netkiller ~ % geth attach ethereum/data2/geth.ipc                                        
Welcome to the Geth JavaScript console!instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1coinbase: 0xa6df3e3c141e27726f4aeb21a5dab2e5c76c9565at block: 319 (Fri, 02 Feb 2018 23:50:07 HST) datadir: /home/neo/ethereum/data2 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0> personal.unlockAccount(eth.accounts[1], "abc123")true> eth.sendTransaction({from: "0xa66c7b8b1c26856d284a0b962385babe02caa51d", to: "0x3e822e05ee975e02be3f15f32b0fddced8d5bdd0", value: web3.toWei(0.1, "ether")})"0x951bd161dfd000ff825379cb0644c4acd4afd4d3e1ac4f4c1c6009b3c2a1d366"> eth.pendingTransactions
[]
> exit

檢視兩個普通賬號的餘額

neo@netkiller ~ % geth --exec 'eth.getBalance(eth.accounts[1])' attach ethereum/data1/geth.ipc100000000000000000neo@netkiller ~ % geth --exec 'eth.getBalance(eth.accounts[1])' attach ethereum/data2/geth.ipc899622000000000000