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

Remove Old Migration Code #5870

Merged
merged 8 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,6 @@ decl_module! {

fn deposit_event() = default;

fn on_runtime_upgrade() -> Weight {
Self::migrate();

0
}

/// Propose a sensitive action to be taken.
///
/// The dispatch origin of this call must be _Signed_ and the sender must
Expand Down Expand Up @@ -1268,25 +1262,6 @@ decl_module! {
}

impl<T: Trait> Module<T> {
fn migrate() {
use frame_support::{Twox64Concat, migration::{StorageKeyIterator, remove_storage_prefix}};
remove_storage_prefix(b"Democracy", b"VotesOf", &[]);
remove_storage_prefix(b"Democracy", b"VotersFor", &[]);
remove_storage_prefix(b"Democracy", b"Delegations", &[]);
for (who, (end, proposal_hash, threshold, delay))
in StorageKeyIterator::<
ReferendumIndex,
(T::BlockNumber, T::Hash, VoteThreshold, T::BlockNumber),
Twox64Concat,
>::new(b"Democracy", b"ReferendumInfoOf").drain()
{
let status = ReferendumStatus {
end, proposal_hash, threshold, delay, tally: Tally::default()
};
ReferendumInfoOf::<T>::insert(who, ReferendumInfo::Ongoing(status))
}
}

// exposed immutables.

/// Get the amount locked in support of `proposal`; `None` if proposal isn't a valid proposal
Expand Down
34 changes: 1 addition & 33 deletions frame/indices/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@
use sp_std::prelude::*;
use codec::Codec;
use sp_runtime::traits::{
StaticLookup, Member, LookupError, Zero, One, BlakeTwo256, Hash, Saturating, AtLeast32Bit
StaticLookup, Member, LookupError, Zero, Saturating, AtLeast32Bit
};
use frame_support::{Parameter, decl_module, decl_error, decl_event, decl_storage, ensure};
use frame_support::weights::Weight;
use frame_support::dispatch::DispatchResult;
use frame_support::traits::{Currency, ReservableCurrency, Get, BalanceStatus::Reserved};
use frame_support::storage::migration::take_storage_value;
use frame_system::{ensure_signed, ensure_root};
use self::address::Address as RawAddress;

Expand Down Expand Up @@ -99,12 +97,6 @@ decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system = frame_system {
fn deposit_event() = default;

fn on_initialize() -> Weight {
Self::migrations();

0
}

/// Assign an previously unassigned index.
///
/// Payment: `Deposit` is reserved from the sender account.
Expand Down Expand Up @@ -241,30 +233,6 @@ impl<T: Trait> Module<T> {
address::Address::Index(i) => Self::lookup_index(i),
}
}

/// Do any migrations.
fn migrations() {
if let Some(set_count) = take_storage_value::<T::AccountIndex>(b"Indices", b"NextEnumSet", b"") {
// migrations need doing.
let set_size: T::AccountIndex = 64.into();

let mut set_index: T::AccountIndex = Zero::zero();
while set_index < set_count {
let maybe_accounts = take_storage_value::<Vec<T::AccountId>>(b"Indices", b"EnumSet", BlakeTwo256::hash_of(&set_index).as_ref());
if let Some(accounts) = maybe_accounts {
for (item_index, target) in accounts.into_iter().enumerate() {
if target != T::AccountId::default() && !T::Currency::total_balance(&target).is_zero() {
let index = set_index * set_size + T::AccountIndex::from(item_index as u32);
Accounts::<T>::insert(index, (target, BalanceOf::<T>::zero()));
}
}
} else {
break;
}
set_index += One::one();
}
}
}
}

impl<T: Trait> StaticLookup for Module<T> {
Expand Down
8 changes: 0 additions & 8 deletions frame/offences/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,6 @@ decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event() = default;

fn on_runtime_upgrade() -> Weight {
Reports::<T>::remove_all();
ConcurrentReportsIndex::<T>::remove_all();
ReportsByKindIndex::remove_all();

0
}

fn on_initialize(now: T::BlockNumber) -> Weight {
// only decode storage if we can actually submit anything again.
if T::OnOffenceHandler::can_report() {
Expand Down
49 changes: 0 additions & 49 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,18 +1246,6 @@ decl_module! {
}
}

fn on_runtime_upgrade() -> Weight {
// For Kusama the type hasn't actually changed as Moment was u64 and was the number of
// millisecond since unix epoch.
StorageVersion::mutate(|v| {
if matches!(v, Releases::V2_0_0) {
Self::migrate_last_reward_to_claimed_rewards();
}
*v = Releases::V3_0_0;
});
0
}

/// Take the origin account as a stash and lock up `value` of its balance. `controller` will
/// be the account that controls it.
///
Expand Down Expand Up @@ -1935,43 +1923,6 @@ decl_module! {
}

impl<T: Trait> Module<T> {
/// Migrate `last_reward` to `claimed_rewards`
pub fn migrate_last_reward_to_claimed_rewards() {
use frame_support::migration::{StorageIterator, put_storage_value};
// Migrate from `last_reward` to `claimed_rewards`.
// We will construct a vector from `current_era - history_depth` to `last_reward`
// for each validator and nominator.
//
// Old Staking Ledger
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
struct OldStakingLedger<AccountId, Balance: HasCompact> {
pub stash: AccountId,
#[codec(compact)]
pub total: Balance,
#[codec(compact)]
pub active: Balance,
pub unlocking: Vec<UnlockChunk<Balance>>,
pub last_reward: Option<EraIndex>,
}
// Current era and history depth
let current_era = Self::current_era().unwrap_or(0);
let history_depth = Self::history_depth();
let last_payout_era = current_era.saturating_sub(history_depth);
// Convert all ledgers to the new format.
for (hash, old_ledger) in StorageIterator::<OldStakingLedger<T::AccountId, BalanceOf<T>>>::new(b"Staking", b"Ledger").drain() {
let last_reward = old_ledger.last_reward.unwrap_or(0);
let new_ledger = StakingLedger {
stash: old_ledger.stash,
total: old_ledger.total,
active: old_ledger.active,
unlocking: old_ledger.unlocking,
claimed_rewards: (last_payout_era..=last_reward).collect(),
};
put_storage_value(b"Staking", b"Ledger", &hash, new_ledger);
}
MigrateEra::put(current_era);
}

/// The total balance that can be slashed from a stash account as of right now.
pub fn slashable_balance_of(stash: &T::AccountId) -> BalanceOf<T> {
Self::bonded(stash).and_then(Self::ledger).map(|l| l.active).unwrap_or_default()
Expand Down
135 changes: 0 additions & 135 deletions frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4267,141 +4267,6 @@ fn bond_during_era_correctly_populates_claimed_rewards() {

/* These migration tests below can be removed once migration code is removed */

#[test]
fn assert_migration_is_noop() {
let kusama_active_era = "4a0200000190e2721171010000";
let era = ActiveEraInfo::decode(&mut &hex::decode(kusama_active_era).unwrap()[..]).unwrap();
assert_eq!(era.index, 586);
assert_eq!(era.start, Some(1585135674000));
}

#[test]
fn test_last_reward_migration() {
use sp_storage::Storage;

let mut s = Storage::default();

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
struct OldStakingLedger<AccountId, Balance: HasCompact> {
pub stash: AccountId,
#[codec(compact)]
pub total: Balance,
#[codec(compact)]
pub active: Balance,
pub unlocking: Vec<UnlockChunk<Balance>>,
pub last_reward: Option<EraIndex>,
}

let old_staking10 = OldStakingLedger::<u64, u64> {
stash: 0,
total: 10,
active: 10,
unlocking: vec![UnlockChunk{ value: 1234, era: 56}],
last_reward: Some(8),
};

let old_staking11 = OldStakingLedger::<u64, u64> {
stash: 1,
total: 0,
active: 0,
unlocking: vec![],
last_reward: None,
};

let old_staking12 = OldStakingLedger::<u64, u64> {
stash: 2,
total: 100,
active: 100,
unlocking: vec![UnlockChunk{ value: 9876, era: 54}, UnlockChunk{ value: 98, era: 76}],
last_reward: Some(23),
};

let old_staking13 = OldStakingLedger::<u64, u64> {
stash: 3,
total: 100,
active: 100,
unlocking: vec![],
last_reward: Some(23),
};

let data = vec![
(
Ledger::<Test>::hashed_key_for(10),
old_staking10.encode().to_vec()
),
(
Ledger::<Test>::hashed_key_for(11),
old_staking11.encode().to_vec()
),
(
Ledger::<Test>::hashed_key_for(12),
old_staking12.encode().to_vec()
),
(
Ledger::<Test>::hashed_key_for(13),
old_staking13.encode().to_vec()
),
];

s.top = data.into_iter().collect();
sp_io::TestExternalities::new(s).execute_with(|| {
HistoryDepth::put(84);
CurrentEra::put(99);
let nominations = Nominations::<AccountId> {
targets: vec![],
submitted_in: 0,
suppressed: false
};
Nominators::<Test>::insert(3, nominations);
Bonded::<Test>::insert(3, 13);
Staking::migrate_last_reward_to_claimed_rewards();
// Test staker out of range
assert_eq!(
Ledger::<Test>::get(10),
Some(StakingLedger {
stash: 0,
total: 10,
active: 10,
unlocking: vec![UnlockChunk{ value: 1234, era: 56}],
claimed_rewards: vec![],
})
);
// Test staker none
assert_eq!(
Ledger::<Test>::get(11),
Some(StakingLedger {
stash: 1,
total: 0,
active: 0,
unlocking: vec![],
claimed_rewards: vec![],
})
);
// Test staker migration
assert_eq!(
Ledger::<Test>::get(12),
Some(StakingLedger {
stash: 2,
total: 100,
active: 100,
unlocking: vec![UnlockChunk{ value: 9876, era: 54}, UnlockChunk{ value: 98, era: 76}],
claimed_rewards: vec![15,16,17,18,19,20,21,22,23],
})
);
// Test nominator migration
assert_eq!(
Ledger::<Test>::get(13),
Some(StakingLedger {
stash: 3,
total: 100,
active: 100,
unlocking: vec![],
claimed_rewards: vec![15,16,17,18,19,20,21,22,23],
})
);
});
}

#[test]
fn rewards_should_work_before_migration() {
// should check that before migration:
Expand Down
48 changes: 0 additions & 48 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,6 @@ decl_module! {
*fm = T::FeeMultiplierUpdate::convert(*fm)
});
}

fn on_runtime_upgrade() -> Weight {
// TODO: Remove this code after on-chain upgrade from u32 to u64 weights
use sp_runtime::Fixed64;
use frame_support::migration::take_storage_value;
if let Some(old_next_fee_multiplier) = take_storage_value::<Fixed64>(b"TransactionPayment", b"NextFeeMultiplier", &[]) {
let raw_multiplier = old_next_fee_multiplier.into_inner() as i128;
// Fixed64 used 10^9 precision, where Fixed128 uses 10^18, so we need to add 9 zeros.
let new_raw_multiplier: i128 = raw_multiplier.saturating_mul(1_000_000_000);
let new_next_fee_multiplier: Fixed128 = Fixed128::from_parts(new_raw_multiplier);
NextFeeMultiplier::put(new_next_fee_multiplier);
}
0
}
}
}

Expand Down Expand Up @@ -844,38 +830,4 @@ mod tests {
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 100 - 5);
});
}

// TODO Remove after u32 to u64 weights upgrade
#[test]
fn upgrade_to_fixed128_works() {
// TODO You can remove this from dev-dependencies after removing this test
use sp_storage::Storage;
use sp_runtime::Fixed64;
use frame_support::storage::generator::StorageValue;
use frame_support::traits::OnRuntimeUpgrade;
use core::num::NonZeroI128;

let mut s = Storage::default();

let original_multiplier = Fixed64::from_rational(1, 2);

let data = vec![
(
NextFeeMultiplier::storage_value_final_key().to_vec(),
original_multiplier.encode().to_vec()
),
];

s.top = data.into_iter().collect();

sp_io::TestExternalities::new(s).execute_with(|| {
let old_value = NextFeeMultiplier::get();
assert!(old_value != Fixed128::from_rational(1, NonZeroI128::new(2).unwrap()));

// Convert Fixed64(.5) to Fixed128(.5)
TransactionPayment::on_runtime_upgrade();
let new_value = NextFeeMultiplier::get();
assert_eq!(new_value, Fixed128::from_rational(1, NonZeroI128::new(2).unwrap()));
});
}
}