搭建多節點的私鏈
阿新 • • 發佈:2019-01-09
為了在本地網路執行多個以太坊節點的例項,必須確保一下幾點:
1. 每個例項都有獨立的資料目錄(–datadir)
2. 每個例項執行都有獨立的埠.(eth和rpc兩者都是)(–port 和 –rpcprot)
3. 在叢集的情況下, 例項之間都必須要知道彼此.
4. 唯一的ipc通訊端點,或者禁用ipc.第一個節點
這裡主要還要指定要連線的主機的地址,也就是–bootnodes引數,該引數可以在主機啟動時的日誌中看到,也可以通過console中輸入命令admin.nodeInfo看到(列印結果的enode的值便是,不過如果是內網,則需要把主機ip修改成內網ip)。當然,實際情況下不止兩臺機,因此可以在data/下建立檔案static-nodes.json,將所有節點的配置都寫在裡邊,參考官網: https://ethereum.gitbooks.io/frontier-guide/content/connecting.html最後部分。
附:
1.在啟動“子”節點的時候bootnodes引數是為了讓它直接去找這個ip,否則它會在全網去嘗試掃描所有節點進行匹配,這樣掃到主機的可能性太低了。可以在啟動時帶引數–verbosity 6即可看到其掃描詳情。
2.禁掉私鏈以外的網路,每個節點啟動時多加兩個引數:–nodiscover –nat “none”
3.經嘗試,在位元組點連線主節點時使用檔案配置的方式(static-nodes.json)是一定會成功的,在命令列配置–bootnodes引數不一定能成功
1啟動第一個節點(指定埠,並禁用ipc),執行命令和結果如下:
[email protected]5tthrr8u:/home/ubuntu/private-geth# geth --datadir ./data/00 --networkid 314590 --ipcdisable --port 61910 --rpcport 8200 console
INFO [07-02|17:18:55] Starting peer-to-peer node instance=Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
INFO [07-02|17 :18:55] Allocated cache and file handles database=/home/ubuntu/private-geth/data/00/geth/chaindata cache=128 handles=1024
Fatal: Error starting protocol stack: resource temporarily unavailable
[email protected]5tthrr8u:/home/ubuntu/private-geth# geth --datadir ./data/00 --networkid 314590 --ipcdisable --port 61910 --rpcport 8200 console
INFO [07-02|17:24:00] Starting peer-to-peer node instance=Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
INFO [07-02|17:24:00] Allocated cache and file handles database=/home/ubuntu/private-geth/data/00/geth/chaindata cache=128 handles=1024
INFO [07-02|17:24:00] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Metropolis: <nil> Engine: unknown}"
INFO [07-02|17:24:00] Disk storage enabled for ethash caches dir=/home/ubuntu/private-geth/data/00/geth/ethash count=3
INFO [07-02|17:24:00] Disk storage enabled for ethash DAGs dir=/root/.ethash count=2
INFO [07-02|17:24:00] Initialising Ethereum protocol versions="[63 62]" network=314590
INFO [07-02|17:24:00] Loaded most recent local header number=9 hash=05bf0e鈥4a3fe td=2462668
INFO [07-02|17:24:00] Loaded most recent local full block number=9 hash=05bf0e鈥4a3fe td=2462668
INFO [07-02|17:24:00] Loaded most recent local fast block number=9 hash=05bf0e鈥4a3fe td=2462668
WARN [07-02|17:24:00] Blockchain not empty, fast sync disabled
INFO [07-02|17:24:00] Starting P2P networking
INFO [07-02|17:24:02] UDP listener up self=enode://96f7a95a8c5e7a1f911142d87ce53ae882568be0435016e4803a6d410a9ecc7a6[email protected][::]:61910
INFO [07-02|17:24:02] RLPx listener up self=enode://96f7a95a8c5e7a1f911142d87ce53ae882568be0435016e4803a6d410a9ecc7a6[email protected][::]:61910
Welcome to the Geth JavaScript console!
instance: Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
coinbase: 0x28b769b3b9109afd1e9e50a9312c5a3bfae8a699
at block: 9 (Sun, 02 Jul 2017 17:19:16 CST)
datadir: /home/ubuntu/private-geth/data/00
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
> eth.accounts
["0x28b769b3b9109afd1e9e50a9312c5a3bfae8a699", "0xb4e2e2514eae3684157bf34a0cee2c07c431cf92"]
上面的命令以命令列的(console)的方式啟動了節點, 所以我們可以通過繼續輸入下面的命令獲取節點例項的enode url:
> admin.nodeInfo.enode
"enode://96f7a95a8c5e7a1f911142d87ce53ae882568be0435016e4803a6d410a9ecc7a6[email protected][::]:61910"
//這一部分是在連線節點後在第一個節點檢視的資訊
> net.peerCount
2
> admin.peers
[{
caps: ["eth/63"],
id: "b3c5cab11033dc6de9deacef1f5d1f5fc74aad685558ef3f3fabc51e9d0417c8d28d09cf87c81f98f333def35d97df960a888ca1ccd470b26a442a8c8aa4e82b",
name: "Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1",
network: {
localAddress: "192.168.0.4:61910",
remoteAddress: "192.168.0.4:43322"
},
protocols: {
eth: {
difficulty: 262144,
head: "0xa0e580c6769ac3dd80894b2a256164a76b796839d2eb7f799ef6b9850ea5e82e",
version: 63
}
}
}]
再開啟一個終端,初始化第二個節點:
[email protected]:/home/ubuntu# cd private-geth/
[email protected]:/home/ubuntu/private-geth# geth --datadir ./data/01 init ./genesis.json
WARN [07-02|17:26:27] No etherbase set and no accounts found as default
INFO [07-02|17:26:27] Allocated cache and file handles database=/home/ubuntu/private-geth/data/01/geth/chaindata cache=16 handles=16
INFO [07-02|17:26:27] Writing custom genesis block
INFO [07-02|17:26:27] Successfully wrote genesis state database=chaindata hash=a0e580鈥5e82e
INFO [07-02|17:26:27] Allocated cache and file handles database=/home/ubuntu/private-geth/data/01/geth/lightchaindata cache=16 handles=16
INFO [07-02|17:26:27] Writing custom genesis block
INFO [07-02|17:26:27] Successfully wrote genesis state database=lightchaindata hash=a0e580鈥5e82e
[email protected]5tthrr8u:/home/ubuntu/private-geth# geth --datadir ./data/01 --networkid 314590 --ipcdisable --port 61911 --rpcport 8101 --bootnodes "enode://96f7a95a8c5e7a1f911142d87ce53ae882568be0435016e4803a6d410a9ecc7a6[email protected]192.168.0.4:61910" console
WARN [07-02|17:34:44] No etherbase set and no accounts found as default
INFO [07-02|17:34:44] Starting peer-to-peer node instance=Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
INFO [07-02|17:34:44] Allocated cache and file handles database=/home/ubuntu/private-geth/data/01/geth/chaindata cache=128 handles=1024
WARN [07-02|17:34:44] Upgrading chain database to use sequential keys
INFO [07-02|17:34:44] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Metropolis: <nil> Engine: unknown}"
INFO [07-02|17:34:44] Disk storage enabled for ethash caches dir=/home/ubuntu/private-geth/data/01/geth/ethash count=3
INFO [07-02|17:34:44] Disk storage enabled for ethash DAGs dir=/root/.ethash count=2
INFO [07-02|17:34:44] Database conversion successful
WARN [07-02|17:34:44] Upgrading db log bloom bins
INFO [07-02|17:34:44] Bloom-bin upgrade completed elapsed=211.238碌s
INFO [07-02|17:34:44] Initialising Ethereum protocol versions="[63 62]" network=314590
INFO [07-02|17:34:44] Loaded most recent local header number=0 hash=a0e580鈥5e82e td=262144
INFO [07-02|17:34:44] Loaded most recent local full block number=0 hash=a0e580鈥5e82e td=262144
INFO [07-02|17:34:44] Loaded most recent local fast block number=0 hash=a0e580鈥5e82e td=262144
INFO [07-02|17:34:44] Starting P2P networking
INFO [07-02|17:34:47] UDP listener up self=enode://b3c5cab11033dc6de9deacef1f5d1f5fc74aad685558ef3f3fabc51e9d0417c8d[email protected][::]:61911
INFO [07-02|17:34:47] RLPx listener up self=enode://b3c5cab11033dc6de9deacef1f5d1f5fc74aad685558ef3f3fabc51e9d0417c8d[email protected][::]:61911
Welcome to the Geth JavaScript console!
instance: Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.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
> eth.accounts
[]
> INFO [07-02|17:35:17] Block synchronisation started
INFO [07-02|17:35:17] Imported new state entries count=1 flushed=0 elapsed=60.525碌s processed=1 pending=3 retry=0 duplicate=0 unexpected=0
INFO [07-02|17:35:17] Imported new state entries count=2 flushed=3 elapsed=98.756碌s processed=3 pending=0 retry=0 duplicate=0 unexpected=0
INFO [07-02|17:35:18] Imported new block headers count=9 elapsed=1.464s number=9 hash=05bf0e鈥4a3fe ignored=0
INFO [07-02|17:35:18] Imported new chain segment blocks=6 txs=0 mgas=0.000 elapsed=1.633ms mgasps=0.000 number=6 hash=391702鈥132d5
INFO [07-02|17:35:18] Imported new chain segment blocks=3 txs=1 mgas=0.021 elapsed=1.444ms mgasps=14.536 number=9 hash=05bf0e鈥4a3fe
INFO [07-02|17:35:18] Fast sync complete, auto disabling
>
>
> eth.accounts
[]
上面的命令中,--bootndoes 是設定當前節點啟動後,直接通過設定--bootndoes 的值來連結第一個節點, --bootnoedes 的值可以通過在第一個節的命令列中,輸入:admin.nodeInfo.enode命令打印出來.
也可以不設定 --bootnodes, 直接啟動,啟動後進入命令列, 通過命令admin.addPeer(enodeUrlOfFirst Instance)把它作為一個peer新增進來.
為了確認連結成功,第二個節點輸入:
> admin.nodeInfo
{
enode: "enode://b3c5cab11033dc6de9deacef1f5d1f5fc74aad685558ef3f3fabc51e9d0417c8d[email protected][::]:61911",
id: "b3c5cab11033dc6de9deacef1f5d1f5fc74aad685558ef3f3fabc51e9d0417c8d28d09cf87c81f98f333def35d97df960a888ca1ccd470b26a442a8c8aa4e82b",
ip: "::",
listenAddr: "[::]:61911",
name: "Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1",
ports: {
discovery: 61911,
listener: 61911
},
protocols: {
eth: {
difficulty: 2462668,
genesis: "0xa0e580c6769ac3dd80894b2a256164a76b796839d2eb7f799ef6b9850ea5e82e",
head: "0x05bf0eccdaab8113a2f62fa975790ecb4afc09c0614f1fa7b9d617a45dc4a3fe",
network: 314590
}
}
}
> net.peerCount
2
> admin.peers
[{
caps: ["eth/62", "eth/63"],
id: "96f7a95a8c5e7a1f911142d87ce53ae882568be0435016e4803a6d410a9ecc7a69322cee8efbe8499ec7c737c377793182cab8869a076389c6b47626df00450b",
name: "Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1",
network: {
localAddress: "192.168.0.4:43322",
remoteAddress: "192.168.0.4:61910"
},
protocols: {
eth: {
difficulty: 2462668,
head: "0x05bf0eccdaab8113a2f62fa975790ecb4afc09c0614f1fa7b9d617a45dc4a3fe",
version: 63
}
}
}]
第三個節點
[email protected]:/home/ubuntu/private-geth# geth --datadir ./data/02 init ./genesis.json
WARN [07-02|17:41:33] No etherbase set and no accounts found as default
INFO [07-02|17:41:33] Allocated cache and file handles database=/home/ubuntu/private-geth/data/02/geth/chaindata cache=16 handles=16
INFO [07-02|17:41:33] Writing custom genesis block
INFO [07-02|17:41:33] Successfully wrote genesis state database=chaindata hash=a0e580鈥5e82e
INFO [07-02|17:41:33] Allocated cache and file handles database=/home/ubuntu/private-geth/data/02/geth/lightchaindata cache=16 handles=16
INFO [07-02|17:41:33] Writing custom genesis block
INFO [07-02|17:41:33] Successfully wrote genesis state database=lightchaindata hash=a0e580鈥5e82e
[email protected]5tthrr8u:/home/ubuntu/private-geth# geth --datadir ./data/02 --networkid 314590 --ipcdisable --port 61912 --rpcport 8122 --bootnodes "enode://96f7a95a8c5e7a1f911142d87ce53ae882568be0435016e4803a6d410a9ecc7a6[email protected]192.168.0.4:61910" console
WARN [07-02|17:43:25] No etherbase set and no accounts found as default
INFO [07-02|17:43:25] Starting peer-to-peer node instance=Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
INFO [07-02|17:43:25] Allocated cache and file handles database=/home/ubuntu/private-geth/data/02/geth/chaindata cache=128 handles=1024
WARN [07-02|17:43:25] Upgrading chain database to use sequential keys
INFO [07-02|17:43:25] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Metropolis: <nil> Engine: unknown}"
INFO [07-02|17:43:25] Disk storage enabled for ethash caches dir=/home/ubuntu/private-geth/data/02/geth/ethash count=3
INFO [07-02|17:43:25] Disk storage enabled for ethash DAGs dir=/root/.ethash count=2
WARN [07-02|17:43:25] Upgrading db log bloom bins
INFO [07-02|17:43:25] Bloom-bin upgrade completed elapsed=459.084碌s
INFO [07-02|17:43:25] Initialising Ethereum protocol versions="[63 62]" network=314590
INFO [07-02|17:43:25] Loaded most recent local header number=0 hash=a0e580鈥5e82e td=262144
INFO [07-02|17:43:25] Loaded most recent local full block number=0 hash=a0e580鈥5e82e td=262144
INFO [07-02|17:43:25] Loaded most recent local fast block number=0 hash=a0e580鈥5e82e td=262144
INFO [07-02|17:43:25] Starting P2P networking
INFO [07-02|17:43:25] Database conversion successful
INFO [07-02|17:43:27] UDP listener up self=enode://1318fc1266e4722e299e51ba28ba32e045e3c01cab00f99458e6de399737ac05b[email protected][::]:61912
INFO [07-02|17:43:27] RLPx listener up self=enode://1318fc1266e4722e299e51ba28ba32e045e3c01cab00f99458e6de399737ac05b[email protected][::]:61912
Welcome to the Geth JavaScript console!
instance: Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.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
> eth.accounts
[]
> net.peerCount
1
> admin.peers
[{
caps: ["eth/62", "eth/63"],
id: "96f7a95a8c5e7a1f911142d87ce53ae882568be0435016e4803a6d410a9ecc7a69322cee8efbe8499ec7c737c377793182cab8869a076389c6b47626df00450b",
name: "Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1",
network: {
localAddress: "192.168.0.4:47940",
remoteAddress: "192.168.0.4:61910"
},
protocols: {
eth: {
difficulty: 2462668,
head: "0x05bf0eccdaab8113a2f62fa975790ecb4afc09c0614f1fa7b9d617a45dc4a3fe",
version: 63
}
}
}]
>
> eth.blockNumber
9
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x28b769b3b9109afd1e9e50a9312c5a3bfae8a699
Passphrase:
true
> eth.sendTransaction({from: "0x28b769b3b9109afd1e9e50a9312c5a3bfae8a699", to: "0x6d6ce75de6e3a5db2a56c67b00e895ba320174bc", gas:31000, 'gasPrice': web3.toWei(300, 'gwei'), "value": "1"})
INFO [07-02|17:55:31] Submitted transaction fullhash=0x7f8c2b4e85a882735d735f0d8ff3d85ddb666b5ba7215566817093594677f604 recipient=0x6d6ce75de6e3a5db2a56c67b00e895ba320174bc
"0x7f8c2b4e85a882735d735f0d8ff3d85ddb666b5ba7215566817093594677f604"
> txpool.status
{
pending: 2,
queued: 0
}
> miner.start()