iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 8
0
Blockchain

以太坊-探索智能合約的多種面向系列 第 8

Day 8 搭建私有鏈(三) - 在私有鏈中觀察同步

  • 分享至 

  • xImage
  •  

在前述的私有鏈操作,描述的是單一節點的運作,在以太坊的主要網路中有多個節點彼此連結,並且同步更新最新的狀態,在私有鏈網路中加入其他節點,觀察這種同步情形。
-在第一個節點的控制台,使用net.listening 和 net.peerCount 來觀察單個節點的狀態 :

>net.peerCount  <= 回傳連接節點個數
0
>net.listening   <= 回傳是否監聽節點
true

-開啟第二個Terminal,創造第二個節點,使用genesis.json檔案再開啟一個私有鏈,放在不同的資料夾。
genesis.json檔案範例 :

{
 "config": {
"chainId":20,
"homesteadBlock":0,
"eip155Block":0,
"eip158Block":0},
"alloc" :{
"0xa3754f5b8277b6733ce633f52b5ff229bec012de":{"balance":"10000000000000000000000"}},
"coinbase" :"0x0000000000000000000000000000000000000000",
"difficulty" :"0x01",
"extradata" :"0x12",
"gasLimit" :"0xffffffff",
"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" :"0x00"
}

-將genesis.json初始化

geth  --datadir "./node2" init genesis.json

參考前述文章,在node2資料夾,產生geth資料夾和Keystore資料夾,開啟節點 :

geth --datadir "./node2" --rpc --rpcaddr=0.0.0.0 --rpcport (自訂,不能跟第一個節點設置相同的值) --rpcapi "eth,web3,personal,admin,net" --port (自訂,不能跟第一個節點設置相同的值) --nodiscover(關閉自動連接節點) console 
INFO [10-23|18:56:50.073] Maximum peer count                       ETH=25 LES=0 total=25
INFO [10-23|18:56:50.074] Starting peer-to-peer node               instance=Geth/v1.8.18-unstable-88b41a9e/linux-amd64/go1.10.4
INFO [10-23|18:56:50.074] Allocated cache and file handles         database=/home/name/Iron/node2/geth/chaindata cache=768 handles=512
INFO [10-23|18:56:50.266] Initialised chain configuration          config="{ChainID: 20 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> Engine: unknown}"
INFO [10-23|18:56:50.266] Disk storage enabled for ethash caches   dir=/home/name/Iron/node2/geth/ethash count=3
INFO [10-23|18:56:50.266] Disk storage enabled for ethash DAGs     dir=/home/name/.ethash                count=2
INFO [10-23|18:56:50.266] Initialising Ethereum protocol           versions="[63 62]" network=1
INFO [10-23|18:56:50.267] Loaded most recent local header          number=12 hash=6b2e8f…ed23c0 td=1575745 age=1d20h1m
INFO [10-23|18:56:50.267] Loaded most recent local full block      number=12 hash=6b2e8f…ed23c0 td=1575745 age=1d20h1m
INFO [10-23|18:56:50.267] Loaded most recent local fast block      number=12 hash=6b2e8f…ed23c0 td=1575745 age=1d20h1m
INFO [10-23|18:56:50.267] Loaded local transaction journal         transactions=0 dropped=0
INFO [10-23|18:56:50.267] Regenerated local transaction journal    transactions=0 accounts=0
WARN [10-23|18:56:50.267] Blockchain not empty, fast sync disabled 
INFO [10-23|18:56:50.494] New local node record                    seq=8 id=6e6b9a2d129850ee ip=127.0.0.1 udp=0 tcp=30305
INFO [10-23|18:56:50.494] Started P2P networking                   self="enode://8603b8f21f6c7888f2bcd1030a83935c63bff9e60da41609777ebfc9790496a0cdf16668e6a064d6d20a2e49abdd3dee4de925a2661f5527b0f03eb95d43df89@127.0.0.1:30305?discport=0"
INFO [10-23|18:56:50.495] IPC endpoint opened                      url=/home/name /Iron/node2/geth.ipc
INFO [10-23|18:56:50.496] HTTP endpoint opened                     url=http://0.0.0.0:8540           cors= vhosts=localhost
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.18-unstable-88b41a9e/linux-amd64/go1.10.4
 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    <= 註記ChainId必須相同才能進行節點連接。

-輸入net.listening 和 net.peerCount :

> net.listening    <= 確認監聽節點的狀態
true
>net.peerCount    <= 確認連接節點的數量  
0

-輸入 : admin.nodeInfo <= 查詢節點資訊

> admin.nodeInfo
{
  enode: "enode://8603b8f21f6c7888f2bcd1030a83935c63bff9e60da41609777ebfc9790496a0cdf16668e6a064d6d20a2e49abdd3dee4de925a2661f5527b0f03eb95d43df89@127.0.0.1:30305?discport=0",
  enr: "0xf895b840326b513f4ebd5bc4aa8bdc9e02b2fd2c08ffac757da589b96ca173ca9edba706713813493043a4f0246464b71e730a829d56ac1ff6a90b9c6ff115fd070a30260883636170ccc5836574683ec5836574683f826964827634826970847f00000189736563703235366b31a1038603b8f21f6c7888f2bcd1030a83935c63bff9e60da41609777ebfc9790496a083746370827661",
  id: "6e6b9a2d129850ee0353d9872212c5120f02a2d7e3a02871f3f6d92c6e9dfa95",
  ip: "127.0.0.1",
  listenAddr: "[::]:30305",
  name: "Geth/v1.8.18-unstable-88b41a9e/linux-amd64/go1.10.4",
  ports: {
    discovery: 0,
    listener: 30305
  },
  protocols: {
    eth: {
      config: {
        chainId: 20,
        eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        eip155Block: 0,
        eip158Block: 0,
        homesteadBlock: 0
      },
      difficulty: 1575745,
      genesis: "0x68eb29f76a6d60413df91e17a3ec53a41208e9f08907f2a6d4a3e36ec9695376",
      head: "0x6b2e8fd8e70880706a743a27253fbc7774a96b052193d81c2f6c1c6070ed23c0",
      network: 1
    }
  }
}         <=  enode為節點資訊。

-回到第一個節點的控制台。
輸入 :

>admin.addPeer("enode://8603b8f21f6c7888f2bcd1030a83935c63bff9e60da41609777ebfc9790496a0cdf16668e6a064d6d20a2e49abdd3dee4de925a2661f5527b0f03eb95d43df89@127.0.0.1:30305?discport=0")  < =參數是第二個節點的節點資訊。
>true   <= 連結成功

-查看已連接的節點。
輸入 admin.peers :

> admin.peers
[{
    caps: ["eth/62", "eth/63"],
    enode: "enode://8603b8f21f6c7888f2bcd1030a83935c63bff9e60da41609777ebfc9790496a0cdf16668e6a064d6d20a2e49abdd3dee4de925a2661f5527b0f03eb95d43df89@127.0.0.1:30305?discport=0",
    id: "6e6b9a2d129850ee0353d9872212c5120f02a2d7e3a02871f3f6d92c6e9dfa95",
    name: "Geth/v1.8.18-unstable-88b41a9e/linux-amd64/go1.10.4",
    network: {
      inbound: false,
      localAddress: "127.0.0.1:60290",
      remoteAddress: "127.0.0.1:30305",
      static: true,
      trusted: false
    },
    protocols: {
      eth: {
        difficulty: 1575745,
        head: "0x6b2e8fd8e70880706a743a27253fbc7774a96b052193d81c2f6c1c6070ed23c0",
        version: 63
      }
    }
}] 

連接成功。


上一篇
Day 7 搭建私有鏈(二) - 交易,轉帳,查詢
下一篇
Day 9 搭建私有鏈(三) - 交易,同步數據
系列文
以太坊-探索智能合約的多種面向20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
akce25874
iT邦新手 5 級 ‧ 2021-09-14 13:28:38

哈囉~請問一下Day8要跟第一個節點做連結時,回到第一個節點控制台的意思是說要在執行一次第一個節點並同時輸入admin.addPeer("")的指令嗎
因為我是這樣輸入可是跳出error :'admin.addPeer' 不是內部或外部命令、可執行的程式或批次檔。
剛接觸不太清楚可以請您幫我解解惑嗎~~謝謝您

我要留言

立即登入留言