use super::*;
use frame_support::{
pallet_prelude::*,
traits::{Get, UncheckedOnRuntimeUpgrade},
};
use sp_std::{marker::PhantomData, vec::Vec};
pub mod versioned {
use super::*;
pub type V3ToV4<T> = frame_support::migrations::VersionedMigration<
3,
4,
unchecked_migration::UncheckedMigrationXcmVersion<{ xcm::v5::VERSION }, T>,
Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;
}
mod unchecked_migration {
use super::*;
use xcm::IntoVersion;
pub struct UncheckedMigrationXcmVersion<const XCM_VERSION: u32, T: Config>(PhantomData<T>);
impl<const XCM_VERSION: u32, T: Config> UncheckedOnRuntimeUpgrade
for UncheckedMigrationXcmVersion<XCM_VERSION, T>
{
fn on_runtime_upgrade() -> Weight {
let mut consumed_weight = Weight::zero();
AssetIdToLocation::<T>::translate::<xcm::VersionedLocation, _>(
|asset_id, multi_location| {
consumed_weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
multi_location
.into_version(XCM_VERSION)
.map_err(|_| {
log::error!(
"Failed to convert AssetIdToLocation value for asset Id: {asset_id:?}",
);
})
.ok()
},
);
let location_to_id_entries: Vec<_> = AssetLocationToId::<T>::drain().collect();
for (multi_location, asset_id) in location_to_id_entries {
consumed_weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
if let Ok(new_location) = multi_location.into_version(XCM_VERSION) {
AssetLocationToId::<T>::insert(new_location, asset_id);
} else {
log::error!(
"Failed to convert AssetLocationToId value for asset Id: {asset_id:?}",
);
}
}
let location_to_price_entries: Vec<_> =
AssetLocationUnitsPerSecond::<T>::drain().collect();
for (multi_location, price) in location_to_price_entries {
consumed_weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
if let Ok(new_location) = multi_location.into_version(XCM_VERSION) {
AssetLocationUnitsPerSecond::<T>::insert(new_location, price);
} else {
log::error!("Failed to convert AssetLocationUnitsPerSecond value failed!");
}
}
consumed_weight
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
let mut count = AssetIdToLocation::<T>::iter().collect::<Vec<_>>().len();
count += AssetLocationToId::<T>::iter().collect::<Vec<_>>().len();
count += AssetLocationUnitsPerSecond::<T>::iter()
.collect::<Vec<_>>()
.len();
Ok((count as u32).encode())
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
let old_count: u32 = Decode::decode(&mut state.as_ref())
.map_err(|_| "Cannot decode data from pre_upgrade")?;
let mut count = AssetIdToLocation::<T>::iter().collect::<Vec<_>>().len();
count += AssetLocationToId::<T>::iter().collect::<Vec<_>>().len();
count += AssetLocationUnitsPerSecond::<T>::iter()
.collect::<Vec<_>>()
.len();
assert_eq!(old_count, count as u32);
Ok(())
}
}
}