use frame_support::{
ensure,
traits::{ProcessMessage, ProcessMessageError},
};
use parity_scale_codec::{Decode, FullCodec, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_std::{fmt::Debug, marker::PhantomData};
use sp_weights::{Weight, WeightMeter};
use xcm::prelude::*;
pub struct ProcessXcmMessage<MessageOrigin, XcmExecutor, Call>(
PhantomData<(MessageOrigin, XcmExecutor, Call)>,
);
impl<
MessageOrigin: Into<MultiLocation> + FullCodec + MaxEncodedLen + Clone + Eq + PartialEq + TypeInfo + Debug,
XcmExecutor: ExecuteXcm<Call>,
Call,
> ProcessMessage for ProcessXcmMessage<MessageOrigin, XcmExecutor, Call>
{
type Origin = MessageOrigin;
fn process_message(
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
id: &mut XcmHash,
) -> Result<bool, ProcessMessageError> {
let versioned_message = VersionedXcm::<Call>::decode(&mut &message[..])
.map_err(|_| ProcessMessageError::Corrupt)?;
let message = Xcm::<Call>::try_from(versioned_message)
.map_err(|_| ProcessMessageError::Unsupported)?;
let pre = XcmExecutor::prepare(message).map_err(|_| ProcessMessageError::Unsupported)?;
let required = pre.weight_of();
ensure!(meter.can_consume(required), ProcessMessageError::Overweight(required));
let (consumed, result) = match XcmExecutor::execute(origin.into(), pre, id, Weight::zero())
{
Outcome::Complete(w) => (w, Ok(true)),
Outcome::Incomplete(w, _) => (w, Ok(false)),
Outcome::Error(_) => (required, Err(ProcessMessageError::Unsupported)),
};
meter.consume(consumed);
result
}
}
#[cfg(test)]
mod tests {
use super::*;
use frame_support::{
assert_err, assert_ok,
traits::{ProcessMessageError, ProcessMessageError::*},
};
use parity_scale_codec::Encode;
use polkadot_test_runtime::*;
use xcm::{v2, v3, VersionedXcm};
const ORIGIN: Junction = Junction::OnlyChild;
type Processor =
ProcessXcmMessage<Junction, xcm_executor::XcmExecutor<xcm_config::XcmConfig>, RuntimeCall>;
#[test]
fn process_message_trivial_works() {
assert!(process(v2_xcm(true)).unwrap());
assert!(process(v3_xcm(true)).unwrap());
}
#[test]
fn process_message_trivial_fails() {
assert!(!process(v3_xcm(false)).unwrap());
assert!(!process(v3_xcm(false)).unwrap());
}
#[test]
fn process_message_corrupted_fails() {
let msgs: &[&[u8]] = &[&[], &[55, 66], &[123, 222, 233]];
for msg in msgs {
assert_err!(process_raw(msg), Corrupt);
}
}
#[test]
fn process_message_overweight_fails() {
for msg in [v3_xcm(true), v3_xcm(false), v3_xcm(false), v2_xcm(false)] {
let msg = &msg.encode()[..];
for i in 0..10 {
let meter = &mut WeightMeter::from_limit((i * 10).into());
let mut id = [0; 32];
assert_err!(
Processor::process_message(msg, ORIGIN, meter, &mut id),
Overweight(1000.into())
);
assert_eq!(meter.consumed(), 0.into());
}
let meter = &mut WeightMeter::from_limit(1000.into());
let mut id = [0; 32];
assert_ok!(Processor::process_message(msg, ORIGIN, meter, &mut id));
assert_eq!(meter.consumed(), 1000.into());
}
}
fn v2_xcm(success: bool) -> VersionedXcm<RuntimeCall> {
let instr = if success {
v3::Instruction::<RuntimeCall>::ClearOrigin
} else {
v3::Instruction::<RuntimeCall>::Trap(1)
};
VersionedXcm::V3(v3::Xcm::<RuntimeCall>(vec![instr]))
}
fn v3_xcm(success: bool) -> VersionedXcm<RuntimeCall> {
let instr = if success {
v2::Instruction::<RuntimeCall>::ClearOrigin
} else {
v2::Instruction::<RuntimeCall>::Trap(1)
};
VersionedXcm::V2(v2::Xcm::<RuntimeCall>(vec![instr]))
}
fn process(msg: VersionedXcm<RuntimeCall>) -> Result<bool, ProcessMessageError> {
process_raw(msg.encode().as_slice())
}
fn process_raw(raw: &[u8]) -> Result<bool, ProcessMessageError> {
Processor::process_message(raw, ORIGIN, &mut WeightMeter::max_limit(), &mut [0; 32])
}
}