diff --git a/frame/bags-list/remote-tests/src/snapshot.rs b/frame/bags-list/remote-tests/src/snapshot.rs index 6e186a65cb2b9..bf6cae7750d17 100644 --- a/frame/bags-list/remote-tests/src/snapshot.rs +++ b/frame/bags-list/remote-tests/src/snapshot.rs @@ -40,8 +40,12 @@ pub async fn execute( .inject_hashed_prefix(&>::prefix_hash()) .inject_hashed_prefix(&>::prefix_hash()) .inject_hashed_prefix(&>::prefix_hash()) - .inject_hashed_key(&>::hashed_key()) - .inject_hashed_key(&>::hashed_key()) + .inject_hashed_key(&>::hashed_key_for( + &>::count(), + )) + .inject_hashed_key(&>::hashed_key_for( + &>::count(), + )) .build() .await .unwrap(); diff --git a/frame/election-provider-multi-phase/src/lib.rs b/frame/election-provider-multi-phase/src/lib.rs index 80a13aa99fb70..4c4de82af592f 100644 --- a/frame/election-provider-multi-phase/src/lib.rs +++ b/frame/election-provider-multi-phase/src/lib.rs @@ -772,7 +772,7 @@ pub mod pallet { Self::on_initialize_open_unsigned(enabled, now); T::WeightInfo::on_initialize_open_unsigned() } - } + }, _ => T::WeightInfo::on_initialize_nothing(), } } diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index 80630818de7e6..0ef205944caa9 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -112,8 +112,8 @@ pub fn create_validator_with_nominators( assert_eq!(new_validators.len(), 1); assert_eq!(new_validators[0], v_stash, "Our validator was not selected!"); - assert_ne!(CounterForValidators::::get(), 0); - assert_ne!(CounterForNominators::::get(), 0); + assert_ne!(CounterForValidators::::count(), 0); + assert_ne!(CounterForNominators::::count(), 0); // Give Era Points let reward = EraRewardPoints:: { diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 7064f06dd12c7..d7916059f1752 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -72,8 +72,8 @@ pub mod v7 { use super::*; pub fn pre_migrate() -> Result<(), &'static str> { - assert!(CounterForValidators::::get().is_zero(), "CounterForValidators already set."); - assert!(CounterForNominators::::get().is_zero(), "CounterForNominators already set."); + assert!(CounterForValidators::::count().is_zero(), "CounterForValidators already set."); + assert!(CounterForNominators::::count().is_zero(), "CounterForNominators already set."); assert!(StorageVersion::::get() == Releases::V6_0_0); Ok(()) } @@ -83,8 +83,10 @@ pub mod v7 { let validator_count = Validators::::iter().count() as u32; let nominator_count = Nominators::::iter().count() as u32; - CounterForValidators::::put(validator_count); - CounterForNominators::::put(nominator_count); + let counter_for_validators = CounterForValidators::::count(); + CounterForValidators::::insert(counter_for_validators, validator_count); + let counter_for_nominators = CounterForNominators::::count(); + CounterForNominators::::insert(counter_for_nominators, nominator_count); StorageVersion::::put(Releases::V7_0_0); log!(info, "Completed staking migration to Releases::V7_0_0"); diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 7ca1cb1a4a61b..d501a08622349 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -665,8 +665,8 @@ impl Pallet { maybe_max_len: Option, ) -> Vec<(T::AccountId, VoteWeight, Vec)> { let max_allowed_len = { - let nominator_count = CounterForNominators::::get() as usize; - let validator_count = CounterForValidators::::get() as usize; + let nominator_count = CounterForNominators::::count() as usize; + let validator_count = CounterForValidators::::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) }; @@ -774,8 +774,11 @@ impl Pallet { /// wrong. pub fn do_add_nominator(who: &T::AccountId, nominations: Nominations) { if !Nominators::::contains_key(who) { + let mut count = CounterForNominators::::count(); + let counter_key = count; + count.saturating_inc(); // maybe update the counter. - CounterForNominators::::mutate(|x| x.saturating_inc()); + CounterForNominators::::insert(counter_key, count); // 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() { @@ -800,10 +803,13 @@ impl Pallet { pub fn do_remove_nominator(who: &T::AccountId) -> bool { if Nominators::::contains_key(who) { Nominators::::remove(who); - CounterForNominators::::mutate(|x| x.saturating_dec()); + let mut count = CounterForNominators::::count(); + let counter_key = count; + count.saturating_dec(); + CounterForNominators::::insert(counter_key, count); T::SortedListProvider::on_remove(who); debug_assert_eq!(T::SortedListProvider::sanity_check(), Ok(())); - debug_assert_eq!(CounterForNominators::::get(), T::SortedListProvider::count()); + debug_assert_eq!(CounterForNominators::::count(), T::SortedListProvider::count()); true } else { false @@ -820,7 +826,10 @@ impl Pallet { /// wrong. pub fn do_add_validator(who: &T::AccountId, prefs: ValidatorPrefs) { if !Validators::::contains_key(who) { - CounterForValidators::::mutate(|x| x.saturating_inc()) + let mut count = CounterForValidators::::count(); + let counter_key = count; + count.saturating_inc(); + CounterForValidators::::insert(counter_key, count) } Validators::::insert(who, prefs); } @@ -836,7 +845,10 @@ impl Pallet { pub fn do_remove_validator(who: &T::AccountId) -> bool { if Validators::::contains_key(who) { Validators::::remove(who); - CounterForValidators::::mutate(|x| x.saturating_dec()); + let mut count = CounterForValidators::::count(); + let counter_key = count; + count.saturating_dec(); + CounterForValidators::::insert(counter_key, count); true } else { false @@ -865,10 +877,10 @@ impl ElectionDataProvider> for Pallet fn voters( maybe_max_len: Option, ) -> data_provider::Result)>> { - debug_assert!(>::iter().count() as u32 == CounterForNominators::::get()); - debug_assert!(>::iter().count() as u32 == CounterForValidators::::get()); + debug_assert!(>::iter().count() as u32 == CounterForNominators::::count()); + debug_assert!(>::iter().count() as u32 == CounterForValidators::::count()); debug_assert_eq!( - CounterForNominators::::get(), + CounterForNominators::::count(), T::SortedListProvider::count(), "voter_count must be accurate", ); @@ -881,7 +893,7 @@ impl ElectionDataProvider> for Pallet } fn targets(maybe_max_len: Option) -> data_provider::Result> { - let target_count = CounterForValidators::::get(); + let target_count = CounterForValidators::::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 +981,8 @@ impl ElectionDataProvider> for Pallet >::remove_all(None); >::remove_all(None); >::remove_all(None); - >::kill(); - >::kill(); + >::remove_all(); + >::remove_all(); let _ = T::SortedListProvider::clear(None); } @@ -1282,7 +1294,7 @@ impl SortedListProvider for UseNominatorsMap { Box::new(Nominators::::iter().map(|(n, _)| n)) } fn count() -> u32 { - CounterForNominators::::get() + CounterForNominators::::count() } fn contains(id: &T::AccountId) -> bool { Nominators::::contains_key(id) @@ -1310,10 +1322,11 @@ impl SortedListProvider for UseNominatorsMap { fn clear(maybe_count: Option) -> u32 { Nominators::::remove_all(maybe_count); if let Some(count) = maybe_count { - CounterForNominators::::mutate(|noms| *noms - count); + let nominator_count = CounterForNominators::::count(); + CounterForNominators::::insert(nominator_count, nominator_count - count); count } else { - CounterForNominators::::take() + CounterForNominators::::take(CounterForNominators::::count()).unwrap() } } } diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 8e97a90e07544..b204209d2cd0d 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -229,9 +229,9 @@ pub mod pallet { pub type Validators = StorageMap<_, Twox64Concat, T::AccountId, ValidatorPrefs, ValueQuery>; - /// A tracker to keep count of the number of items in the `Validators` map. #[pallet::storage] - pub type CounterForValidators = StorageValue<_, u32, ValueQuery>; + pub type CounterForValidators = + CountedStorageMap; /// The maximum validator count before we stop allowing new validators to join. /// @@ -246,10 +246,9 @@ pub mod pallet { #[pallet::getter(fn nominators)] pub type Nominators = StorageMap<_, Twox64Concat, T::AccountId, Nominations>; - - /// A tracker to keep count of the number of items in the `Nominators` map. #[pallet::storage] - pub type CounterForNominators = StorageValue<_, u32, ValueQuery>; + pub type CounterForNominators = + CountedStorageMap; /// The maximum nominator count before we stop allowing new validators to join. /// @@ -548,7 +547,7 @@ pub mod pallet { // all voters are reported to the `SortedListProvider`. assert_eq!( T::SortedListProvider::count(), - CounterForNominators::::get(), + CounterForNominators::::count(), "not all genesis stakers were inserted into sorted list provider, something is wrong." ); } @@ -957,7 +956,7 @@ pub mod pallet { // the runtime. if let Some(max_validators) = MaxValidatorsCount::::get() { ensure!( - CounterForValidators::::get() < max_validators, + CounterForValidators::::count() < max_validators, Error::::TooManyValidators ); } @@ -997,7 +996,7 @@ pub mod pallet { // the runtime. if let Some(max_nominators) = MaxNominatorsCount::::get() { ensure!( - CounterForNominators::::get() < max_nominators, + CounterForNominators::::count() < max_nominators, Error::::TooManyNominators ); } @@ -1570,7 +1569,7 @@ pub mod pallet { let min_active_bond = if Nominators::::contains_key(&stash) { let max_nominator_count = MaxNominatorsCount::::get().ok_or(Error::::CannotChillOther)?; - let current_nominator_count = CounterForNominators::::get(); + let current_nominator_count = CounterForNominators::::count(); ensure!( threshold * max_nominator_count < current_nominator_count, Error::::CannotChillOther @@ -1579,7 +1578,7 @@ pub mod pallet { } else if Validators::::contains_key(&stash) { let max_validator_count = MaxValidatorsCount::::get().ok_or(Error::::CannotChillOther)?; - let current_validator_count = CounterForValidators::::get(); + let current_validator_count = CounterForValidators::::count(); ensure!( threshold * max_validator_count < current_validator_count, Error::::CannotChillOther diff --git a/frame/staking/src/testing_utils.rs b/frame/staking/src/testing_utils.rs index 13762cf5886db..37a3eaff00efb 100644 --- a/frame/staking/src/testing_utils.rs +++ b/frame/staking/src/testing_utils.rs @@ -37,11 +37,11 @@ const SEED: u32 = 0; /// This function removes all validators and nominators from storage. pub fn clear_validators_and_nominators() { Validators::::remove_all(None); - CounterForValidators::::kill(); + CounterForValidators::::remove_all(); // whenever we touch nominators counter we should update `T::SortedListProvider` as well. Nominators::::remove_all(None); - CounterForNominators::::kill(); + CounterForNominators::::remove_all(); let _ = T::SortedListProvider::clear(None); } diff --git a/frame/support/procedural/src/pallet/parse/pallet_struct.rs b/frame/support/procedural/src/pallet/parse/pallet_struct.rs index 278f46e13818e..c528faf669ee3 100644 --- a/frame/support/procedural/src/pallet/parse/pallet_struct.rs +++ b/frame/support/procedural/src/pallet/parse/pallet_struct.rs @@ -130,12 +130,12 @@ impl PalletStructDef { if generate_storage_info.is_none() => { generate_storage_info = Some(span); - } + }, PalletStructAttr::StorageVersion { storage_version, .. } if storage_version_found.is_none() => { storage_version_found = Some(storage_version); - } + }, attr => { let msg = "Unexpected duplicated attribute"; return Err(syn::Error::new(attr.span(), msg))