Skip to content

Commit

Permalink
Merge branch 'main' into hvanz/mbt-votekeeper
Browse files Browse the repository at this point in the history
  • Loading branch information
hvanz committed Nov 22, 2023
2 parents 5f9484f + 2a7862b commit e62abb3
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 84 deletions.
21 changes: 14 additions & 7 deletions Code/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ctx::Value> {
self.env
.get_value(self.round_state.height.clone(), self.round_state.round)
.get_value(self.height().clone(), self.round())
.await
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
};
Expand All @@ -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<RoundMessage<Ctx>> {
Expand Down
65 changes: 33 additions & 32 deletions Code/test/tests/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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");

Expand Down
Loading

0 comments on commit e62abb3

Please sign in to comment.