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
86impl From<H256> for AggregateMessageOrigin {
87	fn from(hash: H256) -> Self {
88		Self::SnowbridgeV2(hash)
89	}
90}
91
92#[cfg(feature = "runtime-benchmarks")]
93impl From<u32> for AggregateMessageOrigin {
94	fn from(x: u32) -> Self {
95		match x {
96			0 => Self::Here,
97			1 => Self::Parent,
98			p => Self::Sibling(ParaId::from(p)),
99		}
100	}
101}
102
103/// Routes messages to either the XCMP or Snowbridge processor.
104pub struct BridgeHubMessageRouter<XcmpProcessor, SnowbridgeProcessor>(
105	PhantomData<(XcmpProcessor, SnowbridgeProcessor)>,
106)
107where
108	XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
109	SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>;
110impl<XcmpProcessor, SnowbridgeProcessor> ProcessMessage
111	for BridgeHubMessageRouter<XcmpProcessor, SnowbridgeProcessor>
112where
113	XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
114	SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
115{
116	type Origin = AggregateMessageOrigin;
117	fn process_message(
118		message: &[u8],
119		origin: Self::Origin,
120		meter: &mut WeightMeter,
121		id: &mut [u8; 32],
122	) -> Result<bool, ProcessMessageError> {
123		use AggregateMessageOrigin::*;
124		match origin {
125			Here | Parent | Sibling(_) =>
126				XcmpProcessor::process_message(message, origin, meter, id),
127			Snowbridge(_) => SnowbridgeProcessor::process_message(message, origin, meter, id),
128			SnowbridgeV2(_) => Err(ProcessMessageError::Unsupported),
129		}
130	}
131}
132
133/// Routes messages to either the XCMP|Snowbridge V1 processor|Snowbridge V2 processor
134pub struct BridgeHubDualMessageRouter<XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2>(
135	PhantomData<(XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2)>,
136)
137where
138	XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
139	SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>;
140
141impl<XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2> ProcessMessage
142	for BridgeHubDualMessageRouter<XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2>
143where
144	XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
145	SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
146	SnowbridgeProcessorV2: ProcessMessage<Origin = AggregateMessageOrigin>,
147{
148	type Origin = AggregateMessageOrigin;
149
150	fn process_message(
151		message: &[u8],
152		origin: Self::Origin,
153		meter: &mut WeightMeter,
154		id: &mut [u8; 32],
155	) -> Result<bool, ProcessMessageError> {
156		use AggregateMessageOrigin::*;
157		match origin {
158			Here | Parent | Sibling(_) =>
159				XcmpProcessor::process_message(message, origin, meter, id),
160			Snowbridge(_) => SnowbridgeProcessor::process_message(message, origin, meter, id),
161			SnowbridgeV2(_) => SnowbridgeProcessorV2::process_message(message, origin, meter, id),
162		}
163	}
164}
165
166/// Narrow the scope of the `Inner` query from `AggregateMessageOrigin` to `ParaId`.
167///
168/// All non-`Sibling` variants will be ignored.
169pub struct NarrowOriginToSibling<Inner>(PhantomData<Inner>);
170impl<Inner: QueuePausedQuery<ParaId>> QueuePausedQuery<AggregateMessageOrigin>
171	for NarrowOriginToSibling<Inner>
172{
173	fn is_paused(origin: &AggregateMessageOrigin) -> bool {
174		match origin {
175			AggregateMessageOrigin::Sibling(id) => Inner::is_paused(id),
176			_ => false,
177		}
178	}
179}
180
181impl<Inner: OnQueueChanged<ParaId>> OnQueueChanged<AggregateMessageOrigin>
182	for NarrowOriginToSibling<Inner>
183{
184	fn on_queue_changed(origin: AggregateMessageOrigin, fp: QueueFootprint) {
185		if let AggregateMessageOrigin::Sibling(id) = origin {
186			Inner::on_queue_changed(id, fp)
187		}
188	}
189}
190
191/// Convert a sibling `ParaId` to an `AggregateMessageOrigin`.
192pub struct ParaIdToSibling;
193impl sp_runtime::traits::Convert<ParaId, AggregateMessageOrigin> for ParaIdToSibling {
194	fn convert(para_id: ParaId) -> AggregateMessageOrigin {
195		AggregateMessageOrigin::Sibling(para_id)
196	}
197}