polkadot_parachain_primitives/
primitives.rs1use alloc::vec::Vec;
21
22use bounded_collections::{BoundedVec, ConstU32};
23use codec::{CompactAs, Decode, Encode, MaxEncodedLen};
24use scale_info::TypeInfo;
25use serde::{Deserialize, Serialize};
26use sp_core::{bytes, RuntimeDebug, TypeId};
27use sp_runtime::traits::Hash as _;
28use sp_weights::Weight;
29
30use polkadot_core_primitives::{Hash, OutboundHrmpMessage};
31
32pub use polkadot_core_primitives::BlockNumber as RelayChainBlockNumber;
34
35#[derive(
37 PartialEq,
38 Eq,
39 Clone,
40 PartialOrd,
41 Ord,
42 Encode,
43 Decode,
44 RuntimeDebug,
45 derive_more::From,
46 TypeInfo,
47 Serialize,
48 Deserialize,
49)]
50#[cfg_attr(feature = "std", derive(Hash, Default))]
51pub struct HeadData(#[serde(with = "bytes")] pub Vec<u8>);
52
53impl HeadData {
54 pub fn hash(&self) -> Hash {
56 sp_runtime::traits::BlakeTwo256::hash(&self.0)
57 }
58}
59
60#[derive(
62 PartialEq,
63 Eq,
64 Clone,
65 Encode,
66 Decode,
67 RuntimeDebug,
68 derive_more::From,
69 TypeInfo,
70 Serialize,
71 Deserialize,
72)]
73#[cfg_attr(feature = "std", derive(Hash))]
74pub struct ValidationCode(#[serde(with = "bytes")] pub Vec<u8>);
75
76impl ValidationCode {
77 pub fn hash(&self) -> ValidationCodeHash {
79 ValidationCodeHash(sp_runtime::traits::BlakeTwo256::hash(&self.0[..]))
80 }
81}
82
83#[derive(Clone, Copy, Encode, Decode, Hash, Eq, PartialEq, PartialOrd, Ord, TypeInfo)]
90pub struct ValidationCodeHash(Hash);
91
92impl core::fmt::Display for ValidationCodeHash {
93 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
94 self.0.fmt(f)
95 }
96}
97
98impl core::fmt::Debug for ValidationCodeHash {
99 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
100 write!(f, "{:?}", self.0)
101 }
102}
103
104impl AsRef<[u8]> for ValidationCodeHash {
105 fn as_ref(&self) -> &[u8] {
106 self.0.as_ref()
107 }
108}
109
110impl From<Hash> for ValidationCodeHash {
111 fn from(hash: Hash) -> ValidationCodeHash {
112 ValidationCodeHash(hash)
113 }
114}
115
116impl From<[u8; 32]> for ValidationCodeHash {
117 fn from(hash: [u8; 32]) -> ValidationCodeHash {
118 ValidationCodeHash(hash.into())
119 }
120}
121
122impl core::fmt::LowerHex for ValidationCodeHash {
123 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
124 core::fmt::LowerHex::fmt(&self.0, f)
125 }
126}
127
128#[derive(PartialEq, Eq, Clone, Encode, Decode, derive_more::From, TypeInfo, RuntimeDebug)]
132#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
133pub struct BlockData(#[cfg_attr(feature = "std", serde(with = "bytes"))] pub Vec<u8>);
134
135#[derive(
137 Clone,
138 CompactAs,
139 Copy,
140 Decode,
141 Default,
142 Encode,
143 Eq,
144 Hash,
145 MaxEncodedLen,
146 Ord,
147 PartialEq,
148 PartialOrd,
149 RuntimeDebug,
150 serde::Serialize,
151 serde::Deserialize,
152 TypeInfo,
153)]
154#[cfg_attr(feature = "std", derive(derive_more::Display))]
155pub struct Id(u32);
156
157impl TypeId for Id {
158 const TYPE_ID: [u8; 4] = *b"para";
159}
160
161impl From<Id> for u32 {
162 fn from(x: Id) -> Self {
163 x.0
164 }
165}
166
167impl From<u32> for Id {
168 fn from(x: u32) -> Self {
169 Id(x)
170 }
171}
172
173impl From<usize> for Id {
174 fn from(x: usize) -> Self {
175 let x = x.try_into().unwrap_or(u32::MAX);
177 Id(x)
178 }
179}
180
181impl From<i32> for Id {
197 fn from(x: i32) -> Self {
198 Id(x as u32)
199 }
200}
201
202const SYSTEM_INDEX_END: u32 = 1999;
204const PUBLIC_INDEX_START: u32 = 2000;
205
206pub const LOWEST_PUBLIC_ID: Id = Id(PUBLIC_INDEX_START);
208
209impl Id {
210 pub const fn new(id: u32) -> Self {
212 Self(id)
213 }
214}
215
216pub trait IsSystem {
218 fn is_system(&self) -> bool;
220}
221
222impl IsSystem for Id {
223 fn is_system(&self) -> bool {
224 self.0 <= SYSTEM_INDEX_END
225 }
226}
227
228impl core::ops::Add<u32> for Id {
229 type Output = Self;
230
231 fn add(self, other: u32) -> Self {
232 Self(self.0 + other)
233 }
234}
235
236impl core::ops::Sub<u32> for Id {
237 type Output = Self;
238
239 fn sub(self, other: u32) -> Self {
240 Self(self.0 - other)
241 }
242}
243
244#[derive(
245 Clone, Copy, Default, Encode, Decode, Eq, PartialEq, Ord, PartialOrd, RuntimeDebug, TypeInfo,
246)]
247pub struct Sibling(pub Id);
248
249impl From<Id> for Sibling {
250 fn from(i: Id) -> Self {
251 Self(i)
252 }
253}
254
255impl From<Sibling> for Id {
256 fn from(i: Sibling) -> Self {
257 i.0
258 }
259}
260
261impl AsRef<Id> for Sibling {
262 fn as_ref(&self) -> &Id {
263 &self.0
264 }
265}
266
267impl TypeId for Sibling {
268 const TYPE_ID: [u8; 4] = *b"sibl";
269}
270
271impl From<Sibling> for u32 {
272 fn from(x: Sibling) -> Self {
273 x.0.into()
274 }
275}
276
277impl From<u32> for Sibling {
278 fn from(x: u32) -> Self {
279 Sibling(x.into())
280 }
281}
282
283impl IsSystem for Sibling {
284 fn is_system(&self) -> bool {
285 IsSystem::is_system(&self.0)
286 }
287}
288
289#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, RuntimeDebug, TypeInfo)]
297#[cfg_attr(feature = "std", derive(Hash))]
298pub struct HrmpChannelId {
299 pub sender: Id,
301 pub recipient: Id,
303}
304
305impl HrmpChannelId {
306 pub fn is_participant(&self, id: Id) -> bool {
308 id == self.sender || id == self.recipient
309 }
310}
311
312pub type UpwardMessage = Vec<u8>;
314
315pub trait DmpMessageHandler {
317 fn handle_dmp_messages(
321 iter: impl Iterator<Item = (RelayChainBlockNumber, Vec<u8>)>,
322 max_weight: Weight,
323 ) -> Weight;
324}
325impl DmpMessageHandler for () {
326 fn handle_dmp_messages(
327 iter: impl Iterator<Item = (RelayChainBlockNumber, Vec<u8>)>,
328 _max_weight: Weight,
329 ) -> Weight {
330 iter.for_each(drop);
331 Weight::zero()
332 }
333}
334
335#[derive(
337 Copy,
338 Clone,
339 Eq,
340 PartialEq,
341 Ord,
342 PartialOrd,
343 Encode,
344 Decode,
345 TypeInfo,
346 RuntimeDebug,
347 MaxEncodedLen,
348)]
349pub enum XcmpMessageFormat {
350 ConcatenatedVersionedXcm,
352 ConcatenatedEncodedBlob,
354 Signals,
357}
358
359pub trait XcmpMessageHandler {
361 fn handle_xcmp_messages<'a, I: Iterator<Item = (Id, RelayChainBlockNumber, &'a [u8])>>(
366 iter: I,
367 max_weight: Weight,
368 ) -> Weight;
369}
370impl XcmpMessageHandler for () {
371 fn handle_xcmp_messages<'a, I: Iterator<Item = (Id, RelayChainBlockNumber, &'a [u8])>>(
372 iter: I,
373 _max_weight: Weight,
374 ) -> Weight {
375 for _ in iter {}
376 Weight::zero()
377 }
378}
379
380#[derive(PartialEq, Eq, Decode, Clone)]
383#[cfg_attr(feature = "std", derive(Debug, Encode))]
384pub struct ValidationParams {
385 pub parent_head: HeadData,
387 pub block_data: BlockData,
389 pub relay_parent_number: RelayChainBlockNumber,
391 pub relay_parent_storage_root: Hash,
393}
394
395pub const MAX_HORIZONTAL_MESSAGE_NUM: u32 = 16 * 1024;
400pub const MAX_UPWARD_MESSAGE_NUM: u32 = 16 * 1024;
405
406pub type UpwardMessages = BoundedVec<UpwardMessage, ConstU32<MAX_UPWARD_MESSAGE_NUM>>;
407
408pub type HorizontalMessages =
409 BoundedVec<OutboundHrmpMessage<Id>, ConstU32<MAX_HORIZONTAL_MESSAGE_NUM>>;
410
411#[derive(PartialEq, Eq, Clone, Encode)]
414#[cfg_attr(feature = "std", derive(Debug, Decode))]
415pub struct ValidationResult {
416 pub head_data: HeadData,
418 pub new_validation_code: Option<ValidationCode>,
420 pub upward_messages: UpwardMessages,
422 pub horizontal_messages: HorizontalMessages,
424 pub processed_downward_messages: u32,
428 pub hrmp_watermark: RelayChainBlockNumber,
431}