From def58735a2282cb3b657c8baf35a933faaa0abd2 Mon Sep 17 00:00:00 2001 From: Jeongseup Date: Sat, 12 Aug 2023 01:23:19 +0900 Subject: [PATCH] update misbehavior --- vetomint/src/misbehavior.rs | 88 ++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/vetomint/src/misbehavior.rs b/vetomint/src/misbehavior.rs index 20384811..99f433ab 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 } @@ -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, @@ -96,3 +113,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 +}