formatting, address pr review concerns, move encryption into epoch_state, turn off borrowed box lint

This commit is contained in:
Logan Collins 2018-10-24 15:46:44 -05:00
parent 877903aeac
commit 982619327b
6 changed files with 47 additions and 30 deletions

2
.gitignore vendored
View File

@ -3,5 +3,3 @@ src/proto/message.rs
**/*.rs.bk
Cargo.lock
net-trace_*.txt
TODOs
.gitignore

View File

@ -24,7 +24,8 @@ impl<N> NodeChange<N> {
pub enum Change<N> {
// Add or Remove a node from the set of validators
NodeChange(NodeChange<N>),
/// Change the threshold encryption schedule. i.e., to increase frequency to prevent censorship or decrease for performance.
/// Change the threshold encryption schedule.
/// Increase frequency to prevent censorship or decrease frequency for increased throughput.
EncryptionSchedule(EncryptionSchedule),
}

View File

@ -16,6 +16,7 @@ use super::{
use fault_log::{Fault, FaultKind, FaultLog};
use honey_badger::{self, HoneyBadger, Message as HbMessage};
use sync_key_gen::{Ack, Part, PartOutcome, SyncKeyGen};
use threshold_decryption::EncryptionSchedule;
use util::SubRng;
use {Contribution, DistAlgorithm, NetworkInfo, NodeIdT, Target};
@ -296,14 +297,14 @@ where
// If DKG completed, apply the change, restart Honey Badger, and inform the user.
debug!("{:?} DKG for {:?} complete!", self.our_id(), kgs.change);
self.netinfo = kgs.key_gen.into_network_info()?;
self.restart_honey_badger(batch_epoch + 1);
self.restart_honey_badger(batch_epoch + 1, None);
ChangeState::Complete(Change::NodeChange(kgs.change))
} else if let Some(change) = self.vote_counter.compute_winner().cloned() {
// If there is a new change, restart DKG. Inform the user about the current change.
step.extend(match &change {
Change::NodeChange(change) => self.update_key_gen(batch_epoch + 1, &change)?,
Change::EncryptionSchedule(_) => {
self.update_encryption_schedule(batch_epoch + 1, &change)?
Change::EncryptionSchedule(schedule) => {
self.update_encryption_schedule(batch_epoch + 1, *schedule)?
}
});
ChangeState::InProgress(change)
@ -330,12 +331,9 @@ where
pub(super) fn update_encryption_schedule(
&mut self,
epoch: u64,
change: &Change<N>,
encryption_schedule: EncryptionSchedule,
) -> Result<Step<C, N>> {
self.restart_honey_badger(epoch);
if let Change::EncryptionSchedule(schedule) = change {
self.honey_badger.encryption_schedule = *schedule;
}
self.restart_honey_badger(epoch, Some(encryption_schedule));
Ok(Step::default())
}
@ -358,7 +356,7 @@ where
} {
info!("{:?} No-op change: {:?}", self.our_id(), change);
}
self.restart_honey_badger(epoch);
self.restart_honey_badger(epoch, None);
// TODO: This needs to be the same as `num_faulty` will be in the _new_
// `NetworkInfo` if the change goes through. It would be safer to deduplicate.
let threshold = (pub_keys.len() - 1) / 3;
@ -374,7 +372,11 @@ where
}
/// Starts a new `HoneyBadger` instance and resets the vote counter.
fn restart_honey_badger(&mut self, epoch: u64) {
fn restart_honey_badger(
&mut self,
epoch: u64,
encryption_schedule: Option<EncryptionSchedule>,
) {
self.start_epoch = epoch;
self.key_gen_msg_buffer.retain(|kg_msg| kg_msg.0 >= epoch);
let netinfo = Arc::new(self.netinfo.clone());
@ -382,8 +384,11 @@ where
self.honey_badger = HoneyBadger::builder(netinfo)
.max_future_epochs(self.max_future_epochs)
.rng(self.rng.sub_rng())
.encryption_schedule(self.honey_badger.encryption_schedule)
.build();
.encryption_schedule(if let Some(schedule) = encryption_schedule {
schedule
} else {
self.honey_badger.encryption_schedule
}).build();
}
/// Handles a `Part` message that was output by Honey Badger.

View File

@ -150,7 +150,7 @@ struct KeyGenState<N> {
key_gen: SyncKeyGen<N>,
/// The change for which key generation is performed.
change: NodeChange<N>,
/// The number of key generation messages received from the candidate. At most _N² + 1_ are
/// The number of key generation messages received from the candidate. At most _N + 1_ are
/// accepted.
msg_count: BTreeMap<N, usize>,
}

View File

@ -1,3 +1,5 @@
#![cfg_attr(feature = "cargo-clippy", allow(borrowed_box))]
use std::collections::btree_map::Entry;
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::marker::PhantomData;
@ -6,7 +8,7 @@ use std::sync::Arc;
use bincode;
use crypto::Ciphertext;
use rand::Rand;
use rand::{Rand, Rng};
use serde::{Deserialize, Serialize};
use super::{Batch, ErrorKind, MessageContent, Result, Step};
@ -213,8 +215,21 @@ where
}
/// If the instance hasn't terminated yet, inputs our encrypted contribution.
pub fn propose(&mut self, proposal: Vec<u8>) -> Result<Step<C, N>> {
let cs_step = self.subset.handle_input(proposal)?;
pub fn propose(
&mut self,
proposal: Vec<u8>,
rng: &mut Box<dyn Rng + Send + Sync>,
) -> Result<Step<C, N>> {
let cs_step = self.subset.handle_input(if self.require_decryption {
let ciphertext = self
.netinfo
.public_key_set()
.public_key()
.encrypt_with_rng(rng, proposal);
bincode::serialize(&ciphertext).map_err(|err| ErrorKind::ProposeBincode(*err))?
} else {
proposal
})?;
self.process_subset(cs_step)
}

View File

@ -106,18 +106,16 @@ where
let ser_prop =
bincode::serialize(&proposal).map_err(|err| ErrorKind::ProposeBincode(*err))?;
let epoch = self.epoch;
let require_decryption = self.encryption_schedule.use_on_epoch(epoch);
let prop = if require_decryption {
let ciphertext = self
.netinfo
.public_key_set()
.public_key()
.encrypt_with_rng(&mut self.rng, ser_prop);
bincode::serialize(&ciphertext).map_err(|err| ErrorKind::ProposeBincode(*err))?
} else {
ser_prop
let mut step = {
let epoch_state = {
self.epoch_state_mut(epoch)?;
self.epochs.get_mut(&epoch).expect(
"We created the epoch_state in `self.epoch_state_mut(...)` just a moment ago.",
)
};
let rng = &mut self.rng;
epoch_state.propose(ser_prop, rng)?
};
let mut step = self.epoch_state_mut(epoch)?.propose(prop)?;
step.extend(self.try_output_batches()?);
Ok(step)
}