-
Notifications
You must be signed in to change notification settings - Fork 96
/
honey_badger.rs
237 lines (211 loc) · 8.54 KB
/
honey_badger.rs
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
use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::sync::Arc;
use derivative::Derivative;
use rand::Rng;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use super::epoch_state::EpochState;
use super::{Batch, Error, FaultKind, HoneyBadgerBuilder, Message, Result};
use crate::{ConsensusProtocol, Contribution, Fault, NetworkInfo, NodeIdT};
use super::Params;
/// An instance of the Honey Badger Byzantine fault tolerant consensus algorithm.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct HoneyBadger<C, N> {
/// Shared network data.
pub(super) netinfo: Arc<NetworkInfo<N>>,
/// A session identifier. Different session IDs foil replay attacks in two instances with the
/// same epoch numbers and the same validators.
pub(super) session_id: u64,
/// The earliest epoch from which we have not yet received output.
pub(super) epoch: u64,
/// Whether we have already submitted a proposal for the current epoch.
pub(super) has_input: bool,
/// The subalgorithms for ongoing epochs.
pub(super) epochs: BTreeMap<u64, EpochState<C, N>>,
/// Parameters controlling Honey Badger's behavior and performance.
pub(super) params: Params,
}
/// A `HoneyBadger` step, possibly containing multiple outputs.
pub type Step<C, N> = crate::CpStep<HoneyBadger<C, N>>;
impl<C, N> ConsensusProtocol for HoneyBadger<C, N>
where
C: Contribution + Serialize + DeserializeOwned,
N: NodeIdT,
{
type NodeId = N;
type Input = C;
type Output = Batch<C, N>;
type Message = Message<N>;
type Error = Error;
type FaultKind = FaultKind;
fn handle_input<R: Rng>(&mut self, input: Self::Input, rng: &mut R) -> Result<Step<C, N>> {
self.propose(&input, rng)
}
fn handle_message<R: Rng>(
&mut self,
sender_id: &Self::NodeId,
message: Self::Message,
_rng: &mut R,
) -> Result<Step<C, N>> {
self.handle_message(sender_id, message)
}
fn terminated(&self) -> bool {
false
}
fn our_id(&self) -> &N {
self.netinfo.our_id()
}
}
impl<C, N> HoneyBadger<C, N>
where
C: Contribution + Serialize + DeserializeOwned,
N: NodeIdT,
{
/// Returns a new `HoneyBadgerBuilder` configured to use the node IDs and cryptographic keys
/// specified by `netinfo`.
pub fn builder(netinfo: Arc<NetworkInfo<N>>) -> HoneyBadgerBuilder<C, N> {
HoneyBadgerBuilder::new(netinfo)
}
/// Proposes a contribution in the current epoch.
///
/// Returns an error if we already made a proposal in this epoch.
///
/// If we are the only validator, this will immediately output a batch, containing our
/// proposal.
pub fn propose<R: Rng>(&mut self, proposal: &C, rng: &mut R) -> Result<Step<C, N>> {
if !self.netinfo.is_validator() {
return Ok(Step::default());
}
self.has_input = true;
let step = self.epoch_state_mut(self.epoch)?.propose(proposal, rng)?;
Ok(step.join(self.try_output_batches()?))
}
/// Handles a message received from `sender_id`.
///
/// This must be called with every message we receive from another node.
pub fn handle_message(&mut self, sender_id: &N, message: Message<N>) -> Result<Step<C, N>> {
if !self.netinfo.is_node_validator(sender_id) {
return Err(Error::UnknownSender);
}
let Message { epoch, content } = message;
if epoch > self.epoch + self.params.max_future_epochs {
Ok(Fault::new(sender_id.clone(), FaultKind::UnexpectedHbMessageEpoch).into())
} else if epoch < self.epoch {
// The message is late; discard it.
Ok(Step::default())
} else {
let step = self
.epoch_state_mut(epoch)?
.handle_message_content(sender_id, content)?;
Ok(step.join(self.try_output_batches()?))
}
}
/// Returns `true` if input for the current epoch has already been provided.
pub fn has_input(&self) -> bool {
!self.netinfo.is_validator() || self.has_input
}
/// Returns the current encryption schedule that determines in which epochs contributions are
/// encrypted.
pub fn get_encryption_schedule(&self) -> EncryptionSchedule {
self.params.encryption_schedule
}
/// Returns the epoch of the next batch that will be output.
pub fn next_epoch(&self) -> u64 {
self.epoch
}
/// Returns the information about the node IDs in the network, and the threshold key shares.
pub fn netinfo(&self) -> &Arc<NetworkInfo<N>> {
&self.netinfo
}
/// Skips all epochs before the specified one.
///
/// This must only be called if it is guaranteed to be called in all instances that have not
/// progressed to that epoch yet. Otherwise an instance can be left behind in a skipped epoch.
pub fn skip_to_epoch(&mut self, epoch: u64) {
while self.epoch < epoch {
self.update_epoch();
}
}
/// Returns the number of validators from which we have already received a proposal for the
/// current epoch.
///
/// This can be used to find out whether our node is stalling progress. Depending on the
/// application logic, nodes may e.g. only propose when they have any pending transactions. In
/// that case, they should repeatedly call this method: if it returns _f + 1_ or more, that
/// means at least one correct node has proposed a contribution. In that case, we might want to
/// propose one, too, even if it's empty, to avoid unnecessarily delaying the next batch.
pub fn received_proposals(&self) -> usize {
self.epochs
.get(&self.epoch)
.map_or(0, EpochState::received_proposals)
}
/// Increments the epoch number and clears any state that is local to the finished epoch.
fn update_epoch(&mut self) {
// Clear the state of the old epoch.
self.epochs.remove(&self.epoch);
self.epoch += 1;
self.has_input = false;
}
/// Tries to decrypt contributions from all proposers and output those in a batch.
fn try_output_batches(&mut self) -> Result<Step<C, N>> {
let mut step = Step::default();
while let Some((batch, fault_log)) = self
.epochs
.get(&self.epoch)
.and_then(EpochState::try_output_batch)
{
// Queue the output and advance the epoch.
step.output.push(batch);
step.fault_log.extend(fault_log);
self.update_epoch();
}
Ok(step)
}
/// Returns a mutable reference to the state of the given `epoch`. Initializes a new one, if it
/// doesn't exist yet.
fn epoch_state_mut(&mut self, epoch: u64) -> Result<&mut EpochState<C, N>> {
Ok(match self.epochs.entry(epoch) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(EpochState::new(
self.netinfo.clone(),
self.session_id,
epoch,
self.params.subset_handling_strategy.clone(),
self.params.encryption_schedule.use_on_epoch(epoch),
)?),
})
}
/// Returns the maximum future epochs of the Honey Badger algorithm instance.
pub fn max_future_epochs(&self) -> u64 {
self.params.max_future_epochs
}
/// Returns the parameters controlling Honey Badger's behavior and performance.
pub fn params(&self) -> &Params {
&self.params
}
}
/// How frequently Threshold Encryption should be used.
#[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Hash, Debug)]
pub enum EncryptionSchedule {
/// Always encrypt. All contributions are encrypted in every epoch.
Always,
/// Never encrypt. All contributions are plaintext in every epoch.
Never,
/// Every _n_-th epoch uses encryption. In all other epochs, contributions are plaintext.
EveryNthEpoch(u32),
/// With `TickTock(n, m)`, `n` epochs use encryption, followed by `m` epochs that don't.
/// `m` out of `n + m` epochs will use plaintext contributions.
TickTock(u32, u32),
}
impl EncryptionSchedule {
/// Returns `true` if the contributions in the `epoch` should be encrypted.
pub fn use_on_epoch(self, epoch: u64) -> bool {
match self {
EncryptionSchedule::Always => true,
EncryptionSchedule::Never => false,
EncryptionSchedule::EveryNthEpoch(n) => (epoch % u64::from(n)) == 0,
EncryptionSchedule::TickTock(on, off) => (epoch % u64::from(on + off)) <= u64::from(on),
}
}
}