Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Make New Staking Counter Backwards Compatible with CountedMap #9126

Merged
1 commit merged into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,8 @@ pub mod migrations {
use super::*;

pub fn pre_migrate<T: Config>() -> Result<(), &'static str> {
assert!(CurrentValidatorsCount::<T>::get().is_zero(), "CurrentValidatorsCount already set.");
assert!(CurrentNominatorsCount::<T>::get().is_zero(), "CurrentNominatorsCount already set.");
assert!(CounterForValidators::<T>::get().is_zero(), "CounterForValidators already set.");
assert!(CounterForNominators::<T>::get().is_zero(), "CounterForNominators already set.");
assert!(StorageVersion::<T>::get() == Releases::V6_0_0);
Ok(())
}
Expand All @@ -772,8 +772,8 @@ pub mod migrations {
let validator_count = Validators::<T>::iter().count() as u32;
let nominator_count = Nominators::<T>::iter().count() as u32;

CurrentValidatorsCount::<T>::put(validator_count);
CurrentNominatorsCount::<T>::put(nominator_count);
CounterForValidators::<T>::put(validator_count);
CounterForNominators::<T>::put(nominator_count);

StorageVersion::<T>::put(Releases::V7_0_0);
log!(info, "Completed staking migration to Releases::V7_0_0");
Expand Down Expand Up @@ -998,14 +998,14 @@ pub mod pallet {

/// The map from (wannabe) validator stash key to the preferences of that validator.
///
/// When updating this storage item, you must also update the `CurrentValidatorsCount`.
/// When updating this storage item, you must also update the `CounterForValidators`.
#[pallet::storage]
#[pallet::getter(fn validators)]
pub type Validators<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, ValidatorPrefs, ValueQuery>;

/// A tracker to keep count of the number of items in the `Validators` map.
#[pallet::storage]
pub type CurrentValidatorsCount<T> = StorageValue<_, u32, ValueQuery>;
pub type CounterForValidators<T> = StorageValue<_, u32, ValueQuery>;

/// The maximum validator count before we stop allowing new validators to join.
///
Expand All @@ -1015,14 +1015,14 @@ pub mod pallet {

/// 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 `CurrentNominatorsCount`.
/// When updating this storage item, you must also update the `CounterForNominators`.
#[pallet::storage]
#[pallet::getter(fn nominators)]
pub type Nominators<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, Nominations<T::AccountId>>;

/// A tracker to keep count of the number of items in the `Nominators` map.
#[pallet::storage]
pub type CurrentNominatorsCount<T> = StorageValue<_, u32, ValueQuery>;
pub type CounterForNominators<T> = StorageValue<_, u32, ValueQuery>;

/// The maximum nominator count before we stop allowing new validators to join.
///
Expand Down Expand Up @@ -1717,7 +1717,7 @@ pub mod pallet {
// If this error is reached, we need to adjust the `MinValidatorBond` and start calling `chill_other`.
// Until then, we explicitly block new validators to protect the runtime.
if let Some(max_validators) = MaxValidatorsCount::<T>::get() {
ensure!(CurrentValidatorsCount::<T>::get() < max_validators, Error::<T>::TooManyValidators);
ensure!(CounterForValidators::<T>::get() < max_validators, Error::<T>::TooManyValidators);
}

let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
Expand Down Expand Up @@ -1758,7 +1758,7 @@ pub mod pallet {
// If this error is reached, we need to adjust the `MinNominatorBond` and start calling `chill_other`.
// Until then, we explicitly block new nominators to protect the runtime.
if let Some(max_nominators) = MaxNominatorsCount::<T>::get() {
ensure!(CurrentNominatorsCount::<T>::get() < max_nominators, Error::<T>::TooManyNominators);
ensure!(CounterForNominators::<T>::get() < max_nominators, Error::<T>::TooManyNominators);
}

let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
Expand Down Expand Up @@ -2966,42 +2966,42 @@ impl<T: Config> Pallet<T> {
}

/// This function will add a nominator to the `Nominators` storage map,
/// and keep track of the `CurrentNominatorsCount`.
/// and keep track of the `CounterForNominators`.
///
/// If the nominator already exists, their nominations will be updated.
pub fn do_add_nominator(who: &T::AccountId, nominations: Nominations<T::AccountId>) {
if !Nominators::<T>::contains_key(who) {
CurrentNominatorsCount::<T>::mutate(|x| x.saturating_inc())
CounterForNominators::<T>::mutate(|x| x.saturating_inc())
}
Nominators::<T>::insert(who, nominations);
}

/// This function will remove a nominator from the `Nominators` storage map,
/// and keep track of the `CurrentNominatorsCount`.
/// and keep track of the `CounterForNominators`.
pub fn do_remove_nominator(who: &T::AccountId) {
if Nominators::<T>::contains_key(who) {
Nominators::<T>::remove(who);
CurrentNominatorsCount::<T>::mutate(|x| x.saturating_dec());
CounterForNominators::<T>::mutate(|x| x.saturating_dec());
}
}

/// This function will add a validator to the `Validators` storage map,
/// and keep track of the `CurrentValidatorsCount`.
/// and keep track of the `CounterForValidators`.
///
/// If the validator already exists, their preferences will be updated.
pub fn do_add_validator(who: &T::AccountId, prefs: ValidatorPrefs) {
if !Validators::<T>::contains_key(who) {
CurrentValidatorsCount::<T>::mutate(|x| x.saturating_inc())
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 `CurrentValidatorsCount`.
/// and keep track of the `CounterForValidators`.
pub fn do_remove_validator(who: &T::AccountId) {
if Validators::<T>::contains_key(who) {
Validators::<T>::remove(who);
CurrentValidatorsCount::<T>::mutate(|x| x.saturating_dec());
CounterForValidators::<T>::mutate(|x| x.saturating_dec());
}
}
}
Expand All @@ -3017,11 +3017,11 @@ impl<T: Config> frame_election_provider_support::ElectionDataProvider<T::Account
fn voters(
maybe_max_len: Option<usize>,
) -> data_provider::Result<(Vec<(T::AccountId, VoteWeight, Vec<T::AccountId>)>, Weight)> {
let nominator_count = CurrentNominatorsCount::<T>::get();
let validator_count = CurrentValidatorsCount::<T>::get();
let nominator_count = CounterForNominators::<T>::get();
let validator_count = CounterForValidators::<T>::get();
let voter_count = nominator_count.saturating_add(validator_count) as usize;
debug_assert!(<Nominators<T>>::iter().count() as u32 == CurrentNominatorsCount::<T>::get());
debug_assert!(<Validators<T>>::iter().count() as u32 == CurrentValidatorsCount::<T>::get());
debug_assert!(<Nominators<T>>::iter().count() as u32 == CounterForNominators::<T>::get());
debug_assert!(<Validators<T>>::iter().count() as u32 == CounterForValidators::<T>::get());

if maybe_max_len.map_or(false, |max_len| voter_count > max_len) {
return Err("Voter snapshot too big");
Expand All @@ -3037,7 +3037,7 @@ impl<T: Config> frame_election_provider_support::ElectionDataProvider<T::Account
}

fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<(Vec<T::AccountId>, Weight)> {
let target_count = CurrentValidatorsCount::<T>::get() as usize;
let target_count = CounterForValidators::<T>::get() as usize;

if maybe_max_len.map_or(false, |max_len| target_count > max_len) {
return Err("Target snapshot too big");
Expand Down
4 changes: 2 additions & 2 deletions frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ fn post_conditions() {
fn check_count() {
let nominator_count = Nominators::<Test>::iter().count() as u32;
let validator_count = Validators::<Test>::iter().count() as u32;
assert_eq!(nominator_count, CurrentNominatorsCount::<Test>::get());
assert_eq!(validator_count, CurrentValidatorsCount::<Test>::get());
assert_eq!(nominator_count, CounterForNominators::<Test>::get());
assert_eq!(validator_count, CounterForValidators::<Test>::get());
}

fn check_ledgers() {
Expand Down
4 changes: 2 additions & 2 deletions frame/staking/src/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ const SEED: u32 = 0;
/// This function removes all validators and nominators from storage.
pub fn clear_validators_and_nominators<T: Config>() {
Validators::<T>::remove_all(None);
CurrentValidatorsCount::<T>::kill();
CounterForValidators::<T>::kill();
Nominators::<T>::remove_all(None);
CurrentNominatorsCount::<T>::kill();
CounterForNominators::<T>::kill();
}

/// Grab a funded user.
Expand Down
4 changes: 2 additions & 2 deletions frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4107,9 +4107,9 @@ mod election_data_provider {
#[test]
fn capped_stakers_works() {
ExtBuilder::default().build_and_execute(|| {
let validator_count = CurrentValidatorsCount::<Test>::get();
let validator_count = CounterForValidators::<Test>::get();
assert_eq!(validator_count, 3);
let nominator_count = CurrentNominatorsCount::<Test>::get();
let nominator_count = CounterForNominators::<Test>::get();
assert_eq!(nominator_count, 1);

// Change the maximums
Expand Down