Skip to content

Commit

Permalink
chore: Hide proposer selector type from Driver struct type (#90)
Browse files Browse the repository at this point in the history
* chore: Hide proposer selector type from `Driver` struct type

* chore: Manually derive `Clone` and `Debug` where appropriate

* chore: Exclude Clone and Debug instances from code coverage

* chore: Cleanup
  • Loading branch information
romac authored Nov 28, 2023
1 parent 60cbc96 commit 952c4e3
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
1 change: 1 addition & 0 deletions Code/common/src/validator_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ where
/// A validator set is a collection of validators.
pub trait ValidatorSet<Ctx>
where
Self: Clone + Debug,
Ctx: Context,
{
/// The total voting power of the validator set.
Expand Down
32 changes: 23 additions & 9 deletions Code/driver/src/driver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use malachite_round::state_machine::Info;
use alloc::boxed::Box;
use core::fmt;

use malachite_common::{
Context, Proposal, Round, SignedVote, Timeout, TimeoutStep, Validator, ValidatorSet, Value,
Expand All @@ -7,6 +8,7 @@ use malachite_common::{
use malachite_round::events::Event as RoundEvent;
use malachite_round::message::Message as RoundMessage;
use malachite_round::state::{State as RoundState, Step};
use malachite_round::state_machine::Info;
use malachite_vote::keeper::Message as VoteMessage;
use malachite_vote::keeper::VoteKeeper;
use malachite_vote::Threshold;
Expand All @@ -19,14 +21,12 @@ use crate::ProposerSelector;
use crate::Validity;

/// Driver for the state machine of the Malachite consensus engine at a given height.
#[derive(Clone, Debug)]
pub struct Driver<Ctx, PSel>
pub struct Driver<Ctx>
where
Ctx: Context,
PSel: ProposerSelector<Ctx>,
{
pub ctx: Ctx,
pub proposer_selector: PSel,
pub proposer_selector: Box<dyn ProposerSelector<Ctx>>,

pub address: Ctx::Address,
pub validator_set: Ctx::ValidatorSet,
Expand All @@ -35,14 +35,13 @@ where
pub round_state: RoundState<Ctx>,
}

impl<Ctx, PSel> Driver<Ctx, PSel>
impl<Ctx> Driver<Ctx>
where
Ctx: Context,
PSel: ProposerSelector<Ctx>,
{
pub fn new(
ctx: Ctx,
proposer_selector: PSel,
proposer_selector: impl ProposerSelector<Ctx> + 'static,
validator_set: Ctx::ValidatorSet,
address: Ctx::Address,
) -> Self {
Expand All @@ -53,7 +52,7 @@ where

Self {
ctx,
proposer_selector,
proposer_selector: Box::new(proposer_selector),
address,
validator_set,
votes,
Expand Down Expand Up @@ -329,3 +328,18 @@ where
Ok(transition.message)
}
}

impl<Ctx> fmt::Debug for Driver<Ctx>
where
Ctx: Context,
{
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Driver")
.field("address", &self.address)
.field("validator_set", &self.validator_set)
.field("votes", &self.votes)
.field("round_state", &self.round_state)
.finish()
}
}
1 change: 1 addition & 0 deletions Code/test/src/validator_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl malachite_common::Validator<TestContext> for Validator {
}

/// A validator set contains a list of validators sorted by address.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ValidatorSet {
pub validators: Vec<Validator>,
}
Expand Down
7 changes: 2 additions & 5 deletions Code/test/tests/driver_extra.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use futures::executor::block_on;

use malachite_common::Round;
use malachite_driver::{Driver, Event, Message, ProposerSelector, Validity};
use malachite_driver::{Driver, Event, Message, Validity};
use malachite_round::state::State;

use malachite_test::{Height, Proposal, TestContext, ValidatorSet, Value};
Expand Down Expand Up @@ -514,10 +514,7 @@ fn driver_steps_polka_previous_with_no_locked() {
run_steps(&mut driver, steps);
}

fn run_steps<P>(driver: &mut Driver<TestContext, P>, steps: Vec<TestStep>)
where
P: ProposerSelector<TestContext>,
{
fn run_steps(driver: &mut Driver<TestContext>, steps: Vec<TestStep>) {
for step in steps {
println!("Step: {}", step.desc);

Expand Down
60 changes: 58 additions & 2 deletions Code/vote/src/keeper.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt;

use alloc::collections::{BTreeMap, BTreeSet};

use malachite_common::{Context, Round, ValueId, Vote, VoteType};
Expand All @@ -17,7 +19,6 @@ pub enum Message<Value> {
SkipRound(Round),
}

#[derive(Clone, Debug)]
struct PerRound<Ctx>
where
Ctx: Context,
Expand All @@ -40,8 +41,35 @@ where
}
}

impl<Ctx> Clone for PerRound<Ctx>
where
Ctx: Context,
{
#[cfg_attr(coverage_nightly, coverage(off))]
fn clone(&self) -> Self {
Self {
votes: self.votes.clone(),
addresses_weights: self.addresses_weights.clone(),
emitted_msgs: self.emitted_msgs.clone(),
}
}
}

impl<Ctx> fmt::Debug for PerRound<Ctx>
where
Ctx: Context,
{
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PerRound")
.field("votes", &self.votes)
.field("addresses_weights", &self.addresses_weights)
.field("emitted_msgs", &self.emitted_msgs)
.finish()
}
}

/// Keeps track of votes and emits messages when thresholds are reached.
#[derive(Clone, Debug)]
pub struct VoteKeeper<Ctx>
where
Ctx: Context,
Expand Down Expand Up @@ -188,3 +216,31 @@ fn threshold_to_message<Value>(
(VoteType::Precommit, Threshold::Value(v)) => Some(Message::PrecommitValue(v)),
}
}

impl<Ctx> Clone for VoteKeeper<Ctx>
where
Ctx: Context,
{
#[cfg_attr(coverage_nightly, coverage(off))]
fn clone(&self) -> Self {
Self {
total_weight: self.total_weight,
threshold_params: self.threshold_params,
per_round: self.per_round.clone(),
}
}
}

impl<Ctx> fmt::Debug for VoteKeeper<Ctx>
where
Ctx: Context,
{
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VoteKeeper")
.field("total_weight", &self.total_weight)
.field("threshold_params", &self.threshold_params)
.field("per_round", &self.per_round)
.finish()
}
}

0 comments on commit 952c4e3

Please sign in to comment.