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

#![deny(missing_docs)]

/// BFT state machine.
pub(crate) mod algorithm;
/// BFT core.
pub mod core;
/// BFT error.
pub mod error;
/// BFT params include time interval and local address.
pub(crate) mod params;
/// BFT timer.
pub(crate) mod timer;
/// BFT types.
pub mod types;
/// BFT vote set.
pub(crate) mod voteset;

/// Re-pub BFT core.
pub use crate::core::Core;

use crate::types::CoreOutput;

/// BFT core send message.
pub trait FromCore {
    /// BFT core send message error.
    type error: ::std::fmt::Debug;
    /// Send a BFT message to outside.
    fn send_msg(&self, msg: CoreOutput) -> Result<(), Self::error>;
}