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
use crate::{
consensus::{ConsensusProtocol, ConsensusState, Dag},
utils, ConsensusOutput,
};
use config::{Committee, Stake};
use fastcrypto::{traits::EncodeDecodeBase64, Hash};
use std::{collections::HashMap, sync::Arc};
use tracing::debug;
use types::{Certificate, CertificateDigest, ConsensusStore, Round, SequenceNumber, StoreResult};
#[cfg(test)]
#[path = "tests/bullshark_tests.rs"]
pub mod bullshark_tests;
pub struct Bullshark {
pub committee: Committee,
pub store: Arc<ConsensusStore>,
pub gc_depth: Round,
}
impl ConsensusProtocol for Bullshark {
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 < 2 {
return Ok(Vec::new());
}
let leader_round = r;
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(&round)
.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 Bullshark {
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))
}
}