Remove use of trait Store from all pallets and deprecate it. (#13535)

* Remove use of trait Store from staking pallet

* Remove use of trait Store from bounties pallet

* Remove use of trait Store from collective pallet

* Remove use of trait Store from babe pallet

* Remove use of trait Store from assets pallet

* Remove use of trait Store from grandpa pallet

* Remove use of trait Store from balances pallet

* Remove use of trait Store from authorship pallet

* Remove use of trait Store from authority-discovery pallet

* Remove use of trait Store from atomic-swap pallet

* Remove use of trait Store from sudo pallet

* Remove use of trait Store from scheduler pallet

* Remove use of trait Store from scored-pool pallet

* Remove use of trait Store from society pallet

* Remove use of trait Store from lottery pallet

* Remove use of trait Store from executive pallet

* Remove use of trait Store from democracy pallet

* Remove use of trait Store from elections-phragmen pallet

* Remove use of trait Store from indices pallet

* Remove use of trait Store from identity pallet

* Remove use of trait Store from multisig pallet

* Remove use of trait Store from merkle-mountain-range pallet

* Remove use of trait Store from im-online pallet

* Remove use of trait Store from membership pallet

* Remove use of trait Store from nicks pallet

* Remove use of trait Store from session pallet

* Remove use of trait Store from transaction-payment pallet

* Remove use of trait Store from utility pallet

* Remove use of trait Store from child-bounties pallet

* Remove use of trait Store from nis pallet

* Remove use of trait Store from nfts pallet

* Remove use of trait Store from conviction-voting pallet

* Remove use of trait Store from treasury pallet

* Remove use of trait Store from vesting pallet

* Remove use of trait Store from preimage pallet

* Remove use of trait Store from uniques pallet

* Remove use of trait Store from ranked-collective pallet

* Remove use of trait Store from beefy-mmr pallet

* Remove use of trait Store from referenda pallet

* Remove use of trait Store from whitelist pallet

* Remove use of trait Store from alliance pallet

* Remove use of trait Store from nomination-pools pallet

* Remove use of trait Store from state-trie-migration pallet

* Remove use of trait Store from message-queue pallet

* Remove use of trait Store from root-offences pallet

* Remove use of trait Store from root-testing pallet

* Remove use of trait Store from timestamps pallet

* Remove use of trait Store from system pallet

* Remove use of trait Store from offences pallet

* Remove use of trait Store from recovery pallet

* Remove use of trait Store from node-authorization pallet

* Remove use of trait Store from proxy pallet

* Remove use of trait Store from benchmarking pallet

* Remove use of trait Store from bags-list pallet

* Add deprecated warning in store_trait

* Change warning message

* Run cargo fmt

* Fix warning and update tests

* Remove unnecessary allow deprecated

* Remove use of trait Store

* Fix mismatch in expected output

* Minor update to warning message for deprecation of generate_store with Store trait attribute

* Fixes as per review comments

* Fixes as per review suggestions

* Remove use of Store trait from core-fellowship pallet

* Fix type in store_trait.rs

* Fixes as pre review comment
This commit is contained in:
Vivek Pandya
2023-03-13 21:00:05 +05:30
committed by GitHub
parent f6b9e056ae
commit 2009821cde
89 changed files with 119 additions and 168 deletions
+1 -1
View File
@@ -265,7 +265,7 @@ pub mod v10 {
impl<T: Config> OnRuntimeUpgrade for MigrateToV10<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
if StorageVersion::<T>::get() == ObsoleteReleases::V9_0_0 {
let pending_slashes = <Pallet<T> as Store>::UnappliedSlashes::iter().take(512);
let pending_slashes = UnappliedSlashes::<T>::iter().take(512);
for (era, slashes) in pending_slashes {
for slash in slashes {
// in the old slashing scheme, the slash era was the key at which we read
+2 -2
View File
@@ -682,7 +682,7 @@ impl<T: Config> Pallet<T> {
/// Apply previously-unapplied slashes on the beginning of a new era, after a delay.
fn apply_unapplied_slashes(active_era: EraIndex) {
let era_slashes = <Self as Store>::UnappliedSlashes::take(&active_era);
let era_slashes = UnappliedSlashes::<T>::take(&active_era);
log!(
debug,
"found {} slashes scheduled to be executed in era {:?}",
@@ -1369,7 +1369,7 @@ where
active_era,
slash_era + slash_defer_duration + 1,
);
<Self as Store>::UnappliedSlashes::mutate(
UnappliedSlashes::<T>::mutate(
slash_era.saturating_add(slash_defer_duration).saturating_add(One::one()),
move |for_later| for_later.push(unapplied),
);
+2 -3
View File
@@ -68,7 +68,6 @@ pub mod pallet {
const STORAGE_VERSION: StorageVersion = StorageVersion::new(13);
#[pallet::pallet]
#[pallet::generate_store(pub(crate) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
@@ -1452,7 +1451,7 @@ pub mod pallet {
ensure!(!slash_indices.is_empty(), Error::<T>::EmptyTargets);
ensure!(is_sorted_and_unique(&slash_indices), Error::<T>::NotSortedAndUnique);
let mut unapplied = <Self as Store>::UnappliedSlashes::get(&era);
let mut unapplied = UnappliedSlashes::<T>::get(&era);
let last_item = slash_indices[slash_indices.len() - 1];
ensure!((last_item as usize) < unapplied.len(), Error::<T>::InvalidSlashIndex);
@@ -1461,7 +1460,7 @@ pub mod pallet {
unapplied.remove(index);
}
<Self as Store>::UnappliedSlashes::insert(&era, &unapplied);
UnappliedSlashes::<T>::insert(&era, &unapplied);
Ok(())
}
+20 -21
View File
@@ -50,8 +50,9 @@
//! Based on research at <https://research.web3.foundation/en/latest/polkadot/slashing/npos.html>
use crate::{
BalanceOf, Config, Error, Exposure, NegativeImbalanceOf, Pallet, Perbill, SessionInterface,
Store, UnappliedSlash,
BalanceOf, Config, Error, Exposure, NegativeImbalanceOf, NominatorSlashInEra,
OffendingValidators, Pallet, Perbill, SessionInterface, SpanSlash, UnappliedSlash,
ValidatorSlashInEra,
};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
@@ -239,14 +240,13 @@ pub(crate) fn compute_slash<T: Config>(
return None
}
let prior_slash_p =
<Pallet<T> as Store>::ValidatorSlashInEra::get(&params.slash_era, params.stash)
.map_or(Zero::zero(), |(prior_slash_proportion, _)| prior_slash_proportion);
let prior_slash_p = ValidatorSlashInEra::<T>::get(&params.slash_era, params.stash)
.map_or(Zero::zero(), |(prior_slash_proportion, _)| prior_slash_proportion);
// compare slash proportions rather than slash values to avoid issues due to rounding
// error.
if params.slash.deconstruct() > prior_slash_p.deconstruct() {
<Pallet<T> as Store>::ValidatorSlashInEra::insert(
ValidatorSlashInEra::<T>::insert(
&params.slash_era,
params.stash,
&(params.slash, own_slash),
@@ -327,7 +327,7 @@ fn kick_out_if_recent<T: Config>(params: SlashParams<T>) {
/// If after adding the validator `OffendingValidatorsThreshold` is reached
/// a new era will be forced.
fn add_offending_validator<T: Config>(stash: &T::AccountId, disable: bool) {
<Pallet<T> as Store>::OffendingValidators::mutate(|offending| {
OffendingValidators::<T>::mutate(|offending| {
let validators = T::SessionInterface::validators();
let validator_index = match validators.iter().position(|i| i == stash) {
Some(index) => index,
@@ -388,10 +388,9 @@ fn slash_nominators<T: Config>(
let own_slash_difference = own_slash_by_validator.saturating_sub(own_slash_prior);
let mut era_slash =
<Pallet<T> as Store>::NominatorSlashInEra::get(&params.slash_era, stash)
.unwrap_or_else(Zero::zero);
NominatorSlashInEra::<T>::get(&params.slash_era, stash).unwrap_or_else(Zero::zero);
era_slash += own_slash_difference;
<Pallet<T> as Store>::NominatorSlashInEra::insert(&params.slash_era, stash, &era_slash);
NominatorSlashInEra::<T>::insert(&params.slash_era, stash, &era_slash);
era_slash
};
@@ -445,9 +444,9 @@ fn fetch_spans<'a, T: Config + 'a>(
slash_of: &'a mut BalanceOf<T>,
reward_proportion: Perbill,
) -> InspectingSpans<'a, T> {
let spans = <Pallet<T> as Store>::SlashingSpans::get(stash).unwrap_or_else(|| {
let spans = crate::SlashingSpans::<T>::get(stash).unwrap_or_else(|| {
let spans = SlashingSpans::new(window_start);
<Pallet<T> as Store>::SlashingSpans::insert(stash, &spans);
crate::SlashingSpans::<T>::insert(stash, &spans);
spans
});
@@ -496,7 +495,7 @@ impl<'a, T: 'a + Config> InspectingSpans<'a, T> {
) -> Option<SpanIndex> {
let target_span = self.era_span(slash_era)?;
let span_slash_key = (self.stash.clone(), target_span.index);
let mut span_record = <Pallet<T> as Store>::SpanSlash::get(&span_slash_key);
let mut span_record = SpanSlash::<T>::get(&span_slash_key);
let mut changed = false;
let reward = if span_record.slashed < slash {
@@ -527,7 +526,7 @@ impl<'a, T: 'a + Config> InspectingSpans<'a, T> {
if changed {
self.dirty = true;
<Pallet<T> as Store>::SpanSlash::insert(&span_slash_key, &span_record);
SpanSlash::<T>::insert(&span_slash_key, &span_record);
}
Some(target_span.index)
@@ -543,20 +542,20 @@ impl<'a, T: 'a + Config> Drop for InspectingSpans<'a, T> {
if let Some((start, end)) = self.spans.prune(self.window_start) {
for span_index in start..end {
<Pallet<T> as Store>::SpanSlash::remove(&(self.stash.clone(), span_index));
SpanSlash::<T>::remove(&(self.stash.clone(), span_index));
}
}
<Pallet<T> as Store>::SlashingSpans::insert(self.stash, &self.spans);
crate::SlashingSpans::<T>::insert(self.stash, &self.spans);
}
}
/// Clear slashing metadata for an obsolete era.
pub(crate) fn clear_era_metadata<T: Config>(obsolete_era: EraIndex) {
#[allow(deprecated)]
<Pallet<T> as Store>::ValidatorSlashInEra::remove_prefix(&obsolete_era, None);
ValidatorSlashInEra::<T>::remove_prefix(&obsolete_era, None);
#[allow(deprecated)]
<Pallet<T> as Store>::NominatorSlashInEra::remove_prefix(&obsolete_era, None);
NominatorSlashInEra::<T>::remove_prefix(&obsolete_era, None);
}
/// Clear slashing metadata for a dead account.
@@ -564,7 +563,7 @@ pub(crate) fn clear_stash_metadata<T: Config>(
stash: &T::AccountId,
num_slashing_spans: u32,
) -> DispatchResult {
let spans = match <Pallet<T> as Store>::SlashingSpans::get(stash) {
let spans = match crate::SlashingSpans::<T>::get(stash) {
None => return Ok(()),
Some(s) => s,
};
@@ -574,7 +573,7 @@ pub(crate) fn clear_stash_metadata<T: Config>(
Error::<T>::IncorrectSlashingSpans
);
<Pallet<T> as Store>::SlashingSpans::remove(stash);
crate::SlashingSpans::<T>::remove(stash);
// kill slashing-span metadata for account.
//
@@ -582,7 +581,7 @@ pub(crate) fn clear_stash_metadata<T: Config>(
// in that case, they may re-bond, but it would count again as span 0. Further ancient
// slashes would slash into this new bond, since metadata has now been cleared.
for span in spans.iter() {
<Pallet<T> as Store>::SpanSlash::remove(&(stash.clone(), span.index));
SpanSlash::<T>::remove(&(stash.clone(), span.index));
}
Ok(())
+22 -22
View File
@@ -512,10 +512,10 @@ fn no_candidate_emergency_condition() {
// initial validators
assert_eq_uvec!(validator_controllers(), vec![10, 20, 30, 40]);
let prefs = ValidatorPrefs { commission: Perbill::one(), ..Default::default() };
<Staking as crate::Store>::Validators::insert(11, prefs.clone());
Validators::<Test>::insert(11, prefs.clone());
// set the minimum validator count.
<Staking as crate::Store>::MinimumValidatorCount::put(10);
MinimumValidatorCount::<Test>::put(10);
// try to chill
let res = Staking::chill(RuntimeOrigin::signed(10));
@@ -536,7 +536,7 @@ fn no_candidate_emergency_condition() {
// changed)
assert_eq_uvec!(validator_controllers(), vec![10, 20, 30, 40]);
// The chill is still pending.
assert!(!<Staking as crate::Store>::Validators::contains_key(11));
assert!(!Validators::<Test>::contains_key(11));
// No new era is created.
assert_eq!(current_era, CurrentEra::<Test>::get());
});
@@ -2432,7 +2432,7 @@ fn slash_in_old_span_does_not_deselect() {
);
// the validator doesn't get chilled again
assert!(<Staking as Store>::Validators::iter().any(|(stash, _)| stash == 11));
assert!(Validators::<Test>::iter().any(|(stash, _)| stash == 11));
// but we are still forcing a new era
assert_eq!(Staking::force_era(), Forcing::ForceNew);
@@ -2449,7 +2449,7 @@ fn slash_in_old_span_does_not_deselect() {
);
// the validator doesn't get chilled again
assert!(<Staking as Store>::Validators::iter().any(|(stash, _)| stash == 11));
assert!(Validators::<Test>::iter().any(|(stash, _)| stash == 11));
// but it's disabled
assert!(is_disabled(10));
@@ -2648,8 +2648,8 @@ fn garbage_collection_after_slashing() {
);
assert_eq!(Balances::free_balance(11), 2000 - 200);
assert!(<Staking as crate::Store>::SlashingSpans::get(&11).is_some());
assert_eq!(<Staking as crate::Store>::SpanSlash::get(&(11, 0)).amount(), &200);
assert!(SlashingSpans::<Test>::get(&11).is_some());
assert_eq!(SpanSlash::<Test>::get(&(11, 0)).amount(), &200);
on_offence_now(
&[OffenceDetails {
@@ -2665,7 +2665,7 @@ fn garbage_collection_after_slashing() {
assert_eq!(Balances::free_balance(11), 2);
assert_eq!(Balances::total_balance(&11), 2);
let slashing_spans = <Staking as crate::Store>::SlashingSpans::get(&11).unwrap();
let slashing_spans = SlashingSpans::<Test>::get(&11).unwrap();
assert_eq!(slashing_spans.iter().count(), 2);
// reap_stash respects num_slashing_spans so that weight is accurate
@@ -2675,8 +2675,8 @@ fn garbage_collection_after_slashing() {
);
assert_ok!(Staking::reap_stash(RuntimeOrigin::signed(20), 11, 2));
assert!(<Staking as crate::Store>::SlashingSpans::get(&11).is_none());
assert_eq!(<Staking as crate::Store>::SpanSlash::get(&(11, 0)).amount(), &0);
assert!(SlashingSpans::<Test>::get(&11).is_none());
assert_eq!(SpanSlash::<Test>::get(&(11, 0)).amount(), &0);
})
}
@@ -2702,19 +2702,19 @@ fn garbage_collection_on_window_pruning() {
assert_eq!(Balances::free_balance(11), 900);
assert_eq!(Balances::free_balance(101), 2000 - (nominated_value / 10));
assert!(<Staking as crate::Store>::ValidatorSlashInEra::get(&now, &11).is_some());
assert!(<Staking as crate::Store>::NominatorSlashInEra::get(&now, &101).is_some());
assert!(ValidatorSlashInEra::<Test>::get(&now, &11).is_some());
assert!(NominatorSlashInEra::<Test>::get(&now, &101).is_some());
// + 1 because we have to exit the bonding window.
for era in (0..(BondingDuration::get() + 1)).map(|offset| offset + now + 1) {
assert!(<Staking as crate::Store>::ValidatorSlashInEra::get(&now, &11).is_some());
assert!(<Staking as crate::Store>::NominatorSlashInEra::get(&now, &101).is_some());
assert!(ValidatorSlashInEra::<Test>::get(&now, &11).is_some());
assert!(NominatorSlashInEra::<Test>::get(&now, &101).is_some());
mock::start_active_era(era);
}
assert!(<Staking as crate::Store>::ValidatorSlashInEra::get(&now, &11).is_none());
assert!(<Staking as crate::Store>::NominatorSlashInEra::get(&now, &101).is_none());
assert!(ValidatorSlashInEra::<Test>::get(&now, &11).is_none());
assert!(NominatorSlashInEra::<Test>::get(&now, &101).is_none());
})
}
@@ -2755,7 +2755,7 @@ fn slashing_nominators_by_span_max() {
slashing::SlashingSpan { index: 0, start: 0, length: Some(4) },
];
let get_span = |account| <Staking as crate::Store>::SlashingSpans::get(&account).unwrap();
let get_span = |account| SlashingSpans::<Test>::get(&account).unwrap();
assert_eq!(get_span(11).iter().collect::<Vec<_>>(), expected_spans);
@@ -2817,7 +2817,7 @@ fn slashes_are_summed_across_spans() {
assert_eq!(Balances::free_balance(21), 2000);
assert_eq!(Staking::slashable_balance_of(&21), 1000);
let get_span = |account| <Staking as crate::Store>::SlashingSpans::get(&account).unwrap();
let get_span = |account| SlashingSpans::<Test>::get(&account).unwrap();
on_offence_now(
&[OffenceDetails {
@@ -3192,7 +3192,7 @@ fn remove_multi_deferred() {
&[Perbill::from_percent(25)],
);
assert_eq!(<Staking as Store>::UnappliedSlashes::get(&4).len(), 5);
assert_eq!(UnappliedSlashes::<Test>::get(&4).len(), 5);
// fails if list is not sorted
assert_noop!(
@@ -3212,7 +3212,7 @@ fn remove_multi_deferred() {
assert_ok!(Staking::cancel_deferred_slash(RuntimeOrigin::root(), 4, vec![0, 2, 4]));
let slashes = <Staking as Store>::UnappliedSlashes::get(&4);
let slashes = UnappliedSlashes::<Test>::get(&4);
assert_eq!(slashes.len(), 2);
assert_eq!(slashes[0].validator, 21);
assert_eq!(slashes[1].validator, 42);
@@ -3267,7 +3267,7 @@ fn slash_kicks_validators_not_nominators_and_disables_nominator_for_kicked_valid
assert_eq!(Balances::free_balance(101), 2000 - nominator_slash_amount_11);
// check that validator was chilled.
assert!(<Staking as Store>::Validators::iter().all(|(stash, _)| stash != 11));
assert!(Validators::<Test>::iter().all(|(stash, _)| stash != 11));
// actually re-bond the slashed validator
assert_ok!(Staking::validate(RuntimeOrigin::signed(10), Default::default()));
@@ -3612,7 +3612,7 @@ fn zero_slash_keeps_nominators() {
assert_eq!(Balances::free_balance(101), 2000);
// 11 is still removed..
assert!(<Staking as Store>::Validators::iter().all(|(stash, _)| stash != 11));
assert!(Validators::<Test>::iter().all(|(stash, _)| stash != 11));
// but their nominations are kept.
assert_eq!(Staking::nominators(101).unwrap().targets, vec![11, 21]);
});