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
Removing without_storage_info from scored-pool pallet. #11996
Merged
paritytech-processbot
merged 12 commits into
master
from
hb-scored-pool-remove-without-storage
Sep 8, 2022
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2ed807c
Removing without_storage_info from scored-pool pallet.
hbulgarini 03faee4
Merge remote-tracking branch 'origin/master' into hb-scored-pool-remo…
8f69158
Merge branch 'master' into hb-scored-pool-remove-without-storage
hbulgarini 857bbbe
Addressing PR feedback
hbulgarini 80e221b
typo
hbulgarini b4848dd
typo
hbulgarini 0f1c6e0
Addressing PR comments and formatting code
hbulgarini 52b506a
Removing unwanted import
hbulgarini f1a6355
Adding a map_err
hbulgarini e1e7b86
Addressing PR feedback
hbulgarini 94286dd
cargo fmt
hbulgarini 62c21e8
fixing some tests
hbulgarini 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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -98,18 +98,24 @@ mod mock; | |||||||
| #[cfg(test)] | ||||||||
| mod tests; | ||||||||
|
|
||||||||
| use codec::FullCodec; | ||||||||
| use codec::{FullCodec, MaxEncodedLen}; | ||||||||
| use frame_support::{ | ||||||||
| ensure, | ||||||||
| traits::{ChangeMembers, Currency, Get, InitializeMembers, ReservableCurrency}, | ||||||||
| BoundedVec, | ||||||||
| }; | ||||||||
| pub use pallet::*; | ||||||||
| use sp_runtime::traits::{AtLeast32Bit, StaticLookup, Zero}; | ||||||||
| use sp_std::{fmt::Debug, prelude::*}; | ||||||||
|
|
||||||||
| type BalanceOf<T, I> = | ||||||||
| <<T as Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance; | ||||||||
| type PoolT<T, I> = Vec<(<T as frame_system::Config>::AccountId, Option<<T as Config<I>>::Score>)>; | ||||||||
| type PoolT<T, I> = BoundedVec< | ||||||||
| (<T as frame_system::Config>::AccountId, Option<<T as Config<I>>::Score>), | ||||||||
| <T as Config<I>>::MaximumMembers, | ||||||||
| >; | ||||||||
| type MembersT<T, I> = | ||||||||
| BoundedVec<<T as frame_system::Config>::AccountId, <T as Config<I>>::MaximumMembers>; | ||||||||
|
|
||||||||
| /// The enum is supplied when refreshing the members set. | ||||||||
| /// Depending on the enum variant the corresponding associated | ||||||||
|
|
@@ -129,14 +135,17 @@ pub mod pallet { | |||||||
|
|
||||||||
| #[pallet::pallet] | ||||||||
| #[pallet::generate_store(pub(super) trait Store)] | ||||||||
| #[pallet::without_storage_info] | ||||||||
| pub struct Pallet<T, I = ()>(_); | ||||||||
|
|
||||||||
| #[pallet::config] | ||||||||
| pub trait Config<I: 'static = ()>: frame_system::Config { | ||||||||
| /// The currency used for deposits. | ||||||||
| type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>; | ||||||||
|
|
||||||||
| /// Maximum members length allowed. | ||||||||
| #[pallet::constant] | ||||||||
| type MaximumMembers: Get<u32>; | ||||||||
|
|
||||||||
| /// The score attributed to a member or candidate. | ||||||||
| type Score: AtLeast32Bit | ||||||||
| + Clone | ||||||||
|
|
@@ -145,7 +154,8 @@ pub mod pallet { | |||||||
| + FullCodec | ||||||||
| + MaybeSerializeDeserialize | ||||||||
| + Debug | ||||||||
| + scale_info::TypeInfo; | ||||||||
| + scale_info::TypeInfo | ||||||||
| + MaxEncodedLen; | ||||||||
|
|
||||||||
| /// The overarching event type. | ||||||||
| type Event: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::Event>; | ||||||||
|
|
@@ -206,9 +216,11 @@ pub mod pallet { | |||||||
| InvalidIndex, | ||||||||
| /// Index does not match requested account. | ||||||||
| WrongAccountIndex, | ||||||||
| /// Number of members exceeds `MaximumMembers`. | ||||||||
| TooManyMembers, | ||||||||
| } | ||||||||
|
|
||||||||
| /// The current pool of candidates, stored as an ordered Vec | ||||||||
| /// The current pool of candidates, stored as an ordered Bounded Vec | ||||||||
| /// (ordered descending by score, `None` last, highest first). | ||||||||
| #[pallet::storage] | ||||||||
| #[pallet::getter(fn pool)] | ||||||||
|
|
@@ -228,7 +240,7 @@ pub mod pallet { | |||||||
| #[pallet::storage] | ||||||||
| #[pallet::getter(fn members)] | ||||||||
| pub(crate) type Members<T: Config<I>, I: 'static = ()> = | ||||||||
| StorageValue<_, Vec<T::AccountId>, ValueQuery>; | ||||||||
| StorageValue<_, MembersT<T, I>, ValueQuery>; | ||||||||
|
|
||||||||
| /// Size of the `Members` set. | ||||||||
| #[pallet::storage] | ||||||||
|
|
@@ -262,10 +274,10 @@ pub mod pallet { | |||||||
| }); | ||||||||
|
|
||||||||
| // Sorts the `Pool` by score in a descending order. Entities which | ||||||||
| // have a score of `None` are sorted to the beginning of the vec. | ||||||||
| // have a score of `None` are sorted to the end of the bounded vec. | ||||||||
| pool.sort_by_key(|(_, maybe_score)| Reverse(maybe_score.unwrap_or_default())); | ||||||||
|
|
||||||||
| <MemberCount<T, I>>::put(self.member_count); | ||||||||
| <Pallet<T, I>>::update_member_count(self.member_count) | ||||||||
| .expect("Number of allowed members exceeded"); | ||||||||
| <Pool<T, I>>::put(&pool); | ||||||||
| <Pallet<T, I>>::refresh_members(pool, ChangeReceiver::MembershipInitialized); | ||||||||
| } | ||||||||
|
|
@@ -307,7 +319,8 @@ pub mod pallet { | |||||||
|
|
||||||||
| // can be inserted as last element in pool, since entities with | ||||||||
| // `None` are always sorted to the end. | ||||||||
|
ggwpez marked this conversation as resolved.
|
||||||||
| <Pool<T, I>>::append((who.clone(), Option::<<T as Config<I>>::Score>::None)); | ||||||||
| <Pool<T, I>>::try_append((who.clone(), Option::<<T as Config<I>>::Score>::None)) | ||||||||
| .map_err(|_| Error::<T, I>::TooManyMembers)?; | ||||||||
|
|
||||||||
| <CandidateExists<T, I>>::insert(&who, true); | ||||||||
|
|
||||||||
|
|
@@ -393,7 +406,7 @@ pub mod pallet { | |||||||
| Reverse(maybe_score.unwrap_or_default()) | ||||||||
| }) | ||||||||
| .unwrap_or_else(|l| l); | ||||||||
| pool.insert(location, item); | ||||||||
| pool.try_insert(location, item).map_err(|_| Error::<T, I>::TooManyMembers)?; | ||||||||
|
|
||||||||
| <Pool<T, I>>::put(&pool); | ||||||||
| Self::deposit_event(Event::<T, I>::CandidateScored); | ||||||||
|
|
@@ -409,7 +422,7 @@ pub mod pallet { | |||||||
| #[pallet::weight(0)] | ||||||||
| pub fn change_member_count(origin: OriginFor<T>, count: u32) -> DispatchResult { | ||||||||
| ensure_root(origin)?; | ||||||||
| MemberCount::<T, I>::put(&count); | ||||||||
| Self::update_member_count(count)?; | ||||||||
| Ok(()) | ||||||||
|
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.
Suggested change
There is one unused |
||||||||
| } | ||||||||
| } | ||||||||
|
|
@@ -423,23 +436,28 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> { | |||||||
| /// type function to invoke at the end of the method. | ||||||||
| fn refresh_members(pool: PoolT<T, I>, notify: ChangeReceiver) { | ||||||||
| let count = MemberCount::<T, I>::get(); | ||||||||
| let old_members = <Members<T, I>>::get(); | ||||||||
|
|
||||||||
| let mut new_members: Vec<T::AccountId> = pool | ||||||||
| let new_members: Vec<T::AccountId> = pool | ||||||||
| .into_iter() | ||||||||
| .filter(|(_, score)| score.is_some()) | ||||||||
| .take(count as usize) | ||||||||
| .map(|(account_id, _)| account_id) | ||||||||
| .collect(); | ||||||||
| new_members.sort(); | ||||||||
|
|
||||||||
| let old_members = <Members<T, I>>::get(); | ||||||||
| <Members<T, I>>::put(&new_members); | ||||||||
| // It's safe to truncate_from at this point since MemberCount | ||||||||
| // is verified that it does not exceed the MaximumMembers value | ||||||||
| let mut new_members_bounded: MembersT<T, I> = BoundedVec::truncate_from(new_members); | ||||||||
|
ggwpez marked this conversation as resolved.
|
||||||||
|
|
||||||||
| new_members_bounded.sort(); | ||||||||
|
|
||||||||
| <Members<T, I>>::put(&new_members_bounded); | ||||||||
|
|
||||||||
| match notify { | ||||||||
| ChangeReceiver::MembershipInitialized => | ||||||||
| T::MembershipInitialized::initialize_members(&new_members), | ||||||||
| T::MembershipInitialized::initialize_members(&new_members_bounded), | ||||||||
| ChangeReceiver::MembershipChanged => | ||||||||
| T::MembershipChanged::set_members_sorted(&new_members[..], &old_members[..]), | ||||||||
| T::MembershipChanged::set_members_sorted(&new_members_bounded[..], &old_members[..]), | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -485,4 +503,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> { | |||||||
|
|
||||||||
| Ok(()) | ||||||||
| } | ||||||||
|
|
||||||||
| /// Make sure the new member count value does not exceed the MaximumMembers | ||||||||
| fn update_member_count(new_member_count: u32) -> Result<(), Error<T, I>> { | ||||||||
| ensure!(new_member_count <= T::MaximumMembers::get(), Error::<T, I>::TooManyMembers); | ||||||||
| <MemberCount<T, I>>::put(new_member_count); | ||||||||
| Ok(()) | ||||||||
| } | ||||||||
| } | ||||||||
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
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.