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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use crate::{
consensus::{ConsensusProtocol, ConsensusState, Dag},
utils, ConsensusOutput, SequenceNumber,
};
use config::{Committee, Stake};
use fastcrypto::{traits::EncodeDecodeBase64, Hash};
use std::{collections::HashMap, sync::Arc};
use tracing::debug;
use types::{Certificate, CertificateDigest, ConsensusStore, Round, StoreResult};
#[cfg(any(test))]
#[path = "tests/tusk_tests.rs"]
pub mod tusk_tests;
pub struct Tusk {
pub committee: Committee,
pub store: Arc<ConsensusStore>,
pub gc_depth: Round,
}
impl ConsensusProtocol for Tusk {
fn process_certificate(
&mut self,
state: &mut ConsensusState,
consensus_index: SequenceNumber,
certificate: Certificate,
) -> StoreResult<Vec<ConsensusOutput>> {
debug!("Processing {:?}", certificate);
let round = certificate.round();
let mut consensus_index = consensus_index;
state
.dag
.entry(round)
.or_insert_with(HashMap::new)
.insert(certificate.origin(), (certificate.digest(), certificate));
let r = round - 1;
if r % 2 != 0 || r < 4 {
return Ok(Vec::new());
}
let leader_round = r - 2;
if leader_round <= state.last_committed_round {
return Ok(Vec::new());
}
let (leader_digest, leader) = match Self::leader(&self.committee, leader_round, &state.dag)
{
Some(x) => x,
None => return Ok(Vec::new()),
};
let stake: Stake = state
.dag
.get(&(r - 1))
.expect("We should have the whole history by now")
.values()
.filter(|(_, x)| x.header.parents.contains(leader_digest))
.map(|(_, x)| self.committee.stake(&x.origin()))
.sum();
if stake < self.committee.validity_threshold() {
debug!("Leader {:?} does not have enough support", leader);
return Ok(Vec::new());
}
debug!("Leader {:?} has enough support", leader);
let mut sequence = Vec::new();
for leader in utils::order_leaders(&self.committee, leader, state, Self::leader)
.iter()
.rev()
{
for x in utils::order_dag(self.gc_depth, leader, state) {
let digest = x.digest();
state.update(&x, self.gc_depth);
sequence.push(ConsensusOutput {
certificate: x,
consensus_index,
});
consensus_index += 1;
self.store.write_consensus_state(
&state.last_committed,
&consensus_index,
&digest,
)?;
}
}
for (name, round) in &state.last_committed {
debug!("Latest commit of {}: Round {}", name.encode_base64(), round);
}
Ok(sequence)
}
fn update_committee(&mut self, new_committee: Committee) -> StoreResult<()> {
self.committee = new_committee;
self.store.clear()
}
}
impl Tusk {
pub fn new(committee: Committee, store: Arc<ConsensusStore>, gc_depth: Round) -> Self {
Self {
committee,
store,
gc_depth,
}
}
fn leader<'a>(
committee: &Committee,
round: Round,
dag: &'a Dag,
) -> Option<&'a (CertificateDigest, Certificate)> {
cfg_if::cfg_if! {
if #[cfg(test)] {
let leader = committee.authorities.iter().next().expect("Empty authorities table!").0;
} else {
let leader = &committee.leader(round);
}
}
dag.get(&round).and_then(|x| x.get(leader))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::metrics::ConsensusMetrics;
use arc_swap::ArcSwap;
use prometheus::Registry;
use rand::Rng;
use std::collections::BTreeSet;
use test_utils::{make_consensus_store, CommitteeFixture};
use types::Certificate;
#[test]
fn state_limits_test() {
let gc_depth = 12;
let rounds: Round = rand::thread_rng().gen_range(10..100);
let fixture = CommitteeFixture::builder().build();
let committee = fixture.committee();
let keys: Vec<_> = fixture.authorities().map(|a| a.public_key()).collect();
let genesis = Certificate::genesis(&committee)
.iter()
.map(|x| x.digest())
.collect::<BTreeSet<_>>();
let (certificates, _next_parents) =
test_utils::make_optimal_certificates(&committee, 1..=rounds, &genesis, &keys);
let store_path = test_utils::temp_dir();
let store = make_consensus_store(&store_path);
let metrics = Arc::new(ConsensusMetrics::new(&Registry::new()));
let consensus_index = 0;
let mut state = ConsensusState::new(Certificate::genesis(&committee), metrics);
let mut tusk = Tusk::new(committee, store, gc_depth);
for certificate in certificates {
tusk.process_certificate(&mut state, consensus_index, certificate)
.unwrap();
}
let n = state.dag.len();
assert!(n <= 6, "DAG size: {}", n);
}
#[test]
fn imperfect_state_limits_test() {
let gc_depth = 12;
let rounds: Round = rand::thread_rng().gen_range(10..100);
let fixture = CommitteeFixture::builder().build();
let committee = fixture.committee();
let keys: Vec<_> = fixture.authorities().map(|a| a.public_key()).collect();
let genesis = Certificate::genesis(&committee)
.iter()
.map(|x| x.digest())
.collect::<BTreeSet<_>>();
let (certificates, _next_parents) =
test_utils::make_certificates(&committee, 1..=rounds, &genesis, &keys, 0.333);
let arc_committee = Arc::new(ArcSwap::from_pointee(committee.clone()));
let store_path = test_utils::temp_dir();
let store = make_consensus_store(&store_path);
let metrics = Arc::new(ConsensusMetrics::new(&Registry::new()));
let mut state = ConsensusState::new(Certificate::genesis(&committee), metrics);
let consensus_index = 0;
let mut tusk = Tusk::new((**arc_committee.load()).clone(), store, gc_depth);
for certificate in certificates {
tusk.process_certificate(&mut state, consensus_index, certificate)
.unwrap();
}
let n = state.dag.len();
assert!(n <= gc_depth as usize, "DAG size: {}", n);
}
}