Skip to content

Commit

Permalink
refactor(consensus): use check_precommit/prevote_quorum in handle_pre…
Browse files Browse the repository at this point in the history
…commit/prevote (#68)
  • Loading branch information
asmaastarkware authored Jul 29, 2024
1 parent cbe5fb5 commit ca2d126
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions crates/sequencing/papyrus_consensus/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,14 @@ impl StateMachine {

// A prevote from a peer (or self) node.
fn handle_prevote(&mut self, block_hash: BlockHash, round: u32) -> VecDeque<StateMachineEvent> {
assert_eq!(round, 0, "Only round 0 is supported in this milestone.");
let prevote_count = self.prevotes.entry(round).or_default().entry(block_hash).or_insert(0);
// TODO(matan): Use variable weight.
*prevote_count += 1;
if *prevote_count < self.quorum {
return VecDeque::new();
}
if self.step != Step::Prevote {

if self.step != Step::Prevote || round != self.round {
return VecDeque::new();
}

self.send_precommit(block_hash, round)
self.check_prevote_quorum(round)
}

// A precommit from a peer (or self) node.
Expand All @@ -229,21 +225,12 @@ impl StateMachine {
block_hash: BlockHash,
round: u32,
) -> VecDeque<StateMachineEvent> {
assert_eq!(round, 0, "Only round 0 is supported in this milestone.");
let precommit_count =
self.precommits.entry(round).or_default().entry(block_hash).or_insert(0);
// TODO(matan): Use variable weight.
*precommit_count += 1;
if *precommit_count < self.quorum {
return VecDeque::new();
}
let Some(proposed_value) = self.proposals.get(&round) else {
return VecDeque::new();
};
// TODO(matan): Handle this due to malicious proposer.
assert_eq!(*proposed_value, block_hash, "Proposal should match quorum.");

VecDeque::from([StateMachineEvent::Decision(block_hash, round)])
self.check_precommit_quorum(round)
}

fn advance_to_step(&mut self, step: Step) -> VecDeque<StateMachineEvent> {
Expand All @@ -265,6 +252,12 @@ impl StateMachine {
if *count < self.quorum {
return VecDeque::new();
}
let Some(proposed_value) = self.proposals.get(&round) else {
return VecDeque::new();
};
// TODO(matan): Handle this due to malicious proposer.
assert_eq!(proposed_value, block_hash, "Proposal should match quorum.");

self.send_precommit(*block_hash, round)
}

Expand All @@ -275,6 +268,12 @@ impl StateMachine {
if *count < self.quorum {
return VecDeque::new();
}
let Some(proposed_value) = self.proposals.get(&round) else {
return VecDeque::new();
};
// TODO(matan): Handle this due to malicious proposer.
assert_eq!(proposed_value, block_hash, "Proposal should match quorum.");

VecDeque::from([StateMachineEvent::Decision(*block_hash, round)])
}

Expand Down

0 comments on commit ca2d126

Please sign in to comment.