mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-23 07:21:17 +00:00
Use Message Queue as DMP and XCMP dispatch queue (#1246)
(imported from https://github.com/paritytech/cumulus/pull/2157) ## Changes This MR refactores the XCMP, Parachains System and DMP pallets to use the [MessageQueue](https://github.com/paritytech/substrate/pull/12485) for delayed execution of incoming messages. The DMP pallet is entirely replaced by the MQ and thereby removed. This allows for PoV-bounded execution and resolves a number of issues that stem from the current work-around. All System Parachains adopt this change. The most important changes are in `primitives/core/src/lib.rs`, `parachains/common/src/process_xcm_message.rs`, `pallets/parachain-system/src/lib.rs`, `pallets/xcmp-queue/src/lib.rs` and the runtime configs. ### DMP Queue Pallet The pallet got removed and its logic refactored into parachain-system. Overweight message management can be done directly through the MQ pallet. Final undeployment migrations are provided by `cumulus_pallet_dmp_queue::UndeployDmpQueue` and `DeleteDmpQueue` that can be configured with an aux config trait like: ```rust parameter_types! { pub const DmpQueuePalletName: &'static str = \"DmpQueue\" < CHANGE ME; pub const RelayOrigin: AggregateMessageOrigin = AggregateMessageOrigin::Parent; } impl cumulus_pallet_dmp_queue::MigrationConfig for Runtime { type PalletName = DmpQueuePalletName; type DmpHandler = frame_support::traits::EnqueueWithOrigin<MessageQueue, RelayOrigin>; type DbWeight = <Runtime as frame_system::Config>::DbWeight; } // And adding them to your Migrations tuple: pub type Migrations = ( ... cumulus_pallet_dmp_queue::UndeployDmpQueue<Runtime>, cumulus_pallet_dmp_queue::DeleteDmpQueue<Runtime>, ); ``` ### XCMP Queue pallet Removed all dispatch queue functionality. Incoming XCMP messages are now either: Immediately handled if they are Signals, enqueued into the MQ pallet otherwise. New config items for the XCMP queue pallet: ```rust /// The actual queue implementation that retains the messages for later processing. type XcmpQueue: EnqueueMessage<ParaId>; /// How a XCM over HRMP from a sibling parachain should be processed. type XcmpProcessor: ProcessMessage<Origin = ParaId>; /// The maximal number of suspended XCMP channels at the same time. #[pallet::constant] type MaxInboundSuspended: Get<u32>; ``` How to configure those: ```rust // Use the MessageQueue pallet to store messages for later processing. The `TransformOrigin` is needed since // the MQ pallet itself operators on `AggregateMessageOrigin` but we want to enqueue `ParaId`s. type XcmpQueue = TransformOrigin<MessageQueue, AggregateMessageOrigin, ParaId, ParaIdToSibling>; // Process XCMP messages from siblings. This is type-safe to only accept `ParaId`s. They will be dispatched // with origin `Junction::Sibling(…)`. type XcmpProcessor = ProcessFromSibling< ProcessXcmMessage< AggregateMessageOrigin, xcm_executor::XcmExecutor<xcm_config::XcmConfig>, RuntimeCall, >, >; // Not really important what to choose here. Just something larger than the maximal number of channels. type MaxInboundSuspended = sp_core::ConstU32<1_000>; ``` The `InboundXcmpStatus` storage item was replaced by `InboundXcmpSuspended` since it now only tracks inbound queue suspension and no message indices anymore. Now only sends the most recent channel `Signals`, as all prio ones are out-dated anyway. ### Parachain System pallet For `DMP` messages instead of forwarding them to the `DMP` pallet, it now pushes them to the configured `DmpQueue`. The message processing which was triggered in `set_validation_data` is now being done by the MQ pallet `on_initialize`. XCMP messages are still handed off to the `XcmpMessageHandler` (XCMP-Queue pallet) - no change here. New config items for the parachain system pallet: ```rust /// Queues inbound downward messages for delayed processing. /// /// Analogous to the `XcmpQueue` of the XCMP queue pallet. type DmpQueue: EnqueueMessage<AggregateMessageOrigin>; ``` How to configure: ```rust /// Use the MQ pallet to store DMP messages for delayed processing. type DmpQueue = MessageQueue; ``` ## Message Flow The flow of messages on the parachain side. Messages come in from the left via the `Validation Data` and finally end up at the `Xcm Executor` on the right.  ## Further changes - Bumped the default suspension, drop and resume thresholds in `QueueConfigData::default()`. - `XcmpQueue::{suspend_xcm_execution, resume_xcm_execution}` errors when they would be a noop. - Properly validate the `QueueConfigData` before setting it. - Marked weight files as auto-generated so they wont auto-expand in the MR files view. - Move the `hypothetical` asserts to `frame_support` under the name `experimental_hypothetically` Questions: - [ ] What about the ugly `#[cfg(feature = \"runtime-benchmarks\")]` in the runtimes? Not sure how to best fix. Just having them like this makes tests fail that rely on the real message processor when the feature is enabled. - [ ] Need a good weight for `MessageQueueServiceWeight`. The scheduler already takes 80% so I put it to 10% but that is quite low. TODO: - [x] Remove c&p code after https://github.com/paritytech/polkadot/pull/6271 - [x] Use `HandleMessage` once it is public in Substrate - [x] fix `runtime-benchmarks` feature https://github.com/paritytech/polkadot/pull/6966 - [x] Benchmarks - [x] Tests - [ ] Migrate `InboundXcmpStatus` to `InboundXcmpSuspended` - [x] Possibly cleanup Migrations (DMP+XCMP) - [x] optional: create `TransformProcessMessageOrigin` in Substrate and replace `ProcessFromSibling` - [ ] Rerun weights on ref HW --------- Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Liam Aharon <liam.aharon@hotmail.com> Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: command-bot <>
This commit is contained in:
committed by
GitHub
parent
7df0417bcd
commit
e1c033ebe1
@@ -3,19 +3,24 @@ name = "cumulus-pallet-dmp-queue"
|
||||
version = "0.1.0"
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
repository.workspace = true
|
||||
description = "Migrates messages from the old DMP queue pallet."
|
||||
license = "Apache-2.0"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "3.0.0", features = [ "derive" ], default-features = false }
|
||||
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
|
||||
log = { version = "0.4.20", default-features = false }
|
||||
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
|
||||
|
||||
# Substrate
|
||||
frame-support = { path = "../../../substrate/frame/support", default-features = false}
|
||||
frame-system = { path = "../../../substrate/frame/system", default-features = false}
|
||||
sp-io = { path = "../../../substrate/primitives/io", default-features = false}
|
||||
sp-runtime = { path = "../../../substrate/primitives/runtime", default-features = false}
|
||||
sp-std = { path = "../../../substrate/primitives/std", default-features = false}
|
||||
frame-benchmarking = { path = "../../../substrate/frame/benchmarking", default-features = false, optional = true }
|
||||
frame-support = { path = "../../../substrate/frame/support", default-features = false }
|
||||
frame-system = { path = "../../../substrate/frame/system", default-features = false }
|
||||
sp-std = { path = "../../../substrate/primitives/std", default-features = false }
|
||||
sp-runtime = { path = "../../../substrate/primitives/runtime", default-features = false }
|
||||
sp-io = { path = "../../../substrate/primitives/io", default-features = false }
|
||||
|
||||
# Polkadot
|
||||
xcm = { package = "staging-xcm", path = "../../../polkadot/xcm", default-features = false}
|
||||
@@ -24,25 +29,34 @@ xcm = { package = "staging-xcm", path = "../../../polkadot/xcm", default-feature
|
||||
cumulus-primitives-core = { path = "../../primitives/core", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
sp-core = { path = "../../../substrate/primitives/core", default-features = false}
|
||||
sp-version = { path = "../../../substrate/primitives/version", default-features = false}
|
||||
sp-core = { path = "../../../substrate/primitives/core" }
|
||||
sp-tracing = { path = "../../../substrate/primitives/tracing" }
|
||||
|
||||
[features]
|
||||
default = [ "std" ]
|
||||
|
||||
std = [
|
||||
"codec/std",
|
||||
"cumulus-primitives-core/std",
|
||||
"frame-benchmarking?/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"log/std",
|
||||
"scale-info/std",
|
||||
"sp-core/std",
|
||||
"sp-io/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
"sp-version/std",
|
||||
"xcm/std",
|
||||
]
|
||||
|
||||
runtime-benchmarks = [
|
||||
"cumulus-primitives-core/runtime-benchmarks",
|
||||
"frame-benchmarking/runtime-benchmarks",
|
||||
"frame-support/runtime-benchmarks",
|
||||
"frame-system/runtime-benchmarks",
|
||||
"sp-runtime/runtime-benchmarks",
|
||||
]
|
||||
|
||||
try-runtime = [
|
||||
"frame-support/try-runtime",
|
||||
"frame-system/try-runtime",
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Benchmarking for the `cumulus-pallet-dmp-queue`.
|
||||
|
||||
#![cfg(feature = "runtime-benchmarks")]
|
||||
|
||||
use crate::*;
|
||||
|
||||
use frame_benchmarking::v2::*;
|
||||
use frame_support::{pallet_prelude::*, traits::Hooks};
|
||||
use sp_std::vec;
|
||||
|
||||
#[benchmarks]
|
||||
mod benchmarks {
|
||||
use super::*;
|
||||
|
||||
/// This benchmark uses the proper maximal message length.
|
||||
#[benchmark]
|
||||
fn on_idle_good_msg() {
|
||||
let msg = vec![123; MaxDmpMessageLenOf::<T>::get() as usize];
|
||||
|
||||
Pages::<T>::insert(0, vec![(123, msg.clone())]);
|
||||
PageIndex::<T>::put(PageIndexData { begin_used: 0, end_used: 1, overweight_count: 0 });
|
||||
MigrationStatus::<T>::set(MigrationState::StartedExport { next_begin_used: 0 });
|
||||
|
||||
#[block]
|
||||
{
|
||||
Pallet::<T>::on_idle(0u32.into(), Weight::MAX);
|
||||
}
|
||||
|
||||
assert_last_event::<T>(Event::Exported { page: 0 }.into());
|
||||
}
|
||||
|
||||
/// This benchmark uses 64 KiB messages to emulate a large old message.
|
||||
#[benchmark]
|
||||
fn on_idle_large_msg() {
|
||||
let msg = vec![123; 1 << 16];
|
||||
|
||||
Pages::<T>::insert(0, vec![(123, msg.clone())]);
|
||||
PageIndex::<T>::put(PageIndexData { begin_used: 0, end_used: 1, overweight_count: 0 });
|
||||
MigrationStatus::<T>::set(MigrationState::StartedExport { next_begin_used: 0 });
|
||||
|
||||
#[block]
|
||||
{
|
||||
Pallet::<T>::on_idle(0u32.into(), Weight::MAX);
|
||||
}
|
||||
|
||||
assert_last_event::<T>(Event::Exported { page: 0 }.into());
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn on_idle_overweight_good_msg() {
|
||||
let msg = vec![123; MaxDmpMessageLenOf::<T>::get() as usize];
|
||||
|
||||
Overweight::<T>::insert(0, (123, msg.clone()));
|
||||
PageIndex::<T>::put(PageIndexData { begin_used: 0, end_used: 1, overweight_count: 1 });
|
||||
MigrationStatus::<T>::set(MigrationState::StartedOverweightExport {
|
||||
next_overweight_index: 0,
|
||||
});
|
||||
|
||||
#[block]
|
||||
{
|
||||
Pallet::<T>::on_idle(0u32.into(), Weight::MAX);
|
||||
}
|
||||
|
||||
assert_last_event::<T>(Event::ExportedOverweight { index: 0 }.into());
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn on_idle_overweight_large_msg() {
|
||||
let msg = vec![123; 1 << 16];
|
||||
|
||||
Overweight::<T>::insert(0, (123, msg.clone()));
|
||||
PageIndex::<T>::put(PageIndexData { begin_used: 0, end_used: 1, overweight_count: 1 });
|
||||
MigrationStatus::<T>::set(MigrationState::StartedOverweightExport {
|
||||
next_overweight_index: 0,
|
||||
});
|
||||
|
||||
#[block]
|
||||
{
|
||||
Pallet::<T>::on_idle(0u32.into(), Weight::MAX);
|
||||
}
|
||||
|
||||
assert_last_event::<T>(Event::ExportOverweightFailed { index: 0 }.into());
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime);
|
||||
}
|
||||
|
||||
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
|
||||
let events = frame_system::Pallet::<T>::events();
|
||||
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
|
||||
let frame_system::EventRecord { event, .. } = events.last().expect("Event expected");
|
||||
assert_eq!(event, &system_event.into());
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -14,110 +14,100 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! A module that is responsible for migration of storage.
|
||||
//! Migrates the storage from the previously deleted DMP pallet.
|
||||
|
||||
use crate::{Config, Configuration, Overweight, Pallet, DEFAULT_POV_SIZE};
|
||||
use frame_support::{
|
||||
pallet_prelude::*,
|
||||
traits::{OnRuntimeUpgrade, StorageVersion},
|
||||
weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight},
|
||||
};
|
||||
use crate::*;
|
||||
use cumulus_primitives_core::relay_chain::BlockNumber as RelayBlockNumber;
|
||||
use frame_support::{pallet_prelude::*, storage_alias, traits::HandleMessage};
|
||||
use sp_std::vec::Vec;
|
||||
|
||||
/// The current storage version.
|
||||
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
|
||||
pub(crate) const LOG: &str = "runtime::dmp-queue-export-xcms";
|
||||
|
||||
/// Migrates the pallet storage to the most recent version.
|
||||
pub struct Migration<T: Config>(PhantomData<T>);
|
||||
|
||||
impl<T: Config> OnRuntimeUpgrade for Migration<T> {
|
||||
fn on_runtime_upgrade() -> Weight {
|
||||
let mut weight = T::DbWeight::get().reads(1);
|
||||
|
||||
if StorageVersion::get::<Pallet<T>>() == 0 {
|
||||
weight.saturating_accrue(migrate_to_v1::<T>());
|
||||
StorageVersion::new(1).put::<Pallet<T>>();
|
||||
weight.saturating_accrue(T::DbWeight::get().writes(1));
|
||||
}
|
||||
|
||||
if StorageVersion::get::<Pallet<T>>() == 1 {
|
||||
weight.saturating_accrue(migrate_to_v2::<T>());
|
||||
StorageVersion::new(2).put::<Pallet<T>>();
|
||||
weight.saturating_accrue(T::DbWeight::get().writes(1));
|
||||
}
|
||||
|
||||
weight
|
||||
}
|
||||
/// The old `PageIndexData` struct.
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Default, Encode, Decode, RuntimeDebug, TypeInfo)]
|
||||
pub struct PageIndexData {
|
||||
/// The lowest used page index.
|
||||
pub begin_used: PageCounter,
|
||||
/// The lowest unused page index.
|
||||
pub end_used: PageCounter,
|
||||
/// The number of overweight messages ever recorded (and thus the lowest free index).
|
||||
pub overweight_count: OverweightIndex,
|
||||
}
|
||||
|
||||
mod v0 {
|
||||
/// The old `MigrationState` type.
|
||||
pub type OverweightIndex = u64;
|
||||
/// The old `MigrationState` type.
|
||||
pub type PageCounter = u32;
|
||||
|
||||
/// The old `PageIndex` storage item.
|
||||
#[storage_alias]
|
||||
pub type PageIndex<T: Config> = StorageValue<Pallet<T>, PageIndexData, ValueQuery>;
|
||||
|
||||
/// The old `Pages` storage item.
|
||||
#[storage_alias]
|
||||
pub type Pages<T: Config> = StorageMap<
|
||||
Pallet<T>,
|
||||
Blake2_128Concat,
|
||||
PageCounter,
|
||||
Vec<(RelayBlockNumber, Vec<u8>)>,
|
||||
ValueQuery,
|
||||
>;
|
||||
|
||||
/// The old `Overweight` storage item.
|
||||
#[storage_alias]
|
||||
pub type Overweight<T: Config> = CountedStorageMap<
|
||||
Pallet<T>,
|
||||
Blake2_128Concat,
|
||||
OverweightIndex,
|
||||
(RelayBlockNumber, Vec<u8>),
|
||||
OptionQuery,
|
||||
>;
|
||||
|
||||
pub(crate) mod testing_only {
|
||||
use super::*;
|
||||
use codec::{Decode, Encode};
|
||||
|
||||
#[derive(Decode, Encode, Debug)]
|
||||
pub struct ConfigData {
|
||||
pub max_individual: u64,
|
||||
}
|
||||
|
||||
impl Default for ConfigData {
|
||||
fn default() -> Self {
|
||||
ConfigData { max_individual: 10u64 * WEIGHT_REF_TIME_PER_MILLIS }
|
||||
}
|
||||
}
|
||||
/// This alias is not used by the migration but only for testing.
|
||||
///
|
||||
/// Note that the alias type is wrong on purpose.
|
||||
#[storage_alias]
|
||||
pub type Configuration<T: Config> = StorageValue<Pallet<T>, u32>;
|
||||
}
|
||||
|
||||
/// Migrates `QueueConfigData` from v1 (using only reference time weights) to v2 (with
|
||||
/// 2D weights).
|
||||
///
|
||||
/// NOTE: Only use this function if you know what you're doing. Default to using
|
||||
/// `migrate_to_latest`.
|
||||
pub fn migrate_to_v1<T: Config>() -> Weight {
|
||||
let translate = |pre: v0::ConfigData| -> super::ConfigData {
|
||||
super::ConfigData {
|
||||
max_individual: Weight::from_parts(pre.max_individual, DEFAULT_POV_SIZE),
|
||||
}
|
||||
/// Migrates a single page to the `DmpSink`.
|
||||
pub(crate) fn migrate_page<T: crate::Config>(p: PageCounter) -> Result<(), ()> {
|
||||
let page = Pages::<T>::take(p);
|
||||
log::debug!(target: LOG, "Migrating page #{p} with {} messages ...", page.len());
|
||||
if page.is_empty() {
|
||||
log::error!(target: LOG, "Page #{p}: EMPTY - storage corrupted?");
|
||||
return Err(())
|
||||
}
|
||||
|
||||
for (m, (block, msg)) in page.iter().enumerate() {
|
||||
let Ok(bound) = BoundedVec::<u8, _>::try_from(msg.clone()) else {
|
||||
log::error!(target: LOG, "[Page {p}] Message #{m}: TOO LONG - dropping");
|
||||
continue
|
||||
};
|
||||
|
||||
T::DmpSink::handle_message(bound.as_bounded_slice());
|
||||
log::debug!(target: LOG, "[Page {p}] Migrated message #{m} from block {block}");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Migrates a single overweight message to the `DmpSink`.
|
||||
pub(crate) fn migrate_overweight<T: crate::Config>(i: OverweightIndex) -> Result<(), ()> {
|
||||
let Some((block, msg)) = Overweight::<T>::take(i) else {
|
||||
log::error!(target: LOG, "[Overweight {i}] Message: EMPTY - storage corrupted?");
|
||||
return Err(())
|
||||
};
|
||||
let Ok(bound) = BoundedVec::<u8, _>::try_from(msg) else {
|
||||
log::error!(target: LOG, "[Overweight {i}] Message: TOO LONG - dropping");
|
||||
return Err(())
|
||||
};
|
||||
|
||||
if Configuration::<T>::translate(|pre| pre.map(translate)).is_err() {
|
||||
log::error!(
|
||||
target: "dmp_queue",
|
||||
"unexpected error when performing translation of the QueueConfig type during storage upgrade to v2"
|
||||
);
|
||||
}
|
||||
T::DmpSink::handle_message(bound.as_bounded_slice());
|
||||
log::debug!(target: LOG, "[Overweight {i}] Migrated message from block {block}");
|
||||
|
||||
T::DbWeight::get().reads_writes(1, 1)
|
||||
}
|
||||
|
||||
/// Migrates `Overweight` so that it initializes the storage map's counter.
|
||||
///
|
||||
/// NOTE: Only use this function if you know what you're doing. Default to using
|
||||
/// `migrate_to_latest`.
|
||||
pub fn migrate_to_v2<T: Config>() -> Weight {
|
||||
let overweight_messages = Overweight::<T>::initialize_counter() as u64;
|
||||
|
||||
T::DbWeight::get().reads_writes(overweight_messages, 1)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::tests::{new_test_ext, Test};
|
||||
|
||||
#[test]
|
||||
fn test_migration_to_v1() {
|
||||
let v0 = v0::ConfigData { max_individual: 30_000_000_000 };
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
frame_support::storage::unhashed::put_raw(
|
||||
&crate::Configuration::<Test>::hashed_key(),
|
||||
&v0.encode(),
|
||||
);
|
||||
|
||||
migrate_to_v1::<Test>();
|
||||
|
||||
let v1 = crate::Configuration::<Test>::get();
|
||||
|
||||
assert_eq!(v0.max_individual, v1.max_individual.ref_time());
|
||||
assert_eq!(v1.max_individual.proof_size(), DEFAULT_POV_SIZE);
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use frame_support::{
|
||||
derive_impl, parameter_types,
|
||||
traits::{HandleMessage, QueueFootprint},
|
||||
};
|
||||
use sp_core::{bounded_vec::BoundedSlice, ConstU32};
|
||||
use sp_runtime::traits::IdentityLookup;
|
||||
|
||||
type Block = frame_system::mocking::MockBlock<Runtime>;
|
||||
|
||||
// Configure a mock runtime to test the pallet.
|
||||
frame_support::construct_runtime!(
|
||||
pub enum Runtime
|
||||
{
|
||||
System: frame_system,
|
||||
DmpQueue: crate,
|
||||
}
|
||||
);
|
||||
|
||||
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
|
||||
impl frame_system::Config for Runtime {
|
||||
type Block = Block;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
}
|
||||
|
||||
impl crate::Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type DmpSink = RecordingDmpSink;
|
||||
type WeightInfo = ();
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
/// All messages that came into the `DmpSink`.
|
||||
pub static RecordedMessages: Vec<Vec<u8>> = vec![];
|
||||
}
|
||||
|
||||
/// Can be used as [`Config::DmpSink`] to record all messages that came in.
|
||||
pub struct RecordingDmpSink;
|
||||
impl HandleMessage for RecordingDmpSink {
|
||||
type MaxMessageLen = ConstU32<16>;
|
||||
|
||||
fn handle_message(msg: BoundedSlice<u8, Self::MaxMessageLen>) {
|
||||
RecordedMessages::mutate(|n| n.push(msg.to_vec()));
|
||||
}
|
||||
|
||||
fn handle_messages<'a>(_: impl Iterator<Item = BoundedSlice<'a, u8, Self::MaxMessageLen>>) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn sweep_queue() {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn footprint() -> QueueFootprint {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||
sp_io::TestExternalities::new(Default::default())
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
// Copyright Parity Technologies (UK) Ltd.
|
||||
// This file is part of Cumulus.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Test the lazy migration.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use super::{migration::*, mock::*};
|
||||
use crate::*;
|
||||
|
||||
use frame_support::{
|
||||
pallet_prelude::*,
|
||||
traits::{OnFinalize, OnIdle, OnInitialize},
|
||||
StorageNoopGuard,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn migration_works() {
|
||||
let mut ext = new_test_ext();
|
||||
ext.execute_with(|| {
|
||||
sp_tracing::try_init_simple();
|
||||
// Insert some storage:
|
||||
PageIndex::<Runtime>::set(PageIndexData {
|
||||
begin_used: 10,
|
||||
end_used: 20,
|
||||
overweight_count: 5,
|
||||
});
|
||||
for p in 10..20 {
|
||||
let msgs = (0..16).map(|i| (p, vec![i as u8; 1])).collect::<Vec<_>>();
|
||||
Pages::<Runtime>::insert(p, msgs);
|
||||
}
|
||||
for i in 0..5 {
|
||||
Overweight::<Runtime>::insert(i, (0, vec![i as u8; 1]));
|
||||
}
|
||||
testing_only::Configuration::<Runtime>::put(123);
|
||||
});
|
||||
// We need to commit, otherwise the keys are removed from the overlay; not the backend.
|
||||
ext.commit_all().unwrap();
|
||||
ext.execute_with(|| {
|
||||
// Run one step of the migration:
|
||||
pre_upgrade_checks::<Runtime>();
|
||||
run_to_block(1);
|
||||
// First we expect a StartedExport event:
|
||||
assert_only_event(Event::StartedExport);
|
||||
|
||||
// Then we expect 10 Exported events:
|
||||
for page in 0..10 {
|
||||
run_to_block(2 + page);
|
||||
assert_only_event(Event::Exported { page: page as u32 + 10 });
|
||||
assert!(!Pages::<Runtime>::contains_key(page as u32), "Page is gone");
|
||||
assert_eq!(
|
||||
MigrationStatus::<Runtime>::get(),
|
||||
MigrationState::StartedExport { next_begin_used: page as u32 + 11 }
|
||||
);
|
||||
}
|
||||
|
||||
// Then we expect a CompletedExport event:
|
||||
run_to_block(12);
|
||||
assert_only_event(Event::CompletedExport);
|
||||
assert_eq!(MigrationStatus::<Runtime>::get(), MigrationState::CompletedExport);
|
||||
|
||||
// Then we expect a StartedOverweightExport event:
|
||||
run_to_block(13);
|
||||
assert_only_event(Event::StartedOverweightExport);
|
||||
assert_eq!(
|
||||
MigrationStatus::<Runtime>::get(),
|
||||
MigrationState::StartedOverweightExport { next_overweight_index: 0 }
|
||||
);
|
||||
|
||||
// Then we expect 5 ExportedOverweight events:
|
||||
for index in 0..5 {
|
||||
run_to_block(14 + index);
|
||||
assert_only_event(Event::ExportedOverweight { index });
|
||||
assert!(!Overweight::<Runtime>::contains_key(index), "Overweight msg is gone");
|
||||
assert_eq!(
|
||||
MigrationStatus::<Runtime>::get(),
|
||||
MigrationState::StartedOverweightExport { next_overweight_index: index + 1 }
|
||||
);
|
||||
}
|
||||
|
||||
// Then we expect a CompletedOverweightExport event:
|
||||
run_to_block(19);
|
||||
assert_only_event(Event::CompletedOverweightExport);
|
||||
assert_eq!(MigrationStatus::<Runtime>::get(), MigrationState::CompletedOverweightExport);
|
||||
|
||||
// Then we expect a StartedCleanup event:
|
||||
run_to_block(20);
|
||||
assert_only_event(Event::StartedCleanup);
|
||||
assert_eq!(
|
||||
MigrationStatus::<Runtime>::get(),
|
||||
MigrationState::StartedCleanup { cursor: None }
|
||||
);
|
||||
});
|
||||
ext.commit_all().unwrap();
|
||||
// Then it cleans up the remaining storage items:
|
||||
ext.execute_with(|| {
|
||||
run_to_block(21);
|
||||
assert_only_event(Event::CleanedSome { keys_removed: 2 });
|
||||
});
|
||||
ext.commit_all().unwrap();
|
||||
ext.execute_with(|| {
|
||||
run_to_block(22);
|
||||
assert_only_event(Event::CleanedSome { keys_removed: 2 });
|
||||
});
|
||||
ext.commit_all().unwrap();
|
||||
ext.execute_with(|| {
|
||||
run_to_block(24);
|
||||
assert_eq!(
|
||||
System::events().into_iter().map(|e| e.event).collect::<Vec<_>>(),
|
||||
vec![
|
||||
Event::CleanedSome { keys_removed: 2 }.into(),
|
||||
Event::Completed { error: false }.into()
|
||||
]
|
||||
);
|
||||
System::reset_events();
|
||||
assert_eq!(MigrationStatus::<Runtime>::get(), MigrationState::Completed);
|
||||
|
||||
post_upgrade_checks::<Runtime>();
|
||||
assert_eq!(RecordedMessages::take().len(), 10 * 16 + 5);
|
||||
|
||||
// Test the storage removal:
|
||||
assert!(!PageIndex::<Runtime>::exists());
|
||||
assert!(!testing_only::Configuration::<Runtime>::exists());
|
||||
assert_eq!(Pages::<Runtime>::iter_keys().count(), 0);
|
||||
assert_eq!(Overweight::<Runtime>::iter_keys().count(), 0);
|
||||
|
||||
// The `MigrationStatus` never disappears and there are no more storage changes:
|
||||
{
|
||||
let _g = StorageNoopGuard::default();
|
||||
|
||||
run_to_block(100);
|
||||
assert_eq!(MigrationStatus::<Runtime>::get(), MigrationState::Completed);
|
||||
assert!(System::events().is_empty());
|
||||
// ... besides the block number
|
||||
System::set_block_number(24);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Too long messages are dropped by the migration.
|
||||
#[test]
|
||||
fn migration_too_long_ignored() {
|
||||
new_test_ext().execute_with(|| {
|
||||
// Setup the storage:
|
||||
PageIndex::<Runtime>::set(PageIndexData {
|
||||
begin_used: 10,
|
||||
end_used: 11,
|
||||
overweight_count: 2,
|
||||
});
|
||||
|
||||
let short = vec![1; 16];
|
||||
let long = vec![0; 17];
|
||||
Pages::<Runtime>::insert(10, vec![(10, short.clone()), (10, long.clone())]);
|
||||
// Insert one good and one bad overweight msg:
|
||||
Overweight::<Runtime>::insert(0, (0, short.clone()));
|
||||
Overweight::<Runtime>::insert(1, (0, long.clone()));
|
||||
|
||||
// Run the migration:
|
||||
pre_upgrade_checks::<Runtime>();
|
||||
run_to_block(100);
|
||||
post_upgrade_checks::<Runtime>();
|
||||
|
||||
assert_eq!(RecordedMessages::take(), vec![short.clone(), short]);
|
||||
|
||||
// Test the storage removal:
|
||||
assert!(!PageIndex::<Runtime>::exists());
|
||||
assert_eq!(Pages::<Runtime>::iter_keys().count(), 0);
|
||||
assert_eq!(Overweight::<Runtime>::iter_keys().count(), 0);
|
||||
});
|
||||
}
|
||||
|
||||
fn run_to_block(n: u64) {
|
||||
assert!(n > System::block_number(), "Cannot go back in time");
|
||||
|
||||
while System::block_number() < n {
|
||||
AllPalletsWithSystem::on_finalize(System::block_number());
|
||||
System::set_block_number(System::block_number() + 1);
|
||||
AllPalletsWithSystem::on_initialize(System::block_number());
|
||||
AllPalletsWithSystem::on_idle(System::block_number(), Weight::MAX);
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_only_event(e: Event<Runtime>) {
|
||||
assert_eq!(System::events().pop().expect("Event expected").event, e.clone().into());
|
||||
assert_eq!(System::events().len(), 1, "Got events: {:?} but wanted {:?}", System::events(), e);
|
||||
System::reset_events();
|
||||
}
|
||||
|
||||
/// TESTING ONLY
|
||||
fn pre_upgrade_checks<T: crate::Config>() {
|
||||
let index = PageIndex::<T>::get();
|
||||
|
||||
// Check that all pages are present.
|
||||
assert!(index.begin_used <= index.end_used, "Invalid page index");
|
||||
for p in index.begin_used..index.end_used {
|
||||
assert!(Pages::<T>::contains_key(p), "Missing page");
|
||||
assert!(Pages::<T>::get(p).len() > 0, "Empty page");
|
||||
}
|
||||
|
||||
// Check that all overweight messages are present.
|
||||
for i in 0..index.overweight_count {
|
||||
assert!(Overweight::<T>::contains_key(i), "Missing overweight message");
|
||||
}
|
||||
}
|
||||
|
||||
/// TESTING ONLY
|
||||
fn post_upgrade_checks<T: crate::Config>() {
|
||||
let index = PageIndex::<T>::get();
|
||||
|
||||
// Check that all pages are removed.
|
||||
for p in index.begin_used..index.end_used {
|
||||
assert!(!Pages::<T>::contains_key(p), "Page should be gone");
|
||||
}
|
||||
assert!(Pages::<T>::iter_keys().next().is_none(), "Un-indexed pages");
|
||||
|
||||
// Check that all overweight messages are removed.
|
||||
for i in 0..index.overweight_count {
|
||||
assert!(!Overweight::<T>::contains_key(i), "Overweight message should be gone");
|
||||
}
|
||||
assert!(Overweight::<T>::iter_keys().next().is_none(), "Un-indexed overweight messages");
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Autogenerated weights for `cumulus_pallet_dmp_queue`
|
||||
//!
|
||||
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
|
||||
//! DATE: 2023-09-15, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
|
||||
//! WORST CASE MAP SIZE: `1000000`
|
||||
//! HOSTNAME: `Olivers-MacBook-Pro.local`, CPU: `<UNKNOWN>`
|
||||
//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("asset-hub-kusama-dev")`, DB CACHE: `1024`
|
||||
|
||||
// Executed Command:
|
||||
// ./target/release/polkadot-parachain
|
||||
// benchmark
|
||||
// pallet
|
||||
// --pallet
|
||||
// cumulus-pallet-dmp-queue
|
||||
// --chain
|
||||
// asset-hub-kusama-dev
|
||||
// --output
|
||||
// cumulus/pallets/dmp-queue/src/weights.rs
|
||||
// --template
|
||||
// substrate/.maintain/frame-weight-template.hbs
|
||||
// --extrinsic
|
||||
//
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
#![allow(unused_parens)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
|
||||
use core::marker::PhantomData;
|
||||
|
||||
/// Weight functions needed for `cumulus_pallet_dmp_queue`.
|
||||
pub trait WeightInfo {
|
||||
fn on_idle_good_msg() -> Weight;
|
||||
fn on_idle_large_msg() -> Weight;
|
||||
fn on_idle_overweight_good_msg() -> Weight;
|
||||
fn on_idle_overweight_large_msg() -> Weight;
|
||||
}
|
||||
|
||||
/// Weights for `cumulus_pallet_dmp_queue` using the Substrate node and recommended hardware.
|
||||
pub struct SubstrateWeight<T>(PhantomData<T>);
|
||||
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
|
||||
/// Storage: `DmpQueue::MigrationStatus` (r:1 w:1)
|
||||
/// Proof: `DmpQueue::MigrationStatus` (`max_values`: Some(1), `max_size`: Some(1028), added: 1523, mode: `MaxEncodedLen`)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca7d95d3e948effbeccff2de2c182672836` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca7d95d3e948effbeccff2de2c182672836` (r:1 w:1)
|
||||
/// Storage: `MessageQueue::BookStateFor` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::BookStateFor` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::ServiceHead` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::ServiceHead` (`max_values`: Some(1), `max_size`: Some(5), added: 500, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::Pages` (r:0 w:1)
|
||||
/// Proof: `MessageQueue::Pages` (`max_values`: None, `max_size`: Some(65585), added: 68060, mode: `MaxEncodedLen`)
|
||||
fn on_idle_good_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65696`
|
||||
// Estimated: `69161`
|
||||
// Minimum execution time: 124_651_000 picoseconds.
|
||||
Weight::from_parts(127_857_000, 0)
|
||||
.saturating_add(Weight::from_parts(0, 69161))
|
||||
.saturating_add(T::DbWeight::get().reads(5))
|
||||
.saturating_add(T::DbWeight::get().writes(5))
|
||||
}
|
||||
/// Storage: `DmpQueue::MigrationStatus` (r:1 w:1)
|
||||
/// Proof: `DmpQueue::MigrationStatus` (`max_values`: Some(1), `max_size`: Some(1028), added: 1523, mode: `MaxEncodedLen`)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca7d95d3e948effbeccff2de2c182672836` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca7d95d3e948effbeccff2de2c182672836` (r:1 w:1)
|
||||
fn on_idle_large_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65659`
|
||||
// Estimated: `69124`
|
||||
// Minimum execution time: 65_684_000 picoseconds.
|
||||
Weight::from_parts(68_039_000, 0)
|
||||
.saturating_add(Weight::from_parts(0, 69124))
|
||||
.saturating_add(T::DbWeight::get().reads(3))
|
||||
.saturating_add(T::DbWeight::get().writes(2))
|
||||
}
|
||||
/// Storage: `DmpQueue::MigrationStatus` (r:1 w:1)
|
||||
/// Proof: `DmpQueue::MigrationStatus` (`max_values`: Some(1), `max_size`: Some(1028), added: 1523, mode: `MaxEncodedLen`)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca70f923ef3252d0166429d36d20ed665a8` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca70f923ef3252d0166429d36d20ed665a8` (r:1 w:1)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca772275f64c354954352b71eea39cfaca2` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca772275f64c354954352b71eea39cfaca2` (r:1 w:1)
|
||||
/// Storage: `MessageQueue::BookStateFor` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::BookStateFor` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::ServiceHead` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::ServiceHead` (`max_values`: Some(1), `max_size`: Some(5), added: 500, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::Pages` (r:0 w:1)
|
||||
/// Proof: `MessageQueue::Pages` (`max_values`: None, `max_size`: Some(65585), added: 68060, mode: `MaxEncodedLen`)
|
||||
fn on_idle_overweight_good_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65726`
|
||||
// Estimated: `69191`
|
||||
// Minimum execution time: 117_657_000 picoseconds.
|
||||
Weight::from_parts(122_035_000, 0)
|
||||
.saturating_add(Weight::from_parts(0, 69191))
|
||||
.saturating_add(T::DbWeight::get().reads(6))
|
||||
.saturating_add(T::DbWeight::get().writes(6))
|
||||
}
|
||||
/// Storage: `DmpQueue::MigrationStatus` (r:1 w:1)
|
||||
/// Proof: `DmpQueue::MigrationStatus` (`max_values`: Some(1), `max_size`: Some(1028), added: 1523, mode: `MaxEncodedLen`)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca70f923ef3252d0166429d36d20ed665a8` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca70f923ef3252d0166429d36d20ed665a8` (r:1 w:1)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca772275f64c354954352b71eea39cfaca2` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca772275f64c354954352b71eea39cfaca2` (r:1 w:1)
|
||||
fn on_idle_overweight_large_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65689`
|
||||
// Estimated: `69154`
|
||||
// Minimum execution time: 59_799_000 picoseconds.
|
||||
Weight::from_parts(61_354_000, 0)
|
||||
.saturating_add(Weight::from_parts(0, 69154))
|
||||
.saturating_add(T::DbWeight::get().reads(4))
|
||||
.saturating_add(T::DbWeight::get().writes(3))
|
||||
}
|
||||
}
|
||||
|
||||
// For backwards compatibility and tests.
|
||||
impl WeightInfo for () {
|
||||
/// Storage: `DmpQueue::MigrationStatus` (r:1 w:1)
|
||||
/// Proof: `DmpQueue::MigrationStatus` (`max_values`: Some(1), `max_size`: Some(1028), added: 1523, mode: `MaxEncodedLen`)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca7d95d3e948effbeccff2de2c182672836` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca7d95d3e948effbeccff2de2c182672836` (r:1 w:1)
|
||||
/// Storage: `MessageQueue::BookStateFor` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::BookStateFor` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::ServiceHead` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::ServiceHead` (`max_values`: Some(1), `max_size`: Some(5), added: 500, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::Pages` (r:0 w:1)
|
||||
/// Proof: `MessageQueue::Pages` (`max_values`: None, `max_size`: Some(65585), added: 68060, mode: `MaxEncodedLen`)
|
||||
fn on_idle_good_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65696`
|
||||
// Estimated: `69161`
|
||||
// Minimum execution time: 124_651_000 picoseconds.
|
||||
Weight::from_parts(127_857_000, 0)
|
||||
.saturating_add(Weight::from_parts(0, 69161))
|
||||
.saturating_add(RocksDbWeight::get().reads(5))
|
||||
.saturating_add(RocksDbWeight::get().writes(5))
|
||||
}
|
||||
/// Storage: `DmpQueue::MigrationStatus` (r:1 w:1)
|
||||
/// Proof: `DmpQueue::MigrationStatus` (`max_values`: Some(1), `max_size`: Some(1028), added: 1523, mode: `MaxEncodedLen`)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca7d95d3e948effbeccff2de2c182672836` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca7d95d3e948effbeccff2de2c182672836` (r:1 w:1)
|
||||
fn on_idle_large_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65659`
|
||||
// Estimated: `69124`
|
||||
// Minimum execution time: 65_684_000 picoseconds.
|
||||
Weight::from_parts(68_039_000, 0)
|
||||
.saturating_add(Weight::from_parts(0, 69124))
|
||||
.saturating_add(RocksDbWeight::get().reads(3))
|
||||
.saturating_add(RocksDbWeight::get().writes(2))
|
||||
}
|
||||
/// Storage: `DmpQueue::MigrationStatus` (r:1 w:1)
|
||||
/// Proof: `DmpQueue::MigrationStatus` (`max_values`: Some(1), `max_size`: Some(1028), added: 1523, mode: `MaxEncodedLen`)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca70f923ef3252d0166429d36d20ed665a8` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca70f923ef3252d0166429d36d20ed665a8` (r:1 w:1)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca772275f64c354954352b71eea39cfaca2` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca772275f64c354954352b71eea39cfaca2` (r:1 w:1)
|
||||
/// Storage: `MessageQueue::BookStateFor` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::BookStateFor` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::ServiceHead` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::ServiceHead` (`max_values`: Some(1), `max_size`: Some(5), added: 500, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::Pages` (r:0 w:1)
|
||||
/// Proof: `MessageQueue::Pages` (`max_values`: None, `max_size`: Some(65585), added: 68060, mode: `MaxEncodedLen`)
|
||||
fn on_idle_overweight_good_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65726`
|
||||
// Estimated: `69191`
|
||||
// Minimum execution time: 117_657_000 picoseconds.
|
||||
Weight::from_parts(122_035_000, 0)
|
||||
.saturating_add(Weight::from_parts(0, 69191))
|
||||
.saturating_add(RocksDbWeight::get().reads(6))
|
||||
.saturating_add(RocksDbWeight::get().writes(6))
|
||||
}
|
||||
/// Storage: `DmpQueue::MigrationStatus` (r:1 w:1)
|
||||
/// Proof: `DmpQueue::MigrationStatus` (`max_values`: Some(1), `max_size`: Some(1028), added: 1523, mode: `MaxEncodedLen`)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca754904d6d8c6fe06c4e5965f9b8397421` (r:1 w:0)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca70f923ef3252d0166429d36d20ed665a8` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca70f923ef3252d0166429d36d20ed665a8` (r:1 w:1)
|
||||
/// Storage: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca772275f64c354954352b71eea39cfaca2` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0xcd5c1f6df63bc97f4a8ce37f14a50ca772275f64c354954352b71eea39cfaca2` (r:1 w:1)
|
||||
fn on_idle_overweight_large_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65689`
|
||||
// Estimated: `69154`
|
||||
// Minimum execution time: 59_799_000 picoseconds.
|
||||
Weight::from_parts(61_354_000, 0)
|
||||
.saturating_add(Weight::from_parts(0, 69154))
|
||||
.saturating_add(RocksDbWeight::get().reads(4))
|
||||
.saturating_add(RocksDbWeight::get().writes(3))
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,10 @@ trie-db = { version = "0.28.0", default-features = false }
|
||||
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
|
||||
|
||||
# Substrate
|
||||
frame-benchmarking = { path = "../../../substrate/frame/benchmarking", default-features = false, optional = true}
|
||||
frame-support = { path = "../../../substrate/frame/support", default-features = false}
|
||||
frame-system = { path = "../../../substrate/frame/system", default-features = false}
|
||||
pallet-message-queue = { path = "../../../substrate/frame/message-queue", default-features = false }
|
||||
sp-core = { path = "../../../substrate/primitives/core", default-features = false}
|
||||
sp-externalities = { path = "../../../substrate/primitives/externalities", default-features = false}
|
||||
sp-inherents = { path = "../../../substrate/primitives/inherents", default-features = false}
|
||||
@@ -42,6 +44,7 @@ cumulus-primitives-parachain-inherent = { path = "../../primitives/parachain-inh
|
||||
assert_matches = "1.5"
|
||||
hex-literal = "0.4.1"
|
||||
lazy_static = "1.4"
|
||||
rand = "0.8.5"
|
||||
|
||||
# Substrate
|
||||
sc-client-api = { path = "../../../substrate/client/api" }
|
||||
@@ -62,9 +65,11 @@ std = [
|
||||
"cumulus-primitives-core/std",
|
||||
"cumulus-primitives-parachain-inherent/std",
|
||||
"environmental/std",
|
||||
"frame-benchmarking/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"log/std",
|
||||
"pallet-message-queue/std",
|
||||
"polkadot-parachain-primitives/std",
|
||||
"polkadot-runtime-parachains/std",
|
||||
"scale-info/std",
|
||||
@@ -75,14 +80,19 @@ std = [
|
||||
"sp-runtime/std",
|
||||
"sp-state-machine/std",
|
||||
"sp-std/std",
|
||||
"sp-tracing/std",
|
||||
"sp-trie/std",
|
||||
"trie-db/std",
|
||||
"xcm/std",
|
||||
]
|
||||
|
||||
runtime-benchmarks = [
|
||||
"cumulus-primitives-core/runtime-benchmarks",
|
||||
"cumulus-test-client/runtime-benchmarks",
|
||||
"frame-benchmarking/runtime-benchmarks",
|
||||
"frame-support/runtime-benchmarks",
|
||||
"frame-system/runtime-benchmarks",
|
||||
"pallet-message-queue/runtime-benchmarks",
|
||||
"polkadot-parachain-primitives/runtime-benchmarks",
|
||||
"polkadot-runtime-parachains/runtime-benchmarks",
|
||||
"sp-runtime/runtime-benchmarks",
|
||||
@@ -91,6 +101,7 @@ runtime-benchmarks = [
|
||||
try-runtime = [
|
||||
"frame-support/try-runtime",
|
||||
"frame-system/try-runtime",
|
||||
"pallet-message-queue/try-runtime",
|
||||
"polkadot-runtime-parachains/try-runtime",
|
||||
"sp-runtime/try-runtime",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Benchmarking for the parachain-system pallet.
|
||||
|
||||
#![cfg(feature = "runtime-benchmarks")]
|
||||
|
||||
use super::*;
|
||||
use cumulus_primitives_core::relay_chain::Hash as RelayHash;
|
||||
use frame_benchmarking::v2::*;
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
|
||||
#[benchmarks]
|
||||
mod benchmarks {
|
||||
use super::*;
|
||||
|
||||
/// Enqueue `n` messages via `enqueue_inbound_downward_messages`.
|
||||
///
|
||||
/// The limit is set to `1000` for benchmarking purposes as the actual limit is only known at
|
||||
/// runtime. However, the limit (and default) for Dotsama are magnitudes smaller.
|
||||
#[benchmark]
|
||||
fn enqueue_inbound_downward_messages(n: Linear<0, 1000>) {
|
||||
let msg = InboundDownwardMessage {
|
||||
sent_at: n, // The block number does not matter.
|
||||
msg: vec![0u8; MaxDmpMessageLenOf::<T>::get() as usize],
|
||||
};
|
||||
let msgs = vec![msg; n as usize];
|
||||
let head = mqp_head(&msgs);
|
||||
|
||||
#[block]
|
||||
{
|
||||
Pallet::<T>::enqueue_inbound_downward_messages(head, msgs);
|
||||
}
|
||||
|
||||
assert_eq!(ProcessedDownwardMessages::<T>::get(), n);
|
||||
assert_eq!(LastDmqMqcHead::<T>::get().head(), head);
|
||||
}
|
||||
|
||||
/// Re-implements an easy version of the `MessageQueueChain` for testing purposes.
|
||||
fn mqp_head(msgs: &Vec<InboundDownwardMessage>) -> RelayHash {
|
||||
let mut head = Default::default();
|
||||
for msg in msgs.iter() {
|
||||
let msg_hash = BlakeTwo256::hash_of(&msg.msg);
|
||||
head = BlakeTwo256::hash_of(&(head, msg.sent_at, msg_hash));
|
||||
}
|
||||
head
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite! {
|
||||
Pallet,
|
||||
crate::mock::new_test_ext(),
|
||||
crate::mock::Test
|
||||
}
|
||||
}
|
||||
@@ -30,16 +30,17 @@
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use cumulus_primitives_core::{
|
||||
relay_chain, AbridgedHostConfiguration, ChannelInfo, ChannelStatus, CollationInfo,
|
||||
DmpMessageHandler, GetChannelInfo, InboundDownwardMessage, InboundHrmpMessage,
|
||||
MessageSendError, OutboundHrmpMessage, ParaId, PersistedValidationData, UpwardMessage,
|
||||
UpwardMessageSender, XcmpMessageHandler, XcmpMessageSource,
|
||||
GetChannelInfo, InboundDownwardMessage, InboundHrmpMessage, MessageSendError,
|
||||
OutboundHrmpMessage, ParaId, PersistedValidationData, UpwardMessage, UpwardMessageSender,
|
||||
XcmpMessageHandler, XcmpMessageSource,
|
||||
};
|
||||
use cumulus_primitives_parachain_inherent::{MessageQueueChain, ParachainInherentData};
|
||||
use frame_support::{
|
||||
defensive,
|
||||
dispatch::{DispatchResult, Pays, PostDispatchInfo},
|
||||
ensure,
|
||||
inherent::{InherentData, InherentIdentifier, ProvideInherent},
|
||||
traits::Get,
|
||||
traits::{Get, HandleMessage},
|
||||
weights::Weight,
|
||||
};
|
||||
use frame_system::{ensure_none, ensure_root, pallet_prelude::HeaderFor};
|
||||
@@ -52,15 +53,20 @@ use sp_runtime::{
|
||||
InvalidTransaction, TransactionLongevity, TransactionSource, TransactionValidity,
|
||||
ValidTransaction,
|
||||
},
|
||||
DispatchError, FixedU128, RuntimeDebug, Saturating,
|
||||
BoundedSlice, DispatchError, FixedU128, RuntimeDebug, Saturating,
|
||||
};
|
||||
use sp_std::{cmp, collections::btree_map::BTreeMap, prelude::*};
|
||||
use xcm::latest::XcmHash;
|
||||
|
||||
mod benchmarking;
|
||||
pub mod migration;
|
||||
|
||||
mod mock;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
pub mod weights;
|
||||
|
||||
pub use weights::WeightInfo;
|
||||
|
||||
mod unincluded_segment;
|
||||
|
||||
pub mod consensus_hook;
|
||||
@@ -177,6 +183,9 @@ where
|
||||
check_version: bool,
|
||||
}
|
||||
|
||||
/// The max length of a DMP message.
|
||||
pub type MaxDmpMessageLenOf<T> = <<T as Config>::DmpQueue as HandleMessage>::MaxMessageLen;
|
||||
|
||||
pub mod ump_constants {
|
||||
use super::FixedU128;
|
||||
|
||||
@@ -216,17 +225,18 @@ pub mod pallet {
|
||||
/// The place where outbound XCMP messages come from. This is queried in `finalize_block`.
|
||||
type OutboundXcmpMessageSource: XcmpMessageSource;
|
||||
|
||||
/// The message handler that will be invoked when messages are received via DMP.
|
||||
type DmpMessageHandler: DmpMessageHandler;
|
||||
/// Queues inbound downward messages for delayed processing.
|
||||
///
|
||||
/// All inbound DMP messages from the relay are pushed into this. The handler is expected to
|
||||
/// eventually process all the messages that are pushed to it.
|
||||
type DmpQueue: HandleMessage;
|
||||
|
||||
/// The weight we reserve at the beginning of the block for processing DMP messages.
|
||||
type ReservedDmpWeight: Get<Weight>;
|
||||
|
||||
/// The message handler that will be invoked when messages are received via XCMP.
|
||||
///
|
||||
/// The messages are dispatched in the order they were relayed by the relay chain. If
|
||||
/// multiple messages were relayed at one block, these will be dispatched in ascending
|
||||
/// order of the sender's para ID.
|
||||
/// This should normally link to the XCMP Queue pallet.
|
||||
type XcmpMessageHandler: XcmpMessageHandler;
|
||||
|
||||
/// The weight we reserve at the beginning of the block for processing XCMP messages.
|
||||
@@ -235,6 +245,9 @@ pub mod pallet {
|
||||
/// Something that can check the associated relay parent block number.
|
||||
type CheckAssociatedRelayNumber: CheckAssociatedRelayNumber;
|
||||
|
||||
/// Weight info for functions and calls.
|
||||
type WeightInfo: WeightInfo;
|
||||
|
||||
/// An entry-point for higher-level logic to manage the backlog of unincluded parachain
|
||||
/// blocks and authorship rights for those blocks.
|
||||
///
|
||||
@@ -631,15 +644,15 @@ pub mod pallet {
|
||||
|
||||
<T::OnSystemEvent as OnSystemEvent>::on_validation_data(&vfp);
|
||||
|
||||
total_weight += Self::process_inbound_downward_messages(
|
||||
total_weight.saturating_accrue(Self::enqueue_inbound_downward_messages(
|
||||
relevant_messaging_state.dmq_mqc_head,
|
||||
downward_messages,
|
||||
);
|
||||
total_weight += Self::process_inbound_horizontal_messages(
|
||||
));
|
||||
total_weight.saturating_accrue(Self::enqueue_inbound_horizontal_messages(
|
||||
&relevant_messaging_state.ingress_channels,
|
||||
horizontal_messages,
|
||||
vfp.relay_parent_number,
|
||||
);
|
||||
));
|
||||
|
||||
Ok(PostDispatchInfo { actual_weight: Some(total_weight), pays_fee: Pays::No })
|
||||
}
|
||||
@@ -1130,7 +1143,7 @@ impl<T: Config> Pallet<T> {
|
||||
// inherent.
|
||||
}
|
||||
|
||||
/// Process all inbound downward messages relayed by the collator.
|
||||
/// Enqueue all inbound downward messages relayed by the collator into the MQ pallet.
|
||||
///
|
||||
/// Checks if the sequence of the messages is valid, dispatches them and communicates the
|
||||
/// number of processed messages to the collator via a storage update.
|
||||
@@ -1139,26 +1152,33 @@ impl<T: Config> Pallet<T> {
|
||||
///
|
||||
/// If it turns out that after processing all messages the Message Queue Chain
|
||||
/// hash doesn't match the expected.
|
||||
fn process_inbound_downward_messages(
|
||||
fn enqueue_inbound_downward_messages(
|
||||
expected_dmq_mqc_head: relay_chain::Hash,
|
||||
downward_messages: Vec<InboundDownwardMessage>,
|
||||
) -> Weight {
|
||||
let dm_count = downward_messages.len() as u32;
|
||||
let mut dmq_head = <LastDmqMqcHead<T>>::get();
|
||||
|
||||
let mut weight_used = Weight::zero();
|
||||
let weight_used = T::WeightInfo::enqueue_inbound_downward_messages(dm_count);
|
||||
if dm_count != 0 {
|
||||
Self::deposit_event(Event::DownwardMessagesReceived { count: dm_count });
|
||||
let max_weight =
|
||||
<ReservedDmpWeightOverride<T>>::get().unwrap_or_else(T::ReservedDmpWeight::get);
|
||||
|
||||
let message_iter = downward_messages
|
||||
.into_iter()
|
||||
.inspect(|m| {
|
||||
dmq_head.extend_downward(m);
|
||||
})
|
||||
.map(|m| (m.sent_at, m.msg));
|
||||
weight_used += T::DmpMessageHandler::handle_dmp_messages(message_iter, max_weight);
|
||||
// Eagerly update the MQC head hash:
|
||||
for m in &downward_messages {
|
||||
dmq_head.extend_downward(m);
|
||||
}
|
||||
let bounded = downward_messages
|
||||
.iter()
|
||||
// Note: we are not using `.defensive()` here since that prints the whole value to
|
||||
// console. In case that the message is too long, this clogs up the log quite badly.
|
||||
.filter_map(|m| match BoundedSlice::try_from(&m.msg[..]) {
|
||||
Ok(bounded) => Some(bounded),
|
||||
Err(_) => {
|
||||
defensive!("Inbound Downward message was too long; dropping");
|
||||
None
|
||||
},
|
||||
});
|
||||
T::DmpQueue::handle_messages(bounded);
|
||||
<LastDmqMqcHead<T>>::put(&dmq_head);
|
||||
|
||||
Self::deposit_event(Event::DownwardMessagesProcessed {
|
||||
@@ -1181,14 +1201,15 @@ impl<T: Config> Pallet<T> {
|
||||
|
||||
/// Process all inbound horizontal messages relayed by the collator.
|
||||
///
|
||||
/// This is similar to `Pallet::process_inbound_downward_messages`, but works on multiple
|
||||
/// inbound channels.
|
||||
/// This is similar to [`enqueue_inbound_downward_messages`], but works with multiple inbound
|
||||
/// channels. It immediately dispatches signals and queues all other XCMs. Blob messages are
|
||||
/// ignored.
|
||||
///
|
||||
/// **Panics** if either any of horizontal messages submitted by the collator was sent from
|
||||
/// a para which has no open channel to this parachain or if after processing
|
||||
/// messages across all inbound channels MQCs were obtained which do not
|
||||
/// correspond to the ones found on the relay-chain.
|
||||
fn process_inbound_horizontal_messages(
|
||||
fn enqueue_inbound_horizontal_messages(
|
||||
ingress_channels: &[(ParaId, cumulus_primitives_core::AbridgedHrmpChannel)],
|
||||
horizontal_messages: BTreeMap<ParaId, Vec<InboundHrmpMessage>>,
|
||||
relay_parent_number: relay_chain::BlockNumber,
|
||||
|
||||
@@ -0,0 +1,485 @@
|
||||
// Copyright Parity Technologies (UK) Ltd.
|
||||
// This file is part of Cumulus.
|
||||
|
||||
// Cumulus is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Cumulus is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Test setup and helpers.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use super::*;
|
||||
|
||||
use codec::Encode;
|
||||
use cumulus_primitives_core::{
|
||||
relay_chain::BlockNumber as RelayBlockNumber, AggregateMessageOrigin, InboundDownwardMessage,
|
||||
InboundHrmpMessage, PersistedValidationData,
|
||||
};
|
||||
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
|
||||
use frame_support::{
|
||||
derive_impl,
|
||||
inherent::{InherentData, ProvideInherent},
|
||||
parameter_types,
|
||||
traits::{
|
||||
OnFinalize, OnInitialize, ProcessMessage, ProcessMessageError, UnfilteredDispatchable,
|
||||
},
|
||||
weights::{Weight, WeightMeter},
|
||||
};
|
||||
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
|
||||
use sp_runtime::{traits::BlakeTwo256, BuildStorage};
|
||||
use sp_std::{collections::vec_deque::VecDeque, num::NonZeroU32};
|
||||
use sp_version::RuntimeVersion;
|
||||
use std::cell::RefCell;
|
||||
|
||||
use crate as parachain_system;
|
||||
use crate::consensus_hook::UnincludedSegmentCapacity;
|
||||
|
||||
type Block = frame_system::mocking::MockBlock<Test>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub enum Test {
|
||||
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
|
||||
ParachainSystem: parachain_system::{Pallet, Call, Config<T>, Storage, Inherent, Event<T>, ValidateUnsigned},
|
||||
MessageQueue: pallet_message_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();
|
||||
}
|
||||
|
||||
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
|
||||
impl frame_system::Config for Test {
|
||||
type Block = Block;
|
||||
type BlockHashCount = BlockHashCount;
|
||||
type Version = Version;
|
||||
type OnSetCode = ParachainSetCode<Self>;
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const RelayOrigin: AggregateMessageOrigin = AggregateMessageOrigin::Parent;
|
||||
}
|
||||
|
||||
impl Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type OnSystemEvent = ();
|
||||
type SelfParaId = ParachainId;
|
||||
type OutboundXcmpMessageSource = FromThreadLocal;
|
||||
type DmpQueue = frame_support::traits::EnqueueWithOrigin<MessageQueue, RelayOrigin>;
|
||||
type ReservedDmpWeight = ReservedDmpWeight;
|
||||
type XcmpMessageHandler = SaveIntoThreadLocal;
|
||||
type ReservedXcmpWeight = ReservedXcmpWeight;
|
||||
type CheckAssociatedRelayNumber = AnyRelayNumber;
|
||||
type ConsensusHook = TestConsensusHook;
|
||||
type WeightInfo = ();
|
||||
}
|
||||
|
||||
std::thread_local! {
|
||||
pub static CONSENSUS_HOOK: RefCell<Box<dyn Fn(&RelayChainStateProof) -> (Weight, UnincludedSegmentCapacity)>>
|
||||
= RefCell::new(Box::new(|_| (Weight::zero(), NonZeroU32::new(1).unwrap().into())));
|
||||
}
|
||||
|
||||
pub struct TestConsensusHook;
|
||||
|
||||
impl ConsensusHook for TestConsensusHook {
|
||||
fn on_state_proof(s: &RelayChainStateProof) -> (Weight, UnincludedSegmentCapacity) {
|
||||
CONSENSUS_HOOK.with(|f| f.borrow_mut()(s))
|
||||
}
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const MaxWeight: Weight = Weight::MAX;
|
||||
}
|
||||
|
||||
impl pallet_message_queue::Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
// NOTE that normally for benchmarking we should use the No-OP message processor, but in this
|
||||
// case its a mocked runtime and will only be used to generate insecure default weights.
|
||||
type MessageProcessor = SaveIntoThreadLocal;
|
||||
type Size = u32;
|
||||
type QueueChangeHandler = ();
|
||||
type QueuePausedQuery = ();
|
||||
type HeapSize = sp_core::ConstU32<{ 64 * 1024 }>;
|
||||
type MaxStale = sp_core::ConstU32<8>;
|
||||
type ServiceWeight = MaxWeight;
|
||||
type WeightInfo = ();
|
||||
}
|
||||
|
||||
/// A `XcmpMessageSource` that takes messages from thread-local.
|
||||
pub struct FromThreadLocal;
|
||||
|
||||
/// A `MessageProcessor` that stores all messages in thread-local.
|
||||
pub struct SaveIntoThreadLocal;
|
||||
|
||||
std::thread_local! {
|
||||
pub static HANDLED_DMP_MESSAGES: RefCell<Vec<Vec<u8>>> = RefCell::new(Vec::new());
|
||||
pub static HANDLED_XCMP_MESSAGES: RefCell<Vec<(ParaId, relay_chain::BlockNumber, Vec<u8>)>> = RefCell::new(Vec::new());
|
||||
pub static SENT_MESSAGES: RefCell<Vec<(ParaId, Vec<u8>)>> = RefCell::new(Vec::new());
|
||||
}
|
||||
|
||||
pub fn send_message(dest: ParaId, message: Vec<u8>) {
|
||||
SENT_MESSAGES.with(|m| m.borrow_mut().push((dest, message)));
|
||||
}
|
||||
|
||||
impl XcmpMessageSource for FromThreadLocal {
|
||||
fn take_outbound_messages(maximum_channels: usize) -> Vec<(ParaId, Vec<u8>)> {
|
||||
let mut ids = std::collections::BTreeSet::<ParaId>::new();
|
||||
let mut taken_messages = 0;
|
||||
let mut taken_bytes = 0;
|
||||
let mut result = Vec::new();
|
||||
SENT_MESSAGES.with(|ms| {
|
||||
ms.borrow_mut().retain(|m| {
|
||||
let status = <Pallet<Test> as GetChannelInfo>::get_channel_status(m.0);
|
||||
let (max_size_now, max_size_ever) = match status {
|
||||
ChannelStatus::Ready(now, ever) => (now, ever),
|
||||
ChannelStatus::Closed => return false, // drop message
|
||||
ChannelStatus::Full => return true, // keep message queued.
|
||||
};
|
||||
|
||||
let msg_len = m.1.len();
|
||||
|
||||
if !ids.contains(&m.0) &&
|
||||
taken_messages < maximum_channels &&
|
||||
msg_len <= max_size_ever &&
|
||||
taken_bytes + msg_len <= max_size_now
|
||||
{
|
||||
ids.insert(m.0);
|
||||
taken_messages += 1;
|
||||
taken_bytes += msg_len;
|
||||
result.push(m.clone());
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
})
|
||||
});
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl ProcessMessage for SaveIntoThreadLocal {
|
||||
type Origin = AggregateMessageOrigin;
|
||||
|
||||
fn process_message(
|
||||
message: &[u8],
|
||||
origin: Self::Origin,
|
||||
_meter: &mut WeightMeter,
|
||||
_id: &mut [u8; 32],
|
||||
) -> Result<bool, ProcessMessageError> {
|
||||
assert_eq!(origin, Self::Origin::Parent);
|
||||
|
||||
HANDLED_DMP_MESSAGES.with(|m| {
|
||||
m.borrow_mut().push(message.to_vec());
|
||||
Weight::zero()
|
||||
});
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
impl XcmpMessageHandler for SaveIntoThreadLocal {
|
||||
fn handle_xcmp_messages<'a, I: Iterator<Item = (ParaId, RelayBlockNumber, &'a [u8])>>(
|
||||
iter: I,
|
||||
_max_weight: Weight,
|
||||
) -> Weight {
|
||||
HANDLED_XCMP_MESSAGES.with(|m| {
|
||||
for (sender, sent_at, message) in iter {
|
||||
m.borrow_mut().push((sender, sent_at, message.to_vec()));
|
||||
}
|
||||
Weight::zero()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// This function basically just builds a genesis storage key/value store according to
|
||||
// our desired mockup.
|
||||
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||
HANDLED_DMP_MESSAGES.with(|m| m.borrow_mut().clear());
|
||||
HANDLED_XCMP_MESSAGES.with(|m| m.borrow_mut().clear());
|
||||
|
||||
frame_system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn mk_dmp(sent_at: u32) -> InboundDownwardMessage {
|
||||
InboundDownwardMessage { sent_at, msg: format!("down{}", sent_at).into_bytes() }
|
||||
}
|
||||
|
||||
pub fn mk_hrmp(sent_at: u32) -> InboundHrmpMessage {
|
||||
InboundHrmpMessage { sent_at, data: format!("{}", sent_at).into_bytes() }
|
||||
}
|
||||
|
||||
pub struct ReadRuntimeVersion(pub Vec<u8>);
|
||||
|
||||
impl sp_core::traits::ReadRuntimeVersion for ReadRuntimeVersion {
|
||||
fn read_runtime_version(
|
||||
&self,
|
||||
_wasm_code: &[u8],
|
||||
_ext: &mut dyn sp_externalities::Externalities,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
Ok(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wasm_ext() -> sp_io::TestExternalities {
|
||||
let version = RuntimeVersion {
|
||||
spec_name: "test".into(),
|
||||
spec_version: 2,
|
||||
impl_version: 1,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let mut ext = new_test_ext();
|
||||
ext.register_extension(sp_core::traits::ReadRuntimeVersionExt::new(ReadRuntimeVersion(
|
||||
version.encode(),
|
||||
)));
|
||||
ext
|
||||
}
|
||||
|
||||
pub struct BlockTest {
|
||||
n: BlockNumberFor<Test>,
|
||||
within_block: Box<dyn Fn()>,
|
||||
after_block: Option<Box<dyn Fn()>>,
|
||||
}
|
||||
|
||||
/// BlockTests exist to test blocks with some setup: we have to assume that
|
||||
/// `validate_block` will mutate and check storage in certain predictable
|
||||
/// ways, for example, and we want to always ensure that tests are executed
|
||||
/// in the context of some particular block number.
|
||||
#[derive(Default)]
|
||||
pub struct BlockTests {
|
||||
tests: Vec<BlockTest>,
|
||||
without_externalities: bool,
|
||||
pending_upgrade: Option<RelayChainBlockNumber>,
|
||||
ran: bool,
|
||||
relay_sproof_builder_hook:
|
||||
Option<Box<dyn Fn(&BlockTests, RelayChainBlockNumber, &mut RelayStateSproofBuilder)>>,
|
||||
inherent_data_hook:
|
||||
Option<Box<dyn Fn(&BlockTests, RelayChainBlockNumber, &mut ParachainInherentData)>>,
|
||||
inclusion_delay: Option<usize>,
|
||||
relay_block_number: Option<Box<dyn Fn(&BlockNumberFor<Test>) -> RelayChainBlockNumber>>,
|
||||
|
||||
included_para_head: Option<relay_chain::HeadData>,
|
||||
pending_blocks: VecDeque<relay_chain::HeadData>,
|
||||
}
|
||||
|
||||
impl BlockTests {
|
||||
pub fn new() -> BlockTests {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
pub fn new_without_externalities() -> BlockTests {
|
||||
let mut tests = BlockTests::new();
|
||||
tests.without_externalities = true;
|
||||
tests
|
||||
}
|
||||
|
||||
pub fn add_raw(mut self, test: BlockTest) -> Self {
|
||||
self.tests.push(test);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add<F>(self, n: BlockNumberFor<Test>, within_block: F) -> Self
|
||||
where
|
||||
F: 'static + Fn(),
|
||||
{
|
||||
self.add_raw(BlockTest { n, within_block: Box::new(within_block), after_block: None })
|
||||
}
|
||||
|
||||
pub fn add_with_post_test<F1, F2>(
|
||||
self,
|
||||
n: BlockNumberFor<Test>,
|
||||
within_block: F1,
|
||||
after_block: F2,
|
||||
) -> Self
|
||||
where
|
||||
F1: 'static + Fn(),
|
||||
F2: 'static + Fn(),
|
||||
{
|
||||
self.add_raw(BlockTest {
|
||||
n,
|
||||
within_block: Box::new(within_block),
|
||||
after_block: Some(Box::new(after_block)),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn with_relay_sproof_builder<F>(mut self, f: F) -> Self
|
||||
where
|
||||
F: 'static + Fn(&BlockTests, RelayChainBlockNumber, &mut RelayStateSproofBuilder),
|
||||
{
|
||||
self.relay_sproof_builder_hook = Some(Box::new(f));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_relay_block_number<F>(mut self, f: F) -> Self
|
||||
where
|
||||
F: 'static + Fn(&BlockNumberFor<Test>) -> RelayChainBlockNumber,
|
||||
{
|
||||
self.relay_block_number = Some(Box::new(f));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_inherent_data<F>(mut self, f: F) -> Self
|
||||
where
|
||||
F: 'static + Fn(&BlockTests, RelayChainBlockNumber, &mut ParachainInherentData),
|
||||
{
|
||||
self.inherent_data_hook = Some(Box::new(f));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_inclusion_delay(mut self, inclusion_delay: usize) -> Self {
|
||||
self.inclusion_delay.replace(inclusion_delay);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn run(&mut self) {
|
||||
wasm_ext().execute_with(|| {
|
||||
self.run_without_ext();
|
||||
});
|
||||
}
|
||||
|
||||
pub fn run_without_ext(&mut self) {
|
||||
self.ran = true;
|
||||
|
||||
let mut parent_head_data = {
|
||||
let header = HeaderFor::<Test>::new_from_number(0);
|
||||
relay_chain::HeadData(header.encode())
|
||||
};
|
||||
|
||||
self.included_para_head = Some(parent_head_data.clone());
|
||||
|
||||
for BlockTest { n, within_block, after_block } in self.tests.iter() {
|
||||
let relay_parent_number = self
|
||||
.relay_block_number
|
||||
.as_ref()
|
||||
.map(|f| f(n))
|
||||
.unwrap_or(*n as RelayChainBlockNumber);
|
||||
// clear pending updates, as applicable
|
||||
if let Some(upgrade_block) = self.pending_upgrade {
|
||||
if n >= &upgrade_block.into() {
|
||||
self.pending_upgrade = None;
|
||||
}
|
||||
}
|
||||
|
||||
// begin initialization
|
||||
let parent_hash = BlakeTwo256::hash(&parent_head_data.0);
|
||||
System::reset_events();
|
||||
System::initialize(n, &parent_hash, &Default::default());
|
||||
|
||||
// now mess with the storage the way validate_block does
|
||||
let mut sproof_builder = RelayStateSproofBuilder::default();
|
||||
sproof_builder.included_para_head = self
|
||||
.included_para_head
|
||||
.clone()
|
||||
.unwrap_or_else(|| parent_head_data.clone())
|
||||
.into();
|
||||
if let Some(ref hook) = self.relay_sproof_builder_hook {
|
||||
hook(self, relay_parent_number, &mut sproof_builder);
|
||||
}
|
||||
let (relay_parent_storage_root, relay_chain_state) =
|
||||
sproof_builder.into_state_root_and_proof();
|
||||
let vfp = PersistedValidationData {
|
||||
relay_parent_number,
|
||||
relay_parent_storage_root,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
<ValidationData<Test>>::put(&vfp);
|
||||
NewValidationCode::<Test>::kill();
|
||||
|
||||
// It is insufficient to push the validation function params
|
||||
// to storage; they must also be included in the inherent data.
|
||||
let inherent_data = {
|
||||
let mut inherent_data = InherentData::default();
|
||||
let mut system_inherent_data = ParachainInherentData {
|
||||
validation_data: vfp.clone(),
|
||||
relay_chain_state,
|
||||
downward_messages: Default::default(),
|
||||
horizontal_messages: Default::default(),
|
||||
};
|
||||
if let Some(ref hook) = self.inherent_data_hook {
|
||||
hook(self, relay_parent_number, &mut system_inherent_data);
|
||||
}
|
||||
inherent_data
|
||||
.put_data(
|
||||
cumulus_primitives_parachain_inherent::INHERENT_IDENTIFIER,
|
||||
&system_inherent_data,
|
||||
)
|
||||
.expect("failed to put VFP inherent");
|
||||
inherent_data
|
||||
};
|
||||
|
||||
// execute the block
|
||||
ParachainSystem::on_initialize(*n);
|
||||
ParachainSystem::create_inherent(&inherent_data)
|
||||
.expect("got an inherent")
|
||||
.dispatch_bypass_filter(RawOrigin::None.into())
|
||||
.expect("dispatch succeeded");
|
||||
MessageQueue::on_initialize(*n);
|
||||
within_block();
|
||||
MessageQueue::on_finalize(*n);
|
||||
ParachainSystem::on_finalize(*n);
|
||||
|
||||
// did block execution set new validation code?
|
||||
if NewValidationCode::<Test>::exists() && self.pending_upgrade.is_some() {
|
||||
panic!("attempted to set validation code while upgrade was pending");
|
||||
}
|
||||
|
||||
// clean up
|
||||
let header = System::finalize();
|
||||
let head_data = relay_chain::HeadData(header.encode());
|
||||
parent_head_data = head_data.clone();
|
||||
match self.inclusion_delay {
|
||||
Some(delay) if delay > 0 => {
|
||||
self.pending_blocks.push_back(head_data);
|
||||
if self.pending_blocks.len() > delay {
|
||||
let included = self.pending_blocks.pop_front().unwrap();
|
||||
|
||||
self.included_para_head.replace(included);
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
self.included_para_head.replace(head_data);
|
||||
},
|
||||
}
|
||||
|
||||
if let Some(after_block) = after_block {
|
||||
after_block();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BlockTests {
|
||||
fn drop(&mut self) {
|
||||
if !self.ran {
|
||||
if self.without_externalities {
|
||||
self.run_without_ext();
|
||||
} else {
|
||||
self.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,433 +13,20 @@
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use super::*;
|
||||
use crate::mock::*;
|
||||
|
||||
use codec::Encode;
|
||||
use cumulus_primitives_core::{
|
||||
relay_chain::BlockNumber as RelayBlockNumber, AbridgedHrmpChannel, InboundDownwardMessage,
|
||||
InboundHrmpMessage, PersistedValidationData,
|
||||
};
|
||||
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
|
||||
use frame_support::{
|
||||
assert_ok,
|
||||
inherent::{InherentData, ProvideInherent},
|
||||
parameter_types,
|
||||
traits::{OnFinalize, OnInitialize, UnfilteredDispatchable},
|
||||
weights::Weight,
|
||||
};
|
||||
use frame_system::{
|
||||
pallet_prelude::{BlockNumberFor, HeaderFor},
|
||||
RawOrigin,
|
||||
};
|
||||
use cumulus_primitives_core::{AbridgedHrmpChannel, InboundDownwardMessage, InboundHrmpMessage};
|
||||
use frame_support::{assert_ok, parameter_types, weights::Weight};
|
||||
use frame_system::RawOrigin;
|
||||
use hex_literal::hex;
|
||||
use rand::Rng;
|
||||
use relay_chain::HrmpChannelId;
|
||||
use sp_core::{blake2_256, H256};
|
||||
use sp_runtime::{
|
||||
traits::{BlakeTwo256, IdentityLookup},
|
||||
BuildStorage, DispatchErrorWithPostInfo,
|
||||
};
|
||||
use sp_std::{collections::vec_deque::VecDeque, num::NonZeroU32};
|
||||
use sp_version::RuntimeVersion;
|
||||
use std::cell::RefCell;
|
||||
|
||||
use crate as parachain_system;
|
||||
use crate::consensus_hook::UnincludedSegmentCapacity;
|
||||
|
||||
type Block = frame_system::mocking::MockBlock<Test>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub enum Test
|
||||
{
|
||||
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
|
||||
ParachainSystem: parachain_system::{Pallet, Call, Config<T>, Storage, Inherent, Event<T>, ValidateUnsigned},
|
||||
}
|
||||
);
|
||||
|
||||
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();
|
||||
}
|
||||
impl frame_system::Config for Test {
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type Nonce = u64;
|
||||
type Hash = H256;
|
||||
type Hashing = BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Block = Block;
|
||||
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 = ParachainSetCode<Self>;
|
||||
type MaxConsumers = frame_support::traits::ConstU32<16>;
|
||||
}
|
||||
impl Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type OnSystemEvent = ();
|
||||
type SelfParaId = ParachainId;
|
||||
type OutboundXcmpMessageSource = FromThreadLocal;
|
||||
type DmpMessageHandler = SaveIntoThreadLocal;
|
||||
type ReservedDmpWeight = ReservedDmpWeight;
|
||||
type XcmpMessageHandler = SaveIntoThreadLocal;
|
||||
type ReservedXcmpWeight = ReservedXcmpWeight;
|
||||
type CheckAssociatedRelayNumber = AnyRelayNumber;
|
||||
type ConsensusHook = TestConsensusHook;
|
||||
}
|
||||
|
||||
pub struct FromThreadLocal;
|
||||
pub struct SaveIntoThreadLocal;
|
||||
|
||||
std::thread_local! {
|
||||
static HANDLED_DMP_MESSAGES: RefCell<Vec<(relay_chain::BlockNumber, Vec<u8>)>> = RefCell::new(Vec::new());
|
||||
static HANDLED_XCMP_MESSAGES: RefCell<Vec<(ParaId, relay_chain::BlockNumber, Vec<u8>)>> = RefCell::new(Vec::new());
|
||||
static SENT_MESSAGES: RefCell<Vec<(ParaId, Vec<u8>)>> = RefCell::new(Vec::new());
|
||||
static CONSENSUS_HOOK: RefCell<Box<dyn Fn(&RelayChainStateProof) -> (Weight, UnincludedSegmentCapacity)>>
|
||||
= RefCell::new(Box::new(|_| (Weight::zero(), NonZeroU32::new(1).unwrap().into())));
|
||||
}
|
||||
|
||||
pub struct TestConsensusHook;
|
||||
|
||||
impl ConsensusHook for TestConsensusHook {
|
||||
fn on_state_proof(s: &RelayChainStateProof) -> (Weight, UnincludedSegmentCapacity) {
|
||||
CONSENSUS_HOOK.with(|f| f.borrow_mut()(s))
|
||||
}
|
||||
}
|
||||
|
||||
fn send_message(dest: ParaId, message: Vec<u8>) {
|
||||
SENT_MESSAGES.with(|m| m.borrow_mut().push((dest, message)));
|
||||
}
|
||||
|
||||
impl XcmpMessageSource for FromThreadLocal {
|
||||
fn take_outbound_messages(maximum_channels: usize) -> Vec<(ParaId, Vec<u8>)> {
|
||||
let mut ids = std::collections::BTreeSet::<ParaId>::new();
|
||||
let mut taken_messages = 0;
|
||||
let mut taken_bytes = 0;
|
||||
let mut result = Vec::new();
|
||||
SENT_MESSAGES.with(|ms| {
|
||||
ms.borrow_mut().retain(|m| {
|
||||
let status = <Pallet<Test> as GetChannelInfo>::get_channel_status(m.0);
|
||||
let (max_size_now, max_size_ever) = match status {
|
||||
ChannelStatus::Ready(now, ever) => (now, ever),
|
||||
ChannelStatus::Closed => return false, // drop message
|
||||
ChannelStatus::Full => return true, // keep message queued.
|
||||
};
|
||||
|
||||
let msg_len = m.1.len();
|
||||
|
||||
if !ids.contains(&m.0) &&
|
||||
taken_messages < maximum_channels &&
|
||||
msg_len <= max_size_ever &&
|
||||
taken_bytes + msg_len <= max_size_now
|
||||
{
|
||||
ids.insert(m.0);
|
||||
taken_messages += 1;
|
||||
taken_bytes += msg_len;
|
||||
result.push(m.clone());
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
})
|
||||
});
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl DmpMessageHandler for SaveIntoThreadLocal {
|
||||
fn handle_dmp_messages(
|
||||
iter: impl Iterator<Item = (RelayBlockNumber, Vec<u8>)>,
|
||||
_max_weight: Weight,
|
||||
) -> Weight {
|
||||
HANDLED_DMP_MESSAGES.with(|m| {
|
||||
for i in iter {
|
||||
m.borrow_mut().push(i);
|
||||
}
|
||||
Weight::zero()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl XcmpMessageHandler for SaveIntoThreadLocal {
|
||||
fn handle_xcmp_messages<'a, I: Iterator<Item = (ParaId, RelayBlockNumber, &'a [u8])>>(
|
||||
iter: I,
|
||||
_max_weight: Weight,
|
||||
) -> Weight {
|
||||
HANDLED_XCMP_MESSAGES.with(|m| {
|
||||
for (sender, sent_at, message) in iter {
|
||||
m.borrow_mut().push((sender, sent_at, message.to_vec()));
|
||||
}
|
||||
Weight::zero()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// This function basically just builds a genesis storage key/value store according to
|
||||
// our desired mockup.
|
||||
fn new_test_ext() -> sp_io::TestExternalities {
|
||||
HANDLED_DMP_MESSAGES.with(|m| m.borrow_mut().clear());
|
||||
HANDLED_XCMP_MESSAGES.with(|m| m.borrow_mut().clear());
|
||||
|
||||
frame_system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
|
||||
}
|
||||
|
||||
struct ReadRuntimeVersion(Vec<u8>);
|
||||
|
||||
impl sp_core::traits::ReadRuntimeVersion for ReadRuntimeVersion {
|
||||
fn read_runtime_version(
|
||||
&self,
|
||||
_wasm_code: &[u8],
|
||||
_ext: &mut dyn sp_externalities::Externalities,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
Ok(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn wasm_ext() -> sp_io::TestExternalities {
|
||||
let version = RuntimeVersion {
|
||||
spec_name: "test".into(),
|
||||
spec_version: 2,
|
||||
impl_version: 1,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let mut ext = new_test_ext();
|
||||
ext.register_extension(sp_core::traits::ReadRuntimeVersionExt::new(ReadRuntimeVersion(
|
||||
version.encode(),
|
||||
)));
|
||||
ext
|
||||
}
|
||||
|
||||
struct BlockTest {
|
||||
n: BlockNumberFor<Test>,
|
||||
within_block: Box<dyn Fn()>,
|
||||
after_block: Option<Box<dyn Fn()>>,
|
||||
}
|
||||
|
||||
/// BlockTests exist to test blocks with some setup: we have to assume that
|
||||
/// `validate_block` will mutate and check storage in certain predictable
|
||||
/// ways, for example, and we want to always ensure that tests are executed
|
||||
/// in the context of some particular block number.
|
||||
#[derive(Default)]
|
||||
struct BlockTests {
|
||||
tests: Vec<BlockTest>,
|
||||
pending_upgrade: Option<RelayChainBlockNumber>,
|
||||
ran: bool,
|
||||
relay_sproof_builder_hook:
|
||||
Option<Box<dyn Fn(&BlockTests, RelayChainBlockNumber, &mut RelayStateSproofBuilder)>>,
|
||||
inherent_data_hook:
|
||||
Option<Box<dyn Fn(&BlockTests, RelayChainBlockNumber, &mut ParachainInherentData)>>,
|
||||
inclusion_delay: Option<usize>,
|
||||
relay_block_number: Option<Box<dyn Fn(&BlockNumberFor<Test>) -> RelayChainBlockNumber>>,
|
||||
|
||||
included_para_head: Option<relay_chain::HeadData>,
|
||||
pending_blocks: VecDeque<relay_chain::HeadData>,
|
||||
}
|
||||
|
||||
impl BlockTests {
|
||||
fn new() -> BlockTests {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
fn add_raw(mut self, test: BlockTest) -> Self {
|
||||
self.tests.push(test);
|
||||
self
|
||||
}
|
||||
|
||||
fn add<F>(self, n: BlockNumberFor<Test>, within_block: F) -> Self
|
||||
where
|
||||
F: 'static + Fn(),
|
||||
{
|
||||
self.add_raw(BlockTest { n, within_block: Box::new(within_block), after_block: None })
|
||||
}
|
||||
|
||||
fn add_with_post_test<F1, F2>(
|
||||
self,
|
||||
n: BlockNumberFor<Test>,
|
||||
within_block: F1,
|
||||
after_block: F2,
|
||||
) -> Self
|
||||
where
|
||||
F1: 'static + Fn(),
|
||||
F2: 'static + Fn(),
|
||||
{
|
||||
self.add_raw(BlockTest {
|
||||
n,
|
||||
within_block: Box::new(within_block),
|
||||
after_block: Some(Box::new(after_block)),
|
||||
})
|
||||
}
|
||||
|
||||
fn with_relay_sproof_builder<F>(mut self, f: F) -> Self
|
||||
where
|
||||
F: 'static + Fn(&BlockTests, RelayChainBlockNumber, &mut RelayStateSproofBuilder),
|
||||
{
|
||||
self.relay_sproof_builder_hook = Some(Box::new(f));
|
||||
self
|
||||
}
|
||||
|
||||
fn with_relay_block_number<F>(mut self, f: F) -> Self
|
||||
where
|
||||
F: 'static + Fn(&BlockNumberFor<Test>) -> RelayChainBlockNumber,
|
||||
{
|
||||
self.relay_block_number = Some(Box::new(f));
|
||||
self
|
||||
}
|
||||
|
||||
fn with_inherent_data<F>(mut self, f: F) -> Self
|
||||
where
|
||||
F: 'static + Fn(&BlockTests, RelayChainBlockNumber, &mut ParachainInherentData),
|
||||
{
|
||||
self.inherent_data_hook = Some(Box::new(f));
|
||||
self
|
||||
}
|
||||
|
||||
fn with_inclusion_delay(mut self, inclusion_delay: usize) -> Self {
|
||||
self.inclusion_delay.replace(inclusion_delay);
|
||||
self
|
||||
}
|
||||
|
||||
fn run(&mut self) {
|
||||
self.ran = true;
|
||||
wasm_ext().execute_with(|| {
|
||||
let mut parent_head_data = {
|
||||
let header = HeaderFor::<Test>::new_from_number(0);
|
||||
relay_chain::HeadData(header.encode())
|
||||
};
|
||||
|
||||
self.included_para_head = Some(parent_head_data.clone());
|
||||
|
||||
for BlockTest { n, within_block, after_block } in self.tests.iter() {
|
||||
let relay_parent_number = self
|
||||
.relay_block_number
|
||||
.as_ref()
|
||||
.map(|f| f(n))
|
||||
.unwrap_or(*n as RelayChainBlockNumber);
|
||||
// clear pending updates, as applicable
|
||||
if let Some(upgrade_block) = self.pending_upgrade {
|
||||
if n >= &upgrade_block.into() {
|
||||
self.pending_upgrade = None;
|
||||
}
|
||||
}
|
||||
|
||||
// begin initialization
|
||||
let parent_hash = BlakeTwo256::hash(&parent_head_data.0);
|
||||
System::reset_events();
|
||||
System::initialize(n, &parent_hash, &Default::default());
|
||||
|
||||
// now mess with the storage the way validate_block does
|
||||
let mut sproof_builder = RelayStateSproofBuilder::default();
|
||||
sproof_builder.included_para_head = self
|
||||
.included_para_head
|
||||
.clone()
|
||||
.unwrap_or_else(|| parent_head_data.clone())
|
||||
.into();
|
||||
if let Some(ref hook) = self.relay_sproof_builder_hook {
|
||||
hook(self, relay_parent_number, &mut sproof_builder);
|
||||
}
|
||||
let (relay_parent_storage_root, relay_chain_state) =
|
||||
sproof_builder.into_state_root_and_proof();
|
||||
let vfp = PersistedValidationData {
|
||||
relay_parent_number,
|
||||
relay_parent_storage_root,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
<ValidationData<Test>>::put(&vfp);
|
||||
NewValidationCode::<Test>::kill();
|
||||
|
||||
// It is insufficient to push the validation function params
|
||||
// to storage; they must also be included in the inherent data.
|
||||
let inherent_data = {
|
||||
let mut inherent_data = InherentData::default();
|
||||
let mut system_inherent_data = ParachainInherentData {
|
||||
validation_data: vfp.clone(),
|
||||
relay_chain_state,
|
||||
downward_messages: Default::default(),
|
||||
horizontal_messages: Default::default(),
|
||||
};
|
||||
if let Some(ref hook) = self.inherent_data_hook {
|
||||
hook(self, relay_parent_number, &mut system_inherent_data);
|
||||
}
|
||||
inherent_data
|
||||
.put_data(
|
||||
cumulus_primitives_parachain_inherent::INHERENT_IDENTIFIER,
|
||||
&system_inherent_data,
|
||||
)
|
||||
.expect("failed to put VFP inherent");
|
||||
inherent_data
|
||||
};
|
||||
|
||||
// execute the block
|
||||
ParachainSystem::on_initialize(*n);
|
||||
ParachainSystem::create_inherent(&inherent_data)
|
||||
.expect("got an inherent")
|
||||
.dispatch_bypass_filter(RawOrigin::None.into())
|
||||
.expect("dispatch succeeded");
|
||||
within_block();
|
||||
ParachainSystem::on_finalize(*n);
|
||||
|
||||
// did block execution set new validation code?
|
||||
if NewValidationCode::<Test>::exists() && self.pending_upgrade.is_some() {
|
||||
panic!("attempted to set validation code while upgrade was pending");
|
||||
}
|
||||
|
||||
// clean up
|
||||
let header = System::finalize();
|
||||
let head_data = relay_chain::HeadData(header.encode());
|
||||
parent_head_data = head_data.clone();
|
||||
match self.inclusion_delay {
|
||||
Some(delay) if delay > 0 => {
|
||||
self.pending_blocks.push_back(head_data);
|
||||
if self.pending_blocks.len() > delay {
|
||||
let included = self.pending_blocks.pop_front().unwrap();
|
||||
|
||||
self.included_para_head.replace(included);
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
self.included_para_head.replace(head_data);
|
||||
},
|
||||
}
|
||||
|
||||
if let Some(after_block) = after_block {
|
||||
after_block();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BlockTests {
|
||||
fn drop(&mut self) {
|
||||
if !self.ran {
|
||||
self.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
use sp_core::H256;
|
||||
use sp_std::num::NonZeroU32;
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
@@ -659,30 +246,6 @@ fn inherent_processed_messages_are_ignored() {
|
||||
CONSENSUS_HOOK.with(|c| {
|
||||
*c.borrow_mut() = Box::new(|_| (Weight::zero(), NonZeroU32::new(2).unwrap().into()))
|
||||
});
|
||||
lazy_static::lazy_static! {
|
||||
static ref DMQ_MSG: InboundDownwardMessage = InboundDownwardMessage {
|
||||
sent_at: 3,
|
||||
msg: b"down".to_vec(),
|
||||
};
|
||||
|
||||
static ref XCMP_MSG_1: InboundHrmpMessage = InboundHrmpMessage {
|
||||
sent_at: 2,
|
||||
data: b"h1".to_vec(),
|
||||
};
|
||||
|
||||
static ref XCMP_MSG_2: InboundHrmpMessage = InboundHrmpMessage {
|
||||
sent_at: 3,
|
||||
data: b"h2".to_vec(),
|
||||
};
|
||||
|
||||
static ref EXPECTED_PROCESSED_DMQ: Vec<(RelayChainBlockNumber, Vec<u8>)> = vec![
|
||||
(DMQ_MSG.sent_at, DMQ_MSG.msg.clone())
|
||||
];
|
||||
static ref EXPECTED_PROCESSED_XCMP: Vec<(ParaId, RelayChainBlockNumber, Vec<u8>)> = vec![
|
||||
(ParaId::from(200), XCMP_MSG_1.sent_at, XCMP_MSG_1.data.clone()),
|
||||
(ParaId::from(200), XCMP_MSG_2.sent_at, XCMP_MSG_2.data.clone()),
|
||||
];
|
||||
}
|
||||
|
||||
BlockTests::new()
|
||||
.with_inclusion_delay(1)
|
||||
@@ -690,11 +253,11 @@ fn inherent_processed_messages_are_ignored() {
|
||||
.with_relay_sproof_builder(|_, relay_block_num, sproof| match relay_block_num {
|
||||
3 => {
|
||||
sproof.dmq_mqc_head =
|
||||
Some(MessageQueueChain::default().extend_downward(&DMQ_MSG).head());
|
||||
Some(MessageQueueChain::default().extend_downward(&mk_dmp(3)).head());
|
||||
sproof.upsert_inbound_channel(ParaId::from(200)).mqc_head = Some(
|
||||
MessageQueueChain::default()
|
||||
.extend_hrmp(&XCMP_MSG_1)
|
||||
.extend_hrmp(&XCMP_MSG_2)
|
||||
.extend_hrmp(&mk_hrmp(2))
|
||||
.extend_hrmp(&mk_hrmp(3))
|
||||
.head(),
|
||||
);
|
||||
},
|
||||
@@ -702,9 +265,8 @@ fn inherent_processed_messages_are_ignored() {
|
||||
})
|
||||
.with_inherent_data(|_, relay_block_num, data| match relay_block_num {
|
||||
3 => {
|
||||
data.downward_messages.push(DMQ_MSG.clone());
|
||||
data.horizontal_messages
|
||||
.insert(ParaId::from(200), vec![XCMP_MSG_1.clone(), XCMP_MSG_2.clone()]);
|
||||
data.downward_messages.push(mk_dmp(3));
|
||||
data.horizontal_messages.insert(ParaId::from(200), vec![mk_hrmp(2), mk_hrmp(3)]);
|
||||
},
|
||||
_ => unreachable!(),
|
||||
})
|
||||
@@ -712,22 +274,29 @@ fn inherent_processed_messages_are_ignored() {
|
||||
// Don't drop processed messages for this test.
|
||||
HANDLED_DMP_MESSAGES.with(|m| {
|
||||
let m = m.borrow();
|
||||
assert_eq!(&*m, EXPECTED_PROCESSED_DMQ.as_slice());
|
||||
// NOTE: if this fails, then run the test without benchmark features.
|
||||
assert_eq!(&*m, &[mk_dmp(3).msg]);
|
||||
});
|
||||
HANDLED_XCMP_MESSAGES.with(|m| {
|
||||
let m = m.borrow_mut();
|
||||
assert_eq!(&*m, EXPECTED_PROCESSED_XCMP.as_slice());
|
||||
assert_eq!(
|
||||
&*m,
|
||||
&[(ParaId::from(200), 2, b"2".to_vec()), (ParaId::from(200), 3, b"3".to_vec()),]
|
||||
);
|
||||
});
|
||||
})
|
||||
.add(2, || {})
|
||||
.add(3, || {
|
||||
HANDLED_DMP_MESSAGES.with(|m| {
|
||||
let m = m.borrow();
|
||||
assert_eq!(&*m, EXPECTED_PROCESSED_DMQ.as_slice());
|
||||
assert_eq!(&*m, &[mk_dmp(3).msg]);
|
||||
});
|
||||
HANDLED_XCMP_MESSAGES.with(|m| {
|
||||
let m = m.borrow_mut();
|
||||
assert_eq!(&*m, EXPECTED_PROCESSED_XCMP.as_slice());
|
||||
assert_eq!(
|
||||
&*m,
|
||||
&[(ParaId::from(200), 2, b"2".to_vec()), (ParaId::from(200), 3, b"3".to_vec()),]
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1183,6 +752,7 @@ fn message_queue_chain() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "runtime-benchmarks"))]
|
||||
fn receive_dmp() {
|
||||
lazy_static::lazy_static! {
|
||||
static ref MSG: InboundDownwardMessage = InboundDownwardMessage {
|
||||
@@ -1208,41 +778,31 @@ fn receive_dmp() {
|
||||
.add(1, || {
|
||||
HANDLED_DMP_MESSAGES.with(|m| {
|
||||
let mut m = m.borrow_mut();
|
||||
assert_eq!(&*m, &[(MSG.sent_at, MSG.msg.clone())]);
|
||||
assert_eq!(&*m, &[(MSG.msg.clone())]);
|
||||
m.clear();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "runtime-benchmarks"))]
|
||||
fn receive_dmp_after_pause() {
|
||||
lazy_static::lazy_static! {
|
||||
static ref MSG_1: InboundDownwardMessage = InboundDownwardMessage {
|
||||
sent_at: 1,
|
||||
msg: b"down1".to_vec(),
|
||||
};
|
||||
static ref MSG_2: InboundDownwardMessage = InboundDownwardMessage {
|
||||
sent_at: 3,
|
||||
msg: b"down2".to_vec(),
|
||||
};
|
||||
}
|
||||
|
||||
BlockTests::new()
|
||||
.with_relay_sproof_builder(|_, relay_block_num, sproof| match relay_block_num {
|
||||
1 => {
|
||||
sproof.dmq_mqc_head =
|
||||
Some(MessageQueueChain::default().extend_downward(&MSG_1).head());
|
||||
Some(MessageQueueChain::default().extend_downward(&mk_dmp(1)).head());
|
||||
},
|
||||
2 => {
|
||||
// no new messages, mqc stayed the same.
|
||||
sproof.dmq_mqc_head =
|
||||
Some(MessageQueueChain::default().extend_downward(&MSG_1).head());
|
||||
Some(MessageQueueChain::default().extend_downward(&mk_dmp(1)).head());
|
||||
},
|
||||
3 => {
|
||||
sproof.dmq_mqc_head = Some(
|
||||
MessageQueueChain::default()
|
||||
.extend_downward(&MSG_1)
|
||||
.extend_downward(&MSG_2)
|
||||
.extend_downward(&mk_dmp(1))
|
||||
.extend_downward(&mk_dmp(3))
|
||||
.head(),
|
||||
);
|
||||
},
|
||||
@@ -1250,20 +810,20 @@ fn receive_dmp_after_pause() {
|
||||
})
|
||||
.with_inherent_data(|_, relay_block_num, data| match relay_block_num {
|
||||
1 => {
|
||||
data.downward_messages.push(MSG_1.clone());
|
||||
data.downward_messages.push(mk_dmp(1));
|
||||
},
|
||||
2 => {
|
||||
// no new messages
|
||||
},
|
||||
3 => {
|
||||
data.downward_messages.push(MSG_2.clone());
|
||||
data.downward_messages.push(mk_dmp(3));
|
||||
},
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.add(1, || {
|
||||
HANDLED_DMP_MESSAGES.with(|m| {
|
||||
let mut m = m.borrow_mut();
|
||||
assert_eq!(&*m, &[(MSG_1.sent_at, MSG_1.msg.clone())]);
|
||||
assert_eq!(&*m, &[(mk_dmp(1).msg.clone())]);
|
||||
m.clear();
|
||||
});
|
||||
})
|
||||
@@ -1271,54 +831,88 @@ fn receive_dmp_after_pause() {
|
||||
.add(3, || {
|
||||
HANDLED_DMP_MESSAGES.with(|m| {
|
||||
let mut m = m.borrow_mut();
|
||||
assert_eq!(&*m, &[(MSG_2.sent_at, MSG_2.msg.clone())]);
|
||||
assert_eq!(&*m, &[(mk_dmp(3).msg.clone())]);
|
||||
m.clear();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Sent up to 100 DMP messages per block over a period of 100 blocks.
|
||||
#[test]
|
||||
#[cfg(not(feature = "runtime-benchmarks"))]
|
||||
fn receive_dmp_many() {
|
||||
wasm_ext().execute_with(|| {
|
||||
parameter_types! {
|
||||
pub storage MqcHead: MessageQueueChain = Default::default();
|
||||
pub storage SentInBlock: Vec<Vec<InboundDownwardMessage>> = Default::default();
|
||||
}
|
||||
|
||||
let mut sent_in_block = vec![vec![]];
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
for block in 1..100 {
|
||||
let mut msgs = vec![];
|
||||
for _ in 1..=rng.gen_range(1..=100) {
|
||||
// Just use the same message multiple times per block.
|
||||
msgs.push(mk_dmp(block));
|
||||
}
|
||||
sent_in_block.push(msgs);
|
||||
}
|
||||
SentInBlock::set(&sent_in_block);
|
||||
|
||||
let mut tester = BlockTests::new_without_externalities()
|
||||
.with_relay_sproof_builder(|_, relay_block_num, sproof| {
|
||||
let mut new_hash = MqcHead::get();
|
||||
|
||||
for msg in SentInBlock::get()[relay_block_num as usize].iter() {
|
||||
new_hash.extend_downward(&msg);
|
||||
}
|
||||
|
||||
sproof.dmq_mqc_head = Some(new_hash.head());
|
||||
MqcHead::set(&new_hash);
|
||||
})
|
||||
.with_inherent_data(|_, relay_block_num, data| {
|
||||
for msg in SentInBlock::get()[relay_block_num as usize].iter() {
|
||||
data.downward_messages.push(msg.clone());
|
||||
}
|
||||
});
|
||||
|
||||
for block in 1..100 {
|
||||
tester = tester.add(block, move || {
|
||||
HANDLED_DMP_MESSAGES.with(|m| {
|
||||
let mut m = m.borrow_mut();
|
||||
let msgs = SentInBlock::get()[block as usize]
|
||||
.iter()
|
||||
.map(|m| m.msg.clone())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(&*m, &msgs);
|
||||
m.clear();
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn receive_hrmp() {
|
||||
lazy_static::lazy_static! {
|
||||
static ref MSG_1: InboundHrmpMessage = InboundHrmpMessage {
|
||||
sent_at: 1,
|
||||
data: b"1".to_vec(),
|
||||
};
|
||||
|
||||
static ref MSG_2: InboundHrmpMessage = InboundHrmpMessage {
|
||||
sent_at: 2,
|
||||
data: b"2".to_vec(),
|
||||
};
|
||||
|
||||
static ref MSG_3: InboundHrmpMessage = InboundHrmpMessage {
|
||||
sent_at: 2,
|
||||
data: b"3".to_vec(),
|
||||
};
|
||||
|
||||
static ref MSG_4: InboundHrmpMessage = InboundHrmpMessage {
|
||||
sent_at: 2,
|
||||
data: b"4".to_vec(),
|
||||
};
|
||||
}
|
||||
|
||||
BlockTests::new()
|
||||
.with_relay_sproof_builder(|_, relay_block_num, sproof| match relay_block_num {
|
||||
1 => {
|
||||
// 200 - doesn't exist yet
|
||||
// 300 - one new message
|
||||
sproof.upsert_inbound_channel(ParaId::from(300)).mqc_head =
|
||||
Some(MessageQueueChain::default().extend_hrmp(&MSG_1).head());
|
||||
Some(MessageQueueChain::default().extend_hrmp(&mk_hrmp(1)).head());
|
||||
},
|
||||
2 => {
|
||||
// 200 - now present with one message
|
||||
// 300 - two new messages
|
||||
sproof.upsert_inbound_channel(ParaId::from(200)).mqc_head =
|
||||
Some(MessageQueueChain::default().extend_hrmp(&MSG_4).head());
|
||||
Some(MessageQueueChain::default().extend_hrmp(&mk_hrmp(4)).head());
|
||||
sproof.upsert_inbound_channel(ParaId::from(300)).mqc_head = Some(
|
||||
MessageQueueChain::default()
|
||||
.extend_hrmp(&MSG_1)
|
||||
.extend_hrmp(&MSG_2)
|
||||
.extend_hrmp(&MSG_3)
|
||||
.extend_hrmp(&mk_hrmp(1))
|
||||
.extend_hrmp(&mk_hrmp(2))
|
||||
.extend_hrmp(&mk_hrmp(3))
|
||||
.head(),
|
||||
);
|
||||
},
|
||||
@@ -1326,13 +920,13 @@ fn receive_hrmp() {
|
||||
// 200 - no new messages
|
||||
// 300 - is gone
|
||||
sproof.upsert_inbound_channel(ParaId::from(200)).mqc_head =
|
||||
Some(MessageQueueChain::default().extend_hrmp(&MSG_4).head());
|
||||
Some(MessageQueueChain::default().extend_hrmp(&mk_hrmp(4)).head());
|
||||
},
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.with_inherent_data(|_, relay_block_num, data| match relay_block_num {
|
||||
1 => {
|
||||
data.horizontal_messages.insert(ParaId::from(300), vec![MSG_1.clone()]);
|
||||
data.horizontal_messages.insert(ParaId::from(300), vec![mk_hrmp(1)]);
|
||||
},
|
||||
2 => {
|
||||
data.horizontal_messages.insert(
|
||||
@@ -1341,11 +935,11 @@ fn receive_hrmp() {
|
||||
// can't be sent at the block 1 actually. However, we cheat here
|
||||
// because we want to test the case where there are multiple messages
|
||||
// but the harness at the moment doesn't support block skipping.
|
||||
MSG_2.clone(),
|
||||
MSG_3.clone(),
|
||||
mk_hrmp(2).clone(),
|
||||
mk_hrmp(3).clone(),
|
||||
],
|
||||
);
|
||||
data.horizontal_messages.insert(ParaId::from(200), vec![MSG_4.clone()]);
|
||||
data.horizontal_messages.insert(ParaId::from(200), vec![mk_hrmp(4)]);
|
||||
},
|
||||
3 => {},
|
||||
_ => unreachable!(),
|
||||
@@ -1363,9 +957,9 @@ fn receive_hrmp() {
|
||||
assert_eq!(
|
||||
&*m,
|
||||
&[
|
||||
(ParaId::from(200), 2, b"4".to_vec()),
|
||||
(ParaId::from(300), 2, b"2".to_vec()),
|
||||
(ParaId::from(300), 2, b"3".to_vec()),
|
||||
(ParaId::from(300), 3, b"3".to_vec()),
|
||||
(ParaId::from(200), 4, b"4".to_vec()),
|
||||
]
|
||||
);
|
||||
m.clear();
|
||||
@@ -1394,55 +988,46 @@ fn receive_hrmp_empty_channel() {
|
||||
|
||||
#[test]
|
||||
fn receive_hrmp_after_pause() {
|
||||
lazy_static::lazy_static! {
|
||||
static ref MSG_1: InboundHrmpMessage = InboundHrmpMessage {
|
||||
sent_at: 1,
|
||||
data: b"mikhailinvanovich".to_vec(),
|
||||
};
|
||||
|
||||
static ref MSG_2: InboundHrmpMessage = InboundHrmpMessage {
|
||||
sent_at: 3,
|
||||
data: b"1000000000".to_vec(),
|
||||
};
|
||||
}
|
||||
|
||||
const ALICE: ParaId = ParaId::new(300);
|
||||
|
||||
BlockTests::new()
|
||||
.with_relay_sproof_builder(|_, relay_block_num, sproof| match relay_block_num {
|
||||
1 => {
|
||||
sproof.upsert_inbound_channel(ALICE).mqc_head =
|
||||
Some(MessageQueueChain::default().extend_hrmp(&MSG_1).head());
|
||||
Some(MessageQueueChain::default().extend_hrmp(&mk_hrmp(1)).head());
|
||||
},
|
||||
2 => {
|
||||
// 300 - no new messages, mqc stayed the same.
|
||||
sproof.upsert_inbound_channel(ALICE).mqc_head =
|
||||
Some(MessageQueueChain::default().extend_hrmp(&MSG_1).head());
|
||||
Some(MessageQueueChain::default().extend_hrmp(&mk_hrmp(1)).head());
|
||||
},
|
||||
3 => {
|
||||
// 300 - new message.
|
||||
sproof.upsert_inbound_channel(ALICE).mqc_head = Some(
|
||||
MessageQueueChain::default().extend_hrmp(&MSG_1).extend_hrmp(&MSG_2).head(),
|
||||
MessageQueueChain::default()
|
||||
.extend_hrmp(&mk_hrmp(1))
|
||||
.extend_hrmp(&mk_hrmp(3))
|
||||
.head(),
|
||||
);
|
||||
},
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.with_inherent_data(|_, relay_block_num, data| match relay_block_num {
|
||||
1 => {
|
||||
data.horizontal_messages.insert(ALICE, vec![MSG_1.clone()]);
|
||||
data.horizontal_messages.insert(ALICE, vec![mk_hrmp(1)]);
|
||||
},
|
||||
2 => {
|
||||
// no new messages
|
||||
},
|
||||
3 => {
|
||||
data.horizontal_messages.insert(ALICE, vec![MSG_2.clone()]);
|
||||
data.horizontal_messages.insert(ALICE, vec![mk_hrmp(3)]);
|
||||
},
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.add(1, || {
|
||||
HANDLED_XCMP_MESSAGES.with(|m| {
|
||||
let mut m = m.borrow_mut();
|
||||
assert_eq!(&*m, &[(ALICE, 1, b"mikhailinvanovich".to_vec())]);
|
||||
assert_eq!(&*m, &[(ALICE, 1, b"1".to_vec())]);
|
||||
m.clear();
|
||||
});
|
||||
})
|
||||
@@ -1450,14 +1035,75 @@ fn receive_hrmp_after_pause() {
|
||||
.add(3, || {
|
||||
HANDLED_XCMP_MESSAGES.with(|m| {
|
||||
let mut m = m.borrow_mut();
|
||||
assert_eq!(&*m, &[(ALICE, 3, b"1000000000".to_vec())]);
|
||||
assert_eq!(&*m, &[(ALICE, 3, b"3".to_vec())]);
|
||||
m.clear();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Sent up to 100 HRMP messages per block over a period of 100 blocks.
|
||||
#[test]
|
||||
fn receive_hrmp_many() {
|
||||
const ALICE: ParaId = ParaId::new(300);
|
||||
|
||||
wasm_ext().execute_with(|| {
|
||||
parameter_types! {
|
||||
pub storage MqcHead: MessageQueueChain = Default::default();
|
||||
pub storage SentInBlock: Vec<Vec<InboundHrmpMessage>> = Default::default();
|
||||
}
|
||||
|
||||
let mut sent_in_block = vec![vec![]];
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
for block in 1..100 {
|
||||
let mut msgs = vec![];
|
||||
for _ in 1..=rng.gen_range(1..=100) {
|
||||
// Just use the same message multiple times per block.
|
||||
msgs.push(mk_hrmp(block));
|
||||
}
|
||||
sent_in_block.push(msgs);
|
||||
}
|
||||
SentInBlock::set(&sent_in_block);
|
||||
|
||||
let mut tester = BlockTests::new_without_externalities()
|
||||
.with_relay_sproof_builder(|_, relay_block_num, sproof| {
|
||||
let mut new_hash = MqcHead::get();
|
||||
|
||||
for msg in SentInBlock::get()[relay_block_num as usize].iter() {
|
||||
new_hash.extend_hrmp(&msg);
|
||||
}
|
||||
|
||||
sproof.upsert_inbound_channel(ALICE).mqc_head = Some(new_hash.head());
|
||||
MqcHead::set(&new_hash);
|
||||
})
|
||||
.with_inherent_data(|_, relay_block_num, data| {
|
||||
// TODO use vector for dmp as well
|
||||
data.horizontal_messages
|
||||
.insert(ALICE, SentInBlock::get()[relay_block_num as usize].clone());
|
||||
});
|
||||
|
||||
for block in 1..100 {
|
||||
tester = tester.add(block, move || {
|
||||
HANDLED_XCMP_MESSAGES.with(|m| {
|
||||
let mut m = m.borrow_mut();
|
||||
let msgs = SentInBlock::get()[block as usize]
|
||||
.iter()
|
||||
.map(|m| (ALICE, m.sent_at, m.data.clone()))
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(&*m, &msgs);
|
||||
m.clear();
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upgrade_version_checks_should_work() {
|
||||
use codec::Encode;
|
||||
use sp_runtime::DispatchErrorWithPostInfo;
|
||||
use sp_version::RuntimeVersion;
|
||||
|
||||
let test_data = vec![
|
||||
("test", 0, 1, Err(frame_system::Error::<Test>::SpecVersionNeedsToIncrease)),
|
||||
("test", 1, 0, Err(frame_system::Error::<Test>::SpecVersionNeedsToIncrease)),
|
||||
@@ -1479,7 +1125,7 @@ fn upgrade_version_checks_should_work() {
|
||||
ext.register_extension(sp_core::traits::ReadRuntimeVersionExt::new(read_runtime_version));
|
||||
ext.execute_with(|| {
|
||||
let new_code = vec![1, 2, 3, 4];
|
||||
let new_code_hash = sp_core::H256(blake2_256(&new_code));
|
||||
let new_code_hash = H256(sp_core::blake2_256(&new_code));
|
||||
|
||||
let _authorize =
|
||||
ParachainSystem::authorize_upgrade(RawOrigin::Root.into(), new_code_hash, true);
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
// Copyright Parity Technologies (UK) Ltd.
|
||||
// This file is part of Cumulus.
|
||||
|
||||
// Cumulus is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Cumulus is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Autogenerated weights for cumulus_pallet_parachain_system
|
||||
//!
|
||||
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
|
||||
//! DATE: 2023-03-28, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
|
||||
//! WORST CASE MAP SIZE: `1000000`
|
||||
//! HOSTNAME: `i9`, CPU: `13th Gen Intel(R) Core(TM) i9-13900K`
|
||||
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("westmint-dev"), DB CACHE: 1024
|
||||
|
||||
// Executed Command:
|
||||
// ./target/release/polkadot-parachain
|
||||
// benchmark
|
||||
// pallet
|
||||
// --chain
|
||||
// westmint-dev
|
||||
// --pallet
|
||||
// cumulus_pallet_parachain_system
|
||||
// --extrinsic
|
||||
// *
|
||||
// --execution
|
||||
// wasm
|
||||
// --wasm-execution
|
||||
// compiled
|
||||
// --output
|
||||
// pallets/parachain-system/src/weights.rs
|
||||
// --steps
|
||||
// 50
|
||||
// --repeat
|
||||
// 20
|
||||
// --template
|
||||
// ../substrate/.maintain/frame-weight-template.hbs
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
#![allow(unused_parens)]
|
||||
#![allow(unused_imports)]
|
||||
|
||||
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
|
||||
use sp_std::marker::PhantomData;
|
||||
|
||||
/// Weight functions needed for cumulus_pallet_parachain_system.
|
||||
pub trait WeightInfo {
|
||||
fn enqueue_inbound_downward_messages(n: u32, ) -> Weight;
|
||||
}
|
||||
|
||||
/// Weights for cumulus_pallet_parachain_system using the Substrate node and recommended hardware.
|
||||
pub struct SubstrateWeight<T>(PhantomData<T>);
|
||||
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
|
||||
/// Storage: ParachainSystem LastDmqMqcHead (r:1 w:1)
|
||||
/// Proof Skipped: ParachainSystem LastDmqMqcHead (max_values: Some(1), max_size: None, mode: Measured)
|
||||
/// Storage: ParachainSystem ReservedDmpWeightOverride (r:1 w:0)
|
||||
/// Proof Skipped: ParachainSystem ReservedDmpWeightOverride (max_values: Some(1), max_size: None, mode: Measured)
|
||||
/// Storage: MessageQueue BookStateFor (r:1 w:1)
|
||||
/// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen)
|
||||
/// Storage: MessageQueue ServiceHead (r:1 w:1)
|
||||
/// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen)
|
||||
/// Storage: ParachainSystem ProcessedDownwardMessages (r:0 w:1)
|
||||
/// Proof Skipped: ParachainSystem ProcessedDownwardMessages (max_values: Some(1), max_size: None, mode: Measured)
|
||||
/// Storage: MessageQueue Pages (r:0 w:16)
|
||||
/// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen)
|
||||
/// The range of component `n` is `[0, 1000]`.
|
||||
fn enqueue_inbound_downward_messages(n: u32, ) -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `12`
|
||||
// Estimated: `8013`
|
||||
// Minimum execution time: 1_625_000 picoseconds.
|
||||
Weight::from_parts(1_735_000, 8013)
|
||||
// Standard Error: 14_563
|
||||
.saturating_add(Weight::from_parts(25_300_108, 0).saturating_mul(n.into()))
|
||||
.saturating_add(T::DbWeight::get().reads(4_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(4_u64))
|
||||
}
|
||||
}
|
||||
|
||||
// For backwards compatibility and tests
|
||||
impl WeightInfo for () {
|
||||
/// Storage: ParachainSystem LastDmqMqcHead (r:1 w:1)
|
||||
/// Proof Skipped: ParachainSystem LastDmqMqcHead (max_values: Some(1), max_size: None, mode: Measured)
|
||||
/// Storage: ParachainSystem ReservedDmpWeightOverride (r:1 w:0)
|
||||
/// Proof Skipped: ParachainSystem ReservedDmpWeightOverride (max_values: Some(1), max_size: None, mode: Measured)
|
||||
/// Storage: MessageQueue BookStateFor (r:1 w:1)
|
||||
/// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen)
|
||||
/// Storage: MessageQueue ServiceHead (r:1 w:1)
|
||||
/// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen)
|
||||
/// Storage: ParachainSystem ProcessedDownwardMessages (r:0 w:1)
|
||||
/// Proof Skipped: ParachainSystem ProcessedDownwardMessages (max_values: Some(1), max_size: None, mode: Measured)
|
||||
/// Storage: MessageQueue Pages (r:0 w:16)
|
||||
/// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen)
|
||||
/// The range of component `n` is `[0, 1000]`.
|
||||
fn enqueue_inbound_downward_messages(n: u32, ) -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `12`
|
||||
// Estimated: `8013`
|
||||
// Minimum execution time: 1_625_000 picoseconds.
|
||||
Weight::from_parts(1_735_000, 8013)
|
||||
// Standard Error: 14_563
|
||||
.saturating_add(Weight::from_parts(25_300_108, 0).saturating_mul(n.into()))
|
||||
.saturating_add(RocksDbWeight::get().reads(4_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(4_u64))
|
||||
}
|
||||
}
|
||||
@@ -20,25 +20,18 @@
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use codec::{Decode, DecodeLimit, Encode};
|
||||
use cumulus_primitives_core::{
|
||||
relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler, ParaId,
|
||||
};
|
||||
use frame_support::weights::Weight;
|
||||
use codec::{Decode, Encode};
|
||||
use cumulus_primitives_core::ParaId;
|
||||
pub use pallet::*;
|
||||
use scale_info::TypeInfo;
|
||||
use sp_runtime::{traits::BadOrigin, RuntimeDebug};
|
||||
use sp_std::{convert::TryFrom, prelude::*};
|
||||
use xcm::{
|
||||
latest::{ExecuteXcm, Outcome, Parent, Xcm},
|
||||
VersionedXcm, MAX_XCM_DECODE_DEPTH,
|
||||
};
|
||||
use sp_std::prelude::*;
|
||||
use xcm::latest::{ExecuteXcm, Outcome};
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_system::pallet_prelude::*;
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(_);
|
||||
@@ -52,17 +45,7 @@ pub mod pallet {
|
||||
type XcmExecutor: ExecuteXcm<Self::RuntimeCall>;
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {}
|
||||
|
||||
#[pallet::hooks]
|
||||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {}
|
||||
|
||||
#[pallet::event]
|
||||
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
||||
pub enum Event<T: Config> {
|
||||
/// Downward message is invalid XCM.
|
||||
/// \[ id \]
|
||||
@@ -85,6 +68,9 @@ pub mod pallet {
|
||||
SiblingParachain(ParaId),
|
||||
}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {}
|
||||
|
||||
impl From<ParaId> for Origin {
|
||||
fn from(id: ParaId) -> Origin {
|
||||
Origin::SiblingParachain(id)
|
||||
@@ -97,75 +83,6 @@ pub mod pallet {
|
||||
}
|
||||
}
|
||||
|
||||
/// For an incoming downward message, this just adapts an XCM executor and executes DMP messages
|
||||
/// immediately. Their origin is asserted to be the Parent location.
|
||||
///
|
||||
/// The weight `limit` is only respected as the maximum for an individual message.
|
||||
///
|
||||
/// Because this largely ignores the given weight limit, it probably isn't good for most production
|
||||
/// uses. Use DmpQueue pallet for a more robust design.
|
||||
pub struct UnlimitedDmpExecution<T>(sp_std::marker::PhantomData<T>);
|
||||
impl<T: Config> DmpMessageHandler for UnlimitedDmpExecution<T> {
|
||||
fn handle_dmp_messages(
|
||||
iter: impl Iterator<Item = (RelayBlockNumber, Vec<u8>)>,
|
||||
limit: Weight,
|
||||
) -> Weight {
|
||||
let mut used = Weight::zero();
|
||||
for (_sent_at, data) in iter {
|
||||
let id = sp_io::hashing::blake2_256(&data[..]);
|
||||
let msg = VersionedXcm::<T::RuntimeCall>::decode_all_with_depth_limit(
|
||||
MAX_XCM_DECODE_DEPTH,
|
||||
&mut data.as_slice(),
|
||||
)
|
||||
.map(Xcm::<T::RuntimeCall>::try_from);
|
||||
match msg {
|
||||
Err(_) => Pallet::<T>::deposit_event(Event::InvalidFormat(id)),
|
||||
Ok(Err(())) => Pallet::<T>::deposit_event(Event::UnsupportedVersion(id)),
|
||||
Ok(Ok(x)) => {
|
||||
let outcome = T::XcmExecutor::execute_xcm(Parent, x, id, limit);
|
||||
used = used.saturating_add(outcome.weight_used());
|
||||
Pallet::<T>::deposit_event(Event::ExecutedDownward(id, outcome));
|
||||
},
|
||||
}
|
||||
}
|
||||
used
|
||||
}
|
||||
}
|
||||
|
||||
/// For an incoming downward message, this just adapts an XCM executor and executes DMP messages
|
||||
/// immediately. Their origin is asserted to be the Parent location.
|
||||
///
|
||||
/// This respects the given weight limit and silently drops messages if they would break it. It
|
||||
/// probably isn't good for most production uses. Use DmpQueue pallet for a more robust design.
|
||||
pub struct LimitAndDropDmpExecution<T>(sp_std::marker::PhantomData<T>);
|
||||
impl<T: Config> DmpMessageHandler for LimitAndDropDmpExecution<T> {
|
||||
fn handle_dmp_messages(
|
||||
iter: impl Iterator<Item = (RelayBlockNumber, Vec<u8>)>,
|
||||
limit: Weight,
|
||||
) -> Weight {
|
||||
let mut used = Weight::zero();
|
||||
for (_sent_at, data) in iter {
|
||||
let id = sp_io::hashing::blake2_256(&data[..]);
|
||||
let msg = VersionedXcm::<T::RuntimeCall>::decode_all_with_depth_limit(
|
||||
MAX_XCM_DECODE_DEPTH,
|
||||
&mut data.as_slice(),
|
||||
)
|
||||
.map(Xcm::<T::RuntimeCall>::try_from);
|
||||
match msg {
|
||||
Err(_) => Pallet::<T>::deposit_event(Event::InvalidFormat(id)),
|
||||
Ok(Err(())) => Pallet::<T>::deposit_event(Event::UnsupportedVersion(id)),
|
||||
Ok(Ok(x)) => {
|
||||
let weight_limit = limit.saturating_sub(used);
|
||||
let outcome = T::XcmExecutor::execute_xcm(Parent, x, id, weight_limit);
|
||||
used = used.saturating_add(outcome.weight_used());
|
||||
Pallet::<T>::deposit_event(Event::ExecutedDownward(id, outcome));
|
||||
},
|
||||
}
|
||||
}
|
||||
used
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensure that the origin `o` represents a sibling parachain.
|
||||
/// Returns `Ok` with the parachain ID of the sibling or an `Err` otherwise.
|
||||
pub fn ensure_sibling_para<OuterOrigin>(o: OuterOrigin) -> Result<ParaId, BadOrigin>
|
||||
|
||||
@@ -8,7 +8,6 @@ license = "Apache-2.0"
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "3.0.0", features = [ "derive" ], default-features = false }
|
||||
log = { version = "0.4.20", default-features = false }
|
||||
rand_chacha = { version = "0.3.0", default-features = false }
|
||||
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
|
||||
|
||||
# Substrate
|
||||
@@ -18,6 +17,7 @@ sp-io = { path = "../../../substrate/primitives/io", default-features = false}
|
||||
sp-core = { path = "../../../substrate/primitives/core", default-features = false }
|
||||
sp-runtime = { path = "../../../substrate/primitives/runtime", default-features = false}
|
||||
sp-std = { path = "../../../substrate/primitives/std", default-features = false}
|
||||
pallet-message-queue = { path = "../../../substrate/frame/message-queue", default-features = false }
|
||||
|
||||
# Polkadot
|
||||
polkadot-runtime-common = { path = "../../../polkadot/runtime/common", default-features = false }
|
||||
@@ -30,6 +30,7 @@ cumulus-primitives-core = { path = "../../primitives/core", default-features = f
|
||||
|
||||
# Optional import for benchmarking
|
||||
frame-benchmarking = { path = "../../../substrate/frame/benchmarking", default-features = false, optional = true}
|
||||
bounded-collections = { version = "0.1.4", default-features = false }
|
||||
|
||||
# Bridges
|
||||
bp-xcm-bridge-hub-router = { path = "../../../bridges/primitives/xcm-bridge-hub-router", default-features = false, optional = true }
|
||||
@@ -39,6 +40,7 @@ bp-xcm-bridge-hub-router = { path = "../../../bridges/primitives/xcm-bridge-hub-
|
||||
# Substrate
|
||||
sp-core = { path = "../../../substrate/primitives/core" }
|
||||
pallet-balances = { path = "../../../substrate/frame/balances" }
|
||||
frame-support = { path = "../../../substrate/frame/support", features = ["experimental"] }
|
||||
|
||||
# Polkadot
|
||||
xcm-builder = { package = "staging-xcm-builder", path = "../../../polkadot/xcm/xcm-builder" }
|
||||
@@ -49,6 +51,7 @@ cumulus-pallet-parachain-system = { path = "../parachain-system", features = ["p
|
||||
[features]
|
||||
default = [ "std" ]
|
||||
std = [
|
||||
"bounded-collections/std",
|
||||
"bp-xcm-bridge-hub-router?/std",
|
||||
"codec/std",
|
||||
"cumulus-primitives-core/std",
|
||||
@@ -56,9 +59,9 @@ std = [
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"log/std",
|
||||
"pallet-message-queue/std",
|
||||
"polkadot-runtime-common/std",
|
||||
"polkadot-runtime-parachains/std",
|
||||
"rand_chacha/std",
|
||||
"scale-info/std",
|
||||
"sp-core/std",
|
||||
"sp-io/std",
|
||||
@@ -70,10 +73,12 @@ std = [
|
||||
|
||||
runtime-benchmarks = [
|
||||
"cumulus-pallet-parachain-system/runtime-benchmarks",
|
||||
"cumulus-primitives-core/runtime-benchmarks",
|
||||
"frame-benchmarking/runtime-benchmarks",
|
||||
"frame-support/runtime-benchmarks",
|
||||
"frame-system/runtime-benchmarks",
|
||||
"pallet-balances/runtime-benchmarks",
|
||||
"pallet-message-queue/runtime-benchmarks",
|
||||
"polkadot-runtime-common/runtime-benchmarks",
|
||||
"polkadot-runtime-parachains/runtime-benchmarks",
|
||||
"sp-runtime/runtime-benchmarks",
|
||||
@@ -85,6 +90,7 @@ try-runtime = [
|
||||
"frame-support/try-runtime",
|
||||
"frame-system/try-runtime",
|
||||
"pallet-balances/try-runtime",
|
||||
"pallet-message-queue/try-runtime",
|
||||
"polkadot-runtime-common/try-runtime",
|
||||
"polkadot-runtime-parachains/try-runtime",
|
||||
"sp-runtime/try-runtime",
|
||||
|
||||
@@ -17,12 +17,158 @@
|
||||
|
||||
use crate::*;
|
||||
|
||||
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};
|
||||
use codec::DecodeAll;
|
||||
use frame_benchmarking::v2::*;
|
||||
use frame_support::traits::Hooks;
|
||||
use frame_system::RawOrigin;
|
||||
use xcm::v3::MAX_INSTRUCTIONS_TO_DECODE;
|
||||
|
||||
benchmarks! {
|
||||
set_config_with_u32 {}: update_resume_threshold(RawOrigin::Root, 100)
|
||||
set_config_with_weight {}: update_weight_restrict_decay(RawOrigin::Root, Weight::from_parts(3_000_000, 0))
|
||||
#[benchmarks]
|
||||
mod benchmarks {
|
||||
use super::*;
|
||||
|
||||
/// Modify any of the `QueueConfig` fields with a new `u32` value.
|
||||
///
|
||||
/// Used as weight for:
|
||||
/// - update_suspend_threshold
|
||||
/// - update_drop_threshold
|
||||
/// - update_resume_threshold
|
||||
#[benchmark]
|
||||
fn set_config_with_u32() {
|
||||
#[extrinsic_call]
|
||||
Pallet::<T>::update_resume_threshold(RawOrigin::Root, 1);
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn enqueue_xcmp_message() {
|
||||
assert!(QueueConfig::<T>::get().drop_threshold * MaxXcmpMessageLenOf::<T>::get() > 1000);
|
||||
let msg = BoundedVec::<u8, MaxXcmpMessageLenOf<T>>::default();
|
||||
|
||||
#[block]
|
||||
{
|
||||
Pallet::<T>::enqueue_xcmp_message(0.into(), msg, &mut WeightMeter::new()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn suspend_channel() {
|
||||
let para = 123.into();
|
||||
let data = ChannelSignal::Suspend.encode();
|
||||
|
||||
#[block]
|
||||
{
|
||||
ChannelSignal::decode_all(&mut &data[..]).unwrap();
|
||||
Pallet::<T>::suspend_channel(para);
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
OutboundXcmpStatus::<T>::get()
|
||||
.iter()
|
||||
.find(|p| p.recipient == para)
|
||||
.unwrap()
|
||||
.state,
|
||||
OutboundState::Suspended
|
||||
);
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn resume_channel() {
|
||||
let para = 123.into();
|
||||
let data = ChannelSignal::Resume.encode();
|
||||
|
||||
Pallet::<T>::suspend_channel(para);
|
||||
|
||||
#[block]
|
||||
{
|
||||
ChannelSignal::decode_all(&mut &data[..]).unwrap();
|
||||
Pallet::<T>::resume_channel(para);
|
||||
}
|
||||
|
||||
assert!(
|
||||
OutboundXcmpStatus::<T>::get().iter().find(|p| p.recipient == para).is_none(),
|
||||
"No messages in the channel; therefore removed."
|
||||
);
|
||||
}
|
||||
|
||||
/// Split a singular XCM.
|
||||
#[benchmark]
|
||||
fn take_first_concatenated_xcm() {
|
||||
let max_downward_message_size = MaxXcmpMessageLenOf::<T>::get() as usize;
|
||||
|
||||
assert!(MAX_INSTRUCTIONS_TO_DECODE as u32 > MAX_XCM_DECODE_DEPTH, "Preconditon failed");
|
||||
let max_instrs = MAX_INSTRUCTIONS_TO_DECODE as u32 - MAX_XCM_DECODE_DEPTH;
|
||||
let mut xcm = Xcm::<T>(vec![ClearOrigin; max_instrs as usize]);
|
||||
|
||||
for _ in 0..MAX_XCM_DECODE_DEPTH - 1 {
|
||||
xcm = Xcm::<T>(vec![Instruction::SetAppendix(xcm)]);
|
||||
}
|
||||
|
||||
let data = VersionedXcm::<T>::from(xcm).encode();
|
||||
assert!(data.len() < max_downward_message_size, "Page size is too small");
|
||||
// Verify that decoding works with the exact recursion limit:
|
||||
VersionedXcm::<T::RuntimeCall>::decode_with_depth_limit(
|
||||
MAX_XCM_DECODE_DEPTH,
|
||||
&mut &data[..],
|
||||
)
|
||||
.unwrap();
|
||||
VersionedXcm::<T::RuntimeCall>::decode_with_depth_limit(
|
||||
MAX_XCM_DECODE_DEPTH - 1,
|
||||
&mut &data[..],
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
#[block]
|
||||
{
|
||||
Pallet::<T>::take_first_concatenated_xcm(&mut &data[..], &mut WeightMeter::new())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Benchmark the migration for a maximal sized message.
|
||||
#[benchmark]
|
||||
fn on_idle_good_msg() {
|
||||
use migration::v3;
|
||||
|
||||
let block = 5;
|
||||
let para = ParaId::from(4);
|
||||
let message = vec![123u8; MaxXcmpMessageLenOf::<T>::get() as usize];
|
||||
let message_metadata = vec![(block, XcmpMessageFormat::ConcatenatedVersionedXcm)];
|
||||
|
||||
v3::InboundXcmpMessages::<T>::insert(para, block, message);
|
||||
v3::InboundXcmpStatus::<T>::set(Some(vec![v3::InboundChannelDetails {
|
||||
sender: para,
|
||||
state: v3::InboundState::Ok,
|
||||
message_metadata,
|
||||
}]));
|
||||
|
||||
#[block]
|
||||
{
|
||||
Pallet::<T>::on_idle(0u32.into(), Weight::MAX);
|
||||
}
|
||||
}
|
||||
|
||||
/// Benchmark the migration with a 64 KiB message that will not be possible to enqueue.
|
||||
#[benchmark]
|
||||
fn on_idle_large_msg() {
|
||||
use migration::v3;
|
||||
|
||||
let block = 5;
|
||||
let para = ParaId::from(4);
|
||||
let message = vec![123u8; 1 << 16]; // 64 KiB message
|
||||
let message_metadata = vec![(block, XcmpMessageFormat::ConcatenatedVersionedXcm)];
|
||||
|
||||
v3::InboundXcmpMessages::<T>::insert(para, block, message);
|
||||
v3::InboundXcmpStatus::<T>::set(Some(vec![v3::InboundChannelDetails {
|
||||
sender: para,
|
||||
state: v3::InboundState::Ok,
|
||||
message_metadata,
|
||||
}]));
|
||||
|
||||
#[block]
|
||||
{
|
||||
Pallet::<T>::on_idle(0u32.into(), Weight::MAX);
|
||||
}
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,20 +16,23 @@
|
||||
|
||||
//! A module that is responsible for migration of storage.
|
||||
|
||||
use crate::{Config, Overweight, Pallet, QueueConfig, DEFAULT_POV_SIZE};
|
||||
use crate::{Config, OverweightIndex, Pallet, ParaId, QueueConfig, DEFAULT_POV_SIZE};
|
||||
use cumulus_primitives_core::XcmpMessageFormat;
|
||||
use frame_support::{
|
||||
pallet_prelude::*,
|
||||
traits::{OnRuntimeUpgrade, StorageVersion},
|
||||
traits::{EnqueueMessage, OnRuntimeUpgrade, StorageVersion},
|
||||
weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight},
|
||||
};
|
||||
|
||||
/// The current storage version.
|
||||
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(3);
|
||||
|
||||
/// Migrates the pallet storage to the most recent version.
|
||||
pub struct Migration<T: Config>(PhantomData<T>);
|
||||
pub const LOG: &str = "runtime::xcmp-queue-migration";
|
||||
|
||||
impl<T: Config> OnRuntimeUpgrade for Migration<T> {
|
||||
/// Migrates the pallet storage to the most recent version.
|
||||
pub struct MigrationToV3<T: Config>(PhantomData<T>);
|
||||
|
||||
impl<T: Config> OnRuntimeUpgrade for MigrationToV3<T> {
|
||||
fn on_runtime_upgrade() -> Weight {
|
||||
let mut weight = T::DbWeight::get().reads(1);
|
||||
|
||||
@@ -77,11 +80,55 @@ mod v1 {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod v3 {
|
||||
use super::*;
|
||||
use crate::*;
|
||||
|
||||
/// Status of the inbound XCMP channels.
|
||||
#[frame_support::storage_alias]
|
||||
pub(crate) type InboundXcmpStatus<T: Config> =
|
||||
StorageValue<Pallet<T>, Vec<InboundChannelDetails>, OptionQuery>;
|
||||
|
||||
/// Inbound aggregate XCMP messages. It can only be one per ParaId/block.
|
||||
#[frame_support::storage_alias]
|
||||
pub(crate) type InboundXcmpMessages<T: Config> = StorageDoubleMap<
|
||||
Pallet<T>,
|
||||
Blake2_128Concat,
|
||||
ParaId,
|
||||
Twox64Concat,
|
||||
RelayBlockNumber,
|
||||
Vec<u8>,
|
||||
OptionQuery,
|
||||
>;
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, TypeInfo)]
|
||||
pub struct InboundChannelDetails {
|
||||
/// The `ParaId` of the parachain that this channel is connected with.
|
||||
pub sender: ParaId,
|
||||
/// The state of the channel.
|
||||
pub state: InboundState,
|
||||
/// The ordered metadata of each inbound message.
|
||||
///
|
||||
/// Contains info about the relay block number that the message was sent at, and the format
|
||||
/// of the incoming message.
|
||||
pub message_metadata: Vec<(RelayBlockNumber, XcmpMessageFormat)>,
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, TypeInfo,
|
||||
)]
|
||||
pub enum InboundState {
|
||||
Ok,
|
||||
Suspended,
|
||||
}
|
||||
}
|
||||
|
||||
/// Migrates `QueueConfigData` from v1 (using only reference time weights) to v2 (with
|
||||
/// 2D weights).
|
||||
///
|
||||
/// NOTE: Only use this function if you know what you're doing. Default to using
|
||||
/// `migrate_to_latest`.
|
||||
#[allow(deprecated)]
|
||||
pub fn migrate_to_v2<T: Config>() -> Weight {
|
||||
let translate = |pre: v1::QueueConfigData| -> super::QueueConfigData {
|
||||
super::QueueConfigData {
|
||||
@@ -108,17 +155,70 @@ pub fn migrate_to_v2<T: Config>() -> Weight {
|
||||
}
|
||||
|
||||
pub fn migrate_to_v3<T: Config>() -> Weight {
|
||||
#[frame_support::storage_alias]
|
||||
type Overweight<T: Config> =
|
||||
CountedStorageMap<Pallet<T>, Twox64Concat, OverweightIndex, ParaId>;
|
||||
let overweight_messages = Overweight::<T>::initialize_counter() as u64;
|
||||
|
||||
T::DbWeight::get().reads_writes(overweight_messages, 1)
|
||||
}
|
||||
|
||||
pub fn lazy_migrate_inbound_queue<T: Config>() {
|
||||
let Some(mut states) = v3::InboundXcmpStatus::<T>::get() else {
|
||||
log::debug!(target: LOG, "Lazy migration finished: item gone");
|
||||
return
|
||||
};
|
||||
let Some(ref mut next) = states.first_mut() else {
|
||||
log::debug!(target: LOG, "Lazy migration finished: item empty");
|
||||
v3::InboundXcmpStatus::<T>::kill();
|
||||
return
|
||||
};
|
||||
log::debug!(
|
||||
"Migrating inbound HRMP channel with sibling {:?}, msgs left {}.",
|
||||
next.sender,
|
||||
next.message_metadata.len()
|
||||
);
|
||||
// We take the last element since the MQ is a FIFO and we want to keep the order.
|
||||
let Some((block_number, format)) = next.message_metadata.pop() else {
|
||||
states.remove(0);
|
||||
v3::InboundXcmpStatus::<T>::put(states);
|
||||
return
|
||||
};
|
||||
if format != XcmpMessageFormat::ConcatenatedVersionedXcm {
|
||||
log::warn!(target: LOG,
|
||||
"Dropping message with format {:?} (not ConcatenatedVersionedXcm)",
|
||||
format
|
||||
);
|
||||
v3::InboundXcmpMessages::<T>::remove(&next.sender, &block_number);
|
||||
v3::InboundXcmpStatus::<T>::put(states);
|
||||
return
|
||||
}
|
||||
|
||||
let Some(msg) = v3::InboundXcmpMessages::<T>::take(&next.sender, &block_number) else {
|
||||
defensive!("Storage corrupted: HRMP message missing:", (next.sender, block_number));
|
||||
v3::InboundXcmpStatus::<T>::put(states);
|
||||
return
|
||||
};
|
||||
|
||||
let Ok(msg): Result<BoundedVec<_, _>, _> = msg.try_into() else {
|
||||
log::error!(target: LOG, "Message dropped: too big");
|
||||
v3::InboundXcmpStatus::<T>::put(states);
|
||||
return
|
||||
};
|
||||
|
||||
// Finally! We have a proper message.
|
||||
T::XcmpQueue::enqueue_message(msg.as_bounded_slice(), next.sender);
|
||||
log::debug!(target: LOG, "Migrated HRMP message to MQ: {:?}", (next.sender, block_number));
|
||||
v3::InboundXcmpStatus::<T>::put(states);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::mock::{new_test_ext, Test};
|
||||
|
||||
#[test]
|
||||
#[allow(deprecated)]
|
||||
fn test_migration_to_v2() {
|
||||
let v1 = v1::QueueConfigData {
|
||||
suspend_threshold: 5,
|
||||
|
||||
@@ -17,10 +17,11 @@ use super::*;
|
||||
use crate as xcmp_queue;
|
||||
use core::marker::PhantomData;
|
||||
use cumulus_pallet_parachain_system::AnyRelayNumber;
|
||||
use cumulus_primitives_core::{IsSystem, ParaId};
|
||||
use cumulus_primitives_core::{ChannelInfo, IsSystem, ParaId};
|
||||
use frame_support::{
|
||||
parameter_types,
|
||||
traits::{ConstU32, Everything, Nothing, OriginTrait},
|
||||
BoundedSlice,
|
||||
};
|
||||
use frame_system::EnsureRoot;
|
||||
use sp_core::H256;
|
||||
@@ -105,11 +106,13 @@ impl pallet_balances::Config for Test {
|
||||
}
|
||||
|
||||
impl cumulus_pallet_parachain_system::Config for Test {
|
||||
type WeightInfo = ();
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type OnSystemEvent = ();
|
||||
type SelfParaId = ();
|
||||
type OutboundXcmpMessageSource = XcmpQueue;
|
||||
type DmpMessageHandler = ();
|
||||
// Ignore all DMP messages by enqueueing them into `()`:
|
||||
type DmpQueue = frame_support::traits::EnqueueWithOrigin<(), sp_core::ConstU8<0>>;
|
||||
type ReservedDmpWeight = ();
|
||||
type XcmpMessageHandler = XcmpQueue;
|
||||
type ReservedXcmpWeight = ();
|
||||
@@ -199,6 +202,54 @@ impl<RuntimeOrigin: OriginTrait> ConvertOrigin<RuntimeOrigin>
|
||||
}
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub static EnqueuedMessages: Vec<(ParaId, Vec<u8>)> = Default::default();
|
||||
}
|
||||
|
||||
/// An `EnqueueMessage` implementation that puts all messages in thread-local storage.
|
||||
pub struct EnqueueToLocalStorage<T>(PhantomData<T>);
|
||||
|
||||
impl<T: OnQueueChanged<ParaId>> EnqueueMessage<ParaId> for EnqueueToLocalStorage<T> {
|
||||
type MaxMessageLen = sp_core::ConstU32<65_536>;
|
||||
|
||||
fn enqueue_message(message: BoundedSlice<u8, Self::MaxMessageLen>, origin: ParaId) {
|
||||
let mut msgs = EnqueuedMessages::get();
|
||||
msgs.push((origin, message.to_vec()));
|
||||
EnqueuedMessages::set(msgs);
|
||||
T::on_queue_changed(origin, Self::footprint(origin));
|
||||
}
|
||||
|
||||
fn enqueue_messages<'a>(
|
||||
iter: impl Iterator<Item = BoundedSlice<'a, u8, Self::MaxMessageLen>>,
|
||||
origin: ParaId,
|
||||
) {
|
||||
let mut msgs = EnqueuedMessages::get();
|
||||
msgs.extend(iter.map(|m| (origin, m.to_vec())));
|
||||
EnqueuedMessages::set(msgs);
|
||||
T::on_queue_changed(origin, Self::footprint(origin));
|
||||
}
|
||||
|
||||
fn sweep_queue(origin: ParaId) {
|
||||
let mut msgs = EnqueuedMessages::get();
|
||||
msgs.retain(|(o, _)| o != &origin);
|
||||
EnqueuedMessages::set(msgs);
|
||||
T::on_queue_changed(origin, Self::footprint(origin));
|
||||
}
|
||||
|
||||
fn footprint(origin: ParaId) -> QueueFootprint {
|
||||
let msgs = EnqueuedMessages::get();
|
||||
let mut footprint = QueueFootprint::default();
|
||||
for (o, m) in msgs {
|
||||
if o == origin {
|
||||
footprint.storage.count += 1;
|
||||
footprint.storage.size += m.len() as u64;
|
||||
}
|
||||
}
|
||||
footprint.pages = footprint.storage.size as u32 / 16; // Number does not matter
|
||||
footprint
|
||||
}
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
/// The asset ID for the asset that we use to pay for message delivery fees.
|
||||
pub FeeAssetId: AssetId = Concrete(RelayChain::get());
|
||||
@@ -217,10 +268,10 @@ pub type PriceForSiblingParachainDelivery = polkadot_runtime_common::xcm_sender:
|
||||
|
||||
impl Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type XcmExecutor = xcm_executor::XcmExecutor<XcmConfig>;
|
||||
type ChannelInfo = ParachainSystem;
|
||||
type ChannelInfo = MockedChannelInfo;
|
||||
type VersionWrapper = ();
|
||||
type ExecuteOverweightOrigin = EnsureRoot<AccountId>;
|
||||
type XcmpQueue = EnqueueToLocalStorage<Pallet<Test>>;
|
||||
type MaxInboundSuspended = sp_core::ConstU32<1_000>;
|
||||
type ControllerOrigin = EnsureRoot<AccountId>;
|
||||
type ControllerOriginConverter = SystemParachainAsSuperuser<RuntimeOrigin>;
|
||||
type WeightInfo = ();
|
||||
@@ -228,6 +279,60 @@ impl Config for Test {
|
||||
}
|
||||
|
||||
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
|
||||
t.into()
|
||||
frame_system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
|
||||
}
|
||||
|
||||
/// A para that we have an HRMP channel with.
|
||||
pub const HRMP_PARA_ID: u32 = 7777;
|
||||
|
||||
pub struct MockedChannelInfo;
|
||||
impl GetChannelInfo for MockedChannelInfo {
|
||||
fn get_channel_status(id: ParaId) -> ChannelStatus {
|
||||
if id == HRMP_PARA_ID.into() {
|
||||
return ChannelStatus::Ready(usize::MAX, usize::MAX)
|
||||
}
|
||||
|
||||
ParachainSystem::get_channel_status(id)
|
||||
}
|
||||
|
||||
fn get_channel_info(id: ParaId) -> Option<ChannelInfo> {
|
||||
if id == HRMP_PARA_ID.into() {
|
||||
return Some(ChannelInfo {
|
||||
max_capacity: u32::MAX,
|
||||
max_total_size: u32::MAX,
|
||||
max_message_size: u32::MAX,
|
||||
msg_count: 0,
|
||||
total_size: 0,
|
||||
})
|
||||
}
|
||||
|
||||
ParachainSystem::get_channel_info(id)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn mk_page() -> Vec<u8> {
|
||||
let mut page = Vec::<u8>::new();
|
||||
|
||||
for i in 0..100 {
|
||||
page.extend(match i % 2 {
|
||||
0 => v2_xcm().encode(),
|
||||
1 => v3_xcm().encode(),
|
||||
// We cannot push an undecodable XCM here since it would break the decode stream.
|
||||
// This is expected and the whole reason to introduce `MaybeDoubleEncodedVersionedXcm`
|
||||
// instead.
|
||||
_ => unreachable!(),
|
||||
});
|
||||
}
|
||||
|
||||
page
|
||||
}
|
||||
|
||||
pub(crate) fn v2_xcm() -> VersionedXcm<()> {
|
||||
let instr = xcm::v2::Instruction::<()>::ClearOrigin;
|
||||
VersionedXcm::V2(xcm::v2::Xcm::<()>(vec![instr; 3]))
|
||||
}
|
||||
|
||||
pub(crate) fn v3_xcm() -> VersionedXcm<()> {
|
||||
let instr = xcm::v3::Instruction::<()>::Trap(1);
|
||||
VersionedXcm::V3(xcm::v3::Xcm::<()>(vec![instr; 3]))
|
||||
}
|
||||
|
||||
@@ -13,247 +13,325 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use super::*;
|
||||
use super::{
|
||||
mock::{mk_page, v2_xcm, v3_xcm, EnqueuedMessages, HRMP_PARA_ID},
|
||||
*,
|
||||
};
|
||||
use XcmpMessageFormat::*;
|
||||
|
||||
use codec::Input;
|
||||
use cumulus_primitives_core::{ParaId, XcmpMessageHandler};
|
||||
use frame_support::{assert_noop, assert_ok};
|
||||
use mock::{new_test_ext, ParachainSystem, RuntimeCall, RuntimeOrigin, Test, XcmpQueue};
|
||||
use sp_runtime::traits::BadOrigin;
|
||||
use frame_support::{
|
||||
assert_err, assert_noop, assert_ok, assert_storage_noop, hypothetically,
|
||||
traits::{Footprint, Hooks},
|
||||
StorageNoopGuard,
|
||||
};
|
||||
use mock::{new_test_ext, ParachainSystem, RuntimeOrigin as Origin, Test, XcmpQueue};
|
||||
use sp_runtime::traits::{BadOrigin, Zero};
|
||||
use std::iter::{once, repeat};
|
||||
|
||||
#[test]
|
||||
fn one_message_does_not_panic() {
|
||||
fn empty_concatenated_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let message_format = XcmpMessageFormat::ConcatenatedVersionedXcm.encode();
|
||||
let messages = vec![(Default::default(), 1u32, message_format.as_slice())];
|
||||
let data = ConcatenatedVersionedXcm.encode();
|
||||
|
||||
// This shouldn't cause a panic
|
||||
XcmpQueue::handle_xcmp_messages(messages.into_iter(), Weight::MAX);
|
||||
XcmpQueue::handle_xcmp_messages(once((1000.into(), 1, data.as_slice())), Weight::MAX);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Invalid incoming blob message data"]
|
||||
fn xcm_enqueueing_basic_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let xcm = VersionedXcm::<Test>::from(Xcm::<Test>(vec![ClearOrigin])).encode();
|
||||
let data = [ConcatenatedVersionedXcm.encode(), xcm.clone()].concat();
|
||||
|
||||
XcmpQueue::handle_xcmp_messages(once((1000.into(), 1, data.as_slice())), Weight::MAX);
|
||||
|
||||
assert_eq!(EnqueuedMessages::get(), vec![(1000.into(), xcm)]);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xcm_enqueueing_many_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let mut encoded_xcms = vec![];
|
||||
for i in 0..10 {
|
||||
let xcm = VersionedXcm::<Test>::from(Xcm::<Test>(vec![ClearOrigin; i as usize]));
|
||||
encoded_xcms.push(xcm.encode());
|
||||
}
|
||||
let mut data = ConcatenatedVersionedXcm.encode();
|
||||
data.extend(encoded_xcms.iter().flatten());
|
||||
|
||||
XcmpQueue::handle_xcmp_messages(once((1000.into(), 1, data.as_slice())), Weight::MAX);
|
||||
|
||||
assert_eq!(
|
||||
EnqueuedMessages::get(),
|
||||
encoded_xcms.into_iter().map(|xcm| (1000.into(), xcm)).collect::<Vec<_>>(),
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xcm_enqueueing_multiple_times_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let mut encoded_xcms = vec![];
|
||||
for _ in 0..10 {
|
||||
let xcm = VersionedXcm::<Test>::from(Xcm::<Test>(vec![ClearOrigin]));
|
||||
encoded_xcms.push(xcm.encode());
|
||||
}
|
||||
let mut data = ConcatenatedVersionedXcm.encode();
|
||||
data.extend(encoded_xcms.iter().flatten());
|
||||
|
||||
for i in 0..10 {
|
||||
XcmpQueue::handle_xcmp_messages(once((1000.into(), 1, data.as_slice())), Weight::MAX);
|
||||
assert_eq!((i + 1) * 10, EnqueuedMessages::get().len());
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
EnqueuedMessages::get(),
|
||||
encoded_xcms
|
||||
.into_iter()
|
||||
.map(|xcm| (1000.into(), xcm))
|
||||
.cycle()
|
||||
.take(100)
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(debug_assertions, should_panic = "Defensive failure")]
|
||||
fn xcm_enqueueing_starts_dropping_on_overflow() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let xcm = VersionedXcm::<Test>::from(Xcm::<Test>(vec![ClearOrigin]));
|
||||
let data = (ConcatenatedVersionedXcm, xcm).encode();
|
||||
// Its possible to enqueue 256 messages at most:
|
||||
let limit = 256;
|
||||
|
||||
XcmpQueue::handle_xcmp_messages(
|
||||
repeat((1000.into(), 1, data.as_slice())).take(limit * 2),
|
||||
Weight::MAX,
|
||||
);
|
||||
assert_eq!(EnqueuedMessages::get().len(), limit);
|
||||
// The drop threshold for pages is 48, the others numbers dont really matter:
|
||||
assert_eq!(
|
||||
<Test as Config>::XcmpQueue::footprint(1000.into()),
|
||||
QueueFootprint { storage: Footprint { count: 256, size: 768 }, pages: 48 }
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
/// First enqueue 10 good, 1 bad and then 10 good XCMs.
|
||||
///
|
||||
/// It should only process the first 10 good though.
|
||||
#[test]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn xcm_enqueueing_broken_xcm_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let mut encoded_xcms = vec![];
|
||||
for _ in 0..10 {
|
||||
let xcm = VersionedXcm::<Test>::from(Xcm::<Test>(vec![ClearOrigin]));
|
||||
encoded_xcms.push(xcm.encode());
|
||||
}
|
||||
let mut good = ConcatenatedVersionedXcm.encode();
|
||||
good.extend(encoded_xcms.iter().flatten());
|
||||
|
||||
let mut bad = ConcatenatedVersionedXcm.encode();
|
||||
bad.extend(vec![0u8].into_iter());
|
||||
|
||||
// Of we enqueue them in multiple pages, then its fine.
|
||||
XcmpQueue::handle_xcmp_messages(once((1000.into(), 1, good.as_slice())), Weight::MAX);
|
||||
XcmpQueue::handle_xcmp_messages(once((1000.into(), 1, bad.as_slice())), Weight::MAX);
|
||||
XcmpQueue::handle_xcmp_messages(once((1000.into(), 1, good.as_slice())), Weight::MAX);
|
||||
assert_eq!(20, EnqueuedMessages::get().len());
|
||||
|
||||
assert_eq!(
|
||||
EnqueuedMessages::get(),
|
||||
encoded_xcms
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|xcm| (1000.into(), xcm))
|
||||
.cycle()
|
||||
.take(20)
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
EnqueuedMessages::set(&vec![]);
|
||||
|
||||
// But if we do it all in one page, then it only uses the first 10:
|
||||
XcmpQueue::handle_xcmp_messages(
|
||||
vec![(1000.into(), 1, good.as_slice()), (1000.into(), 1, bad.as_slice())].into_iter(),
|
||||
Weight::MAX,
|
||||
);
|
||||
assert_eq!(10, EnqueuedMessages::get().len());
|
||||
assert_eq!(
|
||||
EnqueuedMessages::get(),
|
||||
encoded_xcms
|
||||
.into_iter()
|
||||
.map(|xcm| (1000.into(), xcm))
|
||||
.cycle()
|
||||
.take(10)
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
/// Message blobs are not supported and panic in debug mode.
|
||||
#[test]
|
||||
#[should_panic = "Blob messages are unhandled"]
|
||||
#[cfg(debug_assertions)]
|
||||
fn bad_message_is_handled() {
|
||||
fn bad_blob_message_panics() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let bad_data = vec![
|
||||
1, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 64, 239, 139, 0,
|
||||
0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 37, 0,
|
||||
0, 0, 0, 0, 0, 0, 16, 0, 127, 147,
|
||||
];
|
||||
InboundXcmpMessages::<Test>::insert(ParaId::from(1000), 1, bad_data);
|
||||
let format = XcmpMessageFormat::ConcatenatedEncodedBlob;
|
||||
// This should exit with an error.
|
||||
XcmpQueue::process_xcmp_message(
|
||||
1000.into(),
|
||||
(1, format),
|
||||
&mut 0,
|
||||
Weight::from_parts(10_000_000_000, 0),
|
||||
Weight::from_parts(10_000_000_000, 0),
|
||||
);
|
||||
let data = [ConcatenatedEncodedBlob.encode(), vec![1].encode()].concat();
|
||||
|
||||
XcmpQueue::handle_xcmp_messages(once((1000.into(), 1, data.as_slice())), Weight::MAX);
|
||||
});
|
||||
}
|
||||
|
||||
/// Tests that a blob message is handled. Currently this isn't implemented and panics when debug
|
||||
/// assertions are enabled. When this feature is enabled, this test should be rewritten properly.
|
||||
/// Message blobs do not panic in release mode but are just a No-OP.
|
||||
#[test]
|
||||
#[should_panic = "Blob messages not handled."]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn bad_blob_message_no_panic() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let data = [ConcatenatedEncodedBlob.encode(), vec![1].encode()].concat();
|
||||
|
||||
frame_support::assert_storage_noop!(XcmpQueue::handle_xcmp_messages(
|
||||
once((1000.into(), 1, data.as_slice())),
|
||||
Weight::MAX,
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
/// Invalid concatenated XCMs panic in debug mode.
|
||||
#[test]
|
||||
#[should_panic = "HRMP inbound decode stream broke; page will be dropped."]
|
||||
#[cfg(debug_assertions)]
|
||||
fn handle_blob_message() {
|
||||
fn handle_invalid_data_panics() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let bad_data = vec![
|
||||
1, 1, 1, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 64, 239,
|
||||
139, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0,
|
||||
37, 0, 0, 0, 0, 0, 0, 0, 16, 0, 127, 147,
|
||||
];
|
||||
InboundXcmpMessages::<Test>::insert(ParaId::from(1000), 1, bad_data);
|
||||
let format = XcmpMessageFormat::ConcatenatedEncodedBlob;
|
||||
XcmpQueue::process_xcmp_message(
|
||||
1000.into(),
|
||||
(1, format),
|
||||
&mut 0,
|
||||
Weight::from_parts(10_000_000_000, 0),
|
||||
Weight::from_parts(10_000_000_000, 0),
|
||||
);
|
||||
let data = [ConcatenatedVersionedXcm.encode(), Xcm::<Test>(vec![]).encode()].concat();
|
||||
|
||||
XcmpQueue::handle_xcmp_messages(once((1000.into(), 1, data.as_slice())), Weight::MAX);
|
||||
});
|
||||
}
|
||||
|
||||
/// Invalid concatenated XCMs do not panic in release mode but are just a No-OP.
|
||||
#[test]
|
||||
#[should_panic = "Invalid incoming XCMP message data"]
|
||||
#[cfg(debug_assertions)]
|
||||
fn handle_invalid_data() {
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn handle_invalid_data_no_panic() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let data = Xcm::<Test>(vec![]).encode();
|
||||
InboundXcmpMessages::<Test>::insert(ParaId::from(1000), 1, data);
|
||||
let format = XcmpMessageFormat::ConcatenatedVersionedXcm;
|
||||
XcmpQueue::process_xcmp_message(
|
||||
1000.into(),
|
||||
(1, format),
|
||||
&mut 0,
|
||||
Weight::from_parts(10_000_000_000, 0),
|
||||
Weight::from_parts(10_000_000_000, 0),
|
||||
);
|
||||
});
|
||||
}
|
||||
let data = [ConcatenatedVersionedXcm.encode(), Xcm::<Test>(vec![]).encode()].concat();
|
||||
|
||||
#[test]
|
||||
fn service_overweight_unknown() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_noop!(
|
||||
XcmpQueue::service_overweight(RuntimeOrigin::root(), 0, Weight::from_parts(1000, 1000)),
|
||||
Error::<Test>::BadOverweightIndex,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn service_overweight_bad_xcm_format() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let bad_xcm = vec![255];
|
||||
Overweight::<Test>::insert(0, (ParaId::from(1000), 0, bad_xcm));
|
||||
|
||||
assert_noop!(
|
||||
XcmpQueue::service_overweight(RuntimeOrigin::root(), 0, Weight::from_parts(1000, 1000)),
|
||||
Error::<Test>::BadXcm
|
||||
);
|
||||
frame_support::assert_storage_noop!(XcmpQueue::handle_xcmp_messages(
|
||||
once((1000.into(), 1, data.as_slice())),
|
||||
Weight::MAX,
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn suspend_xcm_execution_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert!(!XcmpQueue::is_paused(&2000.into()));
|
||||
QueueSuspended::<Test>::put(true);
|
||||
assert!(XcmpQueue::is_paused(&2000.into()));
|
||||
// System parachains can bypass suspension:
|
||||
assert!(!XcmpQueue::is_paused(&999.into()));
|
||||
});
|
||||
}
|
||||
|
||||
let xcm =
|
||||
VersionedXcm::from(Xcm::<RuntimeCall>(vec![Instruction::<RuntimeCall>::ClearOrigin]))
|
||||
.encode();
|
||||
let mut message_format = XcmpMessageFormat::ConcatenatedVersionedXcm.encode();
|
||||
message_format.extend(xcm.clone());
|
||||
let messages = vec![(ParaId::from(999), 1u32, message_format.as_slice())];
|
||||
#[test]
|
||||
fn suspend_and_resume_xcm_execution_work() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_noop!(XcmpQueue::suspend_xcm_execution(Origin::signed(1)), BadOrigin);
|
||||
assert_ok!(XcmpQueue::suspend_xcm_execution(Origin::root()));
|
||||
assert_noop!(
|
||||
XcmpQueue::suspend_xcm_execution(Origin::root()),
|
||||
Error::<Test>::AlreadySuspended
|
||||
);
|
||||
assert!(QueueSuspended::<Test>::get());
|
||||
|
||||
// This should have executed the incoming XCM, because it came from a system parachain
|
||||
XcmpQueue::handle_xcmp_messages(messages.into_iter(), Weight::MAX);
|
||||
assert_noop!(XcmpQueue::resume_xcm_execution(Origin::signed(1)), BadOrigin);
|
||||
assert_ok!(XcmpQueue::resume_xcm_execution(Origin::root()));
|
||||
assert_noop!(
|
||||
XcmpQueue::resume_xcm_execution(Origin::root()),
|
||||
Error::<Test>::AlreadyResumed
|
||||
);
|
||||
assert!(!QueueSuspended::<Test>::get());
|
||||
});
|
||||
}
|
||||
|
||||
let queued_xcm = InboundXcmpMessages::<Test>::get(ParaId::from(999), 1u32);
|
||||
assert!(queued_xcm.is_empty());
|
||||
#[test]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn xcm_enqueueing_backpressure_works() {
|
||||
let para: ParaId = 1000.into();
|
||||
new_test_ext().execute_with(|| {
|
||||
let xcm = VersionedXcm::<Test>::from(Xcm::<Test>(vec![ClearOrigin]));
|
||||
let data = (ConcatenatedVersionedXcm, xcm).encode();
|
||||
|
||||
let messages = vec![(ParaId::from(2000), 1u32, message_format.as_slice())];
|
||||
XcmpQueue::handle_xcmp_messages(repeat((para, 1, data.as_slice())).take(170), Weight::MAX);
|
||||
|
||||
// This shouldn't have executed the incoming XCM
|
||||
XcmpQueue::handle_xcmp_messages(messages.into_iter(), Weight::MAX);
|
||||
assert_eq!(EnqueuedMessages::get().len(), 170,);
|
||||
// Not yet suspended:
|
||||
assert!(InboundXcmpSuspended::<Test>::get().is_empty());
|
||||
// Enqueueing one more will suspend it:
|
||||
let xcm = VersionedXcm::<Test>::from(Xcm::<Test>(vec![ClearOrigin])).encode();
|
||||
let small = [ConcatenatedVersionedXcm.encode(), xcm].concat();
|
||||
|
||||
let queued_xcm = InboundXcmpMessages::<Test>::get(ParaId::from(2000), 1u32);
|
||||
assert_eq!(queued_xcm, xcm);
|
||||
XcmpQueue::handle_xcmp_messages(once((para, 1, small.as_slice())), Weight::MAX);
|
||||
// Suspended:
|
||||
assert_eq!(InboundXcmpSuspended::<Test>::get().iter().collect::<Vec<_>>(), vec![¶]);
|
||||
|
||||
// Now enqueueing many more will only work until the drop threshold:
|
||||
XcmpQueue::handle_xcmp_messages(repeat((para, 1, data.as_slice())).take(100), Weight::MAX);
|
||||
assert_eq!(mock::EnqueuedMessages::get().len(), 256);
|
||||
|
||||
crate::mock::EnqueueToLocalStorage::<Pallet<Test>>::sweep_queue(para);
|
||||
XcmpQueue::handle_xcmp_messages(once((para, 1, small.as_slice())), Weight::MAX);
|
||||
// Got resumed:
|
||||
assert!(InboundXcmpSuspended::<Test>::get().is_empty());
|
||||
// Still resumed:
|
||||
XcmpQueue::handle_xcmp_messages(once((para, 1, small.as_slice())), Weight::MAX);
|
||||
assert!(InboundXcmpSuspended::<Test>::get().is_empty());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_suspend_threshold_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
assert_eq!(data.suspend_threshold, 2);
|
||||
assert_ok!(XcmpQueue::update_suspend_threshold(RuntimeOrigin::root(), 3));
|
||||
assert_noop!(XcmpQueue::update_suspend_threshold(RuntimeOrigin::signed(2), 5), BadOrigin);
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
assert_eq!(<QueueConfig<Test>>::get().suspend_threshold, 32);
|
||||
assert_noop!(XcmpQueue::update_suspend_threshold(Origin::signed(2), 49), BadOrigin);
|
||||
|
||||
assert_eq!(data.suspend_threshold, 3);
|
||||
assert_ok!(XcmpQueue::update_suspend_threshold(Origin::root(), 33));
|
||||
assert_eq!(<QueueConfig<Test>>::get().suspend_threshold, 33);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_drop_threshold_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
assert_eq!(data.drop_threshold, 5);
|
||||
assert_ok!(XcmpQueue::update_drop_threshold(RuntimeOrigin::root(), 6));
|
||||
assert_noop!(XcmpQueue::update_drop_threshold(RuntimeOrigin::signed(2), 7), BadOrigin);
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
assert_eq!(<QueueConfig<Test>>::get().drop_threshold, 48);
|
||||
assert_ok!(XcmpQueue::update_drop_threshold(Origin::root(), 4000));
|
||||
assert_noop!(XcmpQueue::update_drop_threshold(Origin::signed(2), 7), BadOrigin);
|
||||
|
||||
assert_eq!(data.drop_threshold, 6);
|
||||
assert_eq!(<QueueConfig<Test>>::get().drop_threshold, 4000);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_resume_threshold_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
assert_eq!(data.resume_threshold, 1);
|
||||
assert_ok!(XcmpQueue::update_resume_threshold(RuntimeOrigin::root(), 2));
|
||||
assert_noop!(XcmpQueue::update_resume_threshold(RuntimeOrigin::signed(7), 3), BadOrigin);
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
|
||||
assert_eq!(data.resume_threshold, 2);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_threshold_weight_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
assert_eq!(data.threshold_weight, Weight::from_parts(100_000, 0));
|
||||
assert_ok!(XcmpQueue::update_threshold_weight(
|
||||
RuntimeOrigin::root(),
|
||||
Weight::from_parts(10_000, 0)
|
||||
));
|
||||
assert_eq!(<QueueConfig<Test>>::get().resume_threshold, 8);
|
||||
assert_noop!(
|
||||
XcmpQueue::update_threshold_weight(
|
||||
RuntimeOrigin::signed(5),
|
||||
Weight::from_parts(10_000_000, 0),
|
||||
),
|
||||
BadOrigin
|
||||
XcmpQueue::update_resume_threshold(Origin::root(), 0),
|
||||
Error::<Test>::BadQueueConfig
|
||||
);
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
|
||||
assert_eq!(data.threshold_weight, Weight::from_parts(10_000, 0));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_weight_restrict_decay_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
assert_eq!(data.weight_restrict_decay, Weight::from_parts(2, 0));
|
||||
assert_ok!(XcmpQueue::update_weight_restrict_decay(
|
||||
RuntimeOrigin::root(),
|
||||
Weight::from_parts(5, 0)
|
||||
));
|
||||
assert_noop!(
|
||||
XcmpQueue::update_weight_restrict_decay(
|
||||
RuntimeOrigin::signed(6),
|
||||
Weight::from_parts(4, 0),
|
||||
),
|
||||
BadOrigin
|
||||
XcmpQueue::update_resume_threshold(Origin::root(), 33),
|
||||
Error::<Test>::BadQueueConfig
|
||||
);
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
assert_ok!(XcmpQueue::update_resume_threshold(Origin::root(), 16));
|
||||
assert_noop!(XcmpQueue::update_resume_threshold(Origin::signed(7), 3), BadOrigin);
|
||||
|
||||
assert_eq!(data.weight_restrict_decay, Weight::from_parts(5, 0));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_xcmp_max_individual_weight() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
assert_eq!(
|
||||
data.xcmp_max_individual_weight,
|
||||
Weight::from_parts(20u64 * WEIGHT_REF_TIME_PER_MILLIS, DEFAULT_POV_SIZE),
|
||||
);
|
||||
assert_ok!(XcmpQueue::update_xcmp_max_individual_weight(
|
||||
RuntimeOrigin::root(),
|
||||
Weight::from_parts(30u64 * WEIGHT_REF_TIME_PER_MILLIS, 0)
|
||||
));
|
||||
assert_noop!(
|
||||
XcmpQueue::update_xcmp_max_individual_weight(
|
||||
RuntimeOrigin::signed(3),
|
||||
Weight::from_parts(10u64 * WEIGHT_REF_TIME_PER_MILLIS, 0)
|
||||
),
|
||||
BadOrigin
|
||||
);
|
||||
let data: QueueConfigData = <QueueConfig<Test>>::get();
|
||||
|
||||
assert_eq!(
|
||||
data.xcmp_max_individual_weight,
|
||||
Weight::from_parts(30u64 * WEIGHT_REF_TIME_PER_MILLIS, 0)
|
||||
);
|
||||
assert_eq!(<QueueConfig<Test>>::get().resume_threshold, 16);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -343,6 +421,287 @@ fn xcmp_queue_consumes_dest_and_msg_on_ok_validate() {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xcmp_queue_validate_nested_xcm_works() {
|
||||
let dest = (Parent, X1(Parachain(5555)));
|
||||
// Message that is not too deeply nested:
|
||||
let mut good = Xcm(vec![ClearOrigin]);
|
||||
for _ in 0..MAX_XCM_DECODE_DEPTH - 1 {
|
||||
good = Xcm(vec![SetAppendix(good)]);
|
||||
}
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
// Check that the good message is validated:
|
||||
assert_ok!(<XcmpQueue as SendXcm>::validate(
|
||||
&mut Some(dest.into()),
|
||||
&mut Some(good.clone())
|
||||
));
|
||||
|
||||
// Nesting the message one more time should reject it:
|
||||
let bad = Xcm(vec![SetAppendix(good)]);
|
||||
assert_eq!(
|
||||
Err(SendError::ExceedsMaxMessageSize),
|
||||
<XcmpQueue as SendXcm>::validate(&mut Some(dest.into()), &mut Some(bad))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn send_xcm_nested_works() {
|
||||
let dest = (Parent, X1(Parachain(HRMP_PARA_ID)));
|
||||
// Message that is not too deeply nested:
|
||||
let mut good = Xcm(vec![ClearOrigin]);
|
||||
for _ in 0..MAX_XCM_DECODE_DEPTH - 1 {
|
||||
good = Xcm(vec![SetAppendix(good)]);
|
||||
}
|
||||
|
||||
// Check that the good message is sent:
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(send_xcm::<XcmpQueue>(dest.into(), good.clone()));
|
||||
assert_eq!(
|
||||
XcmpQueue::take_outbound_messages(usize::MAX),
|
||||
vec![(
|
||||
HRMP_PARA_ID.into(),
|
||||
(XcmpMessageFormat::ConcatenatedVersionedXcm, VersionedXcm::V3(good.clone()))
|
||||
.encode(),
|
||||
)]
|
||||
);
|
||||
});
|
||||
|
||||
// Nesting the message one more time should not send it:
|
||||
let bad = Xcm(vec![SetAppendix(good)]);
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_err!(send_xcm::<XcmpQueue>(dest.into(), bad), SendError::ExceedsMaxMessageSize);
|
||||
assert!(XcmpQueue::take_outbound_messages(usize::MAX).is_empty());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hrmp_signals_are_prioritized() {
|
||||
let message = Xcm(vec![Trap(5)]);
|
||||
|
||||
let sibling_para_id = ParaId::from(12345);
|
||||
let dest = (Parent, X1(Parachain(sibling_para_id.into())));
|
||||
let mut dest_wrapper = Some(dest.into());
|
||||
let mut msg_wrapper = Some(message.clone());
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
frame_system::Pallet::<Test>::set_block_number(1);
|
||||
<XcmpQueue as SendXcm>::validate(&mut dest_wrapper, &mut msg_wrapper).unwrap();
|
||||
|
||||
// check wrapper were consumed
|
||||
assert_eq!(None, dest_wrapper.take());
|
||||
assert_eq!(None, msg_wrapper.take());
|
||||
|
||||
ParachainSystem::open_custom_outbound_hrmp_channel_for_benchmarks_or_tests(
|
||||
sibling_para_id,
|
||||
cumulus_primitives_core::AbridgedHrmpChannel {
|
||||
max_capacity: 128,
|
||||
max_total_size: 1 << 16,
|
||||
max_message_size: 128,
|
||||
msg_count: 0,
|
||||
total_size: 0,
|
||||
mqc_head: None,
|
||||
},
|
||||
);
|
||||
|
||||
let taken = XcmpQueue::take_outbound_messages(130);
|
||||
assert_eq!(taken, vec![]);
|
||||
|
||||
// Enqueue some messages
|
||||
let num_events = frame_system::Pallet::<Test>::events().len();
|
||||
for _ in 0..256 {
|
||||
assert_ok!(send_xcm::<XcmpQueue>(dest.into(), message.clone()));
|
||||
}
|
||||
assert_eq!(num_events + 256, frame_system::Pallet::<Test>::events().len());
|
||||
|
||||
// Without a signal we get the messages in order:
|
||||
let mut expected_msg = XcmpMessageFormat::ConcatenatedVersionedXcm.encode();
|
||||
for _ in 0..31 {
|
||||
expected_msg.extend(VersionedXcm::V3(message.clone()).encode());
|
||||
}
|
||||
|
||||
hypothetically!({
|
||||
let taken = XcmpQueue::take_outbound_messages(usize::MAX);
|
||||
assert_eq!(taken, vec![(sibling_para_id.into(), expected_msg,)]);
|
||||
});
|
||||
|
||||
// But a signal gets prioritized instead of the messages:
|
||||
XcmpQueue::send_signal(sibling_para_id.into(), ChannelSignal::Suspend);
|
||||
|
||||
let taken = XcmpQueue::take_outbound_messages(130);
|
||||
assert_eq!(
|
||||
taken,
|
||||
vec![(
|
||||
sibling_para_id.into(),
|
||||
(XcmpMessageFormat::Signals, ChannelSignal::Suspend).encode()
|
||||
)]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maybe_double_encoded_versioned_xcm_works() {
|
||||
// pre conditions
|
||||
assert_eq!(VersionedXcm::<()>::V2(Default::default()).encode(), &[2, 0]);
|
||||
assert_eq!(VersionedXcm::<()>::V3(Default::default()).encode(), &[3, 0]);
|
||||
}
|
||||
|
||||
// Now also testing a page instead of just concat messages.
|
||||
#[test]
|
||||
fn maybe_double_encoded_versioned_xcm_decode_page_works() {
|
||||
let page = mk_page();
|
||||
|
||||
// Now try to decode the page.
|
||||
let input = &mut &page[..];
|
||||
for i in 0..100 {
|
||||
match (i % 2, VersionedXcm::<()>::decode(input)) {
|
||||
(0, Ok(xcm)) => {
|
||||
assert_eq!(xcm, v2_xcm());
|
||||
},
|
||||
(1, Ok(xcm)) => {
|
||||
assert_eq!(xcm, v3_xcm());
|
||||
},
|
||||
unexpected => unreachable!("{:?}", unexpected),
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(input.remaining_len(), Ok(Some(0)), "All data consumed");
|
||||
}
|
||||
|
||||
/// Check that `take_first_concatenated_xcm` correctly splits a page into its XCMs.
|
||||
#[test]
|
||||
fn take_first_concatenated_xcm_works() {
|
||||
let page = mk_page();
|
||||
let input = &mut &page[..];
|
||||
|
||||
for i in 0..100 {
|
||||
let xcm = XcmpQueue::take_first_concatenated_xcm(input, &mut WeightMeter::new()).unwrap();
|
||||
match (i % 2, xcm) {
|
||||
(0, data) | (2, data) => {
|
||||
assert_eq!(data, v2_xcm().encode());
|
||||
},
|
||||
(1, data) | (3, data) => {
|
||||
assert_eq!(data, v3_xcm().encode());
|
||||
},
|
||||
unexpected => unreachable!("{:?}", unexpected),
|
||||
}
|
||||
}
|
||||
assert_eq!(input.remaining_len(), Ok(Some(0)), "All data consumed");
|
||||
}
|
||||
|
||||
/// A message that is not too deeply nested will be accepted by `take_first_concatenated_xcm`.
|
||||
#[test]
|
||||
fn take_first_concatenated_xcm_good_recursion_depth_works() {
|
||||
let mut good = Xcm::<()>(vec![ClearOrigin]);
|
||||
for _ in 0..MAX_XCM_DECODE_DEPTH - 1 {
|
||||
good = Xcm(vec![SetAppendix(good)]);
|
||||
}
|
||||
let good = VersionedXcm::V3(good);
|
||||
|
||||
let page = good.encode();
|
||||
assert_ok!(XcmpQueue::take_first_concatenated_xcm(&mut &page[..], &mut WeightMeter::new()));
|
||||
}
|
||||
|
||||
/// A message that is too deeply nested will be rejected by `take_first_concatenated_xcm`.
|
||||
#[test]
|
||||
fn take_first_concatenated_xcm_good_bad_depth_errors() {
|
||||
let mut bad = Xcm::<()>(vec![ClearOrigin]);
|
||||
for _ in 0..MAX_XCM_DECODE_DEPTH {
|
||||
bad = Xcm(vec![SetAppendix(bad)]);
|
||||
}
|
||||
let bad = VersionedXcm::V3(bad);
|
||||
|
||||
let page = bad.encode();
|
||||
assert_err!(
|
||||
XcmpQueue::take_first_concatenated_xcm(&mut &page[..], &mut WeightMeter::new()),
|
||||
()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lazy_migration_works() {
|
||||
use crate::migration::v3::*;
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
EnqueuedMessages::set(vec![]);
|
||||
let _g = StorageNoopGuard::default(); // No storage is leaked.
|
||||
|
||||
let mut channels = vec![];
|
||||
for i in 0..20 {
|
||||
let para = ParaId::from(i);
|
||||
let mut message_metadata = vec![];
|
||||
for block in 0..30 {
|
||||
message_metadata.push((block, XcmpMessageFormat::ConcatenatedVersionedXcm));
|
||||
InboundXcmpMessages::<Test>::insert(para, block, vec![(i + block) as u8]);
|
||||
}
|
||||
|
||||
channels.push(InboundChannelDetails {
|
||||
sender: para,
|
||||
state: InboundState::Ok,
|
||||
message_metadata,
|
||||
});
|
||||
}
|
||||
InboundXcmpStatus::<Test>::set(Some(channels));
|
||||
|
||||
for para in 0..20u32 {
|
||||
assert_eq!(InboundXcmpStatus::<Test>::get().unwrap().len() as u32, 20 - para);
|
||||
assert_eq!(InboundXcmpMessages::<Test>::iter_prefix(ParaId::from(para)).count(), 30);
|
||||
|
||||
for block in 0..30 {
|
||||
XcmpQueue::on_idle(0u32.into(), Weight::MAX);
|
||||
assert_eq!(
|
||||
EnqueuedMessages::get(),
|
||||
vec![(para.into(), vec![(29 - block + para) as u8])]
|
||||
);
|
||||
EnqueuedMessages::set(vec![]);
|
||||
}
|
||||
// One more to jump to the next channel:
|
||||
XcmpQueue::on_idle(0u32.into(), Weight::MAX);
|
||||
|
||||
assert_eq!(InboundXcmpStatus::<Test>::get().unwrap().len() as u32, 20 - para - 1);
|
||||
assert_eq!(InboundXcmpMessages::<Test>::iter_prefix(ParaId::from(para)).count(), 0);
|
||||
}
|
||||
// One more for the cleanup:
|
||||
XcmpQueue::on_idle(0u32.into(), Weight::MAX);
|
||||
|
||||
assert!(!InboundXcmpStatus::<Test>::exists());
|
||||
assert_eq!(InboundXcmpMessages::<Test>::iter().count(), 0);
|
||||
EnqueuedMessages::set(vec![]);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lazy_migration_noop_when_out_of_weight() {
|
||||
use crate::migration::v3::*;
|
||||
assert!(!XcmpQueue::on_idle_weight().is_zero(), "pre condition");
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
let _g = StorageNoopGuard::default(); // No storage is leaked.
|
||||
let block = 5;
|
||||
let para = ParaId::from(4);
|
||||
let message_metadata = vec![(block, XcmpMessageFormat::ConcatenatedVersionedXcm)];
|
||||
|
||||
InboundXcmpMessages::<Test>::insert(para, block, vec![123u8]);
|
||||
InboundXcmpStatus::<Test>::set(Some(vec![InboundChannelDetails {
|
||||
sender: para,
|
||||
state: InboundState::Ok,
|
||||
message_metadata,
|
||||
}]));
|
||||
|
||||
// Hypothetically, it would do something with enough weight limit:
|
||||
hypothetically!({
|
||||
XcmpQueue::on_idle(0u32.into(), Weight::MAX);
|
||||
assert_eq!(EnqueuedMessages::get(), vec![(para, vec![123u8])]);
|
||||
});
|
||||
// But does not, since the limit is zero:
|
||||
assert_storage_noop!({ XcmpQueue::on_idle(0u32.into(), Weight::zero()) });
|
||||
|
||||
InboundXcmpMessages::<Test>::remove(para, block);
|
||||
InboundXcmpStatus::<Test>::kill();
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xcmp_queue_send_xcm_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
|
||||
@@ -13,51 +13,239 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Autogenerated weights for `cumulus_pallet_xcmp_queue`
|
||||
//!
|
||||
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
|
||||
//! DATE: 2023-09-15, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
|
||||
//! WORST CASE MAP SIZE: `1000000`
|
||||
//! HOSTNAME: `Olivers-MacBook-Pro.local`, CPU: `<UNKNOWN>`
|
||||
//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("asset-hub-kusama-dev")`, DB CACHE: `1024`
|
||||
|
||||
// Executed Command:
|
||||
// ./target/release/polkadot-parachain
|
||||
// benchmark
|
||||
// pallet
|
||||
// --pallet
|
||||
// cumulus-pallet-xcmp-queue
|
||||
// --chain
|
||||
// asset-hub-kusama-dev
|
||||
// --output
|
||||
// cumulus/pallets/xcmp-queue/src/weights.rs
|
||||
// --template
|
||||
// substrate/.maintain/frame-weight-template.hbs
|
||||
// --extrinsic
|
||||
//
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
#![allow(unused_parens)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use frame_support::{
|
||||
traits::Get,
|
||||
weights::{constants::RocksDbWeight, Weight},
|
||||
};
|
||||
use sp_std::marker::PhantomData;
|
||||
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
|
||||
use core::marker::PhantomData;
|
||||
|
||||
// Implemented by autogenerated benchmarking code.
|
||||
/// Weight functions needed for `cumulus_pallet_xcmp_queue`.
|
||||
pub trait WeightInfo {
|
||||
fn set_config_with_u32() -> Weight;
|
||||
fn set_config_with_weight() -> Weight;
|
||||
fn enqueue_xcmp_message() -> Weight;
|
||||
fn suspend_channel() -> Weight;
|
||||
fn resume_channel() -> Weight;
|
||||
fn take_first_concatenated_xcm() -> Weight;
|
||||
fn on_idle_good_msg() -> Weight;
|
||||
fn on_idle_large_msg() -> Weight;
|
||||
}
|
||||
|
||||
/// Weights for `cumulus_pallet_xcmp_queue` using the Substrate node and recommended hardware.
|
||||
pub struct SubstrateWeight<T>(PhantomData<T>);
|
||||
|
||||
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
|
||||
// Storage: XcmpQueue QueueConfig (r:1 w:1)
|
||||
/// Storage: `XcmpQueue::QueueConfig` (r:1 w:1)
|
||||
/// Proof: `XcmpQueue::QueueConfig` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
fn set_config_with_u32() -> Weight {
|
||||
Weight::from_parts(2_717_000_u64, 0)
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `76`
|
||||
// Estimated: `1561`
|
||||
// Minimum execution time: 5_000_000 picoseconds.
|
||||
Weight::from_parts(6_000_000, 1561)
|
||||
.saturating_add(T::DbWeight::get().reads(1_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(1_u64))
|
||||
}
|
||||
|
||||
// Storage: XcmpQueue QueueConfig (r:1 w:1)
|
||||
fn set_config_with_weight() -> Weight {
|
||||
Weight::from_parts(2_717_000_u64, 0)
|
||||
/// Storage: `XcmpQueue::QueueConfig` (r:1 w:0)
|
||||
/// Proof: `XcmpQueue::QueueConfig` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
/// Storage: `MessageQueue::BookStateFor` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::BookStateFor` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::ServiceHead` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::ServiceHead` (`max_values`: Some(1), `max_size`: Some(5), added: 500, mode: `MaxEncodedLen`)
|
||||
/// Storage: `XcmpQueue::InboundXcmpSuspended` (r:1 w:0)
|
||||
/// Proof: `XcmpQueue::InboundXcmpSuspended` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
/// Storage: `MessageQueue::Pages` (r:0 w:1)
|
||||
/// Proof: `MessageQueue::Pages` (`max_values`: None, `max_size`: Some(65585), added: 68060, mode: `MaxEncodedLen`)
|
||||
fn enqueue_xcmp_message() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `118`
|
||||
// Estimated: `3517`
|
||||
// Minimum execution time: 15_000_000 picoseconds.
|
||||
Weight::from_parts(16_000_000, 3517)
|
||||
.saturating_add(T::DbWeight::get().reads(4_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(3_u64))
|
||||
}
|
||||
/// Storage: `XcmpQueue::OutboundXcmpStatus` (r:1 w:1)
|
||||
/// Proof: `XcmpQueue::OutboundXcmpStatus` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
fn suspend_channel() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `76`
|
||||
// Estimated: `1561`
|
||||
// Minimum execution time: 3_000_000 picoseconds.
|
||||
Weight::from_parts(4_000_000, 1561)
|
||||
.saturating_add(T::DbWeight::get().reads(1_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(1_u64))
|
||||
}
|
||||
/// Storage: `XcmpQueue::OutboundXcmpStatus` (r:1 w:1)
|
||||
/// Proof: `XcmpQueue::OutboundXcmpStatus` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
fn resume_channel() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `111`
|
||||
// Estimated: `1596`
|
||||
// Minimum execution time: 4_000_000 picoseconds.
|
||||
Weight::from_parts(4_000_000, 1596)
|
||||
.saturating_add(T::DbWeight::get().reads(1_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(1_u64))
|
||||
}
|
||||
fn take_first_concatenated_xcm() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `0`
|
||||
// Estimated: `0`
|
||||
// Minimum execution time: 44_000_000 picoseconds.
|
||||
Weight::from_parts(45_000_000, 0)
|
||||
}
|
||||
/// Storage: UNKNOWN KEY `0x7b3237373ffdfeb1cab4222e3b520d6b345d8e88afa015075c945637c07e8f20` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0x7b3237373ffdfeb1cab4222e3b520d6b345d8e88afa015075c945637c07e8f20` (r:1 w:1)
|
||||
/// Storage: `XcmpQueue::InboundXcmpMessages` (r:1 w:1)
|
||||
/// Proof: `XcmpQueue::InboundXcmpMessages` (`max_values`: None, `max_size`: None, mode: `Measured`)
|
||||
/// Storage: `MessageQueue::BookStateFor` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::BookStateFor` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::ServiceHead` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::ServiceHead` (`max_values`: Some(1), `max_size`: Some(5), added: 500, mode: `MaxEncodedLen`)
|
||||
/// Storage: `XcmpQueue::QueueConfig` (r:1 w:0)
|
||||
/// Proof: `XcmpQueue::QueueConfig` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
/// Storage: `XcmpQueue::InboundXcmpSuspended` (r:1 w:0)
|
||||
/// Proof: `XcmpQueue::InboundXcmpSuspended` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
/// Storage: `MessageQueue::Pages` (r:0 w:1)
|
||||
/// Proof: `MessageQueue::Pages` (`max_values`: None, `max_size`: Some(65585), added: 68060, mode: `MaxEncodedLen`)
|
||||
fn on_idle_good_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65747`
|
||||
// Estimated: `69212`
|
||||
// Minimum execution time: 63_000_000 picoseconds.
|
||||
Weight::from_parts(66_000_000, 69212)
|
||||
.saturating_add(T::DbWeight::get().reads(6_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(5_u64))
|
||||
}
|
||||
/// Storage: UNKNOWN KEY `0x7b3237373ffdfeb1cab4222e3b520d6b345d8e88afa015075c945637c07e8f20` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0x7b3237373ffdfeb1cab4222e3b520d6b345d8e88afa015075c945637c07e8f20` (r:1 w:1)
|
||||
fn on_idle_large_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65710`
|
||||
// Estimated: `69175`
|
||||
// Minimum execution time: 42_000_000 picoseconds.
|
||||
Weight::from_parts(52_000_000, 69175)
|
||||
.saturating_add(T::DbWeight::get().reads(2_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(2_u64))
|
||||
}
|
||||
}
|
||||
|
||||
// For backwards compatibility and tests.
|
||||
impl WeightInfo for () {
|
||||
// Storage: XcmpQueue QueueConfig (r:1 w:1)
|
||||
/// Storage: `XcmpQueue::QueueConfig` (r:1 w:1)
|
||||
/// Proof: `XcmpQueue::QueueConfig` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
fn set_config_with_u32() -> Weight {
|
||||
Weight::from_parts(2_717_000_u64, 0)
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `76`
|
||||
// Estimated: `1561`
|
||||
// Minimum execution time: 5_000_000 picoseconds.
|
||||
Weight::from_parts(6_000_000, 1561)
|
||||
.saturating_add(RocksDbWeight::get().reads(1_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(1_u64))
|
||||
}
|
||||
|
||||
// Storage: XcmpQueue QueueConfig (r:1 w:1)
|
||||
fn set_config_with_weight() -> Weight {
|
||||
Weight::from_parts(2_717_000_u64, 0)
|
||||
/// Storage: `XcmpQueue::QueueConfig` (r:1 w:0)
|
||||
/// Proof: `XcmpQueue::QueueConfig` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
/// Storage: `MessageQueue::BookStateFor` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::BookStateFor` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::ServiceHead` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::ServiceHead` (`max_values`: Some(1), `max_size`: Some(5), added: 500, mode: `MaxEncodedLen`)
|
||||
/// Storage: `XcmpQueue::InboundXcmpSuspended` (r:1 w:0)
|
||||
/// Proof: `XcmpQueue::InboundXcmpSuspended` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
/// Storage: `MessageQueue::Pages` (r:0 w:1)
|
||||
/// Proof: `MessageQueue::Pages` (`max_values`: None, `max_size`: Some(65585), added: 68060, mode: `MaxEncodedLen`)
|
||||
fn enqueue_xcmp_message() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `118`
|
||||
// Estimated: `3517`
|
||||
// Minimum execution time: 15_000_000 picoseconds.
|
||||
Weight::from_parts(16_000_000, 3517)
|
||||
.saturating_add(RocksDbWeight::get().reads(4_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(3_u64))
|
||||
}
|
||||
/// Storage: `XcmpQueue::OutboundXcmpStatus` (r:1 w:1)
|
||||
/// Proof: `XcmpQueue::OutboundXcmpStatus` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
fn suspend_channel() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `76`
|
||||
// Estimated: `1561`
|
||||
// Minimum execution time: 3_000_000 picoseconds.
|
||||
Weight::from_parts(4_000_000, 1561)
|
||||
.saturating_add(RocksDbWeight::get().reads(1_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(1_u64))
|
||||
}
|
||||
/// Storage: `XcmpQueue::OutboundXcmpStatus` (r:1 w:1)
|
||||
/// Proof: `XcmpQueue::OutboundXcmpStatus` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
fn resume_channel() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `111`
|
||||
// Estimated: `1596`
|
||||
// Minimum execution time: 4_000_000 picoseconds.
|
||||
Weight::from_parts(4_000_000, 1596)
|
||||
.saturating_add(RocksDbWeight::get().reads(1_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(1_u64))
|
||||
}
|
||||
fn take_first_concatenated_xcm() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `0`
|
||||
// Estimated: `0`
|
||||
// Minimum execution time: 44_000_000 picoseconds.
|
||||
Weight::from_parts(45_000_000, 0)
|
||||
}
|
||||
/// Storage: UNKNOWN KEY `0x7b3237373ffdfeb1cab4222e3b520d6b345d8e88afa015075c945637c07e8f20` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0x7b3237373ffdfeb1cab4222e3b520d6b345d8e88afa015075c945637c07e8f20` (r:1 w:1)
|
||||
/// Storage: `XcmpQueue::InboundXcmpMessages` (r:1 w:1)
|
||||
/// Proof: `XcmpQueue::InboundXcmpMessages` (`max_values`: None, `max_size`: None, mode: `Measured`)
|
||||
/// Storage: `MessageQueue::BookStateFor` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::BookStateFor` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
|
||||
/// Storage: `MessageQueue::ServiceHead` (r:1 w:1)
|
||||
/// Proof: `MessageQueue::ServiceHead` (`max_values`: Some(1), `max_size`: Some(5), added: 500, mode: `MaxEncodedLen`)
|
||||
/// Storage: `XcmpQueue::QueueConfig` (r:1 w:0)
|
||||
/// Proof: `XcmpQueue::QueueConfig` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
/// Storage: `XcmpQueue::InboundXcmpSuspended` (r:1 w:0)
|
||||
/// Proof: `XcmpQueue::InboundXcmpSuspended` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
|
||||
/// Storage: `MessageQueue::Pages` (r:0 w:1)
|
||||
/// Proof: `MessageQueue::Pages` (`max_values`: None, `max_size`: Some(65585), added: 68060, mode: `MaxEncodedLen`)
|
||||
fn on_idle_good_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65747`
|
||||
// Estimated: `69212`
|
||||
// Minimum execution time: 63_000_000 picoseconds.
|
||||
Weight::from_parts(66_000_000, 69212)
|
||||
.saturating_add(RocksDbWeight::get().reads(6_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(5_u64))
|
||||
}
|
||||
/// Storage: UNKNOWN KEY `0x7b3237373ffdfeb1cab4222e3b520d6b345d8e88afa015075c945637c07e8f20` (r:1 w:1)
|
||||
/// Proof: UNKNOWN KEY `0x7b3237373ffdfeb1cab4222e3b520d6b345d8e88afa015075c945637c07e8f20` (r:1 w:1)
|
||||
fn on_idle_large_msg() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `65710`
|
||||
// Estimated: `69175`
|
||||
// Minimum execution time: 42_000_000 picoseconds.
|
||||
Weight::from_parts(52_000_000, 69175)
|
||||
.saturating_add(RocksDbWeight::get().reads(2_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(2_u64))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user