From e03fdeea4caa00b019e261cf71e73d62a7d28ee5 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Wed, 7 Sep 2022 16:44:26 +0200 Subject: [PATCH 01/36] replace pallet level unboundedness to individual storage items --- frame/staking/src/pallet/mod.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index b8a457beb5eae..2efb83d3844b6 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -58,7 +58,6 @@ pub mod pallet { #[pallet::pallet] #[pallet::generate_store(pub(crate) trait Store)] - #[pallet::without_storage_info] pub struct Pallet(_); /// Possible operations on the configuration values of this pallet. @@ -261,6 +260,7 @@ pub mod pallet { /// invulnerables) and restricted to testnets. #[pallet::storage] #[pallet::getter(fn invulnerables)] + #[pallet::unbounded] pub type Invulnerables = StorageValue<_, Vec, ValueQuery>; /// Map from all locked "stash" accounts to the controller account. @@ -285,17 +285,20 @@ pub mod pallet { /// Map from all (unlocked) "controller" accounts to the info regarding the staking. #[pallet::storage] #[pallet::getter(fn ledger)] + #[pallet::unbounded] pub type Ledger = StorageMap<_, Blake2_128Concat, T::AccountId, StakingLedger>; /// Where the reward payment should be made. Keyed by stash. #[pallet::storage] #[pallet::getter(fn payee)] + #[pallet::unbounded] pub type Payee = StorageMap<_, Twox64Concat, T::AccountId, RewardDestination, ValueQuery>; /// The map from (wannabe) validator stash key to the preferences of that validator. #[pallet::storage] #[pallet::getter(fn validators)] + #[pallet::unbounded] pub type Validators = CountedStorageMap<_, Twox64Concat, T::AccountId, ValidatorPrefs, ValueQuery>; @@ -323,6 +326,7 @@ pub mod pallet { /// [`Call::chill_other`] dispatchable by anyone. #[pallet::storage] #[pallet::getter(fn nominators)] + #[pallet::unbounded] pub type Nominators = CountedStorageMap<_, Twox64Concat, T::AccountId, Nominations>; @@ -346,6 +350,7 @@ pub mod pallet { /// equal to [`SessionInterface::validators`]. #[pallet::storage] #[pallet::getter(fn active_era)] + #[pallet::unbounded] pub type ActiveEra = StorageValue<_, ActiveEraInfo>; /// The session index at which the era start for the last `HISTORY_DEPTH` eras. @@ -364,6 +369,7 @@ pub mod pallet { /// If stakers hasn't been set or has been removed then empty exposure is returned. #[pallet::storage] #[pallet::getter(fn eras_stakers)] + #[pallet::unbounded] pub type ErasStakers = StorageDoubleMap< _, Twox64Concat, @@ -386,6 +392,7 @@ pub mod pallet { /// Is it removed after `HISTORY_DEPTH` eras. /// If stakers hasn't been set or has been removed then empty exposure is returned. #[pallet::storage] + #[pallet::unbounded] #[pallet::getter(fn eras_stakers_clipped)] pub type ErasStakersClipped = StorageDoubleMap< _, @@ -404,6 +411,7 @@ pub mod pallet { /// Is it removed after `HISTORY_DEPTH` eras. // If prefs hasn't been set or has been removed then 0 commission is returned. #[pallet::storage] + #[pallet::unbounded] #[pallet::getter(fn eras_validator_prefs)] pub type ErasValidatorPrefs = StorageDoubleMap< _, @@ -426,6 +434,7 @@ pub mod pallet { /// If reward hasn't been set or has been removed then 0 reward is returned. #[pallet::storage] #[pallet::getter(fn eras_reward_points)] + #[pallet::unbounded] pub type ErasRewardPoints = StorageMap<_, Twox64Concat, EraIndex, EraRewardPoints, ValueQuery>; @@ -438,6 +447,7 @@ pub mod pallet { /// Mode of era forcing. #[pallet::storage] + #[pallet::unbounded] #[pallet::getter(fn force_era)] pub type ForceEra = StorageValue<_, Forcing, ValueQuery>; @@ -456,6 +466,7 @@ pub mod pallet { /// All unapplied slashes that are queued for later. #[pallet::storage] + #[pallet::unbounded] pub type UnappliedSlashes = StorageMap< _, Twox64Concat, @@ -469,6 +480,7 @@ pub mod pallet { /// Must contains information for eras for the range: /// `[active_era - bounding_duration; active_era]` #[pallet::storage] + #[pallet::unbounded] pub(crate) type BondedEras = StorageValue<_, Vec<(EraIndex, SessionIndex)>, ValueQuery>; @@ -491,12 +503,14 @@ pub mod pallet { /// Slashing spans for stash accounts. #[pallet::storage] + #[pallet::unbounded] pub(crate) type SlashingSpans = StorageMap<_, Twox64Concat, T::AccountId, slashing::SlashingSpans>; /// Records information about the maximum slash of a stash within a slashing span, /// as well as how much reward has been paid out. #[pallet::storage] + #[pallet::unbounded] pub(crate) type SpanSlash = StorageMap< _, Twox64Concat, @@ -522,6 +536,7 @@ pub mod pallet { /// whether a given validator has previously offended using binary search. It gets cleared when /// the era ends. #[pallet::storage] + #[pallet::unbounded] #[pallet::getter(fn offending_validators)] pub type OffendingValidators = StorageValue<_, Vec<(u32, bool)>, ValueQuery>; @@ -530,6 +545,7 @@ pub mod pallet { /// /// This is set to v7.0.0 for new networks. #[pallet::storage] + #[pallet::unbounded] pub(crate) type StorageVersion = StorageValue<_, Releases, ValueQuery>; /// The threshold for when users can start calling `chill_other` for other validators / From 9ac3c4739c5d78f9262f8007c64101936faca30f Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Wed, 7 Sep 2022 18:44:21 +0200 Subject: [PATCH 02/36] bound structs --- frame/staking/src/lib.rs | 14 +++++++------- frame/staking/src/pallet/mod.rs | 11 ++--------- frame/staking/src/slashing.rs | 4 ++-- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 4ff9e99306727..218b4cadd9fb7 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -299,7 +299,7 @@ pub mod weights; mod pallet; -use codec::{Decode, Encode, HasCompact}; +use codec::{Decode, Encode, HasCompact, MaxEncodedLen}; use frame_support::{ parameter_types, traits::{Currency, Defensive, Get}, @@ -354,7 +354,7 @@ parameter_types! { } /// Information regarding the active era (era in used in session). -#[derive(Encode, Decode, RuntimeDebug, TypeInfo)] +#[derive(Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)] pub struct ActiveEraInfo { /// Index of era. pub index: EraIndex, @@ -395,7 +395,7 @@ pub enum StakerStatus { } /// A destination account for payment. -#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] +#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)] pub enum RewardDestination { /// Pay into the stash account, increasing the amount at stake accordingly. Staked, @@ -416,7 +416,7 @@ impl Default for RewardDestination { } /// Preference of what happens regarding validation. -#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)] +#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default, MaxEncodedLen)] pub struct ValidatorPrefs { /// Reward that validator takes up-front; only the rest is split between themselves and /// nominators. @@ -676,7 +676,7 @@ impl StakingLedger { } /// A record of the nominations made by a specific account. -#[derive(PartialEqNoBound, EqNoBound, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo)] +#[derive(PartialEqNoBound, EqNoBound, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)] #[codec(mel_bound())] #[scale_info(skip_type_params(T))] pub struct Nominations { @@ -850,7 +850,7 @@ impl = StorageMap<_, Twox64Concat, T::AccountId, RewardDestination, ValueQuery>; /// The map from (wannabe) validator stash key to the preferences of that validator. #[pallet::storage] #[pallet::getter(fn validators)] - #[pallet::unbounded] pub type Validators = CountedStorageMap<_, Twox64Concat, T::AccountId, ValidatorPrefs, ValueQuery>; @@ -326,7 +325,6 @@ pub mod pallet { /// [`Call::chill_other`] dispatchable by anyone. #[pallet::storage] #[pallet::getter(fn nominators)] - #[pallet::unbounded] pub type Nominators = CountedStorageMap<_, Twox64Concat, T::AccountId, Nominations>; @@ -350,7 +348,6 @@ pub mod pallet { /// equal to [`SessionInterface::validators`]. #[pallet::storage] #[pallet::getter(fn active_era)] - #[pallet::unbounded] pub type ActiveEra = StorageValue<_, ActiveEraInfo>; /// The session index at which the era start for the last `HISTORY_DEPTH` eras. @@ -411,7 +408,6 @@ pub mod pallet { /// Is it removed after `HISTORY_DEPTH` eras. // If prefs hasn't been set or has been removed then 0 commission is returned. #[pallet::storage] - #[pallet::unbounded] #[pallet::getter(fn eras_validator_prefs)] pub type ErasValidatorPrefs = StorageDoubleMap< _, @@ -433,8 +429,8 @@ pub mod pallet { /// Rewards for the last `HISTORY_DEPTH` eras. /// If reward hasn't been set or has been removed then 0 reward is returned. #[pallet::storage] - #[pallet::getter(fn eras_reward_points)] #[pallet::unbounded] + #[pallet::getter(fn eras_reward_points)] pub type ErasRewardPoints = StorageMap<_, Twox64Concat, EraIndex, EraRewardPoints, ValueQuery>; @@ -447,7 +443,6 @@ pub mod pallet { /// Mode of era forcing. #[pallet::storage] - #[pallet::unbounded] #[pallet::getter(fn force_era)] pub type ForceEra = StorageValue<_, Forcing, ValueQuery>; @@ -510,7 +505,6 @@ pub mod pallet { /// Records information about the maximum slash of a stash within a slashing span, /// as well as how much reward has been paid out. #[pallet::storage] - #[pallet::unbounded] pub(crate) type SpanSlash = StorageMap< _, Twox64Concat, @@ -545,7 +539,6 @@ pub mod pallet { /// /// This is set to v7.0.0 for new networks. #[pallet::storage] - #[pallet::unbounded] pub(crate) type StorageVersion = StorageValue<_, Releases, ValueQuery>; /// The threshold for when users can start calling `chill_other` for other validators / diff --git a/frame/staking/src/slashing.rs b/frame/staking/src/slashing.rs index 7372c4390f816..f3272a25fab5c 100644 --- a/frame/staking/src/slashing.rs +++ b/frame/staking/src/slashing.rs @@ -53,7 +53,7 @@ use crate::{ BalanceOf, Config, Error, Exposure, NegativeImbalanceOf, Pallet, Perbill, SessionInterface, Store, UnappliedSlash, }; -use codec::{Decode, Encode}; +use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::{ ensure, traits::{Currency, Defensive, Get, Imbalance, OnUnbalanced}, @@ -182,7 +182,7 @@ impl SlashingSpans { } /// A slashing-span record for a particular stash. -#[derive(Encode, Decode, Default, TypeInfo)] +#[derive(Encode, Decode, Default, TypeInfo, MaxEncodedLen)] pub(crate) struct SpanRecord { slashed: Balance, paid_out: Balance, From 9321b61be32edb38020e704eba1e6a54cafc5b58 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Thu, 8 Sep 2022 10:57:31 +0200 Subject: [PATCH 03/36] bounding history depth --- bin/node/runtime/src/lib.rs | 2 + frame/staking/src/benchmarking.rs | 2 +- frame/staking/src/lib.rs | 11 +-- frame/staking/src/mock.rs | 2 + frame/staking/src/pallet/impls.rs | 16 +++-- frame/staking/src/pallet/mod.rs | 13 +++- frame/staking/src/tests.rs | 112 +++++++++++++++--------------- 7 files changed, 87 insertions(+), 71 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index b5467aa6f8ea7..c620d92b7b589 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -535,6 +535,7 @@ parameter_types! { pub const MaxNominatorRewardedPerValidator: u32 = 256; pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17); pub OffchainRepeat: BlockNumber = 5; + pub HistoryDepth: u16 = 84; } pub struct StakingBenchmarkingConfig; @@ -572,6 +573,7 @@ impl pallet_staking::Config for Runtime { // This a placeholder, to be introduced in the next PR as an instance of bags-list type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; + type HistoryDepth = HistoryDepth; type OnStakerSlash = NominationPools; type WeightInfo = pallet_staking::weights::SubstrateWeight; type BenchmarkingConfig = StakingBenchmarkingConfig; diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index a79eac3c4dc46..05f2221ec026f 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -698,7 +698,7 @@ benchmarks! { active: T::Currency::minimum_balance() - One::one(), total: T::Currency::minimum_balance() - One::one(), unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: Default::default(), }; Ledger::::insert(&controller, l); diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 218b4cadd9fb7..cc7fe85c6e21f 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -351,6 +351,7 @@ type AccountIdLookupOf = <::Lookup as StaticLookup parameter_types! { pub MaxUnlockingChunks: u32 = 32; + pub HistoryDepth: u32 = 84; } /// Information regarding the active era (era in used in session). @@ -429,8 +430,8 @@ pub struct ValidatorPrefs { } /// Just a Balance/BlockNumber tuple to encode when a chunk of funds will be unlocked. -#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] -pub struct UnlockChunk { +#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)] +pub struct UnlockChunk { /// Amount of funds to be unlocked. #[codec(compact)] value: Balance, @@ -440,7 +441,7 @@ pub struct UnlockChunk { } /// The ledger of a (bonded) stash. -#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo)] +#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)] #[scale_info(skip_type_params(T))] pub struct StakingLedger { /// The stash account whose balance is actually locked and at stake. @@ -459,7 +460,7 @@ pub struct StakingLedger { pub unlocking: BoundedVec>, MaxUnlockingChunks>, /// List of eras for which the stakers behind a validator have claimed rewards. Only updated /// for validators. - pub claimed_rewards: Vec, + pub claimed_rewards: BoundedVec, } impl StakingLedger { @@ -470,7 +471,7 @@ impl StakingLedger { total: Zero::zero(), active: Zero::zero(), unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: Default::default(), } } diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 5980e6b40d15d..86883ba2c0282 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -236,6 +236,7 @@ const THRESHOLDS: [sp_npos_elections::VoteWeight; 9] = parameter_types! { pub static BagThresholds: &'static [sp_npos_elections::VoteWeight] = &THRESHOLDS; pub static MaxNominations: u32 = 16; + pub static HistoryDepth: u32 = 84; pub static RewardOnUnbalanceWasCalled: bool = false; pub static LedgerSlashPerEra: (BalanceOf, BTreeMap>) = (Zero::zero(), BTreeMap::new()); } @@ -301,6 +302,7 @@ impl crate::pallet::pallet::Config for Test { type VoterList = VoterBagsList; type TargetList = UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; + type HistoryDepth = HistoryDepth; type OnStakerSlash = OnStakerSlashMock; type BenchmarkingConfig = TestBenchmarkingConfig; type WeightInfo = (); diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 62faaaf68ef62..4b6d8b36a21ee 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -26,7 +26,7 @@ use frame_support::{ pallet_prelude::*, traits::{ Currency, CurrencyToVote, Defensive, EstimateNextNewSession, Get, Imbalance, - LockableCurrency, OnUnbalanced, UnixTime, WithdrawReasons, + LockableCurrency, OnUnbalanced, UnixTime, WithdrawReasons, DefensiveResult, }, weights::Weight, }; @@ -119,15 +119,17 @@ impl Pallet { Error::::NotStash.with_weight(T::WeightInfo::payout_stakers_alive_staked(0)) })?; let mut ledger = >::get(&controller).ok_or(Error::::NotController)?; - + ledger .claimed_rewards .retain(|&x| x >= current_era.saturating_sub(history_depth)); + match ledger.claimed_rewards.binary_search(&era) { Ok(_) => return Err(Error::::AlreadyClaimed .with_weight(T::WeightInfo::payout_stakers_alive_staked(0))), - Err(pos) => ledger.claimed_rewards.insert(pos, era), + Err(pos) => ledger.claimed_rewards.try_insert(pos, era) + .defensive_map_err(|_| Error::::BoundNotMet)?, } let exposure = >::get(&era, &ledger.stash); @@ -966,7 +968,7 @@ impl ElectionDataProvider for Pallet { active: stake, total: stake, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: Default::default(), }, ); @@ -984,7 +986,7 @@ impl ElectionDataProvider for Pallet { active: stake, total: stake, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: Default::default(), }, ); Self::do_add_validator( @@ -1025,7 +1027,7 @@ impl ElectionDataProvider for Pallet { active: stake, total: stake, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: Default::default(), }, ); Self::do_add_validator( @@ -1046,7 +1048,7 @@ impl ElectionDataProvider for Pallet { active: stake, total: stake, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: Default::default(), }, ); Self::do_add_nominator( diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index a2d26b2c915a3..f2d962f02d520 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -24,6 +24,7 @@ use frame_support::{ traits::{ Currency, CurrencyToVote, Defensive, DefensiveSaturating, EnsureOrigin, EstimateNextNewSession, Get, LockIdentifier, LockableCurrency, OnUnbalanced, UnixTime, + TryCollect, }, weights::Weight, BoundedVec, @@ -124,6 +125,11 @@ pub mod pallet { #[pallet::constant] type MaxNominations: Get; + /// Maximum History Depth for the validator rewards to be claimed. + /// This should never be decreased once set. + #[pallet::constant] + type HistoryDepth: Get; + /// Tokens have been minted and are unused for validator-reward. /// See [Era payout](./index.html#era-payout). type RewardRemainder: OnUnbalanced>; @@ -286,7 +292,6 @@ pub mod pallet { /// Map from all (unlocked) "controller" accounts to the info regarding the staking. #[pallet::storage] #[pallet::getter(fn ledger)] - #[pallet::unbounded] pub type Ledger = StorageMap<_, Blake2_128Concat, T::AccountId, StakingLedger>; /// Where the reward payment should be made. Keyed by stash. @@ -738,6 +743,8 @@ pub mod pallet { TooManyValidators, /// Commission is too low. Must be at least `MinCommission`. CommissionTooLow, + /// Some bound is not met. + BoundNotMet, } #[pallet::hooks] @@ -850,7 +857,9 @@ pub mod pallet { total: value, active: value, unlocking: Default::default(), - claimed_rewards: (last_reward_era..current_era).collect(), + claimed_rewards: (last_reward_era..current_era) + .try_collect() + .map_err(|_| Error::::BoundNotMet)?, }; Self::update_ledger(&controller, &item); Ok(()) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 85badfac769af..ec9da1feea3b3 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -148,14 +148,14 @@ fn basic_setup_works() { // Account 10 controls the stash from account 11, which is 100 * balance_factor units assert_eq!( - Staking::ledger(&10), - Some(StakingLedger { + Staking::ledger(&10).unwrap(), + StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![] - }) + claimed_rewards: bounded_vec![], + } ); // Account 20 controls the stash from account 21, which is 200 * balance_factor units assert_eq!( @@ -165,7 +165,7 @@ fn basic_setup_works() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], }) ); // Account 1 does not control any stash @@ -188,7 +188,7 @@ fn basic_setup_works() { total: 500, active: 500, unlocking: Default::default(), - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], }) ); assert_eq!(Staking::nominators(101).unwrap().targets, vec![11, 21]); @@ -429,7 +429,7 @@ fn staking_should_work() { total: 1500, active: 1500, unlocking: Default::default(), - claimed_rewards: vec![0], + claimed_rewards: bounded_vec![0], }) ); // e.g. it cannot reserve more than 500 that it has free from the total 2000 @@ -983,7 +983,7 @@ fn reward_destination_works() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1006,7 +1006,7 @@ fn reward_destination_works() { total: 1000 + total_payout_0, active: 1000 + total_payout_0, unlocking: Default::default(), - claimed_rewards: vec![0], + claimed_rewards: bounded_vec![0], }) ); @@ -1034,7 +1034,7 @@ fn reward_destination_works() { total: 1000 + total_payout_0, active: 1000 + total_payout_0, unlocking: Default::default(), - claimed_rewards: vec![0, 1], + claimed_rewards: bounded_vec![0, 1], }) ); @@ -1063,7 +1063,7 @@ fn reward_destination_works() { total: 1000 + total_payout_0, active: 1000 + total_payout_0, unlocking: Default::default(), - claimed_rewards: vec![0, 1, 2], + claimed_rewards: bounded_vec![0, 1, 2], }) ); // Check that amount in staked account is NOT increased. @@ -1125,7 +1125,7 @@ fn bond_extra_works() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1142,7 +1142,7 @@ fn bond_extra_works() { total: 1000 + 100, active: 1000 + 100, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1156,7 +1156,7 @@ fn bond_extra_works() { total: 1000000, active: 1000000, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); }); @@ -1194,7 +1194,7 @@ fn bond_extra_and_withdraw_unbonded_works() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); assert_eq!( @@ -1212,7 +1212,7 @@ fn bond_extra_and_withdraw_unbonded_works() { total: 1000 + 100, active: 1000 + 100, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); // Exposure is a snapshot! only updated after the next era update. @@ -1233,7 +1233,7 @@ fn bond_extra_and_withdraw_unbonded_works() { total: 1000 + 100, active: 1000 + 100, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); // Exposure is now updated. @@ -1251,7 +1251,7 @@ fn bond_extra_and_withdraw_unbonded_works() { total: 1000 + 100, active: 100, unlocking: bounded_vec![UnlockChunk { value: 1000, era: 2 + 3 }], - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], }), ); @@ -1264,7 +1264,7 @@ fn bond_extra_and_withdraw_unbonded_works() { total: 1000 + 100, active: 100, unlocking: bounded_vec![UnlockChunk { value: 1000, era: 2 + 3 }], - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], }), ); @@ -1280,7 +1280,7 @@ fn bond_extra_and_withdraw_unbonded_works() { total: 1000 + 100, active: 100, unlocking: bounded_vec![UnlockChunk { value: 1000, era: 2 + 3 }], - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], }), ); @@ -1296,7 +1296,7 @@ fn bond_extra_and_withdraw_unbonded_works() { total: 100, active: 100, unlocking: Default::default(), - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], }), ); }) @@ -1366,7 +1366,7 @@ fn rebond_works() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1385,7 +1385,7 @@ fn rebond_works() { total: 1000, active: 100, unlocking: bounded_vec![UnlockChunk { value: 900, era: 2 + 3 }], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1398,7 +1398,7 @@ fn rebond_works() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1411,7 +1411,7 @@ fn rebond_works() { total: 1000, active: 100, unlocking: bounded_vec![UnlockChunk { value: 900, era: 5 }], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1424,7 +1424,7 @@ fn rebond_works() { total: 1000, active: 600, unlocking: bounded_vec![UnlockChunk { value: 400, era: 5 }], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1437,7 +1437,7 @@ fn rebond_works() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1452,7 +1452,7 @@ fn rebond_works() { total: 1000, active: 100, unlocking: bounded_vec![UnlockChunk { value: 900, era: 5 }], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1465,7 +1465,7 @@ fn rebond_works() { total: 1000, active: 600, unlocking: bounded_vec![UnlockChunk { value: 400, era: 5 }], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); }) @@ -1492,7 +1492,7 @@ fn rebond_is_fifo() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1507,7 +1507,7 @@ fn rebond_is_fifo() { total: 1000, active: 600, unlocking: bounded_vec![UnlockChunk { value: 400, era: 2 + 3 }], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1525,7 +1525,7 @@ fn rebond_is_fifo() { UnlockChunk { value: 400, era: 2 + 3 }, UnlockChunk { value: 300, era: 3 + 3 }, ], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1544,7 +1544,7 @@ fn rebond_is_fifo() { UnlockChunk { value: 300, era: 3 + 3 }, UnlockChunk { value: 200, era: 4 + 3 }, ], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1560,7 +1560,7 @@ fn rebond_is_fifo() { UnlockChunk { value: 400, era: 2 + 3 }, UnlockChunk { value: 100, era: 3 + 3 }, ], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); }) @@ -1589,7 +1589,7 @@ fn rebond_emits_right_value_in_event() { total: 1000, active: 100, unlocking: bounded_vec![UnlockChunk { value: 900, era: 1 + 3 }], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -1602,7 +1602,7 @@ fn rebond_emits_right_value_in_event() { total: 1000, active: 200, unlocking: bounded_vec![UnlockChunk { value: 800, era: 1 + 3 }], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); // Event emitted should be correct @@ -1617,7 +1617,7 @@ fn rebond_emits_right_value_in_event() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); // Event emitted should be correct, only 800 @@ -1653,7 +1653,7 @@ fn reward_to_stake_works() { total: 69, active: 69, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }, ); @@ -1714,7 +1714,7 @@ fn reap_stash_works() { total: 5, active: 5, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }, ); @@ -1833,7 +1833,7 @@ fn bond_with_no_staked_value() { active: 0, total: 5, unlocking: bounded_vec![UnlockChunk { value: 5, era: 3 }], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); @@ -2903,7 +2903,7 @@ fn staker_cannot_bail_deferred_slash() { active: 0, total: 500, stash: 101, - claimed_rewards: Default::default(), + claimed_rewards: bounded_vec![], unlocking: bounded_vec![UnlockChunk { era: 4u32, value: 500 }], } ); @@ -3597,7 +3597,7 @@ fn test_payout_stakers() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![1] + claimed_rewards: bounded_vec![1] }) ); @@ -3628,7 +3628,7 @@ fn test_payout_stakers() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: (1..=14).collect() + claimed_rewards: (1..=14).collect::>().try_into().unwrap() }) ); @@ -3649,7 +3649,7 @@ fn test_payout_stakers() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![15, 98] + claimed_rewards: bounded_vec![15, 98] }) ); @@ -3664,7 +3664,7 @@ fn test_payout_stakers() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![15, 23, 42, 69, 98] + claimed_rewards: bounded_vec![15, 23, 42, 69, 98] }) ); }); @@ -3855,7 +3855,7 @@ fn bond_during_era_correctly_populates_claimed_rewards() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }) ); mock::start_active_era(5); @@ -3867,7 +3867,7 @@ fn bond_during_era_correctly_populates_claimed_rewards() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: (0..5).collect(), + claimed_rewards: (0..5).collect::>().try_into().unwrap(), }) ); mock::start_active_era(99); @@ -3879,7 +3879,7 @@ fn bond_during_era_correctly_populates_claimed_rewards() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: (15..99).collect(), + claimed_rewards: (15..99).collect::>().try_into().unwrap(), }) ); }); @@ -4124,7 +4124,7 @@ fn cannot_rebond_to_lower_than_ed() { total: 10 * 1000, active: 10 * 1000, unlocking: Default::default(), - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], } ); @@ -4138,7 +4138,7 @@ fn cannot_rebond_to_lower_than_ed() { total: 10 * 1000, active: 0, unlocking: bounded_vec![UnlockChunk { value: 10 * 1000, era: 3 }], - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], } ); @@ -4161,7 +4161,7 @@ fn cannot_bond_extra_to_lower_than_ed() { total: 10 * 1000, active: 10 * 1000, unlocking: Default::default(), - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], } ); @@ -4175,7 +4175,7 @@ fn cannot_bond_extra_to_lower_than_ed() { total: 10 * 1000, active: 0, unlocking: bounded_vec![UnlockChunk { value: 10 * 1000, era: 3 }], - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], } ); @@ -4202,7 +4202,7 @@ fn do_not_die_when_active_is_ed() { total: 1000 * ed, active: 1000 * ed, unlocking: Default::default(), - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], } ); @@ -4219,7 +4219,7 @@ fn do_not_die_when_active_is_ed() { total: ed, active: ed, unlocking: Default::default(), - claimed_rewards: vec![] + claimed_rewards: bounded_vec![], } ); }) @@ -5053,7 +5053,7 @@ fn proportional_slash_stop_slashing_if_remaining_zero() { active: 20, // we have some chunks, but they are not affected. unlocking: bounded_vec![c(1, 10), c(2, 10)], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }; assert_eq!(BondingDuration::get(), 3); @@ -5071,7 +5071,7 @@ fn proportional_ledger_slash_works() { total: 10, active: 10, unlocking: bounded_vec![], - claimed_rewards: vec![], + claimed_rewards: bounded_vec![], }; assert_eq!(BondingDuration::get(), 3); From b816866f117fce2927eaedda4457e588f67687ca Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Fri, 9 Sep 2022 09:15:30 +0200 Subject: [PATCH 04/36] defensive error --- frame/staking/src/pallet/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index f2d962f02d520..3d090ad05ea7c 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -24,7 +24,7 @@ use frame_support::{ traits::{ Currency, CurrencyToVote, Defensive, DefensiveSaturating, EnsureOrigin, EstimateNextNewSession, Get, LockIdentifier, LockableCurrency, OnUnbalanced, UnixTime, - TryCollect, + TryCollect, DefensiveResult }, weights::Weight, BoundedVec, @@ -859,7 +859,7 @@ pub mod pallet { unlocking: Default::default(), claimed_rewards: (last_reward_era..current_era) .try_collect() - .map_err(|_| Error::::BoundNotMet)?, + .defensive_map_err(|_| Error::::BoundNotMet)?, }; Self::update_ledger(&controller, &item); Ok(()) From c55de4fd7cd357c9d826fcd8acd2ba32c93a4683 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Fri, 9 Sep 2022 11:45:54 +0200 Subject: [PATCH 05/36] use the era history depth from config --- frame/staking/src/lib.rs | 5 ++--- frame/staking/src/mock.rs | 4 ++-- frame/staking/src/pallet/mod.rs | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index cc7fe85c6e21f..c7395515f0bcf 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -351,7 +351,6 @@ type AccountIdLookupOf = <::Lookup as StaticLookup parameter_types! { pub MaxUnlockingChunks: u32 = 32; - pub HistoryDepth: u32 = 84; } /// Information regarding the active era (era in used in session). @@ -441,7 +440,7 @@ pub struct UnlockChunk { } /// The ledger of a (bonded) stash. -#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)] +#[derive(PartialEqNoBound, EqNoBound, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)] #[scale_info(skip_type_params(T))] pub struct StakingLedger { /// The stash account whose balance is actually locked and at stake. @@ -460,7 +459,7 @@ pub struct StakingLedger { pub unlocking: BoundedVec>, MaxUnlockingChunks>, /// List of eras for which the stakers behind a validator have claimed rewards. Only updated /// for validators. - pub claimed_rewards: BoundedVec, + pub claimed_rewards: BoundedVec, } impl StakingLedger { diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 86883ba2c0282..a3b5ecc084fc1 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -236,7 +236,7 @@ const THRESHOLDS: [sp_npos_elections::VoteWeight; 9] = parameter_types! { pub static BagThresholds: &'static [sp_npos_elections::VoteWeight] = &THRESHOLDS; pub static MaxNominations: u32 = 16; - pub static HistoryDepth: u32 = 84; + pub static EraHistoryDepth: u32 = 84; pub static RewardOnUnbalanceWasCalled: bool = false; pub static LedgerSlashPerEra: (BalanceOf, BTreeMap>) = (Zero::zero(), BTreeMap::new()); } @@ -302,7 +302,7 @@ impl crate::pallet::pallet::Config for Test { type VoterList = VoterBagsList; type TargetList = UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; - type HistoryDepth = HistoryDepth; + type EraHistoryDepth = EraHistoryDepth; type OnStakerSlash = OnStakerSlashMock; type BenchmarkingConfig = TestBenchmarkingConfig; type WeightInfo = (); diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 3d090ad05ea7c..934ded4a781ee 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -128,7 +128,7 @@ pub mod pallet { /// Maximum History Depth for the validator rewards to be claimed. /// This should never be decreased once set. #[pallet::constant] - type HistoryDepth: Get; + type EraHistoryDepth: Get; /// Tokens have been minted and are unused for validator-reward. /// See [Era payout](./index.html#era-payout). From 8cf856b8fe4b9286f6d17a660f4ffb59c2bbd580 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Fri, 9 Sep 2022 17:48:22 +0200 Subject: [PATCH 06/36] clean up history depth from storage in v11 --- frame/staking/src/pallet/impls.rs | 5 +++ frame/staking/src/pallet/mod.rs | 65 +------------------------------ frame/staking/src/tests.rs | 19 --------- frame/staking/src/weights.rs | 35 ----------------- 4 files changed, 7 insertions(+), 117 deletions(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 4b6d8b36a21ee..680463305ccbe 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -887,6 +887,11 @@ impl Pallet { DispatchClass::Mandatory, ); } + + /// This will return the currently configured History Depth + pub fn history_depth() -> u32 { + T::EraHistoryDepth::get() + } } impl ElectionDataProvider for Pallet { diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 934ded4a781ee..38e5bfe76ab4e 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -125,7 +125,7 @@ pub mod pallet { #[pallet::constant] type MaxNominations: Get; - /// Maximum History Depth for the validator rewards to be claimed. + /// Era History Depth for the validator rewards to be claimed. /// This should never be decreased once set. #[pallet::constant] type EraHistoryDepth: Get; @@ -236,22 +236,6 @@ pub mod pallet { type WeightInfo: WeightInfo; } - #[pallet::type_value] - pub(crate) fn HistoryDepthOnEmpty() -> u32 { - 84u32 - } - - /// Number of eras to keep in history. - /// - /// Information is kept for eras in `[current_era - history_depth; current_era]`. - /// - /// Must be more than the number of eras delayed by session otherwise. I.e. active era must - /// always be in history. I.e. `active_era > current_era - history_depth` must be - /// guaranteed. - #[pallet::storage] - #[pallet::getter(fn history_depth)] - pub(crate) type HistoryDepth = StorageValue<_, u32, ValueQuery, HistoryDepthOnEmpty>; - /// The ideal number of staking participants. #[pallet::storage] #[pallet::getter(fn validator_count)] @@ -554,7 +538,6 @@ pub mod pallet { #[pallet::genesis_config] pub struct GenesisConfig { - pub history_depth: u32, pub validator_count: u32, pub minimum_validator_count: u32, pub invulnerables: Vec, @@ -573,7 +556,6 @@ pub mod pallet { impl Default for GenesisConfig { fn default() -> Self { GenesisConfig { - history_depth: 84u32, validator_count: Default::default(), minimum_validator_count: Default::default(), invulnerables: Default::default(), @@ -592,7 +574,6 @@ pub mod pallet { #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { fn build(&self) { - HistoryDepth::::put(self.history_depth); ValidatorCount::::put(self.validator_count); MinimumValidatorCount::::put(self.minimum_validator_count); Invulnerables::::put(&self.invulnerables); @@ -846,7 +827,7 @@ pub mod pallet { >::insert(&stash, payee); let current_era = CurrentEra::::get().unwrap_or(0); - let history_depth = Self::history_depth(); + let history_depth = T::EraHistoryDepth::get(); let last_reward_era = current_era.saturating_sub(history_depth); let stash_balance = T::Currency::free_balance(&stash); @@ -1484,48 +1465,6 @@ pub mod pallet { Ok(Some(T::WeightInfo::rebond(removed_chunks)).into()) } - /// Set `HistoryDepth` value. This function will delete any history information - /// when `HistoryDepth` is reduced. - /// - /// Parameters: - /// - `new_history_depth`: The new history depth you would like to set. - /// - `era_items_deleted`: The number of items that will be deleted by this dispatch. This - /// should report all the storage items that will be deleted by clearing old era history. - /// Needed to report an accurate weight for the dispatch. Trusted by `Root` to report an - /// accurate number. - /// - /// Origin must be root. - /// - /// # - /// - E: Number of history depths removed, i.e. 10 -> 7 = 3 - /// - Weight: O(E) - /// - DB Weight: - /// - Reads: Current Era, History Depth - /// - Writes: History Depth - /// - Clear Prefix Each: Era Stakers, EraStakersClipped, ErasValidatorPrefs - /// - Writes Each: ErasValidatorReward, ErasRewardPoints, ErasTotalStake, - /// ErasStartSessionIndex - /// # - #[pallet::weight(T::WeightInfo::set_history_depth(*_era_items_deleted))] - pub fn set_history_depth( - origin: OriginFor, - #[pallet::compact] new_history_depth: EraIndex, - #[pallet::compact] _era_items_deleted: u32, - ) -> DispatchResult { - ensure_root(origin)?; - if let Some(current_era) = Self::current_era() { - HistoryDepth::::mutate(|history_depth| { - let last_kept = current_era.saturating_sub(*history_depth); - let new_last_kept = current_era.saturating_sub(new_history_depth); - for era_index in last_kept..new_last_kept { - Self::clear_era_information(era_index); - } - *history_depth = new_history_depth - }) - } - Ok(()) - } - /// Remove all data structures concerning a staker/stash once it is at a state where it can /// be considered `dust` in the staking system. The requirements are: /// diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index ec9da1feea3b3..d4f00f0b83abf 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -3515,25 +3515,6 @@ fn test_max_nominator_rewarded_per_validator_and_cant_steal_someone_else_reward( }); } -#[test] -fn set_history_depth_works() { - ExtBuilder::default().build_and_execute(|| { - mock::start_active_era(10); - Staking::set_history_depth(Origin::root(), 20, 0).unwrap(); - assert!(::ErasTotalStake::contains_key(10 - 4)); - assert!(::ErasTotalStake::contains_key(10 - 5)); - Staking::set_history_depth(Origin::root(), 4, 0).unwrap(); - assert!(::ErasTotalStake::contains_key(10 - 4)); - assert!(!::ErasTotalStake::contains_key(10 - 5)); - Staking::set_history_depth(Origin::root(), 3, 0).unwrap(); - assert!(!::ErasTotalStake::contains_key(10 - 4)); - assert!(!::ErasTotalStake::contains_key(10 - 5)); - Staking::set_history_depth(Origin::root(), 8, 0).unwrap(); - assert!(!::ErasTotalStake::contains_key(10 - 4)); - assert!(!::ErasTotalStake::contains_key(10 - 5)); - }); -} - #[test] fn test_payout_stakers() { // Test that payout_stakers work in general, including that only the top diff --git a/frame/staking/src/weights.rs b/frame/staking/src/weights.rs index 09c2d8b5de113..a5dd6bcd1e8b7 100644 --- a/frame/staking/src/weights.rs +++ b/frame/staking/src/weights.rs @@ -65,7 +65,6 @@ pub trait WeightInfo { fn payout_stakers_dead_controller(n: u32, ) -> Weight; fn payout_stakers_alive_staked(n: u32, ) -> Weight; fn rebond(l: u32, ) -> Weight; - fn set_history_depth(e: u32, ) -> Weight; fn reap_stash(s: u32, ) -> Weight; fn new_era(v: u32, n: u32, ) -> Weight; fn get_npos_voters(v: u32, n: u32, s: u32, ) -> Weight; @@ -322,23 +321,6 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(9 as u64)) .saturating_add(T::DbWeight::get().writes(8 as u64)) } - // Storage: Staking CurrentEra (r:1 w:0) - // Storage: Staking HistoryDepth (r:1 w:1) - // Storage: Staking ErasStakersClipped (r:0 w:2) - // Storage: Staking ErasValidatorPrefs (r:0 w:2) - // Storage: Staking ErasValidatorReward (r:0 w:1) - // Storage: Staking ErasRewardPoints (r:0 w:1) - // Storage: Staking ErasStakers (r:0 w:2) - // Storage: Staking ErasTotalStake (r:0 w:1) - // Storage: Staking ErasStartSessionIndex (r:0 w:1) - fn set_history_depth(e: u32, ) -> Weight { - Weight::from_ref_time(0 as u64) - // Standard Error: 62_000 - .saturating_add(Weight::from_ref_time(22_829_000 as u64).saturating_mul(e as u64)) - .saturating_add(T::DbWeight::get().reads(2 as u64)) - .saturating_add(T::DbWeight::get().writes(4 as u64)) - .saturating_add(T::DbWeight::get().writes((7 as u64).saturating_mul(e as u64))) - } // Storage: System Account (r:1 w:1) // Storage: Staking Bonded (r:1 w:1) // Storage: Staking Ledger (r:1 w:1) @@ -708,23 +690,6 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(9 as u64)) .saturating_add(RocksDbWeight::get().writes(8 as u64)) } - // Storage: Staking CurrentEra (r:1 w:0) - // Storage: Staking HistoryDepth (r:1 w:1) - // Storage: Staking ErasStakersClipped (r:0 w:2) - // Storage: Staking ErasValidatorPrefs (r:0 w:2) - // Storage: Staking ErasValidatorReward (r:0 w:1) - // Storage: Staking ErasRewardPoints (r:0 w:1) - // Storage: Staking ErasStakers (r:0 w:2) - // Storage: Staking ErasTotalStake (r:0 w:1) - // Storage: Staking ErasStartSessionIndex (r:0 w:1) - fn set_history_depth(e: u32, ) -> Weight { - Weight::from_ref_time(0 as u64) - // Standard Error: 62_000 - .saturating_add(Weight::from_ref_time(22_829_000 as u64).saturating_mul(e as u64)) - .saturating_add(RocksDbWeight::get().reads(2 as u64)) - .saturating_add(RocksDbWeight::get().writes(4 as u64)) - .saturating_add(RocksDbWeight::get().writes((7 as u64).saturating_mul(e as u64))) - } // Storage: System Account (r:1 w:1) // Storage: Staking Bonded (r:1 w:1) // Storage: Staking Ledger (r:1 w:1) From 590472459b7f702de7c400b3bc92901a1bf459d1 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Fri, 9 Sep 2022 17:55:38 +0200 Subject: [PATCH 07/36] keep the name HistoryDepth for the new configuration value --- frame/staking/src/lib.rs | 2 +- frame/staking/src/mock.rs | 4 ++-- frame/staking/src/pallet/impls.rs | 6 +++++- frame/staking/src/pallet/mod.rs | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index c7395515f0bcf..11b3733b57f87 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -459,7 +459,7 @@ pub struct StakingLedger { pub unlocking: BoundedVec>, MaxUnlockingChunks>, /// List of eras for which the stakers behind a validator have claimed rewards. Only updated /// for validators. - pub claimed_rewards: BoundedVec, + pub claimed_rewards: BoundedVec, } impl StakingLedger { diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index a3b5ecc084fc1..86883ba2c0282 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -236,7 +236,7 @@ const THRESHOLDS: [sp_npos_elections::VoteWeight; 9] = parameter_types! { pub static BagThresholds: &'static [sp_npos_elections::VoteWeight] = &THRESHOLDS; pub static MaxNominations: u32 = 16; - pub static EraHistoryDepth: u32 = 84; + pub static HistoryDepth: u32 = 84; pub static RewardOnUnbalanceWasCalled: bool = false; pub static LedgerSlashPerEra: (BalanceOf, BTreeMap>) = (Zero::zero(), BTreeMap::new()); } @@ -302,7 +302,7 @@ impl crate::pallet::pallet::Config for Test { type VoterList = VoterBagsList; type TargetList = UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; - type EraHistoryDepth = EraHistoryDepth; + type HistoryDepth = HistoryDepth; type OnStakerSlash = OnStakerSlashMock; type BenchmarkingConfig = TestBenchmarkingConfig; type WeightInfo = (); diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 680463305ccbe..e315e4f76e319 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -889,8 +889,12 @@ impl Pallet { } /// This will return the currently configured History Depth + /// + /// With release of v11, history_depth is migrated to a configurable + /// value instead of being a storage item + /// This function replaces the old fn history_depth which read from storage pub fn history_depth() -> u32 { - T::EraHistoryDepth::get() + T::HistoryDepth::get() } } diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 38e5bfe76ab4e..d52d4a0079a7e 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -128,7 +128,7 @@ pub mod pallet { /// Era History Depth for the validator rewards to be claimed. /// This should never be decreased once set. #[pallet::constant] - type EraHistoryDepth: Get; + type HistoryDepth: Get; /// Tokens have been minted and are unused for validator-reward. /// See [Era payout](./index.html#era-payout). @@ -827,7 +827,7 @@ pub mod pallet { >::insert(&stash, payee); let current_era = CurrentEra::::get().unwrap_or(0); - let history_depth = T::EraHistoryDepth::get(); + let history_depth = T::HistoryDepth::get(); let last_reward_era = current_era.saturating_sub(history_depth); let stash_balance = T::Currency::free_balance(&stash); From 477e122b491996f1fea4733831610404e8ab6e48 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Fri, 9 Sep 2022 18:20:34 +0200 Subject: [PATCH 08/36] use u32 for history depth in node runtime --- bin/node/runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index c620d92b7b589..5ccedced3c400 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -535,7 +535,7 @@ parameter_types! { pub const MaxNominatorRewardedPerValidator: u32 = 256; pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17); pub OffchainRepeat: BlockNumber = 5; - pub HistoryDepth: u16 = 84; + pub HistoryDepth: u32 = 84; } pub struct StakingBenchmarkingConfig; From 49682d9abbdb9cedcbb3ffc88c883d67cc55cfb0 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Mon, 12 Sep 2022 10:21:29 +0200 Subject: [PATCH 09/36] improve doc comments --- frame/staking/src/pallet/impls.rs | 10 ++++++---- frame/staking/src/pallet/mod.rs | 13 +++++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index e315e4f76e319..984d35938dfa7 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -129,6 +129,8 @@ impl Pallet { return Err(Error::::AlreadyClaimed .with_weight(T::WeightInfo::payout_stakers_alive_staked(0))), Err(pos) => ledger.claimed_rewards.try_insert(pos, era) + // Since we retain era entries in `claimed_rewards` only upto + // `HistoryDepth`, following bound is always expected to be satisfied. .defensive_map_err(|_| Error::::BoundNotMet)?, } @@ -888,11 +890,11 @@ impl Pallet { ); } - /// This will return the currently configured History Depth + /// This will return the currently configured `HistoryDepth` /// - /// With release of v11, history_depth is migrated to a configurable - /// value instead of being a storage item - /// This function replaces the old fn history_depth which read from storage + /// With release of v11, `HistoryDepth` is migrated to a configurable + /// value instead of being a storage item. This function replaces the + /// old fn history_depth that used to read from the storage. pub fn history_depth() -> u32 { T::HistoryDepth::get() } diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index d52d4a0079a7e..c270ee8e811d6 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -125,8 +125,15 @@ pub mod pallet { #[pallet::constant] type MaxNominations: Get; - /// Era History Depth for the validator rewards to be claimed. - /// This should never be decreased once set. + /// `HistoryDepth` stores the number of previous eras for which + /// we keep track of validator `claimed_rewards`. + /// + /// Since `HistoryDepth` is used as a bound for BoundedVec + /// `claimed_rewards`, we should use extreme caution while + /// changing this value once it has been already deployed in + /// production. In general, increasing this value should be okay + /// but decreasing it will cause inconsistencies and needs to be + /// handled by doing migration of `StakingLedger.claimed_rewards`. #[pallet::constant] type HistoryDepth: Get; @@ -840,6 +847,8 @@ pub mod pallet { unlocking: Default::default(), claimed_rewards: (last_reward_era..current_era) .try_collect() + // Since last_reward_era is calculated as `current_era - HistoryDepth`, + // following bound is always expected to be satisfied. .defensive_map_err(|_| Error::::BoundNotMet)?, }; Self::update_ledger(&controller, &item); From 5084202880724a0a88d6cf4000fbdd584eb960e5 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Mon, 12 Sep 2022 11:40:54 +0200 Subject: [PATCH 10/36] add HistoryDepth to mock runtimes with pallet-staking --- frame/babe/src/mock.rs | 1 + frame/grandpa/src/mock.rs | 1 + .../nomination-pools/benchmarking/src/mock.rs | 1 + .../nomination-pools/test-staking/src/mock.rs | 1 + frame/offences/benchmarking/src/mock.rs | 1 + frame/session/benchmarking/src/mock.rs | 1 + frame/staking/src/benchmarking.rs | 19 ------------------- frame/staking/src/lib.rs | 4 ++-- 8 files changed, 8 insertions(+), 21 deletions(-) diff --git a/frame/babe/src/mock.rs b/frame/babe/src/mock.rs index fb251457db9ca..d95d5a7fe9a33 100644 --- a/frame/babe/src/mock.rs +++ b/frame/babe/src/mock.rs @@ -204,6 +204,7 @@ impl pallet_staking::Config for Test { type VoterList = pallet_staking::UseNominatorsAndValidatorsMap; type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; + type HistoryDepth = ConstU32<84>; type OnStakerSlash = (); type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; type WeightInfo = (); diff --git a/frame/grandpa/src/mock.rs b/frame/grandpa/src/mock.rs index 19caf0766a3e0..25c30941c1151 100644 --- a/frame/grandpa/src/mock.rs +++ b/frame/grandpa/src/mock.rs @@ -208,6 +208,7 @@ impl pallet_staking::Config for Test { type VoterList = pallet_staking::UseNominatorsAndValidatorsMap; type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; + type HistoryDepth = ConstU32<84>; type OnStakerSlash = (); type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; type WeightInfo = (); diff --git a/frame/nomination-pools/benchmarking/src/mock.rs b/frame/nomination-pools/benchmarking/src/mock.rs index 2dcac34d39a11..e64163a8dbbfd 100644 --- a/frame/nomination-pools/benchmarking/src/mock.rs +++ b/frame/nomination-pools/benchmarking/src/mock.rs @@ -115,6 +115,7 @@ impl pallet_staking::Config for Runtime { type VoterList = VoterList; type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; + type HistoryDepth = ConstU32<84>; type OnStakerSlash = Pools; type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; type WeightInfo = (); diff --git a/frame/nomination-pools/test-staking/src/mock.rs b/frame/nomination-pools/test-staking/src/mock.rs index cb63ad04d1a38..5971a37174938 100644 --- a/frame/nomination-pools/test-staking/src/mock.rs +++ b/frame/nomination-pools/test-staking/src/mock.rs @@ -129,6 +129,7 @@ impl pallet_staking::Config for Runtime { type VoterList = VoterList; type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; + type HistoryDepth = ConstU32<84>; type OnStakerSlash = Pools; type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; type WeightInfo = (); diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index 43c248a09a0b8..695e4eaa3dc99 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -182,6 +182,7 @@ impl pallet_staking::Config for Test { type VoterList = pallet_staking::UseNominatorsAndValidatorsMap; type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; + type HistoryDepth = ConstU32<84>; type OnStakerSlash = (); type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; type WeightInfo = (); diff --git a/frame/session/benchmarking/src/mock.rs b/frame/session/benchmarking/src/mock.rs index de2f4ae23f423..cbf8d6301c815 100644 --- a/frame/session/benchmarking/src/mock.rs +++ b/frame/session/benchmarking/src/mock.rs @@ -174,6 +174,7 @@ impl pallet_staking::Config for Test { type ElectionProvider = onchain::UnboundedExecution; type GenesisElectionProvider = Self::ElectionProvider; type MaxUnlockingChunks = ConstU32<32>; + type HistoryDepth = ConstU32<84>; type VoterList = pallet_staking::UseNominatorsAndValidatorsMap; type TargetList = pallet_staking::UseValidatorsMap; type OnStakerSlash = (); diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index 05f2221ec026f..404dcaf54220a 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -660,25 +660,6 @@ benchmarks! { assert!(original_bonded < new_bonded); } - set_history_depth { - let e in 1 .. 100; - HistoryDepth::::put(e); - CurrentEra::::put(e); - let dummy = || -> T::AccountId { codec::Decode::decode(&mut TrailingZeroInput::zeroes()).unwrap() }; - for i in 0 .. e { - >::insert(i, dummy(), Exposure::>::default()); - >::insert(i, dummy(), Exposure::>::default()); - >::insert(i, dummy(), ValidatorPrefs::default()); - >::insert(i, BalanceOf::::one()); - >::insert(i, EraRewardPoints::::default()); - >::insert(i, BalanceOf::::one()); - ErasStartSessionIndex::::insert(i, i); - } - }: _(RawOrigin::Root, EraIndex::zero(), u32::MAX) - verify { - assert_eq!(HistoryDepth::::get(), 0); - } - reap_stash { let s in 1 .. MAX_SPANS; // clean up any existing state. diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 11b3733b57f87..315c0c88dc49b 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -304,7 +304,7 @@ use frame_support::{ parameter_types, traits::{Currency, Defensive, Get}, weights::Weight, - BoundedVec, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound, + BoundedVec, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound, CloneNoBound, }; use scale_info::TypeInfo; use sp_runtime::{ @@ -440,7 +440,7 @@ pub struct UnlockChunk { } /// The ledger of a (bonded) stash. -#[derive(PartialEqNoBound, EqNoBound, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)] +#[derive(PartialEqNoBound, EqNoBound, CloneNoBound, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)] #[scale_info(skip_type_params(T))] pub struct StakingLedger { /// The stash account whose balance is actually locked and at stake. From 16b80ff09bc174dd42748de085f1bac25e80f351 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Mon, 12 Sep 2022 11:46:11 +0200 Subject: [PATCH 11/36] rustfmt --- frame/staking/src/lib.rs | 17 ++++++++++++++--- frame/staking/src/migrations.rs | 15 +++++++++++++++ frame/staking/src/pallet/impls.rs | 20 +++++++++++--------- frame/staking/src/pallet/mod.rs | 14 +++++++------- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 315c0c88dc49b..261c4ddc333c0 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -304,7 +304,7 @@ use frame_support::{ parameter_types, traits::{Currency, Defensive, Get}, weights::Weight, - BoundedVec, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound, CloneNoBound, + BoundedVec, CloneNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound, }; use scale_info::TypeInfo; use sp_runtime::{ @@ -440,7 +440,16 @@ pub struct UnlockChunk { } /// The ledger of a (bonded) stash. -#[derive(PartialEqNoBound, EqNoBound, CloneNoBound, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)] +#[derive( + PartialEqNoBound, + EqNoBound, + CloneNoBound, + Encode, + Decode, + RuntimeDebugNoBound, + TypeInfo, + MaxEncodedLen, +)] #[scale_info(skip_type_params(T))] pub struct StakingLedger { /// The stash account whose balance is actually locked and at stake. @@ -676,7 +685,9 @@ impl StakingLedger { } /// A record of the nominations made by a specific account. -#[derive(PartialEqNoBound, EqNoBound, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)] +#[derive( + PartialEqNoBound, EqNoBound, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen, +)] #[codec(mel_bound())] #[scale_info(skip_type_params(T))] pub struct Nominations { diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 2556f6d989e1b..d1563e3eb1650 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -78,6 +78,21 @@ pub mod v11 { log!(warn, "v11::migrate should be removed."); T::DbWeight::get().reads(1) } + + let old_pallet_prefix = twox_128(N::get().as_bytes()); + frame_support::ensure!( + sp_io::storage::next_key(&old_pallet_prefix).is_none(), + "old pallet data hasn't been removed" + ); + + let new_pallet_name =

::name(); + let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); + frame_support::ensure!( + sp_io::storage::next_key(&new_pallet_prefix).is_some(), + "new pallet data hasn't been created" + ); + + Ok(()) } #[cfg(feature = "try-runtime")] diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 984d35938dfa7..a95cbe60a3a6f 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -25,8 +25,8 @@ use frame_support::{ dispatch::WithPostDispatchInfo, pallet_prelude::*, traits::{ - Currency, CurrencyToVote, Defensive, EstimateNextNewSession, Get, Imbalance, - LockableCurrency, OnUnbalanced, UnixTime, WithdrawReasons, DefensiveResult, + Currency, CurrencyToVote, Defensive, DefensiveResult, EstimateNextNewSession, Get, + Imbalance, LockableCurrency, OnUnbalanced, UnixTime, WithdrawReasons, }, weights::Weight, }; @@ -119,19 +119,21 @@ impl Pallet { Error::::NotStash.with_weight(T::WeightInfo::payout_stakers_alive_staked(0)) })?; let mut ledger = >::get(&controller).ok_or(Error::::NotController)?; - + ledger .claimed_rewards .retain(|&x| x >= current_era.saturating_sub(history_depth)); - + match ledger.claimed_rewards.binary_search(&era) { Ok(_) => return Err(Error::::AlreadyClaimed .with_weight(T::WeightInfo::payout_stakers_alive_staked(0))), - Err(pos) => ledger.claimed_rewards.try_insert(pos, era) - // Since we retain era entries in `claimed_rewards` only upto - // `HistoryDepth`, following bound is always expected to be satisfied. - .defensive_map_err(|_| Error::::BoundNotMet)?, + Err(pos) => ledger + .claimed_rewards + .try_insert(pos, era) + // Since we retain era entries in `claimed_rewards` only upto + // `HistoryDepth`, following bound is always expected to be satisfied. + .defensive_map_err(|_| Error::::BoundNotMet)?, } let exposure = >::get(&era, &ledger.stash); @@ -891,7 +893,7 @@ impl Pallet { } /// This will return the currently configured `HistoryDepth` - /// + /// /// With release of v11, `HistoryDepth` is migrated to a configurable /// value instead of being a storage item. This function replaces the /// old fn history_depth that used to read from the storage. diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index c270ee8e811d6..42de320dd496d 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -22,9 +22,9 @@ use frame_support::{ dispatch::Codec, pallet_prelude::*, traits::{ - Currency, CurrencyToVote, Defensive, DefensiveSaturating, EnsureOrigin, - EstimateNextNewSession, Get, LockIdentifier, LockableCurrency, OnUnbalanced, UnixTime, - TryCollect, DefensiveResult + Currency, CurrencyToVote, Defensive, DefensiveResult, DefensiveSaturating, EnsureOrigin, + EstimateNextNewSession, Get, LockIdentifier, LockableCurrency, OnUnbalanced, TryCollect, + UnixTime, }, weights::Weight, BoundedVec, @@ -846,10 +846,10 @@ pub mod pallet { active: value, unlocking: Default::default(), claimed_rewards: (last_reward_era..current_era) - .try_collect() - // Since last_reward_era is calculated as `current_era - HistoryDepth`, - // following bound is always expected to be satisfied. - .defensive_map_err(|_| Error::::BoundNotMet)?, + .try_collect() + // Since last_reward_era is calculated as `current_era - HistoryDepth`, + // following bound is always expected to be satisfied. + .defensive_map_err(|_| Error::::BoundNotMet)?, }; Self::update_ledger(&controller, &item); Ok(()) From 1726af2d24089bb7c9557bcc7afad400b4f7a554 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Mon, 12 Sep 2022 15:24:34 +0200 Subject: [PATCH 12/36] refactor and doc improve --- frame/staking/src/pallet/impls.rs | 16 ++++------------ frame/staking/src/pallet/mod.rs | 24 +++++++++++++----------- frame/staking/src/tests.rs | 4 ++-- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index a95cbe60a3a6f..abb8649169fdd 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -101,7 +101,7 @@ impl Pallet { Error::::InvalidEraToReward .with_weight(T::WeightInfo::payout_stakers_alive_staked(0)) })?; - let history_depth = Self::history_depth(); + let history_depth = T::HistoryDepth::get(); ensure!( era <= current_era && era >= current_era.saturating_sub(history_depth), Error::::InvalidEraToReward @@ -132,7 +132,8 @@ impl Pallet { .claimed_rewards .try_insert(pos, era) // Since we retain era entries in `claimed_rewards` only upto - // `HistoryDepth`, following bound is always expected to be satisfied. + // `HistoryDepth`, following bound is always expected to be + // satisfied. .defensive_map_err(|_| Error::::BoundNotMet)?, } @@ -423,7 +424,7 @@ impl Pallet { ErasStartSessionIndex::::insert(&new_planned_era, &start_session_index); // Clean old era information. - if let Some(old_era) = new_planned_era.checked_sub(Self::history_depth() + 1) { + if let Some(old_era) = new_planned_era.checked_sub(T::HistoryDepth::get() + 1) { Self::clear_era_information(old_era); } @@ -891,15 +892,6 @@ impl Pallet { DispatchClass::Mandatory, ); } - - /// This will return the currently configured `HistoryDepth` - /// - /// With release of v11, `HistoryDepth` is migrated to a configurable - /// value instead of being a storage item. This function replaces the - /// old fn history_depth that used to read from the storage. - pub fn history_depth() -> u32 { - T::HistoryDepth::get() - } } impl ElectionDataProvider for Pallet { diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 42de320dd496d..dfd7494189e4d 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -125,15 +125,16 @@ pub mod pallet { #[pallet::constant] type MaxNominations: Get; - /// `HistoryDepth` stores the number of previous eras for which - /// we keep track of validator `claimed_rewards`. - /// - /// Since `HistoryDepth` is used as a bound for BoundedVec - /// `claimed_rewards`, we should use extreme caution while - /// changing this value once it has been already deployed in - /// production. In general, increasing this value should be okay - /// but decreasing it will cause inconsistencies and needs to be - /// handled by doing migration of `StakingLedger.claimed_rewards`. + /// `HistoryDepth` is the number of eras to keep in history. + /// + /// This used to be a storage value previously. If you are migrating + /// from storage value to config value, you should read this value from + /// storage and set the same value in configuration. + /// + /// Note: `HistoryDepth` is used as the upper bound for the `BoundedVec` + /// item `StakingLedger.claimed_rewards`. Setting this value lower than + /// the existing value can lead to inconsistencies and will need to be + /// handled properly in migration. #[pallet::constant] type HistoryDepth: Get; @@ -847,8 +848,9 @@ pub mod pallet { unlocking: Default::default(), claimed_rewards: (last_reward_era..current_era) .try_collect() - // Since last_reward_era is calculated as `current_era - HistoryDepth`, - // following bound is always expected to be satisfied. + // Since last_reward_era is calculated as `current_era - + // HistoryDepth`, following bound is always expected to be + // satisfied. .defensive_map_err(|_| Error::::BoundNotMet)?, }; Self::update_ledger(&controller, &item); diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index d4f00f0b83abf..21736331c31fb 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -3343,7 +3343,7 @@ fn claim_reward_at_the_last_era_and_no_double_claim_and_invalid_claim() { assert!(total_payout_2 != total_payout_0); assert!(total_payout_2 != total_payout_1); - mock::start_active_era(Staking::history_depth() + 1); + mock::start_active_era(HistoryDepth::get() + 1); let active_era = active_era(); @@ -3351,7 +3351,7 @@ fn claim_reward_at_the_last_era_and_no_double_claim_and_invalid_claim() { let current_era = Staking::current_era().unwrap(); // Last kept is 1: - assert!(current_era - Staking::history_depth() == 1); + assert!(current_era - HistoryDepth::get() == 1); assert_noop!( Staking::payout_stakers(Origin::signed(1337), 11, 0), // Fail: Era out of history From a652db75ac1f13981a891dd379906611e6b89496 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Mon, 12 Sep 2022 16:07:52 +0200 Subject: [PATCH 13/36] apply re-benchmarked weight for staking --- frame/staking/src/weights.rs | 290 +++++++++++++++++++---------------- 1 file changed, 159 insertions(+), 131 deletions(-) diff --git a/frame/staking/src/weights.rs b/frame/staking/src/weights.rs index a5dd6bcd1e8b7..8b402bbc1acad 100644 --- a/frame/staking/src/weights.rs +++ b/frame/staking/src/weights.rs @@ -18,22 +18,24 @@ //! Autogenerated weights for pallet_staking //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-05-24, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-09-12, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: `bm3`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: -// ./target/production/substrate +// /home/benchbot/cargo_target_dir/production/substrate // benchmark // pallet -// --chain=dev // --steps=50 // --repeat=20 -// --pallet=pallet_staking // --extrinsic=* // --execution=wasm // --wasm-execution=compiled -// --template=./.maintain/frame-weight-template.hbs +// --heap-pages=4096 +// --pallet=pallet_staking +// --chain=dev // --output=./frame/staking/src/weights.rs +// --template=./.maintain/frame-weight-template.hbs #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] @@ -81,12 +83,11 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking Bonded (r:1 w:1) // Storage: Staking Ledger (r:1 w:1) // Storage: Staking CurrentEra (r:1 w:0) - // Storage: Staking HistoryDepth (r:1 w:0) // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) fn bond() -> Weight { - Weight::from_ref_time(43_992_000 as u64) - .saturating_add(T::DbWeight::get().reads(5 as u64)) + Weight::from_ref_time(52_087_000 as u64) + .saturating_add(T::DbWeight::get().reads(4 as u64)) .saturating_add(T::DbWeight::get().writes(4 as u64)) } // Storage: Staking Bonded (r:1 w:0) @@ -95,7 +96,7 @@ impl WeightInfo for SubstrateWeight { // Storage: BagsList ListNodes (r:3 w:3) // Storage: BagsList ListBags (r:2 w:2) fn bond_extra() -> Weight { - Weight::from_ref_time(75_827_000 as u64) + Weight::from_ref_time(90_109_000 as u64) .saturating_add(T::DbWeight::get().reads(8 as u64)) .saturating_add(T::DbWeight::get().writes(7 as u64)) } @@ -109,7 +110,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking Bonded (r:1 w:0) // Storage: BagsList ListBags (r:2 w:2) fn unbond() -> Weight { - Weight::from_ref_time(81_075_000 as u64) + Weight::from_ref_time(95_758_000 as u64) .saturating_add(T::DbWeight::get().reads(12 as u64)) .saturating_add(T::DbWeight::get().writes(8 as u64)) } @@ -117,10 +118,11 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking CurrentEra (r:1 w:0) // Storage: Balances Locks (r:1 w:1) // Storage: System Account (r:1 w:1) + /// The range of component `s` is `[0, 100]`. fn withdraw_unbonded_update(s: u32, ) -> Weight { - Weight::from_ref_time(35_763_000 as u64) + Weight::from_ref_time(45_405_000 as u64) // Standard Error: 0 - .saturating_add(Weight::from_ref_time(57_000 as u64).saturating_mul(s as u64)) + .saturating_add(Weight::from_ref_time(62_000 as u64).saturating_mul(s as u64)) .saturating_add(T::DbWeight::get().reads(4 as u64)) .saturating_add(T::DbWeight::get().writes(3 as u64)) } @@ -137,8 +139,9 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) + /// The range of component `s` is `[0, 100]`. fn withdraw_unbonded_kill(_s: u32, ) -> Weight { - Weight::from_ref_time(66_938_000 as u64) + Weight::from_ref_time(83_703_000 as u64) .saturating_add(T::DbWeight::get().reads(13 as u64)) .saturating_add(T::DbWeight::get().writes(11 as u64)) } @@ -154,16 +157,17 @@ impl WeightInfo for SubstrateWeight { // Storage: BagsList CounterForListNodes (r:1 w:1) // Storage: Staking CounterForValidators (r:1 w:1) fn validate() -> Weight { - Weight::from_ref_time(52_943_000 as u64) + Weight::from_ref_time(64_456_000 as u64) .saturating_add(T::DbWeight::get().reads(11 as u64)) .saturating_add(T::DbWeight::get().writes(5 as u64)) } // Storage: Staking Ledger (r:1 w:0) // Storage: Staking Nominators (r:1 w:1) + /// The range of component `k` is `[1, 128]`. fn kick(k: u32, ) -> Weight { - Weight::from_ref_time(23_264_000 as u64) - // Standard Error: 11_000 - .saturating_add(Weight::from_ref_time(8_006_000 as u64).saturating_mul(k as u64)) + Weight::from_ref_time(44_203_000 as u64) + // Standard Error: 13_000 + .saturating_add(Weight::from_ref_time(6_582_000 as u64).saturating_mul(k as u64)) .saturating_add(T::DbWeight::get().reads(1 as u64)) .saturating_add(T::DbWeight::get().reads((1 as u64).saturating_mul(k as u64))) .saturating_add(T::DbWeight::get().writes((1 as u64).saturating_mul(k as u64))) @@ -179,10 +183,11 @@ impl WeightInfo for SubstrateWeight { // Storage: BagsList ListBags (r:1 w:1) // Storage: BagsList CounterForListNodes (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) + /// The range of component `n` is `[1, 16]`. fn nominate(n: u32, ) -> Weight { - Weight::from_ref_time(56_596_000 as u64) - // Standard Error: 14_000 - .saturating_add(Weight::from_ref_time(3_644_000 as u64).saturating_mul(n as u64)) + Weight::from_ref_time(69_707_000 as u64) + // Standard Error: 6_000 + .saturating_add(Weight::from_ref_time(2_736_000 as u64).saturating_mul(n as u64)) .saturating_add(T::DbWeight::get().reads(12 as u64)) .saturating_add(T::DbWeight::get().reads((1 as u64).saturating_mul(n as u64))) .saturating_add(T::DbWeight::get().writes(6 as u64)) @@ -195,47 +200,48 @@ impl WeightInfo for SubstrateWeight { // Storage: BagsList ListBags (r:1 w:1) // Storage: BagsList CounterForListNodes (r:1 w:1) fn chill() -> Weight { - Weight::from_ref_time(51_117_000 as u64) + Weight::from_ref_time(63_314_000 as u64) .saturating_add(T::DbWeight::get().reads(8 as u64)) .saturating_add(T::DbWeight::get().writes(6 as u64)) } // Storage: Staking Ledger (r:1 w:0) // Storage: Staking Payee (r:0 w:1) fn set_payee() -> Weight { - Weight::from_ref_time(11_223_000 as u64) + Weight::from_ref_time(17_994_000 as u64) .saturating_add(T::DbWeight::get().reads(1 as u64)) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking Bonded (r:1 w:1) // Storage: Staking Ledger (r:2 w:2) fn set_controller() -> Weight { - Weight::from_ref_time(19_826_000 as u64) + Weight::from_ref_time(25_821_000 as u64) .saturating_add(T::DbWeight::get().reads(3 as u64)) .saturating_add(T::DbWeight::get().writes(3 as u64)) } // Storage: Staking ValidatorCount (r:0 w:1) fn set_validator_count() -> Weight { - Weight::from_ref_time(3_789_000 as u64) + Weight::from_ref_time(4_775_000 as u64) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_no_eras() -> Weight { - Weight::from_ref_time(3_793_000 as u64) + Weight::from_ref_time(4_901_000 as u64) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_new_era() -> Weight { - Weight::from_ref_time(3_802_000 as u64) + Weight::from_ref_time(4_982_000 as u64) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_new_era_always() -> Weight { - Weight::from_ref_time(3_762_000 as u64) + Weight::from_ref_time(5_036_000 as u64) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking Invulnerables (r:0 w:1) + /// The range of component `v` is `[0, 1000]`. fn set_invulnerables(v: u32, ) -> Weight { - Weight::from_ref_time(4_318_000 as u64) + Weight::from_ref_time(5_771_000 as u64) // Standard Error: 0 .saturating_add(Weight::from_ref_time(10_000 as u64).saturating_mul(v as u64)) .saturating_add(T::DbWeight::get().writes(1 as u64)) @@ -253,24 +259,25 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking Ledger (r:0 w:1) // Storage: Staking Payee (r:0 w:1) // Storage: Staking SpanSlash (r:0 w:2) + /// The range of component `s` is `[0, 100]`. fn force_unstake(s: u32, ) -> Weight { - Weight::from_ref_time(65_265_000 as u64) - // Standard Error: 1_000 - .saturating_add(Weight::from_ref_time(1_029_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(81_408_000 as u64) + // Standard Error: 2_000 + .saturating_add(Weight::from_ref_time(1_052_000 as u64).saturating_mul(s as u64)) .saturating_add(T::DbWeight::get().reads(11 as u64)) .saturating_add(T::DbWeight::get().writes(12 as u64)) .saturating_add(T::DbWeight::get().writes((1 as u64).saturating_mul(s as u64))) } // Storage: Staking UnappliedSlashes (r:1 w:1) + /// The range of component `s` is `[1, 1000]`. fn cancel_deferred_slash(s: u32, ) -> Weight { - Weight::from_ref_time(903_312_000 as u64) - // Standard Error: 56_000 - .saturating_add(Weight::from_ref_time(4_968_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(900_716_000 as u64) + // Standard Error: 59_000 + .saturating_add(Weight::from_ref_time(4_937_000 as u64).saturating_mul(s as u64)) .saturating_add(T::DbWeight::get().reads(1 as u64)) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking CurrentEra (r:1 w:0) - // Storage: Staking HistoryDepth (r:1 w:0) // Storage: Staking ErasValidatorReward (r:1 w:0) // Storage: Staking Bonded (r:2 w:0) // Storage: Staking Ledger (r:1 w:1) @@ -279,17 +286,17 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking ErasValidatorPrefs (r:1 w:0) // Storage: Staking Payee (r:2 w:0) // Storage: System Account (r:2 w:2) + /// The range of component `n` is `[1, 256]`. fn payout_stakers_dead_controller(n: u32, ) -> Weight { - Weight::from_ref_time(87_569_000 as u64) - // Standard Error: 14_000 - .saturating_add(Weight::from_ref_time(24_232_000 as u64).saturating_mul(n as u64)) - .saturating_add(T::DbWeight::get().reads(10 as u64)) + Weight::from_ref_time(184_408_000 as u64) + // Standard Error: 16_000 + .saturating_add(Weight::from_ref_time(21_099_000 as u64).saturating_mul(n as u64)) + .saturating_add(T::DbWeight::get().reads(9 as u64)) .saturating_add(T::DbWeight::get().reads((3 as u64).saturating_mul(n as u64))) .saturating_add(T::DbWeight::get().writes(2 as u64)) .saturating_add(T::DbWeight::get().writes((1 as u64).saturating_mul(n as u64))) } // Storage: Staking CurrentEra (r:1 w:0) - // Storage: Staking HistoryDepth (r:1 w:0) // Storage: Staking ErasValidatorReward (r:1 w:0) // Storage: Staking Bonded (r:2 w:0) // Storage: Staking Ledger (r:2 w:2) @@ -299,11 +306,12 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking Payee (r:2 w:0) // Storage: System Account (r:2 w:2) // Storage: Balances Locks (r:2 w:2) + /// The range of component `n` is `[1, 256]`. fn payout_stakers_alive_staked(n: u32, ) -> Weight { - Weight::from_ref_time(98_839_000 as u64) - // Standard Error: 21_000 - .saturating_add(Weight::from_ref_time(34_480_000 as u64).saturating_mul(n as u64)) - .saturating_add(T::DbWeight::get().reads(11 as u64)) + Weight::from_ref_time(213_358_000 as u64) + // Standard Error: 26_000 + .saturating_add(Weight::from_ref_time(29_938_000 as u64).saturating_mul(n as u64)) + .saturating_add(T::DbWeight::get().reads(10 as u64)) .saturating_add(T::DbWeight::get().reads((5 as u64).saturating_mul(n as u64))) .saturating_add(T::DbWeight::get().writes(3 as u64)) .saturating_add(T::DbWeight::get().writes((3 as u64).saturating_mul(n as u64))) @@ -314,10 +322,11 @@ impl WeightInfo for SubstrateWeight { // Storage: BagsList ListNodes (r:3 w:3) // Storage: Staking Bonded (r:1 w:0) // Storage: BagsList ListBags (r:2 w:2) + /// The range of component `l` is `[1, 32]`. fn rebond(l: u32, ) -> Weight { - Weight::from_ref_time(74_865_000 as u64) - // Standard Error: 3_000 - .saturating_add(Weight::from_ref_time(64_000 as u64).saturating_mul(l as u64)) + Weight::from_ref_time(90_805_000 as u64) + // Standard Error: 4_000 + .saturating_add(Weight::from_ref_time(46_000 as u64).saturating_mul(l as u64)) .saturating_add(T::DbWeight::get().reads(9 as u64)) .saturating_add(T::DbWeight::get().writes(8 as u64)) } @@ -334,10 +343,11 @@ impl WeightInfo for SubstrateWeight { // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) // Storage: Staking SpanSlash (r:0 w:1) + /// The range of component `s` is `[1, 100]`. fn reap_stash(s: u32, ) -> Weight { - Weight::from_ref_time(70_933_000 as u64) + Weight::from_ref_time(89_762_000 as u64) // Standard Error: 1_000 - .saturating_add(Weight::from_ref_time(1_021_000 as u64).saturating_mul(s as u64)) + .saturating_add(Weight::from_ref_time(1_035_000 as u64).saturating_mul(s as u64)) .saturating_add(T::DbWeight::get().reads(12 as u64)) .saturating_add(T::DbWeight::get().writes(12 as u64)) .saturating_add(T::DbWeight::get().writes((1 as u64).saturating_mul(s as u64))) @@ -354,19 +364,20 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking ValidatorCount (r:1 w:0) // Storage: Staking MinimumValidatorCount (r:1 w:0) // Storage: Staking CurrentEra (r:1 w:1) - // Storage: Staking HistoryDepth (r:1 w:0) // Storage: Staking ErasStakersClipped (r:0 w:1) // Storage: Staking ErasValidatorPrefs (r:0 w:1) // Storage: Staking ErasStakers (r:0 w:1) // Storage: Staking ErasTotalStake (r:0 w:1) // Storage: Staking ErasStartSessionIndex (r:0 w:1) + /// The range of component `v` is `[1, 10]`. + /// The range of component `n` is `[1, 100]`. fn new_era(v: u32, n: u32, ) -> Weight { Weight::from_ref_time(0 as u64) - // Standard Error: 897_000 - .saturating_add(Weight::from_ref_time(213_100_000 as u64).saturating_mul(v as u64)) - // Standard Error: 45_000 - .saturating_add(Weight::from_ref_time(31_123_000 as u64).saturating_mul(n as u64)) - .saturating_add(T::DbWeight::get().reads(208 as u64)) + // Standard Error: 1_222_000 + .saturating_add(Weight::from_ref_time(298_086_000 as u64).saturating_mul(v as u64)) + // Standard Error: 117_000 + .saturating_add(Weight::from_ref_time(34_215_000 as u64).saturating_mul(n as u64)) + .saturating_add(T::DbWeight::get().reads(207 as u64)) .saturating_add(T::DbWeight::get().reads((5 as u64).saturating_mul(v as u64))) .saturating_add(T::DbWeight::get().reads((4 as u64).saturating_mul(n as u64))) .saturating_add(T::DbWeight::get().writes(3 as u64)) @@ -380,24 +391,28 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking Validators (r:500 w:0) // Storage: Staking Bonded (r:1500 w:0) // Storage: Staking Ledger (r:1500 w:0) + /// The range of component `v` is `[500, 1000]`. + /// The range of component `n` is `[500, 1000]`. + /// The range of component `s` is `[1, 20]`. fn get_npos_voters(v: u32, n: u32, s: u32, ) -> Weight { Weight::from_ref_time(0 as u64) - // Standard Error: 116_000 - .saturating_add(Weight::from_ref_time(23_745_000 as u64).saturating_mul(v as u64)) - // Standard Error: 116_000 - .saturating_add(Weight::from_ref_time(22_497_000 as u64).saturating_mul(n as u64)) - // Standard Error: 3_968_000 - .saturating_add(Weight::from_ref_time(20_676_000 as u64).saturating_mul(s as u64)) + // Standard Error: 95_000 + .saturating_add(Weight::from_ref_time(22_872_000 as u64).saturating_mul(v as u64)) + // Standard Error: 95_000 + .saturating_add(Weight::from_ref_time(22_964_000 as u64).saturating_mul(n as u64)) + // Standard Error: 2_435_000 + .saturating_add(Weight::from_ref_time(37_949_000 as u64).saturating_mul(s as u64)) .saturating_add(T::DbWeight::get().reads(202 as u64)) .saturating_add(T::DbWeight::get().reads((5 as u64).saturating_mul(v as u64))) .saturating_add(T::DbWeight::get().reads((4 as u64).saturating_mul(n as u64))) .saturating_add(T::DbWeight::get().reads((1 as u64).saturating_mul(s as u64))) } // Storage: Staking Validators (r:501 w:0) + /// The range of component `v` is `[500, 1000]`. fn get_npos_targets(v: u32, ) -> Weight { - Weight::from_ref_time(0 as u64) - // Standard Error: 36_000 - .saturating_add(Weight::from_ref_time(8_097_000 as u64).saturating_mul(v as u64)) + Weight::from_ref_time(115_076_000 as u64) + // Standard Error: 25_000 + .saturating_add(Weight::from_ref_time(8_629_000 as u64).saturating_mul(v as u64)) .saturating_add(T::DbWeight::get().reads(1 as u64)) .saturating_add(T::DbWeight::get().reads((1 as u64).saturating_mul(v as u64))) } @@ -408,7 +423,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking MaxNominatorsCount (r:0 w:1) // Storage: Staking MinNominatorBond (r:0 w:1) fn set_staking_configs_all_set() -> Weight { - Weight::from_ref_time(7_041_000 as u64) + Weight::from_ref_time(9_775_000 as u64) .saturating_add(T::DbWeight::get().writes(6 as u64)) } // Storage: Staking MinCommission (r:0 w:1) @@ -418,7 +433,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking MaxNominatorsCount (r:0 w:1) // Storage: Staking MinNominatorBond (r:0 w:1) fn set_staking_configs_all_remove() -> Weight { - Weight::from_ref_time(6_495_000 as u64) + Weight::from_ref_time(9_042_000 as u64) .saturating_add(T::DbWeight::get().writes(6 as u64)) } // Storage: Staking Ledger (r:1 w:0) @@ -432,14 +447,14 @@ impl WeightInfo for SubstrateWeight { // Storage: BagsList ListBags (r:1 w:1) // Storage: BagsList CounterForListNodes (r:1 w:1) fn chill_other() -> Weight { - Weight::from_ref_time(62_014_000 as u64) + Weight::from_ref_time(79_225_000 as u64) .saturating_add(T::DbWeight::get().reads(11 as u64)) .saturating_add(T::DbWeight::get().writes(6 as u64)) } // Storage: Staking MinCommission (r:1 w:0) // Storage: Staking Validators (r:1 w:1) fn force_apply_min_commission() -> Weight { - Weight::from_ref_time(12_814_000 as u64) + Weight::from_ref_time(19_293_000 as u64) .saturating_add(T::DbWeight::get().reads(2 as u64)) .saturating_add(T::DbWeight::get().writes(1 as u64)) } @@ -450,12 +465,11 @@ impl WeightInfo for () { // Storage: Staking Bonded (r:1 w:1) // Storage: Staking Ledger (r:1 w:1) // Storage: Staking CurrentEra (r:1 w:0) - // Storage: Staking HistoryDepth (r:1 w:0) // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) fn bond() -> Weight { - Weight::from_ref_time(43_992_000 as u64) - .saturating_add(RocksDbWeight::get().reads(5 as u64)) + Weight::from_ref_time(52_087_000 as u64) + .saturating_add(RocksDbWeight::get().reads(4 as u64)) .saturating_add(RocksDbWeight::get().writes(4 as u64)) } // Storage: Staking Bonded (r:1 w:0) @@ -464,7 +478,7 @@ impl WeightInfo for () { // Storage: BagsList ListNodes (r:3 w:3) // Storage: BagsList ListBags (r:2 w:2) fn bond_extra() -> Weight { - Weight::from_ref_time(75_827_000 as u64) + Weight::from_ref_time(90_109_000 as u64) .saturating_add(RocksDbWeight::get().reads(8 as u64)) .saturating_add(RocksDbWeight::get().writes(7 as u64)) } @@ -478,7 +492,7 @@ impl WeightInfo for () { // Storage: Staking Bonded (r:1 w:0) // Storage: BagsList ListBags (r:2 w:2) fn unbond() -> Weight { - Weight::from_ref_time(81_075_000 as u64) + Weight::from_ref_time(95_758_000 as u64) .saturating_add(RocksDbWeight::get().reads(12 as u64)) .saturating_add(RocksDbWeight::get().writes(8 as u64)) } @@ -486,10 +500,11 @@ impl WeightInfo for () { // Storage: Staking CurrentEra (r:1 w:0) // Storage: Balances Locks (r:1 w:1) // Storage: System Account (r:1 w:1) + /// The range of component `s` is `[0, 100]`. fn withdraw_unbonded_update(s: u32, ) -> Weight { - Weight::from_ref_time(35_763_000 as u64) + Weight::from_ref_time(45_405_000 as u64) // Standard Error: 0 - .saturating_add(Weight::from_ref_time(57_000 as u64).saturating_mul(s as u64)) + .saturating_add(Weight::from_ref_time(62_000 as u64).saturating_mul(s as u64)) .saturating_add(RocksDbWeight::get().reads(4 as u64)) .saturating_add(RocksDbWeight::get().writes(3 as u64)) } @@ -506,8 +521,9 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) + /// The range of component `s` is `[0, 100]`. fn withdraw_unbonded_kill(_s: u32, ) -> Weight { - Weight::from_ref_time(66_938_000 as u64) + Weight::from_ref_time(83_703_000 as u64) .saturating_add(RocksDbWeight::get().reads(13 as u64)) .saturating_add(RocksDbWeight::get().writes(11 as u64)) } @@ -523,16 +539,17 @@ impl WeightInfo for () { // Storage: BagsList CounterForListNodes (r:1 w:1) // Storage: Staking CounterForValidators (r:1 w:1) fn validate() -> Weight { - Weight::from_ref_time(52_943_000 as u64) + Weight::from_ref_time(64_456_000 as u64) .saturating_add(RocksDbWeight::get().reads(11 as u64)) .saturating_add(RocksDbWeight::get().writes(5 as u64)) } // Storage: Staking Ledger (r:1 w:0) // Storage: Staking Nominators (r:1 w:1) + /// The range of component `k` is `[1, 128]`. fn kick(k: u32, ) -> Weight { - Weight::from_ref_time(23_264_000 as u64) - // Standard Error: 11_000 - .saturating_add(Weight::from_ref_time(8_006_000 as u64).saturating_mul(k as u64)) + Weight::from_ref_time(44_203_000 as u64) + // Standard Error: 13_000 + .saturating_add(Weight::from_ref_time(6_582_000 as u64).saturating_mul(k as u64)) .saturating_add(RocksDbWeight::get().reads(1 as u64)) .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(k as u64))) .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(k as u64))) @@ -548,10 +565,11 @@ impl WeightInfo for () { // Storage: BagsList ListBags (r:1 w:1) // Storage: BagsList CounterForListNodes (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) + /// The range of component `n` is `[1, 16]`. fn nominate(n: u32, ) -> Weight { - Weight::from_ref_time(56_596_000 as u64) - // Standard Error: 14_000 - .saturating_add(Weight::from_ref_time(3_644_000 as u64).saturating_mul(n as u64)) + Weight::from_ref_time(69_707_000 as u64) + // Standard Error: 6_000 + .saturating_add(Weight::from_ref_time(2_736_000 as u64).saturating_mul(n as u64)) .saturating_add(RocksDbWeight::get().reads(12 as u64)) .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(n as u64))) .saturating_add(RocksDbWeight::get().writes(6 as u64)) @@ -564,47 +582,48 @@ impl WeightInfo for () { // Storage: BagsList ListBags (r:1 w:1) // Storage: BagsList CounterForListNodes (r:1 w:1) fn chill() -> Weight { - Weight::from_ref_time(51_117_000 as u64) + Weight::from_ref_time(63_314_000 as u64) .saturating_add(RocksDbWeight::get().reads(8 as u64)) .saturating_add(RocksDbWeight::get().writes(6 as u64)) } // Storage: Staking Ledger (r:1 w:0) // Storage: Staking Payee (r:0 w:1) fn set_payee() -> Weight { - Weight::from_ref_time(11_223_000 as u64) + Weight::from_ref_time(17_994_000 as u64) .saturating_add(RocksDbWeight::get().reads(1 as u64)) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking Bonded (r:1 w:1) // Storage: Staking Ledger (r:2 w:2) fn set_controller() -> Weight { - Weight::from_ref_time(19_826_000 as u64) + Weight::from_ref_time(25_821_000 as u64) .saturating_add(RocksDbWeight::get().reads(3 as u64)) .saturating_add(RocksDbWeight::get().writes(3 as u64)) } // Storage: Staking ValidatorCount (r:0 w:1) fn set_validator_count() -> Weight { - Weight::from_ref_time(3_789_000 as u64) + Weight::from_ref_time(4_775_000 as u64) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_no_eras() -> Weight { - Weight::from_ref_time(3_793_000 as u64) + Weight::from_ref_time(4_901_000 as u64) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_new_era() -> Weight { - Weight::from_ref_time(3_802_000 as u64) + Weight::from_ref_time(4_982_000 as u64) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_new_era_always() -> Weight { - Weight::from_ref_time(3_762_000 as u64) + Weight::from_ref_time(5_036_000 as u64) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking Invulnerables (r:0 w:1) + /// The range of component `v` is `[0, 1000]`. fn set_invulnerables(v: u32, ) -> Weight { - Weight::from_ref_time(4_318_000 as u64) + Weight::from_ref_time(5_771_000 as u64) // Standard Error: 0 .saturating_add(Weight::from_ref_time(10_000 as u64).saturating_mul(v as u64)) .saturating_add(RocksDbWeight::get().writes(1 as u64)) @@ -622,24 +641,25 @@ impl WeightInfo for () { // Storage: Staking Ledger (r:0 w:1) // Storage: Staking Payee (r:0 w:1) // Storage: Staking SpanSlash (r:0 w:2) + /// The range of component `s` is `[0, 100]`. fn force_unstake(s: u32, ) -> Weight { - Weight::from_ref_time(65_265_000 as u64) - // Standard Error: 1_000 - .saturating_add(Weight::from_ref_time(1_029_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(81_408_000 as u64) + // Standard Error: 2_000 + .saturating_add(Weight::from_ref_time(1_052_000 as u64).saturating_mul(s as u64)) .saturating_add(RocksDbWeight::get().reads(11 as u64)) .saturating_add(RocksDbWeight::get().writes(12 as u64)) .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(s as u64))) } // Storage: Staking UnappliedSlashes (r:1 w:1) + /// The range of component `s` is `[1, 1000]`. fn cancel_deferred_slash(s: u32, ) -> Weight { - Weight::from_ref_time(903_312_000 as u64) - // Standard Error: 56_000 - .saturating_add(Weight::from_ref_time(4_968_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(900_716_000 as u64) + // Standard Error: 59_000 + .saturating_add(Weight::from_ref_time(4_937_000 as u64).saturating_mul(s as u64)) .saturating_add(RocksDbWeight::get().reads(1 as u64)) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking CurrentEra (r:1 w:0) - // Storage: Staking HistoryDepth (r:1 w:0) // Storage: Staking ErasValidatorReward (r:1 w:0) // Storage: Staking Bonded (r:2 w:0) // Storage: Staking Ledger (r:1 w:1) @@ -648,17 +668,17 @@ impl WeightInfo for () { // Storage: Staking ErasValidatorPrefs (r:1 w:0) // Storage: Staking Payee (r:2 w:0) // Storage: System Account (r:2 w:2) + /// The range of component `n` is `[1, 256]`. fn payout_stakers_dead_controller(n: u32, ) -> Weight { - Weight::from_ref_time(87_569_000 as u64) - // Standard Error: 14_000 - .saturating_add(Weight::from_ref_time(24_232_000 as u64).saturating_mul(n as u64)) - .saturating_add(RocksDbWeight::get().reads(10 as u64)) + Weight::from_ref_time(184_408_000 as u64) + // Standard Error: 16_000 + .saturating_add(Weight::from_ref_time(21_099_000 as u64).saturating_mul(n as u64)) + .saturating_add(RocksDbWeight::get().reads(9 as u64)) .saturating_add(RocksDbWeight::get().reads((3 as u64).saturating_mul(n as u64))) .saturating_add(RocksDbWeight::get().writes(2 as u64)) .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(n as u64))) } // Storage: Staking CurrentEra (r:1 w:0) - // Storage: Staking HistoryDepth (r:1 w:0) // Storage: Staking ErasValidatorReward (r:1 w:0) // Storage: Staking Bonded (r:2 w:0) // Storage: Staking Ledger (r:2 w:2) @@ -668,11 +688,12 @@ impl WeightInfo for () { // Storage: Staking Payee (r:2 w:0) // Storage: System Account (r:2 w:2) // Storage: Balances Locks (r:2 w:2) + /// The range of component `n` is `[1, 256]`. fn payout_stakers_alive_staked(n: u32, ) -> Weight { - Weight::from_ref_time(98_839_000 as u64) - // Standard Error: 21_000 - .saturating_add(Weight::from_ref_time(34_480_000 as u64).saturating_mul(n as u64)) - .saturating_add(RocksDbWeight::get().reads(11 as u64)) + Weight::from_ref_time(213_358_000 as u64) + // Standard Error: 26_000 + .saturating_add(Weight::from_ref_time(29_938_000 as u64).saturating_mul(n as u64)) + .saturating_add(RocksDbWeight::get().reads(10 as u64)) .saturating_add(RocksDbWeight::get().reads((5 as u64).saturating_mul(n as u64))) .saturating_add(RocksDbWeight::get().writes(3 as u64)) .saturating_add(RocksDbWeight::get().writes((3 as u64).saturating_mul(n as u64))) @@ -683,10 +704,11 @@ impl WeightInfo for () { // Storage: BagsList ListNodes (r:3 w:3) // Storage: Staking Bonded (r:1 w:0) // Storage: BagsList ListBags (r:2 w:2) + /// The range of component `l` is `[1, 32]`. fn rebond(l: u32, ) -> Weight { - Weight::from_ref_time(74_865_000 as u64) - // Standard Error: 3_000 - .saturating_add(Weight::from_ref_time(64_000 as u64).saturating_mul(l as u64)) + Weight::from_ref_time(90_805_000 as u64) + // Standard Error: 4_000 + .saturating_add(Weight::from_ref_time(46_000 as u64).saturating_mul(l as u64)) .saturating_add(RocksDbWeight::get().reads(9 as u64)) .saturating_add(RocksDbWeight::get().writes(8 as u64)) } @@ -703,10 +725,11 @@ impl WeightInfo for () { // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) // Storage: Staking SpanSlash (r:0 w:1) + /// The range of component `s` is `[1, 100]`. fn reap_stash(s: u32, ) -> Weight { - Weight::from_ref_time(70_933_000 as u64) + Weight::from_ref_time(89_762_000 as u64) // Standard Error: 1_000 - .saturating_add(Weight::from_ref_time(1_021_000 as u64).saturating_mul(s as u64)) + .saturating_add(Weight::from_ref_time(1_035_000 as u64).saturating_mul(s as u64)) .saturating_add(RocksDbWeight::get().reads(12 as u64)) .saturating_add(RocksDbWeight::get().writes(12 as u64)) .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(s as u64))) @@ -723,19 +746,20 @@ impl WeightInfo for () { // Storage: Staking ValidatorCount (r:1 w:0) // Storage: Staking MinimumValidatorCount (r:1 w:0) // Storage: Staking CurrentEra (r:1 w:1) - // Storage: Staking HistoryDepth (r:1 w:0) // Storage: Staking ErasStakersClipped (r:0 w:1) // Storage: Staking ErasValidatorPrefs (r:0 w:1) // Storage: Staking ErasStakers (r:0 w:1) // Storage: Staking ErasTotalStake (r:0 w:1) // Storage: Staking ErasStartSessionIndex (r:0 w:1) + /// The range of component `v` is `[1, 10]`. + /// The range of component `n` is `[1, 100]`. fn new_era(v: u32, n: u32, ) -> Weight { Weight::from_ref_time(0 as u64) - // Standard Error: 897_000 - .saturating_add(Weight::from_ref_time(213_100_000 as u64).saturating_mul(v as u64)) - // Standard Error: 45_000 - .saturating_add(Weight::from_ref_time(31_123_000 as u64).saturating_mul(n as u64)) - .saturating_add(RocksDbWeight::get().reads(208 as u64)) + // Standard Error: 1_222_000 + .saturating_add(Weight::from_ref_time(298_086_000 as u64).saturating_mul(v as u64)) + // Standard Error: 117_000 + .saturating_add(Weight::from_ref_time(34_215_000 as u64).saturating_mul(n as u64)) + .saturating_add(RocksDbWeight::get().reads(207 as u64)) .saturating_add(RocksDbWeight::get().reads((5 as u64).saturating_mul(v as u64))) .saturating_add(RocksDbWeight::get().reads((4 as u64).saturating_mul(n as u64))) .saturating_add(RocksDbWeight::get().writes(3 as u64)) @@ -749,24 +773,28 @@ impl WeightInfo for () { // Storage: Staking Validators (r:500 w:0) // Storage: Staking Bonded (r:1500 w:0) // Storage: Staking Ledger (r:1500 w:0) + /// The range of component `v` is `[500, 1000]`. + /// The range of component `n` is `[500, 1000]`. + /// The range of component `s` is `[1, 20]`. fn get_npos_voters(v: u32, n: u32, s: u32, ) -> Weight { Weight::from_ref_time(0 as u64) - // Standard Error: 116_000 - .saturating_add(Weight::from_ref_time(23_745_000 as u64).saturating_mul(v as u64)) - // Standard Error: 116_000 - .saturating_add(Weight::from_ref_time(22_497_000 as u64).saturating_mul(n as u64)) - // Standard Error: 3_968_000 - .saturating_add(Weight::from_ref_time(20_676_000 as u64).saturating_mul(s as u64)) + // Standard Error: 95_000 + .saturating_add(Weight::from_ref_time(22_872_000 as u64).saturating_mul(v as u64)) + // Standard Error: 95_000 + .saturating_add(Weight::from_ref_time(22_964_000 as u64).saturating_mul(n as u64)) + // Standard Error: 2_435_000 + .saturating_add(Weight::from_ref_time(37_949_000 as u64).saturating_mul(s as u64)) .saturating_add(RocksDbWeight::get().reads(202 as u64)) .saturating_add(RocksDbWeight::get().reads((5 as u64).saturating_mul(v as u64))) .saturating_add(RocksDbWeight::get().reads((4 as u64).saturating_mul(n as u64))) .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(s as u64))) } // Storage: Staking Validators (r:501 w:0) + /// The range of component `v` is `[500, 1000]`. fn get_npos_targets(v: u32, ) -> Weight { - Weight::from_ref_time(0 as u64) - // Standard Error: 36_000 - .saturating_add(Weight::from_ref_time(8_097_000 as u64).saturating_mul(v as u64)) + Weight::from_ref_time(115_076_000 as u64) + // Standard Error: 25_000 + .saturating_add(Weight::from_ref_time(8_629_000 as u64).saturating_mul(v as u64)) .saturating_add(RocksDbWeight::get().reads(1 as u64)) .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(v as u64))) } @@ -777,7 +805,7 @@ impl WeightInfo for () { // Storage: Staking MaxNominatorsCount (r:0 w:1) // Storage: Staking MinNominatorBond (r:0 w:1) fn set_staking_configs_all_set() -> Weight { - Weight::from_ref_time(7_041_000 as u64) + Weight::from_ref_time(9_775_000 as u64) .saturating_add(RocksDbWeight::get().writes(6 as u64)) } // Storage: Staking MinCommission (r:0 w:1) @@ -787,7 +815,7 @@ impl WeightInfo for () { // Storage: Staking MaxNominatorsCount (r:0 w:1) // Storage: Staking MinNominatorBond (r:0 w:1) fn set_staking_configs_all_remove() -> Weight { - Weight::from_ref_time(6_495_000 as u64) + Weight::from_ref_time(9_042_000 as u64) .saturating_add(RocksDbWeight::get().writes(6 as u64)) } // Storage: Staking Ledger (r:1 w:0) @@ -801,14 +829,14 @@ impl WeightInfo for () { // Storage: BagsList ListBags (r:1 w:1) // Storage: BagsList CounterForListNodes (r:1 w:1) fn chill_other() -> Weight { - Weight::from_ref_time(62_014_000 as u64) + Weight::from_ref_time(79_225_000 as u64) .saturating_add(RocksDbWeight::get().reads(11 as u64)) .saturating_add(RocksDbWeight::get().writes(6 as u64)) } // Storage: Staking MinCommission (r:1 w:0) // Storage: Staking Validators (r:1 w:1) fn force_apply_min_commission() -> Weight { - Weight::from_ref_time(12_814_000 as u64) + Weight::from_ref_time(19_293_000 as u64) .saturating_add(RocksDbWeight::get().reads(2 as u64)) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } From ba57f5df1360d8b1e5314ef5afe5c1f0d07818b3 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 13 Sep 2022 13:40:24 +0200 Subject: [PATCH 14/36] pr feedback improvements --- frame/staking/src/pallet/mod.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index dfd7494189e4d..eb1ee1c621159 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -125,16 +125,22 @@ pub mod pallet { #[pallet::constant] type MaxNominations: Get; - /// `HistoryDepth` is the number of eras to keep in history. + /// Number of eras to keep in history. /// - /// This used to be a storage value previously. If you are migrating - /// from storage value to config value, you should read this value from - /// storage and set the same value in configuration. + /// Information is kept for eras in `[current_era - history_depth; + /// current_era]`. + /// + /// Must be more than the number of eras delayed by session otherwise. + /// I.e. active era must always be in history. I.e. `active_era > + /// current_era - history_depth` must be guaranteed. + /// + /// If migrating an existing pallet from storage value to config value, + /// this should be set to same value or greater as in storage. /// /// Note: `HistoryDepth` is used as the upper bound for the `BoundedVec` /// item `StakingLedger.claimed_rewards`. Setting this value lower than /// the existing value can lead to inconsistencies and will need to be - /// handled properly in migration. + /// handled properly in a migration. #[pallet::constant] type HistoryDepth: Get; From 36595e76ac5cdc0fe8828e33ee7ac84ad35a838d Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 13 Sep 2022 14:03:10 +0200 Subject: [PATCH 15/36] test for claimed rewards following the expected bounds --- frame/staking/src/tests.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 21736331c31fb..6690b30c6ed76 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -3851,7 +3851,11 @@ fn bond_during_era_correctly_populates_claimed_rewards() { claimed_rewards: (0..5).collect::>().try_into().unwrap(), }) ); - mock::start_active_era(99); + + // make sure only era upto history depth is stored + let current_era = 99; + let last_reward_era = 99 - HistoryDepth::get(); + mock::start_active_era(current_era); bond_validator(13, 12, 1000); assert_eq!( Staking::ledger(&12), @@ -3860,7 +3864,10 @@ fn bond_during_era_correctly_populates_claimed_rewards() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: (15..99).collect::>().try_into().unwrap(), + claimed_rewards: (last_reward_era..current_era) + .collect::>() + .try_into() + .unwrap(), }) ); }); From 95baa045c9c210843108784ac5de57ea33b8f22d Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 13 Sep 2022 14:36:31 +0200 Subject: [PATCH 16/36] refactor test to calculate first and last reward era programmatically --- frame/staking/src/mock.rs | 2 +- frame/staking/src/tests.rs | 45 ++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 86883ba2c0282..5e80ff51daf47 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -236,7 +236,7 @@ const THRESHOLDS: [sp_npos_elections::VoteWeight; 9] = parameter_types! { pub static BagThresholds: &'static [sp_npos_elections::VoteWeight] = &THRESHOLDS; pub static MaxNominations: u32 = 16; - pub static HistoryDepth: u32 = 84; + pub static HistoryDepth: u32 = 80; pub static RewardOnUnbalanceWasCalled: bool = false; pub static LedgerSlashPerEra: (BalanceOf, BTreeMap>) = (Zero::zero(), BTreeMap::new()); } diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 6690b30c6ed76..c6b395132e2b3 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -3613,7 +3613,11 @@ fn test_payout_stakers() { }) ); - for i in 16..100 { + let last_era = 99; + let history_depth = HistoryDepth::get(); + let expected_last_reward_era = last_era - 1; + let expected_start_reward_era = last_era - history_depth; + for i in 16..=last_era { Staking::reward_by_ids(vec![(11, 1)]); // compute and ensure the reward amount is greater than zero. let _ = current_total_payout_for_duration(reward_time_per_era()); @@ -3621,8 +3625,8 @@ fn test_payout_stakers() { } // We clean it up as history passes - assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 15)); - assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 98)); + assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, expected_start_reward_era)); + assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, expected_last_reward_era)); assert_eq!( Staking::ledger(&10), Some(StakingLedger { @@ -3630,7 +3634,7 @@ fn test_payout_stakers() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: bounded_vec![15, 98] + claimed_rewards: bounded_vec![expected_start_reward_era, expected_last_reward_era] }) ); @@ -3645,7 +3649,13 @@ fn test_payout_stakers() { total: 1000, active: 1000, unlocking: Default::default(), - claimed_rewards: bounded_vec![15, 23, 42, 69, 98] + claimed_rewards: bounded_vec![ + expected_start_reward_era, + 23, + 42, + 69, + expected_last_reward_era + ] }) ); }); @@ -3686,32 +3696,39 @@ fn payout_stakers_handles_basic_errors() { Error::::NotStash.with_weight(err_weight) ); - for i in 3..100 { + let last_era = 99; + for i in 3..=last_era { Staking::reward_by_ids(vec![(11, 1)]); // compute and ensure the reward amount is greater than zero. let _ = current_total_payout_for_duration(reward_time_per_era()); mock::start_active_era(i); } - // We are at era 99, with history depth of 84 - // We should be able to payout era 15 through 98 (84 total eras), but not 14 or 99. + + let history_depth = HistoryDepth::get(); + let expected_last_reward_era = last_era - 1; + let expected_start_reward_era = last_era - history_depth; + + // We are at era last_era=99. Given history_depth=80, we should be able + // to payout era starting from expected_start_reward_era=19 through + // expected_last_reward_era=98 (80 total eras), but not 18 or 99. assert_noop!( - Staking::payout_stakers(Origin::signed(1337), 11, 14), + Staking::payout_stakers(Origin::signed(1337), 11, expected_start_reward_era - 1), Error::::InvalidEraToReward.with_weight(err_weight) ); assert_noop!( - Staking::payout_stakers(Origin::signed(1337), 11, 99), + Staking::payout_stakers(Origin::signed(1337), 11, expected_last_reward_era + 1), Error::::InvalidEraToReward.with_weight(err_weight) ); - assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 15)); - assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 98)); + assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, expected_start_reward_era)); + assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, expected_last_reward_era)); // Can't claim again assert_noop!( - Staking::payout_stakers(Origin::signed(1337), 11, 15), + Staking::payout_stakers(Origin::signed(1337), 11, expected_start_reward_era), Error::::AlreadyClaimed.with_weight(err_weight) ); assert_noop!( - Staking::payout_stakers(Origin::signed(1337), 11, 98), + Staking::payout_stakers(Origin::signed(1337), 11, expected_last_reward_era), Error::::AlreadyClaimed.with_weight(err_weight) ); }); From 420133a11483e4144a04f6139eecf49f35193c96 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 13 Sep 2022 15:46:04 +0200 Subject: [PATCH 17/36] verify previous eras cannot be claimed --- frame/staking/src/tests.rs | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index c6b395132e2b3..111e072a571b9 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -5292,3 +5292,60 @@ fn proportional_ledger_slash_works() { BTreeMap::from([(4, 0), (5, value_slashed), (6, 0), (7, 0)]) ); } + +#[test] +fn pre_bonding_era_cannot_be_claimed() { + // Verifies initial conditions of mock + ExtBuilder::default().nominate(false).build_and_execute(|| { + let history_depth = HistoryDepth::get(); + // jump to some era above history_depth + let mut current_era = history_depth + 10; + let last_reward_era = current_era - 1; + let start_reward_era = current_era - history_depth; + + // put some money in stash=3 and controller=4. + for i in 3..5 { + let _ = Balances::make_free_balance_be(&i, 2000); + } + + mock::start_active_era(current_era); + + // add a new candidate for being a validator. account 3 controlled by 4. + assert_ok!(Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller)); + + // all previous era before the bonding action should be marked as + // claimed. + let mut claimed_rewards = vec![]; + for i in start_reward_era..=last_reward_era { + claimed_rewards.push(i); + } + + let claimed_rewards: BoundedVec<_, _> = claimed_rewards.try_into().unwrap(); + assert_eq!( + Staking::ledger(&4).unwrap(), + StakingLedger { + stash: 3, + total: 1500, + active: 1500, + unlocking: Default::default(), + claimed_rewards, + } + ); + + // start next era + current_era = current_era + 1; + mock::start_active_era(current_era); + + // claiming reward for last era in which validator was active works + assert_ok!(Staking::payout_stakers(Origin::signed(4), 3, current_era - 1)); + + // consumed weight for all payout_stakers dispatches that fail + let err_weight = weights::SubstrateWeight::::payout_stakers_alive_staked(0); + // cannot claim rewards for an era before bonding occured as it is + // already marked as claimed. + assert_noop!( + Staking::payout_stakers(Origin::signed(4), 3, current_era - 2), + Error::::AlreadyClaimed.with_weight(err_weight) + ); + }); +} From f43fe8c79369e54e8a7f3d3d1dab181e5dc9b106 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Mon, 19 Sep 2022 02:17:25 +0200 Subject: [PATCH 18/36] add migration v12 --- frame/staking/src/lib.rs | 1 + frame/staking/src/migrations.rs | 68 +++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 261c4ddc333c0..8aeefe503d96e 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -898,6 +898,7 @@ enum Releases { V9_0_0, // inject validators into `VoterList` as well. V10_0_0, // remove `EarliestUnappliedSlash`. V11_0_0, // Move pallet storage prefix, e.g. BagsList -> VoterBagsList + V12_0_0, // remove `HistoryDepth` } impl Default for Releases { diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index d1563e3eb1650..f8836fd5ac811 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -20,6 +20,59 @@ use super::*; use frame_election_provider_support::SortedListProvider; use frame_support::traits::OnRuntimeUpgrade; +pub mod v12 { + use super::*; + use frame_support::{pallet_prelude::ValueQuery, storage_alias}; + + #[storage_alias] + type HistoryDepth = StorageValue, u32, ValueQuery>; + + /// Clean up `HistoryDepth` from storage + /// + /// We will be depending on the configurable value of `HistoryDepth` post + /// this release. + pub struct MigrateToV12(sp_std::marker::PhantomData); + impl OnRuntimeUpgrade for MigrateToV12 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result<(), &'static str> { + use frame_support::traits::OnRuntimeUpgradeHelpersExt; + frame_support::ensure!( + StorageVersion::::get() == Releases::V11_0_0, + "Expected v11 before upgrading to v12" + ); + + frame_support::ensure!( + T::HistoryDepth::get() == HistoryDepth::::get(), + "Provided value of HistoryDepth should be same as the existing storage value" + ); + + Ok(()) + } + + fn on_runtime_upgrade() -> frame_support::weights::Weight { + if StorageVersion::::get() == Releases::V11_0_0 { + HistoryDepth::::kill(); + StorageVersion::::put(Releases::V12_0_0); + + log!(info, "v12 applied successfully"); + T::DbWeight::get().reads_writes(1, 2) + } else { + log!(warn, "Skipping v12, should be removed"); + T::DbWeight::get().reads(1) + } + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade() -> Result<(), &'static str> { + frame_support::ensure!( + StorageVersion::::get() == crate::Releases::V12_0_0, + "v12 not applied" + ); + Ok(()) + } + } +} + pub mod v11 { use super::*; use frame_support::{ @@ -78,21 +131,6 @@ pub mod v11 { log!(warn, "v11::migrate should be removed."); T::DbWeight::get().reads(1) } - - let old_pallet_prefix = twox_128(N::get().as_bytes()); - frame_support::ensure!( - sp_io::storage::next_key(&old_pallet_prefix).is_none(), - "old pallet data hasn't been removed" - ); - - let new_pallet_name =

::name(); - let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); - frame_support::ensure!( - sp_io::storage::next_key(&new_pallet_prefix).is_some(), - "new pallet data hasn't been created" - ); - - Ok(()) } #[cfg(feature = "try-runtime")] From fc63040a1ba52053ce4f21af0eff1b95692424e1 Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Mon, 19 Sep 2022 02:08:25 +0000 Subject: [PATCH 19/36] ".git/.scripts/bench-bot.sh" pallet dev pallet_staking --- frame/staking/src/weights.rs | 454 +++++++++++++++++------------------ 1 file changed, 227 insertions(+), 227 deletions(-) diff --git a/frame/staking/src/weights.rs b/frame/staking/src/weights.rs index 8b402bbc1acad..5070232365d3e 100644 --- a/frame/staking/src/weights.rs +++ b/frame/staking/src/weights.rs @@ -18,7 +18,7 @@ //! Autogenerated weights for pallet_staking //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-09-12, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-09-19, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! HOSTNAME: `bm3`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 @@ -86,17 +86,17 @@ impl WeightInfo for SubstrateWeight { // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) fn bond() -> Weight { - Weight::from_ref_time(52_087_000 as u64) + Weight::from_ref_time(52_281_000 as u64) .saturating_add(T::DbWeight::get().reads(4 as u64)) .saturating_add(T::DbWeight::get().writes(4 as u64)) } // Storage: Staking Bonded (r:1 w:0) // Storage: Staking Ledger (r:1 w:1) // Storage: Balances Locks (r:1 w:1) - // Storage: BagsList ListNodes (r:3 w:3) - // Storage: BagsList ListBags (r:2 w:2) + // Storage: VoterBagsList ListNodes (r:3 w:3) + // Storage: VoterBagsList ListBags (r:2 w:2) fn bond_extra() -> Weight { - Weight::from_ref_time(90_109_000 as u64) + Weight::from_ref_time(89_748_000 as u64) .saturating_add(T::DbWeight::get().reads(8 as u64)) .saturating_add(T::DbWeight::get().writes(7 as u64)) } @@ -106,11 +106,11 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking CurrentEra (r:1 w:0) // Storage: Balances Locks (r:1 w:1) // Storage: System Account (r:1 w:1) - // Storage: BagsList ListNodes (r:3 w:3) + // Storage: VoterBagsList ListNodes (r:3 w:3) // Storage: Staking Bonded (r:1 w:0) - // Storage: BagsList ListBags (r:2 w:2) + // Storage: VoterBagsList ListBags (r:2 w:2) fn unbond() -> Weight { - Weight::from_ref_time(95_758_000 as u64) + Weight::from_ref_time(94_782_000 as u64) .saturating_add(T::DbWeight::get().reads(12 as u64)) .saturating_add(T::DbWeight::get().writes(8 as u64)) } @@ -120,9 +120,9 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) /// The range of component `s` is `[0, 100]`. fn withdraw_unbonded_update(s: u32, ) -> Weight { - Weight::from_ref_time(45_405_000 as u64) - // Standard Error: 0 - .saturating_add(Weight::from_ref_time(62_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(44_499_000 as u64) + // Standard Error: 387 + .saturating_add(Weight::from_ref_time(72_726 as u64).saturating_mul(s as u64)) .saturating_add(T::DbWeight::get().reads(4 as u64)) .saturating_add(T::DbWeight::get().writes(3 as u64)) } @@ -133,15 +133,17 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking Validators (r:1 w:0) // Storage: Staking Nominators (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) // Storage: System Account (r:1 w:1) // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) /// The range of component `s` is `[0, 100]`. - fn withdraw_unbonded_kill(_s: u32, ) -> Weight { - Weight::from_ref_time(83_703_000 as u64) + fn withdraw_unbonded_kill(s: u32, ) -> Weight { + Weight::from_ref_time(83_230_000 as u64) + // Standard Error: 612 + .saturating_add(Weight::from_ref_time(10_289 as u64).saturating_mul(s as u64)) .saturating_add(T::DbWeight::get().reads(13 as u64)) .saturating_add(T::DbWeight::get().writes(11 as u64)) } @@ -152,12 +154,12 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking MaxValidatorsCount (r:1 w:0) // Storage: Staking Nominators (r:1 w:0) // Storage: Staking Bonded (r:1 w:0) - // Storage: BagsList ListNodes (r:1 w:1) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:1 w:1) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) // Storage: Staking CounterForValidators (r:1 w:1) fn validate() -> Weight { - Weight::from_ref_time(64_456_000 as u64) + Weight::from_ref_time(64_430_000 as u64) .saturating_add(T::DbWeight::get().reads(11 as u64)) .saturating_add(T::DbWeight::get().writes(5 as u64)) } @@ -165,11 +167,12 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking Nominators (r:1 w:1) /// The range of component `k` is `[1, 128]`. fn kick(k: u32, ) -> Weight { - Weight::from_ref_time(44_203_000 as u64) - // Standard Error: 13_000 - .saturating_add(Weight::from_ref_time(6_582_000 as u64).saturating_mul(k as u64)) - .saturating_add(T::DbWeight::get().reads(1 as u64)) + Weight::from_ref_time(42_030_000 as u64) + // Standard Error: 5_873 + .saturating_add(Weight::from_ref_time(6_747_156 as u64).saturating_mul(k as u64)) + .saturating_add(T::DbWeight::get().reads(2 as u64)) .saturating_add(T::DbWeight::get().reads((1 as u64).saturating_mul(k as u64))) + .saturating_add(T::DbWeight::get().writes(1 as u64)) .saturating_add(T::DbWeight::get().writes((1 as u64).saturating_mul(k as u64))) } // Storage: Staking Ledger (r:1 w:0) @@ -179,16 +182,16 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking Validators (r:2 w:0) // Storage: Staking CurrentEra (r:1 w:0) // Storage: Staking Bonded (r:1 w:0) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) /// The range of component `n` is `[1, 16]`. fn nominate(n: u32, ) -> Weight { - Weight::from_ref_time(69_707_000 as u64) - // Standard Error: 6_000 - .saturating_add(Weight::from_ref_time(2_736_000 as u64).saturating_mul(n as u64)) - .saturating_add(T::DbWeight::get().reads(12 as u64)) + Weight::from_ref_time(70_834_000 as u64) + // Standard Error: 4_405 + .saturating_add(Weight::from_ref_time(2_583_120 as u64).saturating_mul(n as u64)) + .saturating_add(T::DbWeight::get().reads(13 as u64)) .saturating_add(T::DbWeight::get().reads((1 as u64).saturating_mul(n as u64))) .saturating_add(T::DbWeight::get().writes(6 as u64)) } @@ -196,54 +199,54 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking Validators (r:1 w:0) // Storage: Staking Nominators (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) fn chill() -> Weight { - Weight::from_ref_time(63_314_000 as u64) + Weight::from_ref_time(63_328_000 as u64) .saturating_add(T::DbWeight::get().reads(8 as u64)) .saturating_add(T::DbWeight::get().writes(6 as u64)) } // Storage: Staking Ledger (r:1 w:0) // Storage: Staking Payee (r:0 w:1) fn set_payee() -> Weight { - Weight::from_ref_time(17_994_000 as u64) + Weight::from_ref_time(17_763_000 as u64) .saturating_add(T::DbWeight::get().reads(1 as u64)) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking Bonded (r:1 w:1) // Storage: Staking Ledger (r:2 w:2) fn set_controller() -> Weight { - Weight::from_ref_time(25_821_000 as u64) + Weight::from_ref_time(26_163_000 as u64) .saturating_add(T::DbWeight::get().reads(3 as u64)) .saturating_add(T::DbWeight::get().writes(3 as u64)) } // Storage: Staking ValidatorCount (r:0 w:1) fn set_validator_count() -> Weight { - Weight::from_ref_time(4_775_000 as u64) + Weight::from_ref_time(4_842_000 as u64) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_no_eras() -> Weight { - Weight::from_ref_time(4_901_000 as u64) + Weight::from_ref_time(4_974_000 as u64) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_new_era() -> Weight { - Weight::from_ref_time(4_982_000 as u64) + Weight::from_ref_time(5_039_000 as u64) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_new_era_always() -> Weight { - Weight::from_ref_time(5_036_000 as u64) + Weight::from_ref_time(4_950_000 as u64) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking Invulnerables (r:0 w:1) /// The range of component `v` is `[0, 1000]`. fn set_invulnerables(v: u32, ) -> Weight { - Weight::from_ref_time(5_771_000 as u64) - // Standard Error: 0 - .saturating_add(Weight::from_ref_time(10_000 as u64).saturating_mul(v as u64)) + Weight::from_ref_time(5_150_000 as u64) + // Standard Error: 30 + .saturating_add(Weight::from_ref_time(10_540 as u64).saturating_mul(v as u64)) .saturating_add(T::DbWeight::get().writes(1 as u64)) } // Storage: Staking Bonded (r:1 w:1) @@ -251,9 +254,9 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking Validators (r:1 w:0) // Storage: Staking Nominators (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) // Storage: System Account (r:1 w:1) // Storage: Balances Locks (r:1 w:1) // Storage: Staking Ledger (r:0 w:1) @@ -261,19 +264,19 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking SpanSlash (r:0 w:2) /// The range of component `s` is `[0, 100]`. fn force_unstake(s: u32, ) -> Weight { - Weight::from_ref_time(81_408_000 as u64) - // Standard Error: 2_000 - .saturating_add(Weight::from_ref_time(1_052_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(76_282_000 as u64) + // Standard Error: 2_397 + .saturating_add(Weight::from_ref_time(1_137_934 as u64).saturating_mul(s as u64)) .saturating_add(T::DbWeight::get().reads(11 as u64)) - .saturating_add(T::DbWeight::get().writes(12 as u64)) + .saturating_add(T::DbWeight::get().writes(11 as u64)) .saturating_add(T::DbWeight::get().writes((1 as u64).saturating_mul(s as u64))) } // Storage: Staking UnappliedSlashes (r:1 w:1) /// The range of component `s` is `[1, 1000]`. fn cancel_deferred_slash(s: u32, ) -> Weight { - Weight::from_ref_time(900_716_000 as u64) - // Standard Error: 59_000 - .saturating_add(Weight::from_ref_time(4_937_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(93_690_000 as u64) + // Standard Error: 43_203 + .saturating_add(Weight::from_ref_time(6_149_862 as u64).saturating_mul(s as u64)) .saturating_add(T::DbWeight::get().reads(1 as u64)) .saturating_add(T::DbWeight::get().writes(1 as u64)) } @@ -288,12 +291,12 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:2 w:2) /// The range of component `n` is `[1, 256]`. fn payout_stakers_dead_controller(n: u32, ) -> Weight { - Weight::from_ref_time(184_408_000 as u64) - // Standard Error: 16_000 - .saturating_add(Weight::from_ref_time(21_099_000 as u64).saturating_mul(n as u64)) - .saturating_add(T::DbWeight::get().reads(9 as u64)) + Weight::from_ref_time(151_647_000 as u64) + // Standard Error: 8_237 + .saturating_add(Weight::from_ref_time(21_296_415 as u64).saturating_mul(n as u64)) + .saturating_add(T::DbWeight::get().reads(12 as u64)) .saturating_add(T::DbWeight::get().reads((3 as u64).saturating_mul(n as u64))) - .saturating_add(T::DbWeight::get().writes(2 as u64)) + .saturating_add(T::DbWeight::get().writes(3 as u64)) .saturating_add(T::DbWeight::get().writes((1 as u64).saturating_mul(n as u64))) } // Storage: Staking CurrentEra (r:1 w:0) @@ -308,25 +311,25 @@ impl WeightInfo for SubstrateWeight { // Storage: Balances Locks (r:2 w:2) /// The range of component `n` is `[1, 256]`. fn payout_stakers_alive_staked(n: u32, ) -> Weight { - Weight::from_ref_time(213_358_000 as u64) - // Standard Error: 26_000 - .saturating_add(Weight::from_ref_time(29_938_000 as u64).saturating_mul(n as u64)) - .saturating_add(T::DbWeight::get().reads(10 as u64)) + Weight::from_ref_time(197_310_000 as u64) + // Standard Error: 13_818 + .saturating_add(Weight::from_ref_time(30_053_043 as u64).saturating_mul(n as u64)) + .saturating_add(T::DbWeight::get().reads(15 as u64)) .saturating_add(T::DbWeight::get().reads((5 as u64).saturating_mul(n as u64))) - .saturating_add(T::DbWeight::get().writes(3 as u64)) + .saturating_add(T::DbWeight::get().writes(6 as u64)) .saturating_add(T::DbWeight::get().writes((3 as u64).saturating_mul(n as u64))) } // Storage: Staking Ledger (r:1 w:1) // Storage: Balances Locks (r:1 w:1) // Storage: System Account (r:1 w:1) - // Storage: BagsList ListNodes (r:3 w:3) + // Storage: VoterBagsList ListNodes (r:3 w:3) // Storage: Staking Bonded (r:1 w:0) - // Storage: BagsList ListBags (r:2 w:2) + // Storage: VoterBagsList ListBags (r:2 w:2) /// The range of component `l` is `[1, 32]`. fn rebond(l: u32, ) -> Weight { - Weight::from_ref_time(90_805_000 as u64) - // Standard Error: 4_000 - .saturating_add(Weight::from_ref_time(46_000 as u64).saturating_mul(l as u64)) + Weight::from_ref_time(89_469_000 as u64) + // Standard Error: 1_197 + .saturating_add(Weight::from_ref_time(74_212 as u64).saturating_mul(l as u64)) .saturating_add(T::DbWeight::get().reads(9 as u64)) .saturating_add(T::DbWeight::get().writes(8 as u64)) } @@ -337,25 +340,25 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking Validators (r:1 w:0) // Storage: Staking Nominators (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) // Storage: Staking SpanSlash (r:0 w:1) /// The range of component `s` is `[1, 100]`. fn reap_stash(s: u32, ) -> Weight { - Weight::from_ref_time(89_762_000 as u64) - // Standard Error: 1_000 - .saturating_add(Weight::from_ref_time(1_035_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(88_333_000 as u64) + // Standard Error: 1_567 + .saturating_add(Weight::from_ref_time(1_101_099 as u64).saturating_mul(s as u64)) .saturating_add(T::DbWeight::get().reads(12 as u64)) - .saturating_add(T::DbWeight::get().writes(12 as u64)) + .saturating_add(T::DbWeight::get().writes(13 as u64)) .saturating_add(T::DbWeight::get().writes((1 as u64).saturating_mul(s as u64))) } - // Storage: BagsList CounterForListNodes (r:1 w:0) + // Storage: VoterBagsList CounterForListNodes (r:1 w:0) // Storage: Staking SlashingSpans (r:1 w:0) - // Storage: BagsList ListBags (r:200 w:0) - // Storage: BagsList ListNodes (r:101 w:0) + // Storage: VoterBagsList ListBags (r:200 w:0) + // Storage: VoterBagsList ListNodes (r:101 w:0) // Storage: Staking Nominators (r:101 w:0) // Storage: Staking Validators (r:2 w:0) // Storage: Staking Bonded (r:101 w:0) @@ -372,21 +375,21 @@ impl WeightInfo for SubstrateWeight { /// The range of component `v` is `[1, 10]`. /// The range of component `n` is `[1, 100]`. fn new_era(v: u32, n: u32, ) -> Weight { - Weight::from_ref_time(0 as u64) - // Standard Error: 1_222_000 - .saturating_add(Weight::from_ref_time(298_086_000 as u64).saturating_mul(v as u64)) - // Standard Error: 117_000 - .saturating_add(Weight::from_ref_time(34_215_000 as u64).saturating_mul(n as u64)) - .saturating_add(T::DbWeight::get().reads(207 as u64)) - .saturating_add(T::DbWeight::get().reads((5 as u64).saturating_mul(v as u64))) + Weight::from_ref_time(572_530_000 as u64) + // Standard Error: 3_166_542 + .saturating_add(Weight::from_ref_time(101_354_358 as u64).saturating_mul(v as u64)) + // Standard Error: 315_238 + .saturating_add(Weight::from_ref_time(15_565_319 as u64).saturating_mul(n as u64)) + .saturating_add(T::DbWeight::get().reads(261 as u64)) + .saturating_add(T::DbWeight::get().reads((1 as u64).saturating_mul(v as u64))) .saturating_add(T::DbWeight::get().reads((4 as u64).saturating_mul(n as u64))) - .saturating_add(T::DbWeight::get().writes(3 as u64)) + .saturating_add(T::DbWeight::get().writes(6 as u64)) .saturating_add(T::DbWeight::get().writes((3 as u64).saturating_mul(v as u64))) } - // Storage: BagsList CounterForListNodes (r:1 w:0) + // Storage: VoterBagsList CounterForListNodes (r:1 w:0) // Storage: Staking SlashingSpans (r:21 w:0) - // Storage: BagsList ListBags (r:200 w:0) - // Storage: BagsList ListNodes (r:1500 w:0) + // Storage: VoterBagsList ListBags (r:200 w:0) + // Storage: VoterBagsList ListNodes (r:1500 w:0) // Storage: Staking Nominators (r:1500 w:0) // Storage: Staking Validators (r:500 w:0) // Storage: Staking Bonded (r:1500 w:0) @@ -394,27 +397,24 @@ impl WeightInfo for SubstrateWeight { /// The range of component `v` is `[500, 1000]`. /// The range of component `n` is `[500, 1000]`. /// The range of component `s` is `[1, 20]`. - fn get_npos_voters(v: u32, n: u32, s: u32, ) -> Weight { - Weight::from_ref_time(0 as u64) - // Standard Error: 95_000 - .saturating_add(Weight::from_ref_time(22_872_000 as u64).saturating_mul(v as u64)) - // Standard Error: 95_000 - .saturating_add(Weight::from_ref_time(22_964_000 as u64).saturating_mul(n as u64)) - // Standard Error: 2_435_000 - .saturating_add(Weight::from_ref_time(37_949_000 as u64).saturating_mul(s as u64)) - .saturating_add(T::DbWeight::get().reads(202 as u64)) - .saturating_add(T::DbWeight::get().reads((5 as u64).saturating_mul(v as u64))) - .saturating_add(T::DbWeight::get().reads((4 as u64).saturating_mul(n as u64))) - .saturating_add(T::DbWeight::get().reads((1 as u64).saturating_mul(s as u64))) + fn get_npos_voters(v: u32, n: u32, _s: u32, ) -> Weight { + Weight::from_ref_time(24_930_788_000 as u64) + // Standard Error: 266_386 + .saturating_add(Weight::from_ref_time(6_687_552 as u64).saturating_mul(v as u64)) + // Standard Error: 266_386 + .saturating_add(Weight::from_ref_time(6_839_134 as u64).saturating_mul(n as u64)) + .saturating_add(T::DbWeight::get().reads(6722 as u64)) + .saturating_add(T::DbWeight::get().reads((2 as u64).saturating_mul(v as u64))) + .saturating_add(T::DbWeight::get().reads((1 as u64).saturating_mul(n as u64))) } + // Storage: Staking CounterForValidators (r:1 w:0) // Storage: Staking Validators (r:501 w:0) /// The range of component `v` is `[500, 1000]`. fn get_npos_targets(v: u32, ) -> Weight { - Weight::from_ref_time(115_076_000 as u64) - // Standard Error: 25_000 - .saturating_add(Weight::from_ref_time(8_629_000 as u64).saturating_mul(v as u64)) - .saturating_add(T::DbWeight::get().reads(1 as u64)) - .saturating_add(T::DbWeight::get().reads((1 as u64).saturating_mul(v as u64))) + Weight::from_ref_time(4_864_727_000 as u64) + // Standard Error: 54_240 + .saturating_add(Weight::from_ref_time(3_319_738 as u64).saturating_mul(v as u64)) + .saturating_add(T::DbWeight::get().reads(502 as u64)) } // Storage: Staking MinCommission (r:0 w:1) // Storage: Staking MinValidatorBond (r:0 w:1) @@ -423,7 +423,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking MaxNominatorsCount (r:0 w:1) // Storage: Staking MinNominatorBond (r:0 w:1) fn set_staking_configs_all_set() -> Weight { - Weight::from_ref_time(9_775_000 as u64) + Weight::from_ref_time(10_141_000 as u64) .saturating_add(T::DbWeight::get().writes(6 as u64)) } // Storage: Staking MinCommission (r:0 w:1) @@ -433,7 +433,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking MaxNominatorsCount (r:0 w:1) // Storage: Staking MinNominatorBond (r:0 w:1) fn set_staking_configs_all_remove() -> Weight { - Weight::from_ref_time(9_042_000 as u64) + Weight::from_ref_time(8_993_000 as u64) .saturating_add(T::DbWeight::get().writes(6 as u64)) } // Storage: Staking Ledger (r:1 w:0) @@ -443,18 +443,18 @@ impl WeightInfo for SubstrateWeight { // Storage: Staking CounterForNominators (r:1 w:1) // Storage: Staking MinNominatorBond (r:1 w:0) // Storage: Staking Validators (r:1 w:0) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) fn chill_other() -> Weight { - Weight::from_ref_time(79_225_000 as u64) + Weight::from_ref_time(79_082_000 as u64) .saturating_add(T::DbWeight::get().reads(11 as u64)) .saturating_add(T::DbWeight::get().writes(6 as u64)) } // Storage: Staking MinCommission (r:1 w:0) // Storage: Staking Validators (r:1 w:1) fn force_apply_min_commission() -> Weight { - Weight::from_ref_time(19_293_000 as u64) + Weight::from_ref_time(19_265_000 as u64) .saturating_add(T::DbWeight::get().reads(2 as u64)) .saturating_add(T::DbWeight::get().writes(1 as u64)) } @@ -468,17 +468,17 @@ impl WeightInfo for () { // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) fn bond() -> Weight { - Weight::from_ref_time(52_087_000 as u64) + Weight::from_ref_time(52_281_000 as u64) .saturating_add(RocksDbWeight::get().reads(4 as u64)) .saturating_add(RocksDbWeight::get().writes(4 as u64)) } // Storage: Staking Bonded (r:1 w:0) // Storage: Staking Ledger (r:1 w:1) // Storage: Balances Locks (r:1 w:1) - // Storage: BagsList ListNodes (r:3 w:3) - // Storage: BagsList ListBags (r:2 w:2) + // Storage: VoterBagsList ListNodes (r:3 w:3) + // Storage: VoterBagsList ListBags (r:2 w:2) fn bond_extra() -> Weight { - Weight::from_ref_time(90_109_000 as u64) + Weight::from_ref_time(89_748_000 as u64) .saturating_add(RocksDbWeight::get().reads(8 as u64)) .saturating_add(RocksDbWeight::get().writes(7 as u64)) } @@ -488,11 +488,11 @@ impl WeightInfo for () { // Storage: Staking CurrentEra (r:1 w:0) // Storage: Balances Locks (r:1 w:1) // Storage: System Account (r:1 w:1) - // Storage: BagsList ListNodes (r:3 w:3) + // Storage: VoterBagsList ListNodes (r:3 w:3) // Storage: Staking Bonded (r:1 w:0) - // Storage: BagsList ListBags (r:2 w:2) + // Storage: VoterBagsList ListBags (r:2 w:2) fn unbond() -> Weight { - Weight::from_ref_time(95_758_000 as u64) + Weight::from_ref_time(94_782_000 as u64) .saturating_add(RocksDbWeight::get().reads(12 as u64)) .saturating_add(RocksDbWeight::get().writes(8 as u64)) } @@ -502,9 +502,9 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) /// The range of component `s` is `[0, 100]`. fn withdraw_unbonded_update(s: u32, ) -> Weight { - Weight::from_ref_time(45_405_000 as u64) - // Standard Error: 0 - .saturating_add(Weight::from_ref_time(62_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(44_499_000 as u64) + // Standard Error: 387 + .saturating_add(Weight::from_ref_time(72_726 as u64).saturating_mul(s as u64)) .saturating_add(RocksDbWeight::get().reads(4 as u64)) .saturating_add(RocksDbWeight::get().writes(3 as u64)) } @@ -515,15 +515,17 @@ impl WeightInfo for () { // Storage: Staking Validators (r:1 w:0) // Storage: Staking Nominators (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) // Storage: System Account (r:1 w:1) // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) /// The range of component `s` is `[0, 100]`. - fn withdraw_unbonded_kill(_s: u32, ) -> Weight { - Weight::from_ref_time(83_703_000 as u64) + fn withdraw_unbonded_kill(s: u32, ) -> Weight { + Weight::from_ref_time(83_230_000 as u64) + // Standard Error: 612 + .saturating_add(Weight::from_ref_time(10_289 as u64).saturating_mul(s as u64)) .saturating_add(RocksDbWeight::get().reads(13 as u64)) .saturating_add(RocksDbWeight::get().writes(11 as u64)) } @@ -534,12 +536,12 @@ impl WeightInfo for () { // Storage: Staking MaxValidatorsCount (r:1 w:0) // Storage: Staking Nominators (r:1 w:0) // Storage: Staking Bonded (r:1 w:0) - // Storage: BagsList ListNodes (r:1 w:1) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:1 w:1) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) // Storage: Staking CounterForValidators (r:1 w:1) fn validate() -> Weight { - Weight::from_ref_time(64_456_000 as u64) + Weight::from_ref_time(64_430_000 as u64) .saturating_add(RocksDbWeight::get().reads(11 as u64)) .saturating_add(RocksDbWeight::get().writes(5 as u64)) } @@ -547,11 +549,12 @@ impl WeightInfo for () { // Storage: Staking Nominators (r:1 w:1) /// The range of component `k` is `[1, 128]`. fn kick(k: u32, ) -> Weight { - Weight::from_ref_time(44_203_000 as u64) - // Standard Error: 13_000 - .saturating_add(Weight::from_ref_time(6_582_000 as u64).saturating_mul(k as u64)) - .saturating_add(RocksDbWeight::get().reads(1 as u64)) + Weight::from_ref_time(42_030_000 as u64) + // Standard Error: 5_873 + .saturating_add(Weight::from_ref_time(6_747_156 as u64).saturating_mul(k as u64)) + .saturating_add(RocksDbWeight::get().reads(2 as u64)) .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(k as u64))) + .saturating_add(RocksDbWeight::get().writes(1 as u64)) .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(k as u64))) } // Storage: Staking Ledger (r:1 w:0) @@ -561,16 +564,16 @@ impl WeightInfo for () { // Storage: Staking Validators (r:2 w:0) // Storage: Staking CurrentEra (r:1 w:0) // Storage: Staking Bonded (r:1 w:0) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) /// The range of component `n` is `[1, 16]`. fn nominate(n: u32, ) -> Weight { - Weight::from_ref_time(69_707_000 as u64) - // Standard Error: 6_000 - .saturating_add(Weight::from_ref_time(2_736_000 as u64).saturating_mul(n as u64)) - .saturating_add(RocksDbWeight::get().reads(12 as u64)) + Weight::from_ref_time(70_834_000 as u64) + // Standard Error: 4_405 + .saturating_add(Weight::from_ref_time(2_583_120 as u64).saturating_mul(n as u64)) + .saturating_add(RocksDbWeight::get().reads(13 as u64)) .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(n as u64))) .saturating_add(RocksDbWeight::get().writes(6 as u64)) } @@ -578,54 +581,54 @@ impl WeightInfo for () { // Storage: Staking Validators (r:1 w:0) // Storage: Staking Nominators (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) fn chill() -> Weight { - Weight::from_ref_time(63_314_000 as u64) + Weight::from_ref_time(63_328_000 as u64) .saturating_add(RocksDbWeight::get().reads(8 as u64)) .saturating_add(RocksDbWeight::get().writes(6 as u64)) } // Storage: Staking Ledger (r:1 w:0) // Storage: Staking Payee (r:0 w:1) fn set_payee() -> Weight { - Weight::from_ref_time(17_994_000 as u64) + Weight::from_ref_time(17_763_000 as u64) .saturating_add(RocksDbWeight::get().reads(1 as u64)) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking Bonded (r:1 w:1) // Storage: Staking Ledger (r:2 w:2) fn set_controller() -> Weight { - Weight::from_ref_time(25_821_000 as u64) + Weight::from_ref_time(26_163_000 as u64) .saturating_add(RocksDbWeight::get().reads(3 as u64)) .saturating_add(RocksDbWeight::get().writes(3 as u64)) } // Storage: Staking ValidatorCount (r:0 w:1) fn set_validator_count() -> Weight { - Weight::from_ref_time(4_775_000 as u64) + Weight::from_ref_time(4_842_000 as u64) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_no_eras() -> Weight { - Weight::from_ref_time(4_901_000 as u64) + Weight::from_ref_time(4_974_000 as u64) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_new_era() -> Weight { - Weight::from_ref_time(4_982_000 as u64) + Weight::from_ref_time(5_039_000 as u64) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking ForceEra (r:0 w:1) fn force_new_era_always() -> Weight { - Weight::from_ref_time(5_036_000 as u64) + Weight::from_ref_time(4_950_000 as u64) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking Invulnerables (r:0 w:1) /// The range of component `v` is `[0, 1000]`. fn set_invulnerables(v: u32, ) -> Weight { - Weight::from_ref_time(5_771_000 as u64) - // Standard Error: 0 - .saturating_add(Weight::from_ref_time(10_000 as u64).saturating_mul(v as u64)) + Weight::from_ref_time(5_150_000 as u64) + // Standard Error: 30 + .saturating_add(Weight::from_ref_time(10_540 as u64).saturating_mul(v as u64)) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } // Storage: Staking Bonded (r:1 w:1) @@ -633,9 +636,9 @@ impl WeightInfo for () { // Storage: Staking Validators (r:1 w:0) // Storage: Staking Nominators (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) // Storage: System Account (r:1 w:1) // Storage: Balances Locks (r:1 w:1) // Storage: Staking Ledger (r:0 w:1) @@ -643,19 +646,19 @@ impl WeightInfo for () { // Storage: Staking SpanSlash (r:0 w:2) /// The range of component `s` is `[0, 100]`. fn force_unstake(s: u32, ) -> Weight { - Weight::from_ref_time(81_408_000 as u64) - // Standard Error: 2_000 - .saturating_add(Weight::from_ref_time(1_052_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(76_282_000 as u64) + // Standard Error: 2_397 + .saturating_add(Weight::from_ref_time(1_137_934 as u64).saturating_mul(s as u64)) .saturating_add(RocksDbWeight::get().reads(11 as u64)) - .saturating_add(RocksDbWeight::get().writes(12 as u64)) + .saturating_add(RocksDbWeight::get().writes(11 as u64)) .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(s as u64))) } // Storage: Staking UnappliedSlashes (r:1 w:1) /// The range of component `s` is `[1, 1000]`. fn cancel_deferred_slash(s: u32, ) -> Weight { - Weight::from_ref_time(900_716_000 as u64) - // Standard Error: 59_000 - .saturating_add(Weight::from_ref_time(4_937_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(93_690_000 as u64) + // Standard Error: 43_203 + .saturating_add(Weight::from_ref_time(6_149_862 as u64).saturating_mul(s as u64)) .saturating_add(RocksDbWeight::get().reads(1 as u64)) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } @@ -670,12 +673,12 @@ impl WeightInfo for () { // Storage: System Account (r:2 w:2) /// The range of component `n` is `[1, 256]`. fn payout_stakers_dead_controller(n: u32, ) -> Weight { - Weight::from_ref_time(184_408_000 as u64) - // Standard Error: 16_000 - .saturating_add(Weight::from_ref_time(21_099_000 as u64).saturating_mul(n as u64)) - .saturating_add(RocksDbWeight::get().reads(9 as u64)) + Weight::from_ref_time(151_647_000 as u64) + // Standard Error: 8_237 + .saturating_add(Weight::from_ref_time(21_296_415 as u64).saturating_mul(n as u64)) + .saturating_add(RocksDbWeight::get().reads(12 as u64)) .saturating_add(RocksDbWeight::get().reads((3 as u64).saturating_mul(n as u64))) - .saturating_add(RocksDbWeight::get().writes(2 as u64)) + .saturating_add(RocksDbWeight::get().writes(3 as u64)) .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(n as u64))) } // Storage: Staking CurrentEra (r:1 w:0) @@ -690,25 +693,25 @@ impl WeightInfo for () { // Storage: Balances Locks (r:2 w:2) /// The range of component `n` is `[1, 256]`. fn payout_stakers_alive_staked(n: u32, ) -> Weight { - Weight::from_ref_time(213_358_000 as u64) - // Standard Error: 26_000 - .saturating_add(Weight::from_ref_time(29_938_000 as u64).saturating_mul(n as u64)) - .saturating_add(RocksDbWeight::get().reads(10 as u64)) + Weight::from_ref_time(197_310_000 as u64) + // Standard Error: 13_818 + .saturating_add(Weight::from_ref_time(30_053_043 as u64).saturating_mul(n as u64)) + .saturating_add(RocksDbWeight::get().reads(15 as u64)) .saturating_add(RocksDbWeight::get().reads((5 as u64).saturating_mul(n as u64))) - .saturating_add(RocksDbWeight::get().writes(3 as u64)) + .saturating_add(RocksDbWeight::get().writes(6 as u64)) .saturating_add(RocksDbWeight::get().writes((3 as u64).saturating_mul(n as u64))) } // Storage: Staking Ledger (r:1 w:1) // Storage: Balances Locks (r:1 w:1) // Storage: System Account (r:1 w:1) - // Storage: BagsList ListNodes (r:3 w:3) + // Storage: VoterBagsList ListNodes (r:3 w:3) // Storage: Staking Bonded (r:1 w:0) - // Storage: BagsList ListBags (r:2 w:2) + // Storage: VoterBagsList ListBags (r:2 w:2) /// The range of component `l` is `[1, 32]`. fn rebond(l: u32, ) -> Weight { - Weight::from_ref_time(90_805_000 as u64) - // Standard Error: 4_000 - .saturating_add(Weight::from_ref_time(46_000 as u64).saturating_mul(l as u64)) + Weight::from_ref_time(89_469_000 as u64) + // Standard Error: 1_197 + .saturating_add(Weight::from_ref_time(74_212 as u64).saturating_mul(l as u64)) .saturating_add(RocksDbWeight::get().reads(9 as u64)) .saturating_add(RocksDbWeight::get().writes(8 as u64)) } @@ -719,25 +722,25 @@ impl WeightInfo for () { // Storage: Staking Validators (r:1 w:0) // Storage: Staking Nominators (r:1 w:1) // Storage: Staking CounterForNominators (r:1 w:1) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) // Storage: Balances Locks (r:1 w:1) // Storage: Staking Payee (r:0 w:1) // Storage: Staking SpanSlash (r:0 w:1) /// The range of component `s` is `[1, 100]`. fn reap_stash(s: u32, ) -> Weight { - Weight::from_ref_time(89_762_000 as u64) - // Standard Error: 1_000 - .saturating_add(Weight::from_ref_time(1_035_000 as u64).saturating_mul(s as u64)) + Weight::from_ref_time(88_333_000 as u64) + // Standard Error: 1_567 + .saturating_add(Weight::from_ref_time(1_101_099 as u64).saturating_mul(s as u64)) .saturating_add(RocksDbWeight::get().reads(12 as u64)) - .saturating_add(RocksDbWeight::get().writes(12 as u64)) + .saturating_add(RocksDbWeight::get().writes(13 as u64)) .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(s as u64))) } - // Storage: BagsList CounterForListNodes (r:1 w:0) + // Storage: VoterBagsList CounterForListNodes (r:1 w:0) // Storage: Staking SlashingSpans (r:1 w:0) - // Storage: BagsList ListBags (r:200 w:0) - // Storage: BagsList ListNodes (r:101 w:0) + // Storage: VoterBagsList ListBags (r:200 w:0) + // Storage: VoterBagsList ListNodes (r:101 w:0) // Storage: Staking Nominators (r:101 w:0) // Storage: Staking Validators (r:2 w:0) // Storage: Staking Bonded (r:101 w:0) @@ -754,21 +757,21 @@ impl WeightInfo for () { /// The range of component `v` is `[1, 10]`. /// The range of component `n` is `[1, 100]`. fn new_era(v: u32, n: u32, ) -> Weight { - Weight::from_ref_time(0 as u64) - // Standard Error: 1_222_000 - .saturating_add(Weight::from_ref_time(298_086_000 as u64).saturating_mul(v as u64)) - // Standard Error: 117_000 - .saturating_add(Weight::from_ref_time(34_215_000 as u64).saturating_mul(n as u64)) - .saturating_add(RocksDbWeight::get().reads(207 as u64)) - .saturating_add(RocksDbWeight::get().reads((5 as u64).saturating_mul(v as u64))) + Weight::from_ref_time(572_530_000 as u64) + // Standard Error: 3_166_542 + .saturating_add(Weight::from_ref_time(101_354_358 as u64).saturating_mul(v as u64)) + // Standard Error: 315_238 + .saturating_add(Weight::from_ref_time(15_565_319 as u64).saturating_mul(n as u64)) + .saturating_add(RocksDbWeight::get().reads(261 as u64)) + .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(v as u64))) .saturating_add(RocksDbWeight::get().reads((4 as u64).saturating_mul(n as u64))) - .saturating_add(RocksDbWeight::get().writes(3 as u64)) + .saturating_add(RocksDbWeight::get().writes(6 as u64)) .saturating_add(RocksDbWeight::get().writes((3 as u64).saturating_mul(v as u64))) } - // Storage: BagsList CounterForListNodes (r:1 w:0) + // Storage: VoterBagsList CounterForListNodes (r:1 w:0) // Storage: Staking SlashingSpans (r:21 w:0) - // Storage: BagsList ListBags (r:200 w:0) - // Storage: BagsList ListNodes (r:1500 w:0) + // Storage: VoterBagsList ListBags (r:200 w:0) + // Storage: VoterBagsList ListNodes (r:1500 w:0) // Storage: Staking Nominators (r:1500 w:0) // Storage: Staking Validators (r:500 w:0) // Storage: Staking Bonded (r:1500 w:0) @@ -776,27 +779,24 @@ impl WeightInfo for () { /// The range of component `v` is `[500, 1000]`. /// The range of component `n` is `[500, 1000]`. /// The range of component `s` is `[1, 20]`. - fn get_npos_voters(v: u32, n: u32, s: u32, ) -> Weight { - Weight::from_ref_time(0 as u64) - // Standard Error: 95_000 - .saturating_add(Weight::from_ref_time(22_872_000 as u64).saturating_mul(v as u64)) - // Standard Error: 95_000 - .saturating_add(Weight::from_ref_time(22_964_000 as u64).saturating_mul(n as u64)) - // Standard Error: 2_435_000 - .saturating_add(Weight::from_ref_time(37_949_000 as u64).saturating_mul(s as u64)) - .saturating_add(RocksDbWeight::get().reads(202 as u64)) - .saturating_add(RocksDbWeight::get().reads((5 as u64).saturating_mul(v as u64))) - .saturating_add(RocksDbWeight::get().reads((4 as u64).saturating_mul(n as u64))) - .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(s as u64))) + fn get_npos_voters(v: u32, n: u32, _s: u32, ) -> Weight { + Weight::from_ref_time(24_930_788_000 as u64) + // Standard Error: 266_386 + .saturating_add(Weight::from_ref_time(6_687_552 as u64).saturating_mul(v as u64)) + // Standard Error: 266_386 + .saturating_add(Weight::from_ref_time(6_839_134 as u64).saturating_mul(n as u64)) + .saturating_add(RocksDbWeight::get().reads(6722 as u64)) + .saturating_add(RocksDbWeight::get().reads((2 as u64).saturating_mul(v as u64))) + .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(n as u64))) } + // Storage: Staking CounterForValidators (r:1 w:0) // Storage: Staking Validators (r:501 w:0) /// The range of component `v` is `[500, 1000]`. fn get_npos_targets(v: u32, ) -> Weight { - Weight::from_ref_time(115_076_000 as u64) - // Standard Error: 25_000 - .saturating_add(Weight::from_ref_time(8_629_000 as u64).saturating_mul(v as u64)) - .saturating_add(RocksDbWeight::get().reads(1 as u64)) - .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(v as u64))) + Weight::from_ref_time(4_864_727_000 as u64) + // Standard Error: 54_240 + .saturating_add(Weight::from_ref_time(3_319_738 as u64).saturating_mul(v as u64)) + .saturating_add(RocksDbWeight::get().reads(502 as u64)) } // Storage: Staking MinCommission (r:0 w:1) // Storage: Staking MinValidatorBond (r:0 w:1) @@ -805,7 +805,7 @@ impl WeightInfo for () { // Storage: Staking MaxNominatorsCount (r:0 w:1) // Storage: Staking MinNominatorBond (r:0 w:1) fn set_staking_configs_all_set() -> Weight { - Weight::from_ref_time(9_775_000 as u64) + Weight::from_ref_time(10_141_000 as u64) .saturating_add(RocksDbWeight::get().writes(6 as u64)) } // Storage: Staking MinCommission (r:0 w:1) @@ -815,7 +815,7 @@ impl WeightInfo for () { // Storage: Staking MaxNominatorsCount (r:0 w:1) // Storage: Staking MinNominatorBond (r:0 w:1) fn set_staking_configs_all_remove() -> Weight { - Weight::from_ref_time(9_042_000 as u64) + Weight::from_ref_time(8_993_000 as u64) .saturating_add(RocksDbWeight::get().writes(6 as u64)) } // Storage: Staking Ledger (r:1 w:0) @@ -825,18 +825,18 @@ impl WeightInfo for () { // Storage: Staking CounterForNominators (r:1 w:1) // Storage: Staking MinNominatorBond (r:1 w:0) // Storage: Staking Validators (r:1 w:0) - // Storage: BagsList ListNodes (r:2 w:2) - // Storage: BagsList ListBags (r:1 w:1) - // Storage: BagsList CounterForListNodes (r:1 w:1) + // Storage: VoterBagsList ListNodes (r:2 w:2) + // Storage: VoterBagsList ListBags (r:1 w:1) + // Storage: VoterBagsList CounterForListNodes (r:1 w:1) fn chill_other() -> Weight { - Weight::from_ref_time(79_225_000 as u64) + Weight::from_ref_time(79_082_000 as u64) .saturating_add(RocksDbWeight::get().reads(11 as u64)) .saturating_add(RocksDbWeight::get().writes(6 as u64)) } // Storage: Staking MinCommission (r:1 w:0) // Storage: Staking Validators (r:1 w:1) fn force_apply_min_commission() -> Weight { - Weight::from_ref_time(19_293_000 as u64) + Weight::from_ref_time(19_265_000 as u64) .saturating_add(RocksDbWeight::get().reads(2 as u64)) .saturating_add(RocksDbWeight::get().writes(1 as u64)) } From a94d03bfbb2954023875ff0f0b1f759dac51baaa Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Mon, 19 Sep 2022 14:10:11 +0200 Subject: [PATCH 20/36] fix compiler error --- frame/uniques/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index e25607e67a224..b02a10270dd7d 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -110,9 +110,9 @@ pub mod pallet { /// Standard collection creation is only allowed if the origin attempting it and the /// collection are in this set. type CreateOrigin: EnsureOriginWithArg< - Success = Self::AccountId, Self::Origin, Self::CollectionId, + Success = Self::AccountId, >; /// Locker trait to enable Locking mechanism downstream. From 5e76abe4deaa8ef552a8ff1fdf5e2d14dead2a17 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 20 Sep 2022 09:55:37 +0200 Subject: [PATCH 21/36] corrupting history depth does not lead to catastrophic issue --- frame/staking/src/tests.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 111e072a571b9..e2bc8cb93186d 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -5347,5 +5347,21 @@ fn pre_bonding_era_cannot_be_claimed() { Staking::payout_stakers(Origin::signed(4), 3, current_era - 2), Error::::AlreadyClaimed.with_weight(err_weight) ); + + // decoding will fail now since Staking Ledger is in corrupt state + HistoryDepth::set(history_depth-1); + assert_eq!( + Staking::ledger(&4), + None + ); + + // make sure stakers still cannot claim rewards that they are not meant to + assert_noop!( + Staking::payout_stakers(Origin::signed(4), 3, current_era - 2), + Error::::NotController + ); + + // fix the corrupted state for post conditions check + HistoryDepth::set(history_depth); }); -} +} \ No newline at end of file From 3d76533cca7e7ecacece4d871050f8580ab6343d Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 20 Sep 2022 09:57:28 +0200 Subject: [PATCH 22/36] fix new line --- frame/staking/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index e2bc8cb93186d..df89583582991 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -5364,4 +5364,4 @@ fn pre_bonding_era_cannot_be_claimed() { // fix the corrupted state for post conditions check HistoryDepth::set(history_depth); }); -} \ No newline at end of file +} From ee6d571113a3d0c625073fa1e220d8ecf2ab25ac Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 20 Sep 2022 10:03:03 +0200 Subject: [PATCH 23/36] remove unused import --- frame/staking/src/migrations.rs | 1 - frame/staking/src/tests.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index cdcb981ce8933..6fe93abd44a7a 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -35,7 +35,6 @@ pub mod v12 { impl OnRuntimeUpgrade for MigrateToV12 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result<(), &'static str> { - use frame_support::traits::OnRuntimeUpgradeHelpersExt; frame_support::ensure!( StorageVersion::::get() == Releases::V11_0_0, "Expected v11 before upgrading to v12" diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index df89583582991..e8cda76394f1e 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -5363,5 +5363,5 @@ fn pre_bonding_era_cannot_be_claimed() { // fix the corrupted state for post conditions check HistoryDepth::set(history_depth); - }); + }); } From 2f5c9e4a41c6f0e9b38f437a9519065b6c56ff2e Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 20 Sep 2022 10:03:24 +0200 Subject: [PATCH 24/36] fmt --- frame/staking/src/tests.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index e8cda76394f1e..2fadf9243e9a8 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -5349,11 +5349,8 @@ fn pre_bonding_era_cannot_be_claimed() { ); // decoding will fail now since Staking Ledger is in corrupt state - HistoryDepth::set(history_depth-1); - assert_eq!( - Staking::ledger(&4), - None - ); + HistoryDepth::set(history_depth - 1); + assert_eq!(Staking::ledger(&4), None); // make sure stakers still cannot claim rewards that they are not meant to assert_noop!( @@ -5363,5 +5360,5 @@ fn pre_bonding_era_cannot_be_claimed() { // fix the corrupted state for post conditions check HistoryDepth::set(history_depth); - }); + }); } From bc7ca4758a14bea47f331dffab9f87bddd6aed39 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 20 Sep 2022 10:41:30 +0200 Subject: [PATCH 25/36] add test to document scenario where history depth is reduced without migration --- frame/staking/src/migrations.rs | 6 +-- frame/staking/src/tests.rs | 87 +++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 6fe93abd44a7a..ca1b174c8514e 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -34,7 +34,7 @@ pub mod v12 { pub struct MigrateToV12(sp_std::marker::PhantomData); impl OnRuntimeUpgrade for MigrateToV12 { #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result<(), &'static str> { + fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( StorageVersion::::get() == Releases::V11_0_0, "Expected v11 before upgrading to v12" @@ -45,7 +45,7 @@ pub mod v12 { "Provided value of HistoryDepth should be same as the existing storage value" ); - Ok(()) + Ok(Default::default()) } fn on_runtime_upgrade() -> frame_support::weights::Weight { @@ -62,7 +62,7 @@ pub mod v12 { } #[cfg(feature = "try-runtime")] - fn post_upgrade() -> Result<(), &'static str> { + fn post_upgrade(_state: Vec) -> Result<(), &'static str> { frame_support::ensure!( StorageVersion::::get() == crate::Releases::V12_0_0, "v12 not applied" diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 2fadf9243e9a8..2b3041f35984e 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -5362,3 +5362,90 @@ fn pre_bonding_era_cannot_be_claimed() { HistoryDepth::set(history_depth); }); } + +#[test] +fn reducing_history_depth_without_migration() { + // Verifies initial conditions of mock + ExtBuilder::default().nominate(false).build_and_execute(|| { + let original_history_depth = HistoryDepth::get(); + let mut current_era = original_history_depth + 10; + let last_reward_era = current_era - 1; + let start_reward_era = current_era - original_history_depth; + + // put some money in (stash, controller)=(3,4),(5,6). + for i in 3..7 { + let _ = Balances::make_free_balance_be(&i, 2000); + } + + // start current era + mock::start_active_era(current_era); + + // add a new candidate for being a staker. account 3 controlled by 4. + assert_ok!(Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller)); + + // all previous era before the bonding action should be marked as + // claimed. + let mut claimed_rewards = vec![]; + for i in start_reward_era..=last_reward_era { + claimed_rewards.push(i); + } + + let claimed_rewards: BoundedVec<_, _> = claimed_rewards.try_into().unwrap(); + assert_eq!( + Staking::ledger(&4).unwrap(), + StakingLedger { + stash: 3, + total: 1500, + active: 1500, + unlocking: Default::default(), + claimed_rewards, + } + ); + + // next era + current_era = current_era + 1; + mock::start_active_era(current_era); + + // claiming reward for last era in which validator was active works + assert_ok!(Staking::payout_stakers(Origin::signed(4), 3, current_era - 1)); + + // next era + current_era = current_era + 1; + mock::start_active_era(current_era); + + // history_depth reduced without migration + let history_depth = original_history_depth -1; + HistoryDepth::set(history_depth); + // claiming reward does not work anymore + assert_noop!( + Staking::payout_stakers(Origin::signed(4), 3, current_era - 1), + Error::::NotController + ); + + // new stakers can still bond + // add new staker works + assert_ok!(Staking::bond(Origin::signed(5), 6, 1200, RewardDestination::Controller)); + + // new staking ledgers created will be bounded by the current history depth + let last_reward_era = current_era - 1; + let start_reward_era = current_era - history_depth; + let mut claimed_rewards = vec![]; + for i in start_reward_era..=last_reward_era { + claimed_rewards.push(i); + } + let claimed_rewards: BoundedVec<_, _> = claimed_rewards.try_into().unwrap(); + assert_eq!( + Staking::ledger(&6).unwrap(), + StakingLedger { + stash: 5, + total: 1200, + active: 1200, + unlocking: Default::default(), + claimed_rewards, + } + ); + + // fix the corrupted state for post conditions check + HistoryDepth::set(original_history_depth); + }); +} \ No newline at end of file From 00b916c3a8b2c7b21e26478dbeb36e324c06decc Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 20 Sep 2022 10:42:09 +0200 Subject: [PATCH 26/36] fmt --- frame/staking/src/tests.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 2b3041f35984e..3d2c25548c9cc 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -5414,7 +5414,7 @@ fn reducing_history_depth_without_migration() { mock::start_active_era(current_era); // history_depth reduced without migration - let history_depth = original_history_depth -1; + let history_depth = original_history_depth - 1; HistoryDepth::set(history_depth); // claiming reward does not work anymore assert_noop!( @@ -5426,7 +5426,7 @@ fn reducing_history_depth_without_migration() { // add new staker works assert_ok!(Staking::bond(Origin::signed(5), 6, 1200, RewardDestination::Controller)); - // new staking ledgers created will be bounded by the current history depth + // new staking ledgers created will be bounded by the current history depth let last_reward_era = current_era - 1; let start_reward_era = current_era - history_depth; let mut claimed_rewards = vec![]; @@ -5444,8 +5444,8 @@ fn reducing_history_depth_without_migration() { claimed_rewards, } ); - + // fix the corrupted state for post conditions check HistoryDepth::set(original_history_depth); }); -} \ No newline at end of file +} From 8db1b592473987a878851b7dd841402fc4130963 Mon Sep 17 00:00:00 2001 From: Ankan <10196091+Ank4n@users.noreply.github.com> Date: Tue, 20 Sep 2022 14:46:52 +0200 Subject: [PATCH 27/36] Update frame/staking/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- frame/staking/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 8aeefe503d96e..56fcb1965bc13 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -898,7 +898,7 @@ enum Releases { V9_0_0, // inject validators into `VoterList` as well. V10_0_0, // remove `EarliestUnappliedSlash`. V11_0_0, // Move pallet storage prefix, e.g. BagsList -> VoterBagsList - V12_0_0, // remove `HistoryDepth` + V12_0_0, // remove `HistoryDepth`. } impl Default for Releases { From c6dfcbf9c02939be3cb9d354f898e4689898f2cf Mon Sep 17 00:00:00 2001 From: Ankan <10196091+Ank4n@users.noreply.github.com> Date: Tue, 20 Sep 2022 14:46:59 +0200 Subject: [PATCH 28/36] Update frame/staking/src/migrations.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- frame/staking/src/migrations.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index ca1b174c8514e..8f37ae30dd056 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -27,7 +27,7 @@ pub mod v12 { #[storage_alias] type HistoryDepth = StorageValue, u32, ValueQuery>; - /// Clean up `HistoryDepth` from storage + /// Clean up `HistoryDepth` from storage. /// /// We will be depending on the configurable value of `HistoryDepth` post /// this release. From 121557df70409447c70c548256e15fe4626a3f6e Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 20 Sep 2022 15:02:52 +0200 Subject: [PATCH 29/36] doc for all storage items bounded by HistoryDepth --- frame/staking/src/pallet/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index eb1ee1c621159..61cf9d573944c 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -127,8 +127,11 @@ pub mod pallet { /// Number of eras to keep in history. /// - /// Information is kept for eras in `[current_era - history_depth; - /// current_era]`. + /// Following information is kept for eras in `[current_era - + /// HistoryDepth, current_era]`: `ErasStakers`, `ErasStakersClipped`, + /// `ErasValidatorPrefs`, `ErasValidatorReward`, `ErasRewardPoints`, + /// `ErasTotalStake`, `ErasStartSessionIndex`, + /// `StakingLedger.claimed_rewards`. /// /// Must be more than the number of eras delayed by session otherwise. /// I.e. active era must always be in history. I.e. `active_era > From 5ae9c8ce98037e9464da613b1f468d48d8909969 Mon Sep 17 00:00:00 2001 From: Ankan <10196091+Ank4n@users.noreply.github.com> Date: Tue, 20 Sep 2022 15:04:36 +0200 Subject: [PATCH 30/36] Update frame/staking/src/pallet/mod.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- frame/staking/src/pallet/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 61cf9d573944c..e5eea46f1974b 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -133,7 +133,7 @@ pub mod pallet { /// `ErasTotalStake`, `ErasStartSessionIndex`, /// `StakingLedger.claimed_rewards`. /// - /// Must be more than the number of eras delayed by session otherwise. + /// Must be more than the number of eras delayed by session. /// I.e. active era must always be in history. I.e. `active_era > /// current_era - history_depth` must be guaranteed. /// From 36b590d8b01e834fcbe9907e8f494ed44353ef01 Mon Sep 17 00:00:00 2001 From: Ankan <10196091+Ank4n@users.noreply.github.com> Date: Tue, 20 Sep 2022 15:05:01 +0200 Subject: [PATCH 31/36] Update frame/staking/src/tests.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- frame/staking/src/tests.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 3d2c25548c9cc..7e9031d61d3fd 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -5313,14 +5313,7 @@ fn pre_bonding_era_cannot_be_claimed() { // add a new candidate for being a validator. account 3 controlled by 4. assert_ok!(Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller)); - // all previous era before the bonding action should be marked as - // claimed. - let mut claimed_rewards = vec![]; - for i in start_reward_era..=last_reward_era { - claimed_rewards.push(i); - } - - let claimed_rewards: BoundedVec<_, _> = claimed_rewards.try_into().unwrap(); + let claimed_rewards: BoundedVec<_, _> = (start_reward_era..=last_reward_era).collect::>().try_into().unwrap(); assert_eq!( Staking::ledger(&4).unwrap(), StakingLedger { From a8f0e7df76f6ac83e9424f52b176282b3468d800 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 20 Sep 2022 15:14:33 +0200 Subject: [PATCH 32/36] pr feedback fixes --- frame/staking/src/tests.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 7e9031d61d3fd..f3abf1519045b 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -3308,7 +3308,7 @@ fn claim_reward_at_the_last_era_and_no_double_claim_and_invalid_claim() { // * double claim of one era fails ExtBuilder::default().nominate(true).build_and_execute(|| { // Consumed weight for all payout_stakers dispatches that fail - let err_weight = weights::SubstrateWeight::::payout_stakers_alive_staked(0); + let err_weight = ::WeightInfo::payout_stakers_alive_staked(0); let init_balance_10 = Balances::total_balance(&10); let init_balance_100 = Balances::total_balance(&100); @@ -3666,7 +3666,7 @@ fn payout_stakers_handles_basic_errors() { // Here we will test payouts handle all errors. ExtBuilder::default().has_stakers(false).build_and_execute(|| { // Consumed weight for all payout_stakers dispatches that fail - let err_weight = weights::SubstrateWeight::::payout_stakers_alive_staked(0); + let err_weight = ::WeightInfo::payout_stakers_alive_staked(0); // Same setup as the test above let balance = 1000; @@ -5313,7 +5313,8 @@ fn pre_bonding_era_cannot_be_claimed() { // add a new candidate for being a validator. account 3 controlled by 4. assert_ok!(Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller)); - let claimed_rewards: BoundedVec<_, _> = (start_reward_era..=last_reward_era).collect::>().try_into().unwrap(); + let claimed_rewards: BoundedVec<_, _> = + (start_reward_era..=last_reward_era).collect::>().try_into().unwrap(); assert_eq!( Staking::ledger(&4).unwrap(), StakingLedger { @@ -5333,7 +5334,7 @@ fn pre_bonding_era_cannot_be_claimed() { assert_ok!(Staking::payout_stakers(Origin::signed(4), 3, current_era - 1)); // consumed weight for all payout_stakers dispatches that fail - let err_weight = weights::SubstrateWeight::::payout_stakers_alive_staked(0); + let err_weight = ::WeightInfo::payout_stakers_alive_staked(0); // cannot claim rewards for an era before bonding occured as it is // already marked as claimed. assert_noop!( @@ -5378,12 +5379,8 @@ fn reducing_history_depth_without_migration() { // all previous era before the bonding action should be marked as // claimed. - let mut claimed_rewards = vec![]; - for i in start_reward_era..=last_reward_era { - claimed_rewards.push(i); - } - - let claimed_rewards: BoundedVec<_, _> = claimed_rewards.try_into().unwrap(); + let claimed_rewards: BoundedVec<_, _> = + (start_reward_era..=last_reward_era).collect::>().try_into().unwrap(); assert_eq!( Staking::ledger(&4).unwrap(), StakingLedger { @@ -5422,11 +5419,8 @@ fn reducing_history_depth_without_migration() { // new staking ledgers created will be bounded by the current history depth let last_reward_era = current_era - 1; let start_reward_era = current_era - history_depth; - let mut claimed_rewards = vec![]; - for i in start_reward_era..=last_reward_era { - claimed_rewards.push(i); - } - let claimed_rewards: BoundedVec<_, _> = claimed_rewards.try_into().unwrap(); + let claimed_rewards: BoundedVec<_, _> = + (start_reward_era..=last_reward_era).collect::>().try_into().unwrap(); assert_eq!( Staking::ledger(&6).unwrap(), StakingLedger { From 1010ed50ebe39e7197b527534c8be98a0da0b999 Mon Sep 17 00:00:00 2001 From: Ankan <10196091+Ank4n@users.noreply.github.com> Date: Tue, 20 Sep 2022 15:30:06 +0200 Subject: [PATCH 33/36] Update frame/staking/src/tests.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- frame/staking/src/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index f3abf1519045b..acf4a18da1b9e 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -5413,7 +5413,6 @@ fn reducing_history_depth_without_migration() { ); // new stakers can still bond - // add new staker works assert_ok!(Staking::bond(Origin::signed(5), 6, 1200, RewardDestination::Controller)); // new staking ledgers created will be bounded by the current history depth From 635bada63697df5318d86a0e4772bb2a3b748a2f Mon Sep 17 00:00:00 2001 From: kianenigma Date: Tue, 20 Sep 2022 16:06:56 +0000 Subject: [PATCH 34/36] remove extra checks --- frame/staking/src/migrations.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 8f37ae30dd056..f47545af694cf 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -35,11 +35,6 @@ pub mod v12 { impl OnRuntimeUpgrade for MigrateToV12 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { - frame_support::ensure!( - StorageVersion::::get() == Releases::V11_0_0, - "Expected v11 before upgrading to v12" - ); - frame_support::ensure!( T::HistoryDepth::get() == HistoryDepth::::get(), "Provided value of HistoryDepth should be same as the existing storage value" @@ -134,11 +129,6 @@ pub mod v11 { #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), &'static str> { - frame_support::ensure!( - StorageVersion::::get() == crate::Releases::V11_0_0, - "wrong version after the upgrade" - ); - let old_pallet_name = N::get(); let new_pallet_name =

::name(); From 734fb6ab94b470edcbc465ca45517a7cceee7872 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Wed, 21 Sep 2022 10:29:21 +0200 Subject: [PATCH 35/36] fix merge --- frame/staking/src/tests.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index c9eb229d4da62..0ad0bb47f02f0 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -3714,8 +3714,8 @@ fn test_payout_stakers() { } // We clean it up as history passes - assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, expected_start_reward_era)); - assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, expected_last_reward_era)); + assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_start_reward_era)); + assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_last_reward_era)); assert_eq!( Staking::ledger(&10), Some(StakingLedger { @@ -3801,23 +3801,23 @@ fn payout_stakers_handles_basic_errors() { // to payout era starting from expected_start_reward_era=19 through // expected_last_reward_era=98 (80 total eras), but not 18 or 99. assert_noop!( - Staking::payout_stakers(Origin::signed(1337), 11, expected_start_reward_era - 1), + Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_start_reward_era - 1), Error::::InvalidEraToReward.with_weight(err_weight) ); assert_noop!( - Staking::payout_stakers(Origin::signed(1337), 11, expected_last_reward_era + 1), + Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_last_reward_era + 1), Error::::InvalidEraToReward.with_weight(err_weight) ); - assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, expected_start_reward_era)); - assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, expected_last_reward_era)); + assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_start_reward_era)); + assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_last_reward_era)); // Can't claim again assert_noop!( - Staking::payout_stakers(Origin::signed(1337), 11, expected_start_reward_era), + Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_start_reward_era), Error::::AlreadyClaimed.with_weight(err_weight) ); assert_noop!( - Staking::payout_stakers(Origin::signed(1337), 11, expected_last_reward_era), + Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_last_reward_era), Error::::AlreadyClaimed.with_weight(err_weight) ); }); @@ -5420,7 +5420,7 @@ fn pre_bonding_era_cannot_be_claimed() { mock::start_active_era(current_era); // add a new candidate for being a validator. account 3 controlled by 4. - assert_ok!(Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller)); + assert_ok!(Staking::bond(RuntimeOrigin::signed(3), 4, 1500, RewardDestination::Controller)); let claimed_rewards: BoundedVec<_, _> = (start_reward_era..=last_reward_era).collect::>().try_into().unwrap(); @@ -5440,14 +5440,14 @@ fn pre_bonding_era_cannot_be_claimed() { mock::start_active_era(current_era); // claiming reward for last era in which validator was active works - assert_ok!(Staking::payout_stakers(Origin::signed(4), 3, current_era - 1)); + assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(4), 3, current_era - 1)); // consumed weight for all payout_stakers dispatches that fail let err_weight = ::WeightInfo::payout_stakers_alive_staked(0); // cannot claim rewards for an era before bonding occured as it is // already marked as claimed. assert_noop!( - Staking::payout_stakers(Origin::signed(4), 3, current_era - 2), + Staking::payout_stakers(RuntimeOrigin::signed(4), 3, current_era - 2), Error::::AlreadyClaimed.with_weight(err_weight) ); @@ -5457,7 +5457,7 @@ fn pre_bonding_era_cannot_be_claimed() { // make sure stakers still cannot claim rewards that they are not meant to assert_noop!( - Staking::payout_stakers(Origin::signed(4), 3, current_era - 2), + Staking::payout_stakers(RuntimeOrigin::signed(4), 3, current_era - 2), Error::::NotController ); @@ -5484,7 +5484,7 @@ fn reducing_history_depth_without_migration() { mock::start_active_era(current_era); // add a new candidate for being a staker. account 3 controlled by 4. - assert_ok!(Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller)); + assert_ok!(Staking::bond(RuntimeOrigin::signed(3), 4, 1500, RewardDestination::Controller)); // all previous era before the bonding action should be marked as // claimed. @@ -5506,7 +5506,7 @@ fn reducing_history_depth_without_migration() { mock::start_active_era(current_era); // claiming reward for last era in which validator was active works - assert_ok!(Staking::payout_stakers(Origin::signed(4), 3, current_era - 1)); + assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(4), 3, current_era - 1)); // next era current_era = current_era + 1; @@ -5517,12 +5517,12 @@ fn reducing_history_depth_without_migration() { HistoryDepth::set(history_depth); // claiming reward does not work anymore assert_noop!( - Staking::payout_stakers(Origin::signed(4), 3, current_era - 1), + Staking::payout_stakers(RuntimeOrigin::signed(4), 3, current_era - 1), Error::::NotController ); // new stakers can still bond - assert_ok!(Staking::bond(Origin::signed(5), 6, 1200, RewardDestination::Controller)); + assert_ok!(Staking::bond(RuntimeOrigin::signed(5), 6, 1200, RewardDestination::Controller)); // new staking ledgers created will be bounded by the current history depth let last_reward_era = current_era - 1; From 8c3ac4933076d622f746b0787ac0585be1be71f2 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Wed, 21 Sep 2022 10:29:43 +0200 Subject: [PATCH 36/36] fmt --- frame/staking/src/tests.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 0ad0bb47f02f0..6798a78030f9e 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -3714,8 +3714,16 @@ fn test_payout_stakers() { } // We clean it up as history passes - assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_start_reward_era)); - assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_last_reward_era)); + assert_ok!(Staking::payout_stakers( + RuntimeOrigin::signed(1337), + 11, + expected_start_reward_era + )); + assert_ok!(Staking::payout_stakers( + RuntimeOrigin::signed(1337), + 11, + expected_last_reward_era + )); assert_eq!( Staking::ledger(&10), Some(StakingLedger { @@ -3808,8 +3816,16 @@ fn payout_stakers_handles_basic_errors() { Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_last_reward_era + 1), Error::::InvalidEraToReward.with_weight(err_weight) ); - assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_start_reward_era)); - assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, expected_last_reward_era)); + assert_ok!(Staking::payout_stakers( + RuntimeOrigin::signed(1337), + 11, + expected_start_reward_era + )); + assert_ok!(Staking::payout_stakers( + RuntimeOrigin::signed(1337), + 11, + expected_last_reward_era + )); // Can't claim again assert_noop!(