Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
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
8 changes: 6 additions & 2 deletions frame/bags-list/remote-tests/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ pub async fn execute<Runtime: crate::RuntimeT, Block: BlockT>(
.inject_hashed_prefix(&<pallet_staking::Ledger<Runtime>>::prefix_hash())
.inject_hashed_prefix(&<pallet_staking::Validators<Runtime>>::prefix_hash())
.inject_hashed_prefix(&<pallet_staking::Nominators<Runtime>>::prefix_hash())
.inject_hashed_key(&<pallet_staking::CounterForNominators<Runtime>>::hashed_key())
.inject_hashed_key(&<pallet_staking::CounterForValidators<Runtime>>::hashed_key())
.inject_hashed_key(&<pallet_staking::CounterForNominators<Runtime>>::hashed_key_for(
&<pallet_staking::CounterForNominators<Runtime>>::count(),
))
.inject_hashed_key(&<pallet_staking::CounterForValidators<Runtime>>::hashed_key_for(
&<pallet_staking::CounterForValidators<Runtime>>::count(),
))
.build()
.await
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion frame/election-provider-multi-phase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ pub fn create_validator_with_nominators<T: Config>(

assert_eq!(new_validators.len(), 1);
assert_eq!(new_validators[0], v_stash, "Our validator was not selected!");
assert_ne!(CounterForValidators::<T>::get(), 0);
assert_ne!(CounterForNominators::<T>::get(), 0);
assert_ne!(CounterForValidators::<T>::count(), 0);
assert_ne!(CounterForNominators::<T>::count(), 0);

// Give Era Points
let reward = EraRewardPoints::<T::AccountId> {
Expand Down
10 changes: 6 additions & 4 deletions frame/staking/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ pub mod v7 {
use super::*;

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

CounterForValidators::<T>::put(validator_count);
CounterForNominators::<T>::put(nominator_count);
let counter_for_validators = CounterForValidators::<T>::count();
CounterForValidators::<T>::insert(counter_for_validators, validator_count);
let counter_for_nominators = CounterForNominators::<T>::count();
CounterForNominators::<T>::insert(counter_for_nominators, nominator_count);

StorageVersion::<T>::put(Releases::V7_0_0);
log!(info, "Completed staking migration to Releases::V7_0_0");
Expand Down
45 changes: 29 additions & 16 deletions frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = CounterForNominators::<T>::count() as usize;
let validator_count = CounterForValidators::<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)
};
Expand Down Expand Up @@ -774,8 +774,11 @@ impl<T: Config> Pallet<T> {
/// wrong.
pub fn do_add_nominator(who: &T::AccountId, nominations: Nominations<T::AccountId>) {
if !Nominators::<T>::contains_key(who) {
let mut count = CounterForNominators::<T>::count();
let counter_key = count;
count.saturating_inc();
// maybe update the counter.
CounterForNominators::<T>::mutate(|x| x.saturating_inc());
CounterForNominators::<T>::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() {
Expand All @@ -800,10 +803,13 @@ impl<T: Config> Pallet<T> {
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());
let mut count = CounterForNominators::<T>::count();
let counter_key = count;
count.saturating_dec();
CounterForNominators::<T>::insert(counter_key, count);
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!(CounterForNominators::<T>::count(), T::SortedListProvider::count());
true
} else {
false
Expand All @@ -820,7 +826,10 @@ impl<T: Config> Pallet<T> {
/// wrong.
pub fn do_add_validator(who: &T::AccountId, prefs: ValidatorPrefs) {
if !Validators::<T>::contains_key(who) {
CounterForValidators::<T>::mutate(|x| x.saturating_inc())
let mut count = CounterForValidators::<T>::count();
let counter_key = count;
count.saturating_inc();
CounterForValidators::<T>::insert(counter_key, count)
}
Validators::<T>::insert(who, prefs);
}
Expand All @@ -836,7 +845,10 @@ impl<T: Config> Pallet<T> {
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());
let mut count = CounterForValidators::<T>::count();
let counter_key = count;
count.saturating_dec();
CounterForValidators::<T>::insert(counter_key, count);
true
} else {
false
Expand Down Expand Up @@ -865,10 +877,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 == CounterForNominators::<T>::count());
debug_assert!(<Validators<T>>::iter().count() as u32 == CounterForValidators::<T>::count());
debug_assert_eq!(
CounterForNominators::<T>::get(),
CounterForNominators::<T>::count(),
T::SortedListProvider::count(),
"voter_count must be accurate",
);
Expand All @@ -881,7 +893,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 = CounterForValidators::<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) {
Expand Down Expand Up @@ -969,8 +981,8 @@ 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();
<CounterForNominators<T>>::remove_all();
<CounterForValidators<T>>::remove_all();
let _ = T::SortedListProvider::clear(None);
}

Expand Down Expand Up @@ -1282,7 +1294,7 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsMap<T> {
Box::new(Nominators::<T>::iter().map(|(n, _)| n))
}
fn count() -> u32 {
CounterForNominators::<T>::get()
CounterForNominators::<T>::count()
}
fn contains(id: &T::AccountId) -> bool {
Nominators::<T>::contains_key(id)
Expand Down Expand Up @@ -1310,10 +1322,11 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsMap<T> {
fn clear(maybe_count: Option<u32>) -> u32 {
Nominators::<T>::remove_all(maybe_count);
if let Some(count) = maybe_count {
CounterForNominators::<T>::mutate(|noms| *noms - count);
let nominator_count = CounterForNominators::<T>::count();
CounterForNominators::<T>::insert(nominator_count, nominator_count - count);
count
} else {
CounterForNominators::<T>::take()
CounterForNominators::<T>::take(CounterForNominators::<T>::count()).unwrap()
}
}
}
19 changes: 9 additions & 10 deletions frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ pub mod pallet {
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 CounterForValidators<T> = StorageValue<_, u32, ValueQuery>;
pub type CounterForValidators<T> =
CountedStorageMap<Hasher = Twox64Concat, Key = u32, Value = u32>;

/// The maximum validator count before we stop allowing new validators to join.
///
Expand All @@ -246,10 +246,9 @@ pub mod pallet {
#[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 CounterForNominators<T> = StorageValue<_, u32, ValueQuery>;
pub type CounterForNominators<T> =
CountedStorageMap<Hasher = Twox64Concat, Key = u32, Value = u32>;

/// The maximum nominator count before we stop allowing new validators to join.
///
Expand Down Expand Up @@ -548,7 +547,7 @@ pub mod pallet {
// all voters are reported to the `SortedListProvider`.
assert_eq!(
T::SortedListProvider::count(),
CounterForNominators::<T>::get(),
CounterForNominators::<T>::count(),
"not all genesis stakers were inserted into sorted list provider, something is wrong."
);
}
Expand Down Expand Up @@ -957,7 +956,7 @@ pub mod pallet {
// the runtime.
if let Some(max_validators) = MaxValidatorsCount::<T>::get() {
ensure!(
CounterForValidators::<T>::get() < max_validators,
CounterForValidators::<T>::count() < max_validators,
Error::<T>::TooManyValidators
);
}
Expand Down Expand Up @@ -997,7 +996,7 @@ pub mod pallet {
// the runtime.
if let Some(max_nominators) = MaxNominatorsCount::<T>::get() {
ensure!(
CounterForNominators::<T>::get() < max_nominators,
CounterForNominators::<T>::count() < max_nominators,
Error::<T>::TooManyNominators
);
}
Expand Down Expand Up @@ -1570,7 +1569,7 @@ pub mod pallet {
let min_active_bond = if Nominators::<T>::contains_key(&stash) {
let max_nominator_count =
MaxNominatorsCount::<T>::get().ok_or(Error::<T>::CannotChillOther)?;
let current_nominator_count = CounterForNominators::<T>::get();
let current_nominator_count = CounterForNominators::<T>::count();
ensure!(
threshold * max_nominator_count < current_nominator_count,
Error::<T>::CannotChillOther
Expand All @@ -1579,7 +1578,7 @@ pub mod pallet {
} else if Validators::<T>::contains_key(&stash) {
let max_validator_count =
MaxValidatorsCount::<T>::get().ok_or(Error::<T>::CannotChillOther)?;
let current_validator_count = CounterForValidators::<T>::get();
let current_validator_count = CounterForValidators::<T>::count();
ensure!(
threshold * max_validator_count < current_validator_count,
Error::<T>::CannotChillOther
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 @@ -37,11 +37,11 @@ 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);
CounterForValidators::<T>::kill();
CounterForValidators::<T>::remove_all();

// whenever we touch nominators counter we should update `T::SortedListProvider` as well.
Nominators::<T>::remove_all(None);
CounterForNominators::<T>::kill();
CounterForNominators::<T>::remove_all();
let _ = T::SortedListProvider::clear(None);
}

Expand Down
4 changes: 2 additions & 2 deletions frame/support/procedural/src/pallet/parse/pallet_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down