Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

Commit

Permalink
Companion for paritytech/cumulus#1169
Browse files Browse the repository at this point in the history
  • Loading branch information
jiguantong committed Jun 1, 2022
1 parent 56671dd commit 3341d8f
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 30 deletions.
2 changes: 2 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#![cfg_attr(not(feature = "std"), no_std)]

pub mod xcm_config;

// --- core ---
use core::marker::PhantomData;
// --- paritytech ---
Expand Down
67 changes: 67 additions & 0 deletions runtime/common/src/xcm_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use core::marker::PhantomData;
use frame_support::{log, weights::Weight};
use xcm::latest::prelude::*;
use xcm_executor::traits::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.
/// 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)
}
}

// See issue #5233
pub struct DenyReserveTransferToRelayChain;
impl ShouldExecute for DenyReserveTransferToRelayChain {
fn should_execute<Call>(
origin: &MultiLocation,
message: &mut Xcm<Call>,
_max_weight: Weight,
_weight_credit: &mut Weight,
) -> Result<(), ()> {
if message.0.iter().any(|inst| {
matches!(
inst,
InitiateReserveWithdraw {
reserve: MultiLocation { parents: 1, interior: Here },
..
} | DepositReserveAsset { dest: MultiLocation { parents: 1, interior: Here }, .. } |
TransferReserveAsset {
dest: MultiLocation { parents: 1, interior: Here },
..
}
)
}) {
return Err(()) // Deny
}

// allow reserve transfers to arrive from relay chain
if matches!(origin, MultiLocation { parents: 1, interior: Here }) &&
message.0.iter().any(|inst| matches!(inst, ReserveAssetDeposited { .. }))
{
log::info!(
target: "runtime::xcm-barier",
"Unexpected Reserve Assets Deposited on the relay chain",
);
}
// Permit everything else
Ok(())
}
}
24 changes: 14 additions & 10 deletions runtime/crab-parachain/src/pallets/polkadot_xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use xcm_builder::*;
use xcm_executor::{Config as XcmCExecutorConfig, XcmExecutor};
// --- darwinia-network ---
use crate::*;
use dc_common_runtime::xcm_config::{DenyReserveTransferToRelayChain, DenyThenTry};

/// Converts a local signed origin into an XCM multilocation.
/// Forms the basis for local origins sending/executing XCMs.
Expand Down Expand Up @@ -41,16 +42,19 @@ pub type XcmRouter = (
XcmpQueue,
);

pub type Barrier = (
TakeWeightCredit,
AllowTopLevelPaidExecutionFrom<Everything>,
// Parent and its plurality get free execution
AllowUnpaidExecutionFrom<ParentOrParentsExecutivePlurality>,
// Expected responses are OK.
AllowKnownQueryResponses<PolkadotXcm>,
// Subscriptions for version tracking are OK.
AllowSubscriptionsFrom<ParentOrSiblings>,
);
pub type Barrier = DenyThenTry<
DenyReserveTransferToRelayChain,
(
TakeWeightCredit,
AllowTopLevelPaidExecutionFrom<Everything>,
// Parent and its exec plurality get free execution
AllowUnpaidExecutionFrom<ParentOrParentsExecutivePlurality>,
// Expected responses are OK.
AllowKnownQueryResponses<PolkadotXcm>,
// Subscriptions for version tracking are OK.
AllowSubscriptionsFrom<ParentOrSiblings>,
),
>;

/// Type for specifying how a `MultiLocation` can be converted into an `AccountId`. This is used
/// when determining ownership of accounts for asset transacting and when attempting to use XCM
Expand Down
24 changes: 14 additions & 10 deletions runtime/darwinia-parachain/src/pallets/polkadot_xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use xcm_builder::*;
use xcm_executor::{Config as XcmCExecutorConfig, XcmExecutor};
// --- darwinia-network ---
use crate::*;
use dc_common_runtime::xcm_config::{DenyReserveTransferToRelayChain, DenyThenTry};

/// Converts a local signed origin into an XCM multilocation.
/// Forms the basis for local origins sending/executing XCMs.
Expand Down Expand Up @@ -41,16 +42,19 @@ pub type XcmRouter = (
XcmpQueue,
);

pub type Barrier = (
TakeWeightCredit,
AllowTopLevelPaidExecutionFrom<Everything>,
// Parent and its plurality get free execution
AllowUnpaidExecutionFrom<ParentOrParentsExecutivePlurality>,
// Expected responses are OK.
AllowKnownQueryResponses<PolkadotXcm>,
// Subscriptions for version tracking are OK.
AllowSubscriptionsFrom<ParentOrSiblings>,
);
pub type Barrier = DenyThenTry<
DenyReserveTransferToRelayChain,
(
TakeWeightCredit,
AllowTopLevelPaidExecutionFrom<Everything>,
// Parent and its exec plurality get free execution
AllowUnpaidExecutionFrom<ParentOrParentsExecutivePlurality>,
// Expected responses are OK.
AllowKnownQueryResponses<PolkadotXcm>,
// Subscriptions for version tracking are OK.
AllowSubscriptionsFrom<ParentOrSiblings>,
),
>;

/// Type for specifying how a `MultiLocation` can be converted into an `AccountId`. This is used
/// when determining ownership of accounts for asset transacting and when attempting to use XCM
Expand Down
24 changes: 14 additions & 10 deletions runtime/pangolin-parachain/src/pallets/polkadot_xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use xcm_builder::*;
use xcm_executor::{Config as XcmCExecutorConfig, XcmExecutor};
// --- darwinia-network ---
use crate::*;
use dc_common_runtime::xcm_config::{DenyReserveTransferToRelayChain, DenyThenTry};

/// Converts a local signed origin into an XCM multilocation.
/// Forms the basis for local origins sending/executing XCMs.
Expand Down Expand Up @@ -41,16 +42,19 @@ pub type XcmRouter = (
XcmpQueue,
);

pub type Barrier = (
TakeWeightCredit,
AllowTopLevelPaidExecutionFrom<Everything>,
// Parent and its plurality get free execution
AllowUnpaidExecutionFrom<ParentOrParentsExecutivePlurality>,
// Expected responses are OK.
AllowKnownQueryResponses<PolkadotXcm>,
// Subscriptions for version tracking are OK.
AllowSubscriptionsFrom<ParentOrSiblings>,
);
pub type Barrier = DenyThenTry<
DenyReserveTransferToRelayChain,
(
TakeWeightCredit,
AllowTopLevelPaidExecutionFrom<Everything>,
// Parent and its exec plurality get free execution
AllowUnpaidExecutionFrom<ParentOrParentsExecutivePlurality>,
// Expected responses are OK.
AllowKnownQueryResponses<PolkadotXcm>,
// Subscriptions for version tracking are OK.
AllowSubscriptionsFrom<ParentOrSiblings>,
),
>;

/// Type for specifying how a `MultiLocation` can be converted into an `AccountId`. This is used
/// when determining ownership of accounts for asset transacting and when attempting to use XCM
Expand Down

0 comments on commit 3341d8f

Please sign in to comment.