-
Notifications
You must be signed in to change notification settings - Fork 28
feat(contract): introduce generic voting structs #2739
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
Closed
kevindeforth
wants to merge
6
commits into
main
from
kd/1573-mpc-contract-introduce-generic-voting-struct
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bc6b41c
rename votes to threshold_votes and introduce generic voting structs
kevindeforth bcbea9f
fix typos
kevindeforth ecbee82
some nits
kevindeforth 63bfbe4
use ProposalHash, instead of hash map
kevindeforth 8100660
improve expect message and comments
kevindeforth 6a579df
add debug assert
kevindeforth 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
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,137 @@ | ||
| use crate::primitives::thresholds::ThresholdParameters; | ||
| use crate::primitives::{key_state::AuthenticatedAccountId, participants::Participants}; | ||
| use near_sdk::{log, near}; | ||
| use std::collections::BTreeMap; | ||
|
|
||
| /// Tracks votes for ThresholdParameters (new participants and threshold). | ||
| /// Each current participant can maintain one vote. | ||
| // TODO(#2825): Replace with Votes<AuthenticatedAccountId, ThresholdParameters> from generic_votes.rs | ||
| // once this type is moved out of RunningContractState (which requires Clone + PartialEq + JSON). | ||
| #[near(serializers=[borsh, json])] | ||
| #[derive(Clone, Debug, Default, PartialEq, Eq)] | ||
| pub struct ThresholdParametersVotes { | ||
| pub(crate) proposal_by_account: BTreeMap<AuthenticatedAccountId, ThresholdParameters>, | ||
| } | ||
|
|
||
| impl ThresholdParametersVotes { | ||
| /// return the number of votes for `proposal` casted by members of `participants` | ||
| pub fn n_votes(&self, proposal: &ThresholdParameters, participants: &Participants) -> u64 { | ||
| self.proposal_by_account | ||
| .iter() | ||
| .filter(|&(acc, prop)| { | ||
| participants | ||
| .participants() | ||
| .iter() | ||
| .any(|(acc_id, _, _)| acc.get() == acc_id) | ||
| && prop == proposal | ||
| }) | ||
| .count() as u64 | ||
| } | ||
| /// Registers a vote by `participant` for `proposal`. | ||
| /// Removes any existing votes by `participant`. | ||
| /// Returns the number of participants who have voted for the same proposal (including the new | ||
| /// vote). | ||
| pub fn vote( | ||
| &mut self, | ||
| proposal: &ThresholdParameters, | ||
| participant: AuthenticatedAccountId, | ||
| ) -> u64 { | ||
| if self | ||
| .proposal_by_account | ||
| .insert(participant, proposal.clone()) | ||
| .is_some() | ||
| { | ||
| log!("removed one vote for signer"); | ||
| } | ||
| self.proposal_by_account | ||
| .values() | ||
| .filter(|&prop| prop == proposal) | ||
| .count() as u64 | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::ThresholdParametersVotes; | ||
| use crate::primitives::{ | ||
| key_state::AuthenticatedAccountId, | ||
| participants::Participants, | ||
| test_utils::{gen_participant, gen_threshold_params}, | ||
| }; | ||
| use near_sdk::{test_utils::VMContextBuilder, testing_env}; | ||
|
|
||
| #[test] | ||
| fn test_voting_and_removal() { | ||
| let mut participants = Participants::default(); | ||
| let p0 = gen_participant(0); | ||
| participants.insert(p0.0.clone(), p0.1).expect("error"); | ||
| let mut ctx = VMContextBuilder::new(); | ||
| ctx.signer_account_id(p0.0); | ||
| testing_env!(ctx.build()); | ||
| let participant = | ||
| AuthenticatedAccountId::new(&participants).expect("expected authentication"); | ||
| let params = gen_threshold_params(30); | ||
| let mut votes = ThresholdParametersVotes::default(); | ||
| assert_eq!(votes.vote(¶ms, participant.clone()), 1); | ||
| assert_eq!(votes.n_votes(¶ms, &participants), 1); | ||
| let params2 = gen_threshold_params(30); | ||
| assert_eq!(votes.vote(¶ms2, participant), 1); | ||
| assert_eq!(votes.n_votes(¶ms2, &participants), 1); | ||
| assert_eq!(votes.n_votes(¶ms, &participants), 0); | ||
|
|
||
| // new participant | ||
| let p1 = gen_participant(1); | ||
| participants.insert(p1.0.clone(), p1.1).expect("error"); | ||
| ctx.signer_account_id(p1.0); | ||
| testing_env!(ctx.build()); | ||
| let participant = | ||
| AuthenticatedAccountId::new(&participants).expect("expected authentication"); | ||
| assert_eq!(votes.vote(¶ms, participant.clone()), 1); | ||
| assert_eq!(votes.n_votes(¶ms2, &participants), 1); | ||
| assert_eq!(votes.vote(¶ms2, participant), 2); | ||
| assert_eq!(votes.n_votes(¶ms2, &participants), 2); | ||
| assert_eq!(votes.n_votes(¶ms2, params2.participants()), 0); | ||
| assert_eq!(votes.n_votes(¶ms, &participants), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_non_participant_votes_not_counted() { | ||
| // given: two participants vote for a proposal | ||
| let mut old_participants = Participants::default(); | ||
| let (p0, p1) = (gen_participant(0), gen_participant(1)); | ||
| old_participants.insert(p0.0.clone(), p0.1.clone()).unwrap(); | ||
| old_participants.insert(p1.0.clone(), p1.1.clone()).unwrap(); | ||
|
|
||
| let mut ctx = VMContextBuilder::new(); | ||
| let auth_p0 = { | ||
| ctx.signer_account_id(p0.0); | ||
| testing_env!(ctx.build()); | ||
| AuthenticatedAccountId::new(&old_participants).unwrap() | ||
| }; | ||
| let auth_p1 = { | ||
| ctx.signer_account_id(p1.0); | ||
| testing_env!(ctx.build()); | ||
| AuthenticatedAccountId::new(&old_participants).unwrap() | ||
| }; | ||
|
|
||
| let params = gen_threshold_params(30); | ||
| let mut votes = ThresholdParametersVotes::default(); | ||
| votes.vote(¶ms, auth_p0); | ||
| votes.vote(¶ms, auth_p1); | ||
| assert_eq!(votes.n_votes(¶ms, &old_participants), 2); | ||
|
|
||
| // when: checking votes against a different participant set (simulating post-resharing) | ||
| let mut new_participants = Participants::default(); | ||
| let p2 = gen_participant(2); | ||
| new_participants.insert(p2.0.clone(), p2.1).unwrap(); | ||
|
|
||
| // then: votes from non-participants are not counted | ||
| assert_eq!(votes.n_votes(¶ms, &new_participants), 0); | ||
|
|
||
| ctx.signer_account_id(p2.0); | ||
| testing_env!(ctx.build()); | ||
| let auth_p2 = AuthenticatedAccountId::new(&new_participants).unwrap(); | ||
| votes.vote(¶ms, auth_p2); | ||
| assert_eq!(votes.n_votes(¶ms, &new_participants), 1); | ||
| } | ||
| } |
Oops, something went wrong.
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.
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.
note: this file was
votes.rsbefore and was just renamed. This is the diff to the file currently on main: