cumulus_primitives_core/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3
4// Cumulus is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Cumulus is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Cumulus.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Cumulus related core primitive types and traits.
18
19#![cfg_attr(not(feature = "std"), no_std)]
20
21extern crate alloc;
22
23use alloc::vec::Vec;
24use codec::{Decode, Encode, MaxEncodedLen};
25use polkadot_parachain_primitives::primitives::HeadData;
26use scale_info::TypeInfo;
27use sp_runtime::RuntimeDebug;
28
29pub use polkadot_core_primitives::InboundDownwardMessage;
30pub use polkadot_parachain_primitives::primitives::{
31	DmpMessageHandler, Id as ParaId, IsSystem, UpwardMessage, ValidationParams, XcmpMessageFormat,
32	XcmpMessageHandler,
33};
34pub use polkadot_primitives::{
35	vstaging::{ClaimQueueOffset, CoreSelector},
36	AbridgedHostConfiguration, AbridgedHrmpChannel, PersistedValidationData,
37};
38
39pub use sp_runtime::{
40	generic::{Digest, DigestItem},
41	traits::Block as BlockT,
42	ConsensusEngineId,
43};
44
45pub use xcm::latest::prelude::*;
46
47/// A module that re-exports relevant relay chain definitions.
48pub mod relay_chain {
49	pub use polkadot_core_primitives::*;
50	pub use polkadot_primitives::*;
51}
52
53/// An inbound HRMP message.
54pub type InboundHrmpMessage = polkadot_primitives::InboundHrmpMessage<relay_chain::BlockNumber>;
55
56/// And outbound HRMP message
57pub type OutboundHrmpMessage = polkadot_primitives::OutboundHrmpMessage<ParaId>;
58
59/// Error description of a message send failure.
60#[derive(Eq, PartialEq, Copy, Clone, RuntimeDebug, Encode, Decode)]
61pub enum MessageSendError {
62	/// The dispatch queue is full.
63	QueueFull,
64	/// There does not exist a channel for sending the message.
65	NoChannel,
66	/// The message is too big to ever fit in a channel.
67	TooBig,
68	/// Some other error.
69	Other,
70	/// There are too many channels open at once.
71	TooManyChannels,
72}
73
74impl From<MessageSendError> for &'static str {
75	fn from(e: MessageSendError) -> Self {
76		use MessageSendError::*;
77		match e {
78			QueueFull => "QueueFull",
79			NoChannel => "NoChannel",
80			TooBig => "TooBig",
81			Other => "Other",
82			TooManyChannels => "TooManyChannels",
83		}
84	}
85}
86
87/// The origin of an inbound message.
88#[derive(Encode, Decode, MaxEncodedLen, Clone, Eq, PartialEq, TypeInfo, Debug)]
89pub enum AggregateMessageOrigin {
90	/// The message came from the para-chain itself.
91	Here,
92	/// The message came from the relay-chain.
93	///
94	/// This is used by the DMP queue.
95	Parent,
96	/// The message came from a sibling para-chain.
97	///
98	/// This is used by the HRMP queue.
99	Sibling(ParaId),
100}
101
102impl From<AggregateMessageOrigin> for Location {
103	fn from(origin: AggregateMessageOrigin) -> Self {
104		match origin {
105			AggregateMessageOrigin::Here => Location::here(),
106			AggregateMessageOrigin::Parent => Location::parent(),
107			AggregateMessageOrigin::Sibling(id) => Location::new(1, Junction::Parachain(id.into())),
108		}
109	}
110}
111
112#[cfg(feature = "runtime-benchmarks")]
113impl From<u32> for AggregateMessageOrigin {
114	fn from(x: u32) -> Self {
115		match x {
116			0 => Self::Here,
117			1 => Self::Parent,
118			p => Self::Sibling(ParaId::from(p)),
119		}
120	}
121}
122
123/// Information about an XCMP channel.
124pub struct ChannelInfo {
125	/// The maximum number of messages that can be pending in the channel at once.
126	pub max_capacity: u32,
127	/// The maximum total size of the messages that can be pending in the channel at once.
128	pub max_total_size: u32,
129	/// The maximum message size that could be put into the channel.
130	pub max_message_size: u32,
131	/// The current number of messages pending in the channel.
132	/// Invariant: should be less or equal to `max_capacity`.s`.
133	pub msg_count: u32,
134	/// The total size in bytes of all message payloads in the channel.
135	/// Invariant: should be less or equal to `max_total_size`.
136	pub total_size: u32,
137}
138
139pub trait GetChannelInfo {
140	fn get_channel_status(id: ParaId) -> ChannelStatus;
141	fn get_channel_info(id: ParaId) -> Option<ChannelInfo>;
142}
143
144/// List all open outgoing channels.
145pub trait ListChannelInfos {
146	fn outgoing_channels() -> Vec<ParaId>;
147}
148
149/// Something that should be called when sending an upward message.
150pub trait UpwardMessageSender {
151	/// Send the given UMP message; return the expected number of blocks before the message will
152	/// be dispatched or an error if the message cannot be sent.
153	/// return the hash of the message sent
154	fn send_upward_message(msg: UpwardMessage) -> Result<(u32, XcmHash), MessageSendError>;
155}
156impl UpwardMessageSender for () {
157	fn send_upward_message(_msg: UpwardMessage) -> Result<(u32, XcmHash), MessageSendError> {
158		Err(MessageSendError::NoChannel)
159	}
160}
161
162/// The status of a channel.
163pub enum ChannelStatus {
164	/// Channel doesn't exist/has been closed.
165	Closed,
166	/// Channel is completely full right now.
167	Full,
168	/// Channel is ready for sending; the two parameters are the maximum size a valid message may
169	/// have right now, and the maximum size a message may ever have (this will generally have been
170	/// available during message construction, but it's possible the channel parameters changed in
171	/// the meantime).
172	Ready(usize, usize),
173}
174
175/// A means of figuring out what outbound XCMP messages should be being sent.
176pub trait XcmpMessageSource {
177	/// Take a single XCMP message from the queue for the given `dest`, if one exists.
178	fn take_outbound_messages(maximum_channels: usize) -> Vec<(ParaId, Vec<u8>)>;
179}
180
181impl XcmpMessageSource for () {
182	fn take_outbound_messages(_maximum_channels: usize) -> Vec<(ParaId, Vec<u8>)> {
183		Vec::new()
184	}
185}
186
187/// The "quality of service" considerations for message sending.
188#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, RuntimeDebug)]
189pub enum ServiceQuality {
190	/// Ensure that this message is dispatched in the same relative order as any other messages
191	/// that were also sent with `Ordered`. This only guarantees message ordering on the dispatch
192	/// side, and not necessarily on the execution side.
193	Ordered,
194	/// Ensure that the message is dispatched as soon as possible, which could result in it being
195	/// dispatched before other messages which are larger and/or rely on relative ordering.
196	Fast,
197}
198
199/// The parachain block that is created by a collator.
200///
201/// This is send as PoV (proof of validity block) to the relay-chain validators. There it will be
202/// passed to the parachain validation Wasm blob to be validated.
203#[derive(codec::Encode, codec::Decode, Clone)]
204pub struct ParachainBlockData<B: BlockT> {
205	/// The header of the parachain block.
206	header: B::Header,
207	/// The extrinsics of the parachain block.
208	extrinsics: alloc::vec::Vec<B::Extrinsic>,
209	/// The data that is required to emulate the storage accesses executed by all extrinsics.
210	storage_proof: sp_trie::CompactProof,
211}
212
213impl<B: BlockT> ParachainBlockData<B> {
214	/// Creates a new instance of `Self`.
215	pub fn new(
216		header: <B as BlockT>::Header,
217		extrinsics: alloc::vec::Vec<<B as BlockT>::Extrinsic>,
218		storage_proof: sp_trie::CompactProof,
219	) -> Self {
220		Self { header, extrinsics, storage_proof }
221	}
222
223	/// Convert `self` into the stored block.
224	pub fn into_block(self) -> B {
225		B::new(self.header, self.extrinsics)
226	}
227
228	/// Convert `self` into the stored header.
229	pub fn into_header(self) -> B::Header {
230		self.header
231	}
232
233	/// Returns the header.
234	pub fn header(&self) -> &B::Header {
235		&self.header
236	}
237
238	/// Returns the extrinsics.
239	pub fn extrinsics(&self) -> &[B::Extrinsic] {
240		&self.extrinsics
241	}
242
243	/// Returns the [`CompactProof`](sp_trie::CompactProof).
244	pub fn storage_proof(&self) -> &sp_trie::CompactProof {
245		&self.storage_proof
246	}
247
248	/// Deconstruct into the inner parts.
249	pub fn deconstruct(self) -> (B::Header, alloc::vec::Vec<B::Extrinsic>, sp_trie::CompactProof) {
250		(self.header, self.extrinsics, self.storage_proof)
251	}
252}
253
254/// A consensus engine ID indicating that this is a Cumulus Parachain.
255pub const CUMULUS_CONSENSUS_ID: ConsensusEngineId = *b"CMLS";
256
257/// Consensus header digests for Cumulus parachains.
258#[derive(Clone, RuntimeDebug, Decode, Encode, PartialEq)]
259pub enum CumulusDigestItem {
260	/// A digest item indicating the relay-parent a parachain block was built against.
261	#[codec(index = 0)]
262	RelayParent(relay_chain::Hash),
263}
264
265impl CumulusDigestItem {
266	/// Encode this as a Substrate [`DigestItem`].
267	pub fn to_digest_item(&self) -> DigestItem {
268		DigestItem::Consensus(CUMULUS_CONSENSUS_ID, self.encode())
269	}
270}
271
272/// Extract the relay-parent from the provided header digest. Returns `None` if none were found.
273///
274/// If there are multiple valid digests, this returns the value of the first one, although
275/// well-behaving runtimes should not produce headers with more than one.
276pub fn extract_relay_parent(digest: &Digest) -> Option<relay_chain::Hash> {
277	digest.convert_first(|d| match d {
278		DigestItem::Consensus(id, val) if id == &CUMULUS_CONSENSUS_ID =>
279			match CumulusDigestItem::decode(&mut &val[..]) {
280				Ok(CumulusDigestItem::RelayParent(hash)) => Some(hash),
281				_ => None,
282			},
283		_ => None,
284	})
285}
286
287/// Utilities for handling the relay-parent storage root as a digest item.
288///
289/// This is not intended to be part of the public API, as it is a workaround for
290/// <https://github.com/paritytech/cumulus/issues/303> via
291/// <https://github.com/paritytech/polkadot/issues/7191>.
292///
293/// Runtimes using the parachain-system pallet are expected to produce this digest item,
294/// but will stop as soon as they are able to provide the relay-parent hash directly.
295///
296/// The relay-chain storage root is, in practice, a unique identifier of a block
297/// in the absence of equivocations (which are slashable). This assumes that the relay chain
298/// uses BABE or SASSAFRAS, because the slot and the author's VRF randomness are both included
299/// in the relay-chain storage root in both cases.
300///
301/// Therefore, the relay-parent storage root is a suitable identifier of unique relay chain
302/// blocks in low-value scenarios such as performance optimizations.
303#[doc(hidden)]
304pub mod rpsr_digest {
305	use super::{relay_chain, ConsensusEngineId, Decode, Digest, DigestItem, Encode};
306	use codec::Compact;
307
308	/// A consensus engine ID for relay-parent storage root digests.
309	pub const RPSR_CONSENSUS_ID: ConsensusEngineId = *b"RPSR";
310
311	/// Construct a digest item for relay-parent storage roots.
312	pub fn relay_parent_storage_root_item(
313		storage_root: relay_chain::Hash,
314		number: impl Into<Compact<relay_chain::BlockNumber>>,
315	) -> DigestItem {
316		DigestItem::Consensus(RPSR_CONSENSUS_ID, (storage_root, number.into()).encode())
317	}
318
319	/// Extract the relay-parent storage root and number from the provided header digest. Returns
320	/// `None` if none were found.
321	pub fn extract_relay_parent_storage_root(
322		digest: &Digest,
323	) -> Option<(relay_chain::Hash, relay_chain::BlockNumber)> {
324		digest.convert_first(|d| match d {
325			DigestItem::Consensus(id, val) if id == &RPSR_CONSENSUS_ID => {
326				let (h, n): (relay_chain::Hash, Compact<relay_chain::BlockNumber>) =
327					Decode::decode(&mut &val[..]).ok()?;
328
329				Some((h, n.0))
330			},
331			_ => None,
332		})
333	}
334}
335
336/// Information about a collation.
337///
338/// This was used in version 1 of the [`CollectCollationInfo`] runtime api.
339#[derive(Clone, Debug, codec::Decode, codec::Encode, PartialEq)]
340pub struct CollationInfoV1 {
341	/// Messages destined to be interpreted by the Relay chain itself.
342	pub upward_messages: Vec<UpwardMessage>,
343	/// The horizontal messages sent by the parachain.
344	pub horizontal_messages: Vec<OutboundHrmpMessage>,
345	/// New validation code.
346	pub new_validation_code: Option<relay_chain::ValidationCode>,
347	/// The number of messages processed from the DMQ.
348	pub processed_downward_messages: u32,
349	/// The mark which specifies the block number up to which all inbound HRMP messages are
350	/// processed.
351	pub hrmp_watermark: relay_chain::BlockNumber,
352}
353
354impl CollationInfoV1 {
355	/// Convert into the latest version of the [`CollationInfo`] struct.
356	pub fn into_latest(self, head_data: HeadData) -> CollationInfo {
357		CollationInfo {
358			upward_messages: self.upward_messages,
359			horizontal_messages: self.horizontal_messages,
360			new_validation_code: self.new_validation_code,
361			processed_downward_messages: self.processed_downward_messages,
362			hrmp_watermark: self.hrmp_watermark,
363			head_data,
364		}
365	}
366}
367
368/// Information about a collation.
369#[derive(Clone, Debug, codec::Decode, codec::Encode, PartialEq, TypeInfo)]
370pub struct CollationInfo {
371	/// Messages destined to be interpreted by the Relay chain itself.
372	pub upward_messages: Vec<UpwardMessage>,
373	/// The horizontal messages sent by the parachain.
374	pub horizontal_messages: Vec<OutboundHrmpMessage>,
375	/// New validation code.
376	pub new_validation_code: Option<relay_chain::ValidationCode>,
377	/// The number of messages processed from the DMQ.
378	pub processed_downward_messages: u32,
379	/// The mark which specifies the block number up to which all inbound HRMP messages are
380	/// processed.
381	pub hrmp_watermark: relay_chain::BlockNumber,
382	/// The head data, aka encoded header, of the block that corresponds to the collation.
383	pub head_data: HeadData,
384}
385
386sp_api::decl_runtime_apis! {
387	/// Runtime api to collect information about a collation.
388	#[api_version(2)]
389	pub trait CollectCollationInfo {
390		/// Collect information about a collation.
391		#[changed_in(2)]
392		fn collect_collation_info() -> CollationInfoV1;
393		/// Collect information about a collation.
394		///
395		/// The given `header` is the header of the built block for that
396		/// we are collecting the collation info for.
397		fn collect_collation_info(header: &Block::Header) -> CollationInfo;
398	}
399
400	/// Runtime api used to select the core for which the next block will be built.
401	pub trait GetCoreSelectorApi {
402		/// Retrieve core selector and claim queue offset for the next block.
403		fn core_selector() -> (CoreSelector, ClaimQueueOffset);
404	}
405}