Skip to content

Commit

Permalink
Replace StartHeight with NewRound with height and round zero
Browse files Browse the repository at this point in the history
  • Loading branch information
ancazamfir authored and romac committed Nov 15, 2023
1 parent 8398069 commit a9ea037
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
11 changes: 4 additions & 7 deletions Code/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -101,12 +103,7 @@ where

async fn apply(&mut self, event: Event<Ctx>) -> Result<Option<RoundMessage<Ctx>>, Error<Ctx>> {
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)
Expand Down
3 changes: 1 addition & 2 deletions Code/driver/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ pub enum Event<Ctx>
where
Ctx: Context,
{
StartHeight(Ctx::Height),
NewRound(Round),
NewRound(Ctx::Height, Round),
Proposal(Ctx::Proposal, Validity),
Vote(SignedVote<Ctx>),
TimeoutElapsed(Timeout),
Expand Down
10 changes: 6 additions & 4 deletions Code/driver/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ where
Vote(SignedVote<Ctx>),
Decide(Round, Ctx::Value),
ScheduleTimeout(Timeout),
NewRound(Round),
NewRound(Ctx::Height, Round),
}

// NOTE: We have to derive these instances manually, otherwise
Expand All @@ -26,7 +26,7 @@ impl<Ctx: Context> Clone for Message<Ctx> {
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),
}
}
}
Expand All @@ -39,7 +39,7 @@ impl<Ctx: Context> fmt::Debug for Message<Ctx> {
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),
}
}
}
Expand All @@ -60,7 +60,9 @@ impl<Ctx: Context> PartialEq for Message<Ctx> {
(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,
}
}
Expand Down
24 changes: 13 additions & 11 deletions Code/test/tests/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn to_input_msg(output: Message<TestContext>) -> Option<Event<TestContext>> {
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)),
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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)));
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a9ea037

Please sign in to comment.