forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xcm: fix for DenyThenTry Barrier (paritytech#7169)
Resolves (partially): paritytech#7148 (see _Problem 1 - `ShouldExecute` tuple implementation and `Deny` filter tuple_) This PR changes the behavior of `DenyThenTry` from the pattern `DenyIfAllMatch` to `DenyIfAnyMatch` for the tuple. I would expect the latter is the right behavior so make the fix in place, but we can also add a dedicated Impl with the legacy one untouched. ## TODO - [x] add unit-test for `DenyReserveTransferToRelayChain` - [x] add test and investigate/check `DenyThenTry` as discussed [here](paritytech#6838 (comment)) and update documentation if needed --------- Co-authored-by: Branislav Kontur <bkontur@gmail.com> Co-authored-by: Francisco Aguirre <franciscoaguirreperez@gmail.com> Co-authored-by: command-bot <> Co-authored-by: Clara van Staden <claravanstaden64@gmail.com> Co-authored-by: Adrian Catangiu <adrian@parity.io>
- Loading branch information
1 parent
db3ff60
commit b30aa31
Showing
11 changed files
with
473 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
cumulus/parachains/runtimes/bridge-hubs/common/src/barriers.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (C) Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use core::{marker::PhantomData, ops::ControlFlow}; | ||
use cumulus_primitives_core::Weight; | ||
use frame_support::traits::{Contains, ProcessMessageError}; | ||
use xcm::prelude::{ExportMessage, Instruction, Location, NetworkId}; | ||
|
||
use xcm_builder::{CreateMatcher, MatchXcm}; | ||
use xcm_executor::traits::{DenyExecution, Properties}; | ||
|
||
/// Deny execution if the message contains instruction `ExportMessage` with | ||
/// a. origin is contained in `FromOrigin` (i.e.`FromOrigin::Contains(origin)`) | ||
/// b. network is contained in `ToGlobalConsensus`, (i.e. `ToGlobalConsensus::contains(network)`) | ||
pub struct DenyExportMessageFrom<FromOrigin, ToGlobalConsensus>( | ||
PhantomData<(FromOrigin, ToGlobalConsensus)>, | ||
); | ||
|
||
impl<FromOrigin, ToGlobalConsensus> DenyExecution | ||
for DenyExportMessageFrom<FromOrigin, ToGlobalConsensus> | ||
where | ||
FromOrigin: Contains<Location>, | ||
ToGlobalConsensus: Contains<NetworkId>, | ||
{ | ||
fn deny_execution<RuntimeCall>( | ||
origin: &Location, | ||
message: &mut [Instruction<RuntimeCall>], | ||
_max_weight: Weight, | ||
_properties: &mut Properties, | ||
) -> Result<(), ProcessMessageError> { | ||
// This barrier only cares about messages with `origin` matching `FromOrigin`. | ||
if !FromOrigin::contains(origin) { | ||
return Ok(()) | ||
} | ||
message.matcher().match_next_inst_while( | ||
|_| true, | ||
|inst| match inst { | ||
ExportMessage { network, .. } if ToGlobalConsensus::contains(network) => | ||
Err(ProcessMessageError::Unsupported), | ||
_ => Ok(ControlFlow::Continue(())), | ||
}, | ||
)?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
cumulus/parachains/runtimes/bridge-hubs/common/tests/tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (C) Parity Technologies (UK) Ltd. | ||
// This file is part of Cumulus. | ||
|
||
// Cumulus is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Cumulus is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![cfg(test)] | ||
use bridge_hub_common::DenyExportMessageFrom; | ||
use frame_support::{ | ||
parameter_types, | ||
traits::{Equals, EverythingBut, ProcessMessageError::Unsupported}, | ||
}; | ||
use xcm::prelude::{ | ||
AliasOrigin, ByGenesis, ExportMessage, Here, Instruction, Location, NetworkId, Parachain, | ||
Weight, | ||
}; | ||
use xcm_executor::traits::{DenyExecution, Properties}; | ||
|
||
#[test] | ||
fn test_deny_export_message_from() { | ||
parameter_types! { | ||
pub Source1: Location = Location::new(1, Parachain(1)); | ||
pub Source2: Location = Location::new(1, Parachain(2)); | ||
pub Remote1: NetworkId = ByGenesis([1;32]); | ||
pub Remote2: NetworkId = ByGenesis([2;32]); | ||
} | ||
|
||
// Deny ExportMessage when both of the conditions met: | ||
// 1: source != Source1 | ||
// 2: network == Remote1 | ||
pub type Denied = DenyExportMessageFrom<EverythingBut<Equals<Source1>>, Equals<Remote1>>; | ||
|
||
let assert_deny_execution = |mut xcm: Vec<Instruction<()>>, origin, expected_result| { | ||
assert_eq!( | ||
Denied::deny_execution( | ||
&origin, | ||
&mut xcm, | ||
Weight::zero(), | ||
&mut Properties { weight_credit: Weight::zero(), message_id: None } | ||
), | ||
expected_result | ||
); | ||
}; | ||
|
||
// A message without an `ExportMessage` should pass | ||
assert_deny_execution(vec![AliasOrigin(Here.into())], Source1::get(), Ok(())); | ||
|
||
// `ExportMessage` from source1 and remote1 should pass | ||
assert_deny_execution( | ||
vec![ExportMessage { network: Remote1::get(), destination: Here, xcm: Default::default() }], | ||
Source1::get(), | ||
Ok(()), | ||
); | ||
|
||
// `ExportMessage` from source1 and remote2 should pass | ||
assert_deny_execution( | ||
vec![ExportMessage { network: Remote2::get(), destination: Here, xcm: Default::default() }], | ||
Source1::get(), | ||
Ok(()), | ||
); | ||
|
||
// `ExportMessage` from source2 and remote2 should pass | ||
assert_deny_execution( | ||
vec![ExportMessage { network: Remote2::get(), destination: Here, xcm: Default::default() }], | ||
Source2::get(), | ||
Ok(()), | ||
); | ||
|
||
// `ExportMessage` from source2 and remote1 should be banned | ||
assert_deny_execution( | ||
vec![ExportMessage { network: Remote1::get(), destination: Here, xcm: Default::default() }], | ||
Source2::get(), | ||
Err(Unsupported), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.