Skip to main content

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			},
128			Snowbridge(_) => SnowbridgeProcessor::process_message(message, origin, meter, id),
129			SnowbridgeV2(_) => Err(ProcessMessageError::Unsupported),
130		}
131	}
132}
133
134/// Routes messages to either the XCMP|Snowbridge V1 processor|Snowbridge V2 processor
135pub struct BridgeHubDualMessageRouter<XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2>(
136	PhantomData<(XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2)>,
137)
138where
139	XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
140	SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>;
141
142impl<XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2> ProcessMessage
143	for BridgeHubDualMessageRouter<XcmpProcessor, SnowbridgeProcessor, SnowbridgeProcessorV2>
144where
145	XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
146	SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
147	SnowbridgeProcessorV2: ProcessMessage<Origin = AggregateMessageOrigin>,
148{
149	type Origin = AggregateMessageOrigin;
150
151	fn process_message(
152		message: &[u8],
153		origin: Self::Origin,
154		meter: &mut WeightMeter,
155		id: &mut [u8; 32],
156	) -> Result<bool, ProcessMessageError> {
157		use AggregateMessageOrigin::*;
158		match origin {
159			Here | Parent | Sibling(_) => {
160				XcmpProcessor::process_message(message, origin, meter, id)
161			},
162			Snowbridge(_) => SnowbridgeProcessor::process_message(message, origin, meter, id),
163			SnowbridgeV2(_) => SnowbridgeProcessorV2::process_message(message, origin, meter, id),
164		}
165	}
166}
167
168/// Narrow the scope of the `Inner` query from `AggregateMessageOrigin` to `ParaId`.
169///
170/// All non-`Sibling` variants will be ignored.
171pub struct NarrowOriginToSibling<Inner>(PhantomData<Inner>);
172impl<Inner: QueuePausedQuery<ParaId>> QueuePausedQuery<AggregateMessageOrigin>
173	for NarrowOriginToSibling<Inner>
174{
175	fn is_paused(origin: &AggregateMessageOrigin) -> bool {
176		match origin {
177			AggregateMessageOrigin::Sibling(id) => Inner::is_paused(id),
178			_ => false,
179		}
180	}
181}
182
183impl<Inner: OnQueueChanged<ParaId>> OnQueueChanged<AggregateMessageOrigin>
184	for NarrowOriginToSibling<Inner>
185{
186	fn on_queue_changed(origin: AggregateMessageOrigin, fp: QueueFootprint) {
187		if let AggregateMessageOrigin::Sibling(id) = origin {
188			Inner::on_queue_changed(id, fp)
189		}
190	}
191}
192
193/// Convert a sibling `ParaId` to an `AggregateMessageOrigin`.
194pub struct ParaIdToSibling;
195impl sp_runtime::traits::Convert<ParaId, AggregateMessageOrigin> for ParaIdToSibling {
196	fn convert(para_id: ParaId) -> AggregateMessageOrigin {
197		AggregateMessageOrigin::Sibling(para_id)
198	}
199}