From ea558d2c8791d4beedce699ffa22189376981303 Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Thu, 17 Oct 2024 12:58:17 +0200 Subject: [PATCH] Add AssetHub to XCM simulator and laosish teleport test (#813) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * init of integration * test cxm-simultor moved * add laos-runtime dep * renaming staging packages * add simulator * simulator example integrated * simulator example integrated * integrate simultaor * integrate simultaor * starign point * starign point * active DmpMessage * added mock msg queue * pallet mock msg queue compiling * thinking to the runtime * refactoring * xcm-simulator copiles * added laosish * added laosish * starting laosish * laos-primitives feature fix * laosish has lso balances * laosish uses laos system * rollback xcm_config * add laosish xcm_config * added types * using accountid20 * using accountid20 * balances is from laos * balances is from laos * system by laos * remove warning * removed unused code * xcm_config integration * removed mock * rollback some changes * remove staging_ * ParachainInfo integrated * facing xcm_cofig * remove warning * fixing Cargo.toml * using Cargo.lock of main * using workspace deps * fix comoilation * fmt * refactoring * add Laoish networjk * deactive insecure zero ED * clippy * added cumulus_xcm * active SiblingParachainAsNative * refactoring * refactoring * refactoring * added laosish ump test * refactoring * rafactoring: mod tests extracted * created module test laoish * initializing ethereum * funding account * clippy * add a failing tests * set the relay chain * added constants for chain ID * remove TODO * add dmp test * added test transfer balances * test laos is able to move its balance or relay chain * added reserve transfer between parachains * checking the sovereign account * comment the code of transfer para A -> B * xcm-simultaror parachains have pallet_assets * using parent * test on force create * name refactoring * using foreign location * added check for events * Instance2 * added pallet assets * sibling account id * all tests green * prefund sibling sovreign accounts * refactoring * prefund sovreign account in laosish * laosish is able to create assets in PAraB * removed unuseful code * fix clippy * fix compilation * add params for AssetHub * add params for AssetHub * configure teleporting * no reserve transfer are accepted * starting teleporting test * trying teleport * Trying to teleport * Teleport tokens burnt * Last version2 * Last version * renamed xcm-simulator crate * test ignored * LAOS xcm_config set for Polkadot * WIP: assethub from xcm workshop; change paras xcm v4 to v3 * switch back v3 to latest and fix compilation errors * remove todos; para_teleporter trusts assethub * fix teleport asset destination (broken test); weight unlimited in teleport asset; log events in assethub; genesis funding for sovereign accounts * ParaTeleporter decl parachain runtime fixed; send xcm create asset & commented out set metadata * fix loc conv assethub * Teleport working * Funding assetHub account correctly * Teleport back from AssetHub OK * fix clippy warnings * laosish teleport test (to be fixed); teleport filter accepts everything * assethub does not need parachaininfo; set para_id in laosish genesis * complete laosish roundtrip test * remove `ParaTeleporter` * remove todo and add comment about `IsTeleporter` * Fmt + clippy fixed * remove duplicated comments --------- Co-authored-by: Tomás Senovilla Polo Co-authored-by: luispdm <17044119+luispdm@users.noreply.github.com> --- xcm-simulator/src/asset_hub/mod.rs | 263 ++++++++++++ .../asset_hub/xcm_config/asset_transactor.rs | 72 ++++ .../src/asset_hub/xcm_config/barrier.rs | 26 ++ .../src/asset_hub/xcm_config/constants.rs | 16 + .../xcm_config/location_converter.rs | 15 + xcm-simulator/src/asset_hub/xcm_config/mod.rs | 47 +++ .../asset_hub/xcm_config/origin_converter.rs | 13 + .../src/asset_hub/xcm_config/reserve.rs | 12 + .../src/asset_hub/xcm_config/teleporter.rs | 43 ++ .../src/asset_hub/xcm_config/weigher.rs | 11 + .../src/laosish/configs/xcm_config.rs | 11 +- xcm-simulator/src/lib.rs | 54 ++- xcm-simulator/src/parachain.rs | 7 +- xcm-simulator/src/parachain_teleporter.rs | 385 ------------------ xcm-simulator/src/relay_chain.rs | 2 +- xcm-simulator/src/tests/laosish_xcm.rs | 76 +++- xcm-simulator/src/tests/mod.rs | 43 -- 17 files changed, 628 insertions(+), 468 deletions(-) create mode 100644 xcm-simulator/src/asset_hub/mod.rs create mode 100644 xcm-simulator/src/asset_hub/xcm_config/asset_transactor.rs create mode 100644 xcm-simulator/src/asset_hub/xcm_config/barrier.rs create mode 100644 xcm-simulator/src/asset_hub/xcm_config/constants.rs create mode 100644 xcm-simulator/src/asset_hub/xcm_config/location_converter.rs create mode 100644 xcm-simulator/src/asset_hub/xcm_config/mod.rs create mode 100644 xcm-simulator/src/asset_hub/xcm_config/origin_converter.rs create mode 100644 xcm-simulator/src/asset_hub/xcm_config/reserve.rs create mode 100644 xcm-simulator/src/asset_hub/xcm_config/teleporter.rs create mode 100644 xcm-simulator/src/asset_hub/xcm_config/weigher.rs delete mode 100644 xcm-simulator/src/parachain_teleporter.rs diff --git a/xcm-simulator/src/asset_hub/mod.rs b/xcm-simulator/src/asset_hub/mod.rs new file mode 100644 index 00000000..a0ca9323 --- /dev/null +++ b/xcm-simulator/src/asset_hub/mod.rs @@ -0,0 +1,263 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! AssetHub Parachain runtime mock. + +mod xcm_config; +use crate::mock_msg_queue; +pub use xcm_config::*; +use xcm_simulator::{Asset, AssetFilter}; + +use core::marker::PhantomData; +use frame_support::{ + construct_runtime, derive_impl, parameter_types, + traits::{ + AsEnsureOriginWithArg, ConstU128, ContainsPair, EnsureOrigin, EnsureOriginWithArg, + Everything, + }, + weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight}, +}; +use frame_system::{EnsureRoot, EnsureSigned}; +use sp_core::ConstU32; +use sp_runtime::{ + traits::{Get, IdentityLookup}, + AccountId32, +}; +use sp_std::prelude::*; +use xcm::latest::prelude::*; +use xcm_builder::{EnsureXcmOrigin, SignedToAccountId32}; +use xcm_executor::{traits::ConvertLocation, XcmExecutor}; + +pub type AccountId = AccountId32; +pub type Balance = u128; + +parameter_types! { + pub const BlockHashCount: u64 = 250; +} + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] +impl frame_system::Config for Runtime { + type AccountId = AccountId; + type Lookup = IdentityLookup; + type Block = Block; + type AccountData = pallet_balances::AccountData; +} + +#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] +impl pallet_balances::Config for Runtime { + type Balance = Balance; + type ExistentialDeposit = ConstU128<1>; + type AccountStore = System; +} + +parameter_types! { + pub const AssetDeposit: Balance = 1; + pub const ApprovalDeposit: Balance = 1; + pub const AssetAccountDeposit: Balance = 1; + pub const MetadataDepositBase: Balance = 1; + pub const MetadataDepositPerByte: Balance = 1; +} + +#[derive_impl(pallet_assets::config_preludes::TestDefaultConfig)] +impl pallet_assets::Config for Runtime { + type Currency = Balances; + type Balance = Balance; + type CreateOrigin = AsEnsureOriginWithArg>; + type ForceOrigin = EnsureRoot; + type AssetDeposit = AssetDeposit; + type ApprovalDeposit = ApprovalDeposit; + type AssetAccountDeposit = AssetAccountDeposit; + type MetadataDepositBase = MetadataDepositBase; + type MetadataDepositPerByte = MetadataDepositPerByte; + type Freezer = (); +} + +// `EnsureOriginWithArg` impl for `CreateOrigin` which allows only XCM origins +// which are locations containing the class location. +pub struct ForeignCreators; +impl EnsureOriginWithArg for ForeignCreators { + type Success = AccountId; + + fn try_origin( + o: RuntimeOrigin, + a: &Location, + ) -> sp_std::result::Result { + let origin_location = pallet_xcm::EnsureXcm::::try_origin(o.clone())?; + if !a.starts_with(&origin_location) { + return Err(o); + } + xcm_config::location_converter::LocationConverter::convert_location(&origin_location) + .ok_or(o) + } + + #[cfg(feature = "runtime-benchmarks")] + fn try_successful_origin(a: &Location) -> Result { + Ok(pallet_xcm::Origin::Xcm(a.clone()).into()) + } +} + +/// Simple conversion of `u32` into an `AssetId` for use in benchmarking. +#[cfg(feature = "runtime-benchmarks")] +pub struct XcmBenchmarkHelper; +#[cfg(feature = "runtime-benchmarks")] +impl pallet_assets::BenchmarkHelper for XcmBenchmarkHelper { + fn create_asset_id_parameter(id: u32) -> Location { + Location::new(1, Junction::Parachain(id)) + } +} + +pub type ForeignAssetsInstance = pallet_assets::Instance2; +#[derive_impl(pallet_assets::config_preludes::TestDefaultConfig)] +impl pallet_assets::Config for Runtime { + type AssetId = Location; + type AssetIdParameter = Location; + type Currency = Balances; + type Balance = Balance; + type CreateOrigin = ForeignCreators; + type ForceOrigin = EnsureRoot; + type AssetDeposit = AssetDeposit; + type ApprovalDeposit = ApprovalDeposit; + type AssetAccountDeposit = AssetAccountDeposit; + type MetadataDepositBase = MetadataDepositBase; + type MetadataDepositPerByte = MetadataDepositPerByte; + type Freezer = (); + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkHelper = XcmBenchmarkHelper; +} + +#[cfg(feature = "runtime-benchmarks")] +pub struct UniquesHelper; +#[cfg(feature = "runtime-benchmarks")] +impl pallet_uniques::BenchmarkHelper for UniquesHelper { + fn collection(i: u16) -> Location { + GeneralIndex(i as u128).into() + } + fn item(i: u16) -> AssetInstance { + AssetInstance::Index(i as u128) + } +} + +impl pallet_uniques::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type CollectionId = u32; // To identify collections. + type ItemId = u32; // To identify individual NFTs. + type Currency = Balances; + type ForceOrigin = EnsureRoot; + type CollectionDeposit = ConstU128<1_000>; + type ItemDeposit = ConstU128<1_000>; + type MetadataDepositBase = ConstU128<1_000>; + type AttributeDepositBase = ConstU128<1_000>; + type DepositPerByte = ConstU128<1>; + type StringLimit = ConstU32<64>; + type KeyLimit = ConstU32<64>; + type ValueLimit = ConstU32<128>; + type WeightInfo = (); + #[cfg(feature = "runtime-benchmarks")] + type Helper = (); + type CreateOrigin = AsEnsureOriginWithArg>; + type Locker = (); +} + +impl pallet_uniques::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type CollectionId = Location; + type ItemId = AssetInstance; + type Currency = Balances; + type CreateOrigin = ForeignCreators; + type ForceOrigin = EnsureRoot; + type CollectionDeposit = ConstU128<1_000>; + type ItemDeposit = ConstU128<1_000>; + type MetadataDepositBase = ConstU128<1_000>; + type AttributeDepositBase = ConstU128<1_000>; + type DepositPerByte = ConstU128<1>; + type StringLimit = ConstU32<64>; + type KeyLimit = ConstU32<64>; + type ValueLimit = ConstU32<128>; + type Locker = (); + type WeightInfo = (); + #[cfg(feature = "runtime-benchmarks")] + type Helper = UniquesHelper; +} + +parameter_types! { + pub const ReservedXcmpWeight: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_div(4), 0); + pub const ReservedDmpWeight: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_div(4), 0); +} + +impl mock_msg_queue::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type XcmExecutor = XcmExecutor; +} + +pub type LocalOriginToLocation = + SignedToAccountId32; + +pub struct TrustedLockerCase(PhantomData); +impl> ContainsPair for TrustedLockerCase { + fn contains(origin: &Location, asset: &Asset) -> bool { + let (o, a) = T::get(); + a.matches(asset) && &o == origin + } +} + +parameter_types! { + pub RelayTokenForRelay: (Location, AssetFilter) = (Parent.into(), Wild(AllOf { id: Parent.into(), fun: WildFungible })); +} + +pub type TrustedLockers = TrustedLockerCase; + +impl pallet_xcm::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type SendXcmOrigin = EnsureXcmOrigin; + type XcmRouter = XcmRouter; + type ExecuteXcmOrigin = EnsureXcmOrigin; + type XcmExecuteFilter = Everything; + type XcmExecutor = XcmExecutor; + type XcmTeleportFilter = Everything; + type XcmReserveTransferFilter = Everything; + type Weigher = weigher::Weigher; + type UniversalLocation = constants::UniversalLocation; + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100; + type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion; + type Currency = Balances; + type CurrencyMatcher = (); + type TrustedLockers = TrustedLockers; + type SovereignAccountOf = location_converter::LocationConverter; + type MaxLockers = ConstU32<8>; + type MaxRemoteLockConsumers = ConstU32<0>; + type RemoteLockConsumerIdentifier = (); + type WeightInfo = pallet_xcm::TestWeightInfo; + type AdminOrigin = EnsureRoot; +} + +type Block = frame_system::mocking::MockBlock; + +impl parachain_info::Config for Runtime {} +construct_runtime!( + pub enum Runtime + { + System: frame_system, + Balances: pallet_balances, + Assets: pallet_assets::, + MsgQueue: mock_msg_queue, + PolkadotXcm: pallet_xcm, + Uniques: pallet_uniques::, + ForeignUniques: pallet_uniques::, + ForeignAssets: pallet_assets::, + } +); diff --git a/xcm-simulator/src/asset_hub/xcm_config/asset_transactor.rs b/xcm-simulator/src/asset_hub/xcm_config/asset_transactor.rs new file mode 100644 index 00000000..a6221cf0 --- /dev/null +++ b/xcm-simulator/src/asset_hub/xcm_config/asset_transactor.rs @@ -0,0 +1,72 @@ +pub use sandbox::*; + +mod sandbox { + use frame_support::{parameter_types, traits::EverythingBut}; + use xcm::prelude::*; + use xcm_builder::{ + FungibleAdapter, FungiblesAdapter, IsConcrete, MatchedConvertedConcreteId, NoChecking, + StartsWith, + }; + use xcm_executor::traits::JustTry; + + use crate::asset_hub::{ + location_converter::LocationConverter, AccountId, Balance, Balances, ForeignAssets, + PolkadotXcm, + }; + + /// AssetTransactor for handling the chain's native token. + pub type FungibleTransactor = FungibleAdapter< + // Use this implementation of the `fungible::*` traits. + // `Balances` is the name given to the balances pallet in this particular example. + // Any implementation of the traits would suffice. + Balances, + // This transactor deals with the native token of the Relay Chain. + // This token is referenced by the Location of the Relay Chain relative to this chain + // -- Location::parent(). + IsConcrete, + // How to convert an XCM Location into a local account id. + // This is also something that's configured in the XCM executor. + LocationConverter, + // The type for account ids, only needed because `fungible` is generic over it. + AccountId, + // Not tracking teleports. + // This recipe only uses reserve asset transfers to handle the Relay Chain token. + (), + >; + + parameter_types! { + pub ParentLocation: Location = Location::parent(); + pub LocalPrefix: Location = Location::here(); + pub CheckingAccount: AccountId = PolkadotXcm::check_account(); + } + + /// Type that matches foreign assets. + /// We do this by matching on all possible Locations and excluding the ones + /// inside our local chain. + pub type ForeignAssetsMatcher = MatchedConvertedConcreteId< + Location, // Asset id. + Balance, // Balance type. + EverythingBut>, // Location matcher. + JustTry, // How to convert from Location to AssetId. + JustTry, // How to convert from u128 to Balance. + >; + + /// AssetTransactor for handling other parachains' native tokens. + pub type ForeignFungiblesTransactor = FungiblesAdapter< + // Use this implementation of the `fungibles::*` traits. + // `Balances` is the name given to the balances pallet in this particular example. + ForeignAssets, + // This transactor deals with the native token of sibling parachains. + ForeignAssetsMatcher, + // How we convert from a Location to an account id. + LocationConverter, + // The `AccountId` type. + AccountId, + // Not tracking teleports since we only use reserve asset transfers. + NoChecking, + // The account for checking. + CheckingAccount, + >; + + pub type AssetTransactor = (FungibleTransactor, ForeignFungiblesTransactor); +} diff --git a/xcm-simulator/src/asset_hub/xcm_config/barrier.rs b/xcm-simulator/src/asset_hub/xcm_config/barrier.rs new file mode 100644 index 00000000..245a708c --- /dev/null +++ b/xcm-simulator/src/asset_hub/xcm_config/barrier.rs @@ -0,0 +1,26 @@ +pub use sandbox::*; + +#[cfg(not(feature = "barrier"))] +mod sandbox { + use frame_support::traits::Everything; + use xcm_builder::AllowUnpaidExecutionFrom; + + pub type Barrier = AllowUnpaidExecutionFrom; +} + +#[cfg(feature = "barrier")] +mod sandbox { + use frame_support::traits::{Contains, Everything}; + use xcm::prelude::*; + use xcm_builder::{AllowExplicitUnpaidExecutionFrom, AllowTopLevelPaidExecutionFrom}; + + pub struct ParentRelay; + impl Contains for ParentRelay { + fn contains(location: &Location) -> bool { + matches!(location.unpack(), (1, [])) + } + } + + pub type Barrier = + (AllowTopLevelPaidExecutionFrom, AllowExplicitUnpaidExecutionFrom); +} diff --git a/xcm-simulator/src/asset_hub/xcm_config/constants.rs b/xcm-simulator/src/asset_hub/xcm_config/constants.rs new file mode 100644 index 00000000..103b1a4c --- /dev/null +++ b/xcm-simulator/src/asset_hub/xcm_config/constants.rs @@ -0,0 +1,16 @@ +use frame_support::parameter_types; +use xcm::latest::prelude::*; + +use crate::asset_hub::MsgQueue; + +parameter_types! { + pub KsmPerSecondPerByte: (AssetId, u128, u128) = (Parent.into(), 1, 1); + pub const MaxAssetsIntoHolding: u32 = 64; +} + +// You are a parachain on Kusama, these are fixed constants for you. +parameter_types! { + pub const KsmLocation: Location = Location::parent(); + pub const RelayNetwork: NetworkId = NetworkId::Kusama; + pub UniversalLocation: InteriorLocation = [GlobalConsensus(RelayNetwork::get()), Parachain(MsgQueue::parachain_id().into())].into(); +} diff --git a/xcm-simulator/src/asset_hub/xcm_config/location_converter.rs b/xcm-simulator/src/asset_hub/xcm_config/location_converter.rs new file mode 100644 index 00000000..05ae57cd --- /dev/null +++ b/xcm-simulator/src/asset_hub/xcm_config/location_converter.rs @@ -0,0 +1,15 @@ +use super::{super::AccountId, constants::RelayNetwork}; +use polkadot_parachain_primitives::primitives::Sibling; +use xcm_builder::{ + AccountId32Aliases, DescribeAllTerminal, DescribeFamily, HashedDescription, ParentIsPreset, + SiblingParachainConvertsVia, +}; + +type LocationToAccountId = ( + ParentIsPreset, + SiblingParachainConvertsVia, + HashedDescription>, + AccountId32Aliases, +); + +pub type LocationConverter = LocationToAccountId; diff --git a/xcm-simulator/src/asset_hub/xcm_config/mod.rs b/xcm-simulator/src/asset_hub/xcm_config/mod.rs new file mode 100644 index 00000000..44616a06 --- /dev/null +++ b/xcm-simulator/src/asset_hub/xcm_config/mod.rs @@ -0,0 +1,47 @@ +pub mod asset_transactor; +pub mod barrier; +pub mod constants; +pub mod location_converter; +pub mod origin_converter; +pub mod reserve; +pub mod teleporter; +pub mod weigher; + +use super::{MsgQueue, PolkadotXcm, RuntimeCall}; +use frame_support::traits::{Everything, Nothing}; +use xcm_builder::{FixedRateOfFungible, FrameTransactionalProcessor}; + +// Generated from `decl_test_network!` +pub type XcmRouter = crate::ParachainXcmRouter; + +pub struct XcmConfig; +impl xcm_executor::Config for XcmConfig { + type RuntimeCall = RuntimeCall; + type XcmSender = XcmRouter; + type AssetTransactor = asset_transactor::AssetTransactor; + type OriginConverter = origin_converter::OriginConverter; + type IsReserve = reserve::TrustedReserves; + type IsTeleporter = teleporter::TrustedTeleporters; + type UniversalLocation = constants::UniversalLocation; + type Barrier = barrier::Barrier; + type Weigher = weigher::Weigher; + type Trader = FixedRateOfFungible; + type ResponseHandler = PolkadotXcm; + type AssetTrap = PolkadotXcm; + type AssetLocker = PolkadotXcm; + type AssetExchanger = (); + type AssetClaims = PolkadotXcm; + type SubscriptionService = PolkadotXcm; + type PalletInstancesInfo = (); + type FeeManager = (); + type MaxAssetsIntoHolding = constants::MaxAssetsIntoHolding; + type MessageExporter = (); + type UniversalAliases = Nothing; + type CallDispatcher = RuntimeCall; + type SafeCallFilter = Everything; + type Aliasers = Nothing; + type TransactionalProcessor = FrameTransactionalProcessor; + type HrmpNewChannelOpenRequestHandler = (); + type HrmpChannelAcceptedHandler = (); + type HrmpChannelClosingHandler = (); +} diff --git a/xcm-simulator/src/asset_hub/xcm_config/origin_converter.rs b/xcm-simulator/src/asset_hub/xcm_config/origin_converter.rs new file mode 100644 index 00000000..efa416fd --- /dev/null +++ b/xcm-simulator/src/asset_hub/xcm_config/origin_converter.rs @@ -0,0 +1,13 @@ +use crate::asset_hub::{ + constants::RelayNetwork, location_converter::LocationConverter, RuntimeOrigin, +}; +use pallet_xcm::XcmPassthrough; +use xcm_builder::{SignedAccountId32AsNative, SovereignSignedViaLocation}; + +type XcmOriginToCallOrigin = ( + SovereignSignedViaLocation, + SignedAccountId32AsNative, + XcmPassthrough, +); + +pub type OriginConverter = XcmOriginToCallOrigin; diff --git a/xcm-simulator/src/asset_hub/xcm_config/reserve.rs b/xcm-simulator/src/asset_hub/xcm_config/reserve.rs new file mode 100644 index 00000000..f01f2cde --- /dev/null +++ b/xcm-simulator/src/asset_hub/xcm_config/reserve.rs @@ -0,0 +1,12 @@ +pub use sandbox::*; + +#[cfg(feature = "start")] +mod sandbox { + pub type TrustedReserves = (); +} + +#[cfg(not(feature = "start"))] +mod sandbox { + /// We don't trust any chain as a reserve. + pub type TrustedReserves = (); +} diff --git a/xcm-simulator/src/asset_hub/xcm_config/teleporter.rs b/xcm-simulator/src/asset_hub/xcm_config/teleporter.rs new file mode 100644 index 00000000..7ab70d2a --- /dev/null +++ b/xcm-simulator/src/asset_hub/xcm_config/teleporter.rs @@ -0,0 +1,43 @@ +pub use sandbox::*; + +mod sandbox { + use frame_support::{parameter_types, traits::ContainsPair}; + use sp_runtime::traits::Get; + use xcm::latest::prelude::*; + use xcm_simulator::{Asset, ParaId}; + + pub struct IsForeignConcreteAsset(sp_std::marker::PhantomData); + impl> ContainsPair + for IsForeignConcreteAsset + { + fn contains(asset: &Asset, origin: &Location) -> bool { + log::trace!(target: "xcm::contains", "IsForeignConcreteAsset asset: {:?}, origin: {:?}", asset, origin); + matches!(asset.id, ref id if IsForeign::contains(&id.0, origin)) + } + } + + pub struct FromSiblingParachain(sp_std::marker::PhantomData); + impl> ContainsPair + for FromSiblingParachain + { + fn contains(a: &Location, b: &Location) -> bool { + // `a` needs to be from `b` at least. + if !a.starts_with(b) { + return false; + } + + // here we check if sibling + match a.unpack() { + (1, interior) => + matches!(interior.first(), Some(Parachain(sibling_para_id)) if sibling_para_id.ne(&u32::from(SelfParaId::get()))), + _ => false, + } + } + } + + parameter_types! { + pub SelfParaId: ParaId = 1000.into(); + } + + pub type TrustedTeleporters = IsForeignConcreteAsset>; +} diff --git a/xcm-simulator/src/asset_hub/xcm_config/weigher.rs b/xcm-simulator/src/asset_hub/xcm_config/weigher.rs new file mode 100644 index 00000000..b97c6437 --- /dev/null +++ b/xcm-simulator/src/asset_hub/xcm_config/weigher.rs @@ -0,0 +1,11 @@ +use crate::asset_hub::RuntimeCall; +use frame_support::parameter_types; +use xcm::latest::prelude::*; +use xcm_builder::FixedWeightBounds; + +parameter_types! { + pub const UnitWeightCost: Weight = Weight::from_parts(1, 1); + pub const MaxInstructions: u32 = 100; +} + +pub type Weigher = FixedWeightBounds; diff --git a/xcm-simulator/src/laosish/configs/xcm_config.rs b/xcm-simulator/src/laosish/configs/xcm_config.rs index e59c4199..3032d517 100644 --- a/xcm-simulator/src/laosish/configs/xcm_config.rs +++ b/xcm-simulator/src/laosish/configs/xcm_config.rs @@ -38,8 +38,7 @@ use xcm_builder::{ TakeWeightCredit, TrailingSetTopicAsId, WithComputedOrigin, }; use xcm_executor::XcmExecutor; - -pub const ASSET_HUB_ID: u32 = crate::PARA_B_ID; +use xcm_simulator::AssetFilter; parameter_types! { pub const RelayLocation: Location = Location::parent(); @@ -136,13 +135,13 @@ pub type Barrier = TrailingSetTopicAsId< >; parameter_types! { - pub ParentTokenPerSecondPerByte: (AssetId, u128, u128) = (AssetId(Parent.into()), 1, 1); + pub ParentTokenPerSecondPerByte: (AssetId, u128, u128) = (Parent.into(), 1, 1); } parameter_types! { pub NativeToken: AssetId = AssetId(Location::here()); pub NativeTokenFilter: AssetFilter = Wild(AllOf { fun: WildFungible, id: NativeToken::get() }); - pub AssetHubLocation: Location = Location::new(1, [Parachain(ASSET_HUB_ID)]); + pub AssetHubLocation: Location = Location::new(1, [Parachain(crate::PARA_ASSETHUB_ID)]); pub AssetHubTrustedTeleporter: (AssetFilter, Location) = (NativeTokenFilter::get(), AssetHubLocation::get()); } @@ -192,7 +191,9 @@ impl pallet_xcm::Config for Runtime { type ExecuteXcmOrigin = EnsureXcmOrigin; type XcmExecuteFilter = Nothing; type XcmExecutor = XcmExecutor; - type XcmTeleportFilter = Nothing; + // it is safe to have `Everything` as `IsTeleporter` performs an additional check on the + // parachain + type XcmTeleportFilter = Everything; type XcmReserveTransferFilter = Everything; type Weigher = FixedWeightBounds; type UniversalLocation = UniversalLocation; diff --git a/xcm-simulator/src/lib.rs b/xcm-simulator/src/lib.rs index 25f24012..83bcdbc0 100644 --- a/xcm-simulator/src/lib.rs +++ b/xcm-simulator/src/lib.rs @@ -14,11 +14,13 @@ // You should have received a copy of the GNU General Public License // along with Polkadot. If not, see . +mod asset_hub; mod laosish; mod parachain; -mod parachain_teleporter; mod relay_chain; +use std::marker::PhantomData; + use hex_literal::hex; use sp_runtime::BuildStorage; @@ -33,8 +35,8 @@ pub const INITIAL_BALANCE: u128 = 1_000_000_000; const PARA_A_ID: u32 = 1; const PARA_B_ID: u32 = 2; -const PARA_TELEPORTER_ID: u32 = 7; const PARA_LAOSISH_ID: u32 = 3; +const PARA_ASSETHUB_ID: u32 = 1000; decl_test_parachain! { pub struct ParaA { @@ -55,11 +57,11 @@ decl_test_parachain! { } decl_test_parachain! { - pub struct ParaTeleporter { - Runtime = parachain::Runtime, - XcmpMessageHandler = parachain::MsgQueue, - DmpMessageHandler = parachain::MsgQueue, - new_ext = para_ext(PARA_TELEPORTER_ID), + pub struct AssetHub { + Runtime = asset_hub::Runtime, + XcmpMessageHandler = asset_hub::MsgQueue, + DmpMessageHandler = asset_hub::MsgQueue, + new_ext = para_ext_asset_hub(PARA_ASSETHUB_ID), } } @@ -90,8 +92,8 @@ decl_test_network! { parachains = vec![ (PARA_A_ID, ParaA), (PARA_B_ID, ParaB), - (PARA_TELEPORTER_ID, ParaTeleporter), (PARA_LAOSISH_ID, Laosish), + (PARA_ASSETHUB_ID, AssetHub), ], } } @@ -137,8 +139,35 @@ pub fn para_ext(para_id: u32) -> sp_io::TestExternalities { (parent_account_id(), INITIAL_BALANCE), (sibling_account_id(PARA_A_ID), INITIAL_BALANCE), (sibling_account_id(PARA_B_ID), INITIAL_BALANCE), - (sibling_account_id(PARA_TELEPORTER_ID), INITIAL_BALANCE), (sibling_account_id(PARA_LAOSISH_ID), INITIAL_BALANCE), + (sibling_account_id(PARA_ASSETHUB_ID), INITIAL_BALANCE), + ], + } + .assimilate_storage(&mut t) + .unwrap(); + + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| { + sp_tracing::try_init_simple(); + System::set_block_number(1); + MsgQueue::set_para_id(para_id.into()); + }); + ext +} + +pub fn para_ext_asset_hub(para_id: u32) -> sp_io::TestExternalities { + use asset_hub::{MsgQueue, Runtime, System}; + + let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); + + pallet_balances::GenesisConfig:: { + balances: vec![ + (ALICE, INITIAL_BALANCE), + (parent_account_id(), INITIAL_BALANCE), + (sibling_account_id(PARA_A_ID), INITIAL_BALANCE), + (sibling_account_id(PARA_B_ID), INITIAL_BALANCE), + (sibling_account_id(PARA_LAOSISH_ID), INITIAL_BALANCE), + (sibling_account_id(PARA_ASSETHUB_ID), INITIAL_BALANCE), ], } .assimilate_storage(&mut t) @@ -162,6 +191,10 @@ pub fn para_ext_ethereum(para_id: u32) -> sp_io::TestExternalities { .assimilate_storage(&mut t) .unwrap(); + parachain_info::GenesisConfig:: { parachain_id: para_id.into(), _config: PhantomData } + .assimilate_storage(&mut t) + .unwrap(); + let mut ext = sp_io::TestExternalities::new(t); ext.execute_with(|| { sp_tracing::try_init_simple(); @@ -182,6 +215,7 @@ pub fn relay_ext() -> sp_io::TestExternalities { (child_account_id(PARA_A_ID), INITIAL_BALANCE), (child_account_id(PARA_B_ID), INITIAL_BALANCE), (child_account_id(PARA_LAOSISH_ID), INITIAL_BALANCE), + (child_account_id(PARA_ASSETHUB_ID), INITIAL_BALANCE), ], } .assimilate_storage(&mut t) @@ -198,7 +232,7 @@ pub fn relay_ext() -> sp_io::TestExternalities { pub type RelayChainPalletXcm = pallet_xcm::Pallet; pub type ParachainPalletXcm = pallet_xcm::Pallet; -pub type ParachainTeleporterPalletXcm = pallet_xcm::Pallet; +pub type AssetHubPalletXcm = pallet_xcm::Pallet; pub type LaosishPalletXcm = pallet_xcm::Pallet; #[frame_support::pallet] diff --git a/xcm-simulator/src/parachain.rs b/xcm-simulator/src/parachain.rs index c0a91ad7..7263f563 100644 --- a/xcm-simulator/src/parachain.rs +++ b/xcm-simulator/src/parachain.rs @@ -30,6 +30,7 @@ use sp_runtime::{ AccountId32, }; use sp_std::prelude::*; +use xcm_simulator::{Asset, AssetFilter}; use crate::mock_msg_queue; use assets_common::{foreign_creators::ForeignCreators, matching::FromSiblingParachain}; @@ -179,7 +180,7 @@ pub type XcmOriginToCallOrigin = ( parameter_types! { pub const UnitWeightCost: Weight = Weight::from_parts(1, 1); - pub KsmPerSecondPerByte: (AssetId, u128, u128) = (AssetId(Parent.into()), 1, 1); + pub KsmPerSecondPerByte: (AssetId, u128, u128) = (Parent.into(), 1, 1); pub const MaxInstructions: u32 = 100; pub const MaxAssetsIntoHolding: u32 = 64; pub ForeignPrefix: Location = (Parent,).into(); @@ -202,7 +203,7 @@ pub type Barrier = AllowUnpaidExecutionFrom; parameter_types! { pub NftCollectionOne: AssetFilter - = Wild(AllOf { fun: WildNonFungible, id: AssetId((Parent, GeneralIndex(1)).into()) }); + = Wild(AllOf { fun: WildNonFungible, id: (Parent, GeneralIndex(1)).into() }); pub NftCollectionOneForRelay: (AssetFilter, Location) = (NftCollectionOne::get(), (Parent,).into()); } @@ -257,7 +258,7 @@ impl> ContainsPair for TrustedL } parameter_types! { - pub RelayTokenForRelay: (Location, AssetFilter) = (Parent.into(), Wild(AllOf { id: AssetId(Parent.into()), fun: WildFungible })); + pub RelayTokenForRelay: (Location, AssetFilter) = (Parent.into(), Wild(AllOf { id: Parent.into(), fun: WildFungible })); } pub type TrustedLockers = TrustedLockerCase; diff --git a/xcm-simulator/src/parachain_teleporter.rs b/xcm-simulator/src/parachain_teleporter.rs deleted file mode 100644 index 1ca21d1d..00000000 --- a/xcm-simulator/src/parachain_teleporter.rs +++ /dev/null @@ -1,385 +0,0 @@ -// Copyright (C) Parity Technologies (UK) Ltd. -// This file is part of Polkadot. - -// Polkadot is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Polkadot is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Polkadot. If not, see . - -//! Parachain runtime mock. - -use core::marker::PhantomData; -use frame_support::{ - construct_runtime, derive_impl, parameter_types, - traits::{ContainsPair, EnsureOrigin, EnsureOriginWithArg, Everything, Nothing}, - weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight}, -}; - -use frame_system::EnsureRoot; -use sp_core::ConstU32; -use sp_runtime::{ - traits::{Get, IdentityLookup}, - AccountId32, -}; -use sp_std::prelude::*; - -use crate::mock_msg_queue; -use assets_common::{foreign_creators::ForeignCreators, matching::FromSiblingParachain}; -use frame_support::traits::AsEnsureOriginWithArg; -use frame_system::EnsureSigned; -use pallet_xcm::XcmPassthrough; -use parachains_common::AssetIdForTrustBackedAssets; -use polkadot_parachain_primitives::primitives::Sibling; -use sp_runtime::codec; -use xcm::latest::prelude::*; -use xcm_builder::{ - Account32Hash, AccountId32Aliases, AllowUnpaidExecutionFrom, EnsureDecodableXcm, - EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, FrameTransactionalProcessor, - FungibleAdapter, GlobalConsensusParachainConvertsFor, IsConcrete, MintLocation, ParentIsPreset, - SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, - SovereignSignedViaLocation, -}; -use xcm_executor::{traits::ConvertLocation, Config, XcmExecutor}; - -pub type SovereignAccountOf = ( - SiblingParachainConvertsVia, - AccountId32Aliases, - ParentIsPreset, -); - -pub type AccountId = AccountId32; -pub type Balance = u128; - -parameter_types! { - pub const BlockHashCount: u64 = 250; -} - -#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] -impl frame_system::Config for Runtime { - type Nonce = u64; - type AccountId = AccountId; - type Lookup = IdentityLookup; - type Block = Block; - type BlockHashCount = BlockHashCount; - type AccountData = pallet_balances::AccountData; -} - -parameter_types! { - pub ExistentialDeposit: Balance = 1; - pub const MaxLocks: u32 = 50; - pub const MaxReserves: u32 = 50; -} - -impl pallet_balances::Config for Runtime { - type MaxLocks = MaxLocks; - type Balance = Balance; - type RuntimeEvent = RuntimeEvent; - type DustRemoval = (); - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type WeightInfo = (); - type MaxReserves = MaxReserves; - type ReserveIdentifier = [u8; 8]; - type RuntimeHoldReason = RuntimeHoldReason; - type RuntimeFreezeReason = RuntimeFreezeReason; - type FreezeIdentifier = (); - type MaxFreezes = ConstU32<0>; -} - -#[cfg(feature = "runtime-benchmarks")] -pub struct UniquesHelper; -#[cfg(feature = "runtime-benchmarks")] -impl pallet_uniques::BenchmarkHelper for UniquesHelper { - fn collection(i: u16) -> Location { - GeneralIndex(i as u128).into() - } - fn item(i: u16) -> AssetInstance { - AssetInstance::Index(i as u128) - } -} - -impl pallet_uniques::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type CollectionId = Location; - type ItemId = AssetInstance; - type Currency = Balances; - type CreateOrigin = ForeignCreatorsUnique; - type ForceOrigin = frame_system::EnsureRoot; - type CollectionDeposit = frame_support::traits::ConstU128<1_000>; - type ItemDeposit = frame_support::traits::ConstU128<1_000>; - type MetadataDepositBase = frame_support::traits::ConstU128<1_000>; - type AttributeDepositBase = frame_support::traits::ConstU128<1_000>; - type DepositPerByte = frame_support::traits::ConstU128<1>; - type StringLimit = ConstU32<64>; - type KeyLimit = ConstU32<64>; - type ValueLimit = ConstU32<128>; - type Locker = (); - type WeightInfo = (); - #[cfg(feature = "runtime-benchmarks")] - type Helper = UniquesHelper; -} - -// `EnsureOriginWithArg` impl for `CreateOrigin` which allows only XCM origins -// which are locations containing the class location. -pub struct ForeignCreatorsUnique; -impl EnsureOriginWithArg for ForeignCreatorsUnique { - type Success = AccountId; - - fn try_origin( - o: RuntimeOrigin, - a: &Location, - ) -> sp_std::result::Result { - let origin_location = pallet_xcm::EnsureXcm::::try_origin(o.clone())?; - if !a.starts_with(&origin_location) { - return Err(o) - } - SovereignAccountOf::convert_location(&origin_location).ok_or(o) - } - - #[cfg(feature = "runtime-benchmarks")] - fn try_successful_origin(a: &Location) -> Result { - Ok(pallet_xcm::Origin::Xcm(a.clone()).into()) - } -} - -parameter_types! { - pub const ReservedXcmpWeight: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_div(4), 0); - pub const ReservedDmpWeight: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_div(4), 0); -} - -parameter_types! { - pub const RelayLocation: Location = Location::parent(); - pub const RelayNetwork: NetworkId = NetworkId::Kusama; - pub UniversalLocation: InteriorLocation = [GlobalConsensus(RelayNetwork::get()), Parachain(MsgQueue::parachain_id().into())].into(); - pub HereLocation: Location = Location::here(); - pub CheckingAccount: AccountId = PolkadotXcm::check_account(); - pub Checking: (AccountId, MintLocation) = (CheckingAccount::get(), MintLocation::Local); -} - -pub type LocationToAccountId = ( - ParentIsPreset, - SiblingParachainConvertsVia, - AccountId32Aliases, - Account32Hash<(), AccountId>, -); - -pub type XcmOriginToCallOrigin = ( - SovereignSignedViaLocation, - SignedAccountId32AsNative, - XcmPassthrough, -); - -parameter_types! { - pub const UnitWeightCost: Weight = Weight::from_parts(1, 1); - pub KsmPerSecondPerByte: (AssetId, u128, u128) = (AssetId(Parent.into()), 1, 1); - pub const MaxInstructions: u32 = 100; - pub const MaxAssetsIntoHolding: u32 = 64; - pub ForeignPrefix: Location = (Parent,).into(); -} - -/// Means for transacting assets on this chain. -pub type LocalAssetTransactor = FungibleAdapter< - // Use this currency: - Balances, - // Use this currency when it is a fungible asset matching the given location or name: - IsConcrete, - // Do a simple pun to convert an AccountId20 Location into a native chain account ID: - LocationToAccountId, - // Our chain's account ID type (we can't get away without mentioning it explicitly): - AccountId, - // We track any teleports. - Checking, ->; - -pub type XcmRouter = EnsureDecodableXcm>; -pub type Barrier = AllowUnpaidExecutionFrom; -parameter_types! { - pub NativeToken: AssetId = AssetId(Location::here()); - pub NativeTokenFilter: AssetFilter = Wild(AllOf { fun: WildFungible, id: NativeToken::get() }); - pub AssetHubLocation: Location = Location::new(1, [Parachain(crate::PARA_A_ID)]); - pub AssetHubTrustedTeleporter: (AssetFilter, Location) = (NativeTokenFilter::get(), AssetHubLocation::get()); -} - -pub type TrustedTeleporters = xcm_builder::Case; - -pub struct XcmConfig; -impl Config for XcmConfig { - type RuntimeCall = RuntimeCall; - type XcmSender = XcmRouter; - type AssetTransactor = LocalAssetTransactor; - type OriginConverter = XcmOriginToCallOrigin; - type IsReserve = (); - type IsTeleporter = TrustedTeleporters; - type UniversalLocation = UniversalLocation; - type Barrier = Barrier; - type Weigher = FixedWeightBounds; - type Trader = FixedRateOfFungible; - type ResponseHandler = (); - type AssetTrap = (); - type AssetLocker = PolkadotXcm; - type AssetExchanger = (); - type AssetClaims = (); - type SubscriptionService = (); - type PalletInstancesInfo = (); - type FeeManager = (); - type MaxAssetsIntoHolding = MaxAssetsIntoHolding; - type MessageExporter = (); - type UniversalAliases = Nothing; - type CallDispatcher = RuntimeCall; - type SafeCallFilter = Everything; - type Aliasers = Nothing; - type TransactionalProcessor = FrameTransactionalProcessor; - type HrmpNewChannelOpenRequestHandler = (); - type HrmpChannelAcceptedHandler = (); - type HrmpChannelClosingHandler = (); -} - -impl mock_msg_queue::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type XcmExecutor = XcmExecutor; -} - -pub type LocalOriginToLocation = SignedToAccountId32; - -pub struct TrustedLockerCase(PhantomData); -impl> ContainsPair for TrustedLockerCase { - fn contains(origin: &Location, asset: &Asset) -> bool { - let (o, a) = T::get(); - a.matches(asset) && &o == origin - } -} - -parameter_types! { - pub RelayTokenForRelay: (Location, AssetFilter) = (Parent.into(), Wild(AllOf { id: AssetId(Parent.into()), fun: WildFungible })); -} - -pub type TrustedLockers = TrustedLockerCase; - -impl pallet_xcm::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type SendXcmOrigin = EnsureXcmOrigin; - type XcmRouter = XcmRouter; - type ExecuteXcmOrigin = EnsureXcmOrigin; - type XcmExecuteFilter = Everything; - type XcmExecutor = XcmExecutor; - type XcmTeleportFilter = Everything; - type XcmReserveTransferFilter = Everything; - type Weigher = FixedWeightBounds; - type UniversalLocation = UniversalLocation; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100; - type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion; - type Currency = Balances; - type CurrencyMatcher = (); - type TrustedLockers = TrustedLockers; - type SovereignAccountOf = LocationToAccountId; - type MaxLockers = ConstU32<8>; - type MaxRemoteLockConsumers = ConstU32<0>; - type RemoteLockConsumerIdentifier = (); - type WeightInfo = pallet_xcm::TestWeightInfo; - type AdminOrigin = EnsureRoot; -} - -pub type ForeignCreatorsSovereignAccountOf = ( - SiblingParachainConvertsVia, - AccountId32Aliases, - ParentIsPreset, - GlobalConsensusParachainConvertsFor, -); - -// Called "Trust Backed" assets because these are generally registered by some account, and users of -// the asset assume it has some claimed backing. The pallet is called `Assets` in -// `construct_runtime` to avoid breaking changes on storage reads. -pub type TrustBackedAssetsInstance = pallet_assets::Instance1; -impl pallet_assets::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type Balance = Balance; - type AssetId = AssetIdForTrustBackedAssets; - type AssetIdParameter = codec::Compact; - type Currency = Balances; - type CreateOrigin = AsEnsureOriginWithArg>; - type ForceOrigin = EnsureRoot; - type AssetDeposit = frame_support::traits::ConstU128<1_000>; - type MetadataDepositBase = frame_support::traits::ConstU128<1_000>; - type MetadataDepositPerByte = frame_support::traits::ConstU128<1_000>; - type ApprovalDeposit = ExistentialDeposit; - type StringLimit = frame_support::traits::ConstU32<50>; - type Freezer = (); - type Extra = (); - type WeightInfo = (); - type CallbackHandle = (); - type AssetAccountDeposit = frame_support::traits::ConstU128<1_000>; - type RemoveItemsLimit = frame_support::traits::ConstU32<1000>; - #[cfg(feature = "runtime-benchmarks")] - type BenchmarkHelper = (); -} - -/// Simple conversion of `u32` into an `AssetId` for use in benchmarking. -#[cfg(feature = "runtime-benchmarks")] -pub struct XcmBenchmarkHelper; -#[cfg(feature = "runtime-benchmarks")] -impl pallet_assets::BenchmarkHelper for XcmBenchmarkHelper { - fn create_asset_id_parameter(id: u32) -> xcm::v3::Location { - xcm::v3::Location::new(1, xcm::v3::Junction::Parachain(id)) - } -} - -/// Assets managed by some foreign location. Note: we do not declare a `ForeignAssetsCall` type, as -/// this type is used in proxy definitions. We assume that a foreign location would not want to set -/// an individual, local account as a proxy for the issuance of their assets. This issuance should -/// be managed by the foreign location's governance. -pub type ForeignAssetsInstance = pallet_assets::Instance2; -impl pallet_assets::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type Balance = Balance; - type AssetId = xcm::v3::Location; - type AssetIdParameter = xcm::v3::Location; - type Currency = Balances; - type CreateOrigin = ForeignCreators< - FromSiblingParachain, xcm::v3::Location>, - ForeignCreatorsSovereignAccountOf, - AccountId, - xcm::v3::Location, - >; - type ForceOrigin = EnsureRoot; - type AssetDeposit = frame_support::traits::ConstU128<1_000>; - type MetadataDepositBase = frame_support::traits::ConstU128<1_000>; - type MetadataDepositPerByte = frame_support::traits::ConstU128<1_000>; - type ApprovalDeposit = ExistentialDeposit; - type StringLimit = ConstU32<64>; - type Freezer = (); - type Extra = (); - type WeightInfo = (); - type CallbackHandle = (); - type AssetAccountDeposit = frame_support::traits::ConstU128<1_000>; - type RemoveItemsLimit = frame_support::traits::ConstU32<1000>; - #[cfg(feature = "runtime-benchmarks")] - type BenchmarkHelper = XcmBenchmarkHelper; -} - -impl parachain_info::Config for Runtime {} - -type Block = frame_system::mocking::MockBlock; - -construct_runtime!( - pub enum Runtime - { - System: frame_system, - ParachainInfo: parachain_info, - Balances: pallet_balances, - MsgQueue: mock_msg_queue, - PolkadotXcm: pallet_xcm, - ForeignUniques: pallet_uniques, - Assets: pallet_assets:: = 50, - ForeignAssets: pallet_assets:: = 53, - } -); diff --git a/xcm-simulator/src/relay_chain.rs b/xcm-simulator/src/relay_chain.rs index 3728dd1d..4d01f0ab 100644 --- a/xcm-simulator/src/relay_chain.rs +++ b/xcm-simulator/src/relay_chain.rs @@ -146,7 +146,7 @@ type LocalOriginConverter = ( parameter_types! { pub const BaseXcmWeight: Weight = Weight::from_parts(1_000, 1_000); pub TokensPerSecondPerByte: (AssetId, u128, u128) = - (AssetId(TokenLocation::get()), 1_000_000_000_000, 1024 * 1024); + (TokenLocation::get().into(), 1_000_000_000_000, 1024 * 1024); pub const MaxInstructions: u32 = 100; pub const MaxAssetsIntoHolding: u32 = 64; } diff --git a/xcm-simulator/src/tests/laosish_xcm.rs b/xcm-simulator/src/tests/laosish_xcm.rs index 3f380bfc..fcdcee8c 100644 --- a/xcm-simulator/src/tests/laosish_xcm.rs +++ b/xcm-simulator/src/tests/laosish_xcm.rs @@ -1,5 +1,8 @@ use super::*; +pub type AssetHubAssetsCall = + pallet_assets::Call; + #[test] fn alish_has_tokens() { MockNet::reset(); @@ -109,11 +112,9 @@ fn ump_transfer_balance() { } #[test] -fn xcmp_create_foreign_asset() { +fn xcmp_create_foreign_asset_in_para_b() { MockNet::reset(); - assert_eq!(laosish::configs::xcm_config::ASSET_HUB_ID, PARA_B_ID); - let para_a_native_asset_location = xcm::v3::Location::new(1, [xcm::v3::Junction::Parachain(PARA_LAOSISH_ID)]); @@ -126,7 +127,7 @@ fn xcmp_create_foreign_asset() { Laosish::execute_with(|| { assert_ok!(LaosishPalletXcm::send_xcm( Here, - (Parent, Parachain(laosish::configs::xcm_config::ASSET_HUB_ID)), + (Parent, Parachain(PARA_B_ID)), Xcm(vec![Transact { origin_kind: OriginKind::Xcm, require_weight_at_most: Weight::from_parts(INITIAL_BALANCE as u64, 1024 * 1024), @@ -143,40 +144,73 @@ fn xcmp_create_foreign_asset() { }); } -#[ignore] // TODO #[test] -fn xcmp_teleport_native_assets_to_asset_hub() { +fn roundtrip_teleport_laosish_to_assethub() { MockNet::reset(); - let para_a_native_asset_location = - xcm::v3::Location::new(1, [xcm::v3::Junction::Parachain(PARA_LAOSISH_ID)]); + let laosish_native_asset_location = Location::new(1, [Junction::Parachain(PARA_LAOSISH_ID)]); - let create_asset = parachain::RuntimeCall::ForeignAssets(ForeignAssetsCall::create { - id: para_a_native_asset_location, + let create_asset = asset_hub::RuntimeCall::ForeignAssets(AssetHubAssetsCall::create { + id: laosish_native_asset_location, admin: sibling_account_id(PARA_LAOSISH_ID), - min_balance: 1000, + min_balance: 1, }); + let teleport_amount_1 = 100; + let teleport_amount_2 = 10; + + let alith: laosish::AccountId = ALITH.into(); + Laosish::execute_with(|| { assert_ok!(LaosishPalletXcm::send_xcm( Here, - (Parent, Parachain(laosish::configs::xcm_config::ASSET_HUB_ID)), + (Parent, Parachain(PARA_ASSETHUB_ID)), Xcm(vec![Transact { origin_kind: OriginKind::Xcm, require_weight_at_most: Weight::from_parts(INITIAL_BALANCE as u64, 1024 * 1024), call: create_asset.encode().into(), }]), )); + + assert_eq!(laosish::Balances::free_balance(alith), INITIAL_BALANCE); + + assert_ok!(LaosishPalletXcm::limited_teleport_assets( + laosish::RuntimeOrigin::signed(ALITH.into()), + Box::new((Parent, Parachain(PARA_ASSETHUB_ID)).into()), + Box::new(AccountId32 { network: None, id: ALICE.into() }.into()), + Box::new((Here, teleport_amount_1).into()), + 0, + WeightLimit::Unlimited, + )); + + assert_eq!(laosish::Balances::free_balance(alith), INITIAL_BALANCE - teleport_amount_1); }); - let amount = 1; + AssetHub::execute_with(|| { + assert_eq!( + asset_hub::ForeignAssets::balance((Parent, Parachain(PARA_LAOSISH_ID)).into(), &ALICE), + teleport_amount_1 + ); - assert_ok!(LaosishPalletXcm::limited_teleport_assets( - laosish::RuntimeOrigin::signed(ALITH.into()), - Box::new(Parachain(laosish::configs::xcm_config::ASSET_HUB_ID).into()), - Box::new(AccountId32 { network: None, id: ALICE.into() }.into()), - Box::new((Here, amount).into()), - 0, - WeightLimit::Limited(Weight::from_parts(INITIAL_BALANCE as u64, 1024 * 1024)), - )); + assert_ok!(AssetHubPalletXcm::limited_teleport_assets( + asset_hub::RuntimeOrigin::signed(ALICE), + Box::new((Parent, Parachain(PARA_LAOSISH_ID)).into()), + Box::new(AccountKey20 { network: None, key: ALITH }.into()), + Box::new(((Parent, Parachain(PARA_LAOSISH_ID)), teleport_amount_2).into()), + 0, + WeightLimit::Unlimited, + )); + + assert_eq!( + asset_hub::ForeignAssets::balance((Parent, Parachain(PARA_LAOSISH_ID)).into(), &ALICE), + teleport_amount_1 - teleport_amount_2 + ); + }); + + Laosish::execute_with(|| { + assert_eq!( + laosish::Balances::free_balance(alith), + INITIAL_BALANCE - (teleport_amount_1 - teleport_amount_2) + ); + }); } diff --git a/xcm-simulator/src/tests/mod.rs b/xcm-simulator/src/tests/mod.rs index 02d4b9c3..d50b09ba 100644 --- a/xcm-simulator/src/tests/mod.rs +++ b/xcm-simulator/src/tests/mod.rs @@ -10,8 +10,6 @@ mod laosish_xcm; pub type ForeignAssetsCall = pallet_assets::Call; -pub type TeleportAssetsCall = - pallet_assets::Call; pub type TrustBackedAssetsCall = pallet_assets::Call; @@ -708,44 +706,3 @@ fn xcmp_create_foreign_asset() { ))); }); } - -#[ignore] // TODO -#[test] -fn teleport_para_teleport_to_para_a() { - MockNet::reset(); - - let para_teleporter_native_asset_location = - xcm::v3::Location::new(1, [xcm::v3::Junction::Parachain(PARA_TELEPORTER_ID)]); - - let create_asset = - parachain_teleporter::RuntimeCall::ForeignAssets(TeleportAssetsCall::create { - id: para_teleporter_native_asset_location, - admin: sibling_account_id(PARA_TELEPORTER_ID), - min_balance: 1000, - }); - - ParaTeleporter::execute_with(|| { - assert_ok!(ParachainTeleporterPalletXcm::send_xcm( - Here, - (Parent, Parachain(PARA_A_ID)), - Xcm(vec![Transact { - origin_kind: OriginKind::Xcm, - require_weight_at_most: Weight::from_parts(INITIAL_BALANCE as u64, 1024 * 1024), - call: create_asset.encode().into(), - }]), - )); - - let amount = 1_000; - - assert_ok!(ParachainTeleporterPalletXcm::limited_teleport_assets( - parachain_teleporter::RuntimeOrigin::signed(ALICE), - Box::new(Parachain(PARA_A_ID).into()), - Box::new(AccountId32 { network: None, id: ALICE.into() }.into()), - Box::new((Here, amount).into()), - 0, - WeightLimit::Limited(Weight::from_parts(INITIAL_BALANCE as u64, 1024 * 1024)), - )); - - assert_eq!(parachain_teleporter::Balances::free_balance(ALICE), INITIAL_BALANCE - amount); - }); -}