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

Commit d7009f9

Browse files
committed
WIP
1 parent 2c602ed commit d7009f9

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
7878
// and set impl_version to equal spec_version. If only runtime
7979
// implementation changes and behavior does not, then leave spec_version as
8080
// is and increment impl_version.
81-
spec_version: 198,
82-
impl_version: 198,
81+
spec_version: 199,
82+
impl_version: 199,
8383
apis: RUNTIME_API_VERSIONS,
8484
};
8585

frame/staking/src/lib.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ decl_storage! {
683683
pub EraStartSessionIndex get(fn era_start_session_index) build(|_| vec![0]): Vec<SessionIndex>;
684684

685685
/// Rewards for the current era. Using indices of current elected set.
686-
pub CurrentEraPointsEarned get(fn current_era_reward): EraPoints;
686+
CurrentEraPointsEarned get(fn current_era_reward): EraPoints;
687687

688688
/// Mapping from era to its validator set with all information needed for rewarding.
689689
///
@@ -744,6 +744,15 @@ decl_storage! {
744744

745745
/// The version of storage for upgrade.
746746
StorageVersion: u32;
747+
748+
/// Deprecated: This storage is outdated. Information are not relevant anymore.
749+
Stakers: map T::AccountId => Exposure<T::AccountId, BalanceOf<T>>;
750+
751+
/// Deprecated: This storage is outdated. Information are not relevant anymore.
752+
CurrentElected: Vec<T::AccountId>;
753+
754+
/// Deprecated: This storage is outdated. Information are not relevant anymore.
755+
SlotStake: BalanceOf<T>;
747756
}
748757
add_extra_genesis {
749758
config(stakers):
@@ -1350,21 +1359,21 @@ impl<T: Trait> Module<T> {
13501359
}
13511360

13521361
// Increment current era.
1353-
let current_era = current_era + 1;
1354-
CurrentEra::put(current_era);
1362+
let new_current_era = current_era + 1;
1363+
CurrentEra::put(new_current_era);
13551364
EraStartSessionIndex::mutate(|era_start| {
13561365
era_start.remove(0)
13571366
});
13581367

1359-
Self::apply_unapplied_slashes(current_era);
1368+
Self::apply_unapplied_slashes(new_current_era);
13601369

13611370
let bonding_duration = T::BondingDuration::get();
13621371

13631372
BondedEras::mutate(|bonded| {
1364-
bonded.push((current_era, session_index + 1));
1373+
bonded.push((new_current_era, session_index + 1));
13651374

1366-
if current_era > bonding_duration {
1367-
let first_kept = current_era - bonding_duration;
1375+
if new_current_era > bonding_duration {
1376+
let first_kept = new_current_era - bonding_duration;
13681377

13691378
// prune out everything that's from before the first-kept index.
13701379
let n_to_prune = bonded.iter()

frame/staking/src/migration.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
pub type VersionNumber = u32;
2121

2222
// the current expected version of the storage
23-
pub const CURRENT_VERSION: VersionNumber = 1;
23+
pub const CURRENT_VERSION: VersionNumber = 2;
2424

2525
#[cfg(any(test, feature = "migrate"))]
2626
mod inner {
27-
use crate::{Store, Module, Trait};
28-
use support::{StorageLinkedMap, StorageValue};
29-
use sp_std::vec::Vec;
27+
use crate::{Store, Module, Trait, ValidatorInfoForEra, SessionInterface};
28+
use support::{StorageLinkedMap, StorageValue, StorageMap, StoragePrefixedMap};
29+
use sp_std::{vec, vec::Vec};
3030
use super::{CURRENT_VERSION, VersionNumber};
3131

3232
// the minimum supported version of the migration logic.
@@ -60,6 +60,42 @@ mod inner {
6060
support::print("Finished migrating Staking storage to v1.");
6161
}
6262

63+
// migrate storage from v1 to v2.
64+
//
65+
// * populate `EraStartSessionIndex` storage with only the current era start
66+
// * populate `ValidatorForEra` storage with information from:
67+
// * `Trait::SessionInterface::validators` for validator set
68+
// * `Stakers` for exposure of validators
69+
// * `Validators` for prefs of validators
70+
// * populate `SlotStakeForEra` storage with information from `SlotStake`
71+
// * remove `CurrentEraStartSessionIndex` storage
72+
// * remove `CurrentElected` storage
73+
// * remove `SlotStake` storage
74+
pub fn from_v1_to_v2<T: Trait>(version: &mut VersionNumber) {
75+
if *version != 1 { return }
76+
*version += 1;
77+
78+
let current_era = <Module<T>>::current_era();
79+
let current_validator_infos = T::SessionInterface::validators().into_iter()
80+
.map(|validator| ValidatorInfoForEra {
81+
prefs: <Module<T>>::validators(&validator),
82+
exposure: <Module<T> as Store>::Stakers::get(&validator),
83+
stash: validator,
84+
})
85+
.collect::<Vec<_>>();
86+
let current_slot_stake = <Module<T> as Store>::SlotStake::get();
87+
88+
<Module<T> as Store>::ValidatorForEra::insert(&current_era, current_validator_infos);
89+
<Module<T> as Store>::SlotStakeForEra::insert(&current_era, current_slot_stake);
90+
<Module<T> as Store>::EraStartSessionIndex::put(vec![current_era]);
91+
92+
<Module<T> as Store>::Stakers::remove_all();
93+
<Module<T> as Store>::CurrentElected::kill();
94+
<Module<T> as Store>::SlotStake::kill();
95+
96+
support::print("Finished migrating Staking storage to v2.");
97+
}
98+
6399
pub(super) fn perform_migrations<T: Trait>() {
64100
<Module<T> as Store>::StorageVersion::mutate(|version| {
65101
if *version < MIN_SUPPORTED_VERSION {
@@ -72,6 +108,7 @@ mod inner {
72108
if *version == CURRENT_VERSION { return }
73109

74110
to_v1::<T>(version);
111+
from_v1_to_v2::<T>(version);
75112
});
76113
}
77114
}

0 commit comments

Comments
 (0)