官方文档:https://docs.hecochain.com/#/consensus?id=%e7%b3%bb%e7%bb%9f%e5%90%88%e7%ba%a6
合约源代码:https://github.com/HuobiGroup/huobi-eco-contracts

系统合约

目前验证人的管理,均由系统合约完成,目前的系统合约有:

  • Proposal 负责管理验证人的准入资格,管理验证人提案和投票;
  • Validators 负责对验证人进行排名管理、质押和解质押操作、分发区块奖励等;
  • Punish 负责对不正常工作的活跃验证人进行惩罚操作;

区块链调用系统合约

  • 每个块结束的时候,会调用Validators合约,将区块中所有交易的手续费分发给active validator;
  • 发现validator没有正常工作的时候,会调用Punish合约,对validator进行惩罚;
  • 每个epoch结束的时候,会调用Validators合约,根据排名,更新active validator;

质押

任何账户,都可以对validator进行任意数量的质押操作,每个validator的最小质押量是32HT。 如果想取回已质押的HT,需要按照如下操作进行:

  1. 发送调用Validators合约,发送针对某一个validator的解质押(unstake)的声明交易;
  2. 等待86400个块之后,调用Validators合约,发送提取质押(withdrawStaking)的交易,将所有在此validator的质押取回;

惩罚措施

每当发现验证人没有按照预先设定进行出块的时候,就会在这个块结束时,自动调用Punish合约,对验证人进行计数。当计数达到24时,罚没验证人的所有收入。当计数达到48时,将验证人移除出活跃验证人列表,同时取消验证人资格。

合约初始化

系统合约会在第一区块时被全部初始化
(github code)
(github code2)

// Initialize all system contracts at block 1.
if header.Number.Cmp(common.Big1) == 0 {
    if err := c.initializeSystemContracts(chain, header, state); err != nil { // 在此初始化三个系统合约
        log.Error("Initialize system contracts failed", "err", err)
        return err
    }
}

取消验证验证者资格

当前验证者至少需要保留一个
https://github.com/HuobiGroup/huobi-eco-contracts/blob/d01906925c8ea017334df2e725134de0c22a17a2/contracts/Validators.sol#L757

function tryRemoveValidatorInHighestSet(address val) private {
        for (
            uint256 i = 0;
            // ensure at least one validator exist
            i < highestValidatorsSet.length && highestValidatorsSet.length > 1; // 当前验证者至少保留一个
            i++
        ) 
  1. 当前所在投票数落后于 最大验证者数(排名大于MaxValidators 21)
    https://github.com/HuobiGroup/huobi-eco-contracts/blob/d01906925c8ea017334df2e725134de0c22a17a2/contracts/Validators.sol#L611
  2. 到达惩罚条件
    https://github.com/HuobiGroup/huobi-eco-contracts/blob/d01906925c8ea017334df2e725134de0c22a17a2/contracts/Punish.sol#L68
  3. 投票数小于MinimalStakingCoin
    https://github.com/HuobiGroup/huobi-eco-contracts/blob/d01906925c8ea017334df2e725134de0c22a17a2/contracts/Validators.sol#L336

编译合约

修改solc的依赖版本

 // Configure your compilers
  compilers: {
    solc: {
      version: "^0.6.1", // 修改为该版本以上,不然会报找不到对应的版本,此时会下载0.6.12版本

编译合约

truffle compile

此时会在./build/contracts下生成各个合约对应的abi json文件

参考

https://ethereum.stackexchange.com/questions/234/what-is-an-abi-and-why-is-it-needed-to-interact-with-contracts
https://ethereum.stackexchange.com/questions/1437/do-i-need-a-compiled-contract-just-to-get-the-abi-definition
https://zhuanlan.zhihu.com/p/26089385