mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-07 02:37:22 +00:00
e1c033ebe1
(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 <>
420 lines
12 KiB
Rust
420 lines
12 KiB
Rust
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// This file is part of Polkadot.
|
|
|
|
// Polkadot 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.
|
|
|
|
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! Primitive types which are strictly necessary from a parachain-execution point
|
|
//! of view.
|
|
|
|
use sp_std::vec::Vec;
|
|
|
|
use bounded_collections::{BoundedVec, ConstU32};
|
|
use parity_scale_codec::{CompactAs, Decode, Encode, MaxEncodedLen};
|
|
use scale_info::TypeInfo;
|
|
use serde::{Deserialize, Serialize};
|
|
use sp_core::{bytes, RuntimeDebug, TypeId};
|
|
use sp_runtime::traits::Hash as _;
|
|
use sp_weights::Weight;
|
|
|
|
use polkadot_core_primitives::{Hash, OutboundHrmpMessage};
|
|
|
|
/// Block number type used by the relay chain.
|
|
pub use polkadot_core_primitives::BlockNumber as RelayChainBlockNumber;
|
|
|
|
/// Parachain head data included in the chain.
|
|
#[derive(
|
|
PartialEq,
|
|
Eq,
|
|
Clone,
|
|
PartialOrd,
|
|
Ord,
|
|
Encode,
|
|
Decode,
|
|
RuntimeDebug,
|
|
derive_more::From,
|
|
TypeInfo,
|
|
Serialize,
|
|
Deserialize,
|
|
)]
|
|
#[cfg_attr(feature = "std", derive(Hash, Default))]
|
|
pub struct HeadData(#[serde(with = "bytes")] pub Vec<u8>);
|
|
|
|
impl HeadData {
|
|
/// Returns the hash of this head data.
|
|
pub fn hash(&self) -> Hash {
|
|
sp_runtime::traits::BlakeTwo256::hash(&self.0)
|
|
}
|
|
}
|
|
|
|
/// Parachain validation code.
|
|
#[derive(
|
|
PartialEq,
|
|
Eq,
|
|
Clone,
|
|
Encode,
|
|
Decode,
|
|
RuntimeDebug,
|
|
derive_more::From,
|
|
TypeInfo,
|
|
Serialize,
|
|
Deserialize,
|
|
)]
|
|
#[cfg_attr(feature = "std", derive(Hash))]
|
|
pub struct ValidationCode(#[serde(with = "bytes")] pub Vec<u8>);
|
|
|
|
impl ValidationCode {
|
|
/// Get the blake2-256 hash of the validation code bytes.
|
|
pub fn hash(&self) -> ValidationCodeHash {
|
|
ValidationCodeHash(sp_runtime::traits::BlakeTwo256::hash(&self.0[..]))
|
|
}
|
|
}
|
|
|
|
/// Unit type wrapper around [`type@Hash`] that represents the blake2-256 hash
|
|
/// of validation code in particular.
|
|
///
|
|
/// This type is produced by [`ValidationCode::hash`].
|
|
///
|
|
/// This type makes it easy to enforce that a hash is a validation code hash on the type level.
|
|
#[derive(Clone, Copy, Encode, Decode, Hash, Eq, PartialEq, PartialOrd, Ord, TypeInfo)]
|
|
pub struct ValidationCodeHash(Hash);
|
|
|
|
impl sp_std::fmt::Display for ValidationCodeHash {
|
|
fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
|
|
self.0.fmt(f)
|
|
}
|
|
}
|
|
|
|
impl sp_std::fmt::Debug for ValidationCodeHash {
|
|
fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
|
|
write!(f, "{:?}", self.0)
|
|
}
|
|
}
|
|
|
|
impl AsRef<[u8]> for ValidationCodeHash {
|
|
fn as_ref(&self) -> &[u8] {
|
|
self.0.as_ref()
|
|
}
|
|
}
|
|
|
|
impl From<Hash> for ValidationCodeHash {
|
|
fn from(hash: Hash) -> ValidationCodeHash {
|
|
ValidationCodeHash(hash)
|
|
}
|
|
}
|
|
|
|
impl From<[u8; 32]> for ValidationCodeHash {
|
|
fn from(hash: [u8; 32]) -> ValidationCodeHash {
|
|
ValidationCodeHash(hash.into())
|
|
}
|
|
}
|
|
|
|
impl sp_std::fmt::LowerHex for ValidationCodeHash {
|
|
fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
|
|
sp_std::fmt::LowerHex::fmt(&self.0, f)
|
|
}
|
|
}
|
|
|
|
/// Parachain block data.
|
|
///
|
|
/// Contains everything required to validate para-block, may contain block and witness data.
|
|
#[derive(PartialEq, Eq, Clone, Encode, Decode, derive_more::From, TypeInfo, RuntimeDebug)]
|
|
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
|
pub struct BlockData(#[cfg_attr(feature = "std", serde(with = "bytes"))] pub Vec<u8>);
|
|
|
|
/// Unique identifier of a parachain.
|
|
#[derive(
|
|
Clone,
|
|
CompactAs,
|
|
Copy,
|
|
Decode,
|
|
Default,
|
|
Encode,
|
|
Eq,
|
|
Hash,
|
|
MaxEncodedLen,
|
|
Ord,
|
|
PartialEq,
|
|
PartialOrd,
|
|
RuntimeDebug,
|
|
serde::Serialize,
|
|
serde::Deserialize,
|
|
TypeInfo,
|
|
)]
|
|
#[cfg_attr(feature = "std", derive(derive_more::Display))]
|
|
pub struct Id(u32);
|
|
|
|
impl TypeId for Id {
|
|
const TYPE_ID: [u8; 4] = *b"para";
|
|
}
|
|
|
|
impl From<Id> for u32 {
|
|
fn from(x: Id) -> Self {
|
|
x.0
|
|
}
|
|
}
|
|
|
|
impl From<u32> for Id {
|
|
fn from(x: u32) -> Self {
|
|
Id(x)
|
|
}
|
|
}
|
|
|
|
impl From<usize> for Id {
|
|
fn from(x: usize) -> Self {
|
|
// can't panic, so need to truncate
|
|
let x = x.try_into().unwrap_or(u32::MAX);
|
|
Id(x)
|
|
}
|
|
}
|
|
|
|
// When we added a second From impl for Id, type inference could no longer
|
|
// determine which impl should apply for things like `5.into()`. It therefore
|
|
// raised a bunch of errors in our test code, scattered throughout the
|
|
// various modules' tests, that there is no impl of `From<i32>` (`i32` being
|
|
// the default numeric type).
|
|
//
|
|
// We can't use `cfg(test)` here, because that configuration directive does not
|
|
// propagate between crates, which would fail to fix tests in crates other than
|
|
// this one.
|
|
//
|
|
// Instead, let's take advantage of the observation that what really matters for a
|
|
// ParaId within a test context is that it is unique and constant. I believe that
|
|
// there is no case where someone does `(-1).into()` anyway, but if they do, it
|
|
// never matters whether the actual contained ID is `-1` or `4294967295`. Nobody
|
|
// does arithmetic on a `ParaId`; doing so would be a bug.
|
|
impl From<i32> for Id {
|
|
fn from(x: i32) -> Self {
|
|
Id(x as u32)
|
|
}
|
|
}
|
|
|
|
// System parachain ID is considered `< 2000`.
|
|
const SYSTEM_INDEX_END: u32 = 1999;
|
|
const PUBLIC_INDEX_START: u32 = 2000;
|
|
|
|
/// The ID of the first publicly registrable parachain.
|
|
pub const LOWEST_PUBLIC_ID: Id = Id(PUBLIC_INDEX_START);
|
|
|
|
impl Id {
|
|
/// Create an `Id`.
|
|
pub const fn new(id: u32) -> Self {
|
|
Self(id)
|
|
}
|
|
}
|
|
|
|
/// Determine if a parachain is a system parachain or not.
|
|
pub trait IsSystem {
|
|
/// Returns `true` if a parachain is a system parachain, `false` otherwise.
|
|
fn is_system(&self) -> bool;
|
|
}
|
|
|
|
impl IsSystem for Id {
|
|
fn is_system(&self) -> bool {
|
|
self.0 <= SYSTEM_INDEX_END
|
|
}
|
|
}
|
|
|
|
impl sp_std::ops::Add<u32> for Id {
|
|
type Output = Self;
|
|
|
|
fn add(self, other: u32) -> Self {
|
|
Self(self.0 + other)
|
|
}
|
|
}
|
|
|
|
impl sp_std::ops::Sub<u32> for Id {
|
|
type Output = Self;
|
|
|
|
fn sub(self, other: u32) -> Self {
|
|
Self(self.0 - other)
|
|
}
|
|
}
|
|
|
|
#[derive(
|
|
Clone, Copy, Default, Encode, Decode, Eq, PartialEq, Ord, PartialOrd, RuntimeDebug, TypeInfo,
|
|
)]
|
|
pub struct Sibling(pub Id);
|
|
|
|
impl From<Id> for Sibling {
|
|
fn from(i: Id) -> Self {
|
|
Self(i)
|
|
}
|
|
}
|
|
|
|
impl From<Sibling> for Id {
|
|
fn from(i: Sibling) -> Self {
|
|
i.0
|
|
}
|
|
}
|
|
|
|
impl AsRef<Id> for Sibling {
|
|
fn as_ref(&self) -> &Id {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl TypeId for Sibling {
|
|
const TYPE_ID: [u8; 4] = *b"sibl";
|
|
}
|
|
|
|
impl From<Sibling> for u32 {
|
|
fn from(x: Sibling) -> Self {
|
|
x.0.into()
|
|
}
|
|
}
|
|
|
|
impl From<u32> for Sibling {
|
|
fn from(x: u32) -> Self {
|
|
Sibling(x.into())
|
|
}
|
|
}
|
|
|
|
impl IsSystem for Sibling {
|
|
fn is_system(&self) -> bool {
|
|
IsSystem::is_system(&self.0)
|
|
}
|
|
}
|
|
|
|
/// A type that uniquely identifies an HRMP channel. An HRMP channel is established between two
|
|
/// paras. In text, we use the notation `(A, B)` to specify a channel between A and B. The channels
|
|
/// are unidirectional, meaning that `(A, B)` and `(B, A)` refer to different channels. The
|
|
/// convention is that we use the first item tuple for the sender and the second for the recipient.
|
|
/// Only one channel is allowed between two participants in one direction, i.e. there cannot be 2
|
|
/// different channels identified by `(A, B)`. A channel with the same para id in sender and
|
|
/// recipient is invalid. That is, however, not enforced.
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, RuntimeDebug, TypeInfo)]
|
|
#[cfg_attr(feature = "std", derive(Hash))]
|
|
pub struct HrmpChannelId {
|
|
/// The para that acts as the sender in this channel.
|
|
pub sender: Id,
|
|
/// The para that acts as the recipient in this channel.
|
|
pub recipient: Id,
|
|
}
|
|
|
|
impl HrmpChannelId {
|
|
/// Returns true if the given id corresponds to either the sender or the recipient.
|
|
pub fn is_participant(&self, id: Id) -> bool {
|
|
id == self.sender || id == self.recipient
|
|
}
|
|
}
|
|
|
|
/// A message from a parachain to its Relay Chain.
|
|
pub type UpwardMessage = Vec<u8>;
|
|
|
|
/// Something that should be called when a downward message is received.
|
|
pub trait DmpMessageHandler {
|
|
/// Handle some incoming DMP messages (note these are individual XCM messages).
|
|
///
|
|
/// Also, process messages up to some `max_weight`.
|
|
fn handle_dmp_messages(
|
|
iter: impl Iterator<Item = (RelayChainBlockNumber, Vec<u8>)>,
|
|
max_weight: Weight,
|
|
) -> Weight;
|
|
}
|
|
impl DmpMessageHandler for () {
|
|
fn handle_dmp_messages(
|
|
iter: impl Iterator<Item = (RelayChainBlockNumber, Vec<u8>)>,
|
|
_max_weight: Weight,
|
|
) -> Weight {
|
|
iter.for_each(drop);
|
|
Weight::zero()
|
|
}
|
|
}
|
|
|
|
/// The aggregate XCMP message format.
|
|
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, TypeInfo, RuntimeDebug)]
|
|
pub enum XcmpMessageFormat {
|
|
/// Encoded `VersionedXcm` messages, all concatenated.
|
|
ConcatenatedVersionedXcm,
|
|
/// Encoded `Vec<u8>` messages, all concatenated.
|
|
ConcatenatedEncodedBlob,
|
|
/// One or more channel control signals; these should be interpreted immediately upon receipt
|
|
/// from the relay-chain.
|
|
Signals,
|
|
}
|
|
|
|
/// Something that should be called for each batch of messages received over XCMP.
|
|
pub trait XcmpMessageHandler {
|
|
/// Handle some incoming XCMP messages (note these are the big one-per-block aggregate
|
|
/// messages).
|
|
///
|
|
/// Also, process messages up to some `max_weight`.
|
|
fn handle_xcmp_messages<'a, I: Iterator<Item = (Id, RelayChainBlockNumber, &'a [u8])>>(
|
|
iter: I,
|
|
max_weight: Weight,
|
|
) -> Weight;
|
|
}
|
|
impl XcmpMessageHandler for () {
|
|
fn handle_xcmp_messages<'a, I: Iterator<Item = (Id, RelayChainBlockNumber, &'a [u8])>>(
|
|
iter: I,
|
|
_max_weight: Weight,
|
|
) -> Weight {
|
|
for _ in iter {}
|
|
Weight::zero()
|
|
}
|
|
}
|
|
|
|
/// Validation parameters for evaluating the parachain validity function.
|
|
// TODO: balance downloads (https://github.com/paritytech/polkadot/issues/220)
|
|
#[derive(PartialEq, Eq, Decode, Clone)]
|
|
#[cfg_attr(feature = "std", derive(Debug, Encode))]
|
|
pub struct ValidationParams {
|
|
/// Previous head-data.
|
|
pub parent_head: HeadData,
|
|
/// The collation body.
|
|
pub block_data: BlockData,
|
|
/// The current relay-chain block number.
|
|
pub relay_parent_number: RelayChainBlockNumber,
|
|
/// The relay-chain block's storage root.
|
|
pub relay_parent_storage_root: Hash,
|
|
}
|
|
|
|
/// Maximum number of HRMP messages allowed per candidate.
|
|
///
|
|
/// We also use this as a generous limit, which still prevents possible memory exhaustion, from
|
|
/// malicious parachains that may otherwise return a huge amount of messages in `ValidationResult`.
|
|
pub const MAX_HORIZONTAL_MESSAGE_NUM: u32 = 16 * 1024;
|
|
/// Maximum number of UMP messages allowed per candidate.
|
|
///
|
|
/// We also use this as a generous limit, which still prevents possible memory exhaustion, from
|
|
/// malicious parachains that may otherwise return a huge amount of messages in `ValidationResult`.
|
|
pub const MAX_UPWARD_MESSAGE_NUM: u32 = 16 * 1024;
|
|
|
|
pub type UpwardMessages = BoundedVec<UpwardMessage, ConstU32<MAX_UPWARD_MESSAGE_NUM>>;
|
|
|
|
pub type HorizontalMessages =
|
|
BoundedVec<OutboundHrmpMessage<Id>, ConstU32<MAX_HORIZONTAL_MESSAGE_NUM>>;
|
|
|
|
/// The result of parachain validation.
|
|
// TODO: balance uploads (https://github.com/paritytech/polkadot/issues/220)
|
|
#[derive(PartialEq, Eq, Clone, Encode)]
|
|
#[cfg_attr(feature = "std", derive(Debug, Decode))]
|
|
pub struct ValidationResult {
|
|
/// New head data that should be included in the relay chain state.
|
|
pub head_data: HeadData,
|
|
/// An update to the validation code that should be scheduled in the relay chain.
|
|
pub new_validation_code: Option<ValidationCode>,
|
|
/// Upward messages send by the Parachain.
|
|
pub upward_messages: UpwardMessages,
|
|
/// Outbound horizontal messages sent by the parachain.
|
|
pub horizontal_messages: HorizontalMessages,
|
|
/// Number of downward messages that were processed by the Parachain.
|
|
///
|
|
/// It is expected that the Parachain processes them from first to last.
|
|
pub processed_downward_messages: u32,
|
|
/// The mark which specifies the block number up to which all inbound HRMP messages are
|
|
/// processed.
|
|
pub hrmp_watermark: RelayChainBlockNumber,
|
|
}
|