1#![cfg_attr(not(feature = "std"), no_std)]
20
21extern crate alloc;
22
23use alloc::vec::Vec;
24use codec::{Decode, DecodeWithMemTracking, 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
47pub mod relay_chain {
49 pub use polkadot_core_primitives::*;
50 pub use polkadot_primitives::*;
51}
52
53pub type InboundHrmpMessage = polkadot_primitives::InboundHrmpMessage<relay_chain::BlockNumber>;
55
56pub type OutboundHrmpMessage = polkadot_primitives::OutboundHrmpMessage<ParaId>;
58
59#[derive(Eq, PartialEq, Copy, Clone, RuntimeDebug, Encode, Decode)]
61pub enum MessageSendError {
62 QueueFull,
64 NoChannel,
66 TooBig,
68 Other,
70 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#[derive(
89 Encode, Decode, DecodeWithMemTracking, MaxEncodedLen, Clone, Eq, PartialEq, TypeInfo, Debug,
90)]
91pub enum AggregateMessageOrigin {
92 Here,
94 Parent,
98 Sibling(ParaId),
102}
103
104impl From<AggregateMessageOrigin> for Location {
105 fn from(origin: AggregateMessageOrigin) -> Self {
106 match origin {
107 AggregateMessageOrigin::Here => Location::here(),
108 AggregateMessageOrigin::Parent => Location::parent(),
109 AggregateMessageOrigin::Sibling(id) => Location::new(1, Junction::Parachain(id.into())),
110 }
111 }
112}
113
114#[cfg(feature = "runtime-benchmarks")]
115impl From<u32> for AggregateMessageOrigin {
116 fn from(x: u32) -> Self {
117 match x {
118 0 => Self::Here,
119 1 => Self::Parent,
120 p => Self::Sibling(ParaId::from(p)),
121 }
122 }
123}
124
125pub struct ChannelInfo {
127 pub max_capacity: u32,
129 pub max_total_size: u32,
131 pub max_message_size: u32,
133 pub msg_count: u32,
136 pub total_size: u32,
139}
140
141pub trait GetChannelInfo {
142 fn get_channel_status(id: ParaId) -> ChannelStatus;
143 fn get_channel_info(id: ParaId) -> Option<ChannelInfo>;
144}
145
146pub trait ListChannelInfos {
148 fn outgoing_channels() -> Vec<ParaId>;
149}
150
151pub trait UpwardMessageSender {
153 fn send_upward_message(msg: UpwardMessage) -> Result<(u32, XcmHash), MessageSendError>;
157}
158impl UpwardMessageSender for () {
159 fn send_upward_message(_msg: UpwardMessage) -> Result<(u32, XcmHash), MessageSendError> {
160 Err(MessageSendError::NoChannel)
161 }
162}
163
164pub enum ChannelStatus {
166 Closed,
168 Full,
170 Ready(usize, usize),
175}
176
177pub trait XcmpMessageSource {
179 fn take_outbound_messages(maximum_channels: usize) -> Vec<(ParaId, Vec<u8>)>;
181}
182
183impl XcmpMessageSource for () {
184 fn take_outbound_messages(_maximum_channels: usize) -> Vec<(ParaId, Vec<u8>)> {
185 Vec::new()
186 }
187}
188
189#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, RuntimeDebug)]
191pub enum ServiceQuality {
192 Ordered,
196 Fast,
199}
200
201#[derive(codec::Encode, codec::Decode, Clone)]
206pub struct ParachainBlockData<B: BlockT> {
207 header: B::Header,
209 extrinsics: alloc::vec::Vec<B::Extrinsic>,
211 storage_proof: sp_trie::CompactProof,
213}
214
215impl<B: BlockT> ParachainBlockData<B> {
216 pub fn new(
218 header: <B as BlockT>::Header,
219 extrinsics: alloc::vec::Vec<<B as BlockT>::Extrinsic>,
220 storage_proof: sp_trie::CompactProof,
221 ) -> Self {
222 Self { header, extrinsics, storage_proof }
223 }
224
225 pub fn into_block(self) -> B {
227 B::new(self.header, self.extrinsics)
228 }
229
230 pub fn into_header(self) -> B::Header {
232 self.header
233 }
234
235 pub fn header(&self) -> &B::Header {
237 &self.header
238 }
239
240 pub fn extrinsics(&self) -> &[B::Extrinsic] {
242 &self.extrinsics
243 }
244
245 pub fn storage_proof(&self) -> &sp_trie::CompactProof {
247 &self.storage_proof
248 }
249
250 pub fn deconstruct(self) -> (B::Header, alloc::vec::Vec<B::Extrinsic>, sp_trie::CompactProof) {
252 (self.header, self.extrinsics, self.storage_proof)
253 }
254}
255
256pub const CUMULUS_CONSENSUS_ID: ConsensusEngineId = *b"CMLS";
258
259#[derive(Clone, RuntimeDebug, Decode, Encode, PartialEq)]
261pub enum CumulusDigestItem {
262 #[codec(index = 0)]
264 RelayParent(relay_chain::Hash),
265}
266
267impl CumulusDigestItem {
268 pub fn to_digest_item(&self) -> DigestItem {
270 DigestItem::Consensus(CUMULUS_CONSENSUS_ID, self.encode())
271 }
272}
273
274pub fn extract_relay_parent(digest: &Digest) -> Option<relay_chain::Hash> {
279 digest.convert_first(|d| match d {
280 DigestItem::Consensus(id, val) if id == &CUMULUS_CONSENSUS_ID =>
281 match CumulusDigestItem::decode(&mut &val[..]) {
282 Ok(CumulusDigestItem::RelayParent(hash)) => Some(hash),
283 _ => None,
284 },
285 _ => None,
286 })
287}
288
289#[doc(hidden)]
306pub mod rpsr_digest {
307 use super::{relay_chain, ConsensusEngineId, Decode, Digest, DigestItem, Encode};
308 use codec::Compact;
309
310 pub const RPSR_CONSENSUS_ID: ConsensusEngineId = *b"RPSR";
312
313 pub fn relay_parent_storage_root_item(
315 storage_root: relay_chain::Hash,
316 number: impl Into<Compact<relay_chain::BlockNumber>>,
317 ) -> DigestItem {
318 DigestItem::Consensus(RPSR_CONSENSUS_ID, (storage_root, number.into()).encode())
319 }
320
321 pub fn extract_relay_parent_storage_root(
324 digest: &Digest,
325 ) -> Option<(relay_chain::Hash, relay_chain::BlockNumber)> {
326 digest.convert_first(|d| match d {
327 DigestItem::Consensus(id, val) if id == &RPSR_CONSENSUS_ID => {
328 let (h, n): (relay_chain::Hash, Compact<relay_chain::BlockNumber>) =
329 Decode::decode(&mut &val[..]).ok()?;
330
331 Some((h, n.0))
332 },
333 _ => None,
334 })
335 }
336}
337
338#[derive(Clone, Debug, codec::Decode, codec::Encode, PartialEq)]
342pub struct CollationInfoV1 {
343 pub upward_messages: Vec<UpwardMessage>,
345 pub horizontal_messages: Vec<OutboundHrmpMessage>,
347 pub new_validation_code: Option<relay_chain::ValidationCode>,
349 pub processed_downward_messages: u32,
351 pub hrmp_watermark: relay_chain::BlockNumber,
354}
355
356impl CollationInfoV1 {
357 pub fn into_latest(self, head_data: HeadData) -> CollationInfo {
359 CollationInfo {
360 upward_messages: self.upward_messages,
361 horizontal_messages: self.horizontal_messages,
362 new_validation_code: self.new_validation_code,
363 processed_downward_messages: self.processed_downward_messages,
364 hrmp_watermark: self.hrmp_watermark,
365 head_data,
366 }
367 }
368}
369
370#[derive(Clone, Debug, codec::Decode, codec::Encode, PartialEq, TypeInfo)]
372pub struct CollationInfo {
373 pub upward_messages: Vec<UpwardMessage>,
375 pub horizontal_messages: Vec<OutboundHrmpMessage>,
377 pub new_validation_code: Option<relay_chain::ValidationCode>,
379 pub processed_downward_messages: u32,
381 pub hrmp_watermark: relay_chain::BlockNumber,
384 pub head_data: HeadData,
386}
387
388sp_api::decl_runtime_apis! {
389 #[api_version(2)]
391 pub trait CollectCollationInfo {
392 #[changed_in(2)]
394 fn collect_collation_info() -> CollationInfoV1;
395 fn collect_collation_info(header: &Block::Header) -> CollationInfo;
400 }
401
402 pub trait GetCoreSelectorApi {
404 fn core_selector() -> (CoreSelector, ClaimQueueOffset);
406 }
407}