pub mod v1 {
use crate::{types::BalanceOf, *};
use alloc::vec::Vec;
use frame_support::{
storage::unhashed,
traits::{Defensive, Get, GetStorageVersion, OnRuntimeUpgrade},
weights::Weight,
};
use sp_staking::EraIndex;
#[cfg(feature = "try-runtime")]
use frame_support::ensure;
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;
pub struct MigrateToV1<T>(core::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
fn on_runtime_upgrade() -> Weight {
let current = Pallet::<T>::in_code_storage_version();
let onchain = Pallet::<T>::on_chain_storage_version();
log!(
info,
"Running migration with in-code storage version {:?} / onchain {:?}",
current,
onchain
);
if current == 1 && onchain == 0 {
current.put::<Pallet<T>>();
if Head::<T>::exists() {
if let Some((stash, _, deposit)) =
unhashed::take::<(T::AccountId, Vec<EraIndex>, BalanceOf<T>)>(
&Head::<T>::hashed_key(),
)
.defensive()
{
Queue::<T>::insert(stash, deposit);
} else {
}
T::DbWeight::get().reads_writes(2, 3)
} else {
T::DbWeight::get().reads(2)
}
} else {
log!(info, "Migration did not execute. This probably should be removed");
T::DbWeight::get().reads(1)
}
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
ensure!(
Pallet::<T>::on_chain_storage_version() == 0,
"The onchain storage version must be zero for the migration to execute."
);
Ok(Default::default())
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(_: Vec<u8>) -> Result<(), TryRuntimeError> {
ensure!(
Pallet::<T>::on_chain_storage_version() == 1,
"The onchain version must be updated after the migration."
);
Ok(())
}
}
}