use codec::{Decode, Encode, MaxEncodedLen};
use core::marker::PhantomData;
use cumulus_primitives_core::{AggregateMessageOrigin as CumulusAggregateMessageOrigin, ParaId};
use frame_support::{
traits::{ProcessMessage, ProcessMessageError, QueueFootprint, QueuePausedQuery},
weights::WeightMeter,
};
use pallet_message_queue::OnQueueChanged;
use scale_info::TypeInfo;
use snowbridge_core::ChannelId;
use xcm::latest::prelude::{Junction, Location};
#[derive(Encode, Decode, Copy, MaxEncodedLen, Clone, Eq, PartialEq, TypeInfo, Debug)]
pub enum AggregateMessageOrigin {
Here,
Parent,
Sibling(ParaId),
Snowbridge(ChannelId),
}
impl From<AggregateMessageOrigin> for Location {
fn from(origin: AggregateMessageOrigin) -> Self {
use AggregateMessageOrigin::*;
match origin {
Here => Location::here(),
Parent => Location::parent(),
Sibling(id) => Location::new(1, Junction::Parachain(id.into())),
Snowbridge(_) => Location::default(),
}
}
}
impl From<CumulusAggregateMessageOrigin> for AggregateMessageOrigin {
fn from(origin: CumulusAggregateMessageOrigin) -> Self {
match origin {
CumulusAggregateMessageOrigin::Here => Self::Here,
CumulusAggregateMessageOrigin::Parent => Self::Parent,
CumulusAggregateMessageOrigin::Sibling(id) => Self::Sibling(id),
}
}
}
#[cfg(feature = "runtime-benchmarks")]
impl From<u32> for AggregateMessageOrigin {
fn from(x: u32) -> Self {
match x {
0 => Self::Here,
1 => Self::Parent,
p => Self::Sibling(ParaId::from(p)),
}
}
}
pub struct BridgeHubMessageRouter<XcmpProcessor, SnowbridgeProcessor>(
PhantomData<(XcmpProcessor, SnowbridgeProcessor)>,
)
where
XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>;
impl<XcmpProcessor, SnowbridgeProcessor> ProcessMessage
for BridgeHubMessageRouter<XcmpProcessor, SnowbridgeProcessor>
where
XcmpProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
SnowbridgeProcessor: ProcessMessage<Origin = AggregateMessageOrigin>,
{
type Origin = AggregateMessageOrigin;
fn process_message(
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
id: &mut [u8; 32],
) -> Result<bool, ProcessMessageError> {
use AggregateMessageOrigin::*;
match origin {
Here | Parent | Sibling(_) =>
XcmpProcessor::process_message(message, origin, meter, id),
Snowbridge(_) => SnowbridgeProcessor::process_message(message, origin, meter, id),
}
}
}
pub struct NarrowOriginToSibling<Inner>(PhantomData<Inner>);
impl<Inner: QueuePausedQuery<ParaId>> QueuePausedQuery<AggregateMessageOrigin>
for NarrowOriginToSibling<Inner>
{
fn is_paused(origin: &AggregateMessageOrigin) -> bool {
match origin {
AggregateMessageOrigin::Sibling(id) => Inner::is_paused(id),
_ => false,
}
}
}
impl<Inner: OnQueueChanged<ParaId>> OnQueueChanged<AggregateMessageOrigin>
for NarrowOriginToSibling<Inner>
{
fn on_queue_changed(origin: AggregateMessageOrigin, fp: QueueFootprint) {
if let AggregateMessageOrigin::Sibling(id) = origin {
Inner::on_queue_changed(id, fp)
}
}
}
pub struct ParaIdToSibling;
impl sp_runtime::traits::Convert<ParaId, AggregateMessageOrigin> for ParaIdToSibling {
fn convert(para_id: ParaId) -> AggregateMessageOrigin {
AggregateMessageOrigin::Sibling(para_id)
}
}