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.7k
Used CountedStorageMap in pallet-staking #10233
Merged
paritytech-processbot
merged 29 commits into
paritytech:master
from
ayevbeosa:ayevbeosa-employ-counted-map
Dec 7, 2021
Merged
Changes from 3 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
662fffe
Removed counters and used CountedStorageMap instead.
ayevbeosa 48a4825
Merge branch 'master' of https://github.com/paritytech/substrate into…
ayevbeosa da2d7ec
Little refactoring
ayevbeosa 31f7bd7
Update frame/staking/src/migrations.rs
ayevbeosa a8c2f16
Removed redundant code to update counter for validator & nominator.
ayevbeosa 01a605d
Removed redundant code to update counter for validator & nominator.
ayevbeosa 2c192d8
Removed unreachable code to inject the hashed prefix for nominator & …
ayevbeosa c2f7577
Removed redundant check for nominator & validator count.
ayevbeosa b332858
Merge branch 'master' of https://github.com/paritytech/substrate into…
ayevbeosa d771e4a
Merge branch 'master' of https://github.com/paritytech/substrate into…
ayevbeosa e449fe5
Generated `fn prefix_hash` for `CountedStorageMap`.
ayevbeosa 65d7d02
Merge branch 'master' of https://github.com/paritytech/substrate into…
ayevbeosa b9d0a88
Applied changes from `cargo fmt`
ayevbeosa 9b21bef
Merge branch 'master' of https://github.com/paritytech/substrate into…
ayevbeosa d79d380
Possible correct implementation of migration code
ayevbeosa 16d6a44
Merge branch 'master' of https://github.com/paritytech/substrate into…
ayevbeosa 190ec8e
Implemented fn module_prefix, storage_prefix and prefix_hash.
ayevbeosa 41f8dae
Merge branch 'master' of https://github.com/paritytech/substrate into…
ayevbeosa 3a0500f
Removed counted_map.rs
ayevbeosa fac8882
Merge branch 'master' of https://github.com/paritytech/substrate into…
ayevbeosa bf1bc0a
Renamed `fn storage_prefix` to `storage_counter_prefix`.
ayevbeosa 36115cb
Merge branch 'master' of https://github.com/paritytech/substrate into…
ayevbeosa de685ff
Update frame/support/src/storage/types/counted_map.rs
gui1117 d07414f
Update frame/bags-list/remote-tests/src/snapshot.rs
gui1117 256ac95
Update frame/support/src/storage/types/counted_map.rs
gui1117 fa56998
Merge branch 'master' into ayevbeosa-employ-counted-map
ayevbeosa a437c17
Fixed errors.
ayevbeosa b7e8047
Merge branch 'master' of https://github.com/paritytech/substrate into…
ayevbeosa da0f59f
Merge remote-tracking branch 'origin/master' into 10233
gui1117 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 |
|---|---|---|
|
|
@@ -665,8 +665,8 @@ impl<T: Config> Pallet<T> { | |
| maybe_max_len: Option<usize>, | ||
| ) -> Vec<(T::AccountId, VoteWeight, Vec<T::AccountId>)> { | ||
| let max_allowed_len = { | ||
| let nominator_count = CounterForNominators::<T>::get() as usize; | ||
| let validator_count = CounterForValidators::<T>::get() as usize; | ||
| let nominator_count = Nominators::<T>::count() as usize; | ||
| let validator_count = Validators::<T>::count() as usize; | ||
| let all_voter_count = validator_count.saturating_add(nominator_count); | ||
| maybe_max_len.unwrap_or(all_voter_count).min(all_voter_count) | ||
| }; | ||
|
|
@@ -765,18 +765,15 @@ impl<T: Config> Pallet<T> { | |
| } | ||
|
|
||
| /// This function will add a nominator to the `Nominators` storage map, | ||
| /// [`SortedListProvider`] and keep track of the `CounterForNominators`. | ||
| /// and [`SortedListProvider`]. | ||
| /// | ||
| /// If the nominator already exists, their nominations will be updated. | ||
| /// | ||
| /// NOTE: you must ALWAYS use this function to add nominator or update their targets. Any access | ||
| /// to `Nominators`, its counter, or `VoterList` outside of this function is almost certainly | ||
| /// to `Nominators` or `VoterList` outside of this function is almost certainly | ||
| /// wrong. | ||
| pub fn do_add_nominator(who: &T::AccountId, nominations: Nominations<T::AccountId>) { | ||
| if !Nominators::<T>::contains_key(who) { | ||
| // maybe update the counter. | ||
| CounterForNominators::<T>::mutate(|x| x.saturating_inc()); | ||
|
|
||
| // maybe update sorted list. Error checking is defensive-only - this should never fail. | ||
| if T::SortedListProvider::on_insert(who.clone(), Self::weight_of(who)).is_err() { | ||
| log!(warn, "attempt to insert duplicate nominator ({:#?})", who); | ||
|
|
@@ -790,53 +787,46 @@ impl<T: Config> Pallet<T> { | |
| } | ||
|
|
||
| /// This function will remove a nominator from the `Nominators` storage map, | ||
| /// [`SortedListProvider`] and keep track of the `CounterForNominators`. | ||
| /// and [`SortedListProvider`]. | ||
| /// | ||
| /// Returns true if `who` was removed from `Nominators`, otherwise false. | ||
| /// | ||
| /// NOTE: you must ALWAYS use this function to remove a nominator from the system. Any access to | ||
| /// `Nominators`, its counter, or `VoterList` outside of this function is almost certainly | ||
| /// `Nominators` or `VoterList` outside of this function is almost certainly | ||
| /// wrong. | ||
| pub fn do_remove_nominator(who: &T::AccountId) -> bool { | ||
| if Nominators::<T>::contains_key(who) { | ||
| Nominators::<T>::remove(who); | ||
| CounterForNominators::<T>::mutate(|x| x.saturating_dec()); | ||
| T::SortedListProvider::on_remove(who); | ||
| debug_assert_eq!(T::SortedListProvider::sanity_check(), Ok(())); | ||
| debug_assert_eq!(CounterForNominators::<T>::get(), T::SortedListProvider::count()); | ||
| debug_assert_eq!(Nominators::<T>::count(), T::SortedListProvider::count()); | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| /// This function will add a validator to the `Validators` storage map, and keep track of the | ||
| /// `CounterForValidators`. | ||
| /// This function will add a validator to the `Validators` storage map. | ||
| /// | ||
| /// If the validator already exists, their preferences will be updated. | ||
| /// | ||
| /// NOTE: you must ALWAYS use this function to add a validator to the system. Any access to | ||
| /// `Validators`, its counter, or `VoterList` outside of this function is almost certainly | ||
| /// `Validators` or `VoterList` outside of this function is almost certainly | ||
| /// wrong. | ||
| pub fn do_add_validator(who: &T::AccountId, prefs: ValidatorPrefs) { | ||
| if !Validators::<T>::contains_key(who) { | ||
| CounterForValidators::<T>::mutate(|x| x.saturating_inc()) | ||
| } | ||
| Validators::<T>::insert(who, prefs); | ||
| } | ||
|
|
||
| /// This function will remove a validator from the `Validators` storage map, | ||
| /// and keep track of the `CounterForValidators`. | ||
| /// This function will remove a validator from the `Validators` storage map. | ||
| /// | ||
| /// Returns true if `who` was removed from `Validators`, otherwise false. | ||
| /// | ||
| /// NOTE: you must ALWAYS use this function to remove a validator from the system. Any access to | ||
| /// `Validators`, its counter, or `VoterList` outside of this function is almost certainly | ||
| /// `Validators` or `VoterList` outside of this function is almost certainly | ||
| /// wrong. | ||
| pub fn do_remove_validator(who: &T::AccountId) -> bool { | ||
| if Validators::<T>::contains_key(who) { | ||
| Validators::<T>::remove(who); | ||
| CounterForValidators::<T>::mutate(|x| x.saturating_dec()); | ||
| true | ||
| } else { | ||
| false | ||
|
|
@@ -865,10 +855,10 @@ impl<T: Config> ElectionDataProvider<T::AccountId, BlockNumberFor<T>> for Pallet | |
| fn voters( | ||
| maybe_max_len: Option<usize>, | ||
| ) -> data_provider::Result<Vec<(T::AccountId, VoteWeight, Vec<T::AccountId>)>> { | ||
| debug_assert!(<Nominators<T>>::iter().count() as u32 == CounterForNominators::<T>::get()); | ||
| debug_assert!(<Validators<T>>::iter().count() as u32 == CounterForValidators::<T>::get()); | ||
| debug_assert!(<Nominators<T>>::iter().count() as u32 == Nominators::<T>::count()); | ||
|
||
| debug_assert!(<Validators<T>>::iter().count() as u32 == Validators::<T>::count()); | ||
| debug_assert_eq!( | ||
| CounterForNominators::<T>::get(), | ||
| Nominators::<T>::count(), | ||
| T::SortedListProvider::count(), | ||
| "voter_count must be accurate", | ||
| ); | ||
|
|
@@ -881,7 +871,7 @@ impl<T: Config> ElectionDataProvider<T::AccountId, BlockNumberFor<T>> for Pallet | |
| } | ||
|
|
||
| fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<T::AccountId>> { | ||
| let target_count = CounterForValidators::<T>::get(); | ||
| let target_count = Validators::<T>::count(); | ||
|
|
||
| // We can't handle this case yet -- return an error. | ||
| if maybe_max_len.map_or(false, |max_len| target_count > max_len as u32) { | ||
|
|
@@ -969,8 +959,6 @@ impl<T: Config> ElectionDataProvider<T::AccountId, BlockNumberFor<T>> for Pallet | |
| <Ledger<T>>::remove_all(None); | ||
| <Validators<T>>::remove_all(None); | ||
| <Nominators<T>>::remove_all(None); | ||
| <CounterForNominators<T>>::kill(); | ||
| <CounterForValidators<T>>::kill(); | ||
| let _ = T::SortedListProvider::clear(None); | ||
| } | ||
|
|
||
|
|
@@ -1282,7 +1270,7 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsMap<T> { | |
| Box::new(Nominators::<T>::iter().map(|(n, _)| n)) | ||
| } | ||
| fn count() -> u32 { | ||
| CounterForNominators::<T>::get() | ||
| Nominators::<T>::count() | ||
| } | ||
| fn contains(id: &T::AccountId) -> bool { | ||
| Nominators::<T>::contains_key(id) | ||
|
|
@@ -1308,12 +1296,9 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsMap<T> { | |
| Ok(()) | ||
| } | ||
| fn clear(maybe_count: Option<u32>) -> u32 { | ||
| let count_before = Nominators::<T>::count(); | ||
| Nominators::<T>::remove_all(maybe_count); | ||
| if let Some(count) = maybe_count { | ||
| CounterForNominators::<T>::mutate(|noms| *noms - count); | ||
| count | ||
| } else { | ||
| CounterForNominators::<T>::take() | ||
| } | ||
| let count_after = Nominators::<T>::count(); | ||
| count_before.saturating_sub(count_after) | ||
shawntabrizi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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
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.