-
Notifications
You must be signed in to change notification settings - Fork 1.2k
pallet ranked collective: max member count per rank #4807
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| title: "pallet ranked collective: max member count per rank" | ||
|
|
||
| doc: | ||
| - audience: Runtime Dev | ||
| description: | | ||
| Configuration for the maximum member count per rank, with the option for no limit. | ||
|
|
||
| crates: | ||
| - name: pallet-ranked-collective | ||
| bump: major | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -379,6 +379,7 @@ pub mod pallet { | |
| use super::*; | ||
| use frame_support::{pallet_prelude::*, storage::KeyLenOf}; | ||
| use frame_system::pallet_prelude::*; | ||
| use sp_runtime::traits::MaybeConvert; | ||
|
|
||
| #[pallet::pallet] | ||
| pub struct Pallet<T, I = ()>(PhantomData<(T, I)>); | ||
|
|
@@ -431,6 +432,14 @@ pub mod pallet { | |
| /// in the poll. | ||
| type VoteWeight: Convert<Rank, Votes>; | ||
|
|
||
| /// The maximum number of members for a given rank in the collective. | ||
| /// | ||
| /// The member at rank `x` contributes to the count at rank `x` and all ranks below it. | ||
| /// Therefore, the limit `m` at rank `x` sets the maximum total member count for rank `x` | ||
| /// and all ranks above. | ||
| /// The `None` indicates no member count limit for the given rank. | ||
| type MaxMemberCount: MaybeConvert<Rank, MemberIndex>; | ||
|
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. Do we have a default config for this pallet? Would be nice to set this to
Contributor
Author
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. No, it does not have. There is also a discussion to use default configs only for tests and maybe common configs for system parachains https://forum.polkadot.network/t/seeking-feedback-on-derive-impl-for-default-configurations-in-pallet-templates/7584
Contributor
Author
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. also here #4689
Member
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. I think there should still be a trivial implementation of this to reduce copy&paste.
Contributor
Author
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.
Member
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. Oh, so it is possible to just pass
Contributor
Author
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. Yes |
||
|
|
||
| /// Setup a member for benchmarking. | ||
| #[cfg(feature = "runtime-benchmarks")] | ||
| type BenchmarkSetup: BenchmarkSetup<Self::AccountId>; | ||
|
|
@@ -511,6 +520,8 @@ pub mod pallet { | |
| NoPermission, | ||
| /// The new member to exchange is the same as the old member | ||
| SameMember, | ||
| /// The max member count for the rank has been reached. | ||
| TooManyMembers, | ||
| } | ||
|
|
||
| #[pallet::call] | ||
|
|
@@ -758,6 +769,9 @@ pub mod pallet { | |
| ensure!(!Members::<T, I>::contains_key(&who), Error::<T, I>::AlreadyMember); | ||
| let index = MemberCount::<T, I>::get(0); | ||
| let count = index.checked_add(1).ok_or(Overflow)?; | ||
| if let Some(max) = T::MaxMemberCount::maybe_convert(0) { | ||
| ensure!(count <= max, Error::<T, I>::TooManyMembers); | ||
| } | ||
|
|
||
| Members::<T, I>::insert(&who, MemberRecord { rank: 0 }); | ||
| IdToIndex::<T, I>::insert(0, &who, index); | ||
|
|
@@ -784,6 +798,11 @@ pub mod pallet { | |
| ensure!(max_rank >= rank, Error::<T, I>::NoPermission); | ||
| } | ||
| let index = MemberCount::<T, I>::get(rank); | ||
| let count = index.checked_add(1).ok_or(Overflow)?; | ||
| if let Some(max) = T::MaxMemberCount::maybe_convert(rank) { | ||
| ensure!(count <= max, Error::<T, I>::TooManyMembers); | ||
| } | ||
|
|
||
| MemberCount::<T, I>::insert(rank, index.checked_add(1).ok_or(Overflow)?); | ||
| IdToIndex::<T, I>::insert(rank, &who, index); | ||
| IndexToId::<T, I>::insert(rank, index, &who); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.