git.delta.rocks / unique-network / refs/commits / e5fa9afed8bd

difftreelog

fix disallow xcm teleports completely

Daniel Shiposha2023-03-22parent: #1505aad.patch.diff
in: master

3 files changed

modifiedruntime/common/config/xcm/foreignassets.rsdiffbeforeafterboth
before · runtime/common/config/xcm/foreignassets.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use frame_support::{18	traits::{Contains, Get, fungibles, ContainsPair},19	parameter_types,20};21use sp_runtime::traits::Convert;22use xcm::latest::{MultiAsset, Junction::*, MultiLocation, Junctions::*};23use xcm_builder::{FungiblesAdapter, NonLocalMint, ConvertedConcreteId};24use xcm_executor::traits::{Convert as ConvertXcm, JustTry};25use pallet_foreign_assets::{26	AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, NativeCurrency, FreeForAll, TryAsForeign,27	ForeignAssetId, CurrencyId,28};29use sp_std::{borrow::Borrow, marker::PhantomData};30use crate::{Runtime, Balances, ParachainInfo, PolkadotXcm, ForeignAssets};3132use super::{LocationToAccountId, RelayLocation};3334use up_common::types::{AccountId, Balance};3536parameter_types! {37	pub CheckingAccount: AccountId = PolkadotXcm::check_account();38}3940/// No teleports are allowed41pub struct NoTeleports<AccountId, ForeignAssets>(PhantomData<(AccountId, ForeignAssets)>);4243impl<AccountId, ForeignAssets> Contains<<ForeignAssets as fungibles::Inspect<AccountId>>::AssetId>44	for NoTeleports<AccountId, ForeignAssets>45where46	ForeignAssets: fungibles::Inspect<AccountId>,47{48	fn contains(_id: &<ForeignAssets as fungibles::Inspect<AccountId>>::AssetId) -> bool {49		false50	}51}5253pub struct AsInnerId<AssetId, ConvertAssetId>(PhantomData<(AssetId, ConvertAssetId)>);54impl<AssetId: Clone + PartialEq, ConvertAssetId: ConvertXcm<AssetId, AssetId>>55	ConvertXcm<MultiLocation, AssetId> for AsInnerId<AssetId, ConvertAssetId>56where57	AssetId: Borrow<AssetId>,58	AssetId: TryAsForeign<AssetId, ForeignAssetId>,59	AssetIds: Borrow<AssetId>,60{61	fn convert_ref(id: impl Borrow<MultiLocation>) -> Result<AssetId, ()> {62		let id = id.borrow();6364		log::trace!(65			target: "xcm::AsInnerId::Convert",66			"AsInnerId {:?}",67			id68		);6970		let parent = MultiLocation::parent();71		let here = MultiLocation::here();72		let self_location = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));7374		if *id == parent {75			return ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Parent));76		}7778		if *id == here || *id == self_location {79			return ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Here));80		}8182		match XcmForeignAssetIdMapping::<Runtime>::get_currency_id(id.clone()) {83			Some(AssetIds::ForeignAssetId(foreign_asset_id)) => {84				ConvertAssetId::convert_ref(AssetIds::ForeignAssetId(foreign_asset_id))85			}86			_ => Err(()),87		}88	}8990	fn reverse_ref(what: impl Borrow<AssetId>) -> Result<MultiLocation, ()> {91		log::trace!(92			target: "xcm::AsInnerId::Reverse",93			"AsInnerId",94		);9596		let asset_id = what.borrow();9798		let parent_id =99			ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Parent)).unwrap();100		let here_id =101			ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Here)).unwrap();102103		if asset_id.clone() == parent_id {104			return Ok(MultiLocation::parent());105		}106107		if asset_id.clone() == here_id {108			return Ok(MultiLocation::new(109				1,110				X1(Parachain(ParachainInfo::get().into())),111			));112		}113114		match <AssetId as TryAsForeign<AssetId, ForeignAssetId>>::try_as_foreign(asset_id.clone()) {115			Some(fid) => match XcmForeignAssetIdMapping::<Runtime>::get_multi_location(fid) {116				Some(location) => Ok(location),117				None => Err(()),118			},119			None => Err(()),120		}121	}122}123124/// Means for transacting assets besides the native currency on this chain.125pub type FungiblesTransactor = FungiblesAdapter<126	// Use this fungibles implementation:127	ForeignAssets,128	// Use this currency when it is a fungible asset matching the given location or name:129	ConvertedConcreteId<AssetIds, Balance, AsInnerId<AssetIds, JustTry>, JustTry>,130	// Convert an XCM MultiLocation into a local account id:131	LocationToAccountId,132	// Our chain's account ID type (we can't get away without mentioning it explicitly):133	AccountId,134	// No teleports are allowed135	NonLocalMint<NoTeleports<AccountId, ForeignAssets>>,136	// The account to use for tracking teleports.137	CheckingAccount,138>;139140/// Means for transacting assets on this chain.141pub type AssetTransactors = FungiblesTransactor;142143pub struct AllAsset;144impl ContainsPair<MultiAsset, MultiLocation> for AllAsset {145	fn contains(_asset: &MultiAsset, _origin: &MultiLocation) -> bool {146		// ? Shouldn't we query foreign-asset pallet here, because of the new non-local mint147		// location logic?148		true149	}150}151152pub type IsReserve = AllAsset;153154pub type Trader<T> = FreeForAll<155	pallet_configuration::WeightToFee<T, Balance>,156	RelayLocation,157	AccountId,158	Balances,159	(),160>;161162pub struct CurrencyIdConvert;163impl Convert<AssetIds, Option<MultiLocation>> for CurrencyIdConvert {164	fn convert(id: AssetIds) -> Option<MultiLocation> {165		match id {166			AssetIds::NativeAssetId(NativeCurrency::Here) => Some(MultiLocation::new(167				1,168				X1(Parachain(ParachainInfo::get().into())),169			)),170			AssetIds::NativeAssetId(NativeCurrency::Parent) => Some(MultiLocation::parent()),171			AssetIds::ForeignAssetId(foreign_asset_id) => {172				XcmForeignAssetIdMapping::<Runtime>::get_multi_location(foreign_asset_id)173			}174		}175	}176}177178impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {179	fn convert(location: MultiLocation) -> Option<CurrencyId> {180		if location == MultiLocation::here()181			|| location == MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())))182		{183			return Some(AssetIds::NativeAssetId(NativeCurrency::Here));184		}185186		if location == MultiLocation::parent() {187			return Some(AssetIds::NativeAssetId(NativeCurrency::Parent));188		}189190		if let Some(currency_id) =191			XcmForeignAssetIdMapping::<Runtime>::get_currency_id(location.clone())192		{193			return Some(currency_id);194		}195196		None197	}198}
after · runtime/common/config/xcm/foreignassets.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use frame_support::{18	traits::{Get, ContainsPair},19	parameter_types,20};21use sp_runtime::traits::Convert;22use xcm::latest::{prelude::*, MultiAsset, MultiLocation};23use xcm_builder::{FungiblesAdapter, NoChecking, ConvertedConcreteId};24use xcm_executor::traits::{TransactAsset, Convert as ConvertXcm, JustTry};25use pallet_foreign_assets::{26	AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, NativeCurrency, FreeForAll, TryAsForeign,27	ForeignAssetId, CurrencyId,28};29use sp_std::{borrow::Borrow, marker::PhantomData};30use crate::{Runtime, Balances, ParachainInfo, PolkadotXcm, ForeignAssets};3132use super::{LocationToAccountId, RelayLocation};3334use up_common::types::{AccountId, Balance};3536parameter_types! {37	pub CheckingAccount: AccountId = PolkadotXcm::check_account();38}3940pub struct AsInnerId<AssetId, ConvertAssetId>(PhantomData<(AssetId, ConvertAssetId)>);41impl<AssetId: Clone + PartialEq, ConvertAssetId: ConvertXcm<AssetId, AssetId>>42	ConvertXcm<MultiLocation, AssetId> for AsInnerId<AssetId, ConvertAssetId>43where44	AssetId: Borrow<AssetId>,45	AssetId: TryAsForeign<AssetId, ForeignAssetId>,46	AssetIds: Borrow<AssetId>,47{48	fn convert_ref(id: impl Borrow<MultiLocation>) -> Result<AssetId, ()> {49		let id = id.borrow();5051		log::trace!(52			target: "xcm::AsInnerId::Convert",53			"AsInnerId {:?}",54			id55		);5657		let parent = MultiLocation::parent();58		let here = MultiLocation::here();59		let self_location = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));6061		if *id == parent {62			return ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Parent));63		}6465		if *id == here || *id == self_location {66			return ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Here));67		}6869		match XcmForeignAssetIdMapping::<Runtime>::get_currency_id(id.clone()) {70			Some(AssetIds::ForeignAssetId(foreign_asset_id)) => {71				ConvertAssetId::convert_ref(AssetIds::ForeignAssetId(foreign_asset_id))72			}73			_ => Err(()),74		}75	}7677	fn reverse_ref(what: impl Borrow<AssetId>) -> Result<MultiLocation, ()> {78		log::trace!(79			target: "xcm::AsInnerId::Reverse",80			"AsInnerId",81		);8283		let asset_id = what.borrow();8485		let parent_id =86			ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Parent)).unwrap();87		let here_id =88			ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Here)).unwrap();8990		if asset_id.clone() == parent_id {91			return Ok(MultiLocation::parent());92		}9394		if asset_id.clone() == here_id {95			return Ok(MultiLocation::new(96				1,97				X1(Parachain(ParachainInfo::get().into())),98			));99		}100101		match <AssetId as TryAsForeign<AssetId, ForeignAssetId>>::try_as_foreign(asset_id.clone()) {102			Some(fid) => match XcmForeignAssetIdMapping::<Runtime>::get_multi_location(fid) {103				Some(location) => Ok(location),104				None => Err(()),105			},106			None => Err(()),107		}108	}109}110111/// Means for transacting assets besides the native currency on this chain.112pub type FungiblesTransactor = FungiblesAdapter<113	// Use this fungibles implementation:114	ForeignAssets,115	// Use this currency when it is a fungible asset matching the given location or name:116	ConvertedConcreteId<AssetIds, Balance, AsInnerId<AssetIds, JustTry>, JustTry>,117	// Convert an XCM MultiLocation into a local account id:118	LocationToAccountId,119	// Our chain's account ID type (we can't get away without mentioning it explicitly):120	AccountId,121	// No Checking for teleported assets since we disallow teleports at all.122	NoChecking,123	// The account to use for tracking teleports.124	CheckingAccount,125>;126127/// Means for transacting assets on this chain.128pub struct AssetTransactor;129impl TransactAsset for AssetTransactor {130	fn can_check_in(131		_origin: &MultiLocation,132		_what: &MultiAsset,133		_context: &XcmContext,134	) -> XcmResult {135		Err(XcmError::Unimplemented)136	}137138	fn check_in(_origin: &MultiLocation, _what: &MultiAsset, _context: &XcmContext) {}139140	fn can_check_out(141		_dest: &MultiLocation,142		_what: &MultiAsset,143		_context: &XcmContext,144	) -> XcmResult {145		Err(XcmError::Unimplemented)146	}147148	fn check_out(_dest: &MultiLocation, _what: &MultiAsset, _context: &XcmContext) {}149150	fn deposit_asset(what: &MultiAsset, who: &MultiLocation, context: &XcmContext) -> XcmResult {151		FungiblesTransactor::deposit_asset(what, who, context)152	}153154	fn withdraw_asset(155		what: &MultiAsset,156		who: &MultiLocation,157		maybe_context: Option<&XcmContext>,158	) -> Result<xcm_executor::Assets, XcmError> {159		FungiblesTransactor::withdraw_asset(what, who, maybe_context)160	}161162	fn internal_transfer_asset(163		what: &MultiAsset,164		from: &MultiLocation,165		to: &MultiLocation,166		context: &XcmContext,167	) -> Result<xcm_executor::Assets, XcmError> {168		FungiblesTransactor::internal_transfer_asset(what, from, to, context)169	}170}171172pub struct AllAsset;173impl ContainsPair<MultiAsset, MultiLocation> for AllAsset {174	fn contains(_asset: &MultiAsset, _origin: &MultiLocation) -> bool {175		// ? Shouldn't we query foreign-asset pallet here, because of the new non-local mint176		// location logic?177		true178	}179}180181pub type IsReserve = AllAsset;182183pub type Trader<T> = FreeForAll<184	pallet_configuration::WeightToFee<T, Balance>,185	RelayLocation,186	AccountId,187	Balances,188	(),189>;190191pub struct CurrencyIdConvert;192impl Convert<AssetIds, Option<MultiLocation>> for CurrencyIdConvert {193	fn convert(id: AssetIds) -> Option<MultiLocation> {194		match id {195			AssetIds::NativeAssetId(NativeCurrency::Here) => Some(MultiLocation::new(196				1,197				X1(Parachain(ParachainInfo::get().into())),198			)),199			AssetIds::NativeAssetId(NativeCurrency::Parent) => Some(MultiLocation::parent()),200			AssetIds::ForeignAssetId(foreign_asset_id) => {201				XcmForeignAssetIdMapping::<Runtime>::get_multi_location(foreign_asset_id)202			}203		}204	}205}206207impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {208	fn convert(location: MultiLocation) -> Option<CurrencyId> {209		if location == MultiLocation::here()210			|| location == MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())))211		{212			return Some(AssetIds::NativeAssetId(NativeCurrency::Here));213		}214215		if location == MultiLocation::parent() {216			return Some(AssetIds::NativeAssetId(NativeCurrency::Parent));217		}218219		if let Some(currency_id) =220			XcmForeignAssetIdMapping::<Runtime>::get_currency_id(location.clone())221		{222			return Some(currency_id);223		}224225		None226	}227}
modifiedruntime/common/config/xcm/mod.rsdiffbeforeafterboth
--- a/runtime/common/config/xcm/mod.rs
+++ b/runtime/common/config/xcm/mod.rs
@@ -49,7 +49,7 @@
 #[cfg(not(feature = "foreign-assets"))]
 pub use nativeassets as xcm_assets;
 
-use xcm_assets::{AssetTransactors, IsReserve, Trader};
+use xcm_assets::{AssetTransactor, IsReserve, Trader};
 
 parameter_types! {
 	pub const RelayLocation: MultiLocation = MultiLocation::parent();
@@ -213,7 +213,7 @@
 	type RuntimeCall = RuntimeCall;
 	type XcmSender = XcmRouter;
 	// How to withdraw and deposit an asset.
-	type AssetTransactor = AssetTransactors;
+	type AssetTransactor = AssetTransactor;
 	type OriginConverter = XcmOriginToTransactDispatchOrigin;
 	type IsReserve = IsReserve;
 	type IsTeleporter = (); // Teleportation is disabled
modifiedruntime/common/config/xcm/nativeassets.rsdiffbeforeafterboth
--- a/runtime/common/config/xcm/nativeassets.rs
+++ b/runtime/common/config/xcm/nativeassets.rs
@@ -77,7 +77,7 @@
 	(),
 >;
 
-pub type AssetTransactors = LocalAssetTransactor;
+pub type AssetTransactor = LocalAssetTransactor;
 
 pub type IsReserve = NativeAsset;