區塊鏈學習(三)以太網私有鏈中創建多節點併產生交易

##材料:html

ubuntu18.042版系統node

eth開發環境linux

在上一篇中,咱們介紹了基於以太坊的私有鏈搭建,而且成功挖礦,這篇文中,咱們要在私有鏈網絡中創建多個節點組成集羣,而且發生交易。git

1、準備步驟github

首先咱們要注意的幾個要點:web

  1. 創建的每一個節點都要有獨立的數據目錄(--datadir)
  2. 每一個節點運行都要有獨立的端口(--port(eth端口設置)和--rpcport(rpc端口設置))
  3. 節點之間必需要知道彼此
  4. 節點間有惟一的ipc通訊端點,或者禁用ipc

下面咱們開始創建多個節點網絡json

2、啓動節點ubuntu

首先咱們啓動咱們上一篇創建的第一個節點,指定端口並禁用ipc,咱們以命令行的(console)的方式啓動節點,以下:網絡

 1 milo@milo-K46CB:~/private-geth$ geth --datadir ./data/00 --networkid 100 --ipcdisable --port 61910 --rpcport 8200 console
 2 INFO [06-27|10:14:54.214] Maximum peer count                       ETH=25 LES=0 total=25
 3 INFO [06-27|10:14:54.252] Starting peer-to-peer node               instance=Geth/v1.8.27-stable-4bcc0a37/linux-amd64/go1.10.4
 4 INFO [06-27|10:14:54.252] Allocated cache and file handles         database=/home/milo/private-geth/data/00/geth/chaindata cache=512 handles=2048
 5 INFO [06-27|10:14:54.525] Initialised chain configuration          config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil>  ConstantinopleFix: <nil> Engine: unknown}"
 6 INFO [06-27|10:14:54.526] Disk storage enabled for ethash caches   dir=/home/milo/private-geth/data/00/geth/ethash count=3
 7 INFO [06-27|10:14:54.526] Disk storage enabled for ethash DAGs     dir=/home/milo/.ethash                          count=2
 8 INFO [06-27|10:14:54.526] Initialising Ethereum protocol           versions="[63 62]" network=100
 9 INFO [06-27|10:14:54.621] Loaded most recent local header          number=31 hash=337359…ebd896 td=4091328 age=13h3m27s
10 INFO [06-27|10:14:54.621] Loaded most recent local full block      number=31 hash=337359…ebd896 td=4091328 age=13h3m27s
11 INFO [06-27|10:14:54.621] Loaded most recent local fast block      number=31 hash=337359…ebd896 td=4091328 age=13h3m27s
12 INFO [06-27|10:14:54.621] Loaded local transaction journal         transactions=0 dropped=0
13 INFO [06-27|10:14:54.622] Regenerated local transaction journal    transactions=0 accounts=0
14 WARN [06-27|10:14:54.622] Blockchain not empty, fast sync disabled 
15 INFO [06-27|10:14:54.899] New local node record                    seq=2 id=1b933112e1333f9a ip=127.0.0.1 udp=61910 tcp=61910
16 INFO [06-27|10:14:54.900] Started P2P networking                   self=enode://bfad31051f4ba04bf5bbb2fdd455bfcbf924479991b029a8604ab5abb386431b7c7f7d1edbba0bf6db66ad172b632ca45518239979634f190a0c26c0a9b9864b@127.0.0.1:61910
17 Welcome to the Geth JavaScript console!
18 
19 instance: Geth/v1.8.27-stable-4bcc0a37/linux-amd64/go1.10.4
20 INFO [06-27|10:14:55.087] Etherbase automatically configured       address=0x8830397771710ADE101f0080f0da076181Bad374
21 coinbase: 0x8830397771710ade101f0080f0da076181bad374
22 at block: 31 (Wed, 26 Jun 2019 21:11:27 CST)
23  datadir: /home/milo/private-geth/data/00
24  modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

經過輸入如下命令獲取節點的enode url,咱們須要這個enode url實現與另外節點的互通tcp

1 > admin.nodeInfo.enode
2 "enode://bfad31051f4ba04bf5bbb2fdd455bfcbf924479991b029a8604ab5abb386431b7c7f7d1edbba0bf6db66ad172b632ca45518239979634f190a0c26c0a9b9864b@127.0.0.1:61910"

輸入如下命令獲取本機的IP地址

1 $ ifconfig|grep netmask|awk '{print $2}'
2 127.0.0.1
3 192.168.137.93

此時,咱們已經獲取兩個節點互通的兩個要素,而後咱們在打開一個終端,開始初始化第二個節點:

 1 milo@milo-K46CB:~/private-geth$ geth --datadir ./data/01 init ./genesis.json
 2 INFO [06-27|10:18:33.961] Maximum peer count                       ETH=25 LES=0 total=25
 3 INFO [06-27|10:18:33.970] Allocated cache and file handles         database=/home/milo/private-geth/data/01/geth/chaindata cache=16 handles=16
 4 INFO [06-27|10:18:34.058] Writing custom genesis block 
 5 INFO [06-27|10:18:34.059] Persisted trie from memory database      nodes=0 size=0.00B time=15.094µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
 6 INFO [06-27|10:18:34.061] Successfully wrote genesis state         database=chaindata                                      hash=696bed…1d07ed
 7 INFO [06-27|10:18:34.061] Allocated cache and file handles         database=/home/milo/private-geth/data/01/geth/lightchaindata cache=16 handles=16
 8 INFO [06-27|10:18:34.170] Writing custom genesis block 
 9 INFO [06-27|10:18:34.170] Persisted trie from memory database      nodes=0 size=0.00B time=6.157µs  gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
10 INFO [06-27|10:18:34.171] Successfully wrote genesis state         database=lightchaindata                                      hash=696bed…1d07ed

以上代碼咱們能夠看出,這裏咱們設第二節點的數據目錄爲01

啓動第二節點:

1 milo@milo-K46CB:~/private-geth$ geth --datadir ./data/01 --networkid 100 --ipcdisable --port 61911 --rpcport 8101 --bootnodes "enode://bfad31051f4ba04bf5bbb2fdd455bfcbf924479991b029a8604ab5abb386431b7c7f7d1edbba0bf6db66ad172b632ca45518239979634f190a0c26c0a9b9864b@127.0.0.1:61910" console

咱們發現第二節點的啓動命令與第一個不一樣,多了一個--bootnodes命令,這個是設置當前節點啓動後,直接經過設置--bootnodes的值(第一個節點的enode url)來鏈接第一個節點;固然,咱們還有另外一個方法,就是不設置--bootnodes,咱們直接啓動,啓動後進入命令行,而後經過admin.addPeer(enodeUrlOfFirst Instance)命令把它做爲一個peer添加進來。

輸出結果:

 1 INFO [06-27|10:20:31.279] Maximum peer count                       ETH=25 LES=0 total=25
 2 INFO [06-27|10:20:31.282] Starting peer-to-peer node               instance=Geth/v1.8.27-stable-4bcc0a37/linux-amd64/go1.10.4
 3 INFO [06-27|10:20:31.282] Allocated cache and file handles         database=/home/milo/private-geth/data/01/geth/chaindata cache=512 handles=2048
 4 INFO [06-27|10:20:31.435] Initialised chain configuration          config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil>  ConstantinopleFix: <nil> Engine: unknown}"
 5 INFO [06-27|10:20:31.435] Disk storage enabled for ethash caches   dir=/home/milo/private-geth/data/01/geth/ethash count=3
 6 INFO [06-27|10:20:31.436] Disk storage enabled for ethash DAGs     dir=/home/milo/.ethash                          count=2
 7 INFO [06-27|10:20:31.436] Initialising Ethereum protocol           versions="[63 62]" network=100
 8 INFO [06-27|10:20:31.510] Loaded most recent local header          number=0 hash=696bed…1d07ed td=256 age=50y2mo2w
 9 INFO [06-27|10:20:31.510] Loaded most recent local full block      number=0 hash=696bed…1d07ed td=256 age=50y2mo2w
10 INFO [06-27|10:20:31.510] Loaded most recent local fast block      number=0 hash=696bed…1d07ed td=256 age=50y2mo2w
11 INFO [06-27|10:20:31.510] Regenerated local transaction journal    transactions=0 accounts=0
12 INFO [06-27|10:20:31.606] New local node record                    seq=1 id=e03f0842cd095770 ip=127.0.0.1 udp=61911 tcp=61911
13 INFO [06-27|10:20:31.607] Started P2P networking                   self=enode://dcb9f110ea5233a6dc7dc87c898fda50e6e203205df1f8550363966466f3b62a36c22efda0d9e8596f03a92bd667bf1dbf318ce7386e589e82633a7e567c913c@127.0.0.1:61911
14 Welcome to the Geth JavaScript console!
15 
16 instance: Geth/v1.8.27-stable-4bcc0a37/linux-amd64/go1.10.4
17  modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

第二個節點輸入以下命令,確認兩個節點是否鏈接成功

 1 > admin.nodeInfo
 2 {
 3   enode: "enode://dcb9f110ea5233a6dc7dc87c898fda50e6e203205df1f8550363966466f3b62a36c22efda0d9e8596f03a92bd667bf1dbf318ce7386e589e82633a7e567c913c@127.0.0.1:61911",
 4   enr: "0xf896b840aa8bf5b1a3f8772da7d03a84f0da4d4f34f385ada63a0f3fb588b5ed8a64340a0a7c7bd1326e097e29a128f7bc5426e35bfefdb9bfec82931683c23218031a7e0183636170c6c5836574683f826964827634826970847f00000189736563703235366b31a102dcb9f110ea5233a6dc7dc87c898fda50e6e203205df1f8550363966466f3b62a8374637082f1d78375647082f1d7",
 5   id: "e03f0842cd09577060fa055074d2ff1c15fe9f4b9b269f4dd14025fa81af3fc6",
 6   ip: "127.0.0.1",
 7   listenAddr: "[::]:61911",
 8   name: "Geth/v1.8.27-stable-4bcc0a37/linux-amd64/go1.10.4",
 9   ports: {
10     discovery: 61911,
11     listener: 61911
12   },
13   protocols: {
14     eth: {
15       config: {
16         chainId: 15,
17         eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
18         eip155Block: 0,
19         eip158Block: 0,
20         homesteadBlock: 0
21       },
22       difficulty: 256,
23       genesis: "0x696beda69b95581de321f9cf44d1123bdb2dcf8cc89d0ddce917071c221d07ed",
24       head: "0x696beda69b95581de321f9cf44d1123bdb2dcf8cc89d0ddce917071c221d07ed",
25       network: 100
26     }
27   }
28 }

第一個節點輸入:

 1 > net.peerCount
 2 1
 3 > admin.peers
 4 [{
 5     caps: ["eth/63"],
 6     enode: "enode://dcb9f110ea5233a6dc7dc87c898fda50e6e203205df1f8550363966466f3b62a36c22efda0d9e8596f03a92bd667bf1dbf318ce7386e589e82633a7e567c913c@127.0.0.1:61911",
 7     id: "e03f0842cd09577060fa055074d2ff1c15fe9f4b9b269f4dd14025fa81af3fc6",
 8     name: "Geth/v1.8.27-stable-4bcc0a37/linux-amd64/go1.10.4",
 9     network: {
10       inbound: false,
11       localAddress: "127.0.0.1:38586",
12       remoteAddress: "127.0.0.1:61911",
13       static: false,
14       trusted: false
15     },
16     protocols: {
17       eth: {
18         difficulty: 256,
19         head: "0x696beda69b95581de321f9cf44d1123bdb2dcf8cc89d0ddce917071c221d07ed",
20         version: 63
21       }
22     }
23 }]

這裏能夠看出,咱們輸入net.peerCount命令,輸出爲1,說明第一個節點有一個peer鏈接,鏈接id爲:

"e03f0842cd09577060fa055074d2ff1c15fe9f4b9b269f4dd14025fa81af3fc6"

而這個id,正好是第二個節點的id,這裏咱們就完成了兩個節點的鏈接,而經過這種方式,咱們還能夠繼續擴展下去,一步一步下去就能夠建成本地節點集羣

請參考:https://github.com/ethersphere/eth-utils下的gethcluster.sh腳本,以及README中的使用方法和示例.  (使用腳原本完成上面的工做)

2、交易

在完成兩個節點的連接後,咱們嘗試着讓兩個節點的帳戶進行交易,首先咱們在第二個節點建立帳戶

1 > personal.newAccount("123456")
2 "0x7b61034a115dd5a0d63da7beeb293ef10a638697"
3 > eth.getBalance(eth.accounts[0])
4 0

打開第一個節點,查看帳戶信息:

1 1 > eth.accounts
2 2 ["0x8830397771710ade101f0080f0da076181bad374"]
3 3 > eth.getBalance(eth.accounts[0])
4 4 155000000000000000000

咱們能夠看到,第一個節點中的帳戶已經有ether(單位wei)

在發送「ether」前,須要先將帳戶解鎖,在第一個節點輸入以下命令:

1 > personal.unlockAccount(eth.accounts[0], "123456")
2 true
1 >  eth.sendTransaction({from: "0x8830397771710ade101f0080f0da076181bad374", to: "0x7b61034a115dd5a0d63da7beeb293ef10a638697", value: web3.toWei(1, "ether")})
2 INFO [06-27|10:35:44.634] Setting new local account                address=0x8830397771710ADE101f0080f0da076181Bad374
3 INFO [06-27|10:35:44.634] Submitted transaction                    fullhash=0x5dd8c5cebced5e5d23fcf987645d57846719ca675b4782784bb1f97131461a6e recipient=0x7b61034A115Dd5A0D63dA7bEeb293Ef10A638697
4 "0x5dd8c5cebced5e5d23fcf987645d57846719ca675b4782784bb1f97131461a6e"

如上,eth.sendTransaction執行發送ether的命令,參數from,to分別是發送帳戶和接收帳戶,web3,toWei(1,"ether")是將1單位的「ether」轉換爲「wei」數量

輸入如下命令查看信息

 1 > eth.pendingTransactions
 2 [{
 3     blockHash: null,
 4     blockNumber: null,
 5     from: "0x8830397771710ade101f0080f0da076181bad374",
 6     gas: 90000,
 7     gasPrice: 1000000000,
 8     hash: "0x5dd8c5cebced5e5d23fcf987645d57846719ca675b4782784bb1f97131461a6e",
 9     input: "0x",
10     nonce: 0,
11     r: "0x36b43d7b9cb29aa4597c1a9a1d616a2ed6249178e13beb85ed7cea88e4d7073e",
12     s: "0x1e00730be78f192303674e8cf0e46681abcba23b5ebe6655c8753b908c27ceff",
13     to: "0x7b61034a115dd5a0d63da7beeb293ef10a638697",
14     transactionIndex: 0,
15     v: "0x41",
16     value: 1000000000000000000
17 }]

這個時候咱們已經把以太幣發送出去了,可是咱們查看第二個節點帳戶時發現並無收到,這是由於,每一筆交易須要通過挖礦打包確認後纔會而收到

咱們開始挖礦,在第一個節點輸入如下命令:

 1 > eth.blockNumber
 2 31
 3 > miner.start(3)
 4 INFO [06-27|10:37:00.039] Updated mining threads                   threads=3
 5 INFO [06-27|10:37:00.039] Transaction pool price threshold updated price=1000000000
 6 null
 7 > INFO [06-27|10:37:00.040] Commit new mining work                   number=32 sealhash=d794d7…b3b4c1 uncles=0 txs=0 gas=0 fees=0 elapsed=438.622µs
 8 INFO [06-27|10:37:00.041] Commit new mining work                   number=32 sealhash=bda6ab…09664d uncles=0 txs=1 gas=21000 fees=2.1e-05 elapsed=1.702ms
 9 INFO [06-27|10:41:54.668] Successfully sealed new block            number=32 sealhash=bda6ab…09664d hash=1cdd6d…d53112 elapsed=4m54.627s
10 INFO [06-27|10:41:54.699] 🔨 mined potential block                  number=32 hash=1cdd6d…d53112
11 INFO [06-27|10:41:54.775] Commit new mining work                   number=33 sealhash=b89d72…985623 uncles=0 txs=0 gas=0     fees=0       elapsed=76.026ms
12 INFO [06-27|10:41:55.261] Successfully sealed new block            number=33 sealhash=b89d72…985623 hash=fadd9c…29cc02 elapsed=485.521ms
13 INFO [06-27|10:41:55.261] 🔨 mined potential block                  number=33 hash=fadd9c…29cc02
14 INFO [06-27|10:41:55.261] Commit new mining work                   number=34 sealhash=6475ea…a43d17 uncles=0 txs=0 gas=0     fees=0       elapsed=189.99µs
15 INFO [06-27|10:41:55.867] Successfully sealed new block            number=34 sealhash=6475ea…a43d17 hash=635832…fbd0e8 elapsed=606.661ms
16 INFO [06-27|10:41:55.868] 🔨 mined potential block                  number=34 hash=635832…fbd0e8
17 INFO [06-27|10:41:55.868] Commit new mining work                   number=35 sealhash=b4f992…b16432 uncles=0 txs=0 gas=0     fees=0       elapsed=354.748µs
18 INFO [06-27|10:41:56.996] Successfully sealed new block            number=35 sealhash=b4f992…b16432 hash=e6db53…0af47e elapsed=1.127s
19 INFO [06-27|10:41:56.996] 🔨 mined potential block                  number=35 hash=e6db53…0af47e
20 INFO [06-27|10:41:56.996] Commit new mining work                   number=36 sealhash=a1a51f…6f3f17 uncles=0 txs=0 gas=0     fees=0       elapsed=155.944µs
21 INFO [06-27|10:41:57.149] Successfully sealed new block            number=36 sealhash=a1a51f…6f3f17 hash=4b0db1…b91a50 elapsed=153.048ms
22 INFO [06-27|10:41:57.149] 🔨 mined potential block                  number=36 hash=4b0db1…b91a50
23 INFO [06-27|10:41:57.150] Commit new mining work                   number=37 sealhash=0414f2…fec449 uncles=0 txs=0 gas=0     fees=0       elapsed=210.264µs
24 INFO [06-27|10:41:57.266] Successfully sealed new block            number=37 sealhash=0414f2…fec449 hash=d0d499…9913cc elapsed=116.015ms
25 INFO [06-27|10:41:57.266] 🔨 mined potential block                  number=37 hash=d0d499…9913cc
26 INFO [06-27|10:41:57.266] Mining too far in the future             wait=2s
27 INFO [06-27|10:41:59.267] Commit new mining work                   number=38 sealhash=3dbe12…a2f7c7 uncles=0 txs=0 gas=0     fees=0       elapsed=2.001s
28 INFO [06-27|10:42:00.403] Successfully sealed new block            number=38 sealhash=3dbe12…a2f7c7 hash=6cfb74…32b73e elapsed=1.135s
29 INFO [06-27|10:42:00.403] 🔨 mined potential block                  number=38 hash=6cfb74…32b73e
30 INFO [06-27|10:42:00.403] Commit new mining work                   number=39 sealhash=bbf2dd…4b6915 uncles=0 txs=0 gas=0     fees=0       elapsed=232.694µs
31 > miner.stINFO [06-27|10:42:03.371] Successfully sealed new block            number=39 sealhash=bbf2dd…4b6915 hash=9591ac…edc6ec elapsed=2.967s
32 INFO [06-27|10:42:03.371] 🔗 block reached canonical chain          number=32 hash=1cdd6d…d53112
33 INFO [06-27|10:42:03.371] 🔨 mined potential block                  number=39 hash=9591ac…edc6ec
34 INFO [06-27|10:42:03.371] Commit new mining work                   number=40 sealhash=88dc58…b172f7 uncles=0 txs=0 gas=0     fees=0       elapsed=166.78µs
35 > miner.stopINFO [06-27|10:42:03.979] Successfully sealed new block            number=40 sealhash=88dc58…b172f7 hash=16a734…ef6712 elapsed=607.805ms
36 INFO [06-27|10:42:03.979] 🔗 block reached canonical chain          number=33 hash=fadd9c…29cc02
37 INFO [06-27|10:42:03.979] 🔨 mined potential block                  number=40 hash=16a734…ef6712
38 INFO [06-27|10:42:03.979] Commit new mining work                   number=41 sealhash=b1f996…894978 uncles=0 txs=0 gas=0     fees=0       elapsed=237.324µs
39 > miner.stop()
40 null
41 > eth.blockNumber
42 40

由上面的日誌咱們能夠發現挖礦前是31個區塊,挖礦後是40,咱們再來看一下第二個節點的帳戶信息:

1 > eth.blockNumber
2 40
3 > eth.getBalance(eth.accounts[0])
4 1000000000000000000
5 > 

咱們能夠看到,這時node2帳戶中已經有了1「ether」了

到這裏,咱們就已經完成了今天完成的工做了

 

 

 

 

參考:http://www.javashuo.com/article/p-cbcwvccs-kg.html

相關文章
相關標籤/搜索