Istanbul BFT 實戰

前端時間寫了 Istanbul BFT 的一些原理,以及邏輯詳情 [1]。今天想寫一篇實戰的技術文章,所以底層原理就不贅述。下一篇我們將介紹 Istanbul BFT 的信息存儲。

1、構建節點身份參數,以及創始區塊

  • 下載並編譯 istanbul-tools 工具
    // 參考文檔 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 生成節點參數以及創始區塊

執行:

    ./build/bin/istanbul setup --num 4 --nodes --verbose   

生成:

    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
    git clone https://github.com/ConsenSys/quorum  
    cd /Path/to/your/quorum/  
    make geth  
  • 用之前生成好的 geneis.json(創始區塊配置文件)生成節點數據庫
    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(存儲鏈上數據)目錄:Istanbul BFT 解讀 (中)


3、啓動節點

    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 後的效果:Istanbul BFT 解讀 (中)


4、鏈接節點

節點的通過 enode 進行 p2p 鏈接,每個節點 至少 鏈接一個其他節點,節點取得串起來後,會通過 p2p 機制互相進行鏈接。

    // 在每個節點的命令行中輸入以下指令  
    admin.addPeer("anotherNode'sEnode")  
    // enode 在第 1 步時已經生成,也可以在節點的控制檯用命令行顯示  
    admin.nodeInfo.enode  

下圖展示了 node2 添加 node1 節點:Istanbul BFT 解讀 (中)

查看本節點的 enode 以及 鏈接的節點Istanbul BFT 解讀 (中)

當有 2f+1 個節點串起來後,便會正常出塊
Istanbul BFT 解讀 (中)


5、節點可擴展性

Istanbul BFT 可通過節點提議來進行添加或去除 validator,當有 超過一半 的節點投票後,再經過一個區塊 結果便會生效。

5.1 添加 validator

針對新的 validator 節點,參考之前的 1-3 步,分別爲:1、生成節點配置;2、生成節點數據;3、啓動節點。操作略。

    之前的 4 個節點中至少 3 個節點(既超過一半節點)執行以下:  
    istanbul.propose("newValidatorAddress", true)  

添加綁定地址爲 0x216b593dc69068f56ba3a37823b7fa622b843a05 的 validator 後,會成爲一個 candidate:Istanbul BFT 解讀 (中)當有 超過半數 節點 propose 此 candidate 後,會多出一個 validator:Istanbul BFT 解讀 (中)

5.2 去除 validator

    istanbul.propose("newValidatorAdd", false)  

具體查看方式,參考上一步。

參考資料

[1]

Istanbul BFT 的一些原理,以及邏輯詳情 : https://learnblockchain.cn/article/1040


本文作者:周俊傑

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


Istanbul BFT 解讀 (中)

來源鏈接:mp.weixin.qq.com