Skip to content

Commit

Permalink
refactor: revert NativeAssetFrom<AssetHub> to RelayAssetFromAssetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed Jan 15, 2025
1 parent 3452046 commit 29baf90
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 52 deletions.
23 changes: 13 additions & 10 deletions runtime/devnet/src/config/xcm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::marker::PhantomData;

use frame_support::{
parameter_types,
traits::{ConstU32, Contains, ContainsPair, Everything, Nothing},
traits::{ConstU32, Contains, ContainsPair, Everything, Get, Nothing},
weights::Weight,
};
use frame_system::EnsureRoot;
Expand All @@ -11,10 +13,10 @@ use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowTopLevelPaidExecutionFrom, EnsureXcmOrigin, FixedWeightBounds,
FrameTransactionalProcessor, FungibleAdapter, IsConcrete, ParentIsPreset, RelayChainAsNative,
SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId,
UsingComponents, WithComputedOrigin, WithUniqueTopic,
FrameTransactionalProcessor, FungibleAdapter, IsConcrete, NativeAsset, ParentIsPreset,

Check warning on line 16 in runtime/devnet/src/config/xcm.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `NativeAsset`

warning: unused import: `NativeAsset` --> runtime/devnet/src/config/xcm.rs:16:60 | 16 | FrameTransactionalProcessor, FungibleAdapter, IsConcrete, NativeAsset, ParentIsPreset, | ^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
TrailingSetTopicAsId, UsingComponents, WithComputedOrigin, WithUniqueTopic,
};
use xcm_executor::XcmExecutor;

Expand Down Expand Up @@ -110,11 +112,12 @@ pub type Barrier = TrailingSetTopicAsId<(
>,
)>;

/// Accepts the relay asset from Asset Hub.
pub struct RelayAssetFromAssetHub;
impl ContainsPair<Asset, Location> for RelayAssetFromAssetHub {
/// 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> {
fn contains(asset: &Asset, origin: &Location) -> bool {
let loc = AssetHub::get();
let loc = T::get();
&loc == origin &&
matches!(asset, Asset { id: AssetId(asset_loc), fun: Fungible(_a) }
if *asset_loc == Location::from(Parent))
Expand All @@ -140,7 +143,7 @@ impl ContainsPair<Asset, Location> for NativeAssetExceptRelay {
}

/// Combinations of (Asset, Location) pairs which we trust as reserves.
pub type TrustedReserves = (NativeAssetExceptRelay, RelayAssetFromAssetHub);
pub type TrustedReserves = (NativeAssetFrom<AssetHub>, NativeAssetExceptRelay);

pub struct XcmConfig;

Check warning on line 148 in runtime/devnet/src/config/xcm.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a struct

warning: missing documentation for a struct --> runtime/devnet/src/config/xcm.rs:148:1 | 148 | pub struct XcmConfig; | ^^^^^^^^^^^^^^^^^^^^
impl xcm_executor::Config for XcmConfig {
Expand Down
53 changes: 21 additions & 32 deletions runtime/mainnet/src/config/xcm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::marker::PhantomData;

use frame_support::{
parameter_types,
traits::{tokens::imbalance::ResolveTo, ConstU32, ContainsPair, Everything, Nothing},
traits::{tokens::imbalance::ResolveTo, ConstU32, ContainsPair, Everything, Get, Nothing},
weights::Weight,
};
use frame_system::EnsureRoot;
Expand Down Expand Up @@ -101,37 +103,20 @@ pub type Barrier = TrailingSetTopicAsId<(
>,
)>;

/// Accepts the relay asset from Asset Hub.
pub struct RelayAssetFromAssetHub;
impl ContainsPair<Asset, Location> for RelayAssetFromAssetHub {
/// 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> {
fn contains(asset: &Asset, origin: &Location) -> bool {
let loc = AssetHub::get();
let loc = T::get();
&loc == origin &&
matches!(asset, Asset { id: AssetId(asset_loc), fun: Fungible(_a) }
if *asset_loc == Location::from(Parent))
}
}

/// 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 type TrustedReserves = NativeAssetFrom<AssetHub>;

/// Locations that will not be charged fees in the executor,
/// either execution or delivery.
Expand Down Expand Up @@ -235,33 +220,37 @@ mod tests {

use super::*;

// Reserves accepted for native assets (except relay) and relay asset from Asset Hub.
// Only reserve accepted is the relay asset from Asset Hub.
#[test]
fn reserves() {
assert_eq!(
TypeId::of::<<XcmConfig as xcm_executor::Config>::IsReserve>(),
TypeId::of::<(NativeAssetExceptRelay, RelayAssetFromAssetHub,)>(),
TypeId::of::<NativeAssetFrom<AssetHub>>(),
);
}

#[test]
fn asset_hub_accepted_as_relay_asset_reserve() {
fn asset_hub_as_relay_asset_reserve() {
assert!(TrustedReserves::contains(
&Asset::from((AssetId::from(Parent), Fungibility::from(100u128))),
&AssetHub::get(),
));
}

#[test]
fn accept_native_assets_except_of_relay() {
let chain = Location::new(1, [Parachain(4242)]);
let native_asset = Asset::from((AssetId::from(chain.clone()), Fungibility::from(100u128)));
assert!(TrustedReserves::contains(&native_asset, &chain));

fn relay_as_relay_asset_reserve_fails() {
let relay_asset = Asset::from((AssetId::from(Parent), Fungibility::from(100u128)));
assert!(!TrustedReserves::contains(&relay_asset, &Parent.into()));
}

#[test]
fn decline_native_assets() {
let chain_x = Location::new(1, [Parachain(4242)]);
let chain_x_asset =
Asset::from((AssetId::from(chain_x.clone()), Fungibility::from(100u128)));
assert!(!TrustedReserves::contains(&chain_x_asset, &chain_x));
}

#[test]
fn decline_non_native_assets() {
// Native asset X of chain Y example.
Expand Down
23 changes: 13 additions & 10 deletions runtime/testnet/src/config/xcm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::marker::PhantomData;

use frame_support::{
parameter_types,
traits::{ConstU32, Contains, ContainsPair, Everything, Nothing},
traits::{ConstU32, Contains, ContainsPair, Everything, Get, Nothing},
weights::Weight,
};
use frame_system::EnsureRoot;
Expand All @@ -11,10 +13,10 @@ use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowTopLevelPaidExecutionFrom, EnsureXcmOrigin, FixedWeightBounds,
FrameTransactionalProcessor, FungibleAdapter, IsConcrete, ParentIsPreset, RelayChainAsNative,
SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId,
UsingComponents, WithComputedOrigin, WithUniqueTopic,
FrameTransactionalProcessor, FungibleAdapter, IsConcrete, NativeAsset, ParentIsPreset,

Check warning on line 16 in runtime/testnet/src/config/xcm.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `NativeAsset`

warning: unused import: `NativeAsset` --> runtime/testnet/src/config/xcm.rs:16:60 | 16 | FrameTransactionalProcessor, FungibleAdapter, IsConcrete, NativeAsset, ParentIsPreset, | ^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
TrailingSetTopicAsId, UsingComponents, WithComputedOrigin, WithUniqueTopic,
};
use xcm_executor::XcmExecutor;

Expand Down Expand Up @@ -110,11 +112,12 @@ pub type Barrier = TrailingSetTopicAsId<(
>,
)>;

/// Accepts the relay asset from Asset Hub.
pub struct RelayAssetFromAssetHub;
impl ContainsPair<Asset, Location> for RelayAssetFromAssetHub {
/// 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> {
fn contains(asset: &Asset, origin: &Location) -> bool {
let loc = AssetHub::get();
let loc = T::get();
&loc == origin &&
matches!(asset, Asset { id: AssetId(asset_loc), fun: Fungible(_a) }
if *asset_loc == Location::from(Parent))
Expand All @@ -140,7 +143,7 @@ impl ContainsPair<Asset, Location> for NativeAssetExceptRelay {
}

/// Combinations of (Asset, Location) pairs which we trust as reserves.
pub type TrustedReserves = (NativeAssetExceptRelay, RelayAssetFromAssetHub);
pub type TrustedReserves = (NativeAssetFrom<AssetHub>, NativeAssetExceptRelay);

pub struct XcmConfig;

Check warning on line 148 in runtime/testnet/src/config/xcm.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a struct

warning: missing documentation for a struct --> runtime/testnet/src/config/xcm.rs:148:1 | 148 | pub struct XcmConfig; | ^^^^^^^^^^^^^^^^^^^^
impl xcm_executor::Config for XcmConfig {
Expand Down

0 comments on commit 29baf90

Please sign in to comment.