use super::*;
use frame_support::{
pallet_prelude::*,
traits::{OnRuntimeUpgrade, PalletInfoAccess},
weights::Weight,
};
fn migrate_v0_to_v1<T: Config<I>, I: 'static>(accounts: &[T::AccountId]) -> Weight {
let on_chain_version = Pallet::<T, I>::on_chain_storage_version();
if on_chain_version == 0 {
let total = accounts
.iter()
.map(|a| Pallet::<T, I>::total_balance(a))
.fold(T::Balance::zero(), |a, e| a.saturating_add(e));
Pallet::<T, I>::deactivate(total);
frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
Pallet::<T, I>::name().as_bytes(),
"StorageVersion".as_bytes(),
));
StorageVersion::new(1).put::<Pallet<T, I>>();
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)
}
}
pub struct MigrateToTrackInactive<T, A, I = ()>(PhantomData<(T, A, I)>);
impl<T: Config<I>, A: Get<T::AccountId>, I: 'static> OnRuntimeUpgrade
for MigrateToTrackInactive<T, A, I>
{
fn on_runtime_upgrade() -> Weight {
migrate_v0_to_v1::<T, I>(&[A::get()])
}
}
pub struct MigrateManyToTrackInactive<T, A, I = ()>(PhantomData<(T, A, I)>);
impl<T: Config<I>, A: Get<Vec<T::AccountId>>, I: 'static> OnRuntimeUpgrade
for MigrateManyToTrackInactive<T, A, I>
{
fn on_runtime_upgrade() -> Weight {
migrate_v0_to_v1::<T, I>(&A::get())
}
}
pub struct ResetInactive<T, I = ()>(PhantomData<(T, I)>);
impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for ResetInactive<T, I> {
fn on_runtime_upgrade() -> Weight {
let on_chain_version = Pallet::<T, I>::on_chain_storage_version();
if on_chain_version == 1 {
frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
Pallet::<T, I>::name().as_bytes(),
"StorageVersion".as_bytes(),
));
InactiveIssuance::<T, I>::kill();
StorageVersion::new(0).put::<Pallet<T, I>>();
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)
}
}
}