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, 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(Clone, Copy, Encode, Decode, Hash, Eq, PartialEq, Default, PartialOrd, Ord, TypeInfo)]
69pub struct CandidateHash(pub Hash);
70
71#[cfg(feature = "std")]
72impl std::ops::Deref for CandidateHash {
73 type Target = Hash;
74 fn deref(&self) -> &Self::Target {
75 &self.0
76 }
77}
78
79#[cfg(feature = "std")]
80impl std::fmt::Display for CandidateHash {
81 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82 self.0.fmt(f)
83 }
84}
85
86impl core::fmt::Debug for CandidateHash {
87 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
88 write!(f, "{:?}", self.0)
89 }
90}
91
92/// Index of a transaction in the relay chain. 32-bit should be plenty.
93pub type Nonce = u32;
94
95/// The balance of an account.
96/// 128-bits (or 38 significant decimal figures) will allow for 10 m currency (`10^7`) at a
97/// resolution to all for one second's worth of an annualised 50% reward be paid to a unit holder
98/// (`10^11` unit denomination), or `10^18` total atomic units, to grow at 50%/year for 51 years
99/// (`10^9` multiplier) for an eventual total of `10^27` units (27 significant decimal figures).
100/// We round denomination to `10^12` (12 SDF), and leave the other redundancy at the upper end so
101/// that 32 bits may be multiplied with a balance in 128 bits without worrying about overflow.
102pub type Balance = u128;
103
104/// Header type.
105pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
106/// Block type.
107pub type Block = generic::Block<Header, UncheckedExtrinsic>;
108/// Block ID.
109pub type BlockId = generic::BlockId<Block>;
110
111/// Opaque, encoded, unchecked extrinsic.
112pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
113
114/// The information that goes alongside a `transfer_into_parachain` operation. Entirely opaque, it
115/// will generally be used for identifying the reason for the transfer. Typically it will hold the
116/// destination account to which the transfer should be credited. If still more information is
117/// needed, then this should be a hash with the pre-image presented via an off-chain mechanism on
118/// the parachain.
119pub type Remark = [u8; 32];
120
121/// A message sent from the relay-chain down to a parachain.
122///
123/// The size of the message is limited by the `config.max_downward_message_size` parameter.
124pub type DownwardMessage = alloc::vec::Vec<u8>;
125
126/// A wrapped version of `DownwardMessage`. The difference is that it has attached the block number
127/// when the message was sent.
128#[derive(Encode, Decode, Clone, sp_runtime::RuntimeDebug, PartialEq, TypeInfo)]
129pub struct InboundDownwardMessage<BlockNumber = crate::BlockNumber> {
130 /// The block number at which these messages were put into the downward message queue.
131 pub sent_at: BlockNumber,
132 /// The actual downward message to processes.
133 pub msg: DownwardMessage,
134}
135
136/// An HRMP message seen from the perspective of a recipient.
137#[derive(Encode, Decode, Clone, sp_runtime::RuntimeDebug, PartialEq, TypeInfo)]
138pub struct InboundHrmpMessage<BlockNumber = crate::BlockNumber> {
139 /// The block number at which this message was sent.
140 /// Specifically, it is the block number at which the candidate that sends this message was
141 /// enacted.
142 pub sent_at: BlockNumber,
143 /// The message payload.
144 pub data: alloc::vec::Vec<u8>,
145}
146
147/// An HRMP message seen from the perspective of a sender.
148#[derive(Encode, Decode, Clone, sp_runtime::RuntimeDebug, PartialEq, Eq, Hash, TypeInfo)]
149pub struct OutboundHrmpMessage<Id> {
150 /// The para that will get this message in its downward message queue.
151 pub recipient: Id,
152 /// The message payload.
153 pub data: alloc::vec::Vec<u8>,
154}
155
156/// `V2` primitives.
157pub mod v2 {
158 pub use super::*;
159}