Skip to content

Commit

Permalink
update misbehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongseup committed Aug 11, 2023
1 parent 1f9ac4b commit def5873
Showing 1 changed file with 87 additions and 1 deletion.
88 changes: 87 additions & 1 deletion vetomint/src/misbehavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ pub(crate) fn check_misbehavior(
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
}

Expand All @@ -40,7 +58,6 @@ fn check_double_proposals(state: &ConsensusState, check_round: Round) -> Option<
.collect();

if proposals.len() > 1 {
// returnSome(result[0].1.proposer)
return Some(Misbehavior::DoubleProposal {
byzantine_node: proposals[0].proposer,
round: check_round,
Expand Down Expand Up @@ -96,3 +113,72 @@ fn check_double_precommit(

None
}

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
}

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
}

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
}

0 comments on commit def5873

Please sign in to comment.