Use derivative for configurable Debug impls. (#283)

This commit is contained in:
Andreas Fackler 2018-10-24 13:01:52 +02:00 committed by Marc Brinkmann
parent bc6a67dc01
commit 6375decbc0
6 changed files with 23 additions and 59 deletions

View File

@ -22,6 +22,7 @@ travis-ci = { repository = "poanetwork/hbbft" }
[dependencies]
bincode = "1.0.0"
byteorder = "1.2.3"
derivative = "1.0.1"
env_logger = "0.5.10"
failure = "0.1"
hex_fmt = "0.2"

View File

@ -1,6 +1,6 @@
use std::collections::BTreeMap;
use std::mem;
use std::sync::Arc;
use std::{fmt, mem};
use bincode;
use crypto::Signature;
@ -15,10 +15,12 @@ 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 util::SubRng;
use util::{self, SubRng};
use {Contribution, DistAlgorithm, NetworkInfo, NodeIdT, Target};
/// A Honey Badger instance that can handle adding and removing nodes.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct DynamicHoneyBadger<C, N: Rand> {
/// Shared network data.
pub(super) netinfo: NetworkInfo<N>,
@ -38,29 +40,10 @@ pub struct DynamicHoneyBadger<C, N: Rand> {
pub(super) incoming_queue: Vec<(N, Message<N>)>,
/// 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 rand::Rng + Send + Sync>,
}
impl<C, N> fmt::Debug for DynamicHoneyBadger<C, N>
where
C: fmt::Debug,
N: Rand + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("DynamicHoneyBadger")
.field("netinfo", &self.netinfo)
.field("max_future_epochs", &self.max_future_epochs)
.field("start_epoch", &self.start_epoch)
.field("vote_counter", &self.vote_counter)
.field("key_gen_msg_buffer", &self.key_gen_msg_buffer)
.field("honey_badger", &self.honey_badger)
.field("key_gen_state", &self.key_gen_state)
.field("incoming_queue", &self.incoming_queue)
.field("rng", &"<RNG>")
.finish()
}
}
impl<C, N> DistAlgorithm for DynamicHoneyBadger<C, N>
where
C: Contribution + Serialize + for<'r> Deserialize<'r>,

View File

@ -1,6 +1,5 @@
use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::fmt;
use std::sync::Arc;
use bincode;
@ -9,11 +8,13 @@ use serde::{Deserialize, Serialize};
use super::epoch_state::EpochState;
use super::{Batch, Error, ErrorKind, HoneyBadgerBuilder, Message, MessageContent, Result};
use {Contribution, DistAlgorithm, NetworkInfo, NodeIdT};
use {util, Contribution, DistAlgorithm, NetworkInfo, NodeIdT};
pub use super::epoch_state::SubsetHandlingStrategy;
/// An instance of the Honey Badger Byzantine fault tolerant consensus algorithm.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct HoneyBadger<C, N: Rand> {
/// Shared network data.
pub(super) netinfo: Arc<NetworkInfo<N>>,
@ -29,29 +30,12 @@ pub struct HoneyBadger<C, N: Rand> {
pub(super) incoming_queue: BTreeMap<u64, Vec<(N, MessageContent<N>)>>,
/// 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,
}
impl<C, N> fmt::Debug for HoneyBadger<C, N>
where
N: Rand + fmt::Debug,
C: fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("HoneyBadger")
.field("netinfo", &self.netinfo)
.field("epoch", &self.epoch)
.field("has_input", &self.has_input)
.field("epochs", &self.epochs)
.field("max_future_epochs", &self.max_future_epochs)
.field("incoming_queue", &self.incoming_queue)
.field("rng", &"<RNG>")
.finish()
}
}
pub type Step<C, N> = ::Step<HoneyBadger<C, N>>;
impl<C, N> DistAlgorithm for HoneyBadger<C, N>

View File

@ -119,6 +119,8 @@
extern crate bincode;
extern crate byteorder;
#[macro_use(Derivative)]
extern crate derivative;
#[macro_use]
extern crate failure;
extern crate hex_fmt;

View File

@ -32,7 +32,7 @@ use serde::{Deserialize, Serialize};
use dynamic_honey_badger::{self, Batch as DhbBatch, DynamicHoneyBadger, Message};
use transaction_queue::TransactionQueue;
use {Contribution, DistAlgorithm, NodeIdT};
use {util, Contribution, DistAlgorithm, NodeIdT};
pub use dynamic_honey_badger::{Change, ChangeState, Input};
@ -173,6 +173,8 @@ where
/// A Honey Badger instance that can handle adding and removing nodes and manages a transaction
/// queue.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct QueueingHoneyBadger<T, N, Q>
where
T: Contribution + Serialize + for<'r> Deserialize<'r>,
@ -186,25 +188,10 @@ where
/// The queue of pending transactions that haven't been output in a batch yet.
queue: Q,
/// Random number generator used for choosing transactions from the queue.
#[derivative(Debug(format_with = "util::fmt_rng"))]
rng: Box<dyn Rng + Send + Sync>,
}
impl<T, N, Q> fmt::Debug for QueueingHoneyBadger<T, N, Q>
where
T: Contribution + Serialize + for<'r> Deserialize<'r>,
N: NodeIdT + Serialize + for<'r> Deserialize<'r> + Rand,
Q: TransactionQueue<T>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("QueueingHoneyBadger")
.field("batch_size", &self.batch_size)
.field("dyn_hb", &self.dyn_hb)
.field("queue", &self.queue)
.field("rng", &"<RNG>")
.finish()
}
}
pub type Step<T, N, Q> = ::Step<QueueingHoneyBadger<T, N, Q>>;
impl<T, N, Q> DistAlgorithm for QueueingHoneyBadger<T, N, Q>

View File

@ -3,6 +3,8 @@
//! Functions not large enough to warrant their own crate or module, but flexible enough to be used
//! in multiple disjunct places in the library. May also contain backports, workarounds.
use std::fmt;
use rand;
/// Workaround trait for creating new random number generators
@ -22,3 +24,8 @@ where
Box::new(rng)
}
}
/// Prints "`<RNG>`" as a placeholder for a random number generator in debug output.
pub fn fmt_rng<T>(_: T, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("<RNG>")
}