Add a `Params` struct with HB parameters.

This removes some duplication between DHB, HB and their builders.
This commit is contained in:
Andreas Fackler 2018-11-18 10:18:14 +01:00 committed by Andreas Fackler
parent c94e3ff16f
commit 9049dd1793
5 changed files with 74 additions and 70 deletions

View File

@ -11,7 +11,7 @@ use super::{
Change, ChangeState, DynamicHoneyBadger, EncryptionSchedule, JoinPlan, Result, Step,
VoteCounter,
};
use honey_badger::{HoneyBadger, SubsetHandlingStrategy};
use honey_badger::{HoneyBadger, Params, SubsetHandlingStrategy};
use util::SubRng;
use {Contribution, NetworkInfo, NodeIdT};
@ -20,17 +20,11 @@ use {Contribution, NetworkInfo, NodeIdT};
pub struct DynamicHoneyBadgerBuilder<C, N> {
/// Start in this era.
era: u64,
/// The maximum number of future epochs for which we handle messages simultaneously.
max_future_epochs: u64,
/// Random number generator passed on to algorithm instance for key generation. Also used to
/// instantiate `HoneyBadger`.
rng: Box<dyn rand::Rng>,
/// Strategy used to handle the output of the `Subset` algorithm.
subset_handling_strategy: SubsetHandlingStrategy,
/// Whether to generate a pseudorandom value in each epoch.
random_value: bool,
/// Schedule for adding threshold encryption to some percentage of rounds
encryption_schedule: EncryptionSchedule,
/// Parameters controlling Honey Badger's behavior and performance.
params: Params,
_phantom: PhantomData<(C, N)>,
}
@ -42,11 +36,8 @@ where
// TODO: Use the defaults from `HoneyBadgerBuilder`.
DynamicHoneyBadgerBuilder {
era: 0,
max_future_epochs: 3,
rng: Box::new(rand::thread_rng()),
subset_handling_strategy: SubsetHandlingStrategy::Incremental,
random_value: false,
encryption_schedule: EncryptionSchedule::Always,
params: Params::default(),
_phantom: PhantomData,
}
}
@ -71,7 +62,7 @@ where
/// Sets the maximum number of future epochs for which we handle messages simultaneously.
pub fn max_future_epochs(&mut self, max_future_epochs: u64) -> &mut Self {
self.max_future_epochs = max_future_epochs;
self.params.max_future_epochs = max_future_epochs;
self
}
@ -86,19 +77,25 @@ where
&mut self,
subset_handling_strategy: SubsetHandlingStrategy,
) -> &mut Self {
self.subset_handling_strategy = subset_handling_strategy;
self.params.subset_handling_strategy = subset_handling_strategy;
self
}
/// Whether to generate a pseudorandom value in each epoch.
pub fn random_value(&mut self, random_value: bool) -> &mut Self {
self.random_value = random_value;
self.params.random_value = random_value;
self
}
/// Sets the schedule to use for threshold encryption.
pub fn encryption_schedule(&mut self, encryption_schedule: EncryptionSchedule) -> &mut Self {
self.encryption_schedule = encryption_schedule;
self.params.encryption_schedule = encryption_schedule;
self
}
/// Sets the parameters controlling Honey Badger's behavior and performance.
pub fn params(&mut self, params: Params) -> &mut Self {
self.params = params;
self
}
@ -106,28 +103,20 @@ where
pub fn build(&mut self, netinfo: NetworkInfo<N>) -> DynamicHoneyBadger<C, N> {
let DynamicHoneyBadgerBuilder {
era,
max_future_epochs,
rng,
subset_handling_strategy,
random_value,
encryption_schedule,
params,
_phantom,
} = self;
let era = *era;
let max_future_epochs = *max_future_epochs;
let arc_netinfo = Arc::new(netinfo.clone());
let honey_badger = HoneyBadger::builder(arc_netinfo.clone())
.session_id(era)
.max_future_epochs(max_future_epochs)
.session_id(*era)
.params(params.clone())
.rng(rng.sub_rng())
.subset_handling_strategy(subset_handling_strategy.clone())
.random_value(*random_value)
.encryption_schedule(*encryption_schedule)
.build();
DynamicHoneyBadger {
netinfo,
max_future_epochs,
era,
max_future_epochs: params.max_future_epochs,
era: *era,
vote_counter: VoteCounter::new(arc_netinfo, 0),
key_gen_msg_buffer: Vec::new(),
honey_badger,
@ -164,13 +153,13 @@ where
);
let arc_netinfo = Arc::new(netinfo.clone());
let honey_badger = HoneyBadger::builder(arc_netinfo.clone())
.max_future_epochs(self.max_future_epochs)
.max_future_epochs(self.params.max_future_epochs)
.encryption_schedule(join_plan.encryption_schedule)
.random_value(join_plan.random_value)
.build();
let mut dhb = DynamicHoneyBadger {
netinfo,
max_future_epochs: self.max_future_epochs,
max_future_epochs: self.params.max_future_epochs,
era: join_plan.era,
vote_counter: VoteCounter::new(arc_netinfo, join_plan.era),
key_gen_msg_buffer: Vec::new(),

View File

@ -5,8 +5,7 @@ use std::sync::Arc;
use rand::{self, Rand, Rng};
use serde::{de::DeserializeOwned, Serialize};
use super::{EncryptionSchedule, HoneyBadger};
use honey_badger::SubsetHandlingStrategy;
use super::{EncryptionSchedule, HoneyBadger, Params, SubsetHandlingStrategy};
use util::SubRng;
use {Contribution, NetworkInfo, NodeIdT};
@ -19,16 +18,10 @@ pub struct HoneyBadgerBuilder<C, N> {
session_id: u64,
/// Start in this epoch.
epoch: u64,
/// The maximum number of future epochs for which we handle messages simultaneously.
max_future_epochs: u64,
/// Random number generator passed on to algorithm instance for signing and encrypting.
rng: Box<dyn Rng>,
/// Strategy used to handle the output of the `Subset` algorithm.
subset_handling_strategy: SubsetHandlingStrategy,
/// Whether to generate a pseudorandom value in each epoch.
random_value: bool,
/// Schedule for adding threshold encryption to some percentage of rounds
encryption_schedule: EncryptionSchedule,
/// Parameters controlling Honey Badger's behavior and performance.
params: Params,
_phantom: PhantomData<C>,
}
@ -44,11 +37,8 @@ where
netinfo,
session_id: 0,
epoch: 0,
max_future_epochs: 3,
rng: Box::new(rand::thread_rng()),
subset_handling_strategy: SubsetHandlingStrategy::Incremental,
random_value: false,
encryption_schedule: EncryptionSchedule::Always,
params: Params::default(),
_phantom: PhantomData,
}
}
@ -76,7 +66,7 @@ where
/// Sets the maximum number of future epochs for which we handle messages simultaneously.
pub fn max_future_epochs(&mut self, max_future_epochs: u64) -> &mut Self {
self.max_future_epochs = max_future_epochs;
self.params.max_future_epochs = max_future_epochs;
self
}
@ -85,19 +75,25 @@ where
&mut self,
subset_handling_strategy: SubsetHandlingStrategy,
) -> &mut Self {
self.subset_handling_strategy = subset_handling_strategy;
self.params.subset_handling_strategy = subset_handling_strategy;
self
}
/// Whether to generate a pseudorandom value in each epoch.
pub fn random_value(&mut self, random_value: bool) -> &mut Self {
self.random_value = random_value;
self.params.random_value = random_value;
self
}
/// Sets the schedule to use for threshold encryption.
pub fn encryption_schedule(&mut self, encryption_schedule: EncryptionSchedule) -> &mut Self {
self.encryption_schedule = encryption_schedule;
self.params.encryption_schedule = encryption_schedule;
self
}
/// Sets the parameters controlling Honey Badger's behavior and performance.
pub fn params(&mut self, params: Params) -> &mut Self {
self.params = params;
self
}
@ -109,11 +105,8 @@ where
epoch: self.epoch,
has_input: false,
epochs: BTreeMap::new(),
max_future_epochs: self.max_future_epochs as u64,
params: self.params.clone(),
rng: Box::new(self.rng.sub_rng()),
subset_handling_strategy: self.subset_handling_strategy.clone(),
encryption_schedule: self.encryption_schedule,
random_value: self.random_value,
}
}
}

View File

@ -11,7 +11,7 @@ use super::epoch_state::EpochState;
use super::{Batch, Error, ErrorKind, HoneyBadgerBuilder, Message, Result};
use {util, Contribution, DistAlgorithm, Fault, FaultKind, NetworkInfo, NodeIdT};
pub use super::epoch_state::SubsetHandlingStrategy;
use super::Params;
/// An instance of the Honey Badger Byzantine fault tolerant consensus algorithm.
#[derive(Derivative)]
@ -28,18 +28,12 @@ pub struct HoneyBadger<C, N: Rand> {
pub(super) has_input: bool,
/// The subalgorithms for ongoing epochs.
pub(super) epochs: BTreeMap<u64, EpochState<C, N>>,
/// The maximum number of `Subset` instances that we run simultaneously.
pub(super) max_future_epochs: u64,
/// Parameters controlling Honey Badger's behavior and performance.
pub(super) params: Params,
/// A random number generator used for secret key generation.
// Boxed to avoid overloading the algorithm's type with more generics.
#[derivative(Debug(format_with = "util::fmt_rng"))]
pub(super) rng: Box<dyn Rng + Send + Sync>,
/// Represents the optimization strategy to use for output of the `Subset` algorithm.
pub(super) subset_handling_strategy: SubsetHandlingStrategy,
/// The schedule for which rounds we should use threshold encryption.
pub(super) encryption_schedule: EncryptionSchedule,
/// Whether to generate a pseudorandom value in each epoch.
pub(super) random_value: bool,
}
pub type Step<C, N> = ::DaStep<HoneyBadger<C, N>>;
@ -116,7 +110,7 @@ where
return Err(ErrorKind::UnknownSender.into());
}
let Message { epoch, content } = message;
if epoch > self.epoch + self.max_future_epochs {
if epoch > self.epoch + self.params.max_future_epochs {
Ok(Fault::new(sender_id.clone(), FaultKind::UnexpectedHbMessageEpoch).into())
} else if epoch < self.epoch {
// The message is late; discard it.
@ -137,7 +131,7 @@ where
/// Returns the current encryption schedule that determines in which epochs contributions are
/// encrypted.
pub fn get_encryption_schedule(&self) -> EncryptionSchedule {
self.encryption_schedule
self.params.encryption_schedule
}
/// Returns the epoch of the next batch that will be output.
@ -186,16 +180,16 @@ where
self.netinfo.clone(),
self.session_id,
epoch,
self.subset_handling_strategy.clone(),
self.random_value,
self.encryption_schedule.use_on_epoch(epoch),
self.params.subset_handling_strategy.clone(),
self.params.random_value,
self.params.encryption_schedule.use_on_epoch(epoch),
)?),
})
}
/// Returns the maximum future epochs of the Honey Badger algorithm instance.
pub fn max_future_epochs(&self) -> u64 {
self.max_future_epochs
self.params.max_future_epochs
}
}

View File

@ -28,9 +28,12 @@ mod epoch_state;
mod error;
mod honey_badger;
mod message;
mod params;
pub use self::batch::Batch;
pub use self::builder::HoneyBadgerBuilder;
pub use self::epoch_state::SubsetHandlingStrategy;
pub use self::error::{Error, ErrorKind, Result};
pub use self::honey_badger::{EncryptionSchedule, HoneyBadger, Step, SubsetHandlingStrategy};
pub use self::honey_badger::{EncryptionSchedule, HoneyBadger, Step};
pub use self::message::{Message, MessageContent};
pub use self::params::Params;

View File

@ -0,0 +1,25 @@
use super::{EncryptionSchedule, SubsetHandlingStrategy};
/// Parameters controlling Honey Badger's behavior and performance.
#[derive(Debug, Clone)]
pub struct Params {
/// The maximum number of future epochs for which we handle messages simultaneously.
pub max_future_epochs: u64,
/// Strategy used to handle the output of the `Subset` algorithm.
pub subset_handling_strategy: SubsetHandlingStrategy,
/// Whether to generate a pseudorandom value in each epoch.
pub random_value: bool,
/// Schedule for adding threshold encryption to some percentage of rounds
pub encryption_schedule: EncryptionSchedule,
}
impl Default for Params {
fn default() -> Params {
Params {
max_future_epochs: 3,
subset_handling_strategy: SubsetHandlingStrategy::Incremental,
random_value: false,
encryption_schedule: EncryptionSchedule::Always,
}
}
}