2018-05-16 05:23:57 -07:00
|
|
|
use std::collections::btree_map::Entry;
|
2018-06-19 07:17:16 -07:00
|
|
|
use std::collections::{BTreeMap, BTreeSet, HashSet, VecDeque};
|
2018-05-16 05:23:57 -07:00
|
|
|
use std::fmt::Debug;
|
2018-05-12 07:09:07 -07:00
|
|
|
use std::hash::Hash;
|
2018-05-29 05:17:30 -07:00
|
|
|
use std::rc::Rc;
|
2018-05-16 05:23:57 -07:00
|
|
|
use std::{cmp, iter};
|
2018-05-12 07:09:07 -07:00
|
|
|
|
|
|
|
use bincode;
|
2018-06-19 07:17:16 -07:00
|
|
|
use pairing::bls12_381::Bls12;
|
2018-05-16 05:23:57 -07:00
|
|
|
use rand;
|
2018-05-12 07:09:07 -07:00
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
use common_subset::{self, CommonSubset};
|
2018-06-19 07:17:16 -07:00
|
|
|
use crypto::{Ciphertext, DecryptionShare};
|
|
|
|
use messaging::{DistAlgorithm, NetworkInfo, Target, TargetedMessage};
|
2018-05-12 07:09:07 -07:00
|
|
|
|
2018-05-20 04:51:33 -07:00
|
|
|
error_chain!{
|
|
|
|
types {
|
|
|
|
Error, ErrorKind, ResultExt, HoneyBadgerResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
links {
|
|
|
|
CommonSubset(common_subset::Error, common_subset::ErrorKind);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreign_links {
|
|
|
|
Bincode(Box<bincode::ErrorKind>);
|
|
|
|
}
|
|
|
|
|
|
|
|
errors {
|
|
|
|
UnknownSender
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 07:09:07 -07:00
|
|
|
/// An instance of the Honey Badger Byzantine fault tolerant consensus algorithm.
|
2018-06-18 07:37:07 -07:00
|
|
|
pub struct HoneyBadger<Tx, NodeUid> {
|
2018-05-29 05:17:30 -07:00
|
|
|
/// Shared network data.
|
2018-06-18 07:14:17 -07:00
|
|
|
netinfo: Rc<NetworkInfo<NodeUid>>,
|
2018-05-16 05:23:57 -07:00
|
|
|
/// The buffer of transactions that have not yet been included in any output batch.
|
2018-06-18 07:14:17 -07:00
|
|
|
buffer: Vec<Tx>,
|
2018-05-16 05:23:57 -07:00
|
|
|
/// The earliest epoch from which we have not yet received output.
|
2018-05-12 07:09:07 -07:00
|
|
|
epoch: u64,
|
2018-05-16 05:23:57 -07:00
|
|
|
/// The Asynchronous Common Subset instance that decides which nodes' transactions to include,
|
|
|
|
/// indexed by epoch.
|
2018-06-18 07:14:17 -07:00
|
|
|
common_subsets: BTreeMap<u64, CommonSubset<NodeUid>>,
|
2018-05-12 07:09:07 -07:00
|
|
|
/// The target number of transactions to be included in each batch.
|
|
|
|
// TODO: Do experiments and recommend a batch size. It should be proportional to
|
|
|
|
// `num_nodes * num_nodes * log(num_nodes)`.
|
|
|
|
batch_size: usize,
|
2018-05-14 05:35:06 -07:00
|
|
|
/// The messages that need to be sent to other nodes.
|
2018-06-18 07:14:17 -07:00
|
|
|
messages: MessageQueue<NodeUid>,
|
2018-05-14 05:35:06 -07:00
|
|
|
/// The outputs from completed epochs.
|
2018-06-18 07:14:17 -07:00
|
|
|
output: VecDeque<Batch<Tx, NodeUid>>,
|
2018-06-19 07:17:16 -07:00
|
|
|
/// Received decryption shares for an epoch. Each decryption share has a sender and a
|
|
|
|
/// proposer. The outer `BTreeMap` has epochs as its key. The next `BTreeMap` has proposers as
|
|
|
|
/// its key. The inner `BTreeMap` has the sender as its key.
|
|
|
|
received_shares: BTreeMap<u64, BTreeMap<NodeUid, BTreeMap<NodeUid, DecryptionShare<Bls12>>>>,
|
|
|
|
/// Decoded accepted proposals.
|
|
|
|
decoded_selections: BTreeMap<NodeUid, Vec<u8>>,
|
|
|
|
/// Ciphertexts output by Common Subset.
|
|
|
|
ciphertexts: BTreeMap<NodeUid, Ciphertext<Bls12>>,
|
2018-05-14 05:35:06 -07:00
|
|
|
}
|
|
|
|
|
2018-06-18 07:14:17 -07:00
|
|
|
impl<Tx, NodeUid> DistAlgorithm for HoneyBadger<Tx, NodeUid>
|
2018-05-14 05:35:06 -07:00
|
|
|
where
|
2018-06-18 07:14:17 -07:00
|
|
|
Tx: Eq + Hash + Serialize + DeserializeOwned + Debug,
|
2018-06-18 07:37:07 -07:00
|
|
|
NodeUid: Ord + Clone + Debug,
|
2018-05-14 05:35:06 -07:00
|
|
|
{
|
2018-06-18 07:14:17 -07:00
|
|
|
type NodeUid = NodeUid;
|
|
|
|
type Input = Tx;
|
|
|
|
type Output = Batch<Tx, NodeUid>;
|
|
|
|
type Message = Message<NodeUid>;
|
2018-05-14 05:35:06 -07:00
|
|
|
type Error = Error;
|
|
|
|
|
2018-05-20 04:51:33 -07:00
|
|
|
fn input(&mut self, input: Self::Input) -> HoneyBadgerResult<()> {
|
2018-05-14 05:35:06 -07:00
|
|
|
self.add_transactions(iter::once(input))
|
|
|
|
}
|
|
|
|
|
2018-06-18 07:14:17 -07:00
|
|
|
fn handle_message(
|
|
|
|
&mut self,
|
|
|
|
sender_id: &NodeUid,
|
|
|
|
message: Self::Message,
|
|
|
|
) -> HoneyBadgerResult<()> {
|
2018-05-29 05:17:30 -07:00
|
|
|
if !self.netinfo.all_uids().contains(sender_id) {
|
2018-05-20 04:51:33 -07:00
|
|
|
return Err(ErrorKind::UnknownSender.into());
|
2018-05-14 05:35:06 -07:00
|
|
|
}
|
2018-06-19 07:17:16 -07:00
|
|
|
let Message { epoch, content } = message;
|
|
|
|
if epoch < self.epoch {
|
|
|
|
// Ignore all messages from past epochs.
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
match content {
|
|
|
|
MessageContent::CommonSubset(cs_msg) => {
|
2018-05-14 05:35:06 -07:00
|
|
|
self.handle_common_subset_message(sender_id, epoch, cs_msg)
|
|
|
|
}
|
2018-06-19 07:17:16 -07:00
|
|
|
MessageContent::DecryptionShare { proposer_id, share } => {
|
|
|
|
self.handle_decryption_share(sender_id, epoch, &proposer_id, &share)
|
|
|
|
}
|
2018-05-14 05:35:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 07:14:17 -07:00
|
|
|
fn next_message(&mut self) -> Option<TargetedMessage<Self::Message, NodeUid>> {
|
2018-05-14 05:35:06 -07:00
|
|
|
self.messages.pop_front()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_output(&mut self) -> Option<Self::Output> {
|
|
|
|
self.output.pop_front()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn terminated(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2018-06-18 07:14:17 -07:00
|
|
|
fn our_id(&self) -> &NodeUid {
|
2018-05-29 05:17:30 -07:00
|
|
|
self.netinfo.our_uid()
|
2018-05-14 05:35:06 -07:00
|
|
|
}
|
2018-05-12 07:09:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Use a threshold encryption scheme to encrypt the proposed transactions.
|
2018-06-18 07:14:17 -07:00
|
|
|
impl<Tx, NodeUid> HoneyBadger<Tx, NodeUid>
|
2018-05-12 07:09:07 -07:00
|
|
|
where
|
2018-06-18 07:14:17 -07:00
|
|
|
Tx: Eq + Hash + Serialize + DeserializeOwned + Debug,
|
2018-06-18 07:37:07 -07:00
|
|
|
NodeUid: Ord + Clone + Debug,
|
2018-05-12 07:09:07 -07:00
|
|
|
{
|
|
|
|
/// Returns a new Honey Badger instance with the given parameters, starting at epoch `0`.
|
2018-05-29 06:52:32 -07:00
|
|
|
pub fn new<TI>(
|
2018-06-18 07:14:17 -07:00
|
|
|
netinfo: Rc<NetworkInfo<NodeUid>>,
|
2018-05-21 02:01:49 -07:00
|
|
|
batch_size: usize,
|
|
|
|
txs: TI,
|
|
|
|
) -> HoneyBadgerResult<Self>
|
2018-05-12 07:09:07 -07:00
|
|
|
where
|
2018-06-18 07:14:17 -07:00
|
|
|
TI: IntoIterator<Item = Tx>,
|
2018-05-12 07:09:07 -07:00
|
|
|
{
|
2018-05-16 05:23:57 -07:00
|
|
|
let mut honey_badger = HoneyBadger {
|
2018-05-29 06:52:32 -07:00
|
|
|
netinfo,
|
2018-05-16 05:23:57 -07:00
|
|
|
buffer: txs.into_iter().collect(),
|
2018-05-12 07:09:07 -07:00
|
|
|
epoch: 0,
|
2018-05-16 05:23:57 -07:00
|
|
|
common_subsets: BTreeMap::new(),
|
2018-05-12 07:09:07 -07:00
|
|
|
batch_size,
|
2018-05-19 05:29:31 -07:00
|
|
|
messages: MessageQueue(VecDeque::new()),
|
2018-05-14 05:35:06 -07:00
|
|
|
output: VecDeque::new(),
|
2018-06-19 07:17:16 -07:00
|
|
|
received_shares: BTreeMap::new(),
|
|
|
|
decoded_selections: BTreeMap::new(),
|
|
|
|
ciphertexts: BTreeMap::new(),
|
2018-05-16 05:23:57 -07:00
|
|
|
};
|
|
|
|
honey_badger.propose()?;
|
|
|
|
Ok(honey_badger)
|
2018-05-12 07:09:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds transactions into the buffer.
|
2018-06-18 07:14:17 -07:00
|
|
|
pub fn add_transactions<I: IntoIterator<Item = Tx>>(
|
|
|
|
&mut self,
|
|
|
|
txs: I,
|
|
|
|
) -> HoneyBadgerResult<()> {
|
2018-05-12 07:09:07 -07:00
|
|
|
self.buffer.extend(txs);
|
2018-05-16 05:23:57 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Proposes a new batch in the current epoch.
|
2018-05-20 04:51:33 -07:00
|
|
|
fn propose(&mut self) -> HoneyBadgerResult<()> {
|
2018-05-16 05:23:57 -07:00
|
|
|
let proposal = self.choose_transactions()?;
|
|
|
|
let cs = match self.common_subsets.entry(self.epoch) {
|
|
|
|
Entry::Occupied(entry) => entry.into_mut(),
|
2018-06-12 02:24:09 -07:00
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(CommonSubset::new(self.netinfo.clone(), self.epoch)?)
|
|
|
|
}
|
2018-05-16 05:23:57 -07:00
|
|
|
};
|
2018-06-19 07:17:16 -07:00
|
|
|
let ciphertext = self.netinfo.public_key_set().public_key().encrypt(proposal);
|
|
|
|
cs.input(bincode::serialize(&ciphertext).unwrap())?;
|
2018-05-19 05:29:31 -07:00
|
|
|
self.messages.extend_with_epoch(self.epoch, cs);
|
2018-05-14 05:35:06 -07:00
|
|
|
Ok(())
|
2018-05-12 07:09:07 -07:00
|
|
|
}
|
|
|
|
|
2018-05-16 05:23:57 -07:00
|
|
|
/// Returns a random choice of `batch_size / all_uids.len()` buffered transactions, and
|
|
|
|
/// serializes them.
|
2018-05-20 04:51:33 -07:00
|
|
|
fn choose_transactions(&self) -> HoneyBadgerResult<Vec<u8>> {
|
2018-05-16 05:23:57 -07:00
|
|
|
let mut rng = rand::thread_rng();
|
2018-05-29 05:17:30 -07:00
|
|
|
let amount = cmp::max(1, self.batch_size / self.netinfo.all_uids().len());
|
2018-05-17 02:51:14 -07:00
|
|
|
let batch_size = cmp::min(self.batch_size, self.buffer.len());
|
|
|
|
let sample = match rand::seq::sample_iter(&mut rng, &self.buffer[..batch_size], amount) {
|
2018-05-16 05:23:57 -07:00
|
|
|
Ok(choice) => choice,
|
|
|
|
Err(choice) => choice, // Fewer than `amount` were available, which is fine.
|
|
|
|
};
|
|
|
|
debug!(
|
|
|
|
"{:?} Proposing in epoch {}: {:?}",
|
2018-05-29 05:17:30 -07:00
|
|
|
self.netinfo.our_uid(),
|
|
|
|
self.epoch,
|
|
|
|
sample
|
2018-05-16 05:23:57 -07:00
|
|
|
);
|
|
|
|
Ok(bincode::serialize(&sample)?)
|
|
|
|
}
|
|
|
|
|
2018-05-14 05:35:06 -07:00
|
|
|
/// Handles a message for the common subset sub-algorithm.
|
2018-05-12 07:09:07 -07:00
|
|
|
fn handle_common_subset_message(
|
|
|
|
&mut self,
|
2018-06-18 07:14:17 -07:00
|
|
|
sender_id: &NodeUid,
|
2018-05-12 07:09:07 -07:00
|
|
|
epoch: u64,
|
2018-06-18 07:14:17 -07:00
|
|
|
message: common_subset::Message<NodeUid>,
|
2018-05-20 04:51:33 -07:00
|
|
|
) -> HoneyBadgerResult<()> {
|
2018-05-16 05:23:57 -07:00
|
|
|
{
|
|
|
|
// Borrow the instance for `epoch`, or create it.
|
|
|
|
let cs = match self.common_subsets.entry(epoch) {
|
|
|
|
Entry::Occupied(entry) => entry.into_mut(),
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
if epoch < self.epoch {
|
|
|
|
return Ok(()); // Epoch has already terminated. Message is obsolete.
|
|
|
|
} else {
|
2018-06-12 02:24:09 -07:00
|
|
|
entry.insert(CommonSubset::new(self.netinfo.clone(), epoch)?)
|
2018-05-16 05:23:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Handle the message and put the outgoing messages into the queue.
|
|
|
|
cs.handle_message(sender_id, message)?;
|
2018-05-19 05:29:31 -07:00
|
|
|
self.messages.extend_with_epoch(epoch, cs);
|
2018-05-12 07:09:07 -07:00
|
|
|
}
|
2018-05-16 05:23:57 -07:00
|
|
|
// If this is the current epoch, the message could cause a new output.
|
|
|
|
if epoch == self.epoch {
|
|
|
|
self.process_output()?;
|
|
|
|
}
|
|
|
|
self.remove_terminated(epoch);
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-05-15 10:18:05 -07:00
|
|
|
|
2018-06-19 07:17:16 -07:00
|
|
|
/// Handles decryption shares sent by `HoneyBadger` instances.
|
|
|
|
fn handle_decryption_share(
|
|
|
|
&mut self,
|
|
|
|
sender_id: &NodeUid,
|
|
|
|
epoch: u64,
|
|
|
|
proposer_id: &NodeUid,
|
|
|
|
share: &DecryptionShare<Bls12>,
|
|
|
|
) -> HoneyBadgerResult<()> {
|
|
|
|
self.received_shares
|
|
|
|
.entry(epoch)
|
|
|
|
.and_modify(|epoch_entry| {
|
|
|
|
epoch_entry
|
|
|
|
.entry(proposer_id.clone())
|
|
|
|
.and_modify(|proposer_entry| {
|
|
|
|
proposer_entry.insert(sender_id.clone(), share.clone());
|
|
|
|
})
|
|
|
|
.or_insert_with(|| {
|
|
|
|
vec![(sender_id.clone(), share.clone())]
|
|
|
|
.into_iter()
|
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.or_insert_with(BTreeMap::new);
|
|
|
|
|
|
|
|
self.decode_selection(proposer_id);
|
|
|
|
|
2018-05-16 05:23:57 -07:00
|
|
|
let old_epoch = self.epoch;
|
2018-06-19 07:17:16 -07:00
|
|
|
|
|
|
|
// Wait until selections have been successfully decoded for all proposer nodes with correct
|
|
|
|
// ciphertext outputs.
|
|
|
|
if self.all_selections_decoded() {
|
2018-05-16 05:23:57 -07:00
|
|
|
// Deserialize the output.
|
2018-06-19 07:17:16 -07:00
|
|
|
let transactions: BTreeMap<NodeUid, Vec<Tx>> = self
|
|
|
|
.decoded_selections
|
|
|
|
.iter()
|
|
|
|
.flat_map(|(_, ser_batch)| {
|
2018-05-31 07:32:54 -07:00
|
|
|
// If serialization fails, the proposer of that batch is faulty. Ignore it.
|
2018-06-18 07:14:17 -07:00
|
|
|
bincode::deserialize::<Vec<Tx>>(&ser_batch)
|
|
|
|
.ok()
|
2018-06-19 07:17:16 -07:00
|
|
|
.map(|proposed| (proposer_id.clone(), proposed))
|
2018-05-31 07:32:54 -07:00
|
|
|
})
|
2018-05-16 05:23:57 -07:00
|
|
|
.collect();
|
2018-06-18 07:14:17 -07:00
|
|
|
let batch = Batch {
|
|
|
|
epoch: self.epoch,
|
|
|
|
transactions,
|
|
|
|
};
|
|
|
|
{
|
|
|
|
let tx_set: HashSet<&Tx> = batch.iter().collect();
|
|
|
|
// Remove the output transactions from our buffer.
|
|
|
|
self.buffer.retain(|tx| !tx_set.contains(&tx));
|
|
|
|
}
|
2018-05-16 05:23:57 -07:00
|
|
|
debug!(
|
|
|
|
"{:?} Epoch {} output {:?}",
|
2018-05-29 05:17:30 -07:00
|
|
|
self.netinfo.our_uid(),
|
|
|
|
self.epoch,
|
2018-06-18 07:14:17 -07:00
|
|
|
batch.transactions,
|
2018-05-16 05:23:57 -07:00
|
|
|
);
|
|
|
|
// Queue the output and advance the epoch.
|
2018-06-18 07:14:17 -07:00
|
|
|
self.output.push_back(batch);
|
2018-06-19 07:17:16 -07:00
|
|
|
self.update_epoch();
|
2018-05-16 05:23:57 -07:00
|
|
|
}
|
|
|
|
// If we have moved to a new epoch, propose a new batch of transactions.
|
|
|
|
if self.epoch > old_epoch {
|
|
|
|
self.propose()?;
|
2018-05-14 05:35:06 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
2018-05-12 07:09:07 -07:00
|
|
|
}
|
2018-05-16 05:23:57 -07:00
|
|
|
|
2018-06-19 07:17:16 -07:00
|
|
|
/// 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.ciphertexts.clear();
|
|
|
|
self.decoded_selections.clear();
|
|
|
|
self.received_shares.remove(&self.epoch);
|
|
|
|
self.epoch += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn all_selections_decoded(&self) -> bool {
|
|
|
|
let all_ciphertext_proposers: BTreeSet<_> = self.ciphertexts.keys().collect();
|
|
|
|
let all_decoded_selection_proposers: BTreeSet<_> = self.decoded_selections.keys().collect();
|
|
|
|
all_ciphertext_proposers.is_subset(&all_decoded_selection_proposers)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decode_selection(&mut self, proposer_id: &NodeUid) {
|
|
|
|
let shares = &self.received_shares[&self.epoch][proposer_id];
|
|
|
|
if shares.len() > self.netinfo.num_faulty() {
|
|
|
|
if let Some(ciphertext) = self.ciphertexts.get(proposer_id) {
|
|
|
|
let ids_u64: BTreeMap<&NodeUid, u64> = shares
|
|
|
|
.keys()
|
|
|
|
.map(|id| (id, *self.netinfo.node_index(id).unwrap() as u64))
|
|
|
|
.collect();
|
|
|
|
let indexed_shares: BTreeMap<&u64, &_> = shares
|
|
|
|
.iter()
|
|
|
|
.map(|(id, share)| (&ids_u64[&id], share))
|
|
|
|
.collect();
|
|
|
|
if let Ok(decoded_selection) = self
|
|
|
|
.netinfo
|
|
|
|
.public_key_set()
|
|
|
|
.decrypt(indexed_shares, ciphertext)
|
|
|
|
{
|
|
|
|
self.decoded_selections
|
|
|
|
.insert(proposer_id.clone(), decoded_selection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_decryption_shares(
|
|
|
|
&mut self,
|
|
|
|
cs_output: BTreeMap<NodeUid, Vec<u8>>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
for (proposer_id, v) in cs_output {
|
|
|
|
let mut ciphertext: Ciphertext<Bls12>;
|
|
|
|
if let Ok(ct) = bincode::deserialize(&v) {
|
|
|
|
ciphertext = ct;
|
|
|
|
} else {
|
|
|
|
warn!("Invalid ciphertext from proposer {:?} ignored", proposer_id);
|
|
|
|
// TODO: Log the incorrect node `j`.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Some(share) = self.netinfo.secret_key().decrypt_share(&ciphertext) {
|
|
|
|
// Send the share to remote nodes.
|
|
|
|
self.messages.0.push_back(
|
|
|
|
Target::All.message(
|
|
|
|
MessageContent::DecryptionShare {
|
|
|
|
proposer_id: proposer_id.clone(),
|
|
|
|
share: share.clone(),
|
|
|
|
}.with_epoch(self.epoch),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
let our_id = self.netinfo.our_uid().clone();
|
|
|
|
let epoch = self.epoch;
|
|
|
|
// Receive the share locally.
|
|
|
|
self.handle_decryption_share(&our_id, epoch, &proposer_id, &share)?;
|
|
|
|
} else {
|
|
|
|
warn!("Share decryption failed for proposer {:?}", proposer_id);
|
|
|
|
// TODO: Log the decryption failure.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
self.ciphertexts.insert(proposer_id, ciphertext);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks whether the current epoch has output, and if it does, advances the epoch and
|
|
|
|
/// proposes a new batch.
|
|
|
|
fn process_output(&mut self) -> Result<(), Error> {
|
|
|
|
if let Some(cs_output) = self.take_current_output() {
|
|
|
|
self.send_decryption_shares(cs_output)?;
|
|
|
|
// TODO: May also check that there is no further output from Common Subset.
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-05-16 05:23:57 -07:00
|
|
|
/// Returns the output of the current epoch's `CommonSubset` instance, if any.
|
2018-06-18 07:14:17 -07:00
|
|
|
fn take_current_output(&mut self) -> Option<BTreeMap<NodeUid, Vec<u8>>> {
|
2018-05-16 05:23:57 -07:00
|
|
|
self.common_subsets
|
|
|
|
.get_mut(&self.epoch)
|
|
|
|
.and_then(CommonSubset::next_output)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes all `CommonSubset` instances from _past_ epochs that have terminated.
|
|
|
|
fn remove_terminated(&mut self, from_epoch: u64) {
|
|
|
|
for epoch in from_epoch..self.epoch {
|
2018-05-21 02:01:49 -07:00
|
|
|
if self
|
|
|
|
.common_subsets
|
2018-05-16 05:23:57 -07:00
|
|
|
.get(&epoch)
|
|
|
|
.map_or(false, CommonSubset::terminated)
|
|
|
|
{
|
2018-05-29 05:17:30 -07:00
|
|
|
debug!(
|
|
|
|
"{:?} Epoch {} has terminated.",
|
|
|
|
self.netinfo.our_uid(),
|
|
|
|
epoch
|
|
|
|
);
|
2018-05-16 05:23:57 -07:00
|
|
|
self.common_subsets.remove(&epoch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-12 07:09:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A batch of transactions the algorithm has output.
|
2018-05-17 02:51:14 -07:00
|
|
|
#[derive(Clone)]
|
2018-06-18 07:14:17 -07:00
|
|
|
pub struct Batch<Tx, NodeUid> {
|
2018-05-12 07:09:07 -07:00
|
|
|
pub epoch: u64,
|
2018-06-18 07:14:17 -07:00
|
|
|
pub transactions: BTreeMap<NodeUid, Vec<Tx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Tx, NodeUid: Ord> Batch<Tx, NodeUid> {
|
|
|
|
/// Returns an iterator over all transactions included in the batch.
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &Tx> {
|
|
|
|
self.transactions.values().flat_map(|vec| vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of transactions in the batch (without detecting duplicates).
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.transactions.values().map(Vec::len).sum()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the batch contains no transactions.
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.transactions.values().all(Vec::is_empty)
|
|
|
|
}
|
2018-05-12 07:09:07 -07:00
|
|
|
}
|
|
|
|
|
2018-06-19 07:17:16 -07:00
|
|
|
/// The content of a `HoneyBadger` message. It should be further annotated with an epoch.
|
|
|
|
#[cfg_attr(feature = "serialization-serde", derive(Serialize, Deserialize))]
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum MessageContent<NodeUid> {
|
2018-05-12 07:09:07 -07:00
|
|
|
/// A message belonging to the common subset algorithm in the given epoch.
|
2018-06-19 07:17:16 -07:00
|
|
|
CommonSubset(common_subset::Message<NodeUid>),
|
|
|
|
/// A decrypted share of the output of `proposer_id`.
|
|
|
|
DecryptionShare {
|
|
|
|
proposer_id: NodeUid,
|
|
|
|
share: DecryptionShare<Bls12>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<NodeUid> MessageContent<NodeUid> {
|
|
|
|
fn with_epoch(self, epoch: u64) -> Message<NodeUid> {
|
|
|
|
Message {
|
|
|
|
epoch,
|
|
|
|
content: self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A message sent to or received from another node's Honey Badger instance.
|
|
|
|
#[cfg_attr(feature = "serialization-serde", derive(Serialize, Deserialize))]
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Message<NodeUid> {
|
|
|
|
epoch: u64,
|
|
|
|
content: MessageContent<NodeUid>,
|
2018-05-12 07:09:07 -07:00
|
|
|
}
|
|
|
|
|
2018-05-19 05:29:31 -07:00
|
|
|
/// The queue of outgoing messages in a `HoneyBadger` instance.
|
|
|
|
#[derive(Deref, DerefMut)]
|
2018-06-18 07:14:17 -07:00
|
|
|
struct MessageQueue<NodeUid>(VecDeque<TargetedMessage<Message<NodeUid>, NodeUid>>);
|
2018-05-19 05:29:31 -07:00
|
|
|
|
2018-06-18 07:37:07 -07:00
|
|
|
impl<NodeUid: Clone + Debug + Ord> MessageQueue<NodeUid> {
|
2018-05-19 05:29:31 -07:00
|
|
|
/// Appends to the queue the messages from `cs`, wrapped with `epoch`.
|
2018-06-18 07:14:17 -07:00
|
|
|
fn extend_with_epoch(&mut self, epoch: u64, cs: &mut CommonSubset<NodeUid>) {
|
|
|
|
let convert = |msg: TargetedMessage<common_subset::Message<NodeUid>, NodeUid>| {
|
2018-06-19 07:17:16 -07:00
|
|
|
msg.map(|cs_msg| MessageContent::CommonSubset(cs_msg).with_epoch(epoch))
|
2018-05-19 05:29:31 -07:00
|
|
|
};
|
|
|
|
self.extend(cs.message_iter().map(convert));
|
|
|
|
}
|
|
|
|
}
|