#![cfg_attr(not(feature = "std"), no_std)]
pub mod migration;
use codec::{Decode, DecodeLimit, Encode};
use cumulus_primitives_core::{relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler};
use frame_support::{
traits::EnsureOrigin,
weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight},
};
pub use pallet::*;
use scale_info::TypeInfo;
use sp_runtime::RuntimeDebug;
use sp_std::{convert::TryFrom, prelude::*};
use xcm::{
latest::{prelude::*, Weight as XcmWeight},
VersionedXcm, MAX_XCM_DECODE_DEPTH,
};
const DEFAULT_POV_SIZE: u64 = 64 * 1024; #[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct ConfigData {
max_individual: Weight,
}
impl Default for ConfigData {
fn default() -> Self {
Self {
max_individual: Weight::from_parts(
10u64 * WEIGHT_REF_TIME_PER_MILLIS, DEFAULT_POV_SIZE, ),
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Default, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct PageIndexData {
begin_used: PageCounter,
end_used: PageCounter,
overweight_count: OverweightIndex,
}
pub type MessageId = [u8; 32];
pub type OverweightIndex = u64;
pub type PageCounter = u32;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(migration::STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type XcmExecutor: ExecuteXcm<Self::RuntimeCall>;
type ExecuteOverweightOrigin: EnsureOrigin<Self::RuntimeOrigin>;
}
#[pallet::storage]
pub(super) type Configuration<T> = StorageValue<_, ConfigData, ValueQuery>;
#[pallet::storage]
pub(super) type PageIndex<T> = StorageValue<_, PageIndexData, ValueQuery>;
#[pallet::storage]
pub(super) type Pages<T> =
StorageMap<_, Blake2_128Concat, PageCounter, Vec<(RelayBlockNumber, Vec<u8>)>, ValueQuery>;
#[pallet::storage]
pub(super) type Overweight<T> =
StorageMap<_, Blake2_128Concat, OverweightIndex, (RelayBlockNumber, Vec<u8>), OptionQuery>;
#[pallet::error]
pub enum Error<T> {
Unknown,
OverLimit,
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
migration::migrate_to_latest::<T>()
}
fn on_idle(_now: T::BlockNumber, max_weight: Weight) -> Weight {
Self::service_queue(max_weight)
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_ref_time(weight_limit.saturating_add(1_000_000)))]
pub fn service_overweight(
origin: OriginFor<T>,
index: OverweightIndex,
weight_limit: XcmWeight,
) -> DispatchResultWithPostInfo {
T::ExecuteOverweightOrigin::ensure_origin(origin)?;
let (sent_at, data) = Overweight::<T>::get(index).ok_or(Error::<T>::Unknown)?;
let weight_used =
Self::try_service_message(Weight::from_ref_time(weight_limit), sent_at, &data[..])
.map_err(|_| Error::<T>::OverLimit)?;
Overweight::<T>::remove(index);
Self::deposit_event(Event::OverweightServiced { overweight_index: index, weight_used });
Ok(Some(weight_used.saturating_add(Weight::from_ref_time(1_000_000))).into())
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
InvalidFormat { message_id: MessageId },
UnsupportedVersion { message_id: MessageId },
ExecutedDownward { message_id: MessageId, outcome: Outcome },
WeightExhausted { message_id: MessageId, remaining_weight: Weight, required_weight: Weight },
OverweightEnqueued {
message_id: MessageId,
overweight_index: OverweightIndex,
required_weight: Weight,
},
OverweightServiced { overweight_index: OverweightIndex, weight_used: Weight },
}
impl<T: Config> Pallet<T> {
fn service_queue(limit: Weight) -> Weight {
PageIndex::<T>::mutate(|page_index| Self::do_service_queue(limit, page_index))
}
fn do_service_queue(limit: Weight, page_index: &mut PageIndexData) -> Weight {
let mut used = Weight::zero();
while page_index.begin_used < page_index.end_used {
let page = Pages::<T>::take(page_index.begin_used);
for (i, &(sent_at, ref data)) in page.iter().enumerate() {
match Self::try_service_message(limit.saturating_sub(used), sent_at, &data[..])
{
Ok(w) => used += w,
Err(..) => {
Pages::<T>::insert(page_index.begin_used, &page[i..]);
return used
},
}
}
page_index.begin_used += 1;
}
if page_index.begin_used == page_index.end_used {
page_index.begin_used = 0;
page_index.end_used = 0;
}
used
}
pub(crate) fn try_service_message(
limit: Weight,
_sent_at: RelayBlockNumber,
mut data: &[u8],
) -> Result<Weight, (MessageId, Weight)> {
let message_id = sp_io::hashing::blake2_256(data);
let maybe_msg = VersionedXcm::<T::RuntimeCall>::decode_all_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut data,
)
.map(Xcm::<T::RuntimeCall>::try_from);
match maybe_msg {
Err(_) => {
Self::deposit_event(Event::InvalidFormat { message_id });
Ok(Weight::zero())
},
Ok(Err(())) => {
Self::deposit_event(Event::UnsupportedVersion { message_id });
Ok(Weight::zero())
},
Ok(Ok(x)) => {
let outcome = T::XcmExecutor::execute_xcm(Parent, x, limit.ref_time());
match outcome {
Outcome::Error(XcmError::WeightLimitReached(required)) =>
Err((message_id, Weight::from_ref_time(required))),
outcome => {
let weight_used = Weight::from_ref_time(outcome.weight_used());
Self::deposit_event(Event::ExecutedDownward { message_id, outcome });
Ok(weight_used)
},
}
},
}
}
}
impl<T: Config> DmpMessageHandler for Pallet<T> {
fn handle_dmp_messages(
iter: impl Iterator<Item = (RelayBlockNumber, Vec<u8>)>,
limit: Weight,
) -> Weight {
let mut page_index = PageIndex::<T>::get();
let config = Configuration::<T>::get();
let mut used = Self::do_service_queue(limit, &mut page_index);
let item_count = iter.size_hint().0;
let mut maybe_enqueue_page = if page_index.end_used > page_index.begin_used {
Some(Vec::with_capacity(item_count))
} else {
None
};
for (i, (sent_at, data)) in iter.enumerate() {
if maybe_enqueue_page.is_none() {
let remaining_weight = limit.saturating_sub(used);
match Self::try_service_message(remaining_weight, sent_at, &data[..]) {
Ok(consumed) => used += consumed,
Err((message_id, required_weight)) =>
{
if required_weight.any_gt(config.max_individual) {
let overweight_index = page_index.overweight_count;
Overweight::<T>::insert(overweight_index, (sent_at, data));
Self::deposit_event(Event::OverweightEnqueued {
message_id,
overweight_index,
required_weight,
});
page_index.overweight_count += 1;
continue
} else {
let item_count_left = item_count.saturating_sub(i);
maybe_enqueue_page = Some(Vec::with_capacity(item_count_left));
Self::deposit_event(Event::WeightExhausted {
message_id,
remaining_weight,
required_weight,
});
}
},
}
}
if let Some(ref mut enqueue_page) = maybe_enqueue_page {
enqueue_page.push((sent_at, data));
}
}
if let Some(enqueue_page) = maybe_enqueue_page {
Pages::<T>::insert(page_index.end_used, enqueue_page);
page_index.end_used += 1;
}
PageIndex::<T>::put(page_index);
used
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate as dmp_queue;
use codec::Encode;
use cumulus_primitives_core::ParaId;
use frame_support::{assert_noop, parameter_types, traits::OnIdle};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
DispatchError::BadOrigin,
};
use sp_version::RuntimeVersion;
use std::cell::RefCell;
use xcm::latest::{MultiLocation, OriginKind, Weight as XCMWeight};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
type Xcm = xcm::latest::Xcm<RuntimeCall>;
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
DmpQueue: dmp_queue::{Pallet, Call, Storage, Event<T>},
}
);
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub Version: RuntimeVersion = RuntimeVersion {
spec_name: sp_version::create_runtime_str!("test"),
impl_name: sp_version::create_runtime_str!("system-test"),
authoring_version: 1,
spec_version: 1,
impl_version: 1,
apis: sp_version::create_apis_vec!([]),
transaction_version: 1,
state_version: 1,
};
pub const ParachainId: ParaId = ParaId::new(200);
pub const ReservedXcmpWeight: Weight = Weight::zero();
pub const ReservedDmpWeight: Weight = Weight::zero();
}
type AccountId = u64;
impl frame_system::Config for Test {
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type BlockLength = ();
type BlockWeights = ();
type Version = Version;
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type DbWeight = ();
type BaseCallFilter = frame_support::traits::Everything;
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
thread_local! {
pub static TRACE: RefCell<Vec<(Xcm, Outcome)>> = RefCell::new(Vec::new());
}
pub fn take_trace() -> Vec<(Xcm, Outcome)> {
TRACE.with(|q| {
let q = &mut *q.borrow_mut();
let r = q.clone();
q.clear();
r
})
}
pub struct MockExec;
impl ExecuteXcm<RuntimeCall> for MockExec {
fn execute_xcm_in_credit(
_origin: impl Into<MultiLocation>,
message: Xcm,
weight_limit: XCMWeight,
_credit: XCMWeight,
) -> Outcome {
let o = match (message.0.len(), &message.0.first()) {
(1, Some(Transact { require_weight_at_most, .. })) => {
if *require_weight_at_most <= weight_limit {
Outcome::Complete(*require_weight_at_most)
} else {
Outcome::Error(XcmError::WeightLimitReached(*require_weight_at_most))
}
},
_ => Outcome::Incomplete(1000.min(weight_limit), XcmError::Unimplemented),
};
TRACE.with(|q| q.borrow_mut().push((message, o.clone())));
o
}
}
impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type XcmExecutor = MockExec;
type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;
}
pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
}
fn enqueue(enqueued: &[Xcm]) {
if !enqueued.is_empty() {
let mut index = PageIndex::<Test>::get();
Pages::<Test>::insert(
index.end_used,
enqueued
.iter()
.map(|m| (0, VersionedXcm::<RuntimeCall>::from(m.clone()).encode()))
.collect::<Vec<_>>(),
);
index.end_used += 1;
PageIndex::<Test>::put(index);
}
}
fn handle_messages(incoming: &[Xcm], limit: Weight) -> Weight {
let iter = incoming
.iter()
.map(|m| (0, VersionedXcm::<RuntimeCall>::from(m.clone()).encode()));
DmpQueue::handle_dmp_messages(iter, limit)
}
fn msg(weight: XCMWeight) -> Xcm {
Xcm(vec![Transact {
origin_type: OriginKind::Native,
require_weight_at_most: weight,
call: Vec::new().into(),
}])
}
fn msg_complete(weight: XCMWeight) -> (Xcm, Outcome) {
(msg(weight), Outcome::Complete(weight))
}
fn msg_limit_reached(weight: XCMWeight) -> (Xcm, Outcome) {
(msg(weight), Outcome::Error(XcmError::WeightLimitReached(weight)))
}
fn pages_queued() -> PageCounter {
PageIndex::<Test>::get().end_used - PageIndex::<Test>::get().begin_used
}
fn queue_is_empty() -> bool {
pages_queued() == 0
}
fn overweights() -> Vec<OverweightIndex> {
(0..PageIndex::<Test>::get().overweight_count)
.filter(|i| Overweight::<Test>::contains_key(i))
.collect::<Vec<_>>()
}
#[test]
fn basic_setup_works() {
new_test_ext().execute_with(|| {
let weight_used = handle_messages(&[], Weight::from_ref_time(1000));
assert_eq!(weight_used, Weight::zero());
assert_eq!(take_trace(), Vec::new());
assert!(queue_is_empty());
});
}
#[test]
fn service_inline_complete_works() {
new_test_ext().execute_with(|| {
let incoming = vec![msg(1000), msg(1001)];
let weight_used = handle_messages(&incoming, Weight::from_ref_time(2500));
assert_eq!(weight_used, Weight::from_ref_time(2001));
assert_eq!(take_trace(), vec![msg_complete(1000), msg_complete(1001)]);
assert!(queue_is_empty());
});
}
#[test]
fn service_enqueued_works() {
new_test_ext().execute_with(|| {
let enqueued = vec![msg(1000), msg(1001), msg(1002)];
enqueue(&enqueued);
let weight_used = handle_messages(&[], Weight::from_ref_time(2500));
assert_eq!(weight_used, Weight::from_ref_time(2001));
assert_eq!(
take_trace(),
vec![msg_complete(1000), msg_complete(1001), msg_limit_reached(1002),]
);
});
}
#[test]
fn enqueue_works() {
new_test_ext().execute_with(|| {
let incoming = vec![msg(1000), msg(1001), msg(1002)];
let weight_used = handle_messages(&incoming, Weight::from_ref_time(999));
assert_eq!(weight_used, Weight::zero());
assert_eq!(
PageIndex::<Test>::get(),
PageIndexData { begin_used: 0, end_used: 1, overweight_count: 0 }
);
assert_eq!(Pages::<Test>::get(0).len(), 3);
assert_eq!(take_trace(), vec![msg_limit_reached(1000)]);
let weight_used = handle_messages(&[], Weight::from_ref_time(2500));
assert_eq!(weight_used, Weight::from_ref_time(2001));
assert_eq!(
take_trace(),
vec![msg_complete(1000), msg_complete(1001), msg_limit_reached(1002),]
);
let weight_used = handle_messages(&[], Weight::from_ref_time(2500));
assert_eq!(weight_used, Weight::from_ref_time(1002));
assert_eq!(take_trace(), vec![msg_complete(1002),]);
assert!(queue_is_empty());
});
}
#[test]
fn service_inline_then_enqueue_works() {
new_test_ext().execute_with(|| {
let incoming = vec![msg(1000), msg(1001), msg(1002)];
let weight_used = handle_messages(&incoming, Weight::from_ref_time(1500));
assert_eq!(weight_used, Weight::from_ref_time(1000));
assert_eq!(pages_queued(), 1);
assert_eq!(Pages::<Test>::get(0).len(), 2);
assert_eq!(take_trace(), vec![msg_complete(1000), msg_limit_reached(1001),]);
let weight_used = handle_messages(&[], Weight::from_ref_time(2500));
assert_eq!(weight_used, Weight::from_ref_time(2003));
assert_eq!(take_trace(), vec![msg_complete(1001), msg_complete(1002),]);
assert!(queue_is_empty());
});
}
#[test]
fn service_enqueued_and_inline_works() {
new_test_ext().execute_with(|| {
let enqueued = vec![msg(1000), msg(1001)];
let incoming = vec![msg(1002), msg(1003)];
enqueue(&enqueued);
let weight_used = handle_messages(&incoming, Weight::from_ref_time(5000));
assert_eq!(weight_used, Weight::from_ref_time(4006));
assert_eq!(
take_trace(),
vec![
msg_complete(1000),
msg_complete(1001),
msg_complete(1002),
msg_complete(1003),
]
);
assert!(queue_is_empty());
});
}
#[test]
fn service_enqueued_partially_and_then_enqueue_works() {
new_test_ext().execute_with(|| {
let enqueued = vec![msg(1000), msg(10001)];
let incoming = vec![msg(1002), msg(1003)];
enqueue(&enqueued);
let weight_used = handle_messages(&incoming, Weight::from_ref_time(5000));
assert_eq!(weight_used, Weight::from_ref_time(1000));
assert_eq!(take_trace(), vec![msg_complete(1000), msg_limit_reached(10001),]);
assert_eq!(pages_queued(), 2);
let weight_used = handle_messages(&[], Weight::from_ref_time(5000));
assert_eq!(weight_used, Weight::zero());
assert_eq!(take_trace(), vec![msg_limit_reached(10001),]);
let weight_used = handle_messages(&[], Weight::from_ref_time(20000));
assert_eq!(weight_used, Weight::from_ref_time(12006));
assert_eq!(
take_trace(),
vec![msg_complete(10001), msg_complete(1002), msg_complete(1003),]
);
assert!(queue_is_empty());
});
}
#[test]
fn service_enqueued_completely_and_then_enqueue_works() {
new_test_ext().execute_with(|| {
let enqueued = vec![msg(1000), msg(1001)];
let incoming = vec![msg(10002), msg(1003)];
enqueue(&enqueued);
let weight_used = handle_messages(&incoming, Weight::from_ref_time(5000));
assert_eq!(weight_used, Weight::from_ref_time(2001));
assert_eq!(
take_trace(),
vec![msg_complete(1000), msg_complete(1001), msg_limit_reached(10002),]
);
assert_eq!(pages_queued(), 1);
let weight_used = handle_messages(&[], Weight::from_ref_time(20000));
assert_eq!(weight_used, Weight::from_ref_time(11005));
assert_eq!(take_trace(), vec![msg_complete(10002), msg_complete(1003),]);
assert!(queue_is_empty());
});
}
#[test]
fn service_enqueued_then_inline_then_enqueue_works() {
new_test_ext().execute_with(|| {
let enqueued = vec![msg(1000), msg(1001)];
let incoming = vec![msg(1002), msg(10003)];
enqueue(&enqueued);
let weight_used = handle_messages(&incoming, Weight::from_ref_time(5000));
assert_eq!(weight_used, Weight::from_ref_time(3003));
assert_eq!(
take_trace(),
vec![
msg_complete(1000),
msg_complete(1001),
msg_complete(1002),
msg_limit_reached(10003),
]
);
assert_eq!(pages_queued(), 1);
let weight_used = handle_messages(&[], Weight::from_ref_time(20000));
assert_eq!(weight_used, Weight::from_ref_time(10003));
assert_eq!(take_trace(), vec![msg_complete(10003),]);
assert!(queue_is_empty());
});
}
#[test]
fn page_crawling_works() {
new_test_ext().execute_with(|| {
let enqueued = vec![msg(1000), msg(1001)];
enqueue(&enqueued);
let weight_used = handle_messages(&vec![msg(1002)], Weight::from_ref_time(1500));
assert_eq!(weight_used, Weight::from_ref_time(1000));
assert_eq!(take_trace(), vec![msg_complete(1000), msg_limit_reached(1001),]);
assert_eq!(pages_queued(), 2);
assert_eq!(PageIndex::<Test>::get().begin_used, 0);
let weight_used = handle_messages(&vec![msg(1003)], Weight::from_ref_time(1500));
assert_eq!(weight_used, Weight::from_ref_time(1001));
assert_eq!(take_trace(), vec![msg_complete(1001), msg_limit_reached(1002),]);
assert_eq!(pages_queued(), 2);
assert_eq!(PageIndex::<Test>::get().begin_used, 1);
let weight_used = handle_messages(&vec![msg(1004)], Weight::from_ref_time(1500));
assert_eq!(weight_used, Weight::from_ref_time(1002));
assert_eq!(take_trace(), vec![msg_complete(1002), msg_limit_reached(1003),]);
assert_eq!(pages_queued(), 2);
assert_eq!(PageIndex::<Test>::get().begin_used, 2);
});
}
#[test]
fn overweight_should_not_block_queue() {
new_test_ext().execute_with(|| {
Configuration::<Test>::put(ConfigData { max_individual: Weight::from_ref_time(9999) });
let incoming = vec![msg(1000), msg(10001), msg(1002)];
let weight_used = handle_messages(&incoming, Weight::from_ref_time(2500));
assert_eq!(weight_used, Weight::from_ref_time(2002));
assert!(queue_is_empty());
assert_eq!(
take_trace(),
vec![msg_complete(1000), msg_limit_reached(10001), msg_complete(1002),]
);
assert_eq!(overweights(), vec![0]);
});
}
#[test]
fn overweights_should_be_manually_executable() {
new_test_ext().execute_with(|| {
Configuration::<Test>::put(ConfigData { max_individual: Weight::from_ref_time(9999) });
let incoming = vec![msg(10000)];
let weight_used = handle_messages(&incoming, Weight::from_ref_time(2500));
assert_eq!(weight_used, Weight::zero());
assert_eq!(take_trace(), vec![msg_limit_reached(10000)]);
assert_eq!(overweights(), vec![0]);
assert_noop!(
DmpQueue::service_overweight(RuntimeOrigin::signed(1), 0, 20000),
BadOrigin
);
assert_noop!(
DmpQueue::service_overweight(RuntimeOrigin::root(), 1, 20000),
Error::<Test>::Unknown
);
assert_noop!(
DmpQueue::service_overweight(RuntimeOrigin::root(), 0, 9999),
Error::<Test>::OverLimit
);
assert_eq!(take_trace(), vec![msg_limit_reached(10000)]);
let base_weight = super::Call::<Test>::service_overweight { index: 0, weight_limit: 0 }
.get_dispatch_info()
.weight;
use frame_support::dispatch::GetDispatchInfo;
let info = DmpQueue::service_overweight(RuntimeOrigin::root(), 0, 20000).unwrap();
let actual_weight = info.actual_weight.unwrap();
assert_eq!(actual_weight, base_weight + Weight::from_ref_time(10000));
assert_eq!(take_trace(), vec![msg_complete(10000)]);
assert!(overweights().is_empty());
assert_noop!(
DmpQueue::service_overweight(RuntimeOrigin::root(), 0, 20000),
Error::<Test>::Unknown
);
});
}
#[test]
fn on_idle_should_service_queue() {
new_test_ext().execute_with(|| {
enqueue(&vec![msg(1000), msg(1001)]);
enqueue(&vec![msg(1002), msg(1003)]);
enqueue(&vec![msg(1004), msg(1005)]);
let weight_used = DmpQueue::on_idle(1, Weight::from_ref_time(6000));
assert_eq!(weight_used, Weight::from_ref_time(5010));
assert_eq!(
take_trace(),
vec![
msg_complete(1000),
msg_complete(1001),
msg_complete(1002),
msg_complete(1003),
msg_complete(1004),
msg_limit_reached(1005),
]
);
assert_eq!(pages_queued(), 1);
});
}
}