-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add check_double_prevote & check_double_precommit function
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod misbehavior; | ||
mod progress; | ||
mod state; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters