Skip to content

Commit

Permalink
Allow test env to not supply a value
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Nov 13, 2023
1 parent 0af13bc commit ecc0fd8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 40 deletions.
9 changes: 6 additions & 3 deletions Code/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ where
}
}

async fn get_value(&self) -> Ctx::Value {
self.env.get_value().await
async fn get_value(&self, round: Round) -> Option<Ctx::Value> {
self.env.get_value(self.height.clone(), round).await
}

async fn validate_proposal(&self, proposal: &Ctx::Proposal) -> bool {
Expand Down Expand Up @@ -137,7 +137,10 @@ where
// We are the proposer
// TODO: Schedule propose timeout

let value = self.get_value().await;
let Some(value) = self.get_value(round).await else {
return Err(Error::NoValueToPropose);
};

RoundEvent::NewRoundProposer(value)
} else {
RoundEvent::NewRound
Expand Down
9 changes: 6 additions & 3 deletions Code/driver/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::boxed::Box;

use async_trait::async_trait;

use malachite_common::Context;
use malachite_common::{Context, Round};

/// Environment for use by the [`Driver`](crate::Driver) to ask
/// for a value to propose and validate proposals.
Expand All @@ -11,8 +11,11 @@ pub trait Env<Ctx>
where
Ctx: Context,
{
/// Get the value to propose.
async fn get_value(&self) -> Ctx::Value;
/// Get the value to propose for the given height and round.
///
/// If `None` is returned, the driver will understand this
/// as an error and will not propose a value.
async fn get_value(&self, height: Ctx::Height, round: Round) -> Option<Ctx::Value>;

/// Validate a proposal.
async fn validate_proposal(&self, proposal: &Ctx::Proposal) -> bool;
Expand Down
5 changes: 5 additions & 0 deletions Code/driver/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub enum Error<Ctx>
where
Ctx: Context,
{
/// No value to propose
NoValueToPropose,

/// Proposer not found
ProposerNotFound(Ctx::Address),

Expand All @@ -23,6 +26,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::NoValueToPropose => write!(f, "No value to propose"),
Error::ProposerNotFound(addr) => write!(f, "Proposer not found: {addr}"),
Error::ValidatorNotFound(addr) => write!(f, "Validator not found: {addr}"),
Error::InvalidVoteSignature(vote, validator) => write!(
Expand All @@ -40,6 +44,7 @@ where
{
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Error::NoValueToPropose, Error::NoValueToPropose) => true,
(Error::ProposerNotFound(addr1), Error::ProposerNotFound(addr2)) => addr1 == addr2,
(Error::ValidatorNotFound(addr1), Error::ValidatorNotFound(addr2)) => addr1 == addr2,
(
Expand Down
21 changes: 14 additions & 7 deletions Code/test/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
use async_trait::async_trait;

use malachite_common::Round;
use malachite_driver::Env;

use crate::{Proposal, TestContext, Value};
use crate::{Height, Proposal, TestContext, Value};

pub struct TestEnv {
pub value: Value,
pub is_valid: fn(&Proposal) -> bool,
get_value: Box<dyn Fn(Height, Round) -> Option<Value> + Send + Sync>,
is_valid: Box<dyn Fn(&Proposal) -> bool + Send + Sync>,
}

impl TestEnv {
pub fn new(value: Value, is_valid: fn(&Proposal) -> bool) -> Self {
Self { value, is_valid }
pub fn new(
get_value: impl Fn(Height, Round) -> Option<Value> + Send + Sync + 'static,
is_valid: impl Fn(&Proposal) -> bool + Send + Sync + 'static,
) -> Self {
Self {
get_value: Box::new(get_value),
is_valid: Box::new(is_valid),
}
}
}

#[async_trait]
impl Env<TestContext> for TestEnv {
async fn get_value(&self) -> Value {
self.value.clone()
async fn get_value(&self, height: Height, round: Round) -> Option<Value> {
(self.get_value)(height, round)
}

async fn validate_proposal(&self, proposal: &Proposal) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion Code/test/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl From<u64> for ValueId {
}

/// The value to decide on
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Value(u64);

impl Value {
Expand Down
50 changes: 25 additions & 25 deletions Code/test/tests/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn driver_steps_proposer() {
let value_id = value.id();

let sel = RotateProposer::default();
let env = TestEnv::new(value.clone(), |_| true);
let env = TestEnv::new(move |_, _| Some(value), |_| true);

let mut rng = StdRng::seed_from_u64(0x42);

Expand All @@ -69,7 +69,7 @@ fn driver_steps_proposer() {
let vs = ValidatorSet::new(vec![v1, v2.clone(), v3.clone()]);
let mut driver = Driver::new(ctx, env, sel, Height::new(1), vs, my_addr);

let proposal = Proposal::new(Height::new(1), Round::new(0), value.clone(), Round::new(-1));
let proposal = Proposal::new(Height::new(1), Round::new(0), value, Round::new(-1));

let steps = vec![
TestStep {
Expand Down Expand Up @@ -142,11 +142,11 @@ fn driver_steps_proposer() {
step: Step::Precommit,
proposal: Some(proposal.clone()),
locked: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
valid: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
},
Expand All @@ -161,11 +161,11 @@ fn driver_steps_proposer() {
step: Step::Precommit,
proposal: Some(proposal.clone()),
locked: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
valid: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
},
Expand All @@ -182,11 +182,11 @@ fn driver_steps_proposer() {
step: Step::Precommit,
proposal: Some(proposal.clone()),
locked: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
valid: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
},
Expand All @@ -196,18 +196,18 @@ fn driver_steps_proposer() {
input_event: Some(Event::Vote(
Vote::new_precommit(Round::new(0), Some(value_id), addr3).signed(&sk3),
)),
expected_output: Some(Message::Decide(Round::new(0), value.clone())),
expected_output: Some(Message::Decide(Round::new(0), value)),
expected_round: Round::new(0),
new_state: State {
round: Round::new(0),
step: Step::Commit,
proposal: Some(proposal.clone()),
locked: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
valid: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
},
Expand Down Expand Up @@ -241,7 +241,7 @@ fn driver_steps_not_proposer_valid() {
let value_id = value.id();

let sel = RotateProposer::default();
let env = TestEnv::new(value.clone(), |_| true);
let env = TestEnv::new(move |_, _| Some(value), |_| true);

let mut rng = StdRng::seed_from_u64(0x42);

Expand All @@ -265,7 +265,7 @@ fn driver_steps_not_proposer_valid() {
let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);
let mut driver = Driver::new(ctx, env, sel, Height::new(1), vs, my_addr);

let proposal = Proposal::new(Height::new(1), Round::new(0), value.clone(), Round::new(-1));
let proposal = Proposal::new(Height::new(1), Round::new(0), value, Round::new(-1));

let steps = vec![
TestStep {
Expand Down Expand Up @@ -338,11 +338,11 @@ fn driver_steps_not_proposer_valid() {
step: Step::Precommit,
proposal: Some(proposal.clone()),
locked: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
valid: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
},
Expand All @@ -357,11 +357,11 @@ fn driver_steps_not_proposer_valid() {
step: Step::Precommit,
proposal: Some(proposal.clone()),
locked: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
valid: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
},
Expand All @@ -378,11 +378,11 @@ fn driver_steps_not_proposer_valid() {
step: Step::Precommit,
proposal: Some(proposal.clone()),
locked: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
valid: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
},
Expand All @@ -392,18 +392,18 @@ fn driver_steps_not_proposer_valid() {
input_event: Some(Event::Vote(
Vote::new_precommit(Round::new(0), Some(value_id), addr3).signed(&sk3),
)),
expected_output: Some(Message::Decide(Round::new(0), value.clone())),
expected_output: Some(Message::Decide(Round::new(0), value)),
expected_round: Round::new(0),
new_state: State {
round: Round::new(0),
step: Step::Commit,
proposal: Some(proposal.clone()),
locked: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
valid: Some(RoundValue {
value: value.clone(),
value,
round: Round::new(0),
}),
},
Expand Down Expand Up @@ -437,7 +437,7 @@ fn driver_steps_not_proposer_invalid() {
let value_id = value.id();

let sel = RotateProposer::default();
let env = TestEnv::new(value.clone(), |_| false);
let env = TestEnv::new(move |_, _| Some(value), |_| false);

let mut rng = StdRng::seed_from_u64(0x42);

Expand All @@ -461,7 +461,7 @@ fn driver_steps_not_proposer_invalid() {
let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);
let mut driver = Driver::new(ctx, env, sel, Height::new(1), vs, my_addr);

let proposal = Proposal::new(Height::new(1), Round::new(0), value.clone(), Round::new(-1));
let proposal = Proposal::new(Height::new(1), Round::new(0), value, Round::new(-1));

let steps = vec![
TestStep {
Expand Down Expand Up @@ -579,7 +579,7 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() {
let value_id = value.id();

let sel = RotateProposer::default();
let env = TestEnv::new(value.clone(), |_| true);
let env = TestEnv::new(move |_, _| Some(value), |_| true);

let mut rng = StdRng::seed_from_u64(0x42);

Expand Down
2 changes: 1 addition & 1 deletion Code/test/tests/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn test_prevote() {
Event::Proposal(Proposal::new(
Height::new(1),
Round::new(1),
value.clone(),
value,
Round::Nil,
)),
);
Expand Down

0 comments on commit ecc0fd8

Please sign in to comment.