From 66f396e01c8347bade2c237138a1012c2f782155 Mon Sep 17 00:00:00 2001 From: Andreas Fackler Date: Mon, 18 Jun 2018 16:37:07 +0200 Subject: [PATCH] Remove more NodeUid trait bounds. Use a `BTreeMap` instead of `HashMap` in `NetworkInfo`. The number of nodes can't grow very large anyway. --- src/agreement/mod.rs | 10 +++------- src/broadcast.rs | 7 +++---- src/common_coin.rs | 10 +++------- src/common_subset.rs | 9 ++++----- src/honey_badger.rs | 8 ++++---- src/messaging.rs | 9 ++++----- 6 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/agreement/mod.rs b/src/agreement/mod.rs index 24ccd8d..ba03678 100644 --- a/src/agreement/mod.rs +++ b/src/agreement/mod.rs @@ -4,7 +4,6 @@ pub mod bin_values; use std::collections::{BTreeMap, BTreeSet, VecDeque}; use std::fmt::Debug; -use std::hash::Hash; use std::mem::replace; use std::rc::Rc; @@ -68,10 +67,7 @@ pub struct AgreementMessage { } /// Binary Agreement instance -pub struct Agreement -where - NodeUid: Eq + Hash, -{ +pub struct Agreement { /// Shared network information. netinfo: Rc>, /// Session ID, e.g, the Honey Badger algorithm epoch. @@ -121,7 +117,7 @@ where common_coin: CommonCoin, } -impl DistAlgorithm for Agreement { +impl DistAlgorithm for Agreement { type NodeUid = NodeUid; type Input = bool; type Output = bool; @@ -177,7 +173,7 @@ impl DistAlgorithm for Agreement Agreement { +impl Agreement { pub fn new( netinfo: Rc>, session_id: u64, diff --git a/src/broadcast.rs b/src/broadcast.rs index 80d152f..bed7e00 100644 --- a/src/broadcast.rs +++ b/src/broadcast.rs @@ -1,6 +1,5 @@ use std::collections::{BTreeMap, VecDeque}; use std::fmt::{self, Debug}; -use std::hash::Hash; use std::iter::once; use std::rc::Rc; @@ -92,7 +91,7 @@ impl Debug for BroadcastMessage { /// eventually be able to decode (i.e. receive at least `f + 1` `Echo` messages). /// * So a node with `2 * f + 1` `Ready`s and `f + 1` `Echos` will decode and _output_ the value, /// knowing that every other good node will eventually do the same. -pub struct Broadcast { +pub struct Broadcast { /// Shared network data. netinfo: Rc>, /// The UID of the sending node. @@ -115,7 +114,7 @@ pub struct Broadcast { output: Option>, } -impl DistAlgorithm for Broadcast { +impl DistAlgorithm for Broadcast { type NodeUid = NodeUid; // TODO: Allow anything serializable and deserializable, i.e. make this a type parameter // T: Serialize + DeserializeOwned @@ -168,7 +167,7 @@ impl DistAlgorithm for Broadcast Broadcast { +impl Broadcast { /// Creates a new broadcast instance to be used by node `our_id` which expects a value proposal /// from node `proposer_id`. pub fn new(netinfo: Rc>, proposer_id: NodeUid) -> BroadcastResult { diff --git a/src/common_coin.rs b/src/common_coin.rs index e59bd78..b857c94 100644 --- a/src/common_coin.rs +++ b/src/common_coin.rs @@ -2,7 +2,6 @@ use std::collections::{BTreeMap, VecDeque}; use std::fmt::Debug; -use std::hash::Hash; use std::rc::Rc; use pairing::bls12_381::Bls12; @@ -44,10 +43,7 @@ impl CommonCoinMessage { /// receiving at least `num_faulty + 1` shares, attempts to combine them into a signature. If that /// signature is valid, the instance outputs it and terminates; otherwise the instance aborts. #[derive(Debug)] -pub struct CommonCoin -where - NodeUid: Eq + Hash, -{ +pub struct CommonCoin { netinfo: Rc>, /// The name of this common coin. It is required to be unique for each common coin round. nonce: T, @@ -65,7 +61,7 @@ where impl DistAlgorithm for CommonCoin where - NodeUid: Clone + Debug + Hash + Ord, + NodeUid: Clone + Debug + Ord, T: Clone + AsRef<[u8]>, { type NodeUid = NodeUid; @@ -117,7 +113,7 @@ where impl CommonCoin where - NodeUid: Clone + Debug + Hash + Ord, + NodeUid: Clone + Debug + Ord, T: Clone + AsRef<[u8]>, { pub fn new(netinfo: Rc>, nonce: T) -> Self { diff --git a/src/common_subset.rs b/src/common_subset.rs index 052499d..18d28cb 100644 --- a/src/common_subset.rs +++ b/src/common_subset.rs @@ -2,7 +2,6 @@ use std::collections::{BTreeMap, BTreeSet, VecDeque}; use std::fmt::Debug; -use std::hash::Hash; use std::rc::Rc; use agreement; @@ -47,7 +46,7 @@ pub enum Message { #[derive(Deref, DerefMut)] struct MessageQueue(VecDeque, NodeUid>>); -impl MessageQueue { +impl MessageQueue { /// Appends to the queue the messages from `agr`, wrapped with `proposer_id`. fn extend_agreement(&mut self, proposer_id: &NodeUid, agr: &mut Agreement) { let convert = |msg: TargetedMessage| { @@ -88,7 +87,7 @@ impl MessageQueue { /// remaining ones, where we haven't provided input yet. /// * Once all `Agreement` instances have decided, `CommonSubset` returns the set of all proposed /// values for which the decision was "yes". -pub struct CommonSubset { +pub struct CommonSubset { /// Shared network information. netinfo: Rc>, broadcast_instances: BTreeMap>, @@ -103,7 +102,7 @@ pub struct CommonSubset { decided: bool, } -impl DistAlgorithm for CommonSubset { +impl DistAlgorithm for CommonSubset { type NodeUid = NodeUid; type Input = ProposedValue; type Output = BTreeMap; @@ -147,7 +146,7 @@ impl DistAlgorithm for CommonSubset CommonSubset { +impl CommonSubset { pub fn new(netinfo: Rc>, session_id: u64) -> CommonSubsetResult { // Create all broadcast instances. let mut broadcast_instances: BTreeMap> = BTreeMap::new(); diff --git a/src/honey_badger.rs b/src/honey_badger.rs index bf4003d..58b1735 100644 --- a/src/honey_badger.rs +++ b/src/honey_badger.rs @@ -32,7 +32,7 @@ error_chain!{ } /// An instance of the Honey Badger Byzantine fault tolerant consensus algorithm. -pub struct HoneyBadger { +pub struct HoneyBadger { /// Shared network data. netinfo: Rc>, /// The buffer of transactions that have not yet been included in any output batch. @@ -55,7 +55,7 @@ pub struct HoneyBadger { impl DistAlgorithm for HoneyBadger where Tx: Eq + Hash + Serialize + DeserializeOwned + Debug, - NodeUid: Eq + Hash + Ord + Clone + Debug, + NodeUid: Ord + Clone + Debug, { type NodeUid = NodeUid; type Input = Tx; @@ -103,7 +103,7 @@ where impl HoneyBadger where Tx: Eq + Hash + Serialize + DeserializeOwned + Debug, - NodeUid: Eq + Hash + Ord + Clone + Debug, + NodeUid: Ord + Clone + Debug, { /// Returns a new Honey Badger instance with the given parameters, starting at epoch `0`. pub fn new( @@ -304,7 +304,7 @@ pub enum Message { #[derive(Deref, DerefMut)] struct MessageQueue(VecDeque, NodeUid>>); -impl MessageQueue { +impl MessageQueue { /// Appends to the queue the messages from `cs`, wrapped with `epoch`. fn extend_with_epoch(&mut self, epoch: u64, cs: &mut CommonSubset) { let convert = |msg: TargetedMessage, NodeUid>| { diff --git a/src/messaging.rs b/src/messaging.rs index bb9b24e..7147a8a 100644 --- a/src/messaging.rs +++ b/src/messaging.rs @@ -1,6 +1,5 @@ -use std::collections::{BTreeSet, HashMap}; +use std::collections::{BTreeMap, BTreeSet}; use std::fmt::Debug; -use std::hash::Hash; use pairing::bls12_381::Bls12; @@ -132,17 +131,17 @@ impl<'a, D: DistAlgorithm + 'a> Iterator for OutputIter<'a, D> { /// Common data shared between algorithms. #[derive(Debug)] -pub struct NetworkInfo { +pub struct NetworkInfo { our_uid: NodeUid, all_uids: BTreeSet, num_nodes: usize, num_faulty: usize, secret_key: SecretKey, public_key_set: PublicKeySet, - node_indices: HashMap, + node_indices: BTreeMap, } -impl NetworkInfo { +impl NetworkInfo { pub fn new( our_uid: NodeUid, all_uids: BTreeSet,