11. Chained-BFT共识公共组件

11.1. 概述

超级链共识框架 一文中介绍了超级链底层有一个共识的公共组件叫chained-bft,其是Hotstuff算法的实现。HotStuff是一种简洁而优雅的bft改进算法。它具有以下优点:

  • 它的设计中将liveness和safty解耦开来,使得非常方便与其他的共识进行扩展;

  • 将bft过程拆解成3阶段,每个阶段都是o(n)的通信;

  • 它允许一个节点处于不同的view,并且将view的切换与区块结合起来,使得其能够实现异步共识,进一步提升共识的效率。

这样一个chained-bft可以在给定主集合的场景下确保网络的共识安全性,并且通过与外层共识配合工作实现共识的活性保证。

11.2. 核心数据结构

 1enum QCState {
 2    NEW_VIEW = 0;
 3    PREPARE = 1;
 4    PRE_COMMIT = 2;
 5    COMMIT = 3;
 6    DECIDE = 4;
 7}
 8// QuorumCert is a data type that combines a collection of signatures from replicas.
 9message QuorumCert {
10    // The id of block this QC certified.
11    bytes BlockId = 1;
12    // The current type of this QC certified.
13    // the type contains `NEW_VIEW`, `PREPARE`,`PRE_COMMIT`, `COMMIT`, `DECIDE`.
14    State Type = 2;
15    // The view number of this QC certified.
16    int64 ViewNumber = 3;
17    // SignInfos is the signs of the leader gathered from replicas
18    // of a specifically certType.
19    QCSignInfos SignInfos  = 4;
20}
21// QCSignInfos is the signs of the leader gathered from replicas of a specifically certType.
22// A slice of signs is used at present.
23// TODO zq: It will be change to Threshold-Signatures
24// after Crypto lib support Threshold-Signatures.
25message QCSignInfos {
26    // QCSignInfos
27    map<string, SignInfo> QCSignInfos = 1;
28}
29// SignInfo is the signature information of the
30message SignInfo {
31string Address = 1;
32string PublicKey = 2;
33bytes  Sign = 3;
34}
35// ChainedBftMessage is the message of the protocal
36// In hotstuff, there are two kinds of messages, "NEW_VIEW_Message" and "QC_Message".
37// In XuperChain, there is only one kind of message, "NEW_VIEW. The "QC_Message" is resuded with "BroadcastBlock" message.
38message ChainedBftMessage {
39    // Message Type
40    QCState Type = 1;
41    // Justify is the QC of the leader gathered, send to next leader.
42    QuorumCert Justify = 2;
43}
44// ChainedBftMessage is the vote message of
45message ChainedBftVoteMessage {
46    // The id of block this message vote for.
47    bytes BlockId = 1;
48    // Replica will sign the QCMessage if the QuorumCert if valid.
49    SignInfo signature = 2;
50}

整个chained-bft中主要包括三部分,分别是状态机 SmrSafetyRulesPacemakerInterface

11.3. Smr

Smr是 chained-bft 的核心实例。他的主要的作用有以下几点:

  1. 维护节点链的chained-bft共识状态机;

  2. 在外层共识的驱动下发起 NewViewNewProposal 等消息并更新本地状态;

  3. 处理其他验证节点的消息并更新本地状态;

../_images/chained-bft.png

11.4. Safety Rule

Safety Rule 是一个验证节点是否要接受一个新的Proposal的安全性规则,主要有三条:

  1. 判断当前Proposal的View值是否大于本地locked Proposal的View值;

  2. 验证当前Proposal中上一个Proposal的投票信息有效性和投票个数是否大于系统矿工数目的2/3;

  3. 验证当前Proposal的ProposalMsg是否有效;

当一个验证节点收到一个新的提案时,如果满足上述 Safety Rule 的认证,则会给这个提案进行投票,否则拒绝这次提案。

11.5. PacemakerInterface

Hotstuff算法的一大特点就是将共识的liveness和safety分开。PacemakerInterface是Hotstuff算法Pacemaker的接口定义,外层共识通过实现这些接口,可以推进内层共识的状态轮转。不同的外层共识可以有不同的实现。目前超级链已经实现了DPoS+Hotstuff,具体的方案如下所示:

../_images/pacemaker.png