diff --git a/Code/QUESTIONS.md b/Code/QUESTIONS.md deleted file mode 100644 index 700c9fae4..000000000 --- a/Code/QUESTIONS.md +++ /dev/null @@ -1 +0,0 @@ -- How do we deal with errors? diff --git a/Code/TODO.md b/Code/TODO.md deleted file mode 100644 index 8a7878e9e..000000000 --- a/Code/TODO.md +++ /dev/null @@ -1,11 +0,0 @@ -is proposal complete -if polka not nil, then need to see proof of lock (2f+1 votes) -then send proposal - -count votes for cur, prev, 1/2 next round - -if complete proposal from a past round => to current one -if we have some threshold (f+1) of votes for a future round => skip to that round - -context (get proposer, get value) -signing context diff --git a/Code/driver/src/driver.rs b/Code/driver/src/driver.rs index 3fd8ced40..2873e77fa 100644 --- a/Code/driver/src/driver.rs +++ b/Code/driver/src/driver.rs @@ -5,17 +5,17 @@ use malachite_common::{ Context, Proposal, Round, SignedVote, Timeout, TimeoutStep, Validator, ValidatorSet, Value, Vote, VoteType, }; -use malachite_round::events::Event as RoundEvent; -use malachite_round::message::Message as RoundMessage; +use malachite_round::input::Input as RoundEvent; +use malachite_round::output::Output as RoundOutput; 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::Output as VoteMessage; use malachite_vote::keeper::VoteKeeper; use malachite_vote::Threshold; use malachite_vote::ThresholdParams; -use crate::event::Event; -use crate::message::Message; +use crate::input::Input; +use crate::output::Output; use crate::Error; use crate::ProposerSelector; use crate::Validity; @@ -81,47 +81,47 @@ where Ok(proposer) } - pub async fn execute(&mut self, msg: Event) -> Result>, Error> { - let round_msg = match self.apply(msg).await? { + pub async fn execute(&mut self, msg: Input) -> Result>, Error> { + let round_output = match self.apply(msg).await? { Some(msg) => msg, None => return Ok(None), }; - let msg = match round_msg { - RoundMessage::NewRound(round) => Message::NewRound(self.height().clone(), round), + let output = match round_output { + RoundOutput::NewRound(round) => Output::NewRound(self.height().clone(), round), - RoundMessage::Proposal(proposal) => { - // sign the proposal - Message::Propose(proposal) + RoundOutput::Proposal(proposal) => { + // TODO: sign the proposal + Output::Propose(proposal) } - RoundMessage::Vote(vote) => { + RoundOutput::Vote(vote) => { let signed_vote = self.ctx.sign_vote(vote); - Message::Vote(signed_vote) + Output::Vote(signed_vote) } - RoundMessage::ScheduleTimeout(timeout) => Message::ScheduleTimeout(timeout), + RoundOutput::ScheduleTimeout(timeout) => Output::ScheduleTimeout(timeout), - RoundMessage::GetValueAndScheduleTimeout(round, timeout) => { - Message::GetValueAndScheduleTimeout(round, timeout) + RoundOutput::GetValueAndScheduleTimeout(round, timeout) => { + Output::GetValueAndScheduleTimeout(round, timeout) } - RoundMessage::Decision(value) => { + RoundOutput::Decision(value) => { // TODO: update the state - Message::Decide(value.round, value.value) + Output::Decide(value.round, value.value) } }; - Ok(Some(msg)) + Ok(Some(output)) } - async fn apply(&mut self, event: Event) -> Result>, Error> { - match event { - Event::NewRound(height, round) => self.apply_new_round(height, round).await, - Event::ProposeValue(round, value) => self.apply_propose_value(round, value).await, - Event::Proposal(proposal, validity) => self.apply_proposal(proposal, validity).await, - Event::Vote(signed_vote) => self.apply_vote(signed_vote), - Event::TimeoutElapsed(timeout) => self.apply_timeout(timeout), + async fn apply(&mut self, input: Input) -> Result>, Error> { + match input { + Input::NewRound(height, round) => self.apply_new_round(height, round).await, + Input::ProposeValue(round, value) => self.apply_propose_value(round, value).await, + Input::Proposal(proposal, validity) => self.apply_proposal(proposal, validity).await, + Input::Vote(signed_vote) => self.apply_vote(signed_vote), + Input::TimeoutElapsed(timeout) => self.apply_timeout(timeout), } } @@ -129,7 +129,7 @@ where &mut self, height: Ctx::Height, round: Round, - ) -> Result>, Error> { + ) -> Result>, Error> { if self.height() == &height { // If it's a new round for same height, just reset the round, keep the valid and locked values self.round_state.round = round; @@ -143,7 +143,7 @@ where &mut self, round: Round, value: Ctx::Value, - ) -> Result>, Error> { + ) -> Result>, Error> { self.apply_event(round, RoundEvent::ProposeValue(value)) } @@ -151,7 +151,7 @@ where &mut self, proposal: Ctx::Proposal, validity: Validity, - ) -> Result>, Error> { + ) -> Result>, Error> { // Check that there is an ongoing round if self.round_state.round == Round::Nil { return Ok(None); @@ -241,7 +241,7 @@ where fn apply_vote( &mut self, signed_vote: SignedVote, - ) -> Result>, Error> { + ) -> Result>, Error> { let validator = self .validator_set .get_by_address(signed_vote.validator_address()) @@ -279,7 +279,7 @@ where self.apply_event(vote_round, round_event) } - fn apply_timeout(&mut self, timeout: Timeout) -> Result>, Error> { + fn apply_timeout(&mut self, timeout: Timeout) -> Result>, Error> { let event = match timeout.step { TimeoutStep::Propose => RoundEvent::TimeoutPropose, TimeoutStep::Prevote => RoundEvent::TimeoutPrevote, @@ -294,7 +294,7 @@ where &mut self, event_round: Round, event: RoundEvent, - ) -> Result>, Error> { + ) -> Result>, Error> { let round_state = core::mem::take(&mut self.round_state); let proposer = self.get_proposer(round_state.round)?; @@ -319,13 +319,13 @@ where }; // Apply the event to the round state machine - let transition = round_state.apply_event(&data, mux_event); + let transition = round_state.apply(&data, mux_event); // Update state self.round_state = transition.next_state; - // Return message, if any - Ok(transition.message) + // Return output, if any + Ok(transition.output) } } diff --git a/Code/driver/src/event.rs b/Code/driver/src/input.rs similarity index 96% rename from Code/driver/src/event.rs rename to Code/driver/src/input.rs index b2cf5f726..eda6d7944 100644 --- a/Code/driver/src/event.rs +++ b/Code/driver/src/input.rs @@ -4,7 +4,7 @@ use crate::Validity; /// Events that can be received by the [`Driver`](crate::Driver). #[derive(Clone, Debug, PartialEq, Eq)] -pub enum Event +pub enum Input where Ctx: Context, { diff --git a/Code/driver/src/lib.rs b/Code/driver/src/lib.rs index 6fba7865f..95c0f560f 100644 --- a/Code/driver/src/lib.rs +++ b/Code/driver/src/lib.rs @@ -16,15 +16,15 @@ extern crate alloc; mod driver; mod error; -mod event; -mod message; +mod input; +mod output; mod proposer; mod util; pub use driver::Driver; pub use error::Error; -pub use event::Event; -pub use message::Message; +pub use input::Input; +pub use output::Output; pub use proposer::ProposerSelector; pub use util::Validity; diff --git a/Code/driver/src/message.rs b/Code/driver/src/message.rs deleted file mode 100644 index 52b354d80..000000000 --- a/Code/driver/src/message.rs +++ /dev/null @@ -1,93 +0,0 @@ -use core::fmt; - -use malachite_common::{Context, Round, SignedVote, Timeout}; - -/// Messages emitted by the [`Driver`](crate::Driver) -pub enum Message -where - Ctx: Context, -{ - /// Start a new round - NewRound(Ctx::Height, Round), - - /// Broadcast a proposal - Propose(Ctx::Proposal), - - /// Broadcast a vote for a value - Vote(SignedVote), - - /// Decide on a value - Decide(Round, Ctx::Value), - - /// Schedule a timeout - ScheduleTimeout(Timeout), - - /// Ask for a value to propose and schedule a timeout - GetValueAndScheduleTimeout(Round, Timeout), -} - -// NOTE: We have to derive these instances manually, otherwise -// the compiler would infer a Clone/Debug/PartialEq/Eq bound on `Ctx`, -// which may not hold for all contexts. - -impl Clone for Message { - #[cfg_attr(coverage_nightly, coverage(off))] - fn clone(&self) -> Self { - match self { - Message::NewRound(height, round) => Message::NewRound(height.clone(), *round), - Message::Propose(proposal) => Message::Propose(proposal.clone()), - Message::Vote(signed_vote) => Message::Vote(signed_vote.clone()), - Message::Decide(round, value) => Message::Decide(*round, value.clone()), - Message::ScheduleTimeout(timeout) => Message::ScheduleTimeout(*timeout), - Message::GetValueAndScheduleTimeout(round, timeout) => { - Message::GetValueAndScheduleTimeout(*round, *timeout) - } - } - } -} - -impl fmt::Debug for Message { - #[cfg_attr(coverage_nightly, coverage(off))] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Message::NewRound(height, round) => write!(f, "NewRound({:?}, {:?})", height, round), - Message::Propose(proposal) => write!(f, "Propose({:?})", proposal), - Message::Vote(signed_vote) => write!(f, "Vote({:?})", signed_vote), - Message::Decide(round, value) => write!(f, "Decide({:?}, {:?})", round, value), - Message::ScheduleTimeout(timeout) => write!(f, "ScheduleTimeout({:?})", timeout), - Message::GetValueAndScheduleTimeout(round, timeout) => { - write!(f, "GetValueAndScheduleTimeout({:?}, {:?})", round, timeout) - } - } - } -} - -impl PartialEq for Message { - #[cfg_attr(coverage_nightly, coverage(off))] - fn eq(&self, other: &Self) -> bool { - match (self, other) { - (Message::NewRound(height, round), Message::NewRound(other_height, other_round)) => { - height == other_height && round == other_round - } - (Message::Propose(proposal), Message::Propose(other_proposal)) => { - proposal == other_proposal - } - (Message::Vote(signed_vote), Message::Vote(other_signed_vote)) => { - signed_vote == other_signed_vote - } - (Message::Decide(round, value), Message::Decide(other_round, other_value)) => { - round == other_round && value == other_value - } - (Message::ScheduleTimeout(timeout), Message::ScheduleTimeout(other_timeout)) => { - timeout == other_timeout - } - ( - Message::GetValueAndScheduleTimeout(round, timeout), - Message::GetValueAndScheduleTimeout(other_round, other_timeout), - ) => round == other_round && timeout == other_timeout, - _ => false, - } - } -} - -impl Eq for Message {} diff --git a/Code/driver/src/output.rs b/Code/driver/src/output.rs new file mode 100644 index 000000000..d0565f484 --- /dev/null +++ b/Code/driver/src/output.rs @@ -0,0 +1,93 @@ +use core::fmt; + +use malachite_common::{Context, Round, SignedVote, Timeout}; + +/// Messages emitted by the [`Driver`](crate::Driver) +pub enum Output +where + Ctx: Context, +{ + /// Start a new round + NewRound(Ctx::Height, Round), + + /// Broadcast a proposal + Propose(Ctx::Proposal), + + /// Broadcast a vote for a value + Vote(SignedVote), + + /// Decide on a value + Decide(Round, Ctx::Value), + + /// Schedule a timeout + ScheduleTimeout(Timeout), + + /// Ask for a value to propose and schedule a timeout + GetValueAndScheduleTimeout(Round, Timeout), +} + +// NOTE: We have to derive these instances manually, otherwise +// the compiler would infer a Clone/Debug/PartialEq/Eq bound on `Ctx`, +// which may not hold for all contexts. + +impl Clone for Output { + #[cfg_attr(coverage_nightly, coverage(off))] + fn clone(&self) -> Self { + match self { + Output::NewRound(height, round) => Output::NewRound(height.clone(), *round), + Output::Propose(proposal) => Output::Propose(proposal.clone()), + Output::Vote(signed_vote) => Output::Vote(signed_vote.clone()), + Output::Decide(round, value) => Output::Decide(*round, value.clone()), + Output::ScheduleTimeout(timeout) => Output::ScheduleTimeout(*timeout), + Output::GetValueAndScheduleTimeout(round, timeout) => { + Output::GetValueAndScheduleTimeout(*round, *timeout) + } + } + } +} + +impl fmt::Debug for Output { + #[cfg_attr(coverage_nightly, coverage(off))] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Output::NewRound(height, round) => write!(f, "NewRound({:?}, {:?})", height, round), + Output::Propose(proposal) => write!(f, "Propose({:?})", proposal), + Output::Vote(signed_vote) => write!(f, "Vote({:?})", signed_vote), + Output::Decide(round, value) => write!(f, "Decide({:?}, {:?})", round, value), + Output::ScheduleTimeout(timeout) => write!(f, "ScheduleTimeout({:?})", timeout), + Output::GetValueAndScheduleTimeout(round, timeout) => { + write!(f, "GetValueAndScheduleTimeout({:?}, {:?})", round, timeout) + } + } + } +} + +impl PartialEq for Output { + #[cfg_attr(coverage_nightly, coverage(off))] + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Output::NewRound(height, round), Output::NewRound(other_height, other_round)) => { + height == other_height && round == other_round + } + (Output::Propose(proposal), Output::Propose(other_proposal)) => { + proposal == other_proposal + } + (Output::Vote(signed_vote), Output::Vote(other_signed_vote)) => { + signed_vote == other_signed_vote + } + (Output::Decide(round, value), Output::Decide(other_round, other_value)) => { + round == other_round && value == other_value + } + (Output::ScheduleTimeout(timeout), Output::ScheduleTimeout(other_timeout)) => { + timeout == other_timeout + } + ( + Output::GetValueAndScheduleTimeout(round, timeout), + Output::GetValueAndScheduleTimeout(other_round, other_timeout), + ) => round == other_round && timeout == other_timeout, + _ => false, + } + } +} + +impl Eq for Output {} diff --git a/Code/itf/tests/votekeeper/runner.rs b/Code/itf/tests/votekeeper/runner.rs index 482186b7f..b5c876e06 100644 --- a/Code/itf/tests/votekeeper/runner.rs +++ b/Code/itf/tests/votekeeper/runner.rs @@ -4,7 +4,7 @@ use malachite_common::{Context, Round, Value}; use malachite_itf::votekeeper::State; use malachite_test::{Address, Height, TestContext, Vote}; use malachite_vote::{ - keeper::{Message, VoteKeeper}, + keeper::{Output, VoteKeeper}, ThresholdParams, }; @@ -18,7 +18,7 @@ pub struct VoteKeeperRunner { impl ItfRunner for VoteKeeperRunner { type ActualState = VoteKeeper; - type Result = Option::Value as Value>::Id>>; + type Result = Option::Value as Value>::Id>>; type ExpectedState = State; type Error = (); @@ -75,21 +75,21 @@ impl ItfRunner for VoteKeeperRunner { // Check result against expected result. match result { Some(result) => match result { - Message::PolkaValue(value) => { + Output::PolkaValue(value) => { assert_eq!(expected_result.name, "PolkaValue"); assert_eq!( value_from_model(&expected_result.value).as_ref(), Some(value) ); } - Message::PrecommitValue(value) => { + Output::PrecommitValue(value) => { assert_eq!(expected_result.name, "PrecommitValue"); assert_eq!( value_from_model(&expected_result.value).as_ref(), Some(value) ); } - Message::SkipRound(round) => { + Output::SkipRound(round) => { assert_eq!(expected_result.name, "Skip"); assert_eq!(&Round::new(expected_result.round), round); } @@ -141,9 +141,9 @@ impl ItfRunner for VoteKeeperRunner { for event in actual_events { let event_name = match event { - Message::PolkaValue(_) => "PolkaValue".into(), - Message::PrecommitValue(_) => "PrecommitValue".into(), - Message::SkipRound(_) => "Skip".into(), + Output::PolkaValue(_) => "PolkaValue".into(), + Output::PrecommitValue(_) => "PrecommitValue".into(), + Output::SkipRound(_) => "Skip".into(), _ => format!("{event:?}"), }; let count = event_count.entry(event_name.clone()).or_insert(0); diff --git a/Code/round/src/events.rs b/Code/round/src/events.rs deleted file mode 100644 index 0ea71c90f..000000000 --- a/Code/round/src/events.rs +++ /dev/null @@ -1,25 +0,0 @@ -use malachite_common::{Context, Round, ValueId}; - -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum Event -where - Ctx: Context, -{ - NewRound, // Start a new round, either as proposer or not. L14/L20 - ProposeValue(Ctx::Value), // Propose a value.L14 - Proposal(Ctx::Proposal), // Receive a proposal. L22 + L23 (valid) - InvalidProposal, // Receive an invalid proposal. L26 + L32 (invalid) - ProposalAndPolkaPrevious(Ctx::Proposal), // Received a proposal and a polka value from a previous round. L28 + L29 (valid) - InvalidProposalAndPolkaPrevious(Ctx::Proposal), // Received a proposal and a polka value from a previous round. L28 + L29 (invalid) - PolkaValue(ValueId), // Receive +2/3 prevotes for valueId. L44 - PolkaAny, // Receive +2/3 prevotes for anything. L34 - PolkaNil, // Receive +2/3 prevotes for nil. L44 - ProposalAndPolkaCurrent(Ctx::Proposal), // Receive +2/3 prevotes for Value in current round. L36 - PrecommitAny, // Receive +2/3 precommits for anything. L47 - ProposalAndPrecommitValue(Ctx::Proposal), // Receive +2/3 precommits for Value. L49 - PrecommitValue(ValueId), // Receive +2/3 precommits for ValueId. L51 - SkipRound(Round), // Receive +1/3 messages from a higher round. OneCorrectProcessInHigherRound, L55 - TimeoutPropose, // Timeout waiting for proposal. L57 - TimeoutPrevote, // Timeout waiting for prevotes. L61 - TimeoutPrecommit, // Timeout waiting for precommits. L65 -} diff --git a/Code/round/src/input.rs b/Code/round/src/input.rs new file mode 100644 index 000000000..c095f4dcd --- /dev/null +++ b/Code/round/src/input.rs @@ -0,0 +1,75 @@ +use malachite_common::{Context, Round, ValueId}; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Input +where + Ctx: Context, +{ + /// Start a new round, either as proposer or not. + /// L14/L20 + NewRound, + + /// Propose a value. + /// L14 + ProposeValue(Ctx::Value), + + /// Receive a proposal. + /// L22 + L23 (valid) + Proposal(Ctx::Proposal), + + /// Receive an invalid proposal. + /// L26 + L32 (invalid) + InvalidProposal, + + /// Received a proposal and a polka value from a previous round. + /// L28 + L29 (valid) + ProposalAndPolkaPrevious(Ctx::Proposal), + + /// Received a proposal and a polka value from a previous round. + /// L28 + L29 (invalid) + InvalidProposalAndPolkaPrevious(Ctx::Proposal), + + /// Receive +2/3 prevotes for a value. + /// L44 + PolkaValue(ValueId), + + /// Receive +2/3 prevotes for anything. + /// L34 + PolkaAny, + + /// Receive +2/3 prevotes for nil. + /// L44 + PolkaNil, + + /// Receive +2/3 prevotes for a value in current round. + /// L36 + ProposalAndPolkaCurrent(Ctx::Proposal), + + /// Receive +2/3 precommits for anything. + /// L47 + PrecommitAny, + + /// Receive +2/3 precommits for a value. + /// L49 + ProposalAndPrecommitValue(Ctx::Proposal), + + /// Receive +2/3 precommits for a value. + /// L51 + PrecommitValue(ValueId), + + /// Receive +1/3 messages from a higher round. OneCorrectProcessInHigherRound. + /// L55 + SkipRound(Round), + + /// Timeout waiting for proposal. + /// L57 + TimeoutPropose, + + /// Timeout waiting for prevotes. + /// L61 + TimeoutPrevote, + + /// Timeout waiting for precommits. + /// L65 + TimeoutPrecommit, +} diff --git a/Code/round/src/lib.rs b/Code/round/src/lib.rs index d13887058..33b43bcf6 100644 --- a/Code/round/src/lib.rs +++ b/Code/round/src/lib.rs @@ -14,8 +14,8 @@ extern crate alloc; -pub mod events; -pub mod message; +pub mod input; +pub mod output; pub mod state; pub mod state_machine; pub mod transition; diff --git a/Code/round/src/message.rs b/Code/round/src/output.rs similarity index 50% rename from Code/round/src/message.rs rename to Code/round/src/output.rs index fc5d8f8f5..a7f586015 100644 --- a/Code/round/src/message.rs +++ b/Code/round/src/output.rs @@ -4,7 +4,7 @@ use malachite_common::{Context, Round, Timeout, TimeoutStep, ValueId}; use crate::state::RoundValue; -pub enum Message +pub enum Output where Ctx: Context, { @@ -16,14 +16,14 @@ where Decision(RoundValue), // Decide the value. } -impl Message { +impl Output { pub fn proposal( height: Ctx::Height, round: Round, value: Ctx::Value, pol_round: Round, ) -> Self { - Message::Proposal(Ctx::new_proposal(height, round, value, pol_round)) + Output::Proposal(Ctx::new_proposal(height, round, value, pol_round)) } pub fn prevote( @@ -32,7 +32,7 @@ impl Message { value_id: Option>, address: Ctx::Address, ) -> Self { - Message::Vote(Ctx::new_prevote(height, round, value_id, address)) + Output::Vote(Ctx::new_prevote(height, round, value_id, address)) } pub fn precommit( @@ -41,19 +41,19 @@ impl Message { value_id: Option>, address: Ctx::Address, ) -> Self { - Message::Vote(Ctx::new_precommit(height, round, value_id, address)) + Output::Vote(Ctx::new_precommit(height, round, value_id, address)) } pub fn schedule_timeout(round: Round, step: TimeoutStep) -> Self { - Message::ScheduleTimeout(Timeout { round, step }) + Output::ScheduleTimeout(Timeout { round, step }) } pub fn get_value_and_schedule_timeout(round: Round, step: TimeoutStep) -> Self { - Message::GetValueAndScheduleTimeout(round, Timeout { round, step }) + Output::GetValueAndScheduleTimeout(round, Timeout { round, step }) } pub fn decision(round: Round, value: Ctx::Value) -> Self { - Message::Decision(RoundValue { round, value }) + Output::Decision(RoundValue { round, value }) } } @@ -61,55 +61,55 @@ impl Message { // the compiler would infer a Clone/Debug/PartialEq/Eq bound on `Ctx`, // which may not hold for all contexts. -impl Clone for Message { +impl Clone for Output { #[cfg_attr(coverage_nightly, coverage(off))] fn clone(&self) -> Self { match self { - Message::NewRound(round) => Message::NewRound(*round), - Message::Proposal(proposal) => Message::Proposal(proposal.clone()), - Message::Vote(vote) => Message::Vote(vote.clone()), - Message::ScheduleTimeout(timeout) => Message::ScheduleTimeout(*timeout), - Message::GetValueAndScheduleTimeout(round, timeout) => { - Message::GetValueAndScheduleTimeout(*round, *timeout) + Output::NewRound(round) => Output::NewRound(*round), + Output::Proposal(proposal) => Output::Proposal(proposal.clone()), + Output::Vote(vote) => Output::Vote(vote.clone()), + Output::ScheduleTimeout(timeout) => Output::ScheduleTimeout(*timeout), + Output::GetValueAndScheduleTimeout(round, timeout) => { + Output::GetValueAndScheduleTimeout(*round, *timeout) } - Message::Decision(round_value) => Message::Decision(round_value.clone()), + Output::Decision(round_value) => Output::Decision(round_value.clone()), } } } -impl fmt::Debug for Message { +impl fmt::Debug for Output { #[cfg_attr(coverage_nightly, coverage(off))] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Message::NewRound(round) => write!(f, "NewRound({:?})", round), - Message::Proposal(proposal) => write!(f, "Proposal({:?})", proposal), - Message::Vote(vote) => write!(f, "Vote({:?})", vote), - Message::ScheduleTimeout(timeout) => write!(f, "ScheduleTimeout({:?})", timeout), - Message::GetValueAndScheduleTimeout(round, timeout) => { + Output::NewRound(round) => write!(f, "NewRound({:?})", round), + Output::Proposal(proposal) => write!(f, "Proposal({:?})", proposal), + Output::Vote(vote) => write!(f, "Vote({:?})", vote), + Output::ScheduleTimeout(timeout) => write!(f, "ScheduleTimeout({:?})", timeout), + Output::GetValueAndScheduleTimeout(round, timeout) => { write!(f, "GetValueAndScheduleTimeout({:?}, {:?})", round, timeout) } - Message::Decision(round_value) => write!(f, "Decision({:?})", round_value), + Output::Decision(round_value) => write!(f, "Decision({:?})", round_value), } } } -impl PartialEq for Message { +impl PartialEq for Output { #[cfg_attr(coverage_nightly, coverage(off))] fn eq(&self, other: &Self) -> bool { match (self, other) { - (Message::NewRound(round), Message::NewRound(other_round)) => round == other_round, - (Message::Proposal(proposal), Message::Proposal(other_proposal)) => { + (Output::NewRound(round), Output::NewRound(other_round)) => round == other_round, + (Output::Proposal(proposal), Output::Proposal(other_proposal)) => { proposal == other_proposal } - (Message::Vote(vote), Message::Vote(other_vote)) => vote == other_vote, - (Message::ScheduleTimeout(timeout), Message::ScheduleTimeout(other_timeout)) => { + (Output::Vote(vote), Output::Vote(other_vote)) => vote == other_vote, + (Output::ScheduleTimeout(timeout), Output::ScheduleTimeout(other_timeout)) => { timeout == other_timeout } ( - Message::GetValueAndScheduleTimeout(round, timeout), - Message::GetValueAndScheduleTimeout(other_round, other_timeout), + Output::GetValueAndScheduleTimeout(round, timeout), + Output::GetValueAndScheduleTimeout(other_round, other_timeout), ) => round == other_round && timeout == other_timeout, - (Message::Decision(round_value), Message::Decision(other_round_value)) => { + (Output::Decision(round_value), Output::Decision(other_round_value)) => { round_value == other_round_value } _ => false, @@ -117,4 +117,4 @@ impl PartialEq for Message { } } -impl Eq for Message {} +impl Eq for Output {} diff --git a/Code/round/src/state.rs b/Code/round/src/state.rs index 200c70450..27501cebd 100644 --- a/Code/round/src/state.rs +++ b/Code/round/src/state.rs @@ -1,6 +1,6 @@ use core::fmt; -use crate::events::Event; +use crate::input::Input; use crate::state_machine::Info; use crate::transition::Transition; @@ -76,8 +76,8 @@ where } } - pub fn apply_event(self, data: &Info, event: Event) -> Transition { - crate::state_machine::apply_event(self, data, event) + pub fn apply(self, data: &Info, input: Input) -> Transition { + crate::state_machine::apply(self, data, input) } } diff --git a/Code/round/src/state_machine.rs b/Code/round/src/state_machine.rs index a34346f60..77b7b6162 100644 --- a/Code/round/src/state_machine.rs +++ b/Code/round/src/state_machine.rs @@ -1,19 +1,19 @@ use malachite_common::{Context, Proposal, Round, TimeoutStep, Value}; -use crate::events::Event; -use crate::message::Message; +use crate::input::Input; +use crate::output::Output; use crate::state::{State, Step}; use crate::transition::Transition; -/// Immutable information about the event and our node: +/// Immutable information about the input and our node: /// - Address of our node /// - Proposer for the round we are at -/// - Round for which the event is for, can be different than the round we are at +/// - Round for which the input is for, can be different than the round we are at pub struct Info<'a, Ctx> where Ctx: Context, { - pub event_round: Round, + pub input_round: Round, pub address: &'a Ctx::Address, pub proposer: &'a Ctx::Address, } @@ -22,9 +22,9 @@ impl<'a, Ctx> Info<'a, Ctx> where Ctx: Context, { - pub fn new(event_round: Round, address: &'a Ctx::Address, proposer: &'a Ctx::Address) -> Self { + pub fn new(input_round: Round, address: &'a Ctx::Address, proposer: &'a Ctx::Address) -> Self { Self { - event_round, + input_round, address, proposer, } @@ -43,38 +43,38 @@ where pol_round.is_defined() && pol_round < state.round } -/// Apply an event to the current state at the current round. +/// Apply an input to the current state at the current round. /// -/// This function takes the current state and round, and an event, +/// This function takes the current state and round, and an input, /// and returns the next state and an optional message for the driver to act on. /// -/// Valid transitions result in at least a change to the state and/or an output message. +/// Valid transitions result in at least a change to the state and/or an output. /// /// Commented numbers refer to line numbers in the spec paper. -pub fn apply_event(state: State, info: &Info, event: Event) -> Transition +pub fn apply(state: State, info: &Info, input: Input) -> Transition where Ctx: Context, { - let this_round = state.round == info.event_round; + let this_round = state.round == info.input_round; - match (state.step, event) { - // From NewRound. Event must be for current round. + match (state.step, input) { + // From NewRound. Input must be for current round. // We are the proposer - (Step::NewRound, Event::NewRound) if this_round && info.is_proposer() => { + (Step::NewRound, Input::NewRound) if this_round && info.is_proposer() => { propose_valid_or_get_value(state) // L18 } // We are not the proposer - (Step::NewRound, Event::NewRound) if this_round => schedule_timeout_propose(state), // L11/L20 + (Step::NewRound, Input::NewRound) if this_round => schedule_timeout_propose(state), // L11/L20 - // From Propose. Event must be for current round. - (Step::Propose, Event::ProposeValue(value)) if this_round => { + // From Propose. Input must be for current round. + (Step::Propose, Input::ProposeValue(value)) if this_round => { debug_assert!(info.is_proposer()); propose(state, value) // L11/L14 } // L22 with valid proposal - (Step::Propose, Event::Proposal(proposal)) + (Step::Propose, Input::Proposal(proposal)) if this_round && proposal.pol_round().is_nil() => { if state @@ -89,7 +89,7 @@ where } // L28 with valid proposal - (Step::Propose, Event::ProposalAndPolkaPrevious(proposal)) + (Step::Propose, Input::ProposalAndPolkaPrevious(proposal)) if this_round && is_valid_pol_round(&state, proposal.pol_round()) => { let Some(locked) = state.locked.as_ref() else { @@ -104,32 +104,32 @@ where } // L28 with invalid proposal - (Step::Propose, Event::InvalidProposalAndPolkaPrevious(proposal)) + (Step::Propose, Input::InvalidProposalAndPolkaPrevious(proposal)) if this_round && is_valid_pol_round(&state, proposal.pol_round()) => { prevote_nil(state, info.address) } - (Step::Propose, Event::InvalidProposal) if this_round => prevote_nil(state, info.address), // L22/L25, L28/L31 + (Step::Propose, Input::InvalidProposal) if this_round => prevote_nil(state, info.address), // L22/L25, L28/L31 // We are the proposer. - (Step::Propose, Event::TimeoutPropose) if this_round && info.is_proposer() => { + (Step::Propose, Input::TimeoutPropose) if this_round && info.is_proposer() => { // TODO: Do we need to do something else here? prevote_nil(state, info.address) // L57 } // We are not the proposer. - (Step::Propose, Event::TimeoutPropose) if this_round => prevote_nil(state, info.address), // L57 + (Step::Propose, Input::TimeoutPropose) if this_round => prevote_nil(state, info.address), // L57 - // From Prevote. Event must be for current round. - (Step::Prevote, Event::PolkaAny) if this_round => schedule_timeout_prevote(state), // L34 - (Step::Prevote, Event::PolkaNil) if this_round => precommit_nil(state, info.address), // L44 - (Step::Prevote, Event::ProposalAndPolkaCurrent(proposal)) if this_round => { + // From Prevote. Input must be for current round. + (Step::Prevote, Input::PolkaAny) if this_round => schedule_timeout_prevote(state), // L34 + (Step::Prevote, Input::PolkaNil) if this_round => precommit_nil(state, info.address), // L44 + (Step::Prevote, Input::ProposalAndPolkaCurrent(proposal)) if this_round => { precommit(state, info.address, proposal) // L36/L37 - NOTE: only once? } - (Step::Prevote, Event::TimeoutPrevote) if this_round => precommit_nil(state, info.address), // L61 + (Step::Prevote, Input::TimeoutPrevote) if this_round => precommit_nil(state, info.address), // L61 - // From Precommit. Event must be for current round. - (Step::Precommit, Event::ProposalAndPolkaCurrent(proposal)) if this_round => { + // From Precommit. Input must be for current round. + (Step::Precommit, Input::ProposalAndPolkaCurrent(proposal)) if this_round => { set_valid_value(state, &proposal) // L36/L42 - NOTE: only once? } @@ -137,13 +137,13 @@ where (Step::Commit, _) => Transition::invalid(state), // From all (except Commit). Various round guards. - (_, Event::PrecommitAny) if this_round => schedule_timeout_precommit(state), // L47 - (_, Event::TimeoutPrecommit) if this_round => { - round_skip(state, info.event_round.increment()) + (_, Input::PrecommitAny) if this_round => schedule_timeout_precommit(state), // L47 + (_, Input::TimeoutPrecommit) if this_round => { + round_skip(state, info.input_round.increment()) } // L65 - (_, Event::SkipRound(round)) if state.round < round => round_skip(state, round), // L55 - (_, Event::ProposalAndPrecommitValue(proposal)) => { - commit(state, info.event_round, proposal) + (_, Input::SkipRound(round)) if state.round < round => round_skip(state, round), // L55 + (_, Input::ProposalAndPrecommitValue(proposal)) => { + commit(state, info.input_round, proposal) } // L49 // Invalid transition. @@ -166,18 +166,17 @@ where match &state.valid { Some(round_value) => { let pol_round = round_value.round; - let proposal = Message::proposal( + let proposal = Output::proposal( state.height.clone(), state.round, round_value.value.clone(), pol_round, ); - Transition::to(state.with_step(Step::Propose)).with_message(proposal) + Transition::to(state.with_step(Step::Propose)).with_output(proposal) } None => { - let timeout = - Message::get_value_and_schedule_timeout(state.round, TimeoutStep::Propose); - Transition::to(state.with_step(Step::Propose)).with_message(timeout) + let timeout = Output::get_value_and_schedule_timeout(state.round, TimeoutStep::Propose); + Transition::to(state.with_step(Step::Propose)).with_output(timeout) } } } @@ -190,8 +189,8 @@ pub fn propose(state: State, value: Ctx::Value) -> Transition where Ctx: Context, { - let proposal = Message::proposal(state.height.clone(), state.round, value, Round::Nil); - Transition::to(state.with_step(Step::Propose)).with_message(proposal) + let proposal = Output::proposal(state.height.clone(), state.round, value, Round::Nil); + Transition::to(state.with_step(Step::Propose)).with_output(proposal) } //--------------------------------------------------------------------- @@ -219,9 +218,9 @@ where None => Some(proposed), // not locked, prevote the value }; - let message = Message::prevote(state.height.clone(), state.round, value, address.clone()); + let output = Output::prevote(state.height.clone(), state.round, value, address.clone()); state.proposal = Some(proposal.clone()); - Transition::to(state.with_step(Step::Prevote)).with_message(message) + Transition::to(state.with_step(Step::Prevote)).with_output(output) } /// Received a complete proposal for an empty or invalid value, or timed out; prevote nil. @@ -231,8 +230,8 @@ pub fn prevote_nil(state: State, address: &Ctx::Address) -> Transition where Ctx: Context, { - let message = Message::prevote(state.height.clone(), state.round, None, address.clone()); - Transition::to(state.with_step(Step::Prevote)).with_message(message) + let output = Output::prevote(state.height.clone(), state.round, None, address.clone()); + Transition::to(state.with_step(Step::Prevote)).with_output(output) } // --------------------------------------------------------------------- @@ -258,7 +257,7 @@ where } let value = proposal.value(); - let message = Message::precommit( + let output = Output::precommit( state.height.clone(), state.round, Some(value.id()), @@ -280,7 +279,7 @@ where .set_valid(value.clone()) .with_step(Step::Precommit); - Transition::to(next).with_message(message) + Transition::to(next).with_output(output) } /// Received a polka for nil or timed out of prevote; precommit nil. @@ -290,8 +289,8 @@ pub fn precommit_nil(state: State, address: &Ctx::Address) -> Transiti where Ctx: Context, { - let message = Message::precommit(state.height.clone(), state.round, None, address.clone()); - Transition::to(state.with_step(Step::Precommit)).with_message(message) + let output = Output::precommit(state.height.clone(), state.round, None, address.clone()); + Transition::to(state.with_step(Step::Precommit)).with_output(output) } // --------------------------------------------------------------------- @@ -305,8 +304,8 @@ pub fn schedule_timeout_propose(state: State) -> Transition where Ctx: Context, { - let timeout = Message::schedule_timeout(state.round, TimeoutStep::Propose); - Transition::to(state.with_step(Step::Propose)).with_message(timeout) + let timeout = Output::schedule_timeout(state.round, TimeoutStep::Propose); + Transition::to(state.with_step(Step::Propose)).with_output(timeout) } /// We received a polka for any; schedule timeout prevote. @@ -320,8 +319,8 @@ where Ctx: Context, { if state.step == Step::Prevote { - let message = Message::schedule_timeout(state.round, TimeoutStep::Prevote); - Transition::to(state).with_message(message) + let output = Output::schedule_timeout(state.round, TimeoutStep::Prevote); + Transition::to(state).with_output(output) } else { Transition::to(state) } @@ -334,8 +333,8 @@ pub fn schedule_timeout_precommit(state: State) -> Transition where Ctx: Context, { - let message = Message::schedule_timeout(state.round, TimeoutStep::Precommit); - Transition::to(state).with_message(message) + let output = Output::schedule_timeout(state.round, TimeoutStep::Precommit); + Transition::to(state).with_output(output) } //--------------------------------------------------------------------- @@ -374,7 +373,7 @@ where ..state }; - Transition::to(new_state).with_message(Message::NewRound(round)) + Transition::to(new_state).with_output(Output::NewRound(round)) } /// We received +2/3 precommits for a value - commit and decide that value! @@ -384,6 +383,6 @@ pub fn commit(state: State, round: Round, proposal: Ctx::Proposal) -> where Ctx: Context, { - let message = Message::decision(round, proposal.value().clone()); - Transition::to(state.with_step(Step::Commit)).with_message(message) + let output = Output::decision(round, proposal.value().clone()); + Transition::to(state.with_step(Step::Commit)).with_output(output) } diff --git a/Code/round/src/transition.rs b/Code/round/src/transition.rs index 33bed24fb..5d28df5b0 100644 --- a/Code/round/src/transition.rs +++ b/Code/round/src/transition.rs @@ -1,6 +1,6 @@ use malachite_common::Context; -use crate::message::Message; +use crate::output::Output; use crate::state::State; #[derive(Clone, Debug, PartialEq, Eq)] @@ -9,7 +9,7 @@ where Ctx: Context, { pub next_state: State, - pub message: Option>, + pub output: Option>, pub valid: bool, } @@ -20,7 +20,7 @@ where pub fn to(next_state: State) -> Self { Self { next_state, - message: None, + output: None, valid: true, } } @@ -28,13 +28,13 @@ where pub fn invalid(next_state: State) -> Self { Self { next_state, - message: None, + output: None, valid: false, } } - pub fn with_message(mut self, message: Message) -> Self { - self.message = Some(message); + pub fn with_output(mut self, output: Output) -> Self { + self.output = Some(output); self } } diff --git a/Code/test/src/utils.rs b/Code/test/src/utils.rs index e94d9458b..d4450f206 100644 --- a/Code/test/src/utils.rs +++ b/Code/test/src/utils.rs @@ -2,7 +2,7 @@ use rand::rngs::StdRng; use rand::SeedableRng; use malachite_common::{Round, Timeout, VotingPower}; -use malachite_driver::{Event, Message, ProposerSelector, Validity}; +use malachite_driver::{Input, Output, ProposerSelector, Validity}; use malachite_round::state::{RoundValue, State, Step}; use crate::{ @@ -52,117 +52,121 @@ pub fn make_validators( validators.try_into().expect("N validators") } -pub fn new_round_event(round: Round) -> Event { - Event::NewRound(Height::new(1), round) +pub fn new_round_input(round: Round) -> Input { + Input::NewRound(Height::new(1), round) } -pub fn new_round_msg(round: Round) -> Option> { - Some(Message::NewRound(Height::new(1), round)) +pub fn new_round_output(round: Round) -> Option> { + Some(Output::NewRound(Height::new(1), round)) } -pub fn proposal_msg( +pub fn proposal_output( round: Round, value: Value, locked_round: Round, -) -> Option> { +) -> Option> { let proposal = Proposal::new(Height::new(1), round, value, locked_round); - Some(Message::Propose(proposal)) + Some(Output::Propose(proposal)) } -pub fn proposal_event( +pub fn proposal_input( round: Round, value: Value, locked_round: Round, validity: Validity, -) -> Event { +) -> Input { let proposal = Proposal::new(Height::new(1), round, value, locked_round); - Event::Proposal(proposal, validity) + Input::Proposal(proposal, validity) } -pub fn prevote_msg(round: Round, addr: &Address, sk: &PrivateKey) -> Option> { +pub fn prevote_output( + round: Round, + addr: &Address, + sk: &PrivateKey, +) -> Option> { let value = Value::new(9999); - Some(Message::Vote( + Some(Output::Vote( Vote::new_prevote(Height::new(1), round, Some(value.id()), *addr).signed(sk), )) } -pub fn prevote_nil_msg( +pub fn prevote_nil_output( round: Round, addr: &Address, sk: &PrivateKey, -) -> Option> { - Some(Message::Vote( +) -> Option> { + Some(Output::Vote( Vote::new_prevote(Height::new(1), round, None, *addr).signed(sk), )) } -pub fn prevote_event(addr: &Address, sk: &PrivateKey) -> Event { +pub fn prevote_input(addr: &Address, sk: &PrivateKey) -> Input { let value = Value::new(9999); - Event::Vote( + Input::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), *addr).signed(sk), ) } -pub fn prevote_event_at(round: Round, addr: &Address, sk: &PrivateKey) -> Event { +pub fn prevote_input_at(round: Round, addr: &Address, sk: &PrivateKey) -> Input { let value = Value::new(9999); - Event::Vote(Vote::new_prevote(Height::new(1), round, Some(value.id()), *addr).signed(sk)) + Input::Vote(Vote::new_prevote(Height::new(1), round, Some(value.id()), *addr).signed(sk)) } -pub fn precommit_msg( +pub fn precommit_output( round: Round, value: Value, addr: &Address, sk: &PrivateKey, -) -> Option> { - Some(Message::Vote( +) -> Option> { + Some(Output::Vote( Vote::new_precommit(Height::new(1), round, Some(value.id()), *addr).signed(sk), )) } -pub fn precommit_nil_msg(addr: &Address, sk: &PrivateKey) -> Option> { - Some(Message::Vote( +pub fn precommit_nil_output(addr: &Address, sk: &PrivateKey) -> Option> { + Some(Output::Vote( Vote::new_precommit(Height::new(1), Round::new(0), None, *addr).signed(sk), )) } -pub fn precommit_event( +pub fn precommit_input( round: Round, value: Value, addr: &Address, sk: &PrivateKey, -) -> Event { - Event::Vote(Vote::new_precommit(Height::new(1), round, Some(value.id()), *addr).signed(sk)) +) -> Input { + Input::Vote(Vote::new_precommit(Height::new(1), round, Some(value.id()), *addr).signed(sk)) } -pub fn decide_message(round: Round, value: Value) -> Option> { - Some(Message::Decide(round, value)) +pub fn decide_output(round: Round, value: Value) -> Option> { + Some(Output::Decide(round, value)) } -pub fn start_propose_timer_msg(round: Round) -> Option> { - Some(Message::ScheduleTimeout(Timeout::propose(round))) +pub fn start_propose_timer_output(round: Round) -> Option> { + Some(Output::ScheduleTimeout(Timeout::propose(round))) } -pub fn timeout_propose_event(round: Round) -> Event { - Event::TimeoutElapsed(Timeout::propose(round)) +pub fn timeout_propose_input(round: Round) -> Input { + Input::TimeoutElapsed(Timeout::propose(round)) } -pub fn start_prevote_timer_msg(round: Round) -> Option> { - Some(Message::ScheduleTimeout(Timeout::prevote(round))) +pub fn start_prevote_timer_output(round: Round) -> Option> { + Some(Output::ScheduleTimeout(Timeout::prevote(round))) } -pub fn timeout_prevote_event(round: Round) -> Event { - Event::TimeoutElapsed(Timeout::prevote(round)) +pub fn timeout_prevote_input(round: Round) -> Input { + Input::TimeoutElapsed(Timeout::prevote(round)) } -pub fn start_precommit_timer_msg(round: Round) -> Option> { - Some(Message::ScheduleTimeout(Timeout::precommit(round))) +pub fn start_precommit_timer_output(round: Round) -> Option> { + Some(Output::ScheduleTimeout(Timeout::precommit(round))) } -pub fn timeout_precommit_event(round: Round) -> Event { - Event::TimeoutElapsed(Timeout::precommit(round)) +pub fn timeout_precommit_input(round: Round) -> Input { + Input::TimeoutElapsed(Timeout::precommit(round)) } pub fn propose_state(round: Round) -> State { diff --git a/Code/test/tests/driver.rs b/Code/test/tests/driver.rs index c230b4350..62d4ff454 100644 --- a/Code/test/tests/driver.rs +++ b/Code/test/tests/driver.rs @@ -2,27 +2,27 @@ use futures::executor::block_on; use malachite_test::utils::{make_validators, FixedProposer, RotateProposer}; use malachite_common::{Round, Timeout, TimeoutStep}; -use malachite_driver::{Driver, Error, Event, Message, Validity}; +use malachite_driver::{Driver, Error, Input, Output, Validity}; use malachite_round::state::{RoundValue, State, Step}; use malachite_test::{Height, Proposal, TestContext, ValidatorSet, Value, Vote}; pub struct TestStep { desc: &'static str, - input_event: Option>, - expected_output: Option>, + input: Option>, + expected_output: Option>, expected_round: Round, new_state: State, } -pub fn msg_to_event(output: Message) -> Option> { +pub fn output_to_input(output: Output) -> Option> { match output { - Message::NewRound(height, round) => Some(Event::NewRound(height, round)), + Output::NewRound(height, round) => Some(Input::NewRound(height, round)), // Let's consider our own proposal to always be valid - Message::Propose(p) => Some(Event::Proposal(p, Validity::Valid)), - Message::Vote(v) => Some(Event::Vote(v)), - Message::Decide(_, _) => None, - Message::ScheduleTimeout(_) => None, - Message::GetValueAndScheduleTimeout(_, _) => None, + Output::Propose(p) => Some(Input::Proposal(p, Validity::Valid)), + Output::Vote(v) => Some(Input::Vote(v)), + Output::Decide(_, _) => None, + Output::ScheduleTimeout(_) => None, + Output::GetValueAndScheduleTimeout(_, _) => None, } } @@ -44,8 +44,8 @@ fn driver_steps_proposer() { let steps = vec![ TestStep { desc: "Start round 0, we are proposer, ask for a value to propose", - input_event: Some(Event::NewRound(Height::new(1), Round::new(0))), - expected_output: Some(Message::GetValueAndScheduleTimeout( + input: Some(Input::NewRound(Height::new(1), Round::new(0))), + expected_output: Some(Output::GetValueAndScheduleTimeout( Round::new(0), Timeout::new(Round::new(0), TimeoutStep::Propose), )), @@ -61,8 +61,8 @@ fn driver_steps_proposer() { }, TestStep { desc: "Feed a value to propose, propose that value", - input_event: Some(Event::ProposeValue(Round::new(0), value)), - expected_output: Some(Message::Propose(proposal.clone())), + input: Some(Input::ProposeValue(Round::new(0), value)), + expected_output: Some(Output::Propose(proposal.clone())), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -75,8 +75,8 @@ fn driver_steps_proposer() { }, TestStep { desc: "Receive our own proposal, prevote for it (v1)", - input_event: None, - expected_output: Some(Message::Vote( + input: None, + expected_output: Some(Output::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), my_addr) .signed(&my_sk), )), @@ -92,7 +92,7 @@ fn driver_steps_proposer() { }, TestStep { desc: "Receive our own prevote v1", - input_event: None, + input: None, expected_output: None, expected_round: Round::new(0), new_state: State { @@ -106,7 +106,7 @@ fn driver_steps_proposer() { }, TestStep { desc: "v2 prevotes for our proposal", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), v2.address) .signed(&sk2), )), @@ -123,11 +123,11 @@ fn driver_steps_proposer() { }, TestStep { desc: "v3 prevotes for our proposal, we get +2/3 prevotes, precommit for it (v1)", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), v3.address) .signed(&sk3), )), - expected_output: Some(Message::Vote( + expected_output: Some(Output::Vote( Vote::new_precommit(Height::new(1), Round::new(0), Some(value.id()), my_addr) .signed(&my_sk), )), @@ -149,7 +149,7 @@ fn driver_steps_proposer() { }, TestStep { desc: "v1 receives its own precommit", - input_event: None, + input: None, expected_output: None, expected_round: Round::new(0), new_state: State { @@ -169,7 +169,7 @@ fn driver_steps_proposer() { }, TestStep { desc: "v2 precommits for our proposal", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_precommit(Height::new(1), Round::new(0), Some(value.id()), v2.address) .signed(&sk2), )), @@ -192,11 +192,11 @@ fn driver_steps_proposer() { }, TestStep { desc: "v3 precommits for our proposal, we get +2/3 precommits, decide it (v1)", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_precommit(Height::new(1), Round::new(0), Some(value.id()), v3.address) .signed(&sk3), )), - expected_output: Some(Message::Decide(Round::new(0), value)), + expected_output: Some(Output::Decide(Round::new(0), value)), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -215,17 +215,17 @@ fn driver_steps_proposer() { }, ]; - let mut event_from_previous_msg = None; + let mut output_from_prev_input = None; for step in steps { println!("Step: {}", step.desc); - let execute_event = step - .input_event - .unwrap_or_else(|| event_from_previous_msg.unwrap()); + let input = step + .input + .unwrap_or_else(|| output_from_prev_input.unwrap()); - let output = block_on(driver.execute(execute_event)).expect("execute succeeded"); - assert_eq!(output, step.expected_output, "expected output message"); + let output = block_on(driver.execute(input)).expect("execute succeeded"); + assert_eq!(output, step.expected_output, "expected output"); assert_eq!( driver.round_state.round, step.expected_round, @@ -234,7 +234,7 @@ fn driver_steps_proposer() { assert_eq!(driver.round_state, step.new_state, "expected state"); - event_from_previous_msg = output.and_then(msg_to_event); + output_from_prev_input = output.and_then(output_to_input); } } @@ -252,8 +252,8 @@ fn driver_steps_proposer_timeout_get_value() { let steps = vec![ TestStep { desc: "Start round 0, we are proposer, ask for a value to propose", - input_event: Some(Event::NewRound(Height::new(1), Round::new(0))), - expected_output: Some(Message::GetValueAndScheduleTimeout( + input: Some(Input::NewRound(Height::new(1), Round::new(0))), + expected_output: Some(Output::GetValueAndScheduleTimeout( Round::new(0), Timeout::new(Round::new(0), TimeoutStep::Propose), )), @@ -269,8 +269,8 @@ fn driver_steps_proposer_timeout_get_value() { }, TestStep { desc: "Receive a propose timeout", - input_event: Some(Event::TimeoutElapsed(Timeout::propose(Round::new(0)))), - expected_output: Some(Message::Vote( + input: Some(Input::TimeoutElapsed(Timeout::propose(Round::new(0)))), + expected_output: Some(Output::Vote( Vote::new_prevote(Height::new(1), Round::new(0), None, my_addr).signed(&my_sk), )), expected_round: Round::new(0), @@ -285,17 +285,17 @@ fn driver_steps_proposer_timeout_get_value() { }, ]; - let mut event_from_previous_msg = None; + let mut output_from_prev_input = None; for step in steps { println!("Step: {}", step.desc); - let execute_event = step - .input_event - .unwrap_or_else(|| event_from_previous_msg.unwrap()); + let input = step + .input + .unwrap_or_else(|| output_from_prev_input.unwrap()); - let output = block_on(driver.execute(execute_event)).expect("execute succeeded"); - assert_eq!(output, step.expected_output, "expected output message"); + let output = block_on(driver.execute(input)).expect("execute succeeded"); + assert_eq!(output, step.expected_output, "expected output"); assert_eq!( driver.round_state.round, step.expected_round, @@ -304,7 +304,7 @@ fn driver_steps_proposer_timeout_get_value() { assert_eq!(driver.round_state, step.new_state, "expected state"); - event_from_previous_msg = output.and_then(msg_to_event); + output_from_prev_input = output.and_then(output_to_input); } } @@ -328,8 +328,8 @@ fn driver_steps_not_proposer_valid() { let steps = vec![ TestStep { desc: "Start round 0, we are not the proposer", - input_event: Some(Event::NewRound(Height::new(1), Round::new(0))), - expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), + input: Some(Input::NewRound(Height::new(1), Round::new(0))), + expected_output: Some(Output::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -342,8 +342,8 @@ fn driver_steps_not_proposer_valid() { }, TestStep { desc: "Receive a proposal, prevote for it (v2)", - input_event: Some(Event::Proposal(proposal.clone(), Validity::Valid)), - expected_output: Some(Message::Vote( + input: Some(Input::Proposal(proposal.clone(), Validity::Valid)), + expected_output: Some(Output::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), my_addr) .signed(&my_sk), )), @@ -359,7 +359,7 @@ fn driver_steps_not_proposer_valid() { }, TestStep { desc: "Receive our own prevote (v2)", - input_event: None, + input: None, expected_output: None, expected_round: Round::new(0), new_state: State { @@ -373,7 +373,7 @@ fn driver_steps_not_proposer_valid() { }, TestStep { desc: "v1 prevotes for its own proposal", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), v1.address) .signed(&sk1), )), @@ -390,11 +390,11 @@ fn driver_steps_not_proposer_valid() { }, TestStep { desc: "v3 prevotes for v1's proposal, it gets +2/3 prevotes, precommit for it (v2)", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), v3.address) .signed(&sk3), )), - expected_output: Some(Message::Vote( + expected_output: Some(Output::Vote( Vote::new_precommit(Height::new(1), Round::new(0), Some(value.id()), my_addr) .signed(&my_sk), )), @@ -416,7 +416,7 @@ fn driver_steps_not_proposer_valid() { }, TestStep { desc: "we receive our own precommit", - input_event: None, + input: None, expected_output: None, expected_round: Round::new(0), new_state: State { @@ -436,7 +436,7 @@ fn driver_steps_not_proposer_valid() { }, TestStep { desc: "v1 precommits its proposal", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_precommit(Height::new(1), Round::new(0), Some(value.id()), v1.address) .signed(&sk1), )), @@ -459,11 +459,11 @@ fn driver_steps_not_proposer_valid() { }, TestStep { desc: "v3 precommits for v1's proposal, it gets +2/3 precommits, decide it", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_precommit(Height::new(1), Round::new(0), Some(value.id()), v3.address) .signed(&sk3), )), - expected_output: Some(Message::Decide(Round::new(0), value)), + expected_output: Some(Output::Decide(Round::new(0), value)), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -482,17 +482,17 @@ fn driver_steps_not_proposer_valid() { }, ]; - let mut event_from_previous_msg = None; + let mut output_from_prev_input = None; for step in steps { println!("Step: {}", step.desc); - let execute_event = step - .input_event - .unwrap_or_else(|| event_from_previous_msg.unwrap()); + let input = step + .input + .unwrap_or_else(|| output_from_prev_input.unwrap()); - let output = block_on(driver.execute(execute_event)).expect("execute succeeded"); - assert_eq!(output, step.expected_output, "expected output message"); + let output = block_on(driver.execute(input)).expect("execute succeeded"); + assert_eq!(output, step.expected_output, "expected output"); assert_eq!( driver.round_state.round, step.expected_round, @@ -501,7 +501,7 @@ fn driver_steps_not_proposer_valid() { assert_eq!(driver.round_state, step.new_state, "expected state"); - event_from_previous_msg = output.and_then(msg_to_event); + output_from_prev_input = output.and_then(output_to_input); } } @@ -525,8 +525,8 @@ fn driver_steps_not_proposer_invalid() { let steps = vec![ TestStep { desc: "Start round 0, we are not the proposer", - input_event: Some(Event::NewRound(Height::new(1), Round::new(0))), - expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), + input: Some(Input::NewRound(Height::new(1), Round::new(0))), + expected_output: Some(Output::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -539,8 +539,8 @@ fn driver_steps_not_proposer_invalid() { }, TestStep { desc: "Receive an invalid proposal, prevote for nil (v2)", - input_event: Some(Event::Proposal(proposal.clone(), Validity::Invalid)), - expected_output: Some(Message::Vote( + input: Some(Input::Proposal(proposal.clone(), Validity::Invalid)), + expected_output: Some(Output::Vote( Vote::new_prevote(Height::new(1),Round::new(0), None, my_addr).signed(&my_sk), )), expected_round: Round::new(0), @@ -555,7 +555,7 @@ fn driver_steps_not_proposer_invalid() { }, TestStep { desc: "Receive our own prevote (v2)", - input_event: None, + input: None, expected_output: None, expected_round: Round::new(0), new_state: State { @@ -569,7 +569,7 @@ fn driver_steps_not_proposer_invalid() { }, TestStep { desc: "v1 prevotes for its own proposal", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), v1.address).signed(&sk1), )), expected_output: None, @@ -585,10 +585,10 @@ fn driver_steps_not_proposer_invalid() { }, TestStep { desc: "v3 prevotes for v1's proposal, we have polka for any, schedule prevote timeout (v2)", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), v3.address).signed(&sk3), )), - expected_output: Some(Message::ScheduleTimeout(Timeout::prevote(Round::new(0)))), + expected_output: Some(Output::ScheduleTimeout(Timeout::prevote(Round::new(0)))), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -601,8 +601,8 @@ fn driver_steps_not_proposer_invalid() { }, TestStep { desc: "prevote timeout elapses, we precommit for nil (v2)", - input_event: Some(Event::TimeoutElapsed(Timeout::prevote(Round::new(0)))), - expected_output: Some(Message::Vote( + input: Some(Input::TimeoutElapsed(Timeout::prevote(Round::new(0)))), + expected_output: Some(Output::Vote( Vote::new_precommit(Height::new(1), Round::new(0), None, my_addr).signed(&my_sk), )), expected_round: Round::new(0), @@ -617,16 +617,16 @@ fn driver_steps_not_proposer_invalid() { }, ]; - let mut event_from_previous_msg = None; + let mut output_from_prev_input = None; for step in steps { println!("Step: {}", step.desc); - let execute_event = step - .input_event - .unwrap_or_else(|| event_from_previous_msg.unwrap()); + let input = step + .input + .unwrap_or_else(|| output_from_prev_input.unwrap()); - let output = block_on(driver.execute(execute_event)).expect("execute succeeded"); + let output = block_on(driver.execute(input)).expect("execute succeeded"); assert_eq!(output, step.expected_output, "expected output"); assert_eq!( @@ -636,7 +636,7 @@ fn driver_steps_not_proposer_invalid() { assert_eq!(driver.round_state, step.new_state, "expected state"); - event_from_previous_msg = output.and_then(msg_to_event); + output_from_prev_input = output.and_then(output_to_input); } } @@ -661,8 +661,8 @@ fn driver_steps_not_proposer_other_height() { let steps = vec![ TestStep { desc: "Start round 0, we are not the proposer", - input_event: Some(Event::NewRound(Height::new(1), Round::new(0))), - expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), + input: Some(Input::NewRound(Height::new(1), Round::new(0))), + expected_output: Some(Output::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -675,7 +675,7 @@ fn driver_steps_not_proposer_other_height() { }, TestStep { desc: "Receive a proposal for another height, ignore it (v2)", - input_event: Some(Event::Proposal(proposal.clone(), Validity::Invalid)), + input: Some(Input::Proposal(proposal.clone(), Validity::Invalid)), expected_output: None, expected_round: Round::new(0), new_state: State { @@ -689,16 +689,16 @@ fn driver_steps_not_proposer_other_height() { }, ]; - let mut event_from_previous_msg = None; + let mut output_from_prev_input = None; for step in steps { println!("Step: {}", step.desc); - let execute_event = step - .input_event - .unwrap_or_else(|| event_from_previous_msg.unwrap()); + let input = step + .input + .unwrap_or_else(|| output_from_prev_input.unwrap()); - let output = block_on(driver.execute(execute_event)).expect("execute succeeded"); + let output = block_on(driver.execute(input)).expect("execute succeeded"); assert_eq!(output, step.expected_output, "expected output"); assert_eq!( @@ -708,7 +708,7 @@ fn driver_steps_not_proposer_other_height() { assert_eq!(driver.round_state, step.new_state, "expected state"); - event_from_previous_msg = output.and_then(msg_to_event); + output_from_prev_input = output.and_then(output_to_input); } } @@ -733,8 +733,8 @@ fn driver_steps_not_proposer_other_round() { let steps = vec![ TestStep { desc: "Start round 0, we are not the proposer", - input_event: Some(Event::NewRound(Height::new(1), Round::new(0))), - expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), + input: Some(Input::NewRound(Height::new(1), Round::new(0))), + expected_output: Some(Output::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -747,7 +747,7 @@ fn driver_steps_not_proposer_other_round() { }, TestStep { desc: "Receive a proposal for another round, ignore it (v2)", - input_event: Some(Event::Proposal(proposal.clone(), Validity::Invalid)), + input: Some(Input::Proposal(proposal.clone(), Validity::Invalid)), expected_output: None, expected_round: Round::new(0), new_state: State { @@ -761,16 +761,16 @@ fn driver_steps_not_proposer_other_round() { }, ]; - let mut event_from_previous_msg = None; + let mut output_from_prev_input = None; for step in steps { println!("Step: {}", step.desc); - let execute_event = step - .input_event - .unwrap_or_else(|| event_from_previous_msg.unwrap()); + let input = step + .input + .unwrap_or_else(|| output_from_prev_input.unwrap()); - let output = block_on(driver.execute(execute_event)).expect("execute succeeded"); + let output = block_on(driver.execute(input)).expect("execute succeeded"); assert_eq!(output, step.expected_output, "expected output"); assert_eq!( @@ -780,7 +780,7 @@ fn driver_steps_not_proposer_other_round() { assert_eq!(driver.round_state, step.new_state, "expected state"); - event_from_previous_msg = output.and_then(msg_to_event); + output_from_prev_input = output.and_then(output_to_input); } } @@ -803,8 +803,8 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { // Start round 0, we, v3, are not the proposer TestStep { desc: "Start round 0, we, v3, are not the proposer", - input_event: Some(Event::NewRound(Height::new(1), Round::new(0))), - expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), + input: Some(Input::NewRound(Height::new(1), Round::new(0))), + expected_output: Some(Output::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -818,8 +818,8 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { // Receive a propose timeout, prevote for nil (from v3) TestStep { desc: "Receive a propose timeout, prevote for nil (v3)", - input_event: Some(Event::TimeoutElapsed(Timeout::propose(Round::new(0)))), - expected_output: Some(Message::Vote( + input: Some(Input::TimeoutElapsed(Timeout::propose(Round::new(0)))), + expected_output: Some(Output::Vote( Vote::new_prevote(Height::new(1), Round::new(0), None, my_addr).signed(&my_sk), )), expected_round: Round::new(0), @@ -835,7 +835,7 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { // Receive our own prevote v3 TestStep { desc: "Receive our own prevote v3", - input_event: None, + input: None, expected_output: None, expected_round: Round::new(0), new_state: State { @@ -850,7 +850,7 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { // v1 prevotes for its own proposal TestStep { desc: "v1 prevotes for its own proposal", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), v1.address) .signed(&sk1), )), @@ -868,10 +868,10 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { // v2 prevotes for nil, we get +2/3 nil prevotes and precommit for nil TestStep { desc: "v2 prevotes for nil, we get +2/3 prevotes, precommit for nil", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(Height::new(1), Round::new(0), None, v2.address).signed(&sk2), )), - expected_output: Some(Message::Vote( + expected_output: Some(Output::Vote( Vote::new_precommit(Height::new(1), Round::new(0), None, my_addr).signed(&my_sk), )), expected_round: Round::new(0), @@ -887,7 +887,7 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { // v3 receives its own precommit TestStep { desc: "v3 receives its own precommit", - input_event: None, + input: None, expected_output: None, expected_round: Round::new(0), new_state: State { @@ -902,7 +902,7 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { // v1 precommits its proposal TestStep { desc: "v1 precommits its proposal", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_precommit(Height::new(1), Round::new(0), Some(value.id()), v1.address) .signed(&sk1), )), @@ -920,10 +920,10 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { // v2 precommits for nil TestStep { desc: "v2 precommits for nil", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_precommit(Height::new(1), Round::new(0), None, v2.address).signed(&sk2), )), - expected_output: Some(Message::ScheduleTimeout(Timeout::precommit(Round::new(0)))), + expected_output: Some(Output::ScheduleTimeout(Timeout::precommit(Round::new(0)))), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -937,8 +937,8 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { // we receive a precommit timeout, start a new round TestStep { desc: "we receive a precommit timeout, start a new round", - input_event: Some(Event::TimeoutElapsed(Timeout::precommit(Round::new(0)))), - expected_output: Some(Message::NewRound(Height::new(1), Round::new(1))), + input: Some(Input::TimeoutElapsed(Timeout::precommit(Round::new(0)))), + expected_output: Some(Output::NewRound(Height::new(1), Round::new(1))), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -951,8 +951,8 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { }, TestStep { desc: "Start round 1, we are not the proposer", - input_event: Some(Event::NewRound(Height::new(1), Round::new(1))), - expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(1)))), + input: Some(Input::NewRound(Height::new(1), Round::new(1))), + expected_output: Some(Output::ScheduleTimeout(Timeout::propose(Round::new(1)))), expected_round: Round::new(1), new_state: State { height: Height::new(1), @@ -965,21 +965,21 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { }, ]; - let mut event_from_previous_msg = None; + let mut output_from_prev_input = None; for step in steps { println!("Step: {}", step.desc); - let execute_event = step - .input_event - .unwrap_or_else(|| event_from_previous_msg.unwrap()); + let input = step + .input + .unwrap_or_else(|| output_from_prev_input.unwrap()); - let output = block_on(driver.execute(execute_event)).expect("execute succeeded"); - assert_eq!(output, step.expected_output, "expected output message"); + let output = block_on(driver.execute(input)).expect("execute succeeded"); + assert_eq!(output, step.expected_output, "expected output"); assert_eq!(driver.round_state, step.new_state, "new state"); - event_from_previous_msg = output.and_then(msg_to_event); + output_from_prev_input = output.and_then(output_to_input); } } @@ -996,12 +996,12 @@ fn driver_steps_no_value_to_propose() { let mut driver = Driver::new(ctx, sel, vs, my_addr); - let output = block_on(driver.execute(Event::NewRound(Height::new(1), Round::new(0)))) + let output = block_on(driver.execute(Input::NewRound(Height::new(1), Round::new(0)))) .expect("execute succeeded"); assert_eq!( output, - Some(Message::GetValueAndScheduleTimeout( + Some(Output::GetValueAndScheduleTimeout( Round::new(0), Timeout::propose(Round::new(0)) )) @@ -1022,7 +1022,7 @@ fn driver_steps_proposer_not_found() { let mut driver = Driver::new(ctx, sel, vs, my_addr); - let output = block_on(driver.execute(Event::NewRound(Height::new(1), Round::new(0)))); + let output = block_on(driver.execute(Input::NewRound(Height::new(1), Round::new(0)))); assert_eq!(output, Err(Error::ProposerNotFound(v1.address))); } @@ -1043,11 +1043,11 @@ fn driver_steps_validator_not_found() { let mut driver = Driver::new(ctx, sel, vs, my_addr); // Start new height - block_on(driver.execute(Event::NewRound(Height::new(1), Round::new(0)))) + block_on(driver.execute(Input::NewRound(Height::new(1), Round::new(0)))) .expect("execute succeeded"); // v2 prevotes for some proposal, we cannot find it in the validator set => error - let output = block_on(driver.execute(Event::Vote( + let output = block_on(driver.execute(Input::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), v2.address).signed(&sk2), ))); @@ -1069,12 +1069,12 @@ fn driver_steps_invalid_signature() { let mut driver = Driver::new(ctx, sel, vs, my_addr); // Start new round - block_on(driver.execute(Event::NewRound(Height::new(1), Round::new(0)))) + block_on(driver.execute(Input::NewRound(Height::new(1), Round::new(0)))) .expect("execute succeeded"); // v2 prevotes for some proposal, with an invalid signature, // ie. signed by v1 instead of v2, just a way of forging an invalid signature - let output = block_on(driver.execute(Event::Vote( + let output = block_on(driver.execute(Input::Vote( Vote::new_prevote(Height::new(1), Round::new(0), Some(value.id()), v2.address).signed(&sk1), ))); @@ -1102,8 +1102,8 @@ fn driver_steps_skip_round_skip_threshold() { // Start round 0, we, v3, are not the proposer TestStep { desc: "Start round 0, we, v3, are not the proposer", - input_event: Some(Event::NewRound(height, Round::new(0))), - expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), + input: Some(Input::NewRound(height, Round::new(0))), + expected_output: Some(Output::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { height, @@ -1117,8 +1117,8 @@ fn driver_steps_skip_round_skip_threshold() { // Receive a propose timeout, prevote for nil (from v3) TestStep { desc: "Receive a propose timeout, prevote for nil (v3)", - input_event: Some(Event::TimeoutElapsed(Timeout::propose(Round::new(0)))), - expected_output: Some(Message::Vote( + input: Some(Input::TimeoutElapsed(Timeout::propose(Round::new(0)))), + expected_output: Some(Output::Vote( Vote::new_prevote(height, Round::new(0), None, my_addr).signed(&my_sk), )), expected_round: Round::new(0), @@ -1134,7 +1134,7 @@ fn driver_steps_skip_round_skip_threshold() { // Receive our own prevote v3 TestStep { desc: "Receive our own prevote v3", - input_event: None, + input: None, expected_output: None, expected_round: Round::new(0), new_state: State { @@ -1149,7 +1149,7 @@ fn driver_steps_skip_round_skip_threshold() { // v1 prevotes for its own proposal TestStep { desc: "v1 prevotes for its own proposal in round 1", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(height, Round::new(1), Some(value.id()), v1.address).signed(&sk1), )), expected_output: None, @@ -1166,10 +1166,10 @@ fn driver_steps_skip_round_skip_threshold() { // v2 prevotes for v1 proposal in round 1, expected output is to move to next round TestStep { desc: "v2 prevotes for v1 proposal, we get +1/3 messages from future round", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(height, Round::new(1), Some(value.id()), v2.address).signed(&sk2), )), - expected_output: Some(Message::NewRound(height, Round::new(1))), + expected_output: Some(Output::NewRound(height, Round::new(1))), expected_round: Round::new(1), new_state: State { height, @@ -1182,22 +1182,22 @@ fn driver_steps_skip_round_skip_threshold() { }, ]; - let mut event_from_previous_msg = None; + let mut output_from_prev_input = None; for step in steps { println!("Step: {}", step.desc); - let execute_event = step - .input_event - .unwrap_or_else(|| event_from_previous_msg.unwrap()); + let input = step + .input + .unwrap_or_else(|| output_from_prev_input.unwrap()); - let output = block_on(driver.execute(execute_event)).expect("execute succeeded"); - assert_eq!(output, step.expected_output, "expected output message"); + let output = block_on(driver.execute(input)).expect("execute succeeded"); + assert_eq!(output, step.expected_output, "expected output"); assert_eq!(driver.round(), step.expected_round, "expected round"); assert_eq!(driver.round_state, step.new_state, "new state"); - event_from_previous_msg = output.and_then(msg_to_event); + output_from_prev_input = output.and_then(output_to_input); } } @@ -1222,8 +1222,8 @@ fn driver_steps_skip_round_quorum_threshold() { // Start round 0, we, v3, are not the proposer TestStep { desc: "Start round 0, we, v3, are not the proposer", - input_event: Some(Event::NewRound(height, Round::new(0))), - expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), + input: Some(Input::NewRound(height, Round::new(0))), + expected_output: Some(Output::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { height, @@ -1237,8 +1237,8 @@ fn driver_steps_skip_round_quorum_threshold() { // Receive a propose timeout, prevote for nil (from v3) TestStep { desc: "Receive a propose timeout, prevote for nil (v3)", - input_event: Some(Event::TimeoutElapsed(Timeout::propose(Round::new(0)))), - expected_output: Some(Message::Vote( + input: Some(Input::TimeoutElapsed(Timeout::propose(Round::new(0)))), + expected_output: Some(Output::Vote( Vote::new_prevote(height, Round::new(0), None, my_addr).signed(&my_sk), )), expected_round: Round::new(0), @@ -1254,7 +1254,7 @@ fn driver_steps_skip_round_quorum_threshold() { // Receive our own prevote v3 TestStep { desc: "Receive our own prevote v3", - input_event: None, + input: None, expected_output: None, expected_round: Round::new(0), new_state: State { @@ -1269,7 +1269,7 @@ fn driver_steps_skip_round_quorum_threshold() { // v1 prevotes for its own proposal TestStep { desc: "v1 prevotes for its own proposal in round 1", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(height, Round::new(1), Some(value.id()), v1.address).signed(&sk1), )), expected_output: None, @@ -1286,10 +1286,10 @@ fn driver_steps_skip_round_quorum_threshold() { // v2 prevotes for v1 proposal in round 1, expected output is to move to next round TestStep { desc: "v2 prevotes for v1 proposal, we get +1/3 messages from future round", - input_event: Some(Event::Vote( + input: Some(Input::Vote( Vote::new_prevote(height, Round::new(1), Some(value.id()), v2.address).signed(&sk2), )), - expected_output: Some(Message::NewRound(height, Round::new(1))), + expected_output: Some(Output::NewRound(height, Round::new(1))), expected_round: Round::new(1), new_state: State { height, @@ -1302,22 +1302,22 @@ fn driver_steps_skip_round_quorum_threshold() { }, ]; - let mut event_from_previous_msg = None; + let mut input_from_prev_output = None; for step in steps { println!("Step: {}", step.desc); - let execute_event = step - .input_event - .unwrap_or_else(|| event_from_previous_msg.unwrap()); + let input = step + .input + .unwrap_or_else(|| input_from_prev_output.unwrap()); - let output = block_on(driver.execute(execute_event)).expect("execute succeeded"); - assert_eq!(output, step.expected_output, "expected output message"); + let output = block_on(driver.execute(input)).expect("execute succeeded"); + assert_eq!(output, step.expected_output, "expected output"); assert_eq!(driver.round(), step.expected_round, "expected round"); assert_eq!(driver.round_state, step.new_state, "new state"); - event_from_previous_msg = output.and_then(msg_to_event); + input_from_prev_output = output.and_then(output_to_input); } } diff --git a/Code/test/tests/driver_extra.rs b/Code/test/tests/driver_extra.rs index 199f66aa8..ff9a3b9a7 100644 --- a/Code/test/tests/driver_extra.rs +++ b/Code/test/tests/driver_extra.rs @@ -1,7 +1,7 @@ use futures::executor::block_on; use malachite_common::Round; -use malachite_driver::{Driver, Event, Message, Validity}; +use malachite_driver::{Driver, Input, Output, Validity}; use malachite_round::state::State; use malachite_test::{Height, Proposal, TestContext, ValidatorSet, Value}; @@ -11,21 +11,21 @@ use malachite_test::utils::*; // TODO - move all below to utils? struct TestStep { desc: &'static str, - input_event: Event, - expected_output: Option>, + input: Input, + expected_output: Option>, expected_round: Round, new_state: State, } -pub fn msg_to_event(output: Message) -> Option> { +pub fn output_to_input(output: Output) -> Option> { match output { - Message::NewRound(height, round) => Some(Event::NewRound(height, round)), + Output::NewRound(height, round) => Some(Input::NewRound(height, round)), // Let's consider our own proposal to always be valid - Message::Propose(p) => Some(Event::Proposal(p, Validity::Valid)), - Message::Vote(v) => Some(Event::Vote(v)), - Message::Decide(_, _) => None, - Message::ScheduleTimeout(_) => None, - Message::GetValueAndScheduleTimeout(_, _) => None, + Output::Propose(p) => Some(Input::Proposal(p, Validity::Valid)), + Output::Vote(v) => Some(Input::Vote(v)), + Output::Decide(_, _) => None, + Output::ScheduleTimeout(_) => None, + Output::GetValueAndScheduleTimeout(_, _) => None, } } @@ -57,29 +57,29 @@ fn driver_steps_decide_current_with_no_locked_no_valid() { let steps = vec![ TestStep { desc: "Start round 0, we, v2, are not the proposer, start timeout propose", - input_event: new_round_event(Round::new(0)), - expected_output: start_propose_timer_msg(Round::new(0)), + input: new_round_input(Round::new(0)), + expected_output: start_propose_timer_output(Round::new(0)), expected_round: Round::new(0), new_state: propose_state(Round::new(0)), }, TestStep { desc: "v1 precommits a proposal", - input_event: precommit_event(Round::new(0), value, &v1.address, &sk1), + input: precommit_input(Round::new(0), value, &v1.address, &sk1), expected_output: None, expected_round: Round::new(0), new_state: propose_state(Round::new(0)), }, TestStep { desc: "v2 precommits for same proposal, we get +2/3 precommit, start precommit timer", - input_event: precommit_event(Round::new(0), value, &v2.address, &sk2), - expected_output: start_precommit_timer_msg(Round::new(0)), + input: precommit_input(Round::new(0), value, &v2.address, &sk2), + expected_output: start_precommit_timer_output(Round::new(0)), expected_round: Round::new(0), new_state: propose_state(Round::new(0)), }, TestStep { desc: "Receive proposal", - input_event: proposal_event(Round::new(0), value, Round::Nil, Validity::Valid), - expected_output: decide_message(Round::new(0), value), + input: proposal_input(Round::new(0), value, Round::Nil, Validity::Valid), + expected_output: decide_output(Round::new(0), value), expected_round: Round::new(0), new_state: decided_state( Round::new(0), @@ -129,57 +129,57 @@ fn driver_steps_decide_previous_with_no_locked_no_valid() { let steps = vec![ TestStep { desc: "Start round 0, we, v2, are not the proposer, start timeout propose", - input_event: new_round_event(Round::new(0)), - expected_output: start_propose_timer_msg(Round::new(0)), + input: new_round_input(Round::new(0)), + expected_output: start_propose_timer_output(Round::new(0)), expected_round: Round::new(0), new_state: propose_state(Round::new(0)), }, TestStep { desc: "Timeout propopse, prevote for nil (v2)", - input_event: timeout_propose_event(Round::new(0)), - expected_output: prevote_nil_msg(Round::new(0), &my_addr, &my_sk), + input: timeout_propose_input(Round::new(0)), + expected_output: prevote_nil_output(Round::new(0), &my_addr, &my_sk), expected_round: Round::new(0), new_state: prevote_state(Round::new(0)), }, TestStep { desc: "v1 prevotes a proposal", - input_event: prevote_event(&v1.address, &sk1), + input: prevote_input(&v1.address, &sk1), expected_output: None, expected_round: Round::new(0), new_state: prevote_state(Round::new(0)), }, TestStep { desc: "v2 prevotes for same proposal, we get +2/3 prevotes, start prevote timer", - input_event: prevote_event(&v2.address, &sk2), - expected_output: start_prevote_timer_msg(Round::new(0)), + input: prevote_input(&v2.address, &sk2), + expected_output: start_prevote_timer_output(Round::new(0)), expected_round: Round::new(0), new_state: prevote_state(Round::new(0)), }, TestStep { desc: "v1 precommits a proposal", - input_event: precommit_event(Round::new(0), value, &v1.address, &sk1), + input: precommit_input(Round::new(0), value, &v1.address, &sk1), expected_output: None, expected_round: Round::new(0), new_state: prevote_state(Round::new(0)), }, TestStep { desc: "v2 precommits for same proposal, we get +2/3 precommit, start precommit timer", - input_event: precommit_event(Round::new(0), value, &v2.address, &sk2), - expected_output: start_precommit_timer_msg(Round::new(0)), + input: precommit_input(Round::new(0), value, &v2.address, &sk2), + expected_output: start_precommit_timer_output(Round::new(0)), expected_round: Round::new(0), new_state: prevote_state(Round::new(0)), }, TestStep { desc: "Timeout precommit, start new round", - input_event: timeout_precommit_event(Round::new(0)), - expected_output: new_round_msg(Round::new(1)), + input: timeout_precommit_input(Round::new(0)), + expected_output: new_round_output(Round::new(1)), expected_round: Round::new(1), new_state: new_round(Round::new(1)), }, TestStep { desc: "Receive proposal", - input_event: proposal_event(Round::new(0), value, Round::Nil, Validity::Valid), - expected_output: decide_message(Round::new(0), value), + input: proposal_input(Round::new(0), value, Round::Nil, Validity::Valid), + expected_output: decide_output(Round::new(0), value), expected_round: Round::new(1), new_state: decided_state( Round::new(1), @@ -231,15 +231,15 @@ fn driver_steps_polka_previous_with_locked() { let steps = vec![ TestStep { desc: "Start round 0, we, v2, are not the proposer, start timeout propose", - input_event: new_round_event(Round::new(0)), - expected_output: start_propose_timer_msg(Round::new(0)), + input: new_round_input(Round::new(0)), + expected_output: start_propose_timer_output(Round::new(0)), expected_round: Round::new(0), new_state: propose_state(Round::new(0)), }, TestStep { desc: "receive a proposal from v1 - L22 send prevote", - input_event: proposal_event(Round::new(0), value, Round::Nil, Validity::Valid), - expected_output: prevote_msg(Round::new(0), &my_addr, &my_sk), + input: proposal_input(Round::new(0), value, Round::Nil, Validity::Valid), + expected_output: prevote_output(Round::new(0), &my_addr, &my_sk), expected_round: Round::new(0), new_state: prevote_state_with_proposal( Round::new(0), @@ -248,7 +248,7 @@ fn driver_steps_polka_previous_with_locked() { }, TestStep { desc: "v3 prevotes the proposal", - input_event: prevote_event(&v3.address, &sk3), + input: prevote_input(&v3.address, &sk3), expected_output: None, expected_round: Round::new(0), new_state: prevote_state_with_proposal( @@ -258,8 +258,8 @@ fn driver_steps_polka_previous_with_locked() { }, TestStep { desc: "v1 prevotes for same proposal, we get +2/3 prevotes, precommit", - input_event: prevote_event(&v1.address, &sk1), - expected_output: precommit_msg(Round::new(0), value, &my_addr, &my_sk), + input: prevote_input(&v1.address, &sk1), + expected_output: precommit_output(Round::new(0), value, &my_addr, &my_sk), expected_round: Round::new(0), new_state: precommit_state_with_proposal_and_locked_and_valid( Round::new(0), @@ -268,8 +268,8 @@ fn driver_steps_polka_previous_with_locked() { }, TestStep { desc: "Receive f+1 vote for round 1 from v3", - input_event: precommit_event(Round::new(1), Value::new(8888), &v3.address, &sk3), - expected_output: new_round_msg(Round::new(1)), + input: precommit_input(Round::new(1), Value::new(8888), &v3.address, &sk3), + expected_output: new_round_output(Round::new(1)), expected_round: Round::new(1), new_state: new_round_with_proposal_and_locked_and_valid( Round::new(1), @@ -278,8 +278,8 @@ fn driver_steps_polka_previous_with_locked() { }, TestStep { desc: "start round 1, we are proposer with a valid value, propose it", - input_event: new_round_event(Round::new(1)), - expected_output: proposal_msg(Round::new(1), value, Round::new(0)), + input: new_round_input(Round::new(1)), + expected_output: proposal_output(Round::new(1), value, Round::new(0)), expected_round: Round::new(1), new_state: propose_state_with_proposal_and_locked_and_valid( Round::new(1), @@ -288,8 +288,8 @@ fn driver_steps_polka_previous_with_locked() { }, TestStep { desc: "Receive our own proposal", - input_event: proposal_event(Round::new(1), value, Round::new(0), Validity::Valid), - expected_output: prevote_msg(Round::new(1), &my_addr, &my_sk), + input: proposal_input(Round::new(1), value, Round::new(0), Validity::Valid), + expected_output: prevote_output(Round::new(1), &my_addr, &my_sk), expected_round: Round::new(1), new_state: prevote_state_with_proposal_and_locked_and_valid( Round::new(1), @@ -317,15 +317,15 @@ fn driver_steps_polka_previous_invalid_proposal_with_locked() { let steps = vec![ TestStep { desc: "Start round 0, we, v2, are not the proposer, start timeout propose", - input_event: new_round_event(Round::new(0)), - expected_output: start_propose_timer_msg(Round::new(0)), + input: new_round_input(Round::new(0)), + expected_output: start_propose_timer_output(Round::new(0)), expected_round: Round::new(0), new_state: propose_state(Round::new(0)), }, TestStep { desc: "receive a proposal from v1 - L22 send prevote", - input_event: proposal_event(Round::new(0), value, Round::Nil, Validity::Valid), - expected_output: prevote_msg(Round::new(0), &my_addr, &my_sk), + input: proposal_input(Round::new(0), value, Round::Nil, Validity::Valid), + expected_output: prevote_output(Round::new(0), &my_addr, &my_sk), expected_round: Round::new(0), new_state: prevote_state_with_proposal( Round::new(0), @@ -334,7 +334,7 @@ fn driver_steps_polka_previous_invalid_proposal_with_locked() { }, TestStep { desc: "v3 prevotes the proposal", - input_event: prevote_event(&v3.address, &sk3), + input: prevote_input(&v3.address, &sk3), expected_output: None, expected_round: Round::new(0), new_state: prevote_state_with_proposal( @@ -344,8 +344,8 @@ fn driver_steps_polka_previous_invalid_proposal_with_locked() { }, TestStep { desc: "v1 prevotes for same proposal, we get +2/3 prevotes, precommit", - input_event: prevote_event(&v1.address, &sk1), - expected_output: precommit_msg(Round::new(0), value, &my_addr, &my_sk), + input: prevote_input(&v1.address, &sk1), + expected_output: precommit_output(Round::new(0), value, &my_addr, &my_sk), expected_round: Round::new(0), new_state: precommit_state_with_proposal_and_locked_and_valid( Round::new(0), @@ -354,8 +354,8 @@ fn driver_steps_polka_previous_invalid_proposal_with_locked() { }, TestStep { desc: "Receive f+1 vote for round 1 from v3", - input_event: precommit_event(Round::new(1), Value::new(8888), &v3.address, &sk3), - expected_output: new_round_msg(Round::new(1)), + input: precommit_input(Round::new(1), Value::new(8888), &v3.address, &sk3), + expected_output: new_round_output(Round::new(1)), expected_round: Round::new(1), new_state: new_round_with_proposal_and_locked_and_valid( Round::new(1), @@ -364,8 +364,8 @@ fn driver_steps_polka_previous_invalid_proposal_with_locked() { }, TestStep { desc: "start round 1, we are proposer with a valid value, propose it", - input_event: new_round_event(Round::new(1)), - expected_output: proposal_msg(Round::new(1), value, Round::new(0)), + input: new_round_input(Round::new(1)), + expected_output: proposal_output(Round::new(1), value, Round::new(0)), expected_round: Round::new(1), new_state: propose_state_with_proposal_and_locked_and_valid( Round::new(1), @@ -374,8 +374,8 @@ fn driver_steps_polka_previous_invalid_proposal_with_locked() { }, TestStep { desc: "Receive our own proposal", - input_event: proposal_event(Round::new(1), value, Round::new(0), Validity::Invalid), - expected_output: prevote_nil_msg(Round::new(1), &my_addr, &my_sk), + input: proposal_input(Round::new(1), value, Round::new(0), Validity::Invalid), + expected_output: prevote_nil_output(Round::new(1), &my_addr, &my_sk), expected_round: Round::new(1), new_state: prevote_state_with_proposal_and_locked_and_valid( Round::new(1), @@ -433,42 +433,42 @@ fn driver_steps_polka_previous_with_no_locked() { let steps = vec![ TestStep { desc: "Start round 0, we v2 are not the proposer, start timeout propose", - input_event: new_round_event(Round::new(0)), - expected_output: start_propose_timer_msg(Round::new(0)), + input: new_round_input(Round::new(0)), + expected_output: start_propose_timer_output(Round::new(0)), expected_round: Round::new(0), new_state: propose_state(Round::new(0)), }, TestStep { desc: "Timeout propopse, prevote for nil (v2)", - input_event: timeout_propose_event(Round::new(0)), - expected_output: prevote_nil_msg(Round::new(0), &my_addr, &my_sk), + input: timeout_propose_input(Round::new(0)), + expected_output: prevote_nil_output(Round::new(0), &my_addr, &my_sk), expected_round: Round::new(0), new_state: prevote_state(Round::new(0)), }, TestStep { desc: "v3 prevotes for some proposal", - input_event: prevote_event(&v3.address, &sk3), + input: prevote_input(&v3.address, &sk3), expected_output: None, expected_round: Round::new(0), new_state: prevote_state(Round::new(0)), }, TestStep { desc: "v1 prevotes for same proposal, we get +2/3 prevotes, start timeout prevote", - input_event: prevote_event(&v1.address, &sk1), - expected_output: start_prevote_timer_msg(Round::new(0)), + input: prevote_input(&v1.address, &sk1), + expected_output: start_prevote_timer_output(Round::new(0)), expected_round: Round::new(0), new_state: prevote_state(Round::new(0)), }, TestStep { desc: "timeout prevote, prevote for nil (v2)", - input_event: timeout_prevote_event(Round::new(0)), - expected_output: precommit_nil_msg(&my_addr, &my_sk), + input: timeout_prevote_input(Round::new(0)), + expected_output: precommit_nil_output(&my_addr, &my_sk), expected_round: Round::new(0), new_state: precommit_state(Round::new(0)), }, TestStep { desc: "receive a proposal - L36, we don't lock, we set valid", - input_event: proposal_event(Round::new(0), value, Round::Nil, Validity::Valid), + input: proposal_input(Round::new(0), value, Round::Nil, Validity::Valid), expected_output: None, expected_round: Round::new(0), new_state: precommit_state_with_proposal_and_valid( @@ -479,8 +479,8 @@ fn driver_steps_polka_previous_with_no_locked() { }, TestStep { desc: "Receive f+1 vote for round 1 from v3", - input_event: prevote_event_at(Round::new(1), &v3.address, &sk3), - expected_output: new_round_msg(Round::new(1)), + input: prevote_input_at(Round::new(1), &v3.address, &sk3), + expected_output: new_round_output(Round::new(1)), expected_round: Round::new(1), new_state: new_round_with_proposal_and_valid( Round::new(1), @@ -489,8 +489,8 @@ fn driver_steps_polka_previous_with_no_locked() { }, TestStep { desc: "start round 1, we are proposer with a valid value from round 0, propose it", - input_event: new_round_event(Round::new(1)), - expected_output: proposal_msg(Round::new(1), value, Round::new(0)), + input: new_round_input(Round::new(1)), + expected_output: proposal_output(Round::new(1), value, Round::new(0)), expected_round: Round::new(1), new_state: propose_state_with_proposal_and_valid( Round::new(1), @@ -500,8 +500,8 @@ fn driver_steps_polka_previous_with_no_locked() { }, TestStep { desc: "Receive our own proposal, prevote nil as we are not locked on the value", - input_event: proposal_event(Round::new(1), value, Round::new(0), Validity::Valid), - expected_output: prevote_nil_msg(Round::new(1), &my_addr, &my_sk), + input: proposal_input(Round::new(1), value, Round::new(0), Validity::Valid), + expected_output: prevote_nil_output(Round::new(1), &my_addr, &my_sk), expected_round: Round::new(1), new_state: prevote_state_with_proposal_and_valid( Round::new(1), @@ -518,8 +518,8 @@ fn run_steps(driver: &mut Driver, steps: Vec) { for step in steps { println!("Step: {}", step.desc); - let output = block_on(driver.execute(step.input_event)).expect("execute succeeded"); - assert_eq!(output, step.expected_output, "expected output message"); + let output = block_on(driver.execute(step.input)).expect("execute succeeded"); + assert_eq!(output, step.expected_output, "expected output"); assert_eq!( driver.round_state.round, step.expected_round, diff --git a/Code/test/tests/round.rs b/Code/test/tests/round.rs index 0952ff747..246194a7a 100644 --- a/Code/test/tests/round.rs +++ b/Code/test/tests/round.rs @@ -1,10 +1,10 @@ use malachite_test::{Address, Height, Proposal, TestContext, Value}; use malachite_common::{Round, Timeout, TimeoutStep}; -use malachite_round::events::Event; -use malachite_round::message::Message; +use malachite_round::input::Input; +use malachite_round::output::Output; use malachite_round::state::{State, Step}; -use malachite_round::state_machine::{apply_event, Info}; +use malachite_round::state_machine::{apply, Info}; const ADDRESS: Address = Address::new([42; 20]); const OTHER_ADDRESS: Address = Address::new([21; 20]); @@ -24,22 +24,22 @@ fn test_propose() { // We are the proposer let data = Info::new(round, &ADDRESS, &ADDRESS); - let transition = apply_event(state.clone(), &data, Event::NewRound); + let transition = apply(state.clone(), &data, Input::NewRound); state.step = Step::Propose; assert_eq!(transition.next_state, state); assert_eq!( - transition.message.unwrap(), - Message::get_value_and_schedule_timeout(round, TimeoutStep::Propose) + transition.output.unwrap(), + Output::get_value_and_schedule_timeout(round, TimeoutStep::Propose) ); - let transition = apply_event(transition.next_state, &data, Event::ProposeValue(value)); + let transition = apply(transition.next_state, &data, Input::ProposeValue(value)); state.step = Step::Propose; assert_eq!(transition.next_state, state); assert_eq!( - transition.message.unwrap(), - Message::proposal(Height::new(10), Round::new(0), Value::new(42), Round::Nil) + transition.output.unwrap(), + Output::proposal(Height::new(10), Round::new(0), Value::new(42), Round::Nil) ); } @@ -58,12 +58,12 @@ fn test_prevote() { // We are not the proposer let data = Info::new(Round::new(1), &ADDRESS, &OTHER_ADDRESS); - let transition = apply_event(state, &data, Event::NewRound); + let transition = apply(state, &data, Input::NewRound); assert_eq!(transition.next_state.step, Step::Propose); assert_eq!( - transition.message.unwrap(), - Message::ScheduleTimeout(Timeout { + transition.output.unwrap(), + Output::ScheduleTimeout(Timeout { round: Round::new(1), step: TimeoutStep::Propose }) @@ -71,10 +71,10 @@ fn test_prevote() { let state = transition.next_state; - let transition = apply_event( + let transition = apply( state, &data, - Event::Proposal(Proposal::new( + Input::Proposal(Proposal::new( Height::new(1), Round::new(1), value, @@ -84,7 +84,7 @@ fn test_prevote() { assert_eq!(transition.next_state.step, Step::Prevote); assert_eq!( - transition.message.unwrap(), - Message::prevote(Height::new(1), Round::new(1), Some(value.id()), ADDRESS) + transition.output.unwrap(), + Output::prevote(Height::new(1), Round::new(1), Some(value.id()), ADDRESS) ); } diff --git a/Code/test/tests/vote_keeper.rs b/Code/test/tests/vote_keeper.rs index f1d6d7849..f3cd9df3f 100644 --- a/Code/test/tests/vote_keeper.rs +++ b/Code/test/tests/vote_keeper.rs @@ -1,5 +1,5 @@ use malachite_common::Round; -use malachite_vote::keeper::{Message, VoteKeeper}; +use malachite_vote::keeper::{Output, VoteKeeper}; use malachite_test::{Address, Height, TestContext, ValueId, Vote}; @@ -24,7 +24,7 @@ fn prevote_apply_nil() { let vote = Vote::new_prevote(height, round, None, ADDRESS3); let msg = keeper.apply_vote(vote, 1, round); - assert_eq!(msg, Some(Message::PolkaNil)); + assert_eq!(msg, Some(Output::PolkaNil)); } #[test] @@ -43,7 +43,7 @@ fn precommit_apply_nil() { let vote = Vote::new_precommit(height, Round::new(0), None, ADDRESS3); let msg = keeper.apply_vote(vote, 1, round); - assert_eq!(msg, Some(Message::PrecommitAny)); + assert_eq!(msg, Some(Output::PrecommitAny)); } #[test] @@ -65,11 +65,11 @@ fn prevote_apply_single_value() { let vote_nil = Vote::new_prevote(height, Round::new(0), None, ADDRESS3); let msg = keeper.apply_vote(vote_nil, 1, round); - assert_eq!(msg, Some(Message::PolkaAny)); + assert_eq!(msg, Some(Output::PolkaAny)); let vote = Vote::new_prevote(height, Round::new(0), val, ADDRESS4); let msg = keeper.apply_vote(vote, 1, round); - assert_eq!(msg, Some(Message::PolkaValue(id))); + assert_eq!(msg, Some(Output::PolkaValue(id))); } #[test] @@ -91,11 +91,11 @@ fn precommit_apply_single_value() { let vote_nil = Vote::new_precommit(height, Round::new(0), None, ADDRESS3); let msg = keeper.apply_vote(vote_nil, 1, round); - assert_eq!(msg, Some(Message::PrecommitAny)); + assert_eq!(msg, Some(Output::PrecommitAny)); let vote = Vote::new_precommit(height, Round::new(0), val, ADDRESS4); let msg = keeper.apply_vote(vote, 1, round); - assert_eq!(msg, Some(Message::PrecommitValue(id))); + assert_eq!(msg, Some(Output::PrecommitValue(id))); } #[test] @@ -118,7 +118,7 @@ fn skip_round_small_quorum_prevotes_two_vals() { let vote = Vote::new_prevote(height, fut_round, val, ADDRESS3); let msg = keeper.apply_vote(vote, 1, cur_round); - assert_eq!(msg, Some(Message::SkipRound(Round::new(1)))); + assert_eq!(msg, Some(Output::SkipRound(Round::new(1)))); } #[test] @@ -141,7 +141,7 @@ fn skip_round_small_quorum_with_prevote_precommit_two_vals() { let vote = Vote::new_precommit(height, fut_round, val, ADDRESS3); let msg = keeper.apply_vote(vote, 1, cur_round); - assert_eq!(msg, Some(Message::SkipRound(Round::new(1)))); + assert_eq!(msg, Some(Output::SkipRound(Round::new(1)))); } #[test] @@ -164,7 +164,7 @@ fn skip_round_full_quorum_with_prevote_precommit_two_vals() { let vote = Vote::new_precommit(height, fut_round, val, ADDRESS3); let msg = keeper.apply_vote(vote, 2, cur_round); - assert_eq!(msg, Some(Message::SkipRound(Round::new(1)))); + assert_eq!(msg, Some(Output::SkipRound(Round::new(1)))); } #[test] diff --git a/Code/vote/src/keeper.rs b/Code/vote/src/keeper.rs index 16864e06d..fc4371f00 100644 --- a/Code/vote/src/keeper.rs +++ b/Code/vote/src/keeper.rs @@ -10,7 +10,7 @@ use crate::{Threshold, ThresholdParam, ThresholdParams, Weight}; /// Messages emitted by the vote keeper #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum Message { +pub enum Output { PolkaAny, PolkaNil, PolkaValue(Value), @@ -25,7 +25,7 @@ where { votes: RoundVotes>, addresses_weights: RoundWeights, - emitted_msgs: BTreeSet>>, + emitted_msgs: BTreeSet>>, } impl PerRound @@ -48,7 +48,7 @@ where &self.addresses_weights } - pub fn emitted_msgs(&self) -> &BTreeSet>> { + pub fn emitted_msgs(&self) -> &BTreeSet>> { &self.emitted_msgs } } @@ -111,13 +111,13 @@ where &self.per_round } - /// Apply a vote with a given weight, potentially triggering an event. + /// Apply a vote with a given weight, potentially triggering an output. pub fn apply_vote( &mut self, vote: Ctx::Vote, weight: Weight, current_round: Round, - ) -> Option>> { + ) -> Option>> { let round = self .per_round .entry(vote.round()) @@ -143,7 +143,7 @@ where .is_met(combined_weight, self.total_weight); if skip_round { - let msg = Message::SkipRound(vote.round()); + let msg = Output::SkipRound(vote.round()); round.emitted_msgs.insert(msg.clone()); return Some(msg); } @@ -157,7 +157,7 @@ where self.total_weight, ); - let msg = threshold_to_message(vote.vote_type(), vote.round(), threshold); + let msg = threshold_to_output(vote.vote_type(), vote.round(), threshold); match msg { Some(msg) if !round.emitted_msgs.contains(&msg) => { @@ -217,23 +217,23 @@ where } } -/// Map a vote type and a threshold to a state machine event. -fn threshold_to_message( +/// Map a vote type and a threshold to a state machine output. +fn threshold_to_output( typ: VoteType, round: Round, threshold: Threshold, -) -> Option> { +) -> Option> { match (typ, threshold) { (_, Threshold::Unreached) => None, - (_, Threshold::Skip) => Some(Message::SkipRound(round)), + (_, Threshold::Skip) => Some(Output::SkipRound(round)), - (VoteType::Prevote, Threshold::Any) => Some(Message::PolkaAny), - (VoteType::Prevote, Threshold::Nil) => Some(Message::PolkaNil), - (VoteType::Prevote, Threshold::Value(v)) => Some(Message::PolkaValue(v)), + (VoteType::Prevote, Threshold::Any) => Some(Output::PolkaAny), + (VoteType::Prevote, Threshold::Nil) => Some(Output::PolkaNil), + (VoteType::Prevote, Threshold::Value(v)) => Some(Output::PolkaValue(v)), - (VoteType::Precommit, Threshold::Any) => Some(Message::PrecommitAny), - (VoteType::Precommit, Threshold::Nil) => Some(Message::PrecommitAny), - (VoteType::Precommit, Threshold::Value(v)) => Some(Message::PrecommitValue(v)), + (VoteType::Precommit, Threshold::Any) => Some(Output::PrecommitAny), + (VoteType::Precommit, Threshold::Nil) => Some(Output::PrecommitAny), + (VoteType::Precommit, Threshold::Value(v)) => Some(Output::PrecommitValue(v)), } }