-
Notifications
You must be signed in to change notification settings - Fork 48
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
Double Voting Prevention #3971
Closed
Closed
Double Voting Prevention #3971
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e1bff2
add_vote_tracker.rs
qvkare beef8f4
add_vote_tracker_consensus.rs
qvkare f9a8285
Merge branch 'main' into main
qvkare 1b2d46e
Merge branch 'EspressoSystems:main' into main
qvkare 6450686
update consensus.rs
qvkare 2184897
update vote_tracker.rs
qvkare 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 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 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,110 @@ | ||
// Copyright (c) 2021-2024 Espresso Systems (espressosys.com) | ||
// This file is part of the HotShot repository. | ||
|
||
// You should have received a copy of the MIT License | ||
// along with the HotShot repository. If not, see <https://mit-license.org/>. | ||
|
||
//! Vote tracking implementation for preventing double voting | ||
|
||
use std::collections::{HashMap, HashSet}; | ||
use std::sync::Arc; | ||
|
||
use crate::traits::node_implementation::NodeType; | ||
use crate::traits::signature_key::SignatureKey; | ||
|
||
/// Tracks votes to prevent double voting in HotShot consensus | ||
#[derive(Debug, Default)] | ||
pub struct VoteTracker<TYPES: NodeType> { | ||
/// Maps view numbers to sets of voter public keys | ||
view_votes: HashMap<TYPES::View, HashSet<Arc<TYPES::SignatureKey>>>, | ||
} | ||
|
||
impl<TYPES: NodeType> VoteTracker<TYPES> { | ||
/// Creates a new VoteTracker instance | ||
pub fn new() -> Self { | ||
Self { | ||
view_votes: HashMap::new(), | ||
} | ||
} | ||
|
||
/// Records a vote for a specific view and voter | ||
/// Returns true if the vote was successfully recorded (no double voting) | ||
/// Returns false if this would be a double vote | ||
pub fn record_vote( | ||
&mut self, | ||
view: TYPES::View, | ||
voter_key: Arc<TYPES::SignatureKey>, | ||
) -> bool { | ||
// Get or create the vote set for this view | ||
let votes = self.view_votes.entry(view).or_default(); | ||
|
||
// Check if this voter has already voted | ||
if votes.contains(&voter_key) { | ||
return false; | ||
} | ||
|
||
// Record the vote | ||
votes.insert(voter_key); | ||
true | ||
} | ||
|
||
/// Cleans up old vote records for views that are no longer needed | ||
/// This should be called periodically to prevent memory growth | ||
pub fn cleanup_old_views(&mut self, current_view: TYPES::View) { | ||
self.view_votes.retain(|view, _| *view >= current_view); | ||
} | ||
|
||
/// Returns true if the given voter has already voted in the specified view | ||
pub fn has_voted( | ||
&self, | ||
view: &TYPES::View, | ||
voter_key: &Arc<TYPES::SignatureKey>, | ||
) -> bool { | ||
self.view_votes | ||
.get(view) | ||
.map_or(false, |votes| votes.contains(voter_key)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::simple_vote::SimpleVote; | ||
use std::sync::Arc; | ||
|
||
#[test] | ||
fn test_vote_tracking() { | ||
let mut tracker = VoteTracker::<SimpleVote>::new(); | ||
let view = SimpleVote::View::default(); | ||
let key1 = Arc::new(SimpleVote::SignatureKey::default()); | ||
let key2 = Arc::new(SimpleVote::SignatureKey::default()); | ||
|
||
// First vote should succeed | ||
assert!(tracker.record_vote(view, key1.clone())); | ||
|
||
// Double vote should fail | ||
assert!(!tracker.record_vote(view, key1.clone())); | ||
|
||
// Different voter should succeed | ||
assert!(tracker.record_vote(view, key2.clone())); | ||
} | ||
|
||
#[test] | ||
fn test_cleanup() { | ||
let mut tracker = VoteTracker::<SimpleVote>::new(); | ||
let view1 = SimpleVote::View::default(); | ||
let view2 = SimpleVote::View::default() + SimpleVote::View::from(1); | ||
let key = Arc::new(SimpleVote::SignatureKey::default()); | ||
|
||
tracker.record_vote(view1, key.clone()); | ||
tracker.record_vote(view2, key.clone()); | ||
|
||
// Cleanup views before view2 | ||
tracker.cleanup_old_views(view2); | ||
|
||
// Old vote should be removed | ||
assert!(!tracker.has_voted(&view1, &key)); | ||
// Recent vote should remain | ||
assert!(tracker.has_voted(&view2, &key)); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this open us up tot he opposite problem would have? Vote in view
n
, then viewn+1
then we can vote for viewn
again? The old code of course just allows any duplicate vote so there is an improvement with this code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bfish713 I have implemented a robust system for tracking view sequences. Added a queue mechanism that handles out-of-order votes. This prevents double voting while allowing the network to be resilient. Sorry, I missed the importance of the TODO comment. Comprehensive test coverage of both epoch transitions and network partitioning scenarios is included in this solution.