This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
make MAX_VOTERS and MAX_CANDIDATES in elections-phragmen configurable. Fix: #11092 #11908
Merged
paritytech-processbot
merged 17 commits into
paritytech:master
from
sudipghimire533:master
Aug 14, 2022
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7daf33d
make MAX_VOTERS and MAX_CANDIDATES in elections-phragmen configurable
sudipghimire533 80fe2e0
Configure election-phragmen in node bin configuring max candidates & …
sudipghimire533 84f3ed3
Add document comment for added Config parameter
sudipghimire533 77a690f
Incorporate suggestion
sudipghimire533 4bdd466
Merge branch 'paritytech:master' into master
sudipghimire533 a80efdf
Merge branch 'paritytech:master' into master
sudipghimire533 4d31857
fix benchmarks
Szegoo 1c4a7ef
Update frame/elections-phragmen/src/lib.rs
Szegoo 1f32fc0
Update frame/elections-phragmen/src/lib.rs
Szegoo b74ac41
fix wrong values
Szegoo 0a81699
fix typo
Szegoo e67573b
docs
Szegoo e403cfb
more detailed docs
Szegoo 9f930a6
fmt
Szegoo 7103cfb
Merge branch 'paritytech:master' into master
sudipghimire533 7faf313
".git/.scripts/bench-bot.sh" pallet dev pallet_elections_phragmen
e1326ad
Merge remote-tracking branch 'origin/master'
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,17 +125,6 @@ pub mod migrations; | |
| /// The maximum votes allowed per voter. | ||
| pub const MAXIMUM_VOTE: usize = 16; | ||
|
|
||
| // Some safe temp values to make the wasm execution sane while we still use this pallet. | ||
| #[cfg(test)] | ||
| pub(crate) const MAX_CANDIDATES: u32 = 100; | ||
| #[cfg(not(test))] | ||
| pub(crate) const MAX_CANDIDATES: u32 = 1000; | ||
|
|
||
| #[cfg(test)] | ||
| pub(crate) const MAX_VOTERS: u32 = 1000; | ||
| #[cfg(not(test))] | ||
| pub(crate) const MAX_VOTERS: u32 = 10 * 1000; | ||
|
|
||
| type BalanceOf<T> = | ||
| <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance; | ||
| type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency< | ||
|
|
@@ -259,6 +248,21 @@ pub mod pallet { | |
| #[pallet::constant] | ||
| type TermDuration: Get<Self::BlockNumber>; | ||
|
|
||
| /// The maximum number of candidates in a phragmen election. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kianenigma Should the docs for
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a section warning that the election happens onchain, and these numbers determine how big the election will be. Also note that if the maximum candidates are reached, we don't accept anymore. If max voters are reached, we just ignore voters. |
||
| /// | ||
| /// Warning: The election happens onchain, and this value will determine | ||
| /// the size of the election. When this limit is reached no more | ||
| /// candidates are accepted in the election. | ||
| #[pallet::constant] | ||
| type MaxCandidates: Get<u32>; | ||
|
|
||
| /// The maximum number of voters to allow in a phragmen election. | ||
| /// | ||
| /// Warning: This impacts the size of the election which is run onchain. | ||
| /// When the limit is reached the new voters are ignored. | ||
| #[pallet::constant] | ||
| type MaxVoters: Get<u32>; | ||
|
|
||
| /// Weight information for extrinsics in this pallet. | ||
| type WeightInfo: WeightInfo; | ||
| } | ||
|
|
@@ -397,7 +401,10 @@ pub mod pallet { | |
|
|
||
| let actual_count = <Candidates<T>>::decode_len().unwrap_or(0) as u32; | ||
| ensure!(actual_count <= candidate_count, Error::<T>::InvalidWitnessData); | ||
| ensure!(actual_count <= MAX_CANDIDATES, Error::<T>::TooManyCandidates); | ||
| ensure!( | ||
| actual_count <= <T as Config>::MaxCandidates::get(), | ||
| Error::<T>::TooManyCandidates | ||
| ); | ||
|
|
||
| let index = Self::is_candidate(&who).err().ok_or(Error::<T>::DuplicatedCandidate)?; | ||
|
|
||
|
|
@@ -913,10 +920,11 @@ impl<T: Config> Pallet<T> { | |
|
|
||
| let mut num_edges: u32 = 0; | ||
|
|
||
| let max_voters = <T as Config>::MaxVoters::get() as usize; | ||
| // used for prime election. | ||
| let mut voters_and_stakes = Vec::new(); | ||
| match Voting::<T>::iter().try_for_each(|(voter, Voter { stake, votes, .. })| { | ||
| if voters_and_stakes.len() < MAX_VOTERS as usize { | ||
| if voters_and_stakes.len() < max_voters { | ||
| voters_and_stakes.push((voter, stake, votes)); | ||
| Ok(()) | ||
| } else { | ||
|
|
@@ -930,7 +938,7 @@ impl<T: Config> Pallet<T> { | |
| "Failed to run election. Number of voters exceeded", | ||
| ); | ||
| Self::deposit_event(Event::ElectionError); | ||
| return T::DbWeight::get().reads(3 + MAX_VOTERS as u64) | ||
| return T::DbWeight::get().reads(3 + max_voters as u64) | ||
| }, | ||
| } | ||
|
|
||
|
|
@@ -1266,6 +1274,8 @@ mod tests { | |
|
|
||
| parameter_types! { | ||
| pub const ElectionsPhragmenPalletId: LockIdentifier = *b"phrelect"; | ||
| pub const PhragmenMaxVoters: u32 = 1000; | ||
| pub const PhragmenMaxCandidates: u32 = 100; | ||
| } | ||
|
|
||
| impl Config for Test { | ||
|
|
@@ -1284,6 +1294,8 @@ mod tests { | |
| type LoserCandidate = (); | ||
| type KickedMember = (); | ||
| type WeightInfo = (); | ||
| type MaxVoters = PhragmenMaxVoters; | ||
| type MaxCandidates = PhragmenMaxCandidates; | ||
| } | ||
|
|
||
| pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>; | ||
|
|
||
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.