-
Notifications
You must be signed in to change notification settings - Fork 69
Add migration for ForeignAssetCreator #581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ParthDesai
merged 6 commits into
tanssi-update-1.11.0
from
add-foreign-asset-creator-migration
Jun 6, 2024
+207
−2
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
076476e
Add migration for ForeignAssetCreator
ParthDesai cb15a21
remove `clear_prefix` call as it is not required
ParthDesai e476249
use accurate weight
ParthDesai 8d094a0
add test for migration
ParthDesai 454d71f
remove un-necessary import
ParthDesai 5952aaf
fix clippy
ParthDesai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,15 +36,19 @@ | |
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| use frame_support::ensure; | ||
| use frame_support::migration::storage_key_iter; | ||
|
|
||
| use { | ||
| cumulus_primitives_core::ParaId, | ||
| frame_support::{ | ||
| pallet_prelude::GetStorageVersion, | ||
| traits::{OnRuntimeUpgrade, PalletInfoAccess, StorageVersion}, | ||
| weights::Weight, | ||
| StoragePrefixedMap, | ||
| }, | ||
| pallet_configuration::{weights::WeightInfo as _, HostConfiguration}, | ||
| pallet_foreign_asset_creator::AssetId, | ||
| pallet_foreign_asset_creator::{AssetIdToForeignAsset, ForeignAssetToAssetId}, | ||
| pallet_migrations::{GetMigrations, Migration}, | ||
| sp_core::Get, | ||
| sp_std::{collections::btree_set::BTreeSet, marker::PhantomData, prelude::*}, | ||
|
|
@@ -516,6 +520,9 @@ where | |
| Runtime: pallet_xcm::Config, | ||
| <Runtime as pallet_balances::Config>::RuntimeHoldReason: | ||
| From<pallet_pooled_staking::HoldReason>, | ||
| Runtime: pallet_foreign_asset_creator::Config, | ||
| <Runtime as pallet_foreign_asset_creator::Config>::ForeignAsset: | ||
| TryFrom<staging_xcm::v3::MultiLocation>, | ||
| { | ||
| fn get_migrations() -> Vec<Box<dyn Migration>> { | ||
| // let migrate_invulnerables = MigrateInvulnerables::<Runtime>(Default::default()); | ||
|
|
@@ -540,6 +547,8 @@ where | |
| RegistrarParaManagerMigration::<Runtime>(Default::default()); | ||
|
|
||
| let migrate_pallet_xcm_v4 = MigrateToLatestXcmVersion::<Runtime>(Default::default()); | ||
| let foreign_asset_creator_migration = | ||
| ForeignAssetCreatorMigration::<Runtime>(Default::default()); | ||
|
|
||
| vec![ | ||
| // Applied in runtime 200 | ||
|
|
@@ -564,6 +573,86 @@ where | |
| Box::new(migrate_registrar_pending_verification), | ||
| Box::new(migrate_registrar_manager), | ||
| Box::new(migrate_pallet_xcm_v4), | ||
| Box::new(foreign_asset_creator_migration), | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| pub struct ForeignAssetCreatorMigration<Runtime>(pub PhantomData<Runtime>); | ||
|
|
||
| impl<Runtime> Migration for ForeignAssetCreatorMigration<Runtime> | ||
| where | ||
| Runtime: pallet_foreign_asset_creator::Config, | ||
| <Runtime as pallet_foreign_asset_creator::Config>::ForeignAsset: | ||
| TryFrom<staging_xcm::v3::MultiLocation>, | ||
| { | ||
| fn friendly_name(&self) -> &str { | ||
| "TM_ForeignAssetCreatorMigration" | ||
| } | ||
|
|
||
| fn migrate(&self, _available_weight: Weight) -> Weight { | ||
| use frame_support::pallet_prelude::*; | ||
|
|
||
| use staging_xcm::v3::MultiLocation as OldLocation; | ||
|
|
||
| let pallet_prefix = AssetIdToForeignAsset::<Runtime>::pallet_prefix(); | ||
| let asset_id_to_foreign_asset_storage_prefix = | ||
| AssetIdToForeignAsset::<Runtime>::storage_prefix(); | ||
| let foreign_asset_to_asset_id_prefix = ForeignAssetToAssetId::<Runtime>::storage_prefix(); | ||
|
|
||
| // Data required to migrate ForeignAsset values | ||
| // Read all the data into memory. | ||
| let asset_id_to_foreign_asset_data: Vec<_> = | ||
| storage_key_iter::<AssetId<Runtime>, OldLocation, Blake2_128Concat>( | ||
| pallet_prefix, | ||
| asset_id_to_foreign_asset_storage_prefix, | ||
| ) | ||
| .drain() | ||
| .collect(); | ||
|
|
||
| // Data required to migrate ForeignAsset keys | ||
| let foreign_asset_to_asset_id_data: Vec<_> = | ||
| storage_key_iter::<OldLocation, AssetId<Runtime>, Blake2_128Concat>( | ||
| pallet_prefix, | ||
| foreign_asset_to_asset_id_prefix, | ||
| ) | ||
| .drain() | ||
| .collect(); | ||
|
|
||
| let migrated_count = asset_id_to_foreign_asset_data | ||
| .len() | ||
| .saturating_add(foreign_asset_to_asset_id_data.len()); | ||
|
|
||
| log::info!("Migrating {:?} elements", migrated_count); | ||
|
|
||
| // Write to the new storage with removed and added fields | ||
| for (asset_id, old_location) in asset_id_to_foreign_asset_data { | ||
| if let Ok(new_location) = Runtime::ForeignAsset::try_from(old_location) { | ||
| AssetIdToForeignAsset::<Runtime>::insert(asset_id, new_location); | ||
| } else { | ||
| log::warn!("Location could not be converted safely to xcmV4") | ||
| } | ||
| } | ||
|
|
||
| for (old_location, asset_id) in foreign_asset_to_asset_id_data { | ||
| if let Ok(new_location) = Runtime::ForeignAsset::try_from(old_location) { | ||
| ForeignAssetToAssetId::<Runtime>::insert(new_location, asset_id); | ||
| } else { | ||
| log::warn!("Location could not be converted safely to xcmV4") | ||
| } | ||
| } | ||
|
|
||
| // One db read and one db write per element, plus the on-chain storage | ||
| Runtime::DbWeight::get().reads_writes(migrated_count as u64, 2 * migrated_count as u64) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, 1 write when using |
||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn pre_upgrade(&self) -> Result<Vec<u8>, DispatchError> { | ||
| Ok(vec![]) | ||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn post_upgrade(&self, _state: Vec<u8>) -> Result<(), DispatchError> { | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YOu can probably do both migrations here and not iterate twice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. But, that would mean I am assuming the semantics of these two storage items in runtime.