Giter Club home page Giter Club logo

consensus_bft's Introduction

consensus_bft

类似Tendermint的共识协议,取消commit阶段并使用leader归票降低网络通信复杂度

编译docker镜像

docker build -t citacloud/consensus_bft .   

使用方法

$ consensus -h       
consensus 6.6.0
Rivtower Technologies <[email protected]>
This doc string acts as a help message when the user runs '--help' as do all doc strings on fields

USAGE:
    consensus <SUBCOMMAND>

OPTIONS:
    -h, --help       Print help information
    -V, --version    Print version information

SUBCOMMANDS:
    help    Print this message or the help of the given subcommand(s)
    run     run this service

consensus-run

运行consensus服务。

$ consensus run -h
consensus-run 
run this service

USAGE:
    consensus run [OPTIONS]

OPTIONS:
    -c, --config <CONFIG_PATH>    Chain config path [default: config.toml]
    -h, --help                    Print help information
    -l, --log <LOG_FILE>          log config path [default: consensus-log4rs.yaml]

参数:

  1. 微服务配置文件。

    参见示例example/config.toml

    其中:

    • consensus_port 为该服务监听的端口号。
  2. 日志配置文件。

    参见示例consensus-log4rs.yaml

    其中:

    • level 为日志等级。可选项有:ErrorWarnInfoDebugTrace,默认为Info
    • appenders 为输出选项,类型为一个数组。可选项有:标准输出(stdout)和滚动的日志文件(journey-service),默认为同时输出到两个地方。
$ consensus run -c example/config.toml -l consensus-log4rs.yaml
2022-03-10T10:04:09.850581+08:00 INFO consensus - start consensus bft
2022-03-10T10:04:09.851129+08:00 INFO consensus - grpc port of this service: 50001

状态转换

Leader

start

  1. Leader send new proposal success
  2. Leader send new proposal fail
  3. Leader retry send new proposal success
  4. When leader receive the Prevote vote from follower,if it has send the proposal, and collect above 2/3 Prevote votes then change to PrecommitWait
  5. ProposeWait timeout and leader has collect above 2/3 Prevote votes, then
  • send Prevote vote with None hash
  • start new round
  1. When leader receive the Precommit vote, and it has collect above 2/3 Precommit votes. If current leader isn't the (next height, init round) leader then change to CommitWait temporary and soon change to Commit else change to Commit
  2. Leader send message timeout and hasn't collect above 2/3 votes, change to NewView
  3. In the case below, change to Proposal
  • CommitWait timeout and deal old height success
  • Commit timeout and check and commit block failed, then start new round
  1. Leader has collect 2f NewView votes, start new round
  2. When leader receive Prevote, in the case below change to Proposal
  • has collect 2f Prevote and self.round behind round, start new round
  • hash in the voteset which is above 2/3 n is zero, start new round
  • hasn't receive the Proposal yet which the hash is above 2/3 n, start new round
  1. When leader receive Precommit, in the case below change to Proposal
  • hash in the voteset which is above 2/3 n is zero, start new round
  • self.proposal not equal the hash in the voteset which is above 2/3 n, start new round
  • else if check and commit block failed, then start new round

Follower

follower

  1. Follower hasn't receive the Proposal
  2. Follower has receive the Proposal and add to proposals, hash_proposals , set proposal and send Prevote
  3. When receive the LeaderPrevote , check it, save to wal and send Precommit
  4. In the case below, change to CommitWait/Commit
  • When receive the LeaderPrevote and change to PrecommitWait success, and if has receive LeaderPrecommit with above 2/3 Precommit from leader, then enter the follow case
  • When follower receive the LeaderPrecommit with above 2/3 Precommit votes and save to wal, If current leader isn't the (next height, init round) leader then change to CommitWait temporary and soon change to Commit else change to Commit
  1. Follower receive Precommit whose hash is zero or check and commit block failed, start new round, change to Proposal
  2. Follower receive Prevote whose hash is zero, or hasn't receive the proposal , then start new round, change to Proposal
  3. In the case below, change to Proposal
  • CommitWait timeout and deal old height success
  • Commit timeout and check and commit block failed, then start new round
  1. Follower send message timeout and send NewView message, then change to NewView
  2. Follower has collect 2f NewView votes, start new round

启动及运行流程

start

  • 启动

    • 加载配置
    • 创建controller<->bft, network<->bft, timeout->bft的通道channel,并将通道channel句柄放入bft_channels
    • 初始化bft并开始select loop
    • 启动server,添加uri匹配规则,监听指定端口,并将匹配的uri转发至相应的通道channel
  • 运行

    • controller->bftreconfigurecheck_block请求,做出相应处理,并返回处理结果
      • reconfigure
        • 更新auth_manageis_consensus_node
        • 如果当前节点是(next height, init round)leader,则请求controller GetProposal,根据结果值设置proposal self_proposal proposals及相关值,并发送new proposal
      • check_block
        • 检查收到的LeaderVote中的vote是否正确
    • network->bft的消息会根据其VoteMsgtype进行相应处理,并根据发送消息至其他节点
      • Proposal
        • 只有follower会收到该消息,在校验(height, round, step)通过,请求controller CheckProposal,将其加入votes
        • 上一步检查通过后,设置proposal self_proposal proposals,发送自己的Prevote消息,变更状态,设置超时时间
      • Prevote
        • leader收到消息校验(height, round, step)及消息的发送节点的正确性后,将其加入votes
        • 上一步检查通过后,检查Prevote数量进行相应处理,详见状态转换
      • Precommit
        • leader收到消息校验(height, round, step)及消息的发送节点的正确性后,将其加入votes
        • 上一步检查通过后,检查Precommit数量进行相应处理,详见状态转换
      • LeaderPrevote
        • follower收到消息校验(height, round, step),消息的发送节点的正确性,及其票数是否超过2/3后,将其加入votes,并保存至wal
        • 上一步检查通过后,做进一步处理,详见状态转换
      • LeaderPrecommit
        • follower收到消息校验(height, round, step),消息的发送节点的正确性,及其票数是否超过2/3后,将其加入votes,并保存至wal
        • 上一步检查通过后,做进一步处理,详见状态转换
      • NewView
        • 收到消息校验(height, round, step),消息的发送节点的正确性后,将其加入votes
        • 上一步检查通过后,若当前节点收到2fNewView,发送NewView消息,开始新的round
    • timeout->bft根据超时的消息类型及是否是对应height roundleader做相应处理

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.