1. 程式人生 > >以太坊多節點(兩個節點)私鏈搭建

以太坊多節點(兩個節點)私鏈搭建

一:環境說明

節點1:
1.ubuntu16.04 64位 (ip:172.16.29.11)
2.golang (1.10.3)
3.geth (1.8.12)
4.Etherum Wallet (Etherum-Wallet-liunx64-0-11-1.zip)

節點2:
1.ubuntu16.04 64位 (ip:172.16.29.12)
2.golang (1.10.3)
3.geth (1.8.12)
4.Etherum Wallet (Etherum-Wallet-liunx64-0-11-1.zip)

二:兩節點的環境搭建

2.1創世塊檔案

注意:每個節點的初始化創世塊,都是用此同一個檔案
本檔案在3.1.1 、 3.2.1 中用到

新建檔案genesis.json,內容如下

{
  "config": {
    "chainId": 101,
    "homesteadBlock": 0,
    "eip155Block": 0,
    "eip158Block": 0
  },
  "alloc": {},
  "coinbase": "0x0000000000000000000000000000000000000000",
  "difficulty": "0x400",
  "extraData": "",
  "gasLimit": "0xffffffff",
  "nonce": "0x0000000000000042"
, "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp": "0x00" }

三:啟動節點

注意
使用命令 geth -h 可以檢視geth 相關的幫助文件。這裡我說幾個常用的屬性。
- -Identity : 節點身份標識,起個名字
- -datadir : 指定節點存在位置,“data0”
- -rpc : 啟用http-rpc伺服器
- -rpcapi : 基於http-rpc提供的api介面。eth,net,web3,db…
- -rpcaddr : http-rpc伺服器介面地址:預設“127.0.0.1”
- -rpcport : http-rpc 埠(多節點時,不要重複)
- -port : 節點埠號(多節點時,不要重複)
- -networkid : 網路識別符號 隨便指定一個id(確保多節點是統一網路,保持一致)

3.1節點1

3.1.1初始化節點1的創世塊

> geth --datadir  ~/EthDBSpace  init  ~/genesis.json  # genesis.json檔案~/目錄下

3.1.2啟動節點1

> geth  --datadir ./EthDBSpace --rpcport 8481 --port 30301 
--networkid 29381 console
......
> admin.nodeInfo  # 檢視本節點資訊,獲取enode
{
  enode: "enode://7dafd414a337e4f8673d1de4d1722863f969bb83cf75001b4d40a87e876c7c22c[email protected][::]:30301",
  id: "7dafd414a337e4f8673d1de4d1722863f969bb83cf75001b4d40a87e876c7c22c2033f1bc96dcaf147f30b23eb43d5725c70dd555fa2d8c2b32a98aaca4504cb",
  ip: "::",
  listenAddr: "[::]:30301",
  name: "Geth/v1.8.13-unstable/linux-amd64/go1.10.3",
  ports: {
    discovery: 30301,
    listener: 30301
  },
  protocols: {
    eth: {
      config: {
        byzantiumBlock: 4370000,
        chainId: 1,
        daoForkBlock: 1920000,
        daoForkSupport: true,
        eip150Block: 2463000,
        eip150Hash: "0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0",
        eip155Block: 2675000,
        eip158Block: 2675000,
        ethash: {},
        homesteadBlock: 1150000
      },
      difficulty: 34351349760,
      genesis: "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
      head: "0xceb58c35f0b3424bc3e252cf5457b0e9d06acf1acc876fb024f18173c8d759f5",
      network: 29381
    }
  }
}

3.2:節點2

3.2.1初始化節點2的創世塊

> geth --datadir  ~/EthDBSpace  init  ~/genesis.json  # genesis.json檔案~/目錄下

3.2.2啟動節點2

> geth  --datadir ./EthDBSpace --rpcport 8482 --port 30302 --networkid 29381 
--bootnodes "enode://[email protected]:30301" console  # 指定節點1的enode,並將[::]替換成節點1的ip
> admin.nodeInfo # 檢視本節點資訊
{
  enode: "enode://84710df0914497ca2ee428deb7da7520514131e354ea062d0aefd523ebe597ec4[email protected][::]:30302",
  id: "84710df0914497ca2ee428deb7da7520514131e354ea062d0aefd523ebe597ec453d37afe7aa75fce0bae60c7d6fedab713a46a80608bcc8e140215967d50b66",
  ip: "::",
  listenAddr: "[::]:30302",
  name: "Geth/v1.8.13-unstable/linux-amd64/go1.10.3",
  ports: {
    discovery: 30302,
    listener: 30302
  },
  protocols: {
    eth: {
      config: {
        byzantiumBlock: 4370000,
        chainId: 1,
        daoForkBlock: 1920000,
        daoForkSupport: true,
        eip150Block: 2463000,
        eip150Hash: "0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0",
        eip155Block: 2675000,
        eip158Block: 2675000,
        ethash: {},
        homesteadBlock: 1150000
      },
      difficulty: 17179869184,
      genesis: "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
      head: "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
      network: 29381
    }
  }
}

3.3驗證節點2是否已連線到私鏈

在節點1執行命令:

> admin.peers  # 可以看到這裡的id即是節點2的enode
[{
    caps: ["eth/63"],
    id: "84710df0914497ca2ee428deb7da7520514131e354ea062d0aefd523ebe597ec453d37afe7aa75fce0bae60c7d6fedab713a46a80608bcc8e140215967d50b66",
    name: "Geth/v1.8.13-unstable/linux-amd64/go1.10.3",
    network: {
      inbound: true,
      localAddress: "172.16.29.11:30301",
      remoteAddress: "172.16.29.12:59424",
      static: false,
      trusted: false
    },
    protocols: {
      eth: {
        difficulty: 17179869184,
        head: "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
        version: 63
      }
    }
}]

另一種連線私鏈的方式:
啟動節點時,不指定bootnodes,即:

> geth  --datadir ./EthDBSpace --rpcport 8482 --port 30302 --networkid 29381 >console

節點啟動後,使用命令:

> admin.addPeer("enode://[email protected]:30301")