Bound staking storage items (#12230)

* replace pallet level unboundedness to individual storage items

* bound structs

* bounding history depth

* defensive error

* use the era history depth from config

* clean up history depth from storage in v11

* keep the name HistoryDepth for the new configuration value

* use u32 for history depth in node runtime

* improve doc comments

* add HistoryDepth to mock runtimes with pallet-staking

* rustfmt

* refactor and doc improve

* apply re-benchmarked weight for staking

* pr feedback improvements

* test for claimed rewards following the expected bounds

* refactor test to calculate first and last reward era programmatically

* verify previous eras cannot be claimed

* add migration v12

* ".git/.scripts/bench-bot.sh" pallet dev pallet_staking

* fix compiler error

* corrupting history depth does not lead to catastrophic issue

* fix new line

* remove unused import

* fmt

* add test to document scenario where history depth is reduced without migration

* fmt

* Update frame/staking/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/staking/src/migrations.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* doc for all storage items bounded by HistoryDepth

* Update frame/staking/src/pallet/mod.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/staking/src/tests.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* pr feedback fixes

* Update frame/staking/src/tests.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* remove extra checks

* fix merge

* fmt

Co-authored-by: command-bot <>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: kianenigma <kian@parity.io>
This commit is contained in:
Ankan
2022-09-21 10:57:02 +02:00
committed by GitHub
parent 86198c5471
commit c6a9abcc68
16 changed files with 669 additions and 483 deletions
+16 -9
View File
@@ -25,8 +25,8 @@ use frame_support::{
dispatch::WithPostDispatchInfo,
pallet_prelude::*,
traits::{
Currency, CurrencyToVote, Defensive, EstimateNextNewSession, Get, Imbalance,
LockableCurrency, OnUnbalanced, UnixTime, WithdrawReasons,
Currency, CurrencyToVote, Defensive, DefensiveResult, EstimateNextNewSession, Get,
Imbalance, LockableCurrency, OnUnbalanced, UnixTime, WithdrawReasons,
},
weights::Weight,
};
@@ -101,7 +101,7 @@ impl<T: Config> Pallet<T> {
Error::<T>::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::<T>::InvalidEraToReward
@@ -123,11 +123,18 @@ impl<T: Config> Pallet<T> {
ledger
.claimed_rewards
.retain(|&x| x >= current_era.saturating_sub(history_depth));
match ledger.claimed_rewards.binary_search(&era) {
Ok(_) =>
return Err(Error::<T>::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)
// Since we retain era entries in `claimed_rewards` only upto
// `HistoryDepth`, following bound is always expected to be
// satisfied.
.defensive_map_err(|_| Error::<T>::BoundNotMet)?,
}
let exposure = <ErasStakersClipped<T>>::get(&era, &ledger.stash);
@@ -417,7 +424,7 @@ impl<T: Config> Pallet<T> {
ErasStartSessionIndex::<T>::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);
}
@@ -966,7 +973,7 @@ impl<T: Config> ElectionDataProvider for Pallet<T> {
active: stake,
total: stake,
unlocking: Default::default(),
claimed_rewards: vec![],
claimed_rewards: Default::default(),
},
);
@@ -984,7 +991,7 @@ impl<T: Config> ElectionDataProvider for Pallet<T> {
active: stake,
total: stake,
unlocking: Default::default(),
claimed_rewards: vec![],
claimed_rewards: Default::default(),
},
);
Self::do_add_validator(
@@ -1025,7 +1032,7 @@ impl<T: Config> ElectionDataProvider for Pallet<T> {
active: stake,
total: stake,
unlocking: Default::default(),
claimed_rewards: vec![],
claimed_rewards: Default::default(),
},
);
Self::do_add_validator(
@@ -1046,7 +1053,7 @@ impl<T: Config> ElectionDataProvider for Pallet<T> {
active: stake,
total: stake,
unlocking: Default::default(),
claimed_rewards: vec![],
claimed_rewards: Default::default(),
},
);
Self::do_add_nominator(