From a9ea037544336b9a57501c7b8eff965afcce1d00 Mon Sep 17 00:00:00 2001 From: Anca Zamfir Date: Tue, 14 Nov 2023 18:14:32 +0100 Subject: [PATCH] Replace StartHeight with NewRound with height and round zero --- Code/driver/src/driver.rs | 11 ++++------- Code/driver/src/event.rs | 3 +-- Code/driver/src/message.rs | 10 ++++++---- Code/test/tests/driver.rs | 24 +++++++++++++----------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Code/driver/src/driver.rs b/Code/driver/src/driver.rs index 6863b0c2e..e25c26d98 100644 --- a/Code/driver/src/driver.rs +++ b/Code/driver/src/driver.rs @@ -76,7 +76,9 @@ where }; let msg = match round_msg { - RoundMessage::NewRound(round) => Message::NewRound(round), + RoundMessage::NewRound(round) => { + Message::NewRound(self.round_state.height.clone(), round) + } RoundMessage::Proposal(proposal) => { // sign the proposal @@ -101,12 +103,7 @@ where async fn apply(&mut self, event: Event) -> Result>, Error> { match event { - Event::StartHeight(height) => self.apply_new_round(height, Round::new(0)).await, - - Event::NewRound(round) => { - self.apply_new_round(self.round_state.height.clone(), round) - .await - } + Event::NewRound(height, round) => self.apply_new_round(height, round).await, Event::Proposal(proposal, validity) => { Ok(self.apply_proposal(proposal, validity).await) diff --git a/Code/driver/src/event.rs b/Code/driver/src/event.rs index ab616ea1e..d0a3381e1 100644 --- a/Code/driver/src/event.rs +++ b/Code/driver/src/event.rs @@ -8,8 +8,7 @@ pub enum Event where Ctx: Context, { - StartHeight(Ctx::Height), - NewRound(Round), + NewRound(Ctx::Height, Round), Proposal(Ctx::Proposal, Validity), Vote(SignedVote), TimeoutElapsed(Timeout), diff --git a/Code/driver/src/message.rs b/Code/driver/src/message.rs index 89a56333b..b5f4d7073 100644 --- a/Code/driver/src/message.rs +++ b/Code/driver/src/message.rs @@ -11,7 +11,7 @@ where Vote(SignedVote), Decide(Round, Ctx::Value), ScheduleTimeout(Timeout), - NewRound(Round), + NewRound(Ctx::Height, Round), } // NOTE: We have to derive these instances manually, otherwise @@ -26,7 +26,7 @@ impl Clone for Message { 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::NewRound(round) => Message::NewRound(*round), + Message::NewRound(height, round) => Message::NewRound(height.clone(), *round), } } } @@ -39,7 +39,7 @@ impl fmt::Debug for Message { 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::NewRound(round) => write!(f, "NewRound({:?})", round), + Message::NewRound(height, round) => write!(f, "NewRound({:?}, {:?})", height, round), } } } @@ -60,7 +60,9 @@ impl PartialEq for Message { (Message::ScheduleTimeout(timeout), Message::ScheduleTimeout(other_timeout)) => { timeout == other_timeout } - (Message::NewRound(round), Message::NewRound(other_round)) => round == other_round, + (Message::NewRound(height, round), Message::NewRound(other_height, other_round)) => { + height == other_height && round == other_round + } _ => false, } } diff --git a/Code/test/tests/driver.rs b/Code/test/tests/driver.rs index 7d079bb38..93d4006ba 100644 --- a/Code/test/tests/driver.rs +++ b/Code/test/tests/driver.rs @@ -25,7 +25,7 @@ fn to_input_msg(output: Message) -> Option> { Message::Vote(v) => Some(Event::Vote(v)), Message::Decide(_, _) => None, Message::ScheduleTimeout(_) => None, - Message::NewRound(round) => Some(Event::NewRound(round)), + Message::NewRound(height, round) => Some(Event::NewRound(height, round)), } } @@ -92,7 +92,7 @@ fn driver_steps_proposer() { let steps = vec![ TestStep { desc: "Start round 0, we are proposer, propose value", - input_event: Some(Event::StartHeight(Height::new(1))), + input_event: Some(Event::NewRound(Height::new(1), Round::new(0))), expected_output: Some(Message::Propose(proposal.clone())), expected_round: Round::new(0), new_state: State { @@ -303,7 +303,7 @@ fn driver_steps_not_proposer_valid() { let steps = vec![ TestStep { desc: "Start round 0, we are not the proposer", - input_event: Some(Event::StartHeight(Height::new(1))), + input_event: Some(Event::NewRound(Height::new(1), Round::new(0))), expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { @@ -514,7 +514,7 @@ fn driver_steps_not_proposer_invalid() { let steps = vec![ TestStep { desc: "Start round 0, we are not the proposer", - input_event: Some(Event::StartHeight(Height::new(1))), + input_event: Some(Event::NewRound(Height::new(1), Round::new(0))), expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { @@ -662,7 +662,7 @@ 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::StartHeight(Height::new(1))), + input_event: Some(Event::NewRound(Height::new(1), Round::new(0))), expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { @@ -797,7 +797,7 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { 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(Round::new(1))), + expected_output: Some(Message::NewRound(Height::new(1), Round::new(1))), expected_round: Round::new(0), new_state: State { height: Height::new(1), @@ -810,7 +810,7 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() { }, TestStep { desc: "Start round 1, we are not the proposer", - input_event: Some(Event::NewRound(Round::new(1))), + input_event: Some(Event::NewRound(Height::new(1), Round::new(1))), expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(1)))), expected_round: Round::new(1), new_state: State { @@ -866,7 +866,7 @@ fn driver_steps_no_value_to_propose() { let mut driver = Driver::new(ctx, env, sel, vs, my_addr); - let output = block_on(driver.execute(Event::StartHeight(Height::new(1)))); + let output = block_on(driver.execute(Event::NewRound(Height::new(1), Round::new(0)))); assert_eq!(output, Err(Error::NoValueToPropose)); } @@ -897,7 +897,7 @@ fn driver_steps_proposer_not_found() { let mut driver = Driver::new(ctx, env, sel, vs, my_addr); - let output = block_on(driver.execute(Event::StartHeight(Height::new(1)))); + let output = block_on(driver.execute(Event::NewRound(Height::new(1), Round::new(0)))); assert_eq!(output, Err(Error::ProposerNotFound(v1.address))); } @@ -928,7 +928,8 @@ fn driver_steps_validator_not_found() { let mut driver = Driver::new(ctx, env, sel, vs, my_addr); // Start new height - block_on(driver.execute(Event::StartHeight(Height::new(1)))).expect("execute succeeded"); + block_on(driver.execute(Event::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( @@ -963,7 +964,8 @@ fn driver_steps_invalid_signature() { let mut driver = Driver::new(ctx, env, sel, vs, my_addr); // Start new round - block_on(driver.execute(Event::StartHeight(Height::new(1)))).expect("execute succeeded"); + block_on(driver.execute(Event::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