From e51d3a0a5c836f000d6bc7048ef0383d5a348c65 Mon Sep 17 00:00:00 2001 From: Jeongseup Date: Sat, 12 Aug 2023 00:50:51 +0900 Subject: [PATCH] update misbehavior --- vetomint/src/misbehavior.rs | 87 +++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/vetomint/src/misbehavior.rs b/vetomint/src/misbehavior.rs index 20384811..a7a59f41 100644 --- a/vetomint/src/misbehavior.rs +++ b/vetomint/src/misbehavior.rs @@ -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 } @@ -96,3 +114,72 @@ fn check_double_precommit( None } + +fn check_invalid_proposal( + state: &ConsensusState, + check_proposal: BlockIdentifier, +) -> Option { + 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 { + 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 { + 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 +}