以太坊聯盟鏈 parity 節點搭建


上一篇文章介紹了以太坊私有鏈 geth節點的搭建,本篇介紹下企業級應用 聯盟鏈搭建,運用parity 錢包作為服務節點,實現多節點構建聯盟鏈。

一.parity的實現共識POA

    了解區塊鏈的同學,應該都聽過pow,pos這些共識算法,那parity的poa是一種什么樣的工作原理呢?poa全稱 Proof of Authority ,工作權益證明。

    poa的特點:

  • 有別於PoW (Proof-of-Work)需要解數學難題來產生block,PoA是依靠預設好的Authority nodes,負責產生block。
  • 可依照需求設定Authority node數量。
  • 可指定產生block的時間,例如收到交易的5秒后產生block。
  • 一般的Ethereum node也可以連接到PoA Chain,正常發起transactions, contracts等
  • 可適用於企業內部,構建多企業的聯盟鏈

二.parity 下載安裝

    parity github: https://github.com/paritytech/parity

    parity 下載地址:https://github.com/paritytech/parity/releases

    選擇穩定版  Parity 1.10.4-stable 

   

根據自身linux 系統版本,下載對應的二進制文件

  我這邊用的ubuntu 16.04  x64 

 

  1.wget  https://d1h4xl4cr1h0mo.cloudfront.net/v1.10.4/x86_64-unknown-linux-gnu/parity #下載

  2.chmod 775 parity  #修改可執行文件

  3. ./parity -v  #檢查parity 版本

   若顯示v1.10.4 版本信息,則已成功安裝

 

三.配置創世區塊 genesis-spec.json

   >$ mkdir parity

   >$ cd parity

   >$vim genesis-spec.json

{
    "name": "DemoPoA",
    "engine": {
        "authorityRound": {
            "params": {
                "stepDuration": "2",
                "validators" : {
                    "list": []
                }
            }
        }
    },
"params": {
    "gasLimitBoundDivisor": "0x400",
        "maximumExtraDataSize": "0x20",
        "minGasLimit": "0x1388",
        "networkID" : "0x2323"
    },
    "genesis": {
        "seal": {
            "authorityRound": {
                "step": "0x0",
                "signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
            }
        },
        "difficulty": "0x10000",
        "gasLimit": "0x12a05f200"
    },
    "accounts": {
        "0x0000000000000000000000000000000000000001": { "balance": "1", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } },
        "0x0000000000000000000000000000000000000002": { "balance": "1", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } },
        "0x0000000000000000000000000000000000000003": { "balance": "1", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } },
        "0x0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } },
        "0x6a67f9275dfe3abac6fd5525dec804551cbb7f00": { "balance": "1000000000000000000000000" },
        "0xd62de1cbcf85253b776305ab45fd85367187bc29": { "balance": "1000000000000000000000000" }
    }
}

 name:創世文件名稱

 engine:節點引擎,這里先用寫死驗證人方式,在list 中添加驗證人地址

params: networkID 聯盟鏈節點ID

accounts:初始化賬戶

友情提示:更詳細配置可參考parity wiki : https://wiki.parity.io/Chain-specification

 

四.添加節點配置文件

>$ vim node1.toml

[parity]
chain = "genesis-spec.json" #創世區塊文件
base_path = "parity0" #創建parity0文件夾,代表第一個節點
[network]
port = 30300 #網絡端口
[rpc]
port = 8540 #rpc請求端口
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"] #支持的rpc請求方式
#interface = "192.168.1.175" #默認的rpc 請求地址
[ui]
port = 8180 #parity ui網頁端口
hosts = ["all"]
[account]
password = ["node.pwds"] #節點parity0密碼
[websockets]
port = 8456
[ipc]
disable = true  #不支持ipc 訪問
[mining]
engine_signer = "0x005C6F320C425675bAE78BcF693AE2e6d63e9853"  #礦工地址
reseal_on_txs = "none"

 

 

五.創建用戶名密碼存儲文件

>$ touch node.pwds (創建節點礦工密碼)

>$ vim node.pwds

    123

 

六.創建節點關聯文件

 >$  touch reservedPeers.enode (暫時創建,創建第一個節點時,在補充)

 

七.創建初始化用戶

 >$ ./parity account new --config node0.toml

 

八.修改創世文件 genesis-spec.json 添加驗證人

{
    "name": "DemoPoA",
    "engine": {
        "authorityRound": {
            "params": {
                "stepDuration": "2",
                "validators" : {
                    "list": ["0x6a67f9275dfe3abac6fd5525dec804551cbb7f00"]
 } } } },     "params": { "gasLimitBoundDivisor": "0x400", "maximumExtraDataSize": "0x20", "minGasLimit": "0x1388", "networkID" : "0x2323" }, "genesis": { "seal": { "authorityRound": { "step": "0x0", "signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" } }, "difficulty": "0x10000", "gasLimit": "0x12a05f200" }, "accounts": { "0x0000000000000000000000000000000000000001": { "balance": "1", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } }, "0x0000000000000000000000000000000000000002": { "balance": "1", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } }, "0x0000000000000000000000000000000000000003": { "balance": "1", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } }, "0x0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } }, "0x6a67f9275dfe3abac6fd5525dec804551cbb7f00": { "balance": "1000000000000000000000000" }, "0x6a67f9275dfe3abac6fd5525dec804551cbb7f00": { "balance": "1000000000000000000000000" } 
}

}

 

 

九.修改配置節點

十.啟動parity

>$ ./parity --config node1.toml

目前一個parity節點已經搭建完畢。

 

十一.采用合約管理節點驗證人(可通過合約動態管理)

以下是完整 genesis-spec.json 文件配置

{
  "name": "DemoPoA",
  "engine": {
    "authorityRound": {
      "params": {
        "stepDuration": "5",
        "validators": {
          "safeContract":"0x0000000000000000000000000000000000000005"
        }
      }
    }
  },
  "params": {
    "gasLimitBoundDivisor": "0x0400",
    "maximumExtraDataSize": "0x20",
    "minGasLimit": "0x1388",
    "networkID": "0x2323"
  },
  "genesis": {
    "seal": {
      "authorityRound": {
        "step": "0x0",
        "signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
      }
    },
    "difficulty": "0x20000",
    "gasLimit": "0x5B8D80"
  },
  "accounts": {
    "0x0000000000000000000000000000000000000001": {
      "balance": "1",
      "builtin": {
        "name": "ecrecover",
        "pricing": {
          "linear": {
            "base": 3000,
            "word": 0
          }
        }
      }
    },
    "0x0000000000000000000000000000000000000002": {
      "balance": "1",
      "builtin": {
        "name": "sha256",
        "pricing": {
          "linear": {
            "base": 60,
            "word": 12
          }
        }
      }
    },
    "0x0000000000000000000000000000000000000003": {
      "balance": "1",
      "builtin": {
        "name": "ripemd160",
        "pricing": {
          "linear": {
            "base": 600,
            "word": 120
          }
        }
      }
    },
    "0x0000000000000000000000000000000000000004": {
      "balance": "1",
      "builtin": {
        "name": "identity",
        "pricing": {
          "linear": {
            "base": 15,
            "word": 3
          }
        }
      }
    },
    "0x00A7135cf451B2463517D11e0cE88013042e0a28": {
      "balance": "80000000000000000000000"
    },
    "0x0000000000000000000000000000000000000005":{
      "balance": "1",
      "constructor":"0x606060405260008054600160a060020a0319169055341561001f57600080fd5b6020604051908101604052725c6f320c425675bae78bcf693ae2e6d63e9853815261004e906002906001610131565b5061006464010000000061017a61006982021704565b6101ff565b600554600090819060ff161561007e57600080fd5b600091505b60025482101561010357600280548390811061009b57fe5b600091825260209091200154600160a060020a03169050604080519081016040908152600182526020808301859052600160a060020a03841660009081526004909152208151815460ff19169015151781556020820151600191820155929092019150610083565b6005805460ff1916600190811790915560028054610122929190610198565b50506003805460ff1916905550565b828054828255906000526020600020908101928215610188579160200282015b828111156101885782518254600160a060020a031916600160a060020a039190911617825560209290920191600190910190610151565b506101949291506101d8565b5090565b8280548282559060005260206000209081019282156101885760005260206000209182015b828111156101885782548255916001019190600101906101bd565b6101fc91905b80821115610194578054600160a060020a03191681556001016101de565b90565b61073c8061020e6000396000f3006060604052600436106100775763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166305c81865811461007c578063170f92911461009157806340a141ff146100c35780634d238c8e146100e25780637528621114610101578063b7ab4db514610114575b600080fd5b341561008757600080fd5b61008f61017a565b005b341561009c57600080fd5b6100a7600435610242565b604051600160a060020a03909116815260200160405180910390f35b34156100ce57600080fd5b61008f600160a060020a036004351661026a565b34156100ed57600080fd5b61008f600160a060020a03600435166103b7565b341561010c57600080fd5b61008f610464565b341561011f57600080fd5b61012761052f565b60405160208082528190810183818151815260200191508051906020019060200280838360005b8381101561016657808201518382015260200161014e565b505050509050019250505060405180910390f35b600554600090819060ff161561018f57600080fd5b600091505b6002548210156102145760028054839081106101ac57fe5b600091825260209091200154600160a060020a03169050604080519081016040908152600182526020808301859052600160a060020a03841660009081526004909152208151815460ff19169015151781556020820151600191820155929092019150610194565b6005805460ff191660019081179091556002805461023392919061063a565b50506003805460ff1916905550565b600180548290811061025057fe5b600091825260209091200154600160a060020a0316905081565b600160a060020a03811660009081526004602052604081205481908190849060ff16156103b057600160a060020a038516600090815260046020526040902060010154600280549195506000198201945090849081106102c657fe5b60009182526020909120015460028054600160a060020a0390921693508391869081106102ef57fe5b6000918252602080832091909101805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a039485161790559184168152600490915260409020600101849055600280548490811061034657fe5b6000918252602090912001805473ffffffffffffffffffffffffffffffffffffffff19169055600280549061037f90600019830161068a565b50600160a060020a03851660009081526004602052604081206001810191909155805460ff191690556103b0610598565b5050505050565b600160a060020a038116600090815260046020526040902054819060ff1615156104605760028054600160a060020a03841660009081526004602052604090206001908101829055810161040b838261068a565b506000918252602080832091909101805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a03861690811790915582526004905260409020805460ff19166001179055610460610598565b5050565b60005433600160a060020a039081169116141580610484575060035460ff165b1561048e57600080fd5b6002805461049e9160019161063a565b506003805460ff191660019081179091557f8564cd629b15f47dc310d45bcbfc9bcf5420b0d51bf0659a16c67f91d27632539060405160208082528254908201819052819060408201908490801561051f57602002820191906000526020600020905b8154600160a060020a03168152600190910190602001808311610501575b50509250505060405180910390a1565b6105376106b3565b600180548060200260200160405190810160405280929190818152602001828054801561058d57602002820191906000526020600020905b8154600160a060020a0316815260019091019060200180831161056f575b505050505090505b90565b60035460ff1615156105a957600080fd5b6003805460ff191690556000194301407f55252fa6eee4741b4e24a74a70e9c11fd2c2281df8d6ea13126ff845f7825c89600260405160208082528254908201819052819060408201908490801561062a57602002820191906000526020600020905b8154600160a060020a0316815260019091019060200180831161060c575b50509250505060405180910390a2565b82805482825590600052602060002090810192821561067a5760005260206000209182015b8281111561067a57825482559160010191906001019061065f565b506106869291506106c5565b5090565b8154818355818115116106ae576000838152602090206106ae9181019083016106f6565b505050565b60206040519081016040526000815290565b61059591905b8082111561068657805473ffffffffffffffffffffffffffffffffffffffff191681556001016106cb565b61059591905b8082111561068657600081556001016106fc5600a165627a7a72305820e802334540c1fab99433710f3e01f32156cca8fb5321c2d69963ee71a09b944b0029"
    }
  }
}
View Code

 下面是動態管理驗證人的合約

pragma solidity ^0.4.15;
contract ValidatorSet {
    event InitiateChange(bytes32 indexed _parent_hash, address[] _new_set);
    function getValidators() public constant returns (address[] _validators);
    function finalizeChange() public;
}
contract MajorityList is ValidatorSet {
    event ChangeFinalized(address[] current_set);
    struct ValidatorStatus {
        bool isValidator;
        uint index;
    }
    address SYSTEM_ADDRESS = 0x0000000000000000000000000000000000000000;
    address[] public validatorsList;
    address[] pendingList;
    bool finalized;
    mapping(address => ValidatorStatus) validatorsStatus;
    bool private initialized;
    function MajorityList() public {
        pendingList = [0x005C6F320C425675bAE78BcF693AE2e6d63e9853];
        initializeValidators();
    }
    modifier uninitialized() {
        if (initialized) { revert();}
        _;
    }
    modifier when_finalized() {
        if (!finalized) { revert();}
        _;
    }
    modifier only_system_and_not_finalized() {
        if (msg.sender != SYSTEM_ADDRESS || finalized) { revert(); }
        _;
    }
    modifier is_validator(address someone) {
        if (validatorsStatus[someone].isValidator) { _; }
    }
    modifier is_not_validator(address someone) {
        if (!validatorsStatus[someone].isValidator) { _; }
    }
    function initializeValidators() public uninitialized {
        for (uint j = 0; j < pendingList.length; j++) {
            address validator = pendingList[j];
            validatorsStatus[validator] = ValidatorStatus({
                isValidator: true,
                index: j
            });
        }
        initialized = true;
        validatorsList = pendingList;
        finalized = false;
    }
    function initiateChange() private when_finalized {
        finalized = false;
        emit InitiateChange(block.blockhash(block.number - 1), pendingList);
    }
    function finalizeChange() public only_system_and_not_finalized {
        validatorsList = pendingList;
        finalized = true;
        emit ChangeFinalized(validatorsList);
    }
    function addValidator(address validator) public is_not_validator(validator){
        validatorsStatus[validator].index = pendingList.length;
        pendingList.push(validator);
        validatorsStatus[validator].isValidator = true;
        initiateChange();
    }
    function removeValidator(address validator) public is_validator(validator){
        uint removedIndex = validatorsStatus[validator].index;
        uint lastIndex = pendingList.length-1;
        address lastValidator = pendingList[lastIndex];

        pendingList[removedIndex] = lastValidator;
        validatorsStatus[lastValidator].index = removedIndex;

        delete pendingList[lastIndex];
        pendingList.length--;

        validatorsStatus[validator].index = 0;
        validatorsStatus[validator].isValidator = false;
        initiateChange();

    }
    function getValidators() public constant returns (address[]) {
        return validatorsList;
    }
}
View Code

 

這部分代碼有過更改,為了適應parity 部署。

 

獲取合約 二進制文件 bytecode 的幾種方式

1.如果parity 已經能啟動UI界面, 部署合約(有可能parity錢包不能編譯)

2.使用上一篇文章 geth 搭建的mist錢包編譯 可獲得二進制文件 : https://www.cnblogs.com/sumingk/articles/9030469.html

我這里是使用Mist 錢包編譯的合約,parity 節點部署的合約有時一直加載不出來

 

十二.編輯 reservedPeers.enode 文件,添加節點信息

 要是添加多個parity 需要在reservedPeers.enode 添加對方enode

 

 啟動節點  ./parity  --config node1.toml 

 

----------------------------------------請關注微信公眾號“刻意鏈習”,持續帶你理解更多的區塊鏈技術------------------------------------------------------

                            

 

 

參考parity wiki: https://wiki.parity.io

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM