// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see .
use super::*;
use frame_support::{
pallet_prelude::*,
traits::{OnRuntimeUpgrade, PalletInfoAccess},
weights::Weight,
};
fn migrate_v0_to_v1, I: 'static>(accounts: &[T::AccountId]) -> Weight {
let onchain_version = Pallet::::on_chain_storage_version();
if onchain_version == 0 {
let total = accounts
.iter()
.map(|a| Pallet::::total_balance(a))
.fold(T::Balance::zero(), |a, e| a.saturating_add(e));
Pallet::::deactivate(total);
// Remove the old `StorageVersion` type.
frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
Pallet::::name().as_bytes(),
"StorageVersion".as_bytes(),
));
// Set storage version to `1`.
StorageVersion::new(1).put::>();
log::info!(target: LOG_TARGET, "Storage to version 1");
T::DbWeight::get().reads_writes(2 + accounts.len() as u64, 3)
} else {
log::info!(
target: LOG_TARGET,
"Migration did not execute. This probably should be removed"
);
T::DbWeight::get().reads(1)
}
}
// NOTE: This must be used alongside the account whose balance is expected to be inactive.
// Generally this will be used for the XCM teleport checking account.
pub struct MigrateToTrackInactive(PhantomData<(T, A, I)>);
impl, A: Get, I: 'static> OnRuntimeUpgrade
for MigrateToTrackInactive
{
fn on_runtime_upgrade() -> Weight {
migrate_v0_to_v1::(&[A::get()])
}
}
// NOTE: This must be used alongside the accounts whose balance is expected to be inactive.
// Generally this will be used for the XCM teleport checking accounts.
pub struct MigrateManyToTrackInactive(PhantomData<(T, A, I)>);
impl, A: Get>, I: 'static> OnRuntimeUpgrade
for MigrateManyToTrackInactive
{
fn on_runtime_upgrade() -> Weight {
migrate_v0_to_v1::(&A::get())
}
}
pub struct ResetInactive(PhantomData<(T, I)>);
impl, I: 'static> OnRuntimeUpgrade for ResetInactive {
fn on_runtime_upgrade() -> Weight {
let onchain_version = Pallet::::on_chain_storage_version();
if onchain_version == 1 {
// Remove the old `StorageVersion` type.
frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
Pallet::::name().as_bytes(),
"StorageVersion".as_bytes(),
));
InactiveIssuance::::kill();
// Set storage version to `0`.
StorageVersion::new(0).put::>();
log::info!(target: LOG_TARGET, "Storage to version 0");
T::DbWeight::get().reads_writes(1, 2)
} else {
log::info!(
target: LOG_TARGET,
"Migration did not execute. This probably should be removed"
);
T::DbWeight::get().reads(1)
}
}
}