Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only teleport native asset #855

Merged
merged 10 commits into from
Oct 29, 2024
53 changes: 48 additions & 5 deletions runtime/laos/src/configs/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use crate::{
types::ToAuthor, AccountId, AllPalletsWithSystem, Balances, ParachainInfo, PolkadotXcm,
Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin,
Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, Vec,
};
use core::marker::PhantomData;
use frame_support::{
Expand Down Expand Up @@ -168,7 +168,9 @@ impl xcm_executor::Config for XcmConfig {
type AssetTransactor = LocalAssetTransactor;
// Converts XCM origins to local dispatch origins.
type OriginConverter = XcmOriginToTransactDispatchOrigin;
type IsReserve = (); // no reserve trasfer are accepted
// No reserve transfer accepted
type IsReserve = ();
// This defines tuples of (Asset, Location) we trust as teleports (either from or to LAOS)
type IsTeleporter = TrustedTeleporters;
type UniversalLocation = UniversalLocation;
// Filters and allows XCM messages based on security policies.
Expand Down Expand Up @@ -217,6 +219,21 @@ pub type XcmRouter = xcm_builder::WithUniqueTopic<(
crate::XcmpQueue,
)>;

//Filter all teleports that aren't the native asset
pub struct OnlyTeleportNative;
impl Contains<(Location, Vec<Asset>)> for OnlyTeleportNative {
fn contains(t: &(Location, Vec<Asset>)) -> bool {
t.1.iter().all(|asset| {
log::trace!(target: "xcm::OnlyTeleportNative", "Asset to be teleported: {:?}", asset);
if let Asset { id: asset_id, fun: Fungible(_) } = asset {
asset_id.0 == HereLocation::get()
} else {
false
}
})
}
}

impl pallet_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
// Determines which origins are allowed to send XCM messages.
Expand All @@ -226,9 +243,10 @@ impl pallet_xcm::Config for Runtime {
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type XcmExecuteFilter = Nothing;
type XcmExecutor = XcmExecutor<XcmConfig>;
type XcmTeleportFilter = Everything;
// Allows all reserve asset transfers.
type XcmReserveTransferFilter = Everything;
// This filter wheter an origin may teleport out different assets.
type XcmTeleportFilter = OnlyTeleportNative;
// Deny all reserve asset transfers.
type XcmReserveTransferFilter = Nothing;
// Calculates the weight of XCM messages.
type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type UniversalLocation = UniversalLocation;
Expand Down Expand Up @@ -285,11 +303,36 @@ where
mod tests {
use super::*;

parameter_types! {
pub SomeLocation: Location = Location::new(1, [Junction::Parachain(123)] );
}

#[test]
fn check_checking_account() {
assert_eq!(
CheckingAccount::get().to_string(),
"0x6d6F646c70792F78636D63680000000000000000"
);
}

#[test]
fn only_teleport_native_contains_only_native_asset() {
let assets: Vec<Asset> = vec![(HereLocation::get(), Fungible(1_000)).into()];

// The first parameter passed to contains may be any location as it's not used by the
// function. We use HereLocation for simplicity.
assert!(OnlyTeleportNative::contains(&(HereLocation::get(), assets)));
}

#[test]
fn only_teleport_native_contains_not_all_assets_are_native() {
let assets: Vec<Asset> = vec![
(HereLocation::get(), Fungible(1_000)).into(),
(SomeLocation::get(), Fungible(1_000)).into(),
];

// The first parameter passed to contains may be any location as it's not used by the
// function. We use HereLocation for simplicity.
assert!(!OnlyTeleportNative::contains(&(HereLocation::get(), assets)));
}
}
22 changes: 17 additions & 5 deletions xcm-simulator/src/laosish/configs/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ use xcm_executor::XcmExecutor;
use xcm_simulator::AssetFilter;

parameter_types! {
pub const RelayLocation: Location = Location::parent();
pub const RelayNetwork: NetworkId = NetworkId::Kusama;
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
// For the real deployment, it is recommended to set `RelayNetwork` according to the relay chain
Expand Down Expand Up @@ -184,17 +183,30 @@ impl xcm_executor::Config for XcmConfig {
pub type LocalOriginToLocation = SignedToAccountId20<RuntimeOrigin, AccountId, RelayNetwork>;
pub type XcmRouter = EnsureDecodableXcm<crate::ParachainXcmRouter<MsgQueue>>;

//Filter all teleports that aren't the native asset
pub struct OnlyTeleportNative;
impl Contains<(Location, Vec<Asset>)> for OnlyTeleportNative {
fn contains(t: &(Location, Vec<Asset>)) -> bool {
t.1.iter().all(|asset| {
log::trace!(target: "xcm::OnlyTeleportNative", "Asset to be teleported: {:?}", asset);
if let Asset { id: asset_id, fun: Fungible(_) } = asset {
asset_id.0 == HereLocation::get()
} else {
false
}
})
}
}

impl pallet_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type XcmRouter = XcmRouter;
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type XcmExecuteFilter = Nothing;
type XcmExecutor = XcmExecutor<XcmConfig>;
// it is safe to have `Everything` as `IsTeleporter` performs an additional check on the
// parachain
type XcmTeleportFilter = Everything;
type XcmReserveTransferFilter = Everything;
type XcmTeleportFilter = OnlyTeleportNative;
type XcmReserveTransferFilter = Nothing;
type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type UniversalLocation = UniversalLocation;
type RuntimeOrigin = RuntimeOrigin;
Expand Down
Loading