##材料:html
ubuntu18.042版系统node
eth开发环境linux
在上一篇中,咱们介绍了基于以太坊的私有链搭建,而且成功挖矿,这篇文中,咱们要在私有链网络中创建多个节点组成集群,而且发生交易。git
1、准备步骤github
首先咱们要注意的几个要点:web
下面咱们开始创建多个节点网络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”了
到这里,咱们就已经完成了今天完成的工做了