Skip to content

Commit 9f08bab

Browse files
committed
Add migration for ForeignAssetCreator
1 parent 4c9d892 commit 9f08bab

4 files changed

Lines changed: 124 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

container-chains/runtime-templates/simple/src/migrations.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use {
2727
},
2828
pallet_migrations::{GetMigrations, Migration},
2929
runtime_common::migrations::{
30-
PolkadotXcmMigrationFixVersion, XcmpQueueMigrationFixVersion, XcmpQueueMigrationV3,
31-
XcmpQueueMigrationV4,
30+
ForeignAssetCreatorMigration, PolkadotXcmMigrationFixVersion, XcmpQueueMigrationFixVersion,
31+
XcmpQueueMigrationV3, XcmpQueueMigrationV4,
3232
},
3333
sp_std::{marker::PhantomData, prelude::*},
3434
};
@@ -71,6 +71,9 @@ where
7171
Runtime: cumulus_pallet_xcmp_queue::Config,
7272
Runtime: pallet_xcm_executor_utils::Config,
7373
Runtime: pallet_xcm::Config,
74+
Runtime: pallet_foreign_asset_creator::Config,
75+
<Runtime as pallet_foreign_asset_creator::Config>::ForeignAsset:
76+
TryFrom<staging_xcm::v3::MultiLocation>,
7477
{
7578
fn get_migrations() -> Vec<Box<dyn Migration>> {
7679
let migrate_polkadot_xcm_v1 =

runtime/common/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ workspace = true
1414

1515
[dependencies]
1616
hex-literal = { workspace = true }
17+
log = { workspace = true }
1718
parity-scale-codec = { workspace = true, features = [ "derive" ] }
1819
scale-info = { workspace = true, features = [ "derive" ] }
1920

2021
# Own
2122
pallet-configuration = { workspace = true }
2223
pallet-data-preservers = { workspace = true }
24+
pallet-foreign-asset-creator = { workspace = true }
2325
pallet-invulnerables = { workspace = true }
2426
pallet-pooled-staking = { workspace = true }
2527
pallet-registrar = { workspace = true }
@@ -41,6 +43,7 @@ cumulus-pallet-xcmp-queue = { workspace = true }
4143

4244
# Polkadot
4345
pallet-xcm = { workspace = true }
46+
staging-xcm = { workspace = true }
4447

4548
sp-core = { workspace = true }
4649
sp-runtime = { workspace = true }
@@ -59,9 +62,11 @@ std = [
5962
"frame-support/std",
6063
"frame-system/std",
6164
"frame-try-runtime?/std",
65+
"log/std",
6266
"pallet-balances/std",
6367
"pallet-configuration/std",
6468
"pallet-data-preservers/std",
69+
"pallet-foreign-asset-creator/std",
6570
"pallet-invulnerables/std",
6671
"pallet-migrations/std",
6772
"pallet-pooled-staking/std",
@@ -74,6 +79,7 @@ std = [
7479
"sp-core/std",
7580
"sp-runtime/std",
7681
"sp-std/std",
82+
"staging-xcm/std",
7783
]
7884

7985
runtime-benchmarks = [
@@ -84,6 +90,7 @@ runtime-benchmarks = [
8490
"pallet-balances/runtime-benchmarks",
8591
"pallet-configuration/runtime-benchmarks",
8692
"pallet-data-preservers/runtime-benchmarks",
93+
"pallet-foreign-asset-creator/runtime-benchmarks",
8794
"pallet-invulnerables/runtime-benchmarks",
8895
"pallet-migrations/runtime-benchmarks",
8996
"pallet-pooled-staking/runtime-benchmarks",
@@ -102,6 +109,7 @@ try-runtime = [
102109
"pallet-balances/try-runtime",
103110
"pallet-configuration/try-runtime",
104111
"pallet-data-preservers/try-runtime",
112+
"pallet-foreign-asset-creator/try-runtime",
105113
"pallet-invulnerables/try-runtime",
106114
"pallet-migrations/try-runtime",
107115
"pallet-pooled-staking/try-runtime",

runtime/common/src/migrations.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@
3636
3737
#[cfg(feature = "try-runtime")]
3838
use frame_support::ensure;
39+
use frame_support::migration::storage_key_iter;
3940

4041
use {
4142
cumulus_primitives_core::ParaId,
4243
frame_support::{
4344
pallet_prelude::GetStorageVersion,
4445
traits::{OnRuntimeUpgrade, PalletInfoAccess, StorageVersion},
4546
weights::Weight,
47+
StoragePrefixedMap,
4648
},
4749
pallet_configuration::{weights::WeightInfo as _, HostConfiguration},
50+
pallet_foreign_asset_creator::AssetId,
51+
pallet_foreign_asset_creator::{AssetIdToForeignAsset, ForeignAssetToAssetId},
4852
pallet_migrations::{GetMigrations, Migration},
4953
sp_core::Get,
5054
sp_std::{collections::btree_set::BTreeSet, marker::PhantomData, prelude::*},
@@ -516,6 +520,9 @@ where
516520
Runtime: pallet_xcm::Config,
517521
<Runtime as pallet_balances::Config>::RuntimeHoldReason:
518522
From<pallet_pooled_staking::HoldReason>,
523+
Runtime: pallet_foreign_asset_creator::Config,
524+
<Runtime as pallet_foreign_asset_creator::Config>::ForeignAsset:
525+
TryFrom<staging_xcm::v3::MultiLocation>,
519526
{
520527
fn get_migrations() -> Vec<Box<dyn Migration>> {
521528
// let migrate_invulnerables = MigrateInvulnerables::<Runtime>(Default::default());
@@ -540,6 +547,8 @@ where
540547
RegistrarParaManagerMigration::<Runtime>(Default::default());
541548

542549
let migrate_pallet_xcm_v4 = MigrateToLatestXcmVersion::<Runtime>(Default::default());
550+
let foreign_asset_creator_migration =
551+
ForeignAssetCreatorMigration::<Runtime>(Default::default());
543552

544553
vec![
545554
// Applied in runtime 200
@@ -564,6 +573,105 @@ where
564573
Box::new(migrate_registrar_pending_verification),
565574
Box::new(migrate_registrar_manager),
566575
Box::new(migrate_pallet_xcm_v4),
576+
Box::new(foreign_asset_creator_migration),
567577
]
568578
}
569579
}
580+
581+
pub struct ForeignAssetCreatorMigration<Runtime>(PhantomData<Runtime>);
582+
583+
impl<Runtime> Migration for ForeignAssetCreatorMigration<Runtime>
584+
where
585+
Runtime: pallet_foreign_asset_creator::Config,
586+
<Runtime as pallet_foreign_asset_creator::Config>::ForeignAsset:
587+
TryFrom<staging_xcm::v3::MultiLocation>,
588+
{
589+
fn friendly_name(&self) -> &str {
590+
"TM_ForeignAssetCreatorMigration"
591+
}
592+
593+
fn migrate(&self, _available_weight: Weight) -> Weight {
594+
use frame_support::pallet_prelude::*;
595+
596+
use staging_xcm::v3::MultiLocation as OldLocation;
597+
598+
let pallet_prefix = AssetIdToForeignAsset::<Runtime>::pallet_prefix();
599+
let asset_id_to_foreign_asset_storage_prefix =
600+
AssetIdToForeignAsset::<Runtime>::storage_prefix();
601+
let foreign_asset_to_asset_id_prefix = ForeignAssetToAssetId::<Runtime>::storage_prefix();
602+
603+
// Data required to migrate ForeignAsset values
604+
// Read all the data into memory.
605+
let asset_id_to_foreign_asset_data: Vec<_> =
606+
storage_key_iter::<AssetId<Runtime>, OldLocation, Blake2_128Concat>(
607+
pallet_prefix,
608+
asset_id_to_foreign_asset_storage_prefix,
609+
)
610+
.drain()
611+
.collect();
612+
613+
// Data required to migrate ForeignAsset keys
614+
let foreign_asset_to_asset_id_data: Vec<_> =
615+
storage_key_iter::<OldLocation, AssetId<Runtime>, Blake2_128Concat>(
616+
pallet_prefix,
617+
foreign_asset_to_asset_id_prefix,
618+
)
619+
.drain()
620+
.collect();
621+
622+
let foreign_asset_to_asset_id_count = foreign_asset_to_asset_id_data.len();
623+
624+
let migrated_count = asset_id_to_foreign_asset_data
625+
.len()
626+
.saturating_add(foreign_asset_to_asset_id_data.len());
627+
628+
log::info!("Migrating {:?} elements", migrated_count);
629+
630+
// Removing older entries with old location as key
631+
let mut removal_result = storage::unhashed::clear_prefix(
632+
&ForeignAssetToAssetId::<Runtime>::final_prefix(),
633+
None,
634+
None,
635+
);
636+
while removal_result.maybe_cursor.is_some() {
637+
removal_result = storage::unhashed::clear_prefix(
638+
&ForeignAssetToAssetId::<Runtime>::final_prefix(),
639+
None,
640+
removal_result.maybe_cursor.as_deref(),
641+
);
642+
}
643+
644+
// Write to the new storage with removed and added fields
645+
for (asset_id, old_location) in asset_id_to_foreign_asset_data {
646+
if let Ok(new_location) = Runtime::ForeignAsset::try_from(old_location) {
647+
AssetIdToForeignAsset::<Runtime>::insert(asset_id, new_location);
648+
} else {
649+
log::warn!("Location could not be converted safely to xcmV4")
650+
}
651+
}
652+
653+
for (old_location, asset_id) in foreign_asset_to_asset_id_data {
654+
if let Ok(new_location) = Runtime::ForeignAsset::try_from(old_location) {
655+
ForeignAssetToAssetId::<Runtime>::insert(new_location, asset_id);
656+
} else {
657+
log::warn!("Location could not be converted safely to xcmV4")
658+
}
659+
}
660+
661+
// One db read and one db write per element, plus the on-chain storage
662+
Runtime::DbWeight::get()
663+
.reads(migrated_count as u64)
664+
.saturating_add(Runtime::DbWeight::get().writes(migrated_count as u64 + 3u64))
665+
.saturating_add(Runtime::DbWeight::get().writes(foreign_asset_to_asset_id_count as u64))
666+
}
667+
668+
#[cfg(feature = "try-runtime")]
669+
fn pre_upgrade(&self) -> Result<Vec<u8>, DispatchError> {
670+
Ok(vec![])
671+
}
672+
673+
#[cfg(feature = "try-runtime")]
674+
fn post_upgrade(&self, _state: Vec<u8>) -> Result<(), DispatchError> {
675+
Ok(())
676+
}
677+
}

0 commit comments

Comments
 (0)