以太坊多节点(两个节点)私链搭建

一:环境说明

节点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)

二:两节点的环境搭建

请参见以太坊单节点私链搭建(go+geth+Etherum Wallet)博文中的一、二、三、四、五

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://7dafd414a337e4f8673d1de4d1722863f969bb83cf75001b4d40a87e876c7c22c2033f1bc96dcaf147f30b23eb43d5725c70dd555fa2d8c2b32a98aaca4504cb@[::]: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://84710df0914497ca2ee428deb7da7520514131e354ea062d0aefd523ebe597ec453d37afe7aa75fce0bae60c7d6fedab713a46a80608bcc8e140215967d50b66@[::]: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")

你可能感兴趣的:(环境搭建,区块链)