use frame_support::traits::StorageVersion;
pub mod v1 {
use super::*;
use crate::disputes::{Config, Pallet};
use alloc::vec::Vec;
use frame_support::{
pallet_prelude::*, storage_alias, traits::OnRuntimeUpgrade, weights::Weight,
};
use polkadot_primitives::SessionIndex;
#[storage_alias]
type SpamSlots<T: Config> = StorageMap<Pallet<T>, Twox64Concat, SessionIndex, Vec<u32>>;
pub struct MigrateToV1<T>(core::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
fn on_runtime_upgrade() -> Weight {
let mut weight: Weight = Weight::zero();
if StorageVersion::get::<Pallet<T>>() < 1 {
log::info!(target: crate::disputes::LOG_TARGET, "Migrating disputes storage to v1");
weight += migrate_to_v1::<T>();
StorageVersion::new(1).put::<Pallet<T>>();
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
} else {
log::info!(
target: crate::disputes::LOG_TARGET,
"Disputes storage up to date - no need for migration"
);
}
weight
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
log::trace!(
target: crate::disputes::LOG_TARGET,
"SpamSlots before migration: {}",
SpamSlots::<T>::iter().count()
);
ensure!(
StorageVersion::get::<Pallet<T>>() == 0,
"Storage version should be less than `1` before the migration",
);
Ok(Vec::new())
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
log::trace!(target: crate::disputes::LOG_TARGET, "Running post_upgrade()");
ensure!(
StorageVersion::get::<Pallet<T>>() >= 1,
"Storage version should be `1` after the migration"
);
ensure!(
SpamSlots::<T>::iter().count() == 0,
"SpamSlots should be empty after the migration"
);
Ok(())
}
}
pub fn migrate_to_v1<T: Config>() -> Weight {
let mut weight: Weight = Weight::zero();
let res = SpamSlots::<T>::clear(u32::MAX, None);
weight = weight
.saturating_add(T::DbWeight::get().reads_writes(res.loops as u64, res.backend as u64));
weight
}
}