pub mod mock_message_queue;
extern crate alloc;
pub use codec::Encode;
pub use paste;
pub use alloc::collections::vec_deque::VecDeque;
pub use core::{cell::RefCell, marker::PhantomData};
pub use frame_support::{
traits::{EnqueueMessage, Get, ProcessMessage, ProcessMessageError, ServiceQueues},
weights::{Weight, WeightMeter},
};
pub use sp_io::{hashing::blake2_256, TestExternalities};
pub use polkadot_core_primitives::BlockNumber as RelayBlockNumber;
pub use polkadot_parachain_primitives::primitives::{
DmpMessageHandler as DmpMessageHandlerT, Id as ParaId, XcmpMessageFormat,
XcmpMessageHandler as XcmpMessageHandlerT,
};
pub use polkadot_runtime_parachains::{
dmp,
inclusion::{AggregateMessageOrigin, UmpQueueId},
};
pub use xcm::{latest::prelude::*, VersionedXcm};
pub use xcm_builder::ProcessXcmMessage;
pub use xcm_executor::XcmExecutor;
pub trait TestExt {
fn new_ext() -> sp_io::TestExternalities;
fn reset_ext();
fn execute_without_dispatch<R>(execute: impl FnOnce() -> R) -> R;
fn dispatch_xcm_buses();
fn execute_with<R>(execute: impl FnOnce() -> R) -> R {
let result = Self::execute_without_dispatch(execute);
Self::dispatch_xcm_buses();
result
}
}
pub enum MessageKind {
Ump,
Dmp,
Xcmp,
}
pub fn encode_xcm(message: Xcm<()>, message_kind: MessageKind) -> Vec<u8> {
match message_kind {
MessageKind::Ump | MessageKind::Dmp => VersionedXcm::<()>::from(message).encode(),
MessageKind::Xcmp => {
let fmt = XcmpMessageFormat::ConcatenatedVersionedXcm;
let mut outbound = fmt.encode();
let encoded = VersionedXcm::<()>::from(message).encode();
outbound.extend_from_slice(&encoded[..]);
outbound
},
}
}
pub fn fake_message_hash<T>(message: &Xcm<T>) -> XcmHash {
message.using_encoded(blake2_256)
}
#[macro_export]
#[rustfmt::skip]
macro_rules! decl_test_relay_chain {
(
pub struct $name:ident {
Runtime = $runtime:path,
RuntimeCall = $runtime_call:path,
RuntimeEvent = $runtime_event:path,
XcmConfig = $xcm_config:path,
MessageQueue = $mq:path,
System = $system:path,
new_ext = $new_ext:expr,
}
) => {
pub struct $name;
$crate::__impl_ext!($name, $new_ext);
impl $crate::ProcessMessage for $name {
type Origin = $crate::ParaId;
fn process_message(
msg: &[u8],
para: Self::Origin,
meter: &mut $crate::WeightMeter,
id: &mut [u8; 32],
) -> Result<bool, $crate::ProcessMessageError> {
use $crate::{Weight, AggregateMessageOrigin, UmpQueueId, ServiceQueues, EnqueueMessage};
use $mq as message_queue;
use $runtime_event as runtime_event;
Self::execute_with(|| {
<$mq as EnqueueMessage<AggregateMessageOrigin>>::enqueue_message(
msg.try_into().expect("Message too long"),
AggregateMessageOrigin::Ump(UmpQueueId::Para(para.clone()))
);
<$system>::reset_events();
<$mq as ServiceQueues>::service_queues(Weight::MAX);
let events = <$system>::events();
let event = events.last().expect("There must be at least one event");
match &event.event {
runtime_event::MessageQueue(
pallet_message_queue::Event::Processed {origin, ..}) => {
assert_eq!(origin, &AggregateMessageOrigin::Ump(UmpQueueId::Para(para)));
},
event => panic!("Unexpected event: {:#?}", event),
}
Ok(true)
})
}
}
};
}
#[macro_export]
macro_rules! decl_test_parachain {
(
pub struct $name:ident {
Runtime = $runtime:path,
XcmpMessageHandler = $xcmp_message_handler:path,
DmpMessageHandler = $dmp_message_handler:path,
new_ext = $new_ext:expr,
}
) => {
pub struct $name;
$crate::__impl_ext!($name, $new_ext);
impl $crate::XcmpMessageHandlerT for $name {
fn handle_xcmp_messages<
'a,
I: Iterator<Item = ($crate::ParaId, $crate::RelayBlockNumber, &'a [u8])>,
>(
iter: I,
max_weight: $crate::Weight,
) -> $crate::Weight {
use $crate::{TestExt, XcmpMessageHandlerT};
$name::execute_with(|| {
<$xcmp_message_handler>::handle_xcmp_messages(iter, max_weight)
})
}
}
impl $crate::DmpMessageHandlerT for $name {
fn handle_dmp_messages(
iter: impl Iterator<Item = ($crate::RelayBlockNumber, Vec<u8>)>,
max_weight: $crate::Weight,
) -> $crate::Weight {
use $crate::{DmpMessageHandlerT, TestExt};
$name::execute_with(|| {
<$dmp_message_handler>::handle_dmp_messages(iter, max_weight)
})
}
}
};
}
#[macro_export]
macro_rules! __impl_ext {
($name:ident, $new_ext:expr) => {
$crate::paste::paste! {
$crate::__impl_ext!(@impl $name, $new_ext, [<EXT_ $name:upper>]);
}
};
(@impl $name:ident, $new_ext:expr, $ext_name:ident) => {
thread_local! {
pub static $ext_name: $crate::RefCell<$crate::TestExternalities>
= $crate::RefCell::new($new_ext);
}
impl $crate::TestExt for $name {
fn new_ext() -> $crate::TestExternalities {
$new_ext
}
fn reset_ext() {
$ext_name.with(|v| *v.borrow_mut() = $new_ext);
}
fn execute_without_dispatch<R>(execute: impl FnOnce() -> R) -> R {
$ext_name.with(|v| v.borrow_mut().execute_with(execute))
}
fn dispatch_xcm_buses() {
while exists_messages_in_any_bus() {
if let Err(xcm_error) = process_relay_messages() {
panic!("Relay chain XCM execution failure: {:?}", xcm_error);
}
if let Err(xcm_error) = process_para_messages() {
panic!("Parachain XCM execution failure: {:?}", xcm_error);
}
}
}
}
};
}
thread_local! {
pub static PARA_MESSAGE_BUS: RefCell<VecDeque<(ParaId, Location, Xcm<()>)>>
= RefCell::new(VecDeque::new());
pub static RELAY_MESSAGE_BUS: RefCell<VecDeque<(Location, Xcm<()>)>>
= RefCell::new(VecDeque::new());
}
#[macro_export]
macro_rules! decl_test_network {
(
pub struct $name:ident {
relay_chain = $relay_chain:ty,
parachains = vec![ $( ($para_id:expr, $parachain:ty), )* ],
}
) => {
use $crate::Encode;
pub struct $name;
impl $name {
pub fn reset() {
use $crate::{TestExt, VecDeque};
$crate::RELAY_MESSAGE_BUS.with(|b| b.replace(VecDeque::new()));
$crate::PARA_MESSAGE_BUS.with(|b| b.replace(VecDeque::new()));
<$relay_chain>::reset_ext();
$( <$parachain>::reset_ext(); )*
}
}
fn exists_messages_in_any_bus() -> bool {
use $crate::{RELAY_MESSAGE_BUS, PARA_MESSAGE_BUS};
let no_relay_messages_left = RELAY_MESSAGE_BUS.with(|b| b.borrow().is_empty());
let no_parachain_messages_left = PARA_MESSAGE_BUS.with(|b| b.borrow().is_empty());
!(no_relay_messages_left && no_parachain_messages_left)
}
fn process_para_messages() -> $crate::XcmResult {
use $crate::{ProcessMessage, XcmpMessageHandlerT};
while let Some((para_id, destination, message)) = $crate::PARA_MESSAGE_BUS.with(
|b| b.borrow_mut().pop_front()) {
match destination.unpack() {
(1, []) => {
let encoded = $crate::encode_xcm(message, $crate::MessageKind::Ump);
let mut _id = [0; 32];
let r = <$relay_chain>::process_message(
encoded.as_slice(), para_id,
&mut $crate::WeightMeter::new(),
&mut _id,
);
match r {
Err($crate::ProcessMessageError::Overweight(required)) =>
return Err($crate::XcmError::WeightLimitReached(required)),
Err(_) => return Err($crate::XcmError::Unimplemented),
Ok(_) => (),
}
},
$(
(1, [$crate::Parachain(id)]) if *id == $para_id => {
let encoded = $crate::encode_xcm(message, $crate::MessageKind::Xcmp);
let messages = vec![(para_id, 1, &encoded[..])];
let _weight = <$parachain>::handle_xcmp_messages(
messages.into_iter(),
$crate::Weight::MAX,
);
},
)*
_ => {
return Err($crate::XcmError::Unroutable);
}
}
}
Ok(())
}
fn process_relay_messages() -> $crate::XcmResult {
use $crate::DmpMessageHandlerT;
while let Some((destination, message)) = $crate::RELAY_MESSAGE_BUS.with(
|b| b.borrow_mut().pop_front()) {
match destination.unpack() {
$(
(0, [$crate::Parachain(id)]) if *id == $para_id => {
let encoded = $crate::encode_xcm(message, $crate::MessageKind::Dmp);
let messages = vec![(1, encoded)];
let _weight = <$parachain>::handle_dmp_messages(
messages.into_iter(), $crate::Weight::MAX,
);
},
)*
_ => return Err($crate::XcmError::Transport("Only sends to children parachain.")),
}
}
Ok(())
}
pub struct ParachainXcmRouter<T>($crate::PhantomData<T>);
impl<T: $crate::Get<$crate::ParaId>> $crate::SendXcm for ParachainXcmRouter<T> {
type Ticket = ($crate::ParaId, $crate::Location, $crate::Xcm<()>);
fn validate(
destination: &mut Option<$crate::Location>,
message: &mut Option<$crate::Xcm<()>>,
) -> $crate::SendResult<($crate::ParaId, $crate::Location, $crate::Xcm<()>)> {
use $crate::XcmpMessageHandlerT;
let d = destination.take().ok_or($crate::SendError::MissingArgument)?;
match d.unpack() {
(1, []) => {},
$(
(1, [$crate::Parachain(id)]) if id == &$para_id => {}
)*
_ => {
*destination = Some(d);
return Err($crate::SendError::NotApplicable)
},
}
let m = message.take().ok_or($crate::SendError::MissingArgument)?;
Ok(((T::get(), d, m), $crate::Assets::new()))
}
fn deliver(
triple: ($crate::ParaId, $crate::Location, $crate::Xcm<()>),
) -> Result<$crate::XcmHash, $crate::SendError> {
let hash = $crate::fake_message_hash(&triple.2);
$crate::PARA_MESSAGE_BUS.with(|b| b.borrow_mut().push_back(triple));
Ok(hash)
}
}
pub struct RelayChainXcmRouter;
impl $crate::SendXcm for RelayChainXcmRouter {
type Ticket = ($crate::Location, $crate::Xcm<()>);
fn validate(
destination: &mut Option<$crate::Location>,
message: &mut Option<$crate::Xcm<()>>,
) -> $crate::SendResult<($crate::Location, $crate::Xcm<()>)> {
use $crate::DmpMessageHandlerT;
let d = destination.take().ok_or($crate::SendError::MissingArgument)?;
match d.unpack() {
$(
(0, [$crate::Parachain(id)]) if id == &$para_id => {},
)*
_ => {
*destination = Some(d);
return Err($crate::SendError::NotApplicable)
},
}
let m = message.take().ok_or($crate::SendError::MissingArgument)?;
Ok(((d, m), $crate::Assets::new()))
}
fn deliver(
pair: ($crate::Location, $crate::Xcm<()>),
) -> Result<$crate::XcmHash, $crate::SendError> {
let hash = $crate::fake_message_hash(&pair.1);
$crate::RELAY_MESSAGE_BUS.with(|b| b.borrow_mut().push_back(pair));
Ok(hash)
}
}
};
}