referrerpolicy=no-referrer-when-downgrade

bridge_hub_common/
message_queue.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//! Runtime configuration for MessageQueue pallet
16use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
17use core::marker::PhantomData;
18use cumulus_primitives_core::{AggregateMessageOrigin as CumulusAggregateMessageOrigin, ParaId};
19use frame_support::{
20	traits::{ProcessMessage, ProcessMessageError, QueueFootprint, QueuePausedQuery},
21	weights::WeightMeter,
22};
23use pallet_message_queue::OnQueueChanged;
24use scale_info::TypeInfo;
25use snowbridge_core::ChannelId;
26use sp_core::H256;
27use xcm::latest::prelude::{Junction, Location};
28
29/// The aggregate origin of an inbound message.
30/// This is specialized for BridgeHub, as the snowbridge-outbound-queue-pallet is also using
31/// the shared MessageQueue pallet.
32#[derive(
33	Encode,
34	Decode,
35	DecodeWithMemTracking,
36	Copy,
37	MaxEncodedLen,
38	Clone,
39	Eq,
40	PartialEq,
41	TypeInfo,
42	Debug,
43)]
44pub enum AggregateMessageOrigin {
45	/// The message came from the para-chain itself.
46	Here,
47	/// The message came from the relay-chain.
48	///
49	/// This is used by the DMP queue.
50	Parent,
51	/// The message came from a sibling para-chain.
52	///
53	/// This is used by the HRMP queue.
54	Sibling(ParaId),
55	/// The message came from a snowbridge channel.
56	///
57	/// This is used by Snowbridge inbound queue.
58	Snowbridge(ChannelId),
59	SnowbridgeV2(H256),
60}
61
62impl From<AggregateMessageOrigin> for Location {
63	fn from(origin: AggregateMessageOrigin) -> Self {
64		use AggregateMessageOrigin::*;
65		match origin {
66			Here => Location::here(),
67			Parent => Location::parent(),
68			Sibling(id) => Location::new(1, Junction::Parachain(id.into())),
69			// NOTE: We don't need this conversion for Snowbridge. However, we have to
70			// implement it anyway as xcm_builder::ProcessXcmMessage requires it.
71			_ => Location::default(),
72		}
73	}
74}
75
76impl From<CumulusAggregateMessageOrigin> for AggregateMessageOrigin {
77	fn from(origin: CumulusAggregateMessageOrigin) -> Self {
78		match origin {
79			CumulusAggregateMessageOrigin::Here => Self::Here,
80			CumulusAggregateMessageOrigin::Parent => Self::Parent,
81			CumulusAggregateMessageOrigin::Sibling(id) => Self::Sibling(id),
82		}
83	}
84}
85
86#[cfg(feature = "runtime-benchmarks")]
87impl From<u32> for AggregateMessageOrigin {
88	fn from(x: u32) -> Self {
89		match x {
90			0 => Self::Here,
91			1 => Self::Parent,
92			p => Self::Sibling(ParaId::from(p)),
93		}
94	}
95}
96
97/// Routes messages to either the XCMP or Snowbridge processor.
98pub struct BridgeHubMessageRouter<XcmpProcessor, SnowbridgeProcessor>(
99	PhantomData<(XcmpProcessor, SnowbridgeProcessor)>,
100)
101where
102	XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
103	SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>;
104impl<XcmpProcessor, SnowbridgeProcessor> ProcessMessage
105	for BridgeHubMessageRouter<XcmpProcessor, SnowbridgeProcessor>
106where
107	XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
108	SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
109{
110	type Origin = AggregateMessageOrigin;
111	fn process_message(
112		message: &[u8],
113		origin: Self::Origin,
114		meter: &mut WeightMeter,
115		id: &mut [u8; 32],
116	) -> Result<bool, ProcessMessageError> {
117		use AggregateMessageOrigin::*;
118		match origin {
119			Here | Parent | Sibling(_) =>
120				XcmpProcessor::process_message(message, origin, meter, id),
121			Snowbridge(_) => SnowbridgeProcessor::process_message(message, origin, meter, id),
122			SnowbridgeV2(_) => Err(ProcessMessageError::Unsupported),
123		}
124	}
125}
126
127/// Routes messages to either the XCMP|Snowbridge V1 processor|Snowbridge V2 processor
128pub struct BridgeHubDualMessageRouter<XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2>(
129	PhantomData<(XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2)>,
130)
131where
132	XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
133	SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>;
134
135impl<XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2> ProcessMessage
136	for BridgeHubDualMessageRouter<XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2>
137where
138	XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
139	SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
140	SnowbridgeProcessorV2: ProcessMessage<Origin = AggregateMessageOrigin>,
141{
142	type Origin = AggregateMessageOrigin;
143
144	fn process_message(
145		message: &[u8],
146		origin: Self::Origin,
147		meter: &mut WeightMeter,
148		id: &mut [u8; 32],
149	) -> Result<bool, ProcessMessageError> {
150		use AggregateMessageOrigin::*;
151		match origin {
152			Here | Parent | Sibling(_) =>
153				XcmpProcessor::process_message(message, origin, meter, id),
154			Snowbridge(_) => SnowbridgeProcessor::process_message(message, origin, meter, id),
155			SnowbridgeV2(_) => SnowbridgeProcessorV2::process_message(message, origin, meter, id),
156		}
157	}
158}
159
160/// Narrow the scope of the `Inner` query from `AggregateMessageOrigin` to `ParaId`.
161///
162/// All non-`Sibling` variants will be ignored.
163pub struct NarrowOriginToSibling<Inner>(PhantomData<Inner>);
164impl<Inner: QueuePausedQuery<ParaId>> QueuePausedQuery<AggregateMessageOrigin>
165	for NarrowOriginToSibling<Inner>
166{
167	fn is_paused(origin: &AggregateMessageOrigin) -> bool {
168		match origin {
169			AggregateMessageOrigin::Sibling(id) => Inner::is_paused(id),
170			_ => false,
171		}
172	}
173}
174
175impl<Inner: OnQueueChanged<ParaId>> OnQueueChanged<AggregateMessageOrigin>
176	for NarrowOriginToSibling<Inner>
177{
178	fn on_queue_changed(origin: AggregateMessageOrigin, fp: QueueFootprint) {
179		if let AggregateMessageOrigin::Sibling(id) = origin {
180			Inner::on_queue_changed(id, fp)
181		}
182	}
183}
184
185/// Convert a sibling `ParaId` to an `AggregateMessageOrigin`.
186pub struct ParaIdToSibling;
187impl sp_runtime::traits::Convert<ParaId, AggregateMessageOrigin> for ParaIdToSibling {
188	fn convert(para_id: ParaId) -> AggregateMessageOrigin {
189		AggregateMessageOrigin::Sibling(para_id)
190	}
191}