Skip to content

Commit

Permalink
add check_double_prevote & check_double_precommit function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongseup committed Aug 27, 2023
1 parent 5e4b8b0 commit ff72378
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions vetomint/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod misbehavior;
mod progress;
mod state;

Expand Down
76 changes: 76 additions & 0 deletions vetomint/src/misbehavior.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use super::*;
use state::*;

use std::collections::HashMap;

/// Check whether there are double prevotes in target round
pub(crate) fn check_double_prevote(
state: &ConsensusState,
target_round: Round,
) -> Vec<ConsensusResponse> {
let mut validators_map = HashMap::new();
let prevotes_in_target_round: Vec<_> = state
.prevotes
.iter()
.filter(|vote| vote.round == target_round)
.collect();

for vote in prevotes_in_target_round.iter() {
let (count, origin_proposal) = validators_map
.entry(vote.signer)
.or_insert((0, vote.proposal));
*count += 1;

if *count == 2 {
let byzantine_validator = vote.signer;
let double_proposal = vote.proposal;

return vec![ConsensusResponse::ViolationReport {
violator: byzantine_validator,
misbehavior: Misbehavior::DoublePrevote {
byzantine_node: byzantine_validator,
round: target_round,
proposals: (*origin_proposal, double_proposal),
},
}];
}
}

Vec::new()
}

/// Check whether there are double precommits in target round
pub(crate) fn check_double_precommit(
state: &ConsensusState,
target_round: Round,
) -> Vec<ConsensusResponse> {
let mut validators_map = HashMap::new();
let precommits_in_target_round: Vec<_> = state
.precommits
.iter()
.filter(|vote| vote.round == target_round)
.collect();

for vote in precommits_in_target_round.iter() {
let (count, origin_proposal) = validators_map
.entry(vote.signer)
.or_insert((0, vote.proposal));
*count += 1;

if *count == 2 {
let byzantine_validator = vote.signer;
let double_proposal = vote.proposal;

return vec![ConsensusResponse::ViolationReport {
violator: byzantine_validator,
misbehavior: Misbehavior::DoublePrecommit {
byzantine_node: byzantine_validator,
round: target_round,
proposals: (*origin_proposal, double_proposal),
},
}];
}
}

Vec::new()
}
2 changes: 2 additions & 0 deletions vetomint/src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub(crate) fn progress(
response.extend(on_4f_nil_prevote(state, round));
}
response.extend(on_5f_prevote(state, round, proposal));
response.extend(misbehavior::check_double_prevote(state, round));
response
}
ConsensusEvent::Precommit {
Expand All @@ -104,6 +105,7 @@ pub(crate) fn progress(
if let Some(proposal) = proposal {
response.extend(on_4f_non_nil_precommit(state, round, proposal));
}
response.extend(misbehavior::check_double_precommit(state, round));
response
}
ConsensusEvent::Timer => {
Expand Down

0 comments on commit ff72378

Please sign in to comment.