git.delta.rocks / unique-network / refs/commits / 5a619a936664

difftreelog

fix(xcm) improve denythentry, deny transact xcm

Daniel Shiposha2022-08-30parent: #247c0b0.patch.diff
in: master

8 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4572,6 +4572,16 @@
 ]
 
 [[package]]
+name = "logtest"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eb3e43a8657c1d64516dcc9db8ca03826a4aceaf89d5ce1b37b59f6ff0e43026"
+dependencies = [
+ "lazy_static",
+ "log",
+]
+
+[[package]]
 name = "lru"
 version = "0.6.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -5148,7 +5158,9 @@
  "frame-system-rpc-runtime-api",
  "frame-try-runtime",
  "hex-literal",
+ "impl-trait-for-tuples",
  "log",
+ "logtest",
  "orml-tokens",
  "orml-traits",
  "orml-vesting",
@@ -8466,7 +8478,9 @@
  "frame-system-rpc-runtime-api",
  "frame-try-runtime",
  "hex-literal",
+ "impl-trait-for-tuples",
  "log",
+ "logtest",
  "orml-tokens",
  "orml-traits",
  "orml-vesting",
@@ -12206,7 +12220,7 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675"
 dependencies = [
- "cfg-if 0.1.10",
+ "cfg-if 1.0.0",
  "digest 0.10.3",
  "rand 0.8.5",
  "static_assertions",
@@ -12459,7 +12473,9 @@
  "frame-system-rpc-runtime-api",
  "frame-try-runtime",
  "hex-literal",
+ "impl-trait-for-tuples",
  "log",
+ "logtest",
  "orml-tokens",
  "orml-traits",
  "orml-vesting",
modifiedruntime/common/config/xcm.rsdiffbeforeafterboth
--- a/runtime/common/config/xcm.rs
+++ b/runtime/common/config/xcm.rs
@@ -172,13 +172,32 @@
 	};
 }
 
+pub trait TryPass {
+	fn try_pass<Call>(
+		origin: &MultiLocation,
+		message: &mut Xcm<Call>,
+	) -> Result<(), ()>;
+}
+
+#[impl_trait_for_tuples::impl_for_tuples(30)]
+impl TryPass for Tuple {
+	fn try_pass<Call>(
+		origin: &MultiLocation,
+		message: &mut Xcm<Call>,
+	)  -> Result<(), ()> {
+		for_tuples!( #(
+			Tuple::try_pass(origin, message)?;
+		)* );
+
+		Ok(())
+	}
+}
+
 pub struct DenyTransact;
-impl ShouldExecute for DenyTransact {
-	fn should_execute<Call>(
+impl TryPass for DenyTransact {
+	fn try_pass<Call>(
 		_origin: &MultiLocation,
 		message: &mut Xcm<Call>,
-		_max_weight: Weight,
-		_weight_credit: &mut Weight,
 	) -> Result<(), ()> {
 		let transact_inst = message
 			.0
@@ -203,12 +222,12 @@
 /// If it passes the Deny, and matches one of the Allow cases then it is let through.
 pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)
 where
-	Deny: ShouldExecute,
+	Deny: TryPass,
 	Allow: ShouldExecute;
 
 impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>
 where
-	Deny: ShouldExecute,
+	Deny: TryPass,
 	Allow: ShouldExecute,
 {
 	fn should_execute<Call>(
@@ -217,7 +236,7 @@
 		max_weight: Weight,
 		weight_credit: &mut Weight,
 	) -> Result<(), ()> {
-		Deny::should_execute(origin, message, max_weight, weight_credit)?;
+		Deny::try_pass(origin, message)?;
 		Allow::should_execute(origin, message, max_weight, weight_credit)
 	}
 }
modifiedruntime/common/tests/xcm.rsdiffbeforeafterboth
before · runtime/common/tests/xcm.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 xcm_executor::traits::ShouldExecute;18use xcm::latest::prelude::*;19use logtest::Logger;20use crate::{21    Call,22    xcm_config::Barrier,23};2425fn catch_xcm_barrier_log(logger: &mut Logger, expected_msg: &str) {26    for record in logger {27        if record.target() == "xcm::barrier"28        && record.args() == expected_msg {29            return;30        }31    }3233    panic!("the expected XCM barrier log `{}` is not found", expected_msg);34}3536#[test]37fn xcm_barrier_does_not_allow_transact() {38    // We have a `AllowTopLevelPaidExecutionFrom` barrier,39    // so an XCM program should start from one of the following commands: 40    // * `WithdrawAsset`41    // * `ReceiveTeleportedAsset`42    // * `ReserveAssetDeposited`43    // * `ClaimAsset` 44    // We use the `WithdrawAsset` in this test4546    let location = MultiLocation {47        parents: 0,48        interior: Junctions::Here,49    };5051    // let id = AssetId::Concrete(location.clone());52    // let fun = Fungibility::Fungible(42);53    // let multiasset = MultiAsset {54    //     id,55    //     fun,56    // };57    // let withdraw_inst = WithdrawAsset(multiasset.into());5859    // We will never decode this "call",60    // so it is irrelevant what we are passing to the `transact` cmd.61    let fake_encoded_call = vec![0u8];6263    let transact_inst = Transact {64        origin_type: OriginKind::Superuser,65        require_weight_at_most: 0,66        call: fake_encoded_call.into(),67    };6869    let mut xcm_program = Xcm::<Call>(vec![transact_inst]);7071    let mut logger = Logger::start();7273    let max_weight = 100_000;74    let mut weight_credit = 0;7576    let result = Barrier::should_execute(77        &location,78        &mut xcm_program,79        max_weight,80        &mut weight_credit81    );8283    assert!(result.is_err(), "the barrier should disallow the XCM transact cmd");8485    catch_xcm_barrier_log(&mut logger, "transact XCM rejected");86}
after · runtime/common/tests/xcm.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 xcm_executor::traits::ShouldExecute;18use xcm::latest::prelude::*;19use logtest::Logger;20use crate::{21    Call,22    xcm_config::Barrier,23};2425fn catch_xcm_barrier_log(logger: &mut Logger, expected_msg: &str) {26    for record in logger {27        if record.target() == "xcm::barrier"28        && record.args() == expected_msg {29            return;30        }31    }3233    panic!("the expected XCM barrier log `{}` is not found", expected_msg);34}3536#[test]37fn xcm_barrier_denies_transact() {38    // We have a `AllowTopLevelPaidExecutionFrom` barrier,39    // so an XCM program should start from one of the following commands: 40    // * `WithdrawAsset`41    // * `ReceiveTeleportedAsset`42    // * `ReserveAssetDeposited`43    // * `ClaimAsset` 44    // We use the `WithdrawAsset` in this test4546    let location = MultiLocation {47        parents: 0,48        interior: Junctions::Here,49    };5051    // let id = AssetId::Concrete(location.clone());52    // let fun = Fungibility::Fungible(42);53    // let multiasset = MultiAsset {54    //     id,55    //     fun,56    // };57    // let withdraw_inst = WithdrawAsset(multiasset.into());5859    // We will never decode this "call",60    // so it is irrelevant what we are passing to the `transact` cmd.61    let fake_encoded_call = vec![0u8];6263    let transact_inst = Transact {64        origin_type: OriginKind::Superuser,65        require_weight_at_most: 0,66        call: fake_encoded_call.into(),67    };6869    let mut xcm_program = Xcm::<Call>(vec![transact_inst]);7071    let mut logger = Logger::start();7273    let max_weight = 100_000;74    let mut weight_credit = 0;7576    let result = Barrier::should_execute(77        &location,78        &mut xcm_program,79        max_weight,80        &mut weight_credit81    );8283    assert!(result.is_err(), "the barrier should disallow the XCM transact cmd");8485    catch_xcm_barrier_log(&mut logger, "transact XCM rejected");86}
modifiedruntime/opal/Cargo.tomldiffbeforeafterboth
--- a/runtime/opal/Cargo.toml
+++ b/runtime/opal/Cargo.toml
@@ -463,6 +463,11 @@
 pallet-foreing-assets = { default-features = false, path = "../../pallets/foreing-assets" }
 
 ################################################################################
+# Other Dependencies
+
+impl-trait-for-tuples = "0.2.2"
+
+################################################################################
 # Dev Dependencies
 
 [dev-dependencies.logtest]
modifiedruntime/quartz/Cargo.tomldiffbeforeafterboth
--- a/runtime/quartz/Cargo.toml
+++ b/runtime/quartz/Cargo.toml
@@ -471,6 +471,11 @@
 pallet-foreing-assets = { default-features = false, path = "../../pallets/foreing-assets" }
 
 ################################################################################
+# Other Dependencies
+
+impl-trait-for-tuples = "0.2.2"
+
+################################################################################
 # Dev Dependencies
 
 [dev-dependencies.logtest]
modifiedruntime/quartz/src/xcm_config.rsdiffbeforeafterboth
--- a/runtime/quartz/src/xcm_config.rs
+++ b/runtime/quartz/src/xcm_config.rs
@@ -41,7 +41,7 @@
 };
 use xcm_executor::{
     {Config, XcmExecutor},
-    traits::{Convert as ConvertXcm, FilterAssetLocation, JustTry, MatchesFungible, ShouldExecute},
+    traits::{Convert as ConvertXcm, FilterAssetLocation, JustTry, MatchesFungible},
 };
 
 use up_common::{
@@ -93,12 +93,10 @@
 
 // Allow xcm exchange only with locations in list
 pub struct DenyExchangeWithUnknownLocation;
-impl ShouldExecute for DenyExchangeWithUnknownLocation {
-    fn should_execute<Call>(
+impl TryPass for DenyExchangeWithUnknownLocation {
+    fn try_pass<Call>(
         origin: &MultiLocation,
         message: &mut Xcm<Call>,
-        _max_weight: Weight,
-        _weight_credit: &mut Weight,
     ) -> Result<(), ()> {
 
         // Check if deposit or transfer belongs to allowed parachains
@@ -126,9 +124,11 @@
 }
 
 pub type Barrier = DenyThenTry<
-    DenyExchangeWithUnknownLocation,
     (
         DenyTransact,
+        DenyExchangeWithUnknownLocation,
+    ),
+    (
         TakeWeightCredit,
         AllowTopLevelPaidExecutionFrom<Everything>,
         // Parent and its exec plurality get free execution
modifiedruntime/unique/Cargo.tomldiffbeforeafterboth
--- a/runtime/unique/Cargo.toml
+++ b/runtime/unique/Cargo.toml
@@ -463,6 +463,11 @@
 pallet-foreing-assets = { default-features = false, path = "../../pallets/foreing-assets" }
 
 ################################################################################
+# Other Dependencies
+
+impl-trait-for-tuples = "0.2.2"
+
+################################################################################
 # Dev Dependencies
 
 [dev-dependencies.logtest]
modifiedruntime/unique/src/xcm_config.rsdiffbeforeafterboth
--- a/runtime/unique/src/xcm_config.rs
+++ b/runtime/unique/src/xcm_config.rs
@@ -41,7 +41,7 @@
 };
 use xcm_executor::{
     {Config, XcmExecutor},
-    traits::{Convert as ConvertXcm, FilterAssetLocation, JustTry, MatchesFungible, ShouldExecute},
+    traits::{Convert as ConvertXcm, FilterAssetLocation, JustTry, MatchesFungible},
 };
 
 use up_common::{
@@ -67,9 +67,11 @@
 
 
 pub type Barrier = DenyThenTry<
-    DenyExchangeWithUnknownLocation,
     (
         DenyTransact,
+        DenyExchangeWithUnknownLocation,
+    ),
+    (
         TakeWeightCredit,
         AllowTopLevelPaidExecutionFrom<Everything>,
         // Parent and its exec plurality get free execution
@@ -98,12 +100,10 @@
 
 // Allow xcm exchange only with locations in list
 pub struct DenyExchangeWithUnknownLocation;
-impl ShouldExecute for DenyExchangeWithUnknownLocation {
-    fn should_execute<Call>(
+impl TryPass for DenyExchangeWithUnknownLocation {
+    fn try_pass<Call>(
         origin: &MultiLocation,
         message: &mut Xcm<Call>,
-        _max_weight: Weight,
-        _weight_credit: &mut Weight,
     ) -> Result<(), ()> {
 
         // Check if deposit or transfer belongs to allowed parachains