美图DPOS共识机制ETH节点网络启动和测试

源码

https://github.com/meitu/go-ethereum

节点网络启动 

1、简单测试起见,需要将节点最大验证器数修改为最小数
consensus/dpos/dpos.go

maxValidatorSize = 3

2、构建以太坊docker镜像

cd $GOPATH/src/github.com/meitu/go-ethereum
docker build . -t mgeth

3、创建节点数据目录

mkdir meitu
cd meitu
mkdir node1
mkdir node2
mkdir node3

4、编写docker-compose.yml启动文件

version: '3.3'
services:
  mgeth_node_1:
    image: mgeth
    container_name: mgeth_node_1
    build: 
      context: ..
    command: --ipcpath "/root/.ethereum/geth.ipc" --port 30303
    ports:
      - "15450:8545"
      - "15460:8546"
      - "10303:30303"
      - "10303:30303/udp"
      - "10304:30304/udp"
    volumes:
      - /etc/localtime:/etc/localtime
      - ./node1/:/root/.ethereum/
    environment:
      - TZ=Asia/Shanghai
 
  mgeth_node_2:
    image: mgeth
    container_name: mgeth_node_2
    build: 
      context: ..
    command: --ipcpath "/root/.ethereum/geth.ipc" --port 30303
    depends_on:
      - mgeth_node_1
    ports:
      - "25450:8545"
      - "25460:8546"
      - "20303:30303"
      - "20303:30303/udp"
      - "20304:30304/udp"
    volumes:
      - /etc/localtime:/etc/localtime
      - ./node2/:/root/.ethereum/
    environment:
      - TZ=Asia/Shanghai
 
  mgeth_node_3:
    image: mgeth
    container_name: mgeth_node_3
    build: 
      context: ..
    command: --ipcpath "/root/.ethereum/geth.ipc" --port 30303
    depends_on:
      - mgeth_node_1
    ports:
      - "45450:8545"
      - "45460:8546"
      - "40303:30303"
      - "40303:30303/udp"
      - "40304:30304/udp"
    volumes:
      - /etc/localtime:/etc/localtime
      - ./node3/:/root/.ethereum/
    environment:
      - TZ=Asia/Shanghai

5、在meitu目录下启动3个以太坊节点

docker-compose up -d

启动思路:

  • 在创世块里配置好第一批验证节点,然后启动。
  • 混合POW和DPOS,用POW进行投票,产生第一批验证节点,并自动切换到DPOS。

6、进入以太坊节点容器

docker exec -it mgeth_node_1 /bin/sh
docker exec -it mgeth_node_2 /bin/sh
docker exec -it mgeth_node_3 /bin/sh

7、获取geth JavaScript控制台

geth attach ipc:/root/.ethereum/geth.ipc

或者可以

docker exec -it mgeth_node_1 geth attach ipc:/root/.ethereum/geth.ipc

8、在node1节点建立coinbase

personal.newAccount('xxxx')

"0x8807fa0db2c60675a8f833dd010469e408428b83"

记录下上述地址。 

9、退出node1容器或者打开新窗口进入node2、node3

重复6-9步骤,在node2和node3上分别设立coinbase。

3个节点的地址分别为:

0x8807fa0db2c60675a8f833dd010469e408428b83
0xdf5f5a7abc5d0821c50deb4368528d8691f18737
0xe0d64bfb1a30d66ae0f06ce36d5f4edf6835cd7c

10、写创世块配置文件,把3个节点地址列入第一批验证人列表。
mt_genesis.json

{
    "config": {
        "chainId": 8888,
        "eip155Block": 0,
        "eip158Block": 0,
        "byzantiumBlock":0,
        "dpos":{
            "validators":[
                "0x8807fa0db2c60675a8f833dd010469e408428b83",
                "0xdf5f5a7abc5d0821c50deb4368528d8691f18737",
                "0xe0d64bfb1a30d66ae0f06ce36d5f4edf6835cd7c"
            ]
        }
    },
    "nonce": "0x0000000000000042",
    "difficulty": "0x99999",
    "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x0000000000000000000000000000000000000000",
    "timestamp": "0x00",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
    "gasLimit": "0x500000",
    "alloc": {}
}

11、删除每个节点下个geth目录,保留keystore目录,并用创始配置重新init datadir。

rm -rf node1/geth
rm -rf node2/geth
rm -rf node3/geth

将创始配置分别拷贝入数据目录下,以便在容器内能访问到。

cp mt_genesis.json node1 
cp mt_genesis.json node2
cp mt_genesis.json node3

像第6步一样,进入node1之后,执行init

geth init /root/.ethereum/mt_genesis.json 

重复在node2和node3上分别执行init。

12、重启节点网络

docker-compose down
docker-compose up -d

13、随便进入某个节点,查看验证人是否设置成功。

dpos.getValidators()

["0x8807fa0db2c60675a8f833dd010469e408428b83", "0xdf5f5a7abc5d0821c50deb4368528d8691f18737", "0xe0d64bfb1a30d66ae0f06ce36d5f4edf6835cd7c"]

14、查看节点是否互联

admin.peers

[]

为空,说明节点之间没有互相发现。

15、让节点互联

查看每个节点信息

admin.nodeInfo

确认:enode都不一样,protocols都一样。

记下三个enode

"enode://97caa3fe607663197d8741be1f26a1e53fc936cdef7187cdec737495fa01ae501d95a659bdf48e7d8505a8fc1dacfb715b17775ba86f1575a46d8cffa1f6b509@[::]:30303"
"enode://15bc1789ff8b5108a1e47cf257bae7928d5bfc1eb44c5d61c8dad86f7ac6b985c82dc32e3ffcdf06bd142f4a19183d89c51e4a5fdd1c5447e7172f2e5aadb428@[::]:30303"
"enode://0031048c457163671377bd197d05a78b1c11d474705b29b1b2473b37356d0a4fdf96563fcbda1d6cdf21a6e30f3082935276c4538a82b14da1d660b04ef99213@[::]:30303"

查看docker容器网络信息

docker network inspect mgeth_default

"Containers": {
            "0cd2b0fd97fd53650767302162038878d95b7f39dce48a3db7181d12d3ee673f": {
                "Name": "meitu_node2_1",
                "EndpointID": "2fc2b79fb840ba3a2deae61ef4cef8e944684e5259ff88f43283f3a206b3bb90",
                "MacAddress": "02:42:ac:15:00:03",
                "IPv4Address": "172.21.0.3/16",
                "IPv6Address": ""
            },
            "73c6bafac7173b737495df70d091ea7c1a966826edd79e08c20cd3f0f19ce479": {
                "Name": "meitu_node3_1",
                "EndpointID": "e16177540cb80ae1f731008e1975135119a12ddaf626f07a160d55e3c2d5b0ba",
                "MacAddress": "02:42:ac:15:00:04",
                "IPv4Address": "172.21.0.4/16",
                "IPv6Address": ""
            },
            "a67590ead292ed4d905eb868c6d8c6049b7796be8dd6464f20f96a019749560b": {
                "Name": "meitu_node1_1",
                "EndpointID": "a852916334db8149e43ffcc51176589bf4a379cff7efe48a44eb207e7883d448",
                "MacAddress": "02:42:ac:15:00:02",
                "IPv4Address": "172.21.0.2/16",
                "IPv6Address": ""
            }
        },

记录下每个节点的ip,也可以用127.0.0.1加节点映射到本机的不同网络端口。

在节点1容器内执行

admin.addPeer("enode://15bc1789ff8b5108a1e47cf257bae7928d5bfc1eb44c5d61c8dad86f7ac6b985c82dc32e3ffcdf06bd142f4a19183d89c51e4a5fdd1c5447e7172f2e5aadb428@[172.21.0.3]:30303")
admin.addPeer("enode://0031048c457163671377bd197d05a78b1c11d474705b29b1b2473b37356d0a4fdf96563fcbda1d6cdf21a6e30f3082935276c4538a82b14da1d660b04ef99213@[172.21.0.4]:30303")

再看节点网络

admin.peers

[{
    caps: ["eth/62", "eth/63"],
    id: "0031048c457163671377bd197d05a78b1c11d474705b29b1b2473b37356d0a4fdf96563fcbda1d6cdf21a6e30f3082935276c4538a82b14da1d660b04ef99213",
    name: "Geth/v1.7.4-stable-6be4cd4b/linux-amd64/go1.9.7",
    network: {
      localAddress: "172.21.0.2:48258",
      remoteAddress: "172.21.0.4:30303"
    },
    protocols: {
      eth: {
        difficulty: 131072,
        head: "0x19d4e0c4422cfe81c35cb9420127a6dc590c3e6b09ca835c840d53a601ed1d32",
        version: 63
      }
    }
}, {
    caps: ["eth/62", "eth/63"],
    id: "15bc1789ff8b5108a1e47cf257bae7928d5bfc1eb44c5d61c8dad86f7ac6b985c82dc32e3ffcdf06bd142f4a19183d89c51e4a5fdd1c5447e7172f2e5aadb428",
    name: "Geth/v1.7.4-stable-6be4cd4b/linux-amd64/go1.9.7",
    network: {
      localAddress: "172.21.0.2:49844",
      remoteAddress: "172.21.0.3:30303"
    },
    protocols: {
      eth: {
        difficulty: 131072,
        head: "0x19d4e0c4422cfe81c35cb9420127a6dc590c3e6b09ca835c840d53a601ed1d32",
        version: 63
      }
    }
}]

注意,第15步的互联方式是临时性的,每次重启docker之后admin.peers会重新为空。

为方便互联,可以将bootnodes配置到启动文件里。

version: '3.3'
services:
  mgeth_node_1:
    image: mgeth
    container_name: mgeth_node_1
    build: 
      context: ..
    command: --ipcpath "/root/.ethereum/geth.ipc" --port 30303
    ports:
      - "15450:8545"
      - "15460:8546"
      - "10303:30303"
      - "10303:30303/udp"
      - "10304:30304/udp"
    volumes:
      - /etc/localtime:/etc/localtime
      - ./node1/:/root/.ethereum/
    environment:
      - TZ=Asia/Shanghai
 
  mgeth_node_2:
    image: mgeth
    container_name: mgeth_node_2
    build: 
      context: ..
    command: --ipcpath "/root/.ethereum/geth.ipc" --port 30303 --bootnodes enode://97caa3fe607663197d8741be1f26a1e53fc936cdef7187cdec737495fa01ae501d95a659bdf48e7d8505a8fc1dacfb715b17775ba86f1575a46d8cffa1f6b509@[172.21.0.2]:30303
    depends_on:
      - mgeth_node_1
    ports:
      - "25450:8545"
      - "25460:8546"
      - "20303:30303"
      - "20303:30303/udp"
      - "20304:30304/udp"
    volumes:
      - /etc/localtime:/etc/localtime
      - ./node2/:/root/.ethereum/
    environment:
      - TZ=Asia/Shanghai
 
  mgeth_node_3:
    image: mgeth
    container_name: mgeth_node_3
    build: 
      context: ..
    command: --ipcpath "/root/.ethereum/geth.ipc" --port 30303 --bootnodes enode://97caa3fe607663197d8741be1f26a1e53fc936cdef7187cdec737495fa01ae501d95a659bdf48e7d8505a8fc1dacfb715b17775ba86f1575a46d8cffa1f6b509@[172.21.0.2]:30303
    depends_on:
      - mgeth_node_1
    ports:
      - "45450:8545"
      - "45460:8546"
      - "40303:30303"
      - "40303:30303/udp"
      - "40304:30304/udp"
    volumes:
      - /etc/localtime:/etc/localtime
      - ./node3/:/root/.ethereum/
    environment:
      - TZ=Asia/Shanghai

再验证下节点是否互联

amdin.peers

[{
    caps: ["eth/62", "eth/63"],
    id: "0031048c457163671377bd197d05a78b1c11d474705b29b1b2473b37356d0a4fdf96563fcbda1d6cdf21a6e30f3082935276c4538a82b14da1d660b04ef99213",
    name: "Geth/v1.7.4-stable-6be4cd4b/linux-amd64/go1.9.7",
    network: {
      localAddress: "172.21.0.2:48356",
      remoteAddress: "172.21.0.4:30303"
    },
    protocols: {
      eth: {
        difficulty: 131072,
        head: "0x19d4e0c4422cfe81c35cb9420127a6dc590c3e6b09ca835c840d53a601ed1d32",
        version: 63
      }
    }
}, {
    caps: ["eth/62", "eth/63"],
    id: "15bc1789ff8b5108a1e47cf257bae7928d5bfc1eb44c5d61c8dad86f7ac6b985c82dc32e3ffcdf06bd142f4a19183d89c51e4a5fdd1c5447e7172f2e5aadb428",
    name: "Geth/v1.7.4-stable-6be4cd4b/linux-amd64/go1.9.7",
    network: {
      localAddress: "172.21.0.2:30303",
      remoteAddress: "172.21.0.3:47174"
    },
    protocols: {
      eth: {
        difficulty: 131072,
        head: "0x19d4e0c4422cfe81c35cb9420127a6dc590c3e6b09ca835c840d53a601ed1d32",
        version: 63
      }
    }
}]

“主网”启动成功!

投票、出块验证

分别在3个节点上把validator无限期解锁,谁不解锁谁别出块、跳过你。这里源码默认10秒1块。

personal.unlockAccount(eth.validator,'xxxx', 0)

根据美图解释,这里validator和coinbase的区别:
coinbase收取挖矿奖励,validator可以设置为其他地址,但默认和coinbase一样。

3个节点分别启动挖矿

miner.start()

查看出块信息

eth.getBlock(15)

{
  coinbase: "0x8807fa0db2c60675a8f833dd010469e408428b83",
  difficulty: 1,
  extraData: "0xd783010704846765746887676f312e392e37856c696e757800000000000000000c50642f0a32b6ad0ac5c94ee6a8e4ef2043aa0a763a6eb817223c9d585454a604e836f37b281fdf7962631aaf2963763783556fee7c6bcebf3df2113f93815001",
  gasLimit: 5166620,
  gasUsed: 0,
  hash: "0x2d598f908d0535d58a82019ff0ec88e666065d3efcfc93293decbe1f26ea5d16",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000000",
  number: 15,
  parentHash: "0x2ba52152d2ca5e60aca873c850b1bef7812bf940f04a8756944dfd0f85f8fde3",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 794,
  stateRoot: "0x45e73d97158ba78c2afae887038b18d70d92e865dc3b0b175423061bf6bde29b",
  timestamp: 1530254800,
  totalDifficulty: 131087,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: [],
  validator: "0x8807fa0db2c60675a8f833dd010469e408428b83"
}

eth.getBlock(16)

{
  coinbase: "0xe0d64bfb1a30d66ae0f06ce36d5f4edf6835cd7c",
  difficulty: 1,
  extraData: "0xd783010704846765746887676f312e392e37856c696e757800000000000000000a6277cd4e5153f10abb221aa850ed64059b6fe93b1451417321cf17190a1f6b56707ca05550d4727f48874ce0899c3012990b361fe79e88d4ca51788eb6500e01",
  gasLimit: 5161576,
  gasUsed: 0,
  hash: "0x4de33e49fb536a710737fb1e09c4f713a0ca7e5511682451e006898e71af365d",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000000",
  number: 16,
  parentHash: "0x2d598f908d0535d58a82019ff0ec88e666065d3efcfc93293decbe1f26ea5d16",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 794,
  stateRoot: "0xe50a0e2181198d8543dcaf26be57f2c004e92ee446195d75fe447fcf6c4f0992",
  timestamp: 1530254810,
  totalDifficulty: 131088,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: [],
  validator: "0xe0d64bfb1a30d66ae0f06ce36d5f4edf6835cd7c"
}

eth.getBlock(17)

{
  coinbase: "0xdf5f5a7abc5d0821c50deb4368528d8691f18737",
  difficulty: 1,
  extraData: "0xd783010704846765746887676f312e392e37856c696e757800000000000000004bbf4fbec4706ecae18a4cbb0d92a9c9546e13503ca818f7b22860c78f5bac0748196864c697dabf6891c372e6911c1664ced36fdebfaa9ac2b608a9cb1beba701",
  gasLimit: 5156537,
  gasUsed: 0,
  hash: "0x743db2aa9732c26e80d21fa81fca75e8a952ea9f219b5720b5af7ebf7f832abd",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000000",
  number: 17,
  parentHash: "0x4de33e49fb536a710737fb1e09c4f713a0ca7e5511682451e006898e71af365d",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 794,
  stateRoot: "0x3dcb37771aceff66ff2af6f281e7306ead90824f0ecea18f60ffd87fd722731c",
  timestamp: 1530254820,
  totalDifficulty: 131089,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: [],
  validator: "0xdf5f5a7abc5d0821c50deb4368528d8691f18737"
}

可以看到3个节点轮流出块,10秒1块。

测试1:
假如2节点因某种原因不能出块了,那么下一轮当1节点出块后,3节点在时隔20秒后出块。

Tips:

之前在一台服务器上部署后发现暴露出来的15450端口一直无法访问,把端口加入防火墙规则甚至是关闭防火墙依然无用,检查了云平台上的安全组规则一切正常,最后在服务器用ifconfig检查了一下,发现docker创建的网关和eth0的网卡都是在172.22下面的。


修改docker-compose.yml把bridge更换到另一个网段,重启,成功! 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值