diff --git a/Code/driver/src/driver.rs b/Code/driver/src/driver.rs index c5ca385ce..7b7cbc484 100644 --- a/Code/driver/src/driver.rs +++ b/Code/driver/src/driver.rs @@ -67,9 +67,17 @@ where } } + pub fn height(&self) -> &Ctx::Height { + &self.round_state.height + } + + pub fn round(&self) -> Round { + self.round_state.round + } + async fn get_value(&self) -> Option { self.env - .get_value(self.round_state.height.clone(), self.round_state.round) + .get_value(self.height().clone(), self.round()) .await } @@ -80,9 +88,7 @@ where }; let msg = match round_msg { - RoundMessage::NewRound(round) => { - Message::NewRound(self.round_state.height.clone(), round) - } + RoundMessage::NewRound(round) => Message::NewRound(self.height().clone(), round), RoundMessage::Proposal(proposal) => { // sign the proposal @@ -232,11 +238,12 @@ where )); } - let round = signed_vote.vote.round(); + let vote_round = signed_vote.vote.round(); + let current_round = self.round(); let Some(vote_msg) = self.votes - .apply_vote(signed_vote.vote, validator.voting_power(), round) + .apply_vote(signed_vote.vote, validator.voting_power(), current_round) else { return Ok(None); }; @@ -250,7 +257,7 @@ where VoteMessage::SkipRound(r) => RoundEvent::SkipRound(r), }; - Ok(self.apply_event(round, round_event)) + Ok(self.apply_event(vote_round, round_event)) } fn apply_timeout(&mut self, timeout: Timeout) -> Option> { diff --git a/Code/test/tests/driver.rs b/Code/test/tests/driver.rs index 3dcd3f0cb..2e031e0a0 100644 --- a/Code/test/tests/driver.rs +++ b/Code/test/tests/driver.rs @@ -1001,6 +1001,7 @@ fn driver_steps_skip_round_skip_threshold() { let (my_sk, my_addr) = (sk3, addr3); let ctx = TestContext::new(my_sk.clone()); + let height = Height::new(1); let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]); let mut driver = Driver::new(ctx, env, sel, vs, my_addr); @@ -1009,11 +1010,11 @@ 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::new(1), Round::new(0))), + input_event: Some(Event::NewRound(height, Round::new(0))), expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { - height: Height::new(1), + height, round: Round::new(0), step: Step::Propose, proposal: None, @@ -1026,11 +1027,11 @@ fn driver_steps_skip_round_skip_threshold() { desc: "Receive a propose timeout, prevote for nil (v3)", input_event: Some(Event::TimeoutElapsed(Timeout::propose(Round::new(0)))), expected_output: Some(Message::Vote( - Vote::new_prevote(Height::new(1), Round::new(0), None, my_addr).signed(&my_sk), + Vote::new_prevote(height, Round::new(0), None, my_addr).signed(&my_sk), )), expected_round: Round::new(0), new_state: State { - height: Height::new(1), + height, round: Round::new(0), step: Step::Prevote, proposal: None, @@ -1045,7 +1046,7 @@ fn driver_steps_skip_round_skip_threshold() { expected_output: None, expected_round: Round::new(0), new_state: State { - height: Height::new(1), + height, round: Round::new(0), step: Step::Prevote, proposal: None, @@ -1057,13 +1058,12 @@ fn driver_steps_skip_round_skip_threshold() { TestStep { desc: "v1 prevotes for its own proposal in round 1", input_event: Some(Event::Vote( - Vote::new_prevote(Height::new(1), Round::new(1), Some(value.id()), addr1) - .signed(&sk1), + Vote::new_prevote(height, Round::new(1), Some(value.id()), addr1).signed(&sk1), )), expected_output: None, expected_round: Round::new(0), new_state: State { - height: Height::new(1), + height, round: Round::new(0), step: Step::Prevote, proposal: None, @@ -1075,15 +1075,14 @@ fn driver_steps_skip_round_skip_threshold() { TestStep { desc: "v2 prevotes for v1 proposal, we get +1/3 messages from future round", input_event: Some(Event::Vote( - Vote::new_prevote(Height::new(1), Round::new(1), Some(value.id()), addr2) - .signed(&sk2), + Vote::new_prevote(height, Round::new(1), Some(value.id()), addr2).signed(&sk2), )), - expected_output: Some(Message::NewRound(Height::new(1), Round::new(1))), - expected_round: Round::new(0), + expected_output: Some(Message::NewRound(height, Round::new(1))), + expected_round: Round::new(1), new_state: State { - height: Height::new(1), - round: Round::new(0), - step: Step::Prevote, + height, + round: Round::new(1), + step: Step::NewRound, proposal: None, locked: None, valid: None, @@ -1101,8 +1100,9 @@ fn driver_steps_skip_round_skip_threshold() { .unwrap_or_else(|| previous_message.unwrap()); let output = block_on(driver.execute(execute_message)).expect("execute succeeded"); - // assert_eq!(output, step.expected_output, "expected output message"); + assert_eq!(output, step.expected_output, "expected output message"); + assert_eq!(driver.round(), step.expected_round, "expected round"); assert_eq!(driver.round_state, step.new_state, "new state"); previous_message = output.and_then(to_input_msg); @@ -1134,6 +1134,7 @@ fn driver_steps_skip_round_quorum_threshold() { let (my_sk, my_addr) = (sk3, addr3); let ctx = TestContext::new(my_sk.clone()); + let height = Height::new(1); let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]); let mut driver = Driver::new(ctx, env, sel, vs, my_addr); @@ -1142,11 +1143,11 @@ 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::new(1), Round::new(0))), + input_event: Some(Event::NewRound(height, Round::new(0))), expected_output: Some(Message::ScheduleTimeout(Timeout::propose(Round::new(0)))), expected_round: Round::new(0), new_state: State { - height: Height::new(1), + height, round: Round::new(0), step: Step::Propose, proposal: None, @@ -1159,11 +1160,11 @@ fn driver_steps_skip_round_quorum_threshold() { desc: "Receive a propose timeout, prevote for nil (v3)", input_event: Some(Event::TimeoutElapsed(Timeout::propose(Round::new(0)))), expected_output: Some(Message::Vote( - Vote::new_prevote(Height::new(1), Round::new(0), None, my_addr).signed(&my_sk), + Vote::new_prevote(height, Round::new(0), None, my_addr).signed(&my_sk), )), expected_round: Round::new(0), new_state: State { - height: Height::new(1), + height, round: Round::new(0), step: Step::Prevote, proposal: None, @@ -1178,7 +1179,7 @@ fn driver_steps_skip_round_quorum_threshold() { expected_output: None, expected_round: Round::new(0), new_state: State { - height: Height::new(1), + height, round: Round::new(0), step: Step::Prevote, proposal: None, @@ -1190,13 +1191,12 @@ fn driver_steps_skip_round_quorum_threshold() { TestStep { desc: "v1 prevotes for its own proposal in round 1", input_event: Some(Event::Vote( - Vote::new_prevote(Height::new(1), Round::new(1), Some(value.id()), addr1) - .signed(&sk1), + Vote::new_prevote(height, Round::new(1), Some(value.id()), addr1).signed(&sk1), )), expected_output: None, expected_round: Round::new(0), new_state: State { - height: Height::new(1), + height, round: Round::new(0), step: Step::Prevote, proposal: None, @@ -1208,15 +1208,14 @@ fn driver_steps_skip_round_quorum_threshold() { TestStep { desc: "v2 prevotes for v1 proposal, we get +1/3 messages from future round", input_event: Some(Event::Vote( - Vote::new_prevote(Height::new(1), Round::new(1), Some(value.id()), addr2) - .signed(&sk2), + Vote::new_prevote(height, Round::new(1), Some(value.id()), addr2).signed(&sk2), )), - expected_output: Some(Message::NewRound(Height::new(1), Round::new(1))), - expected_round: Round::new(0), + expected_output: Some(Message::NewRound(height, Round::new(1))), + expected_round: Round::new(1), new_state: State { - height: Height::new(1), - round: Round::new(0), - step: Step::Prevote, + height, + round: Round::new(1), + step: Step::NewRound, proposal: None, locked: None, valid: None, @@ -1234,7 +1233,9 @@ fn driver_steps_skip_round_quorum_threshold() { .unwrap_or_else(|| previous_message.unwrap()); let output = block_on(driver.execute(execute_message)).expect("execute succeeded"); - // assert_eq!(output, step.expected_output, "expected output message"); + assert_eq!(output, step.expected_output, "expected output message"); + + assert_eq!(driver.round(), step.expected_round, "expected round"); assert_eq!(driver.round_state, step.new_state, "new state"); diff --git a/Code/test/tests/vote_keeper.rs b/Code/test/tests/vote_keeper.rs index 3dc615a42..f1d6d7849 100644 --- a/Code/test/tests/vote_keeper.rs +++ b/Code/test/tests/vote_keeper.rs @@ -11,17 +11,18 @@ const ADDRESS4: Address = Address::new([44; 20]); #[test] fn prevote_apply_nil() { let mut keeper: VoteKeeper = VoteKeeper::new(3, Default::default()); + let height = Height::new(1); let round = Round::new(0); - let vote = Vote::new_prevote(Height::new(1), round, None, ADDRESS1); + let vote = Vote::new_prevote(height, round, None, ADDRESS1); let msg = keeper.apply_vote(vote.clone(), 1, round); assert_eq!(msg, None); - let vote = Vote::new_prevote(Height::new(1), round, None, ADDRESS2); + let vote = Vote::new_prevote(height, round, None, ADDRESS2); let msg = keeper.apply_vote(vote.clone(), 1, round); assert_eq!(msg, None); - let vote = Vote::new_prevote(Height::new(1), round, None, ADDRESS3); + let vote = Vote::new_prevote(height, round, None, ADDRESS3); let msg = keeper.apply_vote(vote, 1, round); assert_eq!(msg, Some(Message::PolkaNil)); } @@ -29,17 +30,18 @@ fn prevote_apply_nil() { #[test] fn precommit_apply_nil() { let mut keeper: VoteKeeper = VoteKeeper::new(3, Default::default()); + let height = Height::new(1); let round = Round::new(0); - let vote = Vote::new_precommit(Height::new(1), round, None, ADDRESS1); + let vote = Vote::new_precommit(height, round, None, ADDRESS1); let msg = keeper.apply_vote(vote.clone(), 1, round); assert_eq!(msg, None); - let vote = Vote::new_precommit(Height::new(1), round, None, ADDRESS2); + let vote = Vote::new_precommit(height, Round::new(0), None, ADDRESS2); let msg = keeper.apply_vote(vote.clone(), 1, round); assert_eq!(msg, None); - let vote = Vote::new_precommit(Height::new(1), round, None, ADDRESS3); + 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)); } @@ -48,70 +50,73 @@ fn precommit_apply_nil() { fn prevote_apply_single_value() { let mut keeper: VoteKeeper = VoteKeeper::new(4, Default::default()); - let v = ValueId::new(1); - let val = Some(v); + let id = ValueId::new(1); + let val = Some(id); + let height = Height::new(1); let round = Round::new(0); - let vote = Vote::new_prevote(Height::new(1), round, val, ADDRESS1); + let vote = Vote::new_prevote(height, Round::new(0), val, ADDRESS1); let msg = keeper.apply_vote(vote.clone(), 1, round); assert_eq!(msg, None); - let vote = Vote::new_prevote(Height::new(1), round, val, ADDRESS2); + let vote = Vote::new_prevote(height, Round::new(0), val, ADDRESS2); let msg = keeper.apply_vote(vote.clone(), 1, round); assert_eq!(msg, None); - let vote_nil = Vote::new_prevote(Height::new(1), round, None, ADDRESS3); + 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)); - let vote = Vote::new_prevote(Height::new(1), round, val, ADDRESS4); + 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(v))); + assert_eq!(msg, Some(Message::PolkaValue(id))); } #[test] fn precommit_apply_single_value() { let mut keeper: VoteKeeper = VoteKeeper::new(4, Default::default()); - let v = ValueId::new(1); - let val = Some(v); + let id = ValueId::new(1); + let val = Some(id); + let height = Height::new(1); let round = Round::new(0); - let vote = Vote::new_precommit(Height::new(1), round, val, ADDRESS1); + let vote = Vote::new_precommit(height, Round::new(0), val, ADDRESS1); let msg = keeper.apply_vote(vote.clone(), 1, round); assert_eq!(msg, None); - let vote = Vote::new_precommit(Height::new(1), round, val, ADDRESS2); + let vote = Vote::new_precommit(height, Round::new(0), val, ADDRESS2); let msg = keeper.apply_vote(vote.clone(), 1, round); assert_eq!(msg, None); - let vote_nil = Vote::new_precommit(Height::new(1), round, None, ADDRESS3); + 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)); - let vote = Vote::new_precommit(Height::new(1), round, val, ADDRESS4); + 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(v))); + assert_eq!(msg, Some(Message::PrecommitValue(id))); } #[test] fn skip_round_small_quorum_prevotes_two_vals() { let mut keeper: VoteKeeper = VoteKeeper::new(4, Default::default()); - let v = ValueId::new(1); - let val = Some(v); + let id = ValueId::new(1); + let val = Some(id); + let height = Height::new(1); let cur_round = Round::new(0); let fut_round = Round::new(1); - let vote = Vote::new_prevote(Height::new(1), cur_round, val, ADDRESS1); + let vote = Vote::new_prevote(height, cur_round, val, ADDRESS1); let msg = keeper.apply_vote(vote.clone(), 1, cur_round); assert_eq!(msg, None); - let vote = Vote::new_prevote(Height::new(1), fut_round, val, ADDRESS2); + let vote = Vote::new_prevote(height, fut_round, val, ADDRESS2); let msg = keeper.apply_vote(vote.clone(), 1, cur_round); assert_eq!(msg, None); - let vote = Vote::new_prevote(Height::new(1), fut_round, val, ADDRESS3); + 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)))); } @@ -120,20 +125,21 @@ fn skip_round_small_quorum_prevotes_two_vals() { fn skip_round_small_quorum_with_prevote_precommit_two_vals() { let mut keeper: VoteKeeper = VoteKeeper::new(4, Default::default()); - let v = ValueId::new(1); - let val = Some(v); + let id = ValueId::new(1); + let val = Some(id); + let height = Height::new(1); let cur_round = Round::new(0); let fut_round = Round::new(1); - let vote = Vote::new_prevote(Height::new(1), cur_round, val, ADDRESS1); + let vote = Vote::new_prevote(height, cur_round, val, ADDRESS1); let msg = keeper.apply_vote(vote.clone(), 1, cur_round); assert_eq!(msg, None); - let vote = Vote::new_prevote(Height::new(1), fut_round, val, ADDRESS2); + let vote = Vote::new_prevote(height, fut_round, val, ADDRESS2); let msg = keeper.apply_vote(vote.clone(), 1, cur_round); assert_eq!(msg, None); - let vote = Vote::new_precommit(Height::new(1), fut_round, val, ADDRESS3); + 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)))); } @@ -142,20 +148,21 @@ fn skip_round_small_quorum_with_prevote_precommit_two_vals() { fn skip_round_full_quorum_with_prevote_precommit_two_vals() { let mut keeper: VoteKeeper = VoteKeeper::new(5, Default::default()); - let v = ValueId::new(1); - let val = Some(v); + let id = ValueId::new(1); + let val = Some(id); + let height = Height::new(1); let cur_round = Round::new(0); let fut_round = Round::new(1); - let vote = Vote::new_prevote(Height::new(1), cur_round, val, ADDRESS1); + let vote = Vote::new_prevote(height, cur_round, val, ADDRESS1); let msg = keeper.apply_vote(vote.clone(), 1, cur_round); assert_eq!(msg, None); - let vote = Vote::new_prevote(Height::new(1), fut_round, val, ADDRESS2); + let vote = Vote::new_prevote(height, fut_round, val, ADDRESS2); let msg = keeper.apply_vote(vote.clone(), 1, cur_round); assert_eq!(msg, None); - let vote = Vote::new_precommit(Height::new(1), fut_round, val, ADDRESS3); + 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)))); } @@ -164,20 +171,21 @@ fn skip_round_full_quorum_with_prevote_precommit_two_vals() { fn no_skip_round_small_quorum_with_same_val() { let mut keeper: VoteKeeper = VoteKeeper::new(4, Default::default()); - let v = ValueId::new(1); - let val = Some(v); + let id = ValueId::new(1); + let val = Some(id); + let height = Height::new(1); let cur_round = Round::new(0); let fut_round = Round::new(1); - let vote = Vote::new_prevote(Height::new(1), cur_round, val, ADDRESS1); + let vote = Vote::new_prevote(height, cur_round, val, ADDRESS1); let msg = keeper.apply_vote(vote.clone(), 1, cur_round); assert_eq!(msg, None); - let vote = Vote::new_prevote(Height::new(1), fut_round, val, ADDRESS2); + let vote = Vote::new_prevote(height, fut_round, val, ADDRESS2); let msg = keeper.apply_vote(vote.clone(), 1, cur_round); assert_eq!(msg, None); - let vote = Vote::new_precommit(Height::new(1), fut_round, val, ADDRESS2); + let vote = Vote::new_precommit(height, fut_round, val, ADDRESS2); let msg = keeper.apply_vote(vote, 1, cur_round); assert_eq!(msg, None); } @@ -186,20 +194,21 @@ fn no_skip_round_small_quorum_with_same_val() { fn no_skip_round_full_quorum_with_same_val() { let mut keeper: VoteKeeper = VoteKeeper::new(5, Default::default()); - let v = ValueId::new(1); - let val = Some(v); + let id = ValueId::new(1); + let val = Some(id); + let height = Height::new(1); let cur_round = Round::new(0); let fut_round = Round::new(1); - let vote = Vote::new_prevote(Height::new(1), cur_round, val, ADDRESS1); + let vote = Vote::new_prevote(height, cur_round, val, ADDRESS1); let msg = keeper.apply_vote(vote.clone(), 1, cur_round); assert_eq!(msg, None); - let vote = Vote::new_prevote(Height::new(1), fut_round, val, ADDRESS2); + let vote = Vote::new_prevote(height, fut_round, val, ADDRESS2); let msg = keeper.apply_vote(vote.clone(), 1, cur_round); assert_eq!(msg, None); - let vote = Vote::new_precommit(Height::new(1), fut_round, val, ADDRESS2); + let vote = Vote::new_precommit(height, fut_round, val, ADDRESS2); let msg = keeper.apply_vote(vote, 2, cur_round); assert_eq!(msg, None); }