Skip to content

Commit

Permalink
chore: testnet ah as reserve
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed Jan 15, 2025
1 parent d31223e commit 3452046
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 140 deletions.
127 changes: 0 additions & 127 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ decl_test_sender_receiver_accounts_parameter_types! {
type RelayToParaTest = Test<RelayRelay, PopNetworkPara>;
type SystemParaToParaTest = Test<AssetHubPara, PopNetworkPara>;
type ParaToSystemParaTest = Test<PopNetworkPara, AssetHubPara>;
type ParaToRelayTest = Test<PopNetworkPara, RelayRelay>;

fn relay_to_para_sender_assertions(t: RelayToParaTest) {
type RuntimeEvent = <RelayRelay as Chain>::RuntimeEvent;
Expand Down Expand Up @@ -124,24 +123,6 @@ fn para_to_system_para_sender_assertions(t: ParaToSystemParaTest) {
);
}

fn para_to_relay_sender_assertions(t: ParaToRelayTest) {
type RuntimeEvent = <PopNetworkPara as Chain>::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 = <AssetHubPara as Chain>::RuntimeEvent;
let sov_pop_net_on_ahr = AssetHubPara::sovereign_account_id_of(
Expand All @@ -165,29 +146,6 @@ fn para_to_system_para_receiver_assertions(t: ParaToSystemParaTest) {
);
}

fn para_to_relay_receiver_assertions(t: ParaToRelayTest) {
type RuntimeEvent = <RelayRelay as Chain>::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 {
<RelayRelay as RelayPallet>::XcmPallet::limited_reserve_transfer_assets(
t.signed_origin,
Expand Down Expand Up @@ -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 {
<PopNetworkPara as PopNetworkParaPallet>::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::<RelayRelay>(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,
Expand All @@ -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() {
Expand Down Expand Up @@ -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::<PopNetworkPara>(para_to_relay_sender_assertions);
test.set_assertion::<RelayRelay>(para_to_relay_receiver_assertions);
test.set_dispatchable::<PopNetworkPara>(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::<
<PopNetworkXcmConfig as xcm_executor::Config>::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() {
Expand Down
43 changes: 30 additions & 13 deletions runtime/testnet/src/config/xcm.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<T>(PhantomData<T>);
impl<T: Get<Location>> ContainsPair<Asset, Location> for NativeAssetFrom<T> {
/// Accepts the relay asset from Asset Hub.
pub struct RelayAssetFromAssetHub;
impl ContainsPair<Asset, Location> 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<AssetHub>);

/// Accepts native assets, except from the relay because AH functions as the reserve.
pub struct NativeAssetExceptRelay;
impl ContainsPair<Asset, Location> 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 {
Expand Down

0 comments on commit 3452046

Please sign in to comment.