polkadot_core_primitives/lib.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot 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// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
16
17#![cfg_attr(not(feature = "std"), no_std)]
18
19//! Core Polkadot types.
20//!
21//! These core Polkadot types are used by the relay chain and the Parachains.
22
23extern crate alloc;
24
25use codec::{Decode, DecodeWithMemTracking, Encode};
26use scale_info::TypeInfo;
27use sp_runtime::{
28 generic,
29 traits::{IdentifyAccount, Verify},
30 MultiSignature,
31};
32
33pub use sp_runtime::traits::{BlakeTwo256, Hash as HashT};
34
35/// The block number type used by Polkadot.
36/// 32-bits will allow for 136 years of blocks assuming 1 block per second.
37pub type BlockNumber = u32;
38
39/// An instant or duration in time.
40pub type Moment = u64;
41
42/// Alias to type for a signature for a transaction on the relay chain. This allows one of several
43/// kinds of underlying crypto to be used, so isn't a fixed size when encoded.
44pub type Signature = MultiSignature;
45
46/// Alias to the public key used for this chain, actually a `MultiSigner`. Like the signature, this
47/// also isn't a fixed size when encoded, as different cryptos have different size public keys.
48pub type AccountPublic = <Signature as Verify>::Signer;
49
50/// Alias to the opaque account ID type for this chain, actually a `AccountId32`. This is always
51/// 32 bytes.
52pub type AccountId = <AccountPublic as IdentifyAccount>::AccountId;
53
54/// The type for looking up accounts. We don't expect more than 4 billion of them.
55pub type AccountIndex = u32;
56
57/// Identifier for a chain. 32-bit should be plenty.
58pub type ChainId = u32;
59
60/// A hash of some data used by the relay chain.
61pub type Hash = sp_core::H256;
62
63/// Unit type wrapper around [`type@Hash`] that represents a candidate hash.
64///
65/// This type is produced by `CandidateReceipt::hash`.
66///
67/// This type makes it easy to enforce that a hash is a candidate hash on the type level.
68#[derive(
69 Clone,
70 Copy,
71 Encode,
72 Decode,
73 DecodeWithMemTracking,
74 Hash,
75 Eq,
76 PartialEq,
77 Default,
78 PartialOrd,
79 Ord,
80 TypeInfo,
81)]
82pub struct CandidateHash(pub Hash);
83
84#[cfg(feature = "std")]
85impl std::ops::Deref for CandidateHash {
86 type Target = Hash;
87 fn deref(&self) -> &Self::Target {
88 &self.0
89 }
90}
91
92#[cfg(feature = "std")]
93impl std::fmt::Display for CandidateHash {
94 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95 self.0.fmt(f)
96 }
97}
98
99impl core::fmt::Debug for CandidateHash {
100 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
101 write!(f, "{:?}", self.0)
102 }
103}
104
105/// Index of a transaction in the relay chain. 32-bit should be plenty.
106pub type Nonce = u32;
107
108/// The balance of an account.
109/// 128-bits (or 38 significant decimal figures) will allow for 10 m currency (`10^7`) at a
110/// resolution to all for one second's worth of an annualised 50% reward be paid to a unit holder
111/// (`10^11` unit denomination), or `10^18` total atomic units, to grow at 50%/year for 51 years
112/// (`10^9` multiplier) for an eventual total of `10^27` units (27 significant decimal figures).
113/// We round denomination to `10^12` (12 SDF), and leave the other redundancy at the upper end so
114/// that 32 bits may be multiplied with a balance in 128 bits without worrying about overflow.
115pub type Balance = u128;
116
117/// Header type.
118pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
119/// Block type.
120pub type Block = generic::Block<Header, UncheckedExtrinsic>;
121/// Block ID.
122pub type BlockId = generic::BlockId<Block>;
123
124/// Opaque, encoded, unchecked extrinsic.
125pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
126
127/// The information that goes alongside a `transfer_into_parachain` operation. Entirely opaque, it
128/// will generally be used for identifying the reason for the transfer. Typically it will hold the
129/// destination account to which the transfer should be credited. If still more information is
130/// needed, then this should be a hash with the pre-image presented via an off-chain mechanism on
131/// the parachain.
132pub type Remark = [u8; 32];
133
134/// A message sent from the relay-chain down to a parachain.
135///
136/// The size of the message is limited by the `config.max_downward_message_size` parameter.
137pub type DownwardMessage = alloc::vec::Vec<u8>;
138
139/// A wrapped version of `DownwardMessage`. The difference is that it has attached the block number
140/// when the message was sent.
141#[derive(
142 Encode, Decode, DecodeWithMemTracking, Clone, sp_runtime::RuntimeDebug, PartialEq, TypeInfo,
143)]
144pub struct InboundDownwardMessage<BlockNumber = crate::BlockNumber> {
145 /// The block number at which these messages were put into the downward message queue.
146 pub sent_at: BlockNumber,
147 /// The actual downward message to processes.
148 pub msg: DownwardMessage,
149}
150
151/// An HRMP message seen from the perspective of a recipient.
152#[derive(
153 Encode, Decode, DecodeWithMemTracking, Clone, sp_runtime::RuntimeDebug, PartialEq, TypeInfo,
154)]
155pub struct InboundHrmpMessage<BlockNumber = crate::BlockNumber> {
156 /// The block number at which this message was sent.
157 /// Specifically, it is the block number at which the candidate that sends this message was
158 /// enacted.
159 pub sent_at: BlockNumber,
160 /// The message payload.
161 pub data: alloc::vec::Vec<u8>,
162}
163
164/// An HRMP message seen from the perspective of a sender.
165#[derive(
166 Encode,
167 Decode,
168 DecodeWithMemTracking,
169 Clone,
170 sp_runtime::RuntimeDebug,
171 PartialEq,
172 Eq,
173 Hash,
174 TypeInfo,
175)]
176pub struct OutboundHrmpMessage<Id> {
177 /// The para that will get this message in its downward message queue.
178 pub recipient: Id,
179 /// The message payload.
180 pub data: alloc::vec::Vec<u8>,
181}
182
183/// `V2` primitives.
184pub mod v2 {
185 pub use super::*;
186}