2018-10-10 06:05:04 -07:00
|
|
|
use std::collections::BTreeMap;
|
2018-08-02 11:11:32 -07:00
|
|
|
use std::sync::Arc;
|
2018-11-01 03:22:40 -07:00
|
|
|
use std::{fmt, mem, result};
|
2018-08-02 11:11:32 -07:00
|
|
|
|
|
|
|
use bincode;
|
|
|
|
use crypto::Signature;
|
2018-10-29 07:36:56 -07:00
|
|
|
use derivative::Derivative;
|
2018-11-01 03:22:40 -07:00
|
|
|
use log::{debug, warn};
|
2018-10-10 06:05:04 -07:00
|
|
|
use rand::{self, Rand};
|
2018-10-24 02:38:14 -07:00
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
2018-08-02 11:11:32 -07:00
|
|
|
|
|
|
|
use super::votes::{SignedVote, VoteCounter};
|
|
|
|
use super::{
|
2018-10-23 22:18:18 -07:00
|
|
|
Batch, Change, ChangeState, DynamicHoneyBadgerBuilder, Error, ErrorKind, Input,
|
|
|
|
InternalContrib, KeyGenMessage, KeyGenState, Message, NodeChange, Result, SignedKeyGenMsg,
|
|
|
|
Step,
|
2018-08-02 11:11:32 -07:00
|
|
|
};
|
|
|
|
use fault_log::{Fault, FaultKind, FaultLog};
|
|
|
|
use honey_badger::{self, HoneyBadger, Message as HbMessage};
|
2018-10-25 11:41:56 -07:00
|
|
|
|
2018-10-24 12:23:54 -07:00
|
|
|
use sync_key_gen::{Ack, AckOutcome, Part, PartOutcome, SyncKeyGen};
|
2018-10-24 13:46:44 -07:00
|
|
|
use threshold_decryption::EncryptionSchedule;
|
2018-10-24 04:01:52 -07:00
|
|
|
use util::{self, SubRng};
|
2018-10-10 07:11:27 -07:00
|
|
|
use {Contribution, DistAlgorithm, NetworkInfo, NodeIdT, Target};
|
2018-08-02 11:11:32 -07:00
|
|
|
|
|
|
|
/// A Honey Badger instance that can handle adding and removing nodes.
|
2018-10-24 04:01:52 -07:00
|
|
|
#[derive(Derivative)]
|
|
|
|
#[derivative(Debug)]
|
2018-08-02 14:27:55 -07:00
|
|
|
pub struct DynamicHoneyBadger<C, N: Rand> {
|
2018-08-02 11:11:32 -07:00
|
|
|
/// Shared network data.
|
2018-08-02 14:27:55 -07:00
|
|
|
pub(super) netinfo: NetworkInfo<N>,
|
2018-08-02 11:11:32 -07:00
|
|
|
/// The maximum number of future epochs for which we handle messages simultaneously.
|
|
|
|
pub(super) max_future_epochs: usize,
|
|
|
|
/// The first epoch after the latest node change.
|
|
|
|
pub(super) start_epoch: u64,
|
|
|
|
/// The buffer and counter for the pending and committed change votes.
|
2018-08-02 14:27:55 -07:00
|
|
|
pub(super) vote_counter: VoteCounter<N>,
|
2018-08-02 11:11:32 -07:00
|
|
|
/// Pending node transactions that we will propose in the next epoch.
|
2018-08-02 14:27:55 -07:00
|
|
|
pub(super) key_gen_msg_buffer: Vec<SignedKeyGenMsg<N>>,
|
2018-08-02 11:11:32 -07:00
|
|
|
/// The `HoneyBadger` instance with the current set of nodes.
|
2018-08-02 14:27:55 -07:00
|
|
|
pub(super) honey_badger: HoneyBadger<InternalContrib<C, N>, N>,
|
2018-08-02 11:11:32 -07:00
|
|
|
/// The current key generation process, and the change it applies to.
|
2018-08-02 14:27:55 -07:00
|
|
|
pub(super) key_gen_state: Option<KeyGenState<N>>,
|
2018-08-02 11:11:32 -07:00
|
|
|
/// A queue for messages from future epochs that cannot be handled yet.
|
2018-08-02 14:27:55 -07:00
|
|
|
pub(super) incoming_queue: Vec<(N, Message<N>)>,
|
2018-10-02 07:24:51 -07:00
|
|
|
/// A random number generator used for secret key generation.
|
|
|
|
// Boxed to avoid overloading the algorithm's type with more generics.
|
2018-10-24 04:01:52 -07:00
|
|
|
#[derivative(Debug(format_with = "util::fmt_rng"))]
|
2018-10-04 10:08:52 -07:00
|
|
|
pub(super) rng: Box<dyn rand::Rng + Send + Sync>,
|
2018-10-02 07:24:51 -07:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:27:55 -07:00
|
|
|
impl<C, N> DistAlgorithm for DynamicHoneyBadger<C, N>
|
2018-08-02 11:11:32 -07:00
|
|
|
where
|
2018-10-24 02:38:14 -07:00
|
|
|
C: Contribution + Serialize + DeserializeOwned,
|
|
|
|
N: NodeIdT + Serialize + DeserializeOwned + Rand,
|
2018-08-02 11:11:32 -07:00
|
|
|
{
|
2018-08-29 09:08:35 -07:00
|
|
|
type NodeId = N;
|
2018-08-02 14:27:55 -07:00
|
|
|
type Input = Input<C, N>;
|
|
|
|
type Output = Batch<C, N>;
|
|
|
|
type Message = Message<N>;
|
2018-08-02 11:11:32 -07:00
|
|
|
type Error = Error;
|
|
|
|
|
2018-08-29 08:28:02 -07:00
|
|
|
fn handle_input(&mut self, input: Self::Input) -> Result<Step<C, N>> {
|
2018-08-02 11:11:32 -07:00
|
|
|
// User contributions are forwarded to `HoneyBadger` right away. Votes are signed and
|
|
|
|
// broadcast.
|
|
|
|
match input {
|
|
|
|
Input::User(contrib) => self.propose(contrib),
|
|
|
|
Input::Change(change) => self.vote_for(change),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:27:55 -07:00
|
|
|
fn handle_message(&mut self, sender_id: &N, message: Self::Message) -> Result<Step<C, N>> {
|
2018-10-23 06:38:42 -07:00
|
|
|
self.handle_message(sender_id, message)
|
2018-08-02 11:11:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn terminated(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:27:55 -07:00
|
|
|
fn our_id(&self) -> &N {
|
2018-08-29 09:08:35 -07:00
|
|
|
self.netinfo.our_id()
|
2018-08-02 11:11:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:27:55 -07:00
|
|
|
impl<C, N> DynamicHoneyBadger<C, N>
|
2018-08-02 11:11:32 -07:00
|
|
|
where
|
2018-10-24 02:38:14 -07:00
|
|
|
C: Contribution + Serialize + DeserializeOwned,
|
|
|
|
N: NodeIdT + Serialize + DeserializeOwned + Rand,
|
2018-08-02 11:11:32 -07:00
|
|
|
{
|
|
|
|
/// Returns a new `DynamicHoneyBadgerBuilder`.
|
2018-08-02 14:27:55 -07:00
|
|
|
pub fn builder() -> DynamicHoneyBadgerBuilder<C, N> {
|
2018-08-02 11:11:32 -07:00
|
|
|
DynamicHoneyBadgerBuilder::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if input for the current epoch has already been provided.
|
|
|
|
pub fn has_input(&self) -> bool {
|
|
|
|
self.honey_badger.has_input()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Proposes a contribution in the current epoch.
|
2018-10-24 03:26:43 -07:00
|
|
|
///
|
|
|
|
/// 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.
|
2018-08-02 14:27:55 -07:00
|
|
|
pub fn propose(&mut self, contrib: C) -> Result<Step<C, N>> {
|
2018-10-22 07:09:00 -07:00
|
|
|
let key_gen_messages = self
|
|
|
|
.key_gen_msg_buffer
|
|
|
|
.iter()
|
|
|
|
.filter(|kg_msg| kg_msg.epoch() == self.start_epoch)
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
2018-08-02 11:11:32 -07:00
|
|
|
let step = self
|
|
|
|
.honey_badger
|
2018-08-29 08:28:02 -07:00
|
|
|
.handle_input(InternalContrib {
|
2018-08-02 11:11:32 -07:00
|
|
|
contrib,
|
2018-10-22 07:09:00 -07:00
|
|
|
key_gen_messages,
|
2018-08-02 11:11:32 -07:00
|
|
|
votes: self.vote_counter.pending_votes().cloned().collect(),
|
2018-10-23 22:23:09 -07:00
|
|
|
}).map_err(ErrorKind::ProposeHoneyBadger)?;
|
2018-08-02 11:11:32 -07:00
|
|
|
self.process_output(step)
|
|
|
|
}
|
|
|
|
|
2018-10-23 06:38:42 -07:00
|
|
|
/// Casts a vote to change the set of validators.
|
2018-10-24 03:26:43 -07:00
|
|
|
///
|
|
|
|
/// This stores a pending vote for the change. It will be included in some future batch, and
|
|
|
|
/// once enough validators have been voted for the same change, it will take effect.
|
2018-08-02 14:27:55 -07:00
|
|
|
pub fn vote_for(&mut self, change: Change<N>) -> Result<Step<C, N>> {
|
2018-08-02 11:11:32 -07:00
|
|
|
if !self.netinfo.is_validator() {
|
|
|
|
return Ok(Step::default()); // TODO: Return an error?
|
|
|
|
}
|
|
|
|
let signed_vote = self.vote_counter.sign_vote_for(change)?.clone();
|
|
|
|
let msg = Message::SignedVote(signed_vote);
|
|
|
|
Ok(Target::All.message(msg).into())
|
|
|
|
}
|
|
|
|
|
2018-10-24 03:26:43 -07:00
|
|
|
/// Handles a message received from `sender_id`.
|
|
|
|
///
|
|
|
|
/// This must be called with every message we receive from another node.
|
2018-10-23 06:38:42 -07:00
|
|
|
pub fn handle_message(&mut self, sender_id: &N, message: Message<N>) -> Result<Step<C, N>> {
|
|
|
|
let epoch = message.start_epoch();
|
|
|
|
if epoch < self.start_epoch {
|
|
|
|
// Obsolete message.
|
|
|
|
Ok(Step::default())
|
|
|
|
} else if epoch > self.start_epoch {
|
|
|
|
// Message cannot be handled yet. Save it for later.
|
|
|
|
let entry = (sender_id.clone(), message);
|
|
|
|
self.incoming_queue.push(entry);
|
|
|
|
Ok(Step::default())
|
|
|
|
} else {
|
|
|
|
match message {
|
|
|
|
Message::HoneyBadger(_, hb_msg) => {
|
|
|
|
self.handle_honey_badger_message(sender_id, hb_msg)
|
|
|
|
}
|
|
|
|
Message::KeyGen(_, kg_msg, sig) => self
|
|
|
|
.handle_key_gen_message(sender_id, kg_msg, *sig)
|
|
|
|
.map(FaultLog::into),
|
|
|
|
Message::SignedVote(signed_vote) => self
|
|
|
|
.vote_counter
|
|
|
|
.add_pending_vote(sender_id, signed_vote)
|
|
|
|
.map(FaultLog::into),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 11:11:32 -07:00
|
|
|
/// Returns the information about the node IDs in the network, and the cryptographic keys.
|
2018-08-02 14:27:55 -07:00
|
|
|
pub fn netinfo(&self) -> &NetworkInfo<N> {
|
2018-08-02 11:11:32 -07:00
|
|
|
&self.netinfo
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if we should make our contribution for the next epoch, even if we don't have
|
|
|
|
/// content ourselves, to avoid stalling the network.
|
|
|
|
///
|
|
|
|
/// By proposing only if this returns `true`, you can prevent an adversary from making the
|
|
|
|
/// network output empty baches indefinitely, but it also means that the network won't advance
|
|
|
|
/// if fewer than _f + 1_ nodes have pending contributions.
|
|
|
|
pub fn should_propose(&self) -> bool {
|
|
|
|
if self.has_input() {
|
|
|
|
return false; // We have already proposed.
|
|
|
|
}
|
|
|
|
if self.honey_badger.received_proposals() > self.netinfo.num_faulty() {
|
|
|
|
return true; // At least one correct node wants to move on to the next epoch.
|
|
|
|
}
|
|
|
|
let is_our_vote = |signed_vote: &SignedVote<_>| signed_vote.voter() == self.our_id();
|
|
|
|
if self.vote_counter.pending_votes().any(is_our_vote) {
|
|
|
|
return true; // We have pending input to vote for a validator change.
|
|
|
|
}
|
|
|
|
let kgs = match self.key_gen_state {
|
|
|
|
None => return false, // No ongoing key generation.
|
|
|
|
Some(ref kgs) => kgs,
|
|
|
|
};
|
|
|
|
// If either we or the candidate have a pending key gen message, we should propose.
|
|
|
|
let ours_or_candidates = |msg: &SignedKeyGenMsg<_>| {
|
|
|
|
msg.1 == *self.our_id() || Some(&msg.1) == kgs.change.candidate()
|
|
|
|
};
|
|
|
|
self.key_gen_msg_buffer.iter().any(ours_or_candidates)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles a message for the `HoneyBadger` instance.
|
|
|
|
fn handle_honey_badger_message(
|
|
|
|
&mut self,
|
2018-08-02 14:27:55 -07:00
|
|
|
sender_id: &N,
|
|
|
|
message: HbMessage<N>,
|
|
|
|
) -> Result<Step<C, N>> {
|
2018-10-09 07:07:56 -07:00
|
|
|
if !self.netinfo.is_node_validator(sender_id) {
|
|
|
|
return Err(ErrorKind::UnknownSender.into());
|
|
|
|
}
|
2018-08-02 11:11:32 -07:00
|
|
|
// Handle the message.
|
|
|
|
let step = self
|
|
|
|
.honey_badger
|
|
|
|
.handle_message(sender_id, message)
|
|
|
|
.map_err(ErrorKind::HandleHoneyBadgerMessageHoneyBadger)?;
|
|
|
|
self.process_output(step)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles a vote or key generation message and tries to commit it as a transaction. These
|
|
|
|
/// messages are only handled once they appear in a batch output from Honey Badger.
|
|
|
|
fn handle_key_gen_message(
|
|
|
|
&mut self,
|
2018-08-02 14:27:55 -07:00
|
|
|
sender_id: &N,
|
2018-08-02 11:11:32 -07:00
|
|
|
kg_msg: KeyGenMessage,
|
|
|
|
sig: Signature,
|
2018-08-02 14:27:55 -07:00
|
|
|
) -> Result<FaultLog<N>> {
|
2018-08-02 11:11:32 -07:00
|
|
|
if !self.verify_signature(sender_id, &sig, &kg_msg)? {
|
|
|
|
let fault_kind = FaultKind::InvalidKeyGenMessageSignature;
|
|
|
|
return Ok(Fault::new(sender_id.clone(), fault_kind).into());
|
|
|
|
}
|
|
|
|
let kgs = match self.key_gen_state {
|
|
|
|
Some(ref mut kgs) => kgs,
|
|
|
|
None => {
|
|
|
|
return Ok(Fault::new(sender_id.clone(), FaultKind::UnexpectedKeyGenMessage).into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-23 02:13:55 -07:00
|
|
|
// If the sender is correct, it will send at most _N + 1_ key generation messages:
|
|
|
|
// one `Part`, and for each validator an `Ack`. _N_ is the node number _after_ the change.
|
|
|
|
if kgs.count_messages(sender_id) > kgs.key_gen.num_nodes() + 1 {
|
|
|
|
let fault_kind = FaultKind::TooManyKeyGenMessages;
|
|
|
|
return Ok(Fault::new(sender_id.clone(), fault_kind).into());
|
2018-08-02 11:11:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let tx = SignedKeyGenMsg(self.start_epoch, sender_id.clone(), kg_msg, sig);
|
|
|
|
self.key_gen_msg_buffer.push(tx);
|
|
|
|
Ok(FaultLog::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Processes all pending batches output by Honey Badger.
|
2018-10-09 07:07:56 -07:00
|
|
|
fn process_output(
|
2018-08-02 11:11:32 -07:00
|
|
|
&mut self,
|
2018-08-02 14:27:55 -07:00
|
|
|
hb_step: honey_badger::Step<InternalContrib<C, N>, N>,
|
|
|
|
) -> Result<Step<C, N>> {
|
|
|
|
let mut step: Step<C, N> = Step::default();
|
2018-08-02 11:11:32 -07:00
|
|
|
let start_epoch = self.start_epoch;
|
|
|
|
let output = step.extend_with(hb_step, |hb_msg| Message::HoneyBadger(start_epoch, hb_msg));
|
|
|
|
for hb_batch in output {
|
2018-10-10 06:05:04 -07:00
|
|
|
let batch_epoch = hb_batch.epoch + self.start_epoch;
|
|
|
|
let mut batch_contributions = BTreeMap::new();
|
2018-08-02 11:11:32 -07:00
|
|
|
|
|
|
|
// Add the user transactions to `batch` and handle votes and DKG messages.
|
|
|
|
for (id, int_contrib) in hb_batch.contributions {
|
|
|
|
let InternalContrib {
|
|
|
|
votes,
|
|
|
|
key_gen_messages,
|
|
|
|
contrib,
|
|
|
|
} = int_contrib;
|
|
|
|
step.fault_log
|
|
|
|
.extend(self.vote_counter.add_committed_votes(&id, votes)?);
|
2018-10-10 06:05:04 -07:00
|
|
|
batch_contributions.insert(id.clone(), contrib);
|
2018-08-02 11:11:32 -07:00
|
|
|
self.key_gen_msg_buffer
|
|
|
|
.retain(|skgm| !key_gen_messages.contains(skgm));
|
|
|
|
for SignedKeyGenMsg(epoch, s_id, kg_msg, sig) in key_gen_messages {
|
2018-10-22 07:09:00 -07:00
|
|
|
if epoch != self.start_epoch {
|
|
|
|
let fault_kind = FaultKind::InvalidKeyGenMessageEpoch;
|
|
|
|
step.fault_log.append(id.clone(), fault_kind);
|
|
|
|
} else if !self.verify_signature(&s_id, &sig, &kg_msg)? {
|
2018-08-02 11:11:32 -07:00
|
|
|
let fault_kind = FaultKind::InvalidKeyGenMessageSignature;
|
|
|
|
step.fault_log.append(id.clone(), fault_kind);
|
2018-10-22 07:09:00 -07:00
|
|
|
} else {
|
|
|
|
step.extend(match kg_msg {
|
|
|
|
KeyGenMessage::Part(part) => self.handle_part(&s_id, part)?,
|
2018-10-24 12:23:54 -07:00
|
|
|
KeyGenMessage::Ack(ack) => self.handle_ack(&s_id, ack)?,
|
2018-10-22 07:09:00 -07:00
|
|
|
});
|
2018-08-02 11:11:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 06:05:04 -07:00
|
|
|
let change = if let Some(kgs) = self.take_ready_key_gen() {
|
2018-08-02 11:11:32 -07:00
|
|
|
// If DKG completed, apply the change, restart Honey Badger, and inform the user.
|
2018-11-01 03:22:40 -07:00
|
|
|
debug!("{}: DKG for {:?} complete!", self, kgs.change);
|
2018-08-06 07:53:37 -07:00
|
|
|
self.netinfo = kgs.key_gen.into_network_info()?;
|
2018-10-24 13:46:44 -07:00
|
|
|
self.restart_honey_badger(batch_epoch + 1, None);
|
2018-10-23 21:21:59 -07:00
|
|
|
ChangeState::Complete(Change::NodeChange(kgs.change))
|
2018-08-02 11:11:32 -07:00
|
|
|
} 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.
|
2018-10-23 21:21:59 -07:00
|
|
|
step.extend(match &change {
|
|
|
|
Change::NodeChange(change) => self.update_key_gen(batch_epoch + 1, &change)?,
|
2018-10-24 13:46:44 -07:00
|
|
|
Change::EncryptionSchedule(schedule) => {
|
|
|
|
self.update_encryption_schedule(batch_epoch + 1, *schedule)?
|
2018-10-23 22:18:18 -07:00
|
|
|
}
|
2018-10-17 10:27:00 -07:00
|
|
|
});
|
2018-10-27 15:44:08 -07:00
|
|
|
match change {
|
|
|
|
Change::NodeChange(_) => ChangeState::InProgress(change),
|
|
|
|
Change::EncryptionSchedule(_) => ChangeState::Complete(change),
|
|
|
|
}
|
2018-10-10 06:05:04 -07:00
|
|
|
} else {
|
|
|
|
ChangeState::None
|
|
|
|
};
|
2018-10-25 05:44:28 -07:00
|
|
|
step.output.push(Batch {
|
2018-10-10 06:05:04 -07:00
|
|
|
epoch: batch_epoch,
|
|
|
|
change,
|
|
|
|
netinfo: Arc::new(self.netinfo.clone()),
|
|
|
|
contributions: batch_contributions,
|
2018-10-31 17:44:22 -07:00
|
|
|
encryption_schedule: self.honey_badger.get_encryption_schedule(),
|
2018-10-10 06:05:04 -07:00
|
|
|
});
|
2018-08-02 11:11:32 -07:00
|
|
|
}
|
|
|
|
// If `start_epoch` changed, we can now handle some queued messages.
|
|
|
|
if start_epoch < self.start_epoch {
|
|
|
|
let queue = mem::replace(&mut self.incoming_queue, Vec::new());
|
|
|
|
for (sender_id, msg) in queue {
|
|
|
|
step.extend(self.handle_message(&sender_id, msg)?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(step)
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:18:18 -07:00
|
|
|
pub(super) fn update_encryption_schedule(
|
|
|
|
&mut self,
|
|
|
|
epoch: u64,
|
2018-10-24 13:46:44 -07:00
|
|
|
encryption_schedule: EncryptionSchedule,
|
2018-10-23 22:18:18 -07:00
|
|
|
) -> Result<Step<C, N>> {
|
2018-10-24 13:46:44 -07:00
|
|
|
self.restart_honey_badger(epoch, Some(encryption_schedule));
|
2018-10-23 22:18:18 -07:00
|
|
|
Ok(Step::default())
|
2018-10-21 13:47:44 -07:00
|
|
|
}
|
|
|
|
|
2018-08-02 11:11:32 -07:00
|
|
|
/// If the winner of the vote has changed, restarts Key Generation for the set of nodes implied
|
|
|
|
/// by the current change.
|
2018-10-23 22:18:18 -07:00
|
|
|
pub(super) fn update_key_gen(
|
|
|
|
&mut self,
|
|
|
|
epoch: u64,
|
|
|
|
change: &NodeChange<N>,
|
|
|
|
) -> Result<Step<C, N>> {
|
2018-08-02 11:11:32 -07:00
|
|
|
if self.key_gen_state.as_ref().map(|kgs| &kgs.change) == Some(change) {
|
|
|
|
return Ok(Step::default()); // The change is the same as before. Continue DKG as is.
|
|
|
|
}
|
2018-11-01 03:22:40 -07:00
|
|
|
debug!("{}: Restarting DKG for {:?}.", self, change);
|
2018-08-02 11:11:32 -07:00
|
|
|
// Use the existing key shares - with the change applied - as keys for DKG.
|
|
|
|
let mut pub_keys = self.netinfo.public_key_map().clone();
|
|
|
|
if match *change {
|
2018-10-23 21:21:59 -07:00
|
|
|
NodeChange::Remove(ref id) => pub_keys.remove(id).is_none(),
|
|
|
|
NodeChange::Add(ref id, ref pk) => pub_keys.insert(id.clone(), pk.clone()).is_some(),
|
2018-08-02 11:11:32 -07:00
|
|
|
} {
|
2018-11-01 03:22:40 -07:00
|
|
|
warn!("{}: No-op change: {:?}", self, change);
|
2018-08-02 11:11:32 -07:00
|
|
|
}
|
2018-10-24 13:46:44 -07:00
|
|
|
self.restart_honey_badger(epoch, None);
|
2018-08-02 11:11:32 -07:00
|
|
|
// 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;
|
|
|
|
let sk = self.netinfo.secret_key().clone();
|
2018-08-29 09:08:35 -07:00
|
|
|
let our_id = self.our_id().clone();
|
2018-10-02 07:24:51 -07:00
|
|
|
let (key_gen, part) = SyncKeyGen::new(&mut self.rng, our_id, sk, pub_keys, threshold)?;
|
2018-08-02 11:11:32 -07:00
|
|
|
self.key_gen_state = Some(KeyGenState::new(key_gen, change.clone()));
|
|
|
|
if let Some(part) = part {
|
2018-10-09 07:07:56 -07:00
|
|
|
self.send_transaction(KeyGenMessage::Part(part))
|
|
|
|
} else {
|
|
|
|
Ok(Step::default())
|
2018-08-02 11:11:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts a new `HoneyBadger` instance and resets the vote counter.
|
2018-10-24 13:46:44 -07:00
|
|
|
fn restart_honey_badger(
|
|
|
|
&mut self,
|
|
|
|
epoch: u64,
|
|
|
|
encryption_schedule: Option<EncryptionSchedule>,
|
|
|
|
) {
|
2018-08-02 11:11:32 -07:00
|
|
|
self.start_epoch = epoch;
|
|
|
|
self.key_gen_msg_buffer.retain(|kg_msg| kg_msg.0 >= epoch);
|
|
|
|
let netinfo = Arc::new(self.netinfo.clone());
|
2018-10-22 07:09:00 -07:00
|
|
|
self.vote_counter = VoteCounter::new(netinfo.clone(), epoch);
|
2018-10-09 07:07:56 -07:00
|
|
|
self.honey_badger = HoneyBadger::builder(netinfo)
|
2018-10-24 00:42:59 -07:00
|
|
|
.session_id(epoch)
|
2018-08-02 11:11:32 -07:00
|
|
|
.max_future_epochs(self.max_future_epochs)
|
2018-10-02 07:24:51 -07:00
|
|
|
.rng(self.rng.sub_rng())
|
2018-10-31 17:44:22 -07:00
|
|
|
.encryption_schedule(
|
|
|
|
encryption_schedule.unwrap_or_else(|| self.honey_badger.get_encryption_schedule()),
|
|
|
|
).build();
|
2018-08-02 11:11:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles a `Part` message that was output by Honey Badger.
|
2018-08-02 14:27:55 -07:00
|
|
|
fn handle_part(&mut self, sender_id: &N, part: Part) -> Result<Step<C, N>> {
|
2018-10-03 04:21:59 -07:00
|
|
|
let outcome = if let Some(kgs) = self.key_gen_state.as_mut() {
|
2018-10-24 12:23:54 -07:00
|
|
|
kgs.key_gen
|
|
|
|
.handle_part(&mut self.rng, &sender_id, part)
|
|
|
|
.map_err(ErrorKind::SyncKeyGen)?
|
2018-10-03 04:21:59 -07:00
|
|
|
} else {
|
2018-10-22 07:09:00 -07:00
|
|
|
// No key generation ongoing.
|
|
|
|
let fault_kind = FaultKind::UnexpectedKeyGenPart;
|
|
|
|
return Ok(Fault::new(sender_id.clone(), fault_kind).into());
|
2018-10-02 07:24:51 -07:00
|
|
|
};
|
|
|
|
|
2018-10-03 04:21:59 -07:00
|
|
|
match outcome {
|
2018-10-24 12:23:54 -07:00
|
|
|
PartOutcome::Valid(Some(ack)) => self.send_transaction(KeyGenMessage::Ack(ack)),
|
|
|
|
PartOutcome::Valid(None) => Ok(Step::default()),
|
|
|
|
PartOutcome::Invalid(fault) => {
|
|
|
|
let fault_kind = FaultKind::SyncKeyGenPart(fault);
|
|
|
|
Ok(Fault::new(sender_id.clone(), fault_kind).into())
|
|
|
|
}
|
2018-08-02 11:11:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles an `Ack` message that was output by Honey Badger.
|
2018-10-24 12:23:54 -07:00
|
|
|
fn handle_ack(&mut self, sender_id: &N, ack: Ack) -> Result<Step<C, N>> {
|
|
|
|
let outcome = if let Some(kgs) = self.key_gen_state.as_mut() {
|
|
|
|
kgs.key_gen
|
|
|
|
.handle_ack(sender_id, ack)
|
|
|
|
.map_err(ErrorKind::SyncKeyGen)?
|
2018-08-02 11:11:32 -07:00
|
|
|
} else {
|
2018-10-22 07:09:00 -07:00
|
|
|
// No key generation ongoing.
|
|
|
|
let fault_kind = FaultKind::UnexpectedKeyGenAck;
|
|
|
|
return Ok(Fault::new(sender_id.clone(), fault_kind).into());
|
2018-10-24 12:23:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
match outcome {
|
|
|
|
AckOutcome::Valid => Ok(Step::default()),
|
|
|
|
AckOutcome::Invalid(fault) => {
|
|
|
|
let fault_kind = FaultKind::SyncKeyGenAck(fault);
|
|
|
|
Ok(Fault::new(sender_id.clone(), fault_kind).into())
|
|
|
|
}
|
2018-08-02 11:11:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Signs and sends a `KeyGenMessage` and also tries to commit it.
|
2018-08-02 14:27:55 -07:00
|
|
|
fn send_transaction(&mut self, kg_msg: KeyGenMessage) -> Result<Step<C, N>> {
|
2018-08-02 11:11:32 -07:00
|
|
|
let ser =
|
|
|
|
bincode::serialize(&kg_msg).map_err(|err| ErrorKind::SendTransactionBincode(*err))?;
|
|
|
|
let sig = Box::new(self.netinfo.secret_key().sign(ser));
|
|
|
|
if self.netinfo.is_validator() {
|
2018-10-25 05:44:28 -07:00
|
|
|
let our_id = self.our_id().clone();
|
2018-08-02 11:11:32 -07:00
|
|
|
let signed_msg =
|
2018-08-29 09:08:35 -07:00
|
|
|
SignedKeyGenMsg(self.start_epoch, our_id, kg_msg.clone(), *sig.clone());
|
2018-08-02 11:11:32 -07:00
|
|
|
self.key_gen_msg_buffer.push(signed_msg);
|
|
|
|
}
|
|
|
|
let msg = Message::KeyGen(self.start_epoch, kg_msg, sig);
|
|
|
|
Ok(Target::All.message(msg).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the current Key Generation process is ready, returns the `KeyGenState`.
|
|
|
|
///
|
|
|
|
/// We require the minimum number of completed proposals (`SyncKeyGen::is_ready`) and if a new
|
|
|
|
/// node is joining, we require in addition that the new node's proposal is complete. That way
|
|
|
|
/// the new node knows that it's key is secret, without having to trust any number of nodes.
|
2018-08-02 14:27:55 -07:00
|
|
|
fn take_ready_key_gen(&mut self) -> Option<KeyGenState<N>> {
|
2018-08-02 11:11:32 -07:00
|
|
|
if self
|
|
|
|
.key_gen_state
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, KeyGenState::is_ready)
|
|
|
|
{
|
|
|
|
self.key_gen_state.take()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the signature of `kg_msg` by the node with the specified ID is valid.
|
|
|
|
/// Returns an error if the payload fails to serialize.
|
|
|
|
///
|
|
|
|
/// This accepts signatures from both validators and the currently joining candidate, if any.
|
|
|
|
fn verify_signature(
|
|
|
|
&self,
|
2018-08-02 14:27:55 -07:00
|
|
|
node_id: &N,
|
2018-08-02 11:11:32 -07:00
|
|
|
sig: &Signature,
|
|
|
|
kg_msg: &KeyGenMessage,
|
|
|
|
) -> Result<bool> {
|
|
|
|
let ser =
|
|
|
|
bincode::serialize(kg_msg).map_err(|err| ErrorKind::VerifySignatureBincode(*err))?;
|
|
|
|
let get_candidate_key = || {
|
|
|
|
self.key_gen_state
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|kgs| kgs.candidate_key(node_id))
|
|
|
|
};
|
|
|
|
let pk_opt = self.netinfo.public_key(node_id).or_else(get_candidate_key);
|
|
|
|
Ok(pk_opt.map_or(false, |pk| pk.verify(&sig, ser)))
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 03:22:40 -07:00
|
|
|
|
|
|
|
impl<C, N> fmt::Display for DynamicHoneyBadger<C, N>
|
|
|
|
where
|
|
|
|
C: Contribution + Serialize + DeserializeOwned,
|
|
|
|
N: NodeIdT + Serialize + DeserializeOwned + Rand,
|
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
|
|
|
|
write!(f, "{:?} DHB(era: {})", self.our_id(), self.start_epoch)
|
|
|
|
}
|
|
|
|
}
|