use crate::*;
use frame_support::{pallet_prelude::*, traits::UncheckedOnRuntimeUpgrade};
pub type MigrateV5ToV6<T> = frame_support::migrations::VersionedMigration<
5,
6,
unversioned::UncheckedMigrateV5ToV6<T>,
Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;
mod v5 {
use super::*;
#[derive(Clone, Eq, PartialEq, Encode, Decode, TypeInfo, Debug, MaxEncodedLen)]
pub struct OutboundChannelDetails {
pub recipient: ParaId,
pub state: OutboundState,
pub signals_exist: bool,
pub first_index: u16,
pub last_index: u16,
}
#[frame_support::storage_alias]
pub(super) type OutboundXcmpStatus<T: Config> = StorageValue<
Pallet<T>,
BoundedVec<OutboundChannelDetails, <T as Config>::MaxActiveOutboundChannels>,
ValueQuery,
>;
}
mod unversioned {
use super::*;
pub struct UncheckedMigrateV5ToV6<T: Config>(PhantomData<T>);
}
impl<T: Config> UncheckedOnRuntimeUpgrade for unversioned::UncheckedMigrateV5ToV6<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
let translate = |pre: Vec<v5::OutboundChannelDetails>|
-> BoundedVec<OutboundChannelDetails, T::MaxActiveOutboundChannels> {
BoundedVec::defensive_truncate_from(
pre.iter()
.map(|pre_channel_details| OutboundChannelDetails {
recipient: pre_channel_details.recipient,
state: pre_channel_details.state,
signals_exist: pre_channel_details.signals_exist,
first_index: pre_channel_details.first_index,
last_index: pre_channel_details.last_index,
flags: OutboundChannelFlags::empty(),
})
.collect(),
)
};
if OutboundXcmpStatus::<T>::translate(|pre| pre.map(translate)).is_err() {
defensive!(
"unexpected error when performing translation of the OutboundXcmpStatus type \
during storage upgrade to v6"
);
}
let proof_size = 2 * BoundedVec::<
OutboundChannelDetails,
<T as Config>::MaxActiveOutboundChannels,
>::max_encoded_len();
Weight::from_parts(0, proof_size as u64)
.saturating_add(T::DbWeight::get().reads_writes(1, 1))
}
}