-
Notifications
You must be signed in to change notification settings - Fork 51
Block double vote attempts #1800
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
Merged
foriequal0
merged 5 commits into
CodeChain-io:master
from
foriequal0:fix/block-attempt-to-double-vote
Sep 30, 2019
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2bc17e5
Cleanup vote functions
foriequal0 57f220a
Add VoteRegressionChecker
foriequal0 03bfe3b
Rename VoteCollector::vote to collect
foriequal0 27cd4c5
Fix to use Err(DoubleVote) from VoteCollector
foriequal0 537b86f
Merge branch 'master' into fix/block-attempt-to-double-vote
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
core/src/consensus/tendermint/vote_regression_checker.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
use consensus::{Step, VoteOn}; | ||
use std::cmp::Ordering; | ||
|
||
pub struct VoteRegressionChecker { | ||
last_vote: Option<VoteOn>, | ||
} | ||
|
||
impl VoteRegressionChecker { | ||
pub fn new() -> VoteRegressionChecker { | ||
VoteRegressionChecker { | ||
last_vote: None, | ||
} | ||
} | ||
|
||
pub fn check(&mut self, vote_on: &VoteOn) -> bool { | ||
assert!(match vote_on.step.step { | ||
Step::Propose | Step::Prevote | Step::Precommit => true, | ||
_ => false, | ||
}); | ||
|
||
let monotonic = if let Some(last_vote) = &self.last_vote { | ||
match last_vote.step.cmp(&vote_on.step) { | ||
Ordering::Less => true, | ||
Ordering::Greater => false, | ||
Ordering::Equal => last_vote.block_hash == vote_on.block_hash, | ||
} | ||
} else { | ||
true | ||
}; | ||
|
||
if monotonic { | ||
self.last_vote = Some(vote_on.clone()); | ||
} | ||
monotonic | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use consensus::VoteStep; | ||
use primitives::H256; | ||
|
||
#[test] | ||
fn test_initial_set() { | ||
let mut checker = VoteRegressionChecker::new(); | ||
|
||
let random_step = VoteStep::new(100, 10, Step::Prevote); | ||
let random_hash = Some(H256::random()); | ||
assert!(checker.check(&VoteOn { | ||
step: random_step, | ||
block_hash: random_hash | ||
})) | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_disallow_commit() { | ||
let mut checker = VoteRegressionChecker::new(); | ||
|
||
let random_commit_step = VoteStep::new(100, 10, Step::Commit); | ||
let random_hash = Some(H256::random()); | ||
assert!(checker.check(&VoteOn { | ||
step: random_commit_step, | ||
block_hash: random_hash | ||
})) | ||
} | ||
|
||
#[test] | ||
fn test_allow_height_increase() { | ||
let mut checker = VoteRegressionChecker::new(); | ||
|
||
checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Prevote), | ||
block_hash: Some(H256::from(1)), | ||
}); | ||
|
||
assert!(checker.check(&VoteOn { | ||
step: VoteStep::new(101, 10, Step::Prevote), | ||
block_hash: Some(H256::from(2)) | ||
})) | ||
} | ||
|
||
#[test] | ||
fn test_disallow_height_decrease() { | ||
let mut checker = VoteRegressionChecker::new(); | ||
|
||
checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Prevote), | ||
block_hash: Some(H256::from(1)), | ||
}); | ||
|
||
assert!(!checker.check(&VoteOn { | ||
step: VoteStep::new(99, 10, Step::Prevote), | ||
block_hash: Some(H256::from(2)) | ||
})) | ||
} | ||
|
||
#[test] | ||
fn test_allow_view_increase() { | ||
let mut checker = VoteRegressionChecker::new(); | ||
|
||
checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Prevote), | ||
block_hash: Some(H256::from(1)), | ||
}); | ||
|
||
assert!(checker.check(&VoteOn { | ||
step: VoteStep::new(100, 11, Step::Prevote), | ||
block_hash: Some(H256::from(2)) | ||
})) | ||
} | ||
|
||
#[test] | ||
fn test_disallow_view_decrease() { | ||
let mut checker = VoteRegressionChecker::new(); | ||
|
||
checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Prevote), | ||
block_hash: Some(H256::from(1)), | ||
}); | ||
|
||
assert!(!checker.check(&VoteOn { | ||
step: VoteStep::new(100, 9, Step::Prevote), | ||
block_hash: Some(H256::from(2)) | ||
})) | ||
} | ||
|
||
#[test] | ||
fn test_allow_step_increased() { | ||
let mut checker = VoteRegressionChecker::new(); | ||
|
||
checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Prevote), | ||
block_hash: Some(H256::from(1)), | ||
}); | ||
|
||
assert!(checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Precommit), | ||
block_hash: Some(H256::from(2)) | ||
})) | ||
} | ||
|
||
#[test] | ||
fn test_disallow_step_decreased() { | ||
let mut checker = VoteRegressionChecker::new(); | ||
|
||
checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Prevote), | ||
block_hash: Some(H256::from(1)), | ||
}); | ||
|
||
assert!(!checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Propose), | ||
block_hash: Some(H256::from(2)) | ||
})) | ||
} | ||
|
||
#[test] | ||
fn test_allow_same_hash() { | ||
let mut checker = VoteRegressionChecker::new(); | ||
|
||
let block_hash = Some(H256::random()); | ||
checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Prevote), | ||
block_hash, | ||
}); | ||
|
||
assert!(checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Prevote), | ||
block_hash, | ||
})) | ||
} | ||
|
||
#[test] | ||
fn test_disallow_hash_change() { | ||
let mut checker = VoteRegressionChecker::new(); | ||
|
||
checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Prevote), | ||
block_hash: Some(H256::from(1)), | ||
}); | ||
|
||
assert!(!checker.check(&VoteOn { | ||
step: VoteStep::new(100, 10, Step::Prevote), | ||
block_hash: Some(H256::from(2)) | ||
})) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.