Giter VIP home page Giter VIP logo

bft-rs's Introduction

BFT

An efficient and stable Rust library of BFT protocol for distributed system.

What is BFT?

BFT(Byzantine Fault Tolerance) comprise a class of consensus algorithms that achieve byzantine fault tolerance. BFT can guarantee liveness and safety for a distributed system where there are not more than 33% malicious byzantine nodes, and thus BFT is often used in the blockchain network.

BFT Protocol

Protocol

BFT is a State Machine Replication algorithm, and some states are shown below:

  1. The three states protocol
    NewHeight -> (Propose -> Prevote -> Precommit)+ -> Commit -> NewHeight -> ...
  1. The three states protocol in a height
                            +-------------------------------------+
                            |                                     | (Wait new block)
                            v                                     |
                      +-----------+                         +-----+-----+
         +----------> |  Propose  +--------------+          | NewHeight |
         |            +-----------+              |          +-----------+
         |                                       |                ^
         | (Else)                                |                |
         |                                       v                |
   +-----+-----+                           +-----------+          |
   | Precommit |  <------------------------+  Prevote  |          | (Wait RichStatus)
   +-----+-----+                           +-----------+          |
         |                                                        |
         | (When +2/3 Precommits for the block found)             |
         v                                                        |
   +--------------------------------------------------------------+-----+
   |  Commit                                                            |
   |                                                                    |
   |  * Generate Proof;                                                 |
   |  * Set CommitTime = now;                                           |
   +--------------------------------------------------------------------+

Architecture

A complete BFT model consists of 4 essential parts:

  1. Consensus Module, the consensus algorithm module includes signature verification, proof generation, version check, etc.;

  2. State Machine, the BFT state machine is focused on consensus proposal;

  3. Transport Module, the network for consensus module to communicate with other modules;

  4. Wal Module, the place saving BFT logs.

NOTICE: The bft-rs only provides a basic BFT state machine and does not support the advanced functions such as signature verification, proof generation, compact block, etc. These functions are in consensus module rather than bft-rs library.

Feature

The bft-rs provides verify_req feature to verify transcation after received a proposal. BFT state machine will check the verify result of the proposal before Precommit step. If it has not received the result of the proposal yet, it will wait for an extra 1/2 of the consensus duration.

Interface

If bft-rs works correctly, it needs to receive 4 types of message: Proposal, Vote, Feed, Status. And bft-rs can send 3 types of message: Proposal, Vote, Commit. Besides, bft-rs also provides Stop and Start message that can control state machine stop or go on. These types of messages consist of the enum BftMsg:

enum BftMsg {
    Proposal(Proposal),
    Vote(Vote),
    Feed(Feed),
    Status(Status),
    Commit(Commit),

    #[cfg(feature = "verify_req")]
    VerifyResp(VerifyResp),
    Pause,
    Start,
}

For detailed introduction, click here.

Usage

First, add bft-rs and crossbeam to your Cargo.toml:

[dependencies]
bft-rs = { git = "https://github.com/cryptape/bft-rs.git", branch = "develop" }

If you want to use verify_req feature, needs to add following codes:

[features]
verify_req = ["bft-rs/verify_req"]

Second, add BFT and channel to your crate as following:

extern crate bft_rs as bft;

use bft::{actuator::BftActuator as BFT, *};

Third, initialize a BFT actuator:

let actuator = BFT::new(address);

The address here is the address of this node with type Vec<u8>.

What needs to illustrate is that the BFT machine is in stop step by default, therefore, the first thing is send BftMsg::Start message. Use send_start() function to send a message to BFT state machine. LikeWise use send_proposal(), send_vote(), send_feed(), send_status(), send_pause() functions to send Proposal, Vote, Feed, Status, Pause messages to the BFT actuator, these functions will return a Result. take Status for example:

actuator.send_start(BftMsg::Start).expect("");

actuator.send_status(BftMsg::Status(status)).expect("");

// only in feature verify_req
actuator.send_verify(BftMsg::VerifyResq(result)).expect("");

And use recv() function and match to receive messages from BFT state machine as following:

if let Ok(msg) = actuator.recv() {
      match msg {
            BftMsg::Proposal(proposal) => {}
            BftMsg::Vote(vote) => {}
            BftMsg::Commit(commit) => {}
            _ => {}
      }
}

If you want to use the BFT height to do some verify, use get_height function as following:

let height: u64 = actuator.get_height();

License

This an open source project under the MIT License.

bft-rs's People

Contributors

kaoimin avatar rev-chaos avatar u2 avatar

Watchers

James Cloos avatar Jiayu Ye avatar  avatar

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.