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

Commit 9ee8876

Browse files
committed
refactor and doc improve
1 parent e619706 commit 9ee8876

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed

frame/staking/src/migrations.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ pub mod v11 {
2727
#[storage_alias]
2828
type HistoryDepth<T: Config> = StorageValue<Pallet<T>, u32, ValueQuery>;
2929

30-
/// Clean up History Depth from storage
30+
/// Clean up `HistoryDepth` from storage
3131
///
32-
/// We will be depending on the configurable value of History Depth post this release.
32+
/// We will be depending on the configurable value of `HistoryDepth` post
33+
/// this release.
3334
pub struct MigrateToV11<T>(sp_std::marker::PhantomData<T>);
3435
impl<T: Config> OnRuntimeUpgrade for MigrateToV11<T> {
3536
fn on_runtime_upgrade() -> frame_support::weights::Weight {

frame/staking/src/pallet/impls.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<T: Config> Pallet<T> {
100100
Error::<T>::InvalidEraToReward
101101
.with_weight(T::WeightInfo::payout_stakers_alive_staked(0))
102102
})?;
103-
let history_depth = Self::history_depth();
103+
let history_depth = T::HistoryDepth::get();
104104
ensure!(
105105
era <= current_era && era >= current_era.saturating_sub(history_depth),
106106
Error::<T>::InvalidEraToReward
@@ -131,7 +131,8 @@ impl<T: Config> Pallet<T> {
131131
.claimed_rewards
132132
.try_insert(pos, era)
133133
// Since we retain era entries in `claimed_rewards` only upto
134-
// `HistoryDepth`, following bound is always expected to be satisfied.
134+
// `HistoryDepth`, following bound is always expected to be
135+
// satisfied.
135136
.defensive_map_err(|_| Error::<T>::BoundNotMet)?,
136137
}
137138

@@ -422,7 +423,7 @@ impl<T: Config> Pallet<T> {
422423
ErasStartSessionIndex::<T>::insert(&new_planned_era, &start_session_index);
423424

424425
// Clean old era information.
425-
if let Some(old_era) = new_planned_era.checked_sub(Self::history_depth() + 1) {
426+
if let Some(old_era) = new_planned_era.checked_sub(T::HistoryDepth::get() + 1) {
426427
Self::clear_era_information(old_era);
427428
}
428429

@@ -876,15 +877,6 @@ impl<T: Config> Pallet<T> {
876877
DispatchClass::Mandatory,
877878
);
878879
}
879-
880-
/// This will return the currently configured `HistoryDepth`
881-
///
882-
/// With release of v11, `HistoryDepth` is migrated to a configurable
883-
/// value instead of being a storage item. This function replaces the
884-
/// old fn history_depth that used to read from the storage.
885-
pub fn history_depth() -> u32 {
886-
T::HistoryDepth::get()
887-
}
888880
}
889881

890882
impl<T: Config> ElectionDataProvider for Pallet<T> {

frame/staking/src/pallet/mod.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,16 @@ pub mod pallet {
125125
#[pallet::constant]
126126
type MaxNominations: Get<u32>;
127127

128-
/// `HistoryDepth` stores the number of previous eras for which
129-
/// we keep track of validator `claimed_rewards`.
130-
///
131-
/// Since `HistoryDepth` is used as a bound for BoundedVec
132-
/// `claimed_rewards`, we should use extreme caution while
133-
/// changing this value once it has been already deployed in
134-
/// production. In general, increasing this value should be okay
135-
/// but decreasing it will cause inconsistencies and needs to be
136-
/// handled by doing migration of `StakingLedger.claimed_rewards`.
128+
/// `HistoryDepth` is the number of eras to keep in history.
129+
///
130+
/// This used to be a storage value previously. If you are migrating
131+
/// from storage value to config value, you should read this value from
132+
/// storage and set the same value in configuration.
133+
///
134+
/// Note: `HistoryDepth` is used as the upper bound for the `BoundedVec`
135+
/// item `StakingLedger.claimed_rewards`. Setting this value lower than
136+
/// the existing value can lead to inconsistencies and will need to be
137+
/// handled properly in migration.
137138
#[pallet::constant]
138139
type HistoryDepth: Get<u32>;
139140

@@ -819,8 +820,9 @@ pub mod pallet {
819820
unlocking: Default::default(),
820821
claimed_rewards: (last_reward_era..current_era)
821822
.try_collect()
822-
// Since last_reward_era is calculated as `current_era - HistoryDepth`,
823-
// following bound is always expected to be satisfied.
823+
// Since last_reward_era is calculated as `current_era -
824+
// HistoryDepth`, following bound is always expected to be
825+
// satisfied.
824826
.defensive_map_err(|_| Error::<T>::BoundNotMet)?,
825827
};
826828
Self::update_ledger(&controller, &item);

frame/staking/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,15 +3347,15 @@ fn claim_reward_at_the_last_era_and_no_double_claim_and_invalid_claim() {
33473347
assert!(total_payout_2 != total_payout_0);
33483348
assert!(total_payout_2 != total_payout_1);
33493349

3350-
mock::start_active_era(Staking::history_depth() + 1);
3350+
mock::start_active_era(HistoryDepth::get() + 1);
33513351

33523352
let active_era = active_era();
33533353

33543354
// This is the latest planned era in staking, not the active era
33553355
let current_era = Staking::current_era().unwrap();
33563356

33573357
// Last kept is 1:
3358-
assert!(current_era - Staking::history_depth() == 1);
3358+
assert!(current_era - HistoryDepth::get() == 1);
33593359
assert_noop!(
33603360
Staking::payout_stakers(Origin::signed(1337), 11, 0),
33613361
// Fail: Era out of history

0 commit comments

Comments
 (0)