use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};
use crate::*;
#[derive(
Clone,
PartialEq,
Eq,
parity_scale_codec::Decode,
parity_scale_codec::Encode,
sp_runtime::RuntimeDebug,
)]
pub struct OldParachainBondConfig<AccountId> {
pub account: AccountId,
pub percent: sp_runtime::Percent,
}
pub struct MigrateParachainBondConfig<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateParachainBondConfig<T> {
fn on_runtime_upgrade() -> Weight {
let (account, percent) = if let Some(config) =
frame_support::storage::migration::get_storage_value::<
OldParachainBondConfig<T::AccountId>,
>(b"ParachainStaking", b"ParachainBondInfo", &[])
{
(config.account, config.percent)
} else {
return Weight::default();
};
let pbr = InflationDistributionAccount { account, percent };
let treasury = InflationDistributionAccount::<T::AccountId>::default();
let configs: InflationDistributionConfig<T::AccountId> = [pbr, treasury].into();
InflationDistributionInfo::<T>::put(configs);
frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
b"ParachainStaking",
b"ParachainBondInfo",
));
Weight::default()
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
use frame_support::ensure;
use parity_scale_codec::Encode;
let state = frame_support::storage::migration::get_storage_value::<
OldParachainBondConfig<T::AccountId>,
>(b"ParachainStaking", b"ParachainBondInfo", &[]);
ensure!(state.is_some(), "State not found");
Ok(state.unwrap().encode())
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
use frame_support::ensure;
let old_state: OldParachainBondConfig<T::AccountId> =
parity_scale_codec::Decode::decode(&mut &state[..])
.map_err(|_| sp_runtime::DispatchError::Other("Failed to decode old state"))?;
let new_state = InflationDistributionInfo::<T>::get();
let pbr = InflationDistributionAccount {
account: old_state.account,
percent: old_state.percent,
};
let treasury = InflationDistributionAccount::<T::AccountId>::default();
let expected_new_state: InflationDistributionConfig<T::AccountId> = [pbr, treasury].into();
ensure!(new_state == expected_new_state, "State migration failed");
Ok(())
}
}