上一節(jié)中我們使用geth搭建了以太坊私有網(wǎng)絡(luò),這一次則要在私有網(wǎng)絡(luò)中建立多節(jié)點(diǎn)集群,并互相發(fā)現(xiàn),產(chǎn)生交易.
為了在本地網(wǎng)絡(luò)運(yùn)行多個以太坊節(jié)點(diǎn)的實(shí)例,必須確保以下兩點(diǎn):
- 每個實(shí)例都有獨(dú)立的數(shù)據(jù)目錄
--datadir
- 每個實(shí)例運(yùn)行都有獨(dú)立的端口.
搭建多節(jié)點(diǎn)環(huán)境
啟動第一個節(jié)點(diǎn)
geth --datadir data0 --networkid 1108 console
運(yùn)行結(jié)果如下:
INFO [03-01|10:57:17] Starting peer-to-peer node instance=Geth/v1.7.3-stable/darwin-amd64/go1.9.2
INFO [03-01|10:57:17] Allocated cache and file handles database=/Users/wangsanjun/privatechain/data0/geth/chaindata cache=128 handles=1024
INFO [03-01|10:57:17] Initialised chain configuration config="{ChainID: 10 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Engine: unknown}"
INFO [03-01|10:57:17] Disk storage enabled for ethash caches dir=/Users/wangsanjun/privatechain/data0/geth/ethash count=3
INFO [03-01|10:57:17] Disk storage enabled for ethash DAGs dir=/Users/wangsanjun/.ethash count=2
INFO [03-01|10:57:17] Initialising Ethereum protocol versions="[63 62]" network=1108
INFO [03-01|10:57:17] Loaded most recent local header number=72 hash=8df4da…335b9f td=9710147
INFO [03-01|10:57:17] Loaded most recent local full block number=72 hash=8df4da…335b9f td=9710147
INFO [03-01|10:57:17] Loaded most recent local fast block number=72 hash=8df4da…335b9f td=9710147
INFO [03-01|10:57:17] Loaded local transaction journal transactions=0 dropped=0
INFO [03-01|10:57:17] Regenerated local transaction journal transactions=0 accounts=0
WARN [03-01|10:57:17] Blockchain not empty, fast sync disabled
INFO [03-01|10:57:17] Starting P2P networking
INFO [03-01|10:57:19] UDP listener up self=enode://c1f6afa8c620b842b6ff36b9321ec2f7dd6a08037a31ee093e9acc4c5a13bcb93b5ce95b10768eedafe144f55c79858a8b8e59f121674dbe267d23dba1df3e7a@[::]:30303
INFO [03-01|10:57:19] RLPx listener up self=enode://c1f6afa8c620b842b6ff36b9321ec2f7dd6a08037a31ee093e9acc4c5a13bcb93b5ce95b10768eedafe144f55c79858a8b8e59f121674dbe267d23dba1df3e7a@[::]:30303
INFO [03-01|10:57:19] IPC endpoint opened: /Users/wangsanjun/privatechain/data0/geth.ipc
Welcome to the Geth JavaScript console!
instance: Geth/v1.7.3-stable/darwin-amd64/go1.9.2
coinbase: 0x0416f04c403099184689990674f5b4259dc46bd8
at block: 72 (Wed, 28 Feb 2018 18:33:09 CST)
datadir: /Users/wangsanjun/privatechain/data0
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
獲取節(jié)點(diǎn)實(shí)例的enode url:
> admin.nodeInfo.enode
"enode://c1f6afa8c620b842b6ff36b9321ec2f7dd6a08037a31ee093e9acc4c5a13bcb93b5ce95b10768eedafe144f55c79858a8b8e59f121674dbe267d23dba1df3e7a@[::]:30303"
再打開一個終端,初始化第二個節(jié)點(diǎn):
mkdir data1 //和上一個節(jié)點(diǎn)的data0同目錄痰驱,即在privatechain下面
geth --datadir data1 account new
geth --datadir data1 --networkid 1108 init genesis.json
geth --datadir data1 --networkid 1108 --port 30304 --bootnodes "enode://c1f6afa8c620b842b6ff36b9321ec2f7dd6a08037a31ee093e9acc4c5a13bcb93b5ce95b10768eedafe144f55c79858a8b8e59f121674dbe267d23dba1df3e7a@172.16.70.76:30303" console
上面的命令中,
--bootndoes
是設(shè)置當(dāng)前節(jié)點(diǎn)啟動后,直接通過設(shè)置--bootndoes
的值來鏈接第一個節(jié)點(diǎn),--bootnoedes
的值可以通過在第一個節(jié)的命令行中,輸入admin.nodeInfo.enode
命令打印出來碌廓,需要注意的是需要把@[::]
換成實(shí)際電腦的IP
地址
也可以不設(shè)置--bootnodes
, 直接啟動,啟動后進(jìn)入命令行, 通過命令admin.addPeer(enodeUrlOfFirst Instance)
把它作為一個peer
添加進(jìn)來.
為了確認(rèn)鏈接成功,第二個節(jié)點(diǎn)輸入:
> admin.nodeInfo
{
enode: "enode://61eac7f7900bd17cac39ce506cd20c1f67a2a43fb5f5c7a46ace337922a9e325a63a7aa9cb18df12715550ffa0b66c44272132b93245d15825c2ac2c1ddb8cd8@[::]:30304",
id: "61eac7f7900bd17cac39ce506cd20c1f67a2a43fb5f5c7a46ace337922a9e325a63a7aa9cb18df12715550ffa0b66c44272132b93245d15825c2ac2c1ddb8cd8",
ip: "::",
listenAddr: "[::]:30304",
name: "Geth/v1.7.3-stable/darwin-amd64/go1.9.2",
ports: {
discovery: 30304,
listener: 30304
},
protocols: {
eth: {
difficulty: 9972291,
genesis: "0x5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0",
head: "0xbcc9172f244c3c86d15b04451d2d72dca6953cc1838910f35f486149a37ddc87",
network: 1108
}
}
}
第一個節(jié)點(diǎn)輸入:
> net.peerCount
2
> admin.peers
[{
caps: ["eth/63"],
id: "61eac7f7900bd17cac39ce506cd20c1f67a2a43fb5f5c7a46ace337922a9e325a63a7aa9cb18df12715550ffa0b66c44272132b93245d15825c2ac2c1ddb8cd8",
name: "Geth/v1.7.3-stable/darwin-amd64/go1.9.2",
network: {
localAddress: "172.16.70.76:30303",
remoteAddress: "172.16.70.76:53634"
},
protocols: {
eth: {
difficulty: 131072,
head: "0x5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0",
version: 63
}
}
}]
從得到的結(jié)果可以看出,第一個節(jié)點(diǎn)有1個peer鏈接, 鏈接的node id為:
"61eac7f7900bd17cac39ce506cd20c1f67a2a43fb5f5c7a46ace337922a9e325a63a7aa9cb18df12715550ffa0b66c44272132b93245d15825c2ac2c1ddb8cd8"
這個id,正好就是第二個節(jié)點(diǎn)的id.
鏈接成功后,使用第一個節(jié)點(diǎn)挖礦的賬戶,向第二個節(jié)點(diǎn)發(fā)送 "ether"
首先查看第二個節(jié)點(diǎn)的Wei數(shù)量和整個網(wǎng)絡(luò)的區(qū)塊號,還有接收貨幣的賬號id:
> eth.getBalance(eth.accounts[0])
0
> eth.blockNumber
73
> eth.accounts[0]
"0x0c7df03a68bd85ef3abcfc63a6b788a2faf8ef27"
>
在第一個節(jié)點(diǎn)命令行中,執(zhí)行下面的操作:
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x0416f04c403099184689990674f5b4259dc46bd8
Passphrase:
true
> eth.sendTransaction({from:"0x0416f04c403099184689990674f5b4259dc46bd8",to:"0x0c7df03a68bd85ef3abcfc63a6b788a2faf8ef27",value:web3.toWei(1, "ether")})
INFO [03-01|15:03:04] Submitted transaction fullhash=0xb92d256b66c336c9ffa5c1872bb5b20b6b704d7fbc4dd7aa3cdb97b8668d407d recipient=0x0C7dF03a68Bd85EF3abcfC63A6b788a2fAf8EF27
"0xb92d256b66c336c9ffa5c1872bb5b20b6b704d7fbc4dd7aa3cdb97b8668d407d"
> eth.pendingTransactions
[{
blockHash: null,
blockNumber: null,
from: "0x0416f04c403099184689990674f5b4259dc46bd8",
gas: 90000,
gasPrice: 18000000000,
hash: "0xb92d256b66c336c9ffa5c1872bb5b20b6b704d7fbc4dd7aa3cdb97b8668d407d",
input: "0x",
nonce: 5,
r: "0xfd4d1b29e0ae3bce53599d4e0f89a093b1b483de2cf85f6acdb66c88dd594242",
s: "0x60b4a92b9a2744b3c2f9cd5c5cd0d406c93b4b18877d4cb6357ec7946019bfe2",
to: "0x0c7df03a68bd85ef3abcfc63a6b788a2faf8ef27",
transactionIndex: 0,
v: "0x37",
value: 1000000000000000000
}]
eth.sendTransaction就是執(zhí)行發(fā)送以太幣的操作, 參數(shù)from, to分別是發(fā)送賬戶和接收賬戶, web3.toWei(1, "ether")是將1單位"ether"轉(zhuǎn)換為相應(yīng)的"Wei"數(shù)量.
然后執(zhí)行挖礦
> miner.start(1);admin.sleepBlocks(1);miner.stop();
INFO [03-01|15:05:47] Updated mining threads threads=1
INFO [03-01|15:05:47] Transaction pool price threshold updated price=18000000000
INFO [03-01|15:05:47] Starting mining operation
INFO [03-01|15:05:47] Commit new mining work number=74 txs=1 uncles=0 elapsed=388.33μs
INFO [03-01|15:06:23] Successfully sealed new block number=74 hash=bcc917…7ddc87
INFO [03-01|15:06:23] mined potential block number=74 hash=bcc917…7ddc87
INFO [03-01|15:06:23] Commit new mining work number=75 txs=0 uncles=0 elapsed=202.123μs
true
再在第二個節(jié)點(diǎn)的命令行輸入:
> eth.blockNumber
74
> eth.getBalance(eth.accounts[0])
1000000000000000000
可以看到第二個節(jié)點(diǎn)中的賬戶,已經(jīng)得有了1個"ether", 并且可以看出,以太坊中,1"ether"=1000000000000000000"Wei.
之前輸入eth.blockNumber,得到的值為73,現(xiàn)在增加了一個區(qū)塊變成了74
總結(jié)一下:
這次我們完成了以下內(nèi)容:
1)創(chuàng)建區(qū)塊鏈私有網(wǎng)絡(luò),并在網(wǎng)絡(luò)中建立多節(jié)點(diǎn)集群
2)在多節(jié)點(diǎn)環(huán)境中,通過一個節(jié)點(diǎn)的賬戶向網(wǎng)絡(luò)中的另外一個節(jié)點(diǎn)的賬戶轉(zhuǎn)了1個以太坊幣,交易成功.