From 28622f43d3edfc44e60b61ae443e5055fd3eca10 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Tue, 30 Aug 2022 16:07:40 +0200 Subject: [PATCH 1/5] [Feature] Limit collectives teleports to DOT --- Cargo.lock | 2 ++ pallets/xcm/Cargo.toml | 2 ++ pallets/xcm/src/lib.rs | 18 +++++++++++++++--- .../collectives-polkadot/src/xcm_config.rs | 6 +++--- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 49b507b615c..c7b8d2f2a7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1857,6 +1857,7 @@ dependencies = [ "cumulus-primitives-core", "frame-support", "frame-system", + "log", "parity-scale-codec", "scale-info", "serde", @@ -1864,6 +1865,7 @@ dependencies = [ "sp-runtime", "sp-std", "xcm", + "xcm-executor", ] [[package]] diff --git a/pallets/xcm/Cargo.toml b/pallets/xcm/Cargo.toml index 6ea825c07fc..7c7cc436a56 100644 --- a/pallets/xcm/Cargo.toml +++ b/pallets/xcm/Cargo.toml @@ -8,6 +8,7 @@ version = "0.1.0" codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } scale-info = { version = "2.1.1", default-features = false, features = ["derive"] } serde = { version = "1.0.144", optional = true, features = ["derive"] } +log = "0.4.17" sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } @@ -16,6 +17,7 @@ frame-support = { git = "https://github.com/paritytech/substrate", default-featu frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" } +xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" } cumulus-primitives-core = { path = "../../primitives/core", default-features = false } diff --git a/pallets/xcm/src/lib.rs b/pallets/xcm/src/lib.rs index 9659e65b7ff..d9feaf66c55 100644 --- a/pallets/xcm/src/lib.rs +++ b/pallets/xcm/src/lib.rs @@ -24,15 +24,16 @@ use codec::{Decode, DecodeLimit, Encode}; use cumulus_primitives_core::{ relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler, ParaId, }; -use frame_support::dispatch::Weight; +use frame_support::{dispatch::Weight, traits::Get}; pub use pallet::*; use scale_info::TypeInfo; use sp_runtime::{traits::BadOrigin, RuntimeDebug}; -use sp_std::{convert::TryFrom, prelude::*}; +use sp_std::{convert::TryFrom, marker::PhantomData, prelude::*}; use xcm::{ - latest::{ExecuteXcm, Outcome, Parent, Xcm}, + latest::{AssetId::Concrete, ExecuteXcm, MultiAsset, MultiLocation, Outcome, Parent, Xcm}, VersionedXcm, MAX_XCM_DECODE_DEPTH, }; +use xcm_executor::traits::FilterAssetLocation; #[frame_support::pallet] pub mod pallet { @@ -190,3 +191,14 @@ where _ => Err(BadOrigin), } } + +/// Accepts an asset if it is a native asset of a particular type. +pub struct ConcreteNativeAsset(PhantomData); +impl> FilterAssetLocation for ConcreteNativeAsset { + fn filter_asset_location(asset: &MultiAsset, origin: &MultiLocation) -> bool { + log::trace!(target: "xcm::filter_asset_location", + "ConcreteNativeAsset asset: {:?}, origin: {:?}, id: {:?}", + asset, origin, AssetId::get()); + matches!(asset.id, Concrete(ref id) if id == origin && origin == &AssetId::get()) + } +} diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs b/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs index 78f5b224f8d..40c4f276eb3 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs @@ -32,8 +32,8 @@ use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, CurrencyAdapter, EnsureXcmOrigin, - FixedWeightBounds, IsConcrete, LocationInverter, NativeAsset, ParentAsSuperuser, - ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, + FixedWeightBounds, IsConcrete, LocationInverter, ParentAsSuperuser, ParentIsPreset, + RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, UsingComponents, }; @@ -140,7 +140,7 @@ impl xcm_executor::Config for XcmConfig { // Collectives does not recognize a reserve location for any asset. Users must teleport DOT // where allowed (e.g. with the Relay Chain). type IsReserve = (); - type IsTeleporter = NativeAsset; // <- should be enough to allow teleportation of DOT + type IsTeleporter = cumulus_pallet_xcm::ConcreteNativeAsset; // <- should be enough to allow teleportation of DOT type LocationInverter = LocationInverter; type Barrier = Barrier; type Weigher = FixedWeightBounds; From aa5b7f9cbf26756c59fe1a029062c192c09211e5 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Tue, 30 Aug 2022 19:10:41 +0200 Subject: [PATCH 2/5] Update pallets/xcm/src/lib.rs Co-authored-by: Keith Yeung --- pallets/xcm/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/xcm/src/lib.rs b/pallets/xcm/src/lib.rs index d9feaf66c55..2588429ad53 100644 --- a/pallets/xcm/src/lib.rs +++ b/pallets/xcm/src/lib.rs @@ -192,8 +192,8 @@ where } } -/// Accepts an asset if it is a native asset of a particular type. -pub struct ConcreteNativeAsset(PhantomData); +/// Accepts an asset if it is a native asset from a particular `MultiLocation`. +pub struct ConcreteNativeAssetFrom(PhantomData); impl> FilterAssetLocation for ConcreteNativeAsset { fn filter_asset_location(asset: &MultiAsset, origin: &MultiLocation) -> bool { log::trace!(target: "xcm::filter_asset_location", From c4a35ba75bc7b3b2a8b68cd483589322250d57af Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Tue, 30 Aug 2022 21:00:51 +0200 Subject: [PATCH 3/5] fix review comments --- Cargo.lock | 2 -- pallets/xcm/Cargo.toml | 2 -- pallets/xcm/src/lib.rs | 18 +++--------------- parachains/common/src/xcm_config.rs | 14 +++++++++++++- .../collectives-polkadot/src/xcm_config.rs | 4 ++-- 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c7b8d2f2a7a..49b507b615c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1857,7 +1857,6 @@ dependencies = [ "cumulus-primitives-core", "frame-support", "frame-system", - "log", "parity-scale-codec", "scale-info", "serde", @@ -1865,7 +1864,6 @@ dependencies = [ "sp-runtime", "sp-std", "xcm", - "xcm-executor", ] [[package]] diff --git a/pallets/xcm/Cargo.toml b/pallets/xcm/Cargo.toml index 7c7cc436a56..6ea825c07fc 100644 --- a/pallets/xcm/Cargo.toml +++ b/pallets/xcm/Cargo.toml @@ -8,7 +8,6 @@ version = "0.1.0" codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } scale-info = { version = "2.1.1", default-features = false, features = ["derive"] } serde = { version = "1.0.144", optional = true, features = ["derive"] } -log = "0.4.17" sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } @@ -17,7 +16,6 @@ frame-support = { git = "https://github.com/paritytech/substrate", default-featu frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" } -xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" } cumulus-primitives-core = { path = "../../primitives/core", default-features = false } diff --git a/pallets/xcm/src/lib.rs b/pallets/xcm/src/lib.rs index 2588429ad53..9659e65b7ff 100644 --- a/pallets/xcm/src/lib.rs +++ b/pallets/xcm/src/lib.rs @@ -24,16 +24,15 @@ use codec::{Decode, DecodeLimit, Encode}; use cumulus_primitives_core::{ relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler, ParaId, }; -use frame_support::{dispatch::Weight, traits::Get}; +use frame_support::dispatch::Weight; pub use pallet::*; use scale_info::TypeInfo; use sp_runtime::{traits::BadOrigin, RuntimeDebug}; -use sp_std::{convert::TryFrom, marker::PhantomData, prelude::*}; +use sp_std::{convert::TryFrom, prelude::*}; use xcm::{ - latest::{AssetId::Concrete, ExecuteXcm, MultiAsset, MultiLocation, Outcome, Parent, Xcm}, + latest::{ExecuteXcm, Outcome, Parent, Xcm}, VersionedXcm, MAX_XCM_DECODE_DEPTH, }; -use xcm_executor::traits::FilterAssetLocation; #[frame_support::pallet] pub mod pallet { @@ -191,14 +190,3 @@ where _ => Err(BadOrigin), } } - -/// Accepts an asset if it is a native asset from a particular `MultiLocation`. -pub struct ConcreteNativeAssetFrom(PhantomData); -impl> FilterAssetLocation for ConcreteNativeAsset { - fn filter_asset_location(asset: &MultiAsset, origin: &MultiLocation) -> bool { - log::trace!(target: "xcm::filter_asset_location", - "ConcreteNativeAsset asset: {:?}, origin: {:?}, id: {:?}", - asset, origin, AssetId::get()); - matches!(asset.id, Concrete(ref id) if id == origin && origin == &AssetId::get()) - } -} diff --git a/parachains/common/src/xcm_config.rs b/parachains/common/src/xcm_config.rs index d56876d60ca..aba8ffa29dd 100644 --- a/parachains/common/src/xcm_config.rs +++ b/parachains/common/src/xcm_config.rs @@ -5,8 +5,9 @@ use frame_support::{ traits::{fungibles::Inspect, tokens::BalanceConversion}, weights::{Weight, WeightToFee, WeightToFeePolynomial}, }; +use sp_runtime::traits::Get; use xcm::latest::prelude::*; -use xcm_executor::traits::ShouldExecute; +use xcm_executor::traits::{FilterAssetLocation, ShouldExecute}; //TODO: move DenyThenTry to polkadot's xcm module. /// Deny executing the XCM if it matches any of the Deny filter regardless of anything else. @@ -107,3 +108,14 @@ where Ok(asset_amount) } } + +/// Accepts an asset if it is a native asset from a particular `MultiLocation`. +pub struct ConcreteNativeAssetFrom(PhantomData); +impl> FilterAssetLocation for ConcreteNativeAssetFrom { + fn filter_asset_location(asset: &MultiAsset, origin: &MultiLocation) -> bool { + log::trace!(target: "xcm::filter_asset_location", + "ConcreteNativeAsset asset: {:?}, origin: {:?}, location: {:?}", + asset, origin, Location::get()); + matches!(asset.id, Concrete(ref id) if id == origin && origin == &Location::get()) + } +} diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs b/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs index 40c4f276eb3..d6a8e27c462 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs @@ -25,7 +25,7 @@ use frame_support::{ use pallet_xcm::XcmPassthrough; use parachains_common::{ impls::ToStakingPot, - xcm_config::{DenyReserveTransferToRelayChain, DenyThenTry}, + xcm_config::{ConcreteNativeAssetFrom, DenyReserveTransferToRelayChain, DenyThenTry}, }; use polkadot_parachain::primitives::Sibling; use xcm::latest::prelude::*; @@ -140,7 +140,7 @@ impl xcm_executor::Config for XcmConfig { // Collectives does not recognize a reserve location for any asset. Users must teleport DOT // where allowed (e.g. with the Relay Chain). type IsReserve = (); - type IsTeleporter = cumulus_pallet_xcm::ConcreteNativeAsset; // <- should be enough to allow teleportation of DOT + type IsTeleporter = ConcreteNativeAssetFrom; // <- should be enough to allow teleportation of DOT type LocationInverter = LocationInverter; type Barrier = Barrier; type Weigher = FixedWeightBounds; From ef68d128aa39a01d9b7fb9909032769a9fd5f50c Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Wed, 31 Aug 2022 13:04:16 +0200 Subject: [PATCH 4/5] Update parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs Co-authored-by: Chevdor --- .../collectives/collectives-polkadot/src/xcm_config.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs b/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs index d6a8e27c462..916ead9a502 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs @@ -140,7 +140,8 @@ impl xcm_executor::Config for XcmConfig { // Collectives does not recognize a reserve location for any asset. Users must teleport DOT // where allowed (e.g. with the Relay Chain). type IsReserve = (); - type IsTeleporter = ConcreteNativeAssetFrom; // <- should be enough to allow teleportation of DOT + /// Only allow teleportation of DOT. + type IsTeleporter = ConcreteNativeAssetFrom; type LocationInverter = LocationInverter; type Barrier = Barrier; type Weigher = FixedWeightBounds; From 065a448e08c661709087b7734a280612e2b2386d Mon Sep 17 00:00:00 2001 From: paritytech-ci Date: Wed, 31 Aug 2022 11:04:59 +0000 Subject: [PATCH 5/5] [ci] Apply cargo-fmt --- .../runtimes/collectives/collectives-polkadot/src/xcm_config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs b/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs index 916ead9a502..09869b67e72 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs @@ -141,7 +141,7 @@ impl xcm_executor::Config for XcmConfig { // where allowed (e.g. with the Relay Chain). type IsReserve = (); /// Only allow teleportation of DOT. - type IsTeleporter = ConcreteNativeAssetFrom; + type IsTeleporter = ConcreteNativeAssetFrom; type LocationInverter = LocationInverter; type Barrier = Barrier; type Weigher = FixedWeightBounds;