Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function for checking misbehaviors in Vetomint #495

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
131 changes: 131 additions & 0 deletions vetomint/src/misbehavior.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use super::*;
use state::*;

use std::collections::HashMap;

/// comment for check_double_proposal
pub(crate) fn check_double_proposal(
state: &ConsensusState,
target_round: Round,
) -> Vec<ConsensusResponse> {
let proposals: Vec<_> = state
.proposals
.iter()
.filter(|(_, proposal)| proposal.round == target_round)
.map(|(_, proposal)| proposal)
.collect();

if proposals.len() > 1 {
let byzantine_signer = proposals[0].proposer;
let (origin_proposal, new_proposal) = (proposals[0].proposal, proposals[1].proposal);

return vec![ConsensusResponse::ViolationReport {
violator: 0,
misbehavior: Misbehavior::DoubleProposal {
byzantine_node: byzantine_signer,
round: target_round,
proposals: (origin_proposal, new_proposal),
},
}];
}

Vec::new()
}

/// comment for check_double_prevote
pub(crate) fn check_double_prevote(
state: &ConsensusState,
target_round: Round,
) -> Vec<ConsensusResponse> {
let mut validators_map = HashMap::new();

for vote in state.prevotes.iter() {
let (count, origin_proposal) = validators_map
.entry(vote.signer)
.or_insert((0, vote.proposal));

*count += 1;

if *count == 2 {
let new_proposal = vote.proposal;
let byzantine_signer = vote.signer;

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

Vec::new()
}

/// comment for check_double_precommit
pub(crate) fn check_double_precommit(
state: &ConsensusState,
target_round: Round,
) -> Vec<ConsensusResponse> {
let mut validators_map = HashMap::new();

for vote in state.precommits.iter() {
let (count, origin_proposal) = validators_map
.entry(vote.signer)
.or_insert((0, vote.proposal));

*count += 1;

if *count == 2 {
let byzantine_signer = vote.signer;
let new_proposal = vote.proposal;

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

Vec::new()
}

/// comment for check_invalid_proposal
pub(crate) fn check_invalid_proposal(
byzantine_proposer: usize,
target_round: Round,
target_proposal: BlockIdentifier,
) -> Vec<ConsensusResponse> {
return vec![ConsensusResponse::ViolationReport {
violator: byzantine_proposer,
misbehavior: Misbehavior::InvalidProposal {
byzantine_node: byzantine_proposer,
round: target_round,
proposal: target_proposal,
},
}];
}

/// comment for check_invalid_prevote
pub(crate) fn check_invalid_prevote(
state: &ConsensusState,
target_round: Round,
target_proposal: BlockIdentifier,
) -> Vec<ConsensusResponse> {
unimplemented!()
}

/// comment for check_invalid_precommit
pub(crate) fn check_invalid_precommit(
state: &ConsensusState,
check_round: Round,
check_proposal: BlockIdentifier,
) -> Vec<ConsensusResponse> {
unimplemented!()
}
7 changes: 7 additions & 0 deletions vetomint/src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub(crate) fn progress(
round,
favor,
} => {
if valid == false {
// TODO: add possible invalid proposal logcis
return misbehavior::check_invalid_proposal(proposer, round, proposal);
}
state.proposals.insert(
proposal,
Proposal {
Expand All @@ -46,6 +50,7 @@ pub(crate) fn progress(
state, round, proposal,
));
response.extend(on_4f_non_nil_precommit(state, round, proposal));
response.extend(misbehavior::check_double_proposal(state, round));
response
}
ConsensusEvent::SkipRound { round } => progress(
Expand Down Expand Up @@ -86,6 +91,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 +110,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