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.

![Untitled
(1)](https://github.com/paritytech/cumulus/assets/10380170/6cf8b377-88c9-4aed-96df-baace266e04d)

## 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:
Oliver Tale-Yazdi
2023-11-02 15:31:38 +01:00
committed by GitHub
parent 7df0417bcd
commit e1c033ebe1
277 changed files with 11604 additions and 4733 deletions
@@ -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
}
}
+51 -30
View File
@@ -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();
}
}
}
}
+177 -531
View File
@@ -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))
}
}