git.delta.rocks / unique-network / refs/commits / 8c9b07c4c9bd

difftreelog

feat(xcm) move DenyThenTry to common, add DenyTransact

Daniel Shiposha2022-08-29parent: #6396c5b.patch.diff
in: master

4 files changed

modifiedruntime/common/config/xcm.rsdiffbeforeafterboth
--- a/runtime/common/config/xcm.rs
+++ b/runtime/common/config/xcm.rs
@@ -34,6 +34,7 @@
 	AssetId::{Concrete},
 	Fungibility::Fungible as XcmFungible,
 	MultiAsset, Error as XcmError,
+	Instruction, Xcm,
 };
 use xcm_builder::{
 	AccountId32Aliases, AllowTopLevelPaidExecutionFrom, CurrencyAdapter, EnsureXcmOrigin,
@@ -45,6 +46,7 @@
 use xcm_executor::{Config, XcmExecutor, Assets};
 use xcm_executor::traits::{
 	Convert as ConvertXcm, JustTry, MatchesFungible, WeightTrader, FilterAssetLocation,
+	ShouldExecute,
 };
 use pallet_foreing_assets::{
 	AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, CurrencyId, NativeCurrency, FreeForAll,
@@ -171,13 +173,53 @@
 	};
 }
 
-/*
-pub type Barrier = (
-	TakeWeightCredit,
-	AllowTopLevelPaidExecutionFrom<Everything>,
-	// ^^^ Parent & its unit plurality gets free execution
-);
- */
+pub struct DenyTransact;
+impl ShouldExecute for DenyTransact {
+	fn should_execute<Call>(
+		_origin: &MultiLocation,
+		message: &mut Xcm<Call>,
+		_max_weight: Weight,
+		_weight_credit: &mut Weight,
+	) -> Result<(), ()> {
+		let transact_inst = message.0.iter()
+			.find(|inst| matches![inst, Instruction::Transact { .. }]);
+
+		match transact_inst {
+			Some(_) => {
+				log::warn!(
+					target: "xcm::barrier",
+					"transact XCM rejected"
+				);
+
+				Err(())
+			},
+			None => Ok(())
+		}
+	}
+}
+
+/// Deny executing the XCM if it matches any of the Deny filter regardless of anything else.
+/// 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,
+        Allow: ShouldExecute;
+
+impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>
+    where
+        Deny: ShouldExecute,
+        Allow: ShouldExecute,
+{
+    fn should_execute<Call>(
+        origin: &MultiLocation,
+        message: &mut Xcm<Call>,
+        max_weight: Weight,
+        weight_credit: &mut Weight,
+    ) -> Result<(), ()> {
+        Deny::should_execute(origin, message, max_weight, weight_credit)?;
+        Allow::should_execute(origin, message, max_weight, weight_credit)
+    }
+}
 
 pub struct UsingOnlySelfCurrencyComponents<
 	WeightToFee: WeightToFeePolynomial<Balance = Currency::Balance>,
modifiedruntime/opal/src/xcm_config.rsdiffbeforeafterboth
--- a/runtime/opal/src/xcm_config.rs
+++ b/runtime/opal/src/xcm_config.rs
@@ -301,13 +301,16 @@
 	}
 }
 
-pub type Barrier = (
-	TakeWeightCredit,
-	AllowTopLevelPaidExecutionFrom<Everything>,
-	AllowUnpaidExecutionFrom<ParentOrParentsUnitPlurality>,
-	// ^^^ Parent & its unit plurality gets free execution
-	AllowAllDebug,
-);
+pub type Barrier = DenyThenTry<
+	DenyTransact,
+	(
+		TakeWeightCredit,
+		AllowTopLevelPaidExecutionFrom<Everything>,
+		AllowUnpaidExecutionFrom<ParentOrParentsUnitPlurality>,
+		// ^^^ Parent & its unit plurality gets free execution
+		AllowAllDebug,
+	)
+>;
 
 pub struct AllAsset;
 impl FilterAssetLocation for AllAsset {
modifiedruntime/quartz/src/xcm_config.rsdiffbeforeafterboth
76 };76 };
77}77}
78
79/// Deny executing the XCM if it matches any of the Deny filter regardless of anything else.
80/// If it passes the Deny, and matches one of the Allow cases then it is let through.
81pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)
82 where
83 Deny: ShouldExecute,
84 Allow: ShouldExecute;
85
86impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>
87 where
88 Deny: ShouldExecute,
89 Allow: ShouldExecute,
90{
91 fn should_execute<Call>(
92 origin: &MultiLocation,
93 message: &mut Xcm<Call>,
94 max_weight: Weight,
95 weight_credit: &mut Weight,
96 ) -> Result<(), ()> {
97 Deny::should_execute(origin, message, max_weight, weight_credit)?;
98 Allow::should_execute(origin, message, max_weight, weight_credit)
99 }
100}
10178
102pub fn get_allowed_locations() -> Vec<MultiLocation> {79pub fn get_allowed_locations() -> Vec<MultiLocation> {
103 vec![80 vec![
151pub type Barrier = DenyThenTry<128pub type Barrier = DenyThenTry<
152 DenyExchangeWithUnknownLocation,129 DenyExchangeWithUnknownLocation,
153 (130 (
131 DenyTransact,
154 TakeWeightCredit,132 TakeWeightCredit,
155 AllowTopLevelPaidExecutionFrom<Everything>,133 AllowTopLevelPaidExecutionFrom<Everything>,
156 // Parent and its exec plurality get free execution134 // Parent and its exec plurality get free execution
modifiedruntime/unique/src/xcm_config.rsdiffbeforeafterboth
--- a/runtime/unique/src/xcm_config.rs
+++ b/runtime/unique/src/xcm_config.rs
@@ -69,6 +69,7 @@
 pub type Barrier = DenyThenTry<
     DenyExchangeWithUnknownLocation,
     (
+        DenyTransact,
         TakeWeightCredit,
         AllowTopLevelPaidExecutionFrom<Everything>,
         // Parent and its exec plurality get free execution
@@ -93,27 +94,6 @@
         // Self parachain address
         MultiLocation { parents: 1, interior: X1(Parachain(ParachainInfo::get().into())) },
     ]
-}
-
-pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)
-    where
-        Deny: ShouldExecute,
-        Allow: ShouldExecute;
-
-impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>
-    where
-        Deny: ShouldExecute,
-        Allow: ShouldExecute,
-{
-    fn should_execute<Call>(
-        origin: &MultiLocation,
-        message: &mut Xcm<Call>,
-        max_weight: Weight,
-        weight_credit: &mut Weight,
-    ) -> Result<(), ()> {
-        Deny::should_execute(origin, message, max_weight, weight_credit)?;
-        Allow::should_execute(origin, message, max_weight, weight_credit)
-    }
 }
 
 // Allow xcm exchange only with locations in list