Used CountedStorageMap in pallet-staking (#10233)

* Removed counters and used CountedStorageMap instead.

* Little refactoring

* Update frame/staking/src/migrations.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Removed redundant code to update counter for validator & nominator.

* Removed redundant code to update counter for validator & nominator.

* Removed unreachable code to inject the hashed prefix for nominator & validator.

* Removed redundant check for nominator & validator count.

* Generated `fn prefix_hash` for `CountedStorageMap`.

* Applied changes from `cargo fmt`

* Possible correct implementation of migration code

* Implemented fn module_prefix, storage_prefix and prefix_hash.

* Removed counted_map.rs

* Renamed `fn storage_prefix` to `storage_counter_prefix`.

* Update frame/support/src/storage/types/counted_map.rs

* Update frame/bags-list/remote-tests/src/snapshot.rs

* Update frame/support/src/storage/types/counted_map.rs

* Fixed errors.

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Ayevbeosa Iyamu
2021-12-07 10:21:27 +01:00
committed by GitHub
parent 9e9e18b161
commit 56fb1cfbb6
9 changed files with 74 additions and 82 deletions
@@ -27,6 +27,7 @@ pub async fn execute<Runtime: crate::RuntimeT, Block: BlockT + DeserializeOwned>
ws_url: String, ws_url: String,
) { ) {
use frame_support::storage::generator::StorageMap; use frame_support::storage::generator::StorageMap;
let mut ext = Builder::<Block>::new() let mut ext = Builder::<Block>::new()
.mode(Mode::Online(OnlineConfig { .mode(Mode::Online(OnlineConfig {
transport: ws_url.to_string().into(), transport: ws_url.to_string().into(),
@@ -38,10 +39,10 @@ pub async fn execute<Runtime: crate::RuntimeT, Block: BlockT + DeserializeOwned>
})) }))
.inject_hashed_prefix(&<pallet_staking::Bonded<Runtime>>::prefix_hash()) .inject_hashed_prefix(&<pallet_staking::Bonded<Runtime>>::prefix_hash())
.inject_hashed_prefix(&<pallet_staking::Ledger<Runtime>>::prefix_hash()) .inject_hashed_prefix(&<pallet_staking::Ledger<Runtime>>::prefix_hash())
.inject_hashed_prefix(&<pallet_staking::Validators<Runtime>>::prefix_hash()) .inject_hashed_prefix(&<pallet_staking::Validators<Runtime>>::map_storage_final_prefix())
.inject_hashed_prefix(&<pallet_staking::Nominators<Runtime>>::prefix_hash()) .inject_hashed_prefix(&<pallet_staking::Nominators<Runtime>>::map_storage_final_prefix())
.inject_hashed_key(&<pallet_staking::CounterForNominators<Runtime>>::hashed_key()) .inject_hashed_key(&<pallet_staking::Validators<Runtime>>::counter_storage_final_key())
.inject_hashed_key(&<pallet_staking::CounterForValidators<Runtime>>::hashed_key()) .inject_hashed_key(&<pallet_staking::Nominators<Runtime>>::counter_storage_final_key())
.build() .build()
.await .await
.unwrap(); .unwrap();
+4 -4
View File
@@ -112,8 +112,8 @@ pub fn create_validator_with_nominators<T: Config>(
assert_eq!(new_validators.len(), 1); assert_eq!(new_validators.len(), 1);
assert_eq!(new_validators[0], v_stash, "Our validator was not selected!"); assert_eq!(new_validators[0], v_stash, "Our validator was not selected!");
assert_ne!(CounterForValidators::<T>::get(), 0); assert_ne!(Validators::<T>::count(), 0);
assert_ne!(CounterForNominators::<T>::get(), 0); assert_ne!(Nominators::<T>::count(), 0);
// Give Era Points // Give Era Points
let reward = EraRewardPoints::<T::AccountId> { let reward = EraRewardPoints::<T::AccountId> {
@@ -922,8 +922,8 @@ mod tests {
let count_validators = Validators::<Test>::iter().count(); let count_validators = Validators::<Test>::iter().count();
let count_nominators = Nominators::<Test>::iter().count(); let count_nominators = Nominators::<Test>::iter().count();
assert_eq!(count_validators, CounterForValidators::<Test>::get() as usize); assert_eq!(count_validators, Validators::<Test>::count() as usize);
assert_eq!(count_nominators, CounterForNominators::<Test>::get() as usize); assert_eq!(count_nominators, Nominators::<Test>::count() as usize);
assert_eq!(count_validators, v as usize); assert_eq!(count_validators, v as usize);
assert_eq!(count_nominators, n as usize); assert_eq!(count_nominators, n as usize);
+16 -4
View File
@@ -70,10 +70,22 @@ pub mod v8 {
pub mod v7 { pub mod v7 {
use super::*; use super::*;
use frame_support::generate_storage_alias;
generate_storage_alias!(Staking, CounterForValidators => Value<u32>);
generate_storage_alias!(Staking, CounterForNominators => Value<u32>);
pub fn pre_migrate<T: Config>() -> Result<(), &'static str> { pub fn pre_migrate<T: Config>() -> Result<(), &'static str> {
assert!(CounterForValidators::<T>::get().is_zero(), "CounterForValidators already set."); assert!(
assert!(CounterForNominators::<T>::get().is_zero(), "CounterForNominators already set."); CounterForValidators::get().unwrap().is_zero(),
"CounterForValidators already set."
);
assert!(
CounterForNominators::get().unwrap().is_zero(),
"CounterForNominators already set."
);
assert!(Validators::<T>::count().is_zero(), "Validators already set.");
assert!(Nominators::<T>::count().is_zero(), "Nominators already set.");
assert!(StorageVersion::<T>::get() == Releases::V6_0_0); assert!(StorageVersion::<T>::get() == Releases::V6_0_0);
Ok(()) Ok(())
} }
@@ -83,8 +95,8 @@ pub mod v7 {
let validator_count = Validators::<T>::iter().count() as u32; let validator_count = Validators::<T>::iter().count() as u32;
let nominator_count = Nominators::<T>::iter().count() as u32; let nominator_count = Nominators::<T>::iter().count() as u32;
CounterForValidators::<T>::put(validator_count); CounterForValidators::put(validator_count);
CounterForNominators::<T>::put(nominator_count); CounterForNominators::put(nominator_count);
StorageVersion::<T>::put(Releases::V7_0_0); StorageVersion::<T>::put(Releases::V7_0_0);
log!(info, "Completed staking migration to Releases::V7_0_0"); log!(info, "Completed staking migration to Releases::V7_0_0");
+2 -2
View File
@@ -536,8 +536,8 @@ fn post_conditions() {
fn check_count() { fn check_count() {
let nominator_count = Nominators::<Test>::iter().count() as u32; let nominator_count = Nominators::<Test>::iter().count() as u32;
let validator_count = Validators::<Test>::iter().count() as u32; let validator_count = Validators::<Test>::iter().count() as u32;
assert_eq!(nominator_count, CounterForNominators::<Test>::get()); assert_eq!(nominator_count, Nominators::<Test>::count());
assert_eq!(validator_count, CounterForValidators::<Test>::get()); assert_eq!(validator_count, Validators::<Test>::count());
// the voters that the `SortedListProvider` list is storing for us. // the voters that the `SortedListProvider` list is storing for us.
let external_voters = <Test as Config>::SortedListProvider::count(); let external_voters = <Test as Config>::SortedListProvider::count();
+18 -37
View File
@@ -665,8 +665,8 @@ impl<T: Config> Pallet<T> {
maybe_max_len: Option<usize>, maybe_max_len: Option<usize>,
) -> Vec<(T::AccountId, VoteWeight, Vec<T::AccountId>)> { ) -> Vec<(T::AccountId, VoteWeight, Vec<T::AccountId>)> {
let max_allowed_len = { let max_allowed_len = {
let nominator_count = CounterForNominators::<T>::get() as usize; let nominator_count = Nominators::<T>::count() as usize;
let validator_count = CounterForValidators::<T>::get() as usize; let validator_count = Validators::<T>::count() as usize;
let all_voter_count = validator_count.saturating_add(nominator_count); let all_voter_count = validator_count.saturating_add(nominator_count);
maybe_max_len.unwrap_or(all_voter_count).min(all_voter_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, /// 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. /// 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 /// 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. /// wrong.
pub fn do_add_nominator(who: &T::AccountId, nominations: Nominations<T::AccountId>) { pub fn do_add_nominator(who: &T::AccountId, nominations: Nominations<T::AccountId>) {
if !Nominators::<T>::contains_key(who) { 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. // 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() { if T::SortedListProvider::on_insert(who.clone(), Self::weight_of(who)).is_err() {
log!(warn, "attempt to insert duplicate nominator ({:#?})", who); 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, /// 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. /// 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 /// 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. /// wrong.
pub fn do_remove_nominator(who: &T::AccountId) -> bool { pub fn do_remove_nominator(who: &T::AccountId) -> bool {
if Nominators::<T>::contains_key(who) { if Nominators::<T>::contains_key(who) {
Nominators::<T>::remove(who); Nominators::<T>::remove(who);
CounterForNominators::<T>::mutate(|x| x.saturating_dec());
T::SortedListProvider::on_remove(who); T::SortedListProvider::on_remove(who);
debug_assert_eq!(T::SortedListProvider::sanity_check(), Ok(())); 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 true
} else { } else {
false false
} }
} }
/// This function will add a validator to the `Validators` storage map, and keep track of the /// This function will add a validator to the `Validators` storage map.
/// `CounterForValidators`.
/// ///
/// If the validator already exists, their preferences will be updated. /// 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 /// 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. /// wrong.
pub fn do_add_validator(who: &T::AccountId, prefs: ValidatorPrefs) { 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); Validators::<T>::insert(who, prefs);
} }
/// This function will remove a validator from the `Validators` storage map, /// This function will remove a validator from the `Validators` storage map.
/// and keep track of the `CounterForValidators`.
/// ///
/// Returns true if `who` was removed from `Validators`, otherwise false. /// 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 /// 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. /// wrong.
pub fn do_remove_validator(who: &T::AccountId) -> bool { pub fn do_remove_validator(who: &T::AccountId) -> bool {
if Validators::<T>::contains_key(who) { if Validators::<T>::contains_key(who) {
Validators::<T>::remove(who); Validators::<T>::remove(who);
CounterForValidators::<T>::mutate(|x| x.saturating_dec());
true true
} else { } else {
false false
@@ -865,14 +855,6 @@ impl<T: Config> ElectionDataProvider<T::AccountId, BlockNumberFor<T>> for Pallet
fn voters( fn voters(
maybe_max_len: Option<usize>, maybe_max_len: Option<usize>,
) -> data_provider::Result<Vec<(T::AccountId, VoteWeight, Vec<T::AccountId>)>> { ) -> 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_eq!(
CounterForNominators::<T>::get(),
T::SortedListProvider::count(),
"voter_count must be accurate",
);
// This can never fail -- if `maybe_max_len` is `Some(_)` we handle it. // This can never fail -- if `maybe_max_len` is `Some(_)` we handle it.
let voters = Self::get_npos_voters(maybe_max_len); let voters = Self::get_npos_voters(maybe_max_len);
debug_assert!(maybe_max_len.map_or(true, |max| voters.len() <= max)); debug_assert!(maybe_max_len.map_or(true, |max| voters.len() <= max));
@@ -881,7 +863,7 @@ impl<T: Config> ElectionDataProvider<T::AccountId, BlockNumberFor<T>> for Pallet
} }
fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<T::AccountId>> { 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. // 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) { if maybe_max_len.map_or(false, |max_len| target_count > max_len as u32) {
@@ -967,10 +949,9 @@ impl<T: Config> ElectionDataProvider<T::AccountId, BlockNumberFor<T>> for Pallet
fn clear() { fn clear() {
<Bonded<T>>::remove_all(None); <Bonded<T>>::remove_all(None);
<Ledger<T>>::remove_all(None); <Ledger<T>>::remove_all(None);
<Validators<T>>::remove_all(None); <Validators<T>>::remove_all();
<Nominators<T>>::remove_all(None); <Nominators<T>>::remove_all();
<CounterForNominators<T>>::kill();
<CounterForValidators<T>>::kill();
T::SortedListProvider::unsafe_clear(); T::SortedListProvider::unsafe_clear();
} }
@@ -1284,7 +1265,7 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsMap<T> {
Box::new(Nominators::<T>::iter().map(|(n, _)| n)) Box::new(Nominators::<T>::iter().map(|(n, _)| n))
} }
fn count() -> u32 { fn count() -> u32 {
CounterForNominators::<T>::get() Nominators::<T>::count()
} }
fn contains(id: &T::AccountId) -> bool { fn contains(id: &T::AccountId) -> bool {
Nominators::<T>::contains_key(id) Nominators::<T>::contains_key(id)
@@ -1309,10 +1290,10 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsMap<T> {
fn sanity_check() -> Result<(), &'static str> { fn sanity_check() -> Result<(), &'static str> {
Ok(()) Ok(())
} }
fn unsafe_clear() { fn unsafe_clear() {
// NOTE: Caller must ensure this doesn't lead to too many storage accesses. This is a // NOTE: Caller must ensure this doesn't lead to too many storage accesses. This is a
// condition of SortedListProvider::unsafe_clear. // condition of SortedListProvider::unsafe_clear.
Nominators::<T>::remove_all(None); Nominators::<T>::remove_all();
CounterForNominators::<T>::take();
} }
} }
+7 -19
View File
@@ -234,16 +234,10 @@ pub mod pallet {
StorageMap<_, Twox64Concat, T::AccountId, RewardDestination<T::AccountId>, ValueQuery>; StorageMap<_, Twox64Concat, T::AccountId, RewardDestination<T::AccountId>, ValueQuery>;
/// The map from (wannabe) validator stash key to the preferences of that validator. /// The map from (wannabe) validator stash key to the preferences of that validator.
///
/// When updating this storage item, you must also update the `CounterForValidators`.
#[pallet::storage] #[pallet::storage]
#[pallet::getter(fn validators)] #[pallet::getter(fn validators)]
pub type Validators<T: Config> = pub type Validators<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, ValidatorPrefs, ValueQuery>; CountedStorageMap<_, Twox64Concat, T::AccountId, ValidatorPrefs, ValueQuery>;
/// A tracker to keep count of the number of items in the `Validators` map.
#[pallet::storage]
pub type CounterForValidators<T> = StorageValue<_, u32, ValueQuery>;
/// The maximum validator count before we stop allowing new validators to join. /// The maximum validator count before we stop allowing new validators to join.
/// ///
@@ -252,16 +246,10 @@ pub mod pallet {
pub type MaxValidatorsCount<T> = StorageValue<_, u32, OptionQuery>; pub type MaxValidatorsCount<T> = StorageValue<_, u32, OptionQuery>;
/// The map from nominator stash key to the set of stash keys of all validators to nominate. /// The map from nominator stash key to the set of stash keys of all validators to nominate.
///
/// When updating this storage item, you must also update the `CounterForNominators`.
#[pallet::storage] #[pallet::storage]
#[pallet::getter(fn nominators)] #[pallet::getter(fn nominators)]
pub type Nominators<T: Config> = pub type Nominators<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, Nominations<T::AccountId>>; CountedStorageMap<_, Twox64Concat, T::AccountId, Nominations<T::AccountId>>;
/// A tracker to keep count of the number of items in the `Nominators` map.
#[pallet::storage]
pub type CounterForNominators<T> = StorageValue<_, u32, ValueQuery>;
/// The maximum nominator count before we stop allowing new validators to join. /// The maximum nominator count before we stop allowing new validators to join.
/// ///
@@ -570,7 +558,7 @@ pub mod pallet {
// all voters are reported to the `SortedListProvider`. // all voters are reported to the `SortedListProvider`.
assert_eq!( assert_eq!(
T::SortedListProvider::count(), T::SortedListProvider::count(),
CounterForNominators::<T>::get(), Nominators::<T>::count(),
"not all genesis stakers were inserted into sorted list provider, something is wrong." "not all genesis stakers were inserted into sorted list provider, something is wrong."
); );
} }
@@ -987,7 +975,7 @@ pub mod pallet {
// the runtime. // the runtime.
if let Some(max_validators) = MaxValidatorsCount::<T>::get() { if let Some(max_validators) = MaxValidatorsCount::<T>::get() {
ensure!( ensure!(
CounterForValidators::<T>::get() < max_validators, Validators::<T>::count() < max_validators,
Error::<T>::TooManyValidators Error::<T>::TooManyValidators
); );
} }
@@ -1027,7 +1015,7 @@ pub mod pallet {
// the runtime. // the runtime.
if let Some(max_nominators) = MaxNominatorsCount::<T>::get() { if let Some(max_nominators) = MaxNominatorsCount::<T>::get() {
ensure!( ensure!(
CounterForNominators::<T>::get() < max_nominators, Nominators::<T>::count() < max_nominators,
Error::<T>::TooManyNominators Error::<T>::TooManyNominators
); );
} }
@@ -1610,7 +1598,7 @@ pub mod pallet {
let min_active_bond = if Nominators::<T>::contains_key(&stash) { let min_active_bond = if Nominators::<T>::contains_key(&stash) {
let max_nominator_count = let max_nominator_count =
MaxNominatorsCount::<T>::get().ok_or(Error::<T>::CannotChillOther)?; MaxNominatorsCount::<T>::get().ok_or(Error::<T>::CannotChillOther)?;
let current_nominator_count = CounterForNominators::<T>::get(); let current_nominator_count = Nominators::<T>::count();
ensure!( ensure!(
threshold * max_nominator_count < current_nominator_count, threshold * max_nominator_count < current_nominator_count,
Error::<T>::CannotChillOther Error::<T>::CannotChillOther
@@ -1619,7 +1607,7 @@ pub mod pallet {
} else if Validators::<T>::contains_key(&stash) { } else if Validators::<T>::contains_key(&stash) {
let max_validator_count = let max_validator_count =
MaxValidatorsCount::<T>::get().ok_or(Error::<T>::CannotChillOther)?; MaxValidatorsCount::<T>::get().ok_or(Error::<T>::CannotChillOther)?;
let current_validator_count = CounterForValidators::<T>::get(); let current_validator_count = Validators::<T>::count();
ensure!( ensure!(
threshold * max_validator_count < current_validator_count, threshold * max_validator_count < current_validator_count,
Error::<T>::CannotChillOther Error::<T>::CannotChillOther
+3 -4
View File
@@ -36,12 +36,11 @@ const SEED: u32 = 0;
/// This function removes all validators and nominators from storage. /// This function removes all validators and nominators from storage.
pub fn clear_validators_and_nominators<T: Config>() { pub fn clear_validators_and_nominators<T: Config>() {
Validators::<T>::remove_all(None); Validators::<T>::remove_all();
CounterForValidators::<T>::kill();
// whenever we touch nominators counter we should update `T::SortedListProvider` as well. // whenever we touch nominators counter we should update `T::SortedListProvider` as well.
Nominators::<T>::remove_all(None); Nominators::<T>::remove_all();
CounterForNominators::<T>::kill();
// NOTE: safe to call outside block production // NOTE: safe to call outside block production
T::SortedListProvider::unsafe_clear(); T::SortedListProvider::unsafe_clear();
} }
+8 -8
View File
@@ -4300,8 +4300,8 @@ fn chill_other_works() {
.min_nominator_bond(1_000) .min_nominator_bond(1_000)
.min_validator_bond(1_500) .min_validator_bond(1_500)
.build_and_execute(|| { .build_and_execute(|| {
let initial_validators = CounterForValidators::<Test>::get(); let initial_validators = Validators::<Test>::count();
let initial_nominators = CounterForNominators::<Test>::get(); let initial_nominators = Nominators::<Test>::count();
for i in 0..15 { for i in 0..15 {
let a = 4 * i; let a = 4 * i;
let b = 4 * i + 1; let b = 4 * i + 1;
@@ -4424,8 +4424,8 @@ fn chill_other_works() {
)); ));
// 16 people total because tests start with 2 active one // 16 people total because tests start with 2 active one
assert_eq!(CounterForNominators::<Test>::get(), 15 + initial_nominators); assert_eq!(Nominators::<Test>::count(), 15 + initial_nominators);
assert_eq!(CounterForValidators::<Test>::get(), 15 + initial_validators); assert_eq!(Validators::<Test>::count(), 15 + initial_validators);
// Users can now be chilled down to 7 people, so we try to remove 9 of them (starting // Users can now be chilled down to 7 people, so we try to remove 9 of them (starting
// with 16) // with 16)
@@ -4437,13 +4437,13 @@ fn chill_other_works() {
} }
// chill a nominator. Limit is not reached, not chill-able // chill a nominator. Limit is not reached, not chill-able
assert_eq!(CounterForNominators::<Test>::get(), 7); assert_eq!(Nominators::<Test>::count(), 7);
assert_noop!( assert_noop!(
Staking::chill_other(Origin::signed(1337), 1), Staking::chill_other(Origin::signed(1337), 1),
Error::<Test>::CannotChillOther Error::<Test>::CannotChillOther
); );
// chill a validator. Limit is reached, chill-able. // chill a validator. Limit is reached, chill-able.
assert_eq!(CounterForValidators::<Test>::get(), 9); assert_eq!(Validators::<Test>::count(), 9);
assert_ok!(Staking::chill_other(Origin::signed(1337), 3)); assert_ok!(Staking::chill_other(Origin::signed(1337), 3));
}) })
} }
@@ -4451,9 +4451,9 @@ fn chill_other_works() {
#[test] #[test]
fn capped_stakers_works() { fn capped_stakers_works() {
ExtBuilder::default().build_and_execute(|| { ExtBuilder::default().build_and_execute(|| {
let validator_count = CounterForValidators::<Test>::get(); let validator_count = Validators::<Test>::count();
assert_eq!(validator_count, 3); assert_eq!(validator_count, 3);
let nominator_count = CounterForNominators::<Test>::get(); let nominator_count = Nominators::<Test>::count();
assert_eq!(nominator_count, 1); assert_eq!(nominator_count, 1);
// Change the maximums // Change the maximums
@@ -98,6 +98,17 @@ where
OnEmpty: Get<QueryKind::Query> + 'static, OnEmpty: Get<QueryKind::Query> + 'static,
MaxValues: Get<Option<u32>>, MaxValues: Get<Option<u32>>,
{ {
/// The key used to store the counter of the map.
pub fn counter_storage_final_key() -> [u8; 32] {
CounterFor::<Prefix>::hashed_key()
}
/// The prefix used to generate the key of the map.
pub fn map_storage_final_prefix() -> Vec<u8> {
use crate::storage::generator::StorageMap;
<Self as MapWrapper>::Map::prefix_hash()
}
/// Get the storage key used to fetch a value corresponding to a specific key. /// Get the storage key used to fetch a value corresponding to a specific key.
pub fn hashed_key_for<KeyArg: EncodeLike<Key>>(key: KeyArg) -> Vec<u8> { pub fn hashed_key_for<KeyArg: EncodeLike<Key>>(key: KeyArg) -> Vec<u8> {
<Self as MapWrapper>::Map::hashed_key_for(key) <Self as MapWrapper>::Map::hashed_key_for(key)