bp_polkadot_core/parachains.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common 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// Parity Bridges Common 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 Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
16
17//! Primitives of polkadot-like chains, that are related to parachains functionality.
18//!
19//! Even though this (bridges) repository references polkadot repository, we can't
20//! reference polkadot crates from pallets. That's because bridges repository is
21//! included in the Cumulus repository and included pallets are used by Cumulus
22//! parachains. Having pallets that are referencing polkadot, would mean that there may
23//! be two versions of polkadot crates included in the runtime. Which is bad.
24
25use bp_runtime::{raw_storage_proof_size, RawStorageProof, Size};
26use codec::{CompactAs, Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
27use scale_info::TypeInfo;
28use sp_core::Hasher;
29use sp_runtime::RuntimeDebug;
30use sp_std::vec::Vec;
31
32#[cfg(feature = "std")]
33use serde::{Deserialize, Serialize};
34
35/// Parachain id.
36///
37/// This is an equivalent of the `polkadot_parachain_primitives::Id`, which is a compact-encoded
38/// `u32`.
39#[derive(
40 Clone,
41 CompactAs,
42 Copy,
43 Decode,
44 DecodeWithMemTracking,
45 Default,
46 Encode,
47 Eq,
48 Hash,
49 MaxEncodedLen,
50 Ord,
51 PartialEq,
52 PartialOrd,
53 RuntimeDebug,
54 TypeInfo,
55)]
56pub struct ParaId(pub u32);
57
58impl From<u32> for ParaId {
59 fn from(id: u32) -> Self {
60 ParaId(id)
61 }
62}
63
64/// Parachain head.
65///
66/// This is an equivalent of the `polkadot_parachain_primitives::HeadData`.
67///
68/// The parachain head means (at least in Cumulus) a SCALE-encoded parachain header.
69#[derive(
70 PartialEq,
71 Eq,
72 Clone,
73 PartialOrd,
74 Ord,
75 Encode,
76 Decode,
77 DecodeWithMemTracking,
78 RuntimeDebug,
79 TypeInfo,
80 Default,
81)]
82#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Hash))]
83pub struct ParaHead(pub Vec<u8>);
84
85impl ParaHead {
86 /// Returns the hash of this head data.
87 pub fn hash(&self) -> crate::Hash {
88 sp_runtime::traits::BlakeTwo256::hash(&self.0)
89 }
90}
91
92/// Parachain head hash.
93pub type ParaHash = crate::Hash;
94
95/// Parachain head hasher.
96pub type ParaHasher = crate::Hasher;
97
98/// Raw storage proof of parachain heads, stored in polkadot-like chain runtime.
99#[derive(Clone, Decode, DecodeWithMemTracking, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
100pub struct ParaHeadsProof {
101 /// Unverified storage proof of finalized parachain heads.
102 pub storage_proof: RawStorageProof,
103}
104
105impl Size for ParaHeadsProof {
106 fn size(&self) -> u32 {
107 use frame_support::sp_runtime::SaturatedConversion;
108 raw_storage_proof_size(&self.storage_proof).saturated_into()
109 }
110}