Skip to content

Commit e1ff57d

Browse files
authored
Merge pull request #10 from hicommonwealth/apopiak-staking-migration
Add staking migration
2 parents fee4052 + 39a031b commit e1ff57d

File tree

11 files changed

+183
-39
lines changed

11 files changed

+183
-39
lines changed

frame/atomic-swap/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl frame_system::Trait for Test {
5252
type Version = ();
5353
type ModuleToIndex = ();
5454
type AccountData = pallet_balances::AccountData<u64>;
55+
type MigrateAccount = ();
5556
type OnNewAccount = ();
5657
type OnKilledAccount = ();
5758
}

frame/balances/src/migration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn on_runtime_upgrade<T: Trait<I>, I: Instance>() -> Weight {
3333

3434
// Upgrade from the pre-#4649 balances/vesting into the new balances.
3535
fn upgrade_v1_to_v2<T: Trait<I>, I: Instance>() -> Weight {
36-
sp_runtime::print("Upgrading Account Balances...");
36+
sp_runtime::print("🕊️ Migrating Account Balances...");
3737
// First, migrate from old FreeBalance to new Account.
3838
// We also move all locks across since only accounts with FreeBalance values have locks.
3939
// FreeBalance: map T::AccountId => T::Balance
@@ -139,7 +139,7 @@ fn upgrade_v1_to_v2<T: Trait<I>, I: Instance>() -> Weight {
139139

140140
StorageVersion::<I>::put(Releases::V2_0_0);
141141

142-
sp_runtime::print("Done Account Balances.");
143-
// TODO determine actual weight
142+
sp_runtime::print("🕊️ Done Account Balances.");
143+
// TODO determine actual weight?
144144
T::MaximumBlockWeight::get()
145145
}

frame/contracts/src/migration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn on_runtime_upgrade<T: Trait>() -> Weight {
3333
// upgraded, nothing here will happen anyway.
3434

3535
fn change_name_contract_to_contracts<T: Trait>() -> Weight {
36-
sp_runtime::print("Migrating Contracts.");
36+
sp_runtime::print("🕊️ Migrating Contracts.");
3737

3838
let mut weight = 0;
3939
let db = T::DbWeight::get();
@@ -76,6 +76,6 @@ fn change_name_contract_to_contracts<T: Trait>() -> Weight {
7676
}
7777
weight += db.reads(1);
7878

79-
sp_runtime::print("Done Contracts.");
79+
sp_runtime::print("🕊️ Done Contracts.");
8080
weight
8181
}

frame/democracy/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ impl<T: Trait> MigrateAccount<T::AccountId> for Module<T> {
630630
mod migration {
631631
use super::*;
632632

633-
pub fn migrate<T: Trait>() {
633+
pub fn migrate<T: Trait>() -> Weight {
634634
mod deprecated {
635635
use super::*;
636636

@@ -660,6 +660,9 @@ mod migration {
660660
DepositOf::<T>::migrate_key_from_blake(p);
661661
Preimages::<T>::migrate_key_from_blake(h);
662662
}
663+
664+
// TODO: figure out actual weight
665+
0
663666
}
664667
}
665668

@@ -697,6 +700,10 @@ decl_module! {
697700

698701
fn deposit_event() = default;
699702

703+
fn on_runtime_upgrade() -> Weight {
704+
migration::migrate::<T>()
705+
}
706+
700707
/// Propose a sensitive action to be taken.
701708
///
702709
/// The dispatch origin of this call must be _Signed_ and the sender must

frame/finality-tracker/src/migration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn on_runtime_upgrade<T: Trait>() -> Weight {
3131
// upgraded, nothing here will happen anyway.
3232

3333
fn change_name_timestamp_to_finality_tracker<T:Trait>() -> Weight {
34-
sp_runtime::print("Migrating Finality Tracker.");
34+
sp_runtime::print("🕊️ Migrating Finality Tracker.");
3535

3636
let mut reads = 0;
3737
let mut writes = 0;
@@ -65,6 +65,6 @@ fn change_name_timestamp_to_finality_tracker<T:Trait>() -> Weight {
6565
}
6666
reads += 1;
6767

68-
sp_runtime::print("Done Finality Tracker.");
68+
sp_runtime::print("🕊️ Done Finality Tracker.");
6969
T::DbWeight::get().reads_writes(reads, writes)
7070
}

frame/identity/src/migration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn on_runtime_upgrade<T: Trait>() -> Weight {
3131
// upgraded, nothing here will happen anyway.
3232

3333
fn change_name_sudo_to_identity<T: Trait>() -> Weight {
34-
sp_runtime::print("Migrating Identity.");
34+
sp_runtime::print("🕊️ Migrating Identity.");
3535

3636
let mut weight = 0;
3737
let db = T::DbWeight::get();
@@ -56,6 +56,6 @@ fn change_name_sudo_to_identity<T: Trait>() -> Weight {
5656
}
5757
weight += db.reads(1);
5858

59-
sp_runtime::print("Done Identity.");
59+
sp_runtime::print("🕊️ Done Identity.");
6060
weight
6161
}

frame/staking/src/lib.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ pub mod slashing;
280280
pub mod offchain_election;
281281
pub mod inflation;
282282

283+
pub mod migration;
284+
283285
use sp_std::{
284286
result,
285287
prelude::*,
@@ -1380,7 +1382,7 @@ decl_module! {
13801382
}
13811383

13821384
fn on_runtime_upgrade() -> Weight {
1383-
migrate::<T>();
1385+
migrate_hasher::<T>();
13841386
// TODO: determine actual weight
13851387
0
13861388
}
@@ -2228,7 +2230,7 @@ impl<T: Trait> MigrateAccount<T::AccountId> for Module<T> {
22282230
}
22292231
}
22302232

2231-
fn migrate<T: Trait>() {
2233+
fn migrate_hasher<T: Trait>() {
22322234
if let Some(current_era) = CurrentEra::get() {
22332235
let history_depth = HistoryDepth::get();
22342236
for era in current_era.saturating_sub(history_depth)..=current_era {
@@ -2240,27 +2242,6 @@ fn migrate<T: Trait>() {
22402242
}
22412243
}
22422244

2243-
fn remove_migrate_era<T: Trait>() -> Weight {
2244-
#[allow(dead_code)]
2245-
mod inner {
2246-
pub struct Module<T>(sp_std::marker::PhantomData<T>);
2247-
frame_support::decl_storage! {
2248-
trait Store for Module<T: super::Trait> as Staking {
2249-
pub MigrateEra: Option<super::EraIndex>;
2250-
}
2251-
}
2252-
}
2253-
2254-
if let Releases::V3_0_0 = StorageVersion::get() {
2255-
StorageVersion::put(Releases::V4_0_0);
2256-
inner::MigrateEra::kill();
2257-
2258-
T::DbWeight::get().reads_writes(1, 1)
2259-
} else {
2260-
T::DbWeight::get().reads(1)
2261-
}
2262-
}
2263-
22642245
impl<T: Trait> Module<T> {
22652246
/// The total balance that can be slashed from a stash account as of right now.
22662247
pub fn slashable_balance_of(stash: &T::AccountId) -> BalanceOf<T> {

frame/staking/src/migration.rs

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
use super::*;
2+
3+
use frame_support::weights::Weight;
4+
5+
/// Deprecated storages used for migration only.
6+
mod deprecated {
7+
use crate::{Trait, BalanceOf, SessionIndex, Exposure};
8+
use codec::{Encode, Decode};
9+
use frame_support::{decl_module, decl_storage};
10+
use sp_std::prelude::*;
11+
12+
// edgeware uses `u64` for `Moment`
13+
type Moment = u64;
14+
15+
/// Reward points of an era. Used to split era total payout between validators.
16+
#[derive(Encode, Decode, Default)]
17+
pub struct EraPoints {
18+
/// Total number of points. Equals the sum of reward points for each validator.
19+
pub total: u32,
20+
/// The reward points earned by a given validator. The index of this vec corresponds to the
21+
/// index into the current validator set.
22+
pub individual: Vec<u32>,
23+
}
24+
25+
decl_module! {
26+
pub struct Module<T: Trait> for enum Call where origin: T::Origin { }
27+
}
28+
29+
decl_storage! {
30+
pub trait Store for Module<T: Trait> as Staking {
31+
pub SlotStake: BalanceOf<T>;
32+
33+
/// The currently elected validator set keyed by stash account ID.
34+
pub CurrentElected: Vec<T::AccountId>;
35+
36+
/// The start of the current era.
37+
pub CurrentEraStart: Moment;
38+
39+
/// The session index at which the current era started.
40+
pub CurrentEraStartSessionIndex: SessionIndex;
41+
42+
/// Rewards for the current era. Using indices of current elected set.
43+
pub CurrentEraPointsEarned: EraPoints;
44+
45+
/// Nominators for a particular account that is in action right now. You can't iterate
46+
/// through validators here, but you can find them in the Session module.
47+
///
48+
/// This is keyed by the stash account.
49+
pub Stakers: map hasher(opaque_blake2_256) T::AccountId => Exposure<T::AccountId, BalanceOf<T>>;
50+
}
51+
}
52+
}
53+
54+
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
55+
struct OldStakingLedger<AccountId, Balance: HasCompact> {
56+
pub stash: AccountId,
57+
#[codec(compact)]
58+
pub total: Balance,
59+
#[codec(compact)]
60+
pub active: Balance,
61+
pub unlocking: Vec<UnlockChunk<Balance>>,
62+
}
63+
64+
/// Update storages to current version
65+
///
66+
/// In old version the staking module has several issue about handling session delay, the
67+
/// current era was always considered the active one.
68+
///
69+
/// After the migration the current era will still be considered the active one for the era of
70+
/// the upgrade. And the delay issue will be fixed when planning the next era.
71+
// * create:
72+
// * ActiveEraStart
73+
// * ErasRewardPoints
74+
// * ActiveEra
75+
// * ErasStakers
76+
// * ErasStakersClipped
77+
// * ErasValidatorPrefs
78+
// * ErasTotalStake
79+
// * ErasStartSessionIndex
80+
// * translate StakingLedger
81+
// * removal of:
82+
// * Stakers
83+
// * SlotStake
84+
// * CurrentElected
85+
// * CurrentEraStart
86+
// * CurrentEraStartSessionIndex
87+
// * CurrentEraPointsEarned
88+
pub fn migrate_to_simple_payouts<T: Trait>() -> Weight {
89+
sp_runtime::print("🕊️ Migrating Staking...");
90+
let current_era_start_index = deprecated::CurrentEraStartSessionIndex::get();
91+
let current_era = <Module<T> as Store>::CurrentEra::get().unwrap_or(0);
92+
let current_era_start = deprecated::CurrentEraStart::get();
93+
<Module<T> as Store>::ErasStartSessionIndex::insert(current_era, current_era_start_index);
94+
<Module<T> as Store>::ActiveEra::put(ActiveEraInfo {
95+
index: current_era,
96+
start: Some(current_era_start),
97+
});
98+
99+
let current_elected = deprecated::CurrentElected::<T>::get();
100+
let mut current_total_stake = <BalanceOf<T>>::zero();
101+
for validator in &current_elected {
102+
let exposure = deprecated::Stakers::<T>::get(validator);
103+
current_total_stake += exposure.total;
104+
<Module<T> as Store>::ErasStakers::insert(current_era, validator, &exposure);
105+
106+
let mut exposure_clipped = exposure;
107+
let clipped_max_len = T::MaxNominatorRewardedPerValidator::get() as usize;
108+
if exposure_clipped.others.len() > clipped_max_len {
109+
exposure_clipped.others.sort_unstable_by(|a, b| a.value.cmp(&b.value).reverse());
110+
exposure_clipped.others.truncate(clipped_max_len);
111+
}
112+
<Module<T> as Store>::ErasStakersClipped::insert(current_era, validator, exposure_clipped);
113+
114+
let pref = <Module<T> as Store>::Validators::get(validator);
115+
<Module<T> as Store>::ErasValidatorPrefs::insert(current_era, validator, pref);
116+
}
117+
<Module<T> as Store>::ErasTotalStake::insert(current_era, current_total_stake);
118+
119+
let points = deprecated::CurrentEraPointsEarned::get();
120+
<Module<T> as Store>::ErasRewardPoints::insert(current_era, EraRewardPoints {
121+
total: points.total,
122+
individual: current_elected.iter().cloned().zip(points.individual.iter().cloned()).collect(),
123+
});
124+
125+
let res = <Module<T> as Store>::Ledger::translate_values(
126+
|old: OldStakingLedger<T::AccountId, BalanceOf<T>>| StakingLedger {
127+
stash: old.stash,
128+
total: old.total,
129+
active: old.active,
130+
unlocking: old.unlocking,
131+
claimed_rewards: vec![],
132+
}
133+
);
134+
if let Err(e) = res {
135+
frame_support::print("Encountered error in migration of Staking::Ledger map.");
136+
frame_support::print("The number of removed key/value is:");
137+
frame_support::print(e);
138+
}
139+
140+
// Kill old storages
141+
deprecated::Stakers::<T>::remove_all();
142+
deprecated::SlotStake::<T>::kill();
143+
deprecated::CurrentElected::<T>::kill();
144+
deprecated::CurrentEraStart::kill();
145+
deprecated::CurrentEraStartSessionIndex::kill();
146+
deprecated::CurrentEraPointsEarned::kill();
147+
148+
sp_runtime::print("🕊️ Done Staking.");
149+
// TODO: determine actual weight?
150+
T::MaximumBlockWeight::get()
151+
}

frame/system/src/migration.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn migrate_block_hash<T: Trait>() -> Weight {
1414
let db = T::DbWeight::get();
1515
let block_num = Number::<T>::get();
1616
if block_num > One::one() {
17-
sp_runtime::print("Migrating BlockHashes...");
17+
sp_runtime::print("🕊️ Migrating BlockHashes...");
1818
BlockHash::<T>::migrate_key_from_blake(T::BlockNumber::zero());
1919
let mut n = block_num - One::one() - One::one();
2020
let mut migrations = 1;
@@ -26,7 +26,7 @@ pub fn migrate_block_hash<T: Trait>() -> Weight {
2626
}
2727
n -= One::one();
2828
}
29-
sp_runtime::print("Done BlockHashes");
29+
sp_runtime::print("🕊️ Done BlockHashes");
3030
db.reads_writes(migrations + 1, migrations)
3131
} else {
3232
sp_runtime::print("No BlockHashes to migrate...");
@@ -35,7 +35,7 @@ pub fn migrate_block_hash<T: Trait>() -> Weight {
3535
}
3636

3737
pub fn migrate_accounts<T: Trait>() -> Weight {
38-
sp_runtime::print("Migrating Accounts...");
38+
sp_runtime::print("🕊️ Migrating Accounts...");
3939
let mut count = 0u32;
4040
if let Ok(accounts) = Vec::<T::AccountId>::decode(&mut &include_bytes!("accounts.scale")[..]) {
4141
for a in &accounts {
@@ -50,6 +50,6 @@ pub fn migrate_accounts<T: Trait>() -> Weight {
5050
}
5151
}
5252
sp_runtime::print(count);
53-
sp_runtime::print("Done Accounts.");
53+
sp_runtime::print("🕊️ Done Accounts.");
5454
T::MaximumBlockWeight::get()
5555
}

frame/transaction-payment/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ decl_module! {
232232
).unwrap(),
233233
);
234234
}
235+
236+
fn on_runtime_upgrade() -> Weight {
237+
migration::on_runtime_upgrade::<T>()
238+
}
235239
}
236240
}
237241

0 commit comments

Comments
 (0)