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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#![deny(missing_docs)]
#![recursion_limit = "512"]
#![allow(clippy::mutable_key_type)]
mod codec;
pub mod error;
pub mod overlord;
pub mod serde_hex;
mod serde_multi_hex;
mod smr;
mod state;
mod timer;
pub mod types;
mod utils;
mod wal;
pub use self::overlord::Overlord;
pub use self::overlord::OverlordHandler;
pub use self::utils::auth_manage::{extract_voters, get_leader};
pub use creep::Context;
pub use wal::WalInfo;
use std::error::Error;
use std::fmt::Debug;
use async_trait::async_trait;
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use crate::error::ConsensusError;
use crate::types::{Address, Commit, Hash, Node, OverlordMsg, Signature, Status, ViewChangeReason};
pub type ConsensusResult<T> = std::result::Result<T, ConsensusError>;
const INIT_HEIGHT: u64 = 0;
const INIT_ROUND: u64 = 0;
#[async_trait]
pub trait Consensus<T: Codec>: Send + Sync {
async fn get_block(
&self,
ctx: Context,
height: u64,
) -> Result<(T, Hash), Box<dyn Error + Send>>;
async fn check_block(
&self,
ctx: Context,
height: u64,
hash: Hash,
block: T,
) -> Result<(), Box<dyn Error + Send>>;
async fn commit(
&self,
ctx: Context,
height: u64,
commit: Commit<T>,
) -> Result<Status, Box<dyn Error + Send>>;
async fn get_authority_list(
&self,
ctx: Context,
height: u64,
) -> Result<Vec<Node>, Box<dyn Error + Send>>;
async fn broadcast_to_other(
&self,
ctx: Context,
msg: OverlordMsg<T>,
) -> Result<(), Box<dyn Error + Send>>;
async fn transmit_to_relayer(
&self,
ctx: Context,
addr: Address,
msg: OverlordMsg<T>,
) -> Result<(), Box<dyn Error + Send>>;
fn report_error(&self, ctx: Context, error: ConsensusError);
fn report_view_change(&self, ctx: Context, height: u64, round: u64, reason: ViewChangeReason);
}
pub trait Codec: Clone + Debug + Send + PartialEq + Eq {
fn encode(&self) -> Result<Bytes, Box<dyn Error + Send>>;
fn decode(data: Bytes) -> Result<Self, Box<dyn Error + Send>>;
}
#[async_trait]
pub trait Wal {
async fn save(&self, info: Bytes) -> Result<(), Box<dyn Error + Send>>;
async fn load(&self) -> Result<Option<Bytes>, Box<dyn Error + Send>>;
}
pub trait Crypto: Send {
fn hash(&self, msg: Bytes) -> Hash;
fn sign(&self, hash: Hash) -> Result<Signature, Box<dyn Error + Send>>;
fn aggregate_signatures(
&self,
signatures: Vec<Signature>,
voters: Vec<Address>,
) -> Result<Signature, Box<dyn Error + Send>>;
fn verify_signature(
&self,
signature: Signature,
hash: Hash,
voter: Address,
) -> Result<(), Box<dyn Error + Send>>;
fn verify_aggregated_signature(
&self,
aggregate_signature: Signature,
msg_hash: Hash,
voters: Vec<Address>,
) -> Result<(), Box<dyn Error + Send>>;
}
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq)]
pub struct DurationConfig {
pub propose_ratio: u64,
pub prevote_ratio: u64,
pub precommit_ratio: u64,
pub brake_ratio: u64,
}
impl DurationConfig {
pub fn new(
propose_ratio: u64,
prevote_ratio: u64,
precommit_ratio: u64,
brake_ratio: u64,
) -> Self {
DurationConfig {
propose_ratio,
prevote_ratio,
precommit_ratio,
brake_ratio,
}
}
pub(crate) fn get_propose_config(&self) -> (u64, u64) {
(self.propose_ratio, 10u64)
}
pub(crate) fn get_prevote_config(&self) -> (u64, u64) {
(self.prevote_ratio, 10u64)
}
pub(crate) fn get_precommit_config(&self) -> (u64, u64) {
(self.precommit_ratio, 10u64)
}
pub(crate) fn get_brake_config(&self) -> (u64, u64) {
(self.brake_ratio, 10u64)
}
}
#[cfg(test)]
mod test {
use super::DurationConfig;
#[test]
fn test_duration_config() {
let config = DurationConfig::new(1, 2, 3, 4);
assert_eq!(config.get_propose_config(), (1, 10));
assert_eq!(config.get_prevote_config(), (2, 10));
assert_eq!(config.get_precommit_config(), (3, 10));
assert_eq!(config.get_brake_config(), (4, 10));
}
}