referrerpolicy=no-referrer-when-downgrade

bridge_hub_common/
barriers.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use core::{marker::PhantomData, ops::ControlFlow};
17use cumulus_primitives_core::Weight;
18use frame_support::traits::{Contains, ProcessMessageError};
19use xcm::prelude::{ExportMessage, Instruction, Location, NetworkId};
20
21use xcm_builder::{CreateMatcher, MatchXcm};
22use xcm_executor::traits::{DenyExecution, Properties};
23
24/// Deny execution if the message contains instruction `ExportMessage` with
25/// a. origin is contained in `FromOrigin` (i.e.`FromOrigin::Contains(origin)`)
26/// b. network is contained in `ToGlobalConsensus`, (i.e. `ToGlobalConsensus::contains(network)`)
27pub struct DenyExportMessageFrom<FromOrigin, ToGlobalConsensus>(
28	PhantomData<(FromOrigin, ToGlobalConsensus)>,
29);
30
31impl<FromOrigin, ToGlobalConsensus> DenyExecution
32	for DenyExportMessageFrom<FromOrigin, ToGlobalConsensus>
33where
34	FromOrigin: Contains<Location>,
35	ToGlobalConsensus: Contains<NetworkId>,
36{
37	fn deny_execution<RuntimeCall>(
38		origin: &Location,
39		message: &mut [Instruction<RuntimeCall>],
40		_max_weight: Weight,
41		_properties: &mut Properties,
42	) -> Result<(), ProcessMessageError> {
43		// This barrier only cares about messages with `origin` matching `FromOrigin`.
44		if !FromOrigin::contains(origin) {
45			return Ok(())
46		}
47		message.matcher().match_next_inst_while(
48			|_| true,
49			|inst| match inst {
50				ExportMessage { network, .. } if ToGlobalConsensus::contains(network) =>
51					Err(ProcessMessageError::Unsupported),
52				_ => Ok(ControlFlow::Continue(())),
53			},
54		)?;
55		Ok(())
56	}
57}