Skip to content

Commit

Permalink
modify misbehavior module in vetomint
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongseup committed Aug 27, 2023
1 parent def5873 commit 3e047e6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 134 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
215 changes: 81 additions & 134 deletions vetomint/src/misbehavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,182 +3,129 @@ use state::*;

use std::collections::HashMap;

pub(crate) fn check_misbehavior(
/// comment for check_double_proposal
pub(crate) fn check_double_proposal(
state: &ConsensusState,
check_round: Round,
check_proposal: BlockIdentifier,
) -> Vec<Misbehavior> {
let mut misbehavior: Vec<Misbehavior> = vec![];

if let Some(m) = check_double_proposals(state, check_round) {
misbehavior.push(m);
} else {
println!("not found double proposals in this round");
}

if let Some(m) = check_double_prevote(state, check_round, check_proposal) {
misbehavior.push(m);
} else {
println!("not found double prevote in this round");
}

if let Some(m) = check_double_precommit(state, check_round, check_proposal) {
misbehavior.push(m);
} else {
println!("not found double precommit in this round");
}

if let Some(m) = check_invalid_proposal(state, check_proposal) {
misbehavior.push(m);
} else {
println!("not found invalid proposal in this round");
}

if let Some(m) = check_invalid_prevote(state, check_round, check_proposal) {
misbehavior.push(m);
} else {
println!("not found double precommit in this round");
}

if let Some(m) = check_invalid_precommit(state, check_round, check_proposal) {
misbehavior.push(m);
} else {
println!("not found double precommit in this round");
}

misbehavior
}

fn check_double_proposals(state: &ConsensusState, check_round: Round) -> Option<Misbehavior> {
target_round: Round,
) -> Vec<ConsensusResponse> {
let proposals: Vec<_> = state
.proposals
.iter()
.filter(|(_, proposal)| proposal.round == check_round)
.filter(|(_, proposal)| proposal.round == target_round)
.map(|(_, proposal)| proposal)
.collect();

if proposals.len() > 1 {
return Some(Misbehavior::DoubleProposal {
byzantine_node: proposals[0].proposer,
round: check_round,
proposals: (proposals[0].proposal, proposals[1].proposal),
});
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),
},
}];
}

None
Vec::new()
}

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

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

*count += 1;

if *count == 2 {
return Some(Misbehavior::DoublePrevote {
byzantine_node: vote.signer,
round: check_round,
proposals: (Some(check_proposal), Some(check_proposal)),
});
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),
},
}];
}
}

None
Vec::new()
}

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

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

*count += 1;

if *count == 2 {
return Some(Misbehavior::DoublePrecommit {
byzantine_node: vote.signer,
round: check_round,
proposals: (Some(check_proposal), Some(check_proposal)),
});
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),
},
}];
}
}

None
Vec::new()
}

fn check_invalid_proposal(
state: &ConsensusState,
check_proposal: BlockIdentifier,
) -> Option<Misbehavior> {
if let Some(proposal) = state.proposals.get(&check_proposal) {
if proposal.valid == false {
return Some(Misbehavior::InvalidProposal {
byzantine_node: proposal.proposer,
round: proposal.round,
proposal: proposal.proposal,
});
}
}

None
/// 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,
},
}];
}

fn check_invalid_prevote(
/// comment for check_invalid_prevote
pub(crate) fn check_invalid_prevote(
state: &ConsensusState,
check_round: Round,
check_proposal: BlockIdentifier,
) -> Option<Misbehavior> {
let valid_prevotes: Vec<_> = state
.prevotes
.iter()
.filter(|prevote| prevote.round == check_round)
.collect();

for prevote in valid_prevotes.iter() {
if let Some(proposal) = prevote.proposal {
if proposal == check_proposal {
return Some(Misbehavior::InvalidPrevote {
byzantine_node: prevote.signer,
round: prevote.round,
proposal: proposal,
});
}
}
}

None
target_round: Round,
target_proposal: BlockIdentifier,
) -> Vec<ConsensusResponse> {
unimplemented!()
}

fn check_invalid_precommit(
/// comment for check_invalid_precommit
pub(crate) fn check_invalid_precommit(
state: &ConsensusState,
check_round: Round,
check_proposal: BlockIdentifier,
) -> Option<Misbehavior> {
let valid_precommits: Vec<_> = state
.precommits
.iter()
.filter(|prevote| prevote.round == check_round)
.collect();

for precommit in valid_precommits.iter() {
if let Some(proposal) = precommit.proposal {
if proposal == check_proposal {
return Some(Misbehavior::InvalidPrecommit {
byzantine_node: precommit.signer,
round: precommit.round,
proposal: proposal,
});
}
}
}

None
) -> 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

0 comments on commit 3e047e6

Please sign in to comment.