Istanbul BFT解读(中)

2020-09-24 10:54:06 浏览数 (1)

Istanbul BFT实战

前端时间写了Istanbul BFT的一些原理,以及逻辑详情[1]。今天想写一篇实战的技术文章,所以底层原理就不赘述。下一篇我们将介绍Istanbul BFT的信息存储。

1、构建节点身份参数,以及创始区块

  • 下载并编译istanbul-tools工具
代码语言:javascript复制
// 参考文档https://github.com/getamis/istanbul-tools
git clone https://github.com/getamis/istanbul-tools.git
cd /Path/to/your/istanbul-tools/
make istanbul
  • 利用istanbul-tools生成节点参数以及创始区块

执行:

代码语言:javascript复制
./build/bin/istanbul setup --num 4 --nodes --verbose 

生成:

代码语言:javascript复制
validators
{
        "Address": "0x4b143cd4ddd264fe472b0b308fa968523f2b5c8b",
        "Nodekey": "9e9adc3b2d801fbb3410e4c50067c513cad247c7c8ef82c491aebbe8693576be",
        "NodeInfo": "enode://af0b65802b745c10fcc476f42e41084b62f1208f56a6e2a32f3104a0ff8751d394a784caec321edd9b9cf5e4bb70da2a73d74f00284fea42900361ec9f87ccff@0.0.0.0:30303?discport=0"
}
{
        "Address": "0x11bc449be1cad5b68f0b7a9a207142b2306a3ff8",
        "Nodekey": "46e79f8da7d4e7462df73b812e6f5db10f7b7525bed38b4347dad9edadc3b503",
        "NodeInfo": "enode://ca75f485e1f3f98d6f6e0c75535134376312a2454946ad283f3aba6000faba41adf20a50f9d286f237fcb5495863b5e07470fc9acff4ff3e40a1766ce87e27bf@0.0.0.0:30303?discport=0"
}
{
        "Address": "0xb9e292fffa811ec765e9b716d86f9f985870c688",
        "Nodekey": "216b99fd388a26d90de92ca6c45df8c07138f13413f4946986d49c4cf50593d1",
        "NodeInfo": "enode://bc3ab0eb5efe336151c57e361bd0c59fe23b391bdbec1dd55c5952156f7bdc089cb7293cc2ac11f52b8f9727dc1d27001f159ed6c8d4391850670ac5c537adb5@0.0.0.0:30303?discport=0"
}
{
        "Address": "0x35187775b6ef2597b32b93548546560de65ef4f5",
        "Nodekey": "24e86c4df24c1842b4a0a97ce234e6d87f632c18c2a7db0586d77b0bba5a9ae0",
        "NodeInfo": "enode://518e07daa6b546a417e105c8a66905bf78b0d7c7784e120e5e9a30a9dbdb2de3c9fcb55685cee2405e434553ef0b0e43cb462c494388cf1b2d49532ccadbba12@0.0.0.0:30303?discport=0"
}



static-nodes.json
[
        "enode://af0b65802b745c10fcc476f42e41084b62f1208f56a6e2a32f3104a0ff8751d394a784caec321edd9b9cf5e4bb70da2a73d74f00284fea42900361ec9f87ccff@0.0.0.0:30303?discport=0",
        "enode://ca75f485e1f3f98d6f6e0c75535134376312a2454946ad283f3aba6000faba41adf20a50f9d286f237fcb5495863b5e07470fc9acff4ff3e40a1766ce87e27bf@0.0.0.0:30303?discport=0",
        "enode://bc3ab0eb5efe336151c57e361bd0c59fe23b391bdbec1dd55c5952156f7bdc089cb7293cc2ac11f52b8f9727dc1d27001f159ed6c8d4391850670ac5c537adb5@0.0.0.0:30303?discport=0",
        "enode://518e07daa6b546a417e105c8a66905bf78b0d7c7784e120e5e9a30a9dbdb2de3c9fcb55685cee2405e434553ef0b0e43cb462c494388cf1b2d49532ccadbba12@0.0.0.0:30303?discport=0"
]



genesis.json
{
    "config": {
        "chainId": 2017,
        "homesteadBlock": 1,
        "eip150Block": 2,
        "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
        "eip155Block": 3,
        "eip158Block": 3,
        "istanbul": {
            "epoch": 30000,
            "policy": 0
        }
    },
    "nonce": "0x0",
    "timestamp": "0x5f65a55d",
    "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000f89af854944b143cd4ddd264fe472b0b308fa968523f2b5c8b9411bc449be1cad5b68f0b7a9a207142b2306a3ff894b9e292fffa811ec765e9b716d86f9f985870c6889435187775b6ef2597b32b93548546560de65ef4f5b8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0",
    "gasLimit": "0x47b760",
    "difficulty": "0x1",
    "mixHash": "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
    "coinbase": "0x0000000000000000000000000000000000000000",
    "alloc": {
        "11bc449be1cad5b68f0b7a9a207142b2306a3ff8": {
            "balance": "0x446c3b15f9926687d2c40534fdb564000000000000"
        },
        "35187775b6ef2597b32b93548546560de65ef4f5": {
            "balance": "0x446c3b15f9926687d2c40534fdb564000000000000"
        },
        "4b143cd4ddd264fe472b0b308fa968523f2b5c8b": {
            "balance": "0x446c3b15f9926687d2c40534fdb564000000000000"
        },
        "b9e292fffa811ec765e9b716d86f9f985870c688": {
            "balance": "0x446c3b15f9926687d2c40534fdb564000000000000"
        }
    },
    "number": "0x0",
    "gasUsed": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

2、生成节点数据

  • 下载并编译quorum节点可执行程序geth
代码语言:javascript复制
git clone https://github.com/ConsenSys/quorum
cd /Path/to/your/quorum/
make geth
  • 用之前生成好的geneis.json(创始区块配置文件)生成节点数据库
代码语言:javascript复制
build/bin/geth  --datadir "node1" init "genesis.json"
build/bin/geth  --datadir "node2" init "genesis.json"
build/bin/geth  --datadir "node3" init "genesis.json"
build/bin/geth  --datadir "node4" init "genesis.json"

下图展示了node1生成后的效果,node1文件夹中包含了keystoregeth(存储链上数据)目录:


3、启动节点

代码语言:javascript复制
build/bin/geth --identity "validator1" --datadir "node1" --nodekeyhex "9e9adc3b2d801fbb3410e4c50067c513cad247c7c8ef82c491aebbe8693576be" --mine --debug --metrics --syncmode="full" --istanbul.blockperiod 5 --gasprice 0 --port 30303 --nat any console

build/bin/geth --identity "validator2" --datadir "node2" --nodekeyhex "46e79f8da7d4e7462df73b812e6f5db10f7b7525bed38b4347dad9edadc3b503" --mine --debug --metrics --syncmode="full" --istanbul.blockperiod 5 --gasprice 0 --port 30304 --nat any console

build/bin/geth --identity "validator3" --datadir "node3" --nodekeyhex "216b99fd388a26d90de92ca6c45df8c07138f13413f4946986d49c4cf50593d1" --mine --debug --metrics --syncmode="full" --istanbul.blockperiod 5 --gasprice 0 --port 30305 --nat any console

build/bin/geth --identity "validator4" --datadir "node4" --nodekeyhex "24e86c4df24c1842b4a0a97ce234e6d87f632c18c2a7db0586d77b0bba5a9ae0" --mine --debug --metrics --syncmode="full" --istanbul.blockperiod 5 --gasprice 0 --port 30306 --nat any console

下图展示了启动node1后的效果:


4、链接节点

节点的通过enode进行p2p链接,每个节点至少链接一个其他节点,节点取得串起来后,会通过p2p机制互相进行链接。

代码语言:javascript复制
// 在每个节点的命令行中输入以下指令
admin.addPeer("anotherNode'sEnode")
// enode在第1步时已经生成,也可以在节点的控制台用命令行显示
admin.nodeInfo.enode

下图展示了node2添加node1节点:

查看本节点的enode以及链接的节点

当有2f 1个节点串起来后,便会正常出块


5、节点可扩展性

Istanbul BFT可通过节点提议来进行添加或去除validator,当有超过一半的节点投票后,再经过一个区块结果便会生效。

5.1 添加validator

针对新的validator节点,参考之前的1-3步,分别为:1、生成节点配置;2、生成节点数据;3、启动节点。操作略。

代码语言:javascript复制
之前的4个节点中至少3个节点(既超过一半节点)执行以下:
istanbul.propose("newValidatorAddress", true)

添加绑定地址为0x216b593dc69068f56ba3a37823b7fa622b843a05的validator后,会成为一个candidate:

当有超过半数节点propose此candidate后,会多出一个validator:

5.2 去除validator

代码语言:javascript复制
istanbul.propose("newValidatorAdd", false)

具体查看方式,参考上一步。

参考资料

[1]

Istanbul BFT的一些原理,以及逻辑详情: https://learnblockchain.cn/article/1040


本文作者:周俊杰

作者主页:https://learnblockchain.cn/people/740

0 人点赞