bft_core/lib.rs
1//! An efficient and stable Rust library of BFT core for distributed system.
2//!
3//! The crate provides a simplification BFT core which only includes a BFT state
4//! machine. The BFT machine is in stop state in default, so send `BftMsg::Start`
5//! message firstly. If wants to pause running it, send `BftMsg::Pause` message.
6//!
7//! ## Example
8//!
9//! ```rust
10//! # use bft_core::{types::*, Core, FromCore};
11//! # use crossbeam_channel::{Sender, unbounded};
12//! #
13//! # #[derive(Debug)]
14//! # enum Error {
15//! # SendErr,
16//! # }
17//! #
18//! # struct SendMsg(Sender<CoreOutput>);
19//! # impl FromCore for SendMsg {
20//! # type error = Error;
21//! #
22//! # fn send_msg(&self, msg: CoreOutput) -> Result<(), Error> {
23//! # self.0.send(msg).map_err(|_| Error::SendErr)?;
24//! # Ok(())
25//! # }
26//! # }
27//! #
28//! # impl SendMsg {
29//! # fn new(s: Sender<CoreOutput>) -> Self {
30//! # SendMsg(s)
31//! # }
32//! # }
33//! #
34//! # let status = Status {
35//! # height: 0,
36//! # interval: None,
37//! # authority_list: vec![vec![0]],
38//! # };
39//! #
40//! # let feed = Feed {
41//! # height: 1,
42//! # proposal: vec![6, 5, 5, 3, 5],
43//! # };
44//! #
45//! let (s, r) = unbounded();
46//! let mut bft = Core::new(SendMsg::new(s), vec![0]);
47//!
48//! // send message
49//! bft.send_bft_msg(CoreInput::Start).unwrap();
50//! bft.send_bft_msg(CoreInput::Status(status)).unwrap();
51//! bft.send_bft_msg(CoreInput::Feed(feed)).unwrap();
52//! bft.send_bft_msg(CoreInput::Pause).unwrap();
53//!
54//! // receive message
55//! r.recv().unwrap();
56//! ```
57//!
58
59#![deny(missing_docs)]
60
61/// BFT state machine.
62pub(crate) mod algorithm;
63/// BFT core.
64pub mod core;
65/// BFT error.
66pub mod error;
67/// BFT params include time interval and local address.
68pub(crate) mod params;
69/// BFT timer.
70pub(crate) mod timer;
71/// BFT types.
72pub mod types;
73/// BFT vote set.
74pub(crate) mod voteset;
75
76/// Re-pub BFT core.
77pub use crate::core::Core;
78
79use crate::types::CoreOutput;
80
81/// BFT core send message.
82pub trait FromCore {
83 /// BFT core send message error.
84 type error: ::std::fmt::Debug;
85 /// Send a BFT message to outside.
86 fn send_msg(&self, msg: CoreOutput) -> Result<(), Self::error>;
87}