Skip to content

Commit 81391c7

Browse files
authored
Adds versioning to staking migration (#20)
1 parent 70657f5 commit 81391c7

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

frame/staking/src/migration.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use frame_support::weights::Weight;
44

55
/// Deprecated storages used for migration only.
66
mod deprecated {
7-
use crate::{Trait, BalanceOf, SessionIndex, Exposure};
8-
use codec::{Encode, Decode};
7+
use crate::{BalanceOf, Exposure, SessionIndex, Trait};
8+
use codec::{Decode, Encode};
99
use frame_support::{decl_module, decl_storage};
1010
use sp_std::prelude::*;
1111

1212
// edgeware uses `u64` for `Moment`
13-
type Moment = u64;
13+
pub type Moment = u64;
1414

1515
/// Reward points of an era. Used to split era total payout between validators.
1616
#[derive(Encode, Decode, Default)]
@@ -47,6 +47,8 @@ mod deprecated {
4747
///
4848
/// This is keyed by the stash account.
4949
pub Stakers: map hasher(opaque_blake2_256) T::AccountId => Exposure<T::AccountId, BalanceOf<T>>;
50+
51+
pub StorageVersion: u32;
5052
}
5153
}
5254
}
@@ -88,6 +90,11 @@ struct OldStakingLedger<AccountId, Balance: HasCompact> {
8890
//
8991
// The edgeware migration is so big we just assume it consumes the whole block.
9092
pub fn migrate_to_simple_payouts<T: Trait>() -> Weight {
93+
let version = deprecated::StorageVersion::take();
94+
if version != 2 {
95+
frame_support::runtime_print!("🕊️ Unexpected Staking StorageVersion: {}", version);
96+
return 0;
97+
}
9198
sp_runtime::print("🕊️ Migrating Staking...");
9299
let current_era_start_index = deprecated::CurrentEraStartSessionIndex::get();
93100
let current_era = <Module<T> as Store>::CurrentEra::get().unwrap_or(0);
@@ -108,7 +115,9 @@ pub fn migrate_to_simple_payouts<T: Trait>() -> Weight {
108115
let mut exposure_clipped = exposure;
109116
let clipped_max_len = T::MaxNominatorRewardedPerValidator::get() as usize;
110117
if exposure_clipped.others.len() > clipped_max_len {
111-
exposure_clipped.others.sort_unstable_by(|a, b| a.value.cmp(&b.value).reverse());
118+
exposure_clipped
119+
.others
120+
.sort_unstable_by(|a, b| a.value.cmp(&b.value).reverse());
112121
exposure_clipped.others.truncate(clipped_max_len);
113122
}
114123
<Module<T> as Store>::ErasStakersClipped::insert(current_era, validator, exposure_clipped);
@@ -119,10 +128,17 @@ pub fn migrate_to_simple_payouts<T: Trait>() -> Weight {
119128
<Module<T> as Store>::ErasTotalStake::insert(current_era, current_total_stake);
120129

121130
let points = deprecated::CurrentEraPointsEarned::get();
122-
<Module<T> as Store>::ErasRewardPoints::insert(current_era, EraRewardPoints {
123-
total: points.total,
124-
individual: current_elected.iter().cloned().zip(points.individual.iter().cloned()).collect(),
125-
});
131+
<Module<T> as Store>::ErasRewardPoints::insert(
132+
current_era,
133+
EraRewardPoints {
134+
total: points.total,
135+
individual: current_elected
136+
.iter()
137+
.cloned()
138+
.zip(points.individual.iter().cloned())
139+
.collect(),
140+
},
141+
);
126142

127143
let res = <Module<T> as Store>::Ledger::translate_values(
128144
|old: OldStakingLedger<T::AccountId, BalanceOf<T>>| StakingLedger {
@@ -131,7 +147,7 @@ pub fn migrate_to_simple_payouts<T: Trait>() -> Weight {
131147
active: old.active,
132148
unlocking: old.unlocking,
133149
claimed_rewards: vec![],
134-
}
150+
},
135151
);
136152
if let Err(e) = res {
137153
frame_support::print("Encountered error in migration of Staking::Ledger map.");
@@ -147,6 +163,8 @@ pub fn migrate_to_simple_payouts<T: Trait>() -> Weight {
147163
deprecated::CurrentEraStartSessionIndex::kill();
148164
deprecated::CurrentEraPointsEarned::kill();
149165

166+
StorageVersion::put(Releases::V4_0_0);
167+
150168
sp_runtime::print("🕊️ Done Staking.");
151169
T::MaximumBlockWeight::get()
152-
}
170+
}

0 commit comments

Comments
 (0)