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 check mishavior function with misbehavior enum in vetomint #1

Closed
Closed
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
9 changes: 7 additions & 2 deletions consensus/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl State {
}
ConsensusResponse::ViolationReport {
violator,
description,
misbehavior,
} => {
let pubkey = self
.block_header
Expand All @@ -368,7 +368,12 @@ impl State {
.0
.clone();
(
ProgressResult::ViolationReported(pubkey, description, timestamp),
// TODO: add misbehavior handling
ProgressResult::ViolationReported(
pubkey,
format!("{misbehavior:?}"),
timestamp,
),
None,
)
}
Expand Down
55 changes: 54 additions & 1 deletion vetomint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,59 @@ pub enum ConsensusEvent {
Timer,
}

/// The report and trace of a misbehavior committed by a malicious node.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum Misbehavior {
DoubleProposal {
/// The malicious node.
byzantine_node: ValidatorIndex,
/// The round in which the misbehavior is committed.
round: Round,
/// The two conflicting proposals.
proposals: (BlockIdentifier, BlockIdentifier),
},
DoublePrevote {
/// The malicious node.
byzantine_node: ValidatorIndex,
/// The round in which the misbehavior is committed.
round: Round,
/// The two conflicting proposals that the node has prevoted.
proposals: (Option<BlockIdentifier>, Option<BlockIdentifier>),
},
DoublePrecommit {
/// The malicious node.
byzantine_node: ValidatorIndex,
/// The round in which the misbehavior is committed.
round: Round,
/// The two conflicting proposals that the node has precommitted.
proposals: (Option<BlockIdentifier>, Option<BlockIdentifier>),
},
InvalidProposal {
/// The malicious node.
byzantine_node: ValidatorIndex,
/// The round in which the misbehavior is committed.
round: Round,
/// The proposal that the node has proposed.
proposal: BlockIdentifier,
},
InvalidPrevote {
/// The malicious node.
byzantine_node: ValidatorIndex,
/// The round in which the misbehavior is committed.
round: Round,
/// The proposal that the node has prevoted.
proposal: BlockIdentifier,
},
InvalidPrecommit {
/// The malicious node.
byzantine_node: ValidatorIndex,
/// The round in which the misbehavior is committed.
round: Round,
/// The proposal that the node has precommitted.
proposal: BlockIdentifier,
},
}

/// A response that the consensus might emit for a given event, which must be properly handled by the lower layer.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum ConsensusResponse {
Expand All @@ -84,7 +137,7 @@ pub enum ConsensusResponse {
},
ViolationReport {
violator: ValidatorIndex,
description: String,
misbehavior: Misbehavior,
},
}

Expand Down
185 changes: 185 additions & 0 deletions vetomint/src/misbehavior.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
use super::*;
use state::*;

use std::collections::HashMap;

pub(crate) fn check_misbehavior(
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> {
let proposals: Vec<_> = state
.proposals
.iter()
.filter(|(_, proposal)| proposal.round == check_round)
.map(|(_, proposal)| proposal)
.collect();

if proposals.len() > 1 {
// returnSome(result[0].1.proposer)
return Some(Misbehavior::DoubleProposal {
byzantine_node: proposals[0].proposer,
round: check_round,
proposals: (proposals[0].proposal, proposals[1].proposal),
});
}

None
}

fn check_double_prevote(
state: &ConsensusState,
check_round: Round,
check_proposal: BlockIdentifier,
) -> Option<Misbehavior> {
let mut validators_map = HashMap::new();

for vote in state.prevotes.iter() {
let count = validators_map.entry(vote.signer).or_insert(0);
*count += 1;

if *count == 2 {
return Some(Misbehavior::DoublePrevote {
byzantine_node: vote.signer,
round: check_round,
proposals: (Some(check_proposal), Some(check_proposal)),
});
}
}

None
}

fn check_double_precommit(
state: &ConsensusState,
check_round: Round,
check_proposal: BlockIdentifier,
) -> Option<Misbehavior> {
let mut validators_map = HashMap::new();

for vote in state.precommits.iter() {
let count = validators_map.entry(vote.signer).or_insert(0);
*count += 1;

if *count == 2 {
return Some(Misbehavior::DoublePrecommit {
byzantine_node: vote.signer,
round: check_round,
proposals: (Some(check_proposal), Some(check_proposal)),
});
}
}

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
}
Loading