From eabaf12b89015e6bfd2b21563c15da04e47156c2 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Mon, 3 Jun 2024 14:28:55 +0200 Subject: [PATCH] add correct location for centrifuge --- Cargo.lock | 1 + runtime/centrifuge/src/migrations.rs | 3 + runtime/common/Cargo.toml | 1 + .../src/migrations/restricted_location.rs | 76 +++++++++++-------- 4 files changed, 50 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 513bd274b7..1ea4855062 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11652,6 +11652,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "hex", "hex-literal", "log", "num_enum 0.5.11", diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index 38880827ae..16330d0311 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -18,6 +18,9 @@ const IDENTITY_MIGRATION_KEY_LIMIT: u64 = 1000; /// The migration set for Centrifuge @ Polkadot. /// It includes all the migrations that have to be applied on that chain. pub type UpgradeCentrifuge1029 = ( + runtime_common::migrations::restricted_location::MigrateRestrictedTransferLocation< + crate::Runtime, + >, runtime_common::migrations::increase_storage_version::Migration, runtime_common::migrations::increase_storage_version::Migration, pallet_collator_selection::migration::v1::MigrateToV1, diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index 5878dd85d6..bdac1c6db0 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -9,6 +9,7 @@ repository.workspace = true documentation.workspace = true [dependencies] +hex = { workspace = true } hex-literal = { workspace = true } log = { workspace = true } num_enum = { workspace = true } diff --git a/runtime/common/src/migrations/restricted_location.rs b/runtime/common/src/migrations/restricted_location.rs index 164a5f5fd8..0fefd4c61d 100644 --- a/runtime/common/src/migrations/restricted_location.rs +++ b/runtime/common/src/migrations/restricted_location.rs @@ -1,5 +1,5 @@ use cfg_primitives::AccountId; -use cfg_types::{locations::RestrictedTransferLocation, tokens::CurrencyId}; +use cfg_types::{locations::RestrictedTransferLocation, tokens::FilterCurrency}; use frame_support::{ traits::{Get, OnRuntimeUpgrade}, weights::Weight, @@ -12,9 +12,10 @@ use staging_xcm::v4; mod old { use cfg_primitives::AccountId; - use cfg_types::{domain_address::DomainAddress, tokens::CurrencyId}; + use cfg_types::{domain_address::DomainAddress, tokens::FilterCurrency}; use frame_support::{pallet_prelude::*, storage_alias}; use frame_system::pallet_prelude::*; + use hex::FromHex; use pallet_transfer_allowlist::AllowanceDetails; use sp_core::H256; use staging_xcm::v3; @@ -33,28 +34,62 @@ mod old { pallet_transfer_allowlist::Pallet, ( NMapKey, - NMapKey, + NMapKey, NMapKey, ), AllowanceDetails>, OptionQuery, >; - pub fn create_apps_location_v3(account_id: &AccountId) -> v3::Location { + pub fn location_v3_created_by_apps(_account_id: &AccountId) -> v3::Location { // Ref: https://github.com/centrifuge/apps/blob/b59bdd34561a4ccd90e0d803c14a3729fc2f3a6d/centrifuge-app/src/utils/usePermissions.tsx#L386 - //v3::Location::new(1, v3::Junctions::X2((), ())) - todo!() + // for account_id == "4dTeMxuPJCK7zQGhFcgCivSJqBs9Wo2SuMSQeYCCuVJ9xrE2" + v3::Location::new( + 1, + v3::Junctions::X2( + v3::Junction::Parachain(1000), // AssetHub + v3::Junction::AccountId32 { + network: None, + id: <[u8; 32]>::from_hex( + // Address used by Anemoy to withdraw in AssetHub + "10c03288a534d77418e3c19e745dfbc952423e179e1e3baa89e287092fc7802f", + ) + .expect("keep in the array"), + }, + ), + ) } } const LOG_PREFIX: &str = "MigrateRestrictedTransferLocation:"; +fn migrate_location_key(account_id: &AccountId, hash: H256) -> Option { + let old_location = old::location_v3_created_by_apps(account_id); + if BlakeTwo256::hash(&old_location.encode()) == hash { + match v4::Location::try_from(old_location) { + Ok(location) => { + log::info!("{LOG_PREFIX} Hash: '{hash}' migrated!"); + let new_restricted_location = RestrictedTransferLocation::XCM(location); + + Some(new_restricted_location) + } + Err(_) => { + log::error!("{LOG_PREFIX} Non isometric location v3 -> v4"); + None + } + } + } else { + log::error!("{LOG_PREFIX} Hash can not be recovered"); + None + } +} + pub struct MigrateRestrictedTransferLocation(sp_std::marker::PhantomData); impl OnRuntimeUpgrade for MigrateRestrictedTransferLocation where T: pallet_transfer_allowlist::Config< AccountId = AccountId, - CurrencyId = CurrencyId, + CurrencyId = FilterCurrency, Location = RestrictedTransferLocation, >, { @@ -95,32 +130,11 @@ where #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { - todo!() + Ok(Vec::new()) } #[cfg(feature = "try-runtime")] - fn post_upgrade(pre_state: sp_std::vec::Vec) -> Result<(), sp_runtime::TryRuntimeError> { - todo!() - } -} - -fn migrate_location_key(account_id: &AccountId, hash: H256) -> Option { - let old_location = old::create_apps_location_v3(account_id); - if BlakeTwo256::hash(&old_location.encode()) == hash { - match v4::Location::try_from(old_location) { - Ok(location) => { - log::info!("{LOG_PREFIX} Hash: '{hash}' migrated!"); - let new_restricted_location = RestrictedTransferLocation::XCM(location); - - Some(new_restricted_location) - } - Err(_) => { - log::error!("{LOG_PREFIX} Non isometric location v3 -> v4"); - None - } - } - } else { - log::error!("{LOG_PREFIX} Hash can not be recovered"); - None + fn post_upgrade(_pre_state: sp_std::vec::Vec) -> Result<(), sp_runtime::TryRuntimeError> { + Ok(()) } }