From 34520463514e5cd1e059a0c584e4c4b407b6176e Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Mon, 13 Jan 2025 17:30:16 +0200 Subject: [PATCH] chore: testnet ah as reserve --- integration-tests/src/lib.rs | 127 ------------------------------ runtime/testnet/src/config/xcm.rs | 43 +++++++--- 2 files changed, 30 insertions(+), 140 deletions(-) diff --git a/integration-tests/src/lib.rs b/integration-tests/src/lib.rs index c765fda8d..beacbd24e 100644 --- a/integration-tests/src/lib.rs +++ b/integration-tests/src/lib.rs @@ -48,7 +48,6 @@ decl_test_sender_receiver_accounts_parameter_types! { type RelayToParaTest = Test; type SystemParaToParaTest = Test; type ParaToSystemParaTest = Test; -type ParaToRelayTest = Test; fn relay_to_para_sender_assertions(t: RelayToParaTest) { type RuntimeEvent = ::RuntimeEvent; @@ -124,24 +123,6 @@ fn para_to_system_para_sender_assertions(t: ParaToSystemParaTest) { ); } -fn para_to_relay_sender_assertions(t: ParaToRelayTest) { - type RuntimeEvent = ::RuntimeEvent; - PopNetworkPara::assert_xcm_pallet_attempted_complete(Some(Weight::from_parts( - 864_610_000, - 8_799, - ))); - assert_expected_events!( - PopNetworkPara, - vec![ - // Amount to reserve transfer is transferred to Parachain's Sovereign account - RuntimeEvent::Balances(pallet_balances::Event::Burned { who, amount }) => { - who: *who == t.sender.account_id, - amount: *amount == t.args.amount, - }, - ] - ); -} - fn para_to_system_para_receiver_assertions(t: ParaToSystemParaTest) { type RuntimeEvent = ::RuntimeEvent; let sov_pop_net_on_ahr = AssetHubPara::sovereign_account_id_of( @@ -165,29 +146,6 @@ fn para_to_system_para_receiver_assertions(t: ParaToSystemParaTest) { ); } -fn para_to_relay_receiver_assertions(t: ParaToRelayTest) { - type RuntimeEvent = ::RuntimeEvent; - let sov_pop_net_on_relay = RelayRelay::sovereign_account_id_of(RelayRelay::child_location_of( - PopNetworkPara::para_id(), - )); - assert_expected_events!( - RelayRelay, - vec![ - // Amount to reserve transfer is withdrawn from Parachain's Sovereign account - RuntimeEvent::Balances( - pallet_balances::Event::Burned { who, amount } - ) => { - who: *who == sov_pop_net_on_relay.clone().into(), - amount: *amount == t.args.amount, - }, - RuntimeEvent::Balances(pallet_balances::Event::Minted { .. }) => {}, - RuntimeEvent::MessageQueue( - pallet_message_queue::Event::Processed { success: true, .. } - ) => {}, - ] - ); -} - fn relay_to_para_reserve_transfer_assets(t: RelayToParaTest) -> DispatchResult { ::XcmPallet::limited_reserve_transfer_assets( t.signed_origin, @@ -221,35 +179,6 @@ fn para_to_system_para_reserve_transfer_assets(t: ParaToSystemParaTest) -> Dispa ) } -fn para_to_relay_reserve_transfer_assets(t: ParaToRelayTest) -> DispatchResult { - ::PolkadotXcm::limited_reserve_transfer_assets( - t.signed_origin, - bx!(t.args.dest.into()), - bx!(t.args.beneficiary.into()), - bx!(t.args.assets.into()), - t.args.fee_asset_item, - t.args.weight_limit, - ) -} - -// Funds Pop with relay tokens -fn fund_pop_from_relay( - sender: sp_runtime::AccountId32, - amount_to_send: Balance, - beneficiary: sp_runtime::AccountId32, -) { - let destination = RelayRelay::child_location_of(PopNetworkPara::para_id()); - let test_args = TestContext { - sender, - receiver: beneficiary.clone(), - args: TestArgs::new_relay(destination, beneficiary, amount_to_send), - }; - - let mut test = RelayToParaTest::new(test_args); - test.set_dispatchable::(relay_to_para_reserve_transfer_assets); - test.assert(); -} - // Funds Pop with relay tokens from system para fn fund_pop_from_system_para( sender: sp_runtime::AccountId32, @@ -270,7 +199,6 @@ fn fund_pop_from_system_para( } /// Reserve Transfers of native asset from Relay to Parachain should work -#[cfg(not(feature = "testnet"))] #[test] #[should_panic] fn reserve_transfer_native_asset_from_relay_to_para() { @@ -318,61 +246,6 @@ fn reserve_transfer_native_asset_from_relay_to_para() { assert!(receiver_balance_after < receiver_balance_before + amount_to_send); } -/// Reserve Transfers of native asset from Parachain to Relay should work -#[cfg(not(feature = "testnet"))] -#[test] -#[should_panic] -fn reserve_transfer_native_asset_from_para_to_relay() { - init_tracing(); - - // Setup: reserve transfer from relay to Pop, so that sovereign account accurate for return - // transfer - let amount_to_send: Balance = RELAY_ED * 1_000; - fund_pop_from_relay(RelayRelaySender::get(), amount_to_send, PopNetworkParaReceiver::get()); // alice on relay > bob on pop - - // Init values for Pop Network Parachain - let destination = PopNetworkPara::parent_location(); // relay - let beneficiary_id = RelayRelayReceiver::get(); // bob on relay - let amount_to_send = PopNetworkPara::account_data_of(PopNetworkParaReceiver::get()).free; // bob on pop balance - let assets = (Parent, amount_to_send).into(); - - let test_args = TestContext { - sender: PopNetworkParaReceiver::get(), // bob on pop - receiver: RelayRelayReceiver::get(), // bob on relay - args: TestArgs::new_para(destination, beneficiary_id, amount_to_send, assets, None, 0), - }; - - let mut test = ParaToRelayTest::new(test_args); - - let sender_balance_before = test.sender.balance; - let receiver_balance_before = test.receiver.balance; - - test.set_assertion::(para_to_relay_sender_assertions); - test.set_assertion::(para_to_relay_receiver_assertions); - test.set_dispatchable::(para_to_relay_reserve_transfer_assets); - test.assert(); - - let sender_balance_after = test.sender.balance; - let receiver_balance_after = test.receiver.balance; - - let delivery_fees = PopNetworkPara::execute_with(|| { - xcm_helpers::teleport_assets_delivery_fees::< - ::XcmSender, - >( - test.args.assets.clone(), 0, test.args.weight_limit, test.args.beneficiary, test.args.dest - ) - }); - - // Sender's balance is reduced - assert_eq!(sender_balance_before - amount_to_send - delivery_fees, sender_balance_after); - // Receiver's balance is increased - assert!(receiver_balance_after > receiver_balance_before); - // Receiver's balance increased by `amount_to_send - delivery_fees - bought_execution`; - // `delivery_fees` might be paid from transfer or JIT, also `bought_execution` is unknown - // but should be non-zero - assert!(receiver_balance_after < receiver_balance_before + amount_to_send); -} - /// Reserve Transfers of native asset from System Parachain to Parachain should work #[test] fn reserve_transfer_native_asset_from_system_para_to_para() { diff --git a/runtime/testnet/src/config/xcm.rs b/runtime/testnet/src/config/xcm.rs index 4fe54a4fd..8c7500987 100644 --- a/runtime/testnet/src/config/xcm.rs +++ b/runtime/testnet/src/config/xcm.rs @@ -1,8 +1,6 @@ -use core::marker::PhantomData; - use frame_support::{ parameter_types, - traits::{ConstU32, Contains, ContainsPair, Everything, Get, Nothing}, + traits::{ConstU32, Contains, ContainsPair, Everything, Nothing}, weights::Weight, }; use frame_system::EnsureRoot; @@ -13,10 +11,10 @@ use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, AllowTopLevelPaidExecutionFrom, EnsureXcmOrigin, FixedWeightBounds, - FrameTransactionalProcessor, FungibleAdapter, IsConcrete, NativeAsset, ParentIsPreset, - RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, - SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, - TrailingSetTopicAsId, UsingComponents, WithComputedOrigin, WithUniqueTopic, + FrameTransactionalProcessor, FungibleAdapter, IsConcrete, ParentIsPreset, RelayChainAsNative, + SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative, + SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId, + UsingComponents, WithComputedOrigin, WithUniqueTopic, }; use xcm_executor::XcmExecutor; @@ -112,18 +110,37 @@ pub type Barrier = TrailingSetTopicAsId<( >, )>; -/// Asset filter that allows native/relay asset if coming from a certain location. -// Borrowed from https://github.com/paritytech/polkadot-sdk/blob/ea458d0b95d819d31683a8a09ca7973ae10b49be/cumulus/parachains/runtimes/testing/penpal/src/xcm_config.rs#L239 for now -pub struct NativeAssetFrom(PhantomData); -impl> ContainsPair for NativeAssetFrom { +/// Accepts the relay asset from Asset Hub. +pub struct RelayAssetFromAssetHub; +impl ContainsPair for RelayAssetFromAssetHub { fn contains(asset: &Asset, origin: &Location) -> bool { - let loc = T::get(); + let loc = AssetHub::get(); &loc == origin && matches!(asset, Asset { id: AssetId(asset_loc), fun: Fungible(_a) } if *asset_loc == Location::from(Parent)) } } -pub type TrustedReserves = (NativeAsset, NativeAssetFrom); + +/// Accepts native assets, except from the relay because AH functions as the reserve. +pub struct NativeAssetExceptRelay; +impl ContainsPair for NativeAssetExceptRelay { + fn contains(asset: &Asset, origin: &Location) -> bool { + log::trace!( + target: "xcm::contains", + "asset: {:?}, origin: {:?}", + asset, + origin + ); + // Exclude the relay location. + if matches!(origin, Location { parents: 1, interior: Here }) { + return false; + } + matches!(asset.id, AssetId(ref id) if id == origin) + } +} + +/// Combinations of (Asset, Location) pairs which we trust as reserves. +pub type TrustedReserves = (NativeAssetExceptRelay, RelayAssetFromAssetHub); pub struct XcmConfig; impl xcm_executor::Config for XcmConfig {