美文网首页
tendermint consensus 代码分析

tendermint consensus 代码分析

作者: bradyjoestar | 来源:发表于2019-02-20 11:43 被阅读0次

    Tendermint最核心的代码位于consensus包下,consensus下的文件是tendermint最大的理论创新之处.

    从这篇文章开始,我们分析tendermint的核心代码consensus模块.

    未命名文件.png

    最简单的分类方法如上图,主要由三个核心组件构成,分别是consensusReactor,consensusState以及TimeoutTicker.

    其中consensusReactor主要负责与其它的节点的reactor交互,核心职能有以下两部分:

    • 接收到其它节点发送的消息,将其传送到consensusState
    • 接收到自己的状态机发送的消息,决定如何将其发送到其它节点的consensusReactor.
      当然consensusReactor还提供了更多的功能,为了提高快速达成共识的速度.

    其中TimeoutTiker就是一个计时器的代码实现,主要提供三部分功能:

    • 收到重新计时请求,判断是否重新计时.若是,则停止上一个计时器,重新开始这次计时.
    • 计时时间到发送提醒
    • 退出

    consensusState是tendermint中最重要的一个文件,里面有状态切换的状态机存放在其中.
    consensusState 会调用各种资源来完成状态的接受与转变及外部的交互.
    在consensusState中有几个核心组件:

    • peerMsgQueue : 外部节点收到的消息,主要包括三种:proposal,blockPartMsg,voteMsg
    • internalMsgQueue:自己内部给自己发送的消息,同样包括上述三种.
    • RoundState:记录当前自己的参与共识状态的必要数据.
    • PeerRoundState:记录当前自己连接的peer的参与共识状态的必要数据.

    状态跃迁的核心的函数:

    • enterNewRound
    • enterPropose
    • enterPrevote
    • enterPrevoteWait
    • enterPrecommit
    • enterPrecommitWait
    • enterCommit

    会引起进入状态跃迁的核心函数的情况:

    • handleMsg
    • handleTimeout
    • 其它的状态跃迁函数,如enterPropose满足isProposalComplete的条件会立刻进入enterPrevote

    八个状态:

    • RoundStepNewHeight = RoundStepType(0x01) // Wait til CommitTime + timeoutCommit
    • RoundStepNewRound = RoundStepType(0x02) // Setup new round and go to RoundStepPropose
    • RoundStepPropose = RoundStepType(0x03) // Did propose, gossip proposal
    • RoundStepPrevote = RoundStepType(0x04) // Did prevote, gossip prevotes
    • RoundStepPrevoteWait = RoundStepType(0x05) // Did receive any +2/3 prevotes for current round, start timeout
    • RoundStepPrecommit = RoundStepType(0x06) // Did precommit, gossip precommits
    • RoundStepPrecommitWait = RoundStepType(0x07) // Did receive any +2/3 precommits for current round, start timeout
    • RoundStepCommit = RoundStepType(0x08) // Entered commit state machine

    对于代码执行细节的分析,由简如繁可以更容易地理解整个过程.以下面单个节点情景作为说明:

    只有单个节点参与共识的情况

    1.首先进入enterNewRound
    2.之后从enterNewRound进入enterPropose
    3.进入enterPropose后,判断自己是不是validator.只有一个节点自己就是,进入defaultDecideProposal
    4.进入defaultDecideProposal,把proposal和blockPartMsg发送到internalMsgQueue
    5.收到internalMsgQueue的消息,然后进入handleMsg,通过handleMsg进入addProposalBlockPart
    6.通过addProposalBlockPart 最后进入到enterPrevote
    7.通过enterPrevote进入到defaultDoPrevote,对proposal进行签名,并发送到internalMsgQueue
    8.handleMsg对收到的消息进行处理,进入到tryAddVote
    9.tryAddVote判断vote正确,并且满足超过三分之二的情况,进入enterPrevoteWait
    10.计时器超时,从enterPrevoteWait进入到enterPrecommit
    11.通过enterPrevote对proposal进行再次签名,并发送到internalMsgQueue
    12.handleMsg对收到的消息进行处理,进入到tryAddVote
    13.tryAddVote判断vote正确,进入enterCommit,这里涉及情况比较多(在多个节点的条件下).
    14.enterCommit落地区块,将区块发送给abci,收到返回后,此次共识结束.

    由于只有单个节点,从某种角度上ticker并不会经常触发超时,并且不需要考虑收到某一个future round超过三分之二的选票.

    相关文章

      网友评论

          本文标题:tendermint consensus 代码分析

          本文链接:https://www.haomeiwen.com/subject/swucyqtx.html