forked from poanetwork/hbbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubset.rs
376 lines (342 loc) · 13.8 KB
/
subset.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//! # Subset algorithm.
//!
//! The Subset protocol assumes a network of _N_ nodes that send signed
//! messages to each other, with at most _f_ of them malicious, where _3 f < N_. Handling the
//! networking and signing is the responsibility of the user: only when a message has been
//! verified to be "from node i" (e.g. using cryptographic signatures), it can be handed to the
//! `Subset` instance.
//!
//! Each node proposes an element for inclusion. Under the above conditions, the protocol
//! guarantees that all correct nodes output the same set, consisting of at least _N - f_ of the
//! proposed elements.
//!
//! ## How it works
//!
//! * `Subset` instantiates one `Broadcast` algorithm for each of the participating nodes.
//! At least _N - f_ of these - the ones whose proposer is not faulty - will eventually output
//! the element proposed by that node.
//! * It also instantiates Binary Agreement for each participating node, to decide whether
//! that node's proposed element should be included in the set. Whenever an element is
//! received via broadcast, we input "yes" (`true`) into the corresponding `BinaryAgreement` instance.
//! * When _N - f_ `BinaryAgreement` instances have decided "yes", we input "no" (`false`) into the
//! remaining ones, where we haven't provided input yet.
//! * Once all `BinaryAgreement` instances have decided, `Subset` returns the set of all proposed
//! values for which the decision was "yes".
use std::collections::{BTreeMap, BTreeSet};
use std::result;
use std::sync::Arc;
use hex_fmt::HexFmt;
use binary_agreement::{self, BinaryAgreement};
use broadcast::{self, Broadcast};
use rand::Rand;
use {DistAlgorithm, NetworkInfo, NodeIdT};
/// A subset error.
#[derive(Clone, PartialEq, Debug, Fail)]
pub enum Error {
#[fail(display = "NewBinaryAgreement error: {}", _0)]
NewBinaryAgreement(binary_agreement::Error),
#[fail(display = "ProcessBinaryAgreement0 error: {}", _0)]
ProcessBinaryAgreement0(binary_agreement::Error),
#[fail(display = "ProcessBinaryAgreement1 error: {}", _0)]
ProcessBinaryAgreement1(binary_agreement::Error),
#[fail(display = "NewBroadcast error: {}", _0)]
NewBroadcast(broadcast::Error),
#[fail(display = "ProcessBroadcastBroadcast error: {}", _0)]
ProcessBroadcastBroadcast(broadcast::Error),
#[fail(display = "Multiple Binary Agreement results")]
MultipleBinaryAgreementResults,
#[fail(display = "No such Binary Agreement instance")]
NoSuchBinaryAgreementInstance,
#[fail(display = "No such broadcast instance")]
NoSuchBroadcastInstance,
}
/// A subset result.
pub type Result<T> = ::std::result::Result<T, Error>;
// TODO: Make this a generic argument of `Subset`.
type ProposedValue = Vec<u8>;
/// Message from Subset to remote nodes.
#[derive(Serialize, Deserialize, Clone, Debug, Rand)]
pub enum Message<N: Rand> {
/// A message for the broadcast algorithm concerning the set element proposed by the given node.
Broadcast(N, broadcast::Message),
/// A message for the Binary Agreement algorithm concerning the set element proposed by the given
/// node.
BinaryAgreement(N, binary_agreement::Message),
}
/// Subset algorithm instance
#[derive(Debug)]
pub struct Subset<N: Rand> {
/// Shared network information.
netinfo: Arc<NetworkInfo<N>>,
broadcast_instances: BTreeMap<N, Broadcast<N>>,
ba_instances: BTreeMap<N, BinaryAgreement<N>>,
/// `None` means that that item has already been output.
broadcast_results: BTreeMap<N, Option<ProposedValue>>,
ba_results: BTreeMap<N, bool>,
/// Whether the instance has decided on a value.
decided: bool,
}
pub type Step<N> = ::Step<Subset<N>>;
impl<N: NodeIdT + Rand> DistAlgorithm for Subset<N> {
type NodeId = N;
type Input = ProposedValue;
type Output = SubsetOutput<N>;
type Message = Message<N>;
type Error = Error;
fn handle_input(&mut self, input: Self::Input) -> Result<Step<N>> {
debug!("{:?} Proposing {:?}", self.netinfo.our_id(), HexFmt(&input));
self.send_proposed_value(input)
}
fn handle_message(
&mut self,
sender_id: &Self::NodeId,
message: Self::Message,
) -> Result<Step<N>> {
match message {
Message::Broadcast(p_id, b_msg) => self.handle_broadcast(sender_id, &p_id, b_msg),
Message::BinaryAgreement(p_id, a_msg) => {
self.handle_binary_agreement(sender_id, &p_id, a_msg)
}
}
}
fn terminated(&self) -> bool {
self.ba_instances.values().all(BinaryAgreement::terminated)
}
fn our_id(&self) -> &Self::NodeId {
self.netinfo.our_id()
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SubsetOutput<N> {
Contribution(N, Vec<u8>),
Done,
}
impl<N: NodeIdT + Rand> Subset<N> {
pub fn new(netinfo: Arc<NetworkInfo<N>>, session_id: u64) -> Result<Self> {
// Create all broadcast instances.
let mut broadcast_instances: BTreeMap<N, Broadcast<N>> = BTreeMap::new();
for proposer_id in netinfo.all_ids() {
broadcast_instances.insert(
proposer_id.clone(),
Broadcast::new(netinfo.clone(), proposer_id.clone())
.map_err(Error::NewBroadcast)?,
);
}
// Create all Binary Agreement instances.
let mut ba_instances: BTreeMap<N, BinaryAgreement<N>> = BTreeMap::new();
for proposer_id in netinfo.all_ids() {
ba_instances.insert(
proposer_id.clone(),
BinaryAgreement::new(netinfo.clone(), session_id, proposer_id.clone())
.map_err(Error::NewBinaryAgreement)?,
);
}
Ok(Subset {
netinfo,
broadcast_instances,
ba_instances,
broadcast_results: BTreeMap::new(),
ba_results: BTreeMap::new(),
decided: false,
})
}
/// Subset input message handler. It receives a value for broadcast
/// and redirects it to the corresponding broadcast instance.
pub fn send_proposed_value(&mut self, value: ProposedValue) -> Result<Step<N>> {
if !self.netinfo.is_validator() {
return Ok(Step::default());
}
let id = self.netinfo.our_id().clone();
// Upon receiving input v_i , input v_i to RBC_i. See Figure 2.
self.process_broadcast(&id, |bc| bc.handle_input(value))
}
/// Returns the number of validators from which we have already received a proposal.
pub(crate) fn received_proposals(&self) -> usize {
self.broadcast_results.len()
}
/// Receives a broadcast message from a remote node `sender_id` concerning a
/// value proposed by the node `proposer_id`.
fn handle_broadcast(
&mut self,
sender_id: &N,
proposer_id: &N,
bmessage: broadcast::Message,
) -> Result<Step<N>> {
self.process_broadcast(proposer_id, |bc| bc.handle_message(sender_id, bmessage))
}
/// Receives a Binary Agreement message from a remote node `sender_id` concerning
/// a value proposed by the node `proposer_id`.
fn handle_binary_agreement(
&mut self,
sender_id: &N,
proposer_id: &N,
amessage: binary_agreement::Message,
) -> Result<Step<N>> {
// Send the message to the local instance of Binary Agreement.
self.process_binary_agreement(proposer_id, |binary_agreement| {
binary_agreement.handle_message(sender_id, amessage)
})
}
/// Upon delivery of v_j from RBC_j, if input has not yet been provided to
/// BA_j, then provide input 1 to BA_j. See Figure 11.
fn process_broadcast<F>(&mut self, proposer_id: &N, f: F) -> Result<Step<N>>
where
F: FnOnce(&mut Broadcast<N>) -> result::Result<broadcast::Step<N>, broadcast::Error>,
{
let mut step = Step::default();
let value = {
let broadcast = self
.broadcast_instances
.get_mut(proposer_id)
.ok_or(Error::NoSuchBroadcastInstance)?;
let to_msg = |b_msg| Message::Broadcast(proposer_id.clone(), b_msg);
let output = step.extend_with(
f(broadcast).map_err(Error::ProcessBroadcastBroadcast)?,
to_msg,
);
if let Some(output) = output.into_iter().next() {
output
} else {
return Ok(step);
}
};
let val_to_insert = if let Some(true) = self.ba_results.get(proposer_id) {
debug!(" {:?} → {:?}", proposer_id, HexFmt(&value));
step.output
.extend(Some(SubsetOutput::Contribution(proposer_id.clone(), value)));
None
} else {
Some(value)
};
if let Some(inval) = self
.broadcast_results
.insert(proposer_id.clone(), val_to_insert)
{
error!("Duplicate insert in broadcast_results: {:?}", inval)
}
let set_binary_agreement_input = |ba: &mut BinaryAgreement<N>| {
if ba.accepts_input() {
ba.handle_input(true)
} else {
Ok(binary_agreement::Step::default())
}
};
step.extend(self.process_binary_agreement(proposer_id, set_binary_agreement_input)?);
Ok(step)
}
/// Callback to be invoked on receipt of the decision value of the Binary Agreement
/// instance `id`.
fn process_binary_agreement<F>(&mut self, proposer_id: &N, f: F) -> Result<Step<N>>
where
F: FnOnce(&mut BinaryAgreement<N>) -> binary_agreement::Result<binary_agreement::Step<N>>,
{
let mut step = Step::default();
let accepted = {
let binary_agreement = self
.ba_instances
.get_mut(proposer_id)
.ok_or(Error::NoSuchBinaryAgreementInstance)?;
if binary_agreement.terminated() {
return Ok(step);
}
let to_msg = |a_msg| Message::BinaryAgreement(proposer_id.clone(), a_msg);
let output = step.extend_with(
f(binary_agreement).map_err(Error::ProcessBinaryAgreement0)?,
to_msg,
);
if let Some(accepted) = output.into_iter().next() {
accepted
} else {
return Ok(step);
}
};
// Binary agreement result accepted.
if self
.ba_results
.insert(proposer_id.clone(), accepted)
.is_some()
{
return Err(Error::MultipleBinaryAgreementResults);
}
debug!(
"{:?} Updated Binary Agreement results: {:?}",
self.netinfo.our_id(),
self.ba_results
);
if accepted {
if self.count_true() == self.netinfo.num_correct() {
// Upon delivery of value 1 from at least N − f instances of BA, provide
// input 0 to each instance of BA that has not yet been provided input.
for (id, binary_agreement) in &mut self.ba_instances {
if binary_agreement.accepts_input() {
let to_msg = |a_msg| Message::BinaryAgreement(id.clone(), a_msg);
for output in step.extend_with(
binary_agreement
.handle_input(false)
.map_err(Error::ProcessBinaryAgreement1)?,
to_msg,
) {
if self.ba_results.insert(id.clone(), output).is_some() {
return Err(Error::MultipleBinaryAgreementResults);
}
}
}
}
}
if let Some(Some(value)) = self.broadcast_results.insert(proposer_id.clone(), None) {
debug!(" {:?} → {:?}", proposer_id, HexFmt(&value));
step.output
.extend(Some(SubsetOutput::Contribution(proposer_id.clone(), value)));
}
}
step.output.extend(self.try_binary_agreement_completion());
Ok(step)
}
/// Returns the number of Binary Agreement instances that have decided "yes".
fn count_true(&self) -> usize {
self.ba_results.values().filter(|v| **v).count()
}
fn try_binary_agreement_completion(&mut self) -> Option<SubsetOutput<N>> {
if self.decided || self.count_true() < self.netinfo.num_correct() {
return None;
}
// Once all instances of BA have completed, let C ⊂ [1..N] be
// the indexes of each BA that delivered 1. Wait for the output
// v_j for each RBC_j such that j∈C. Finally output ∪ j∈C v_j.
if self.ba_results.len() < self.netinfo.num_nodes() {
return None;
}
debug!(
"{:?} All Binary Agreement instances have terminated",
self.netinfo.our_id()
);
// All instances of BinaryAgreement that delivered `true` (or "1" in the paper).
let delivered_1: BTreeSet<&N> = self
.ba_results
.iter()
.filter(|(_, v)| **v)
.map(|(k, _)| k)
.collect();
debug!(
"Binary Agreement instances that delivered 1: {:?}",
delivered_1
);
// Results of Broadcast instances in `delivered_1`
let broadcast_results: BTreeSet<&N> = self
.broadcast_results
.iter()
.filter(|(k, _)| delivered_1.contains(k))
.map(|(k, _)| k)
.collect();
if delivered_1.len() == broadcast_results.len() {
debug!(
"{:?} Binary Agreement instances completed:",
self.netinfo.our_id()
);
self.decided = true;
Some(SubsetOutput::Done)
} else {
None
}
}
}