parachains_common/lib.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#![cfg_attr(not(feature = "std"), no_std)]
17
18extern crate alloc;
19
20pub mod impls;
21pub mod message_queue;
22pub mod pay;
23pub mod xcm_config;
24pub use constants::*;
25pub use opaque::*;
26pub use types::*;
27
28/// Common types of parachains.
29mod types {
30 use sp_runtime::traits::{IdentifyAccount, Verify};
31
32 /// An index to a block.
33 pub type BlockNumber = u32;
34
35 /// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
36 pub type Signature = sp_runtime::MultiSignature;
37
38 /// Some way of identifying an account on the chain. We intentionally make it equivalent
39 /// to the public key of our transaction signing scheme.
40 pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
41
42 /// The type for looking up accounts. We don't expect more than 4 billion of them, but you
43 /// never know...
44 pub type AccountIndex = u32;
45
46 /// Balance of an account.
47 pub type Balance = u128;
48
49 /// Index of a transaction in the chain.
50 pub type Nonce = u32;
51
52 /// A hash of some data used by the chain.
53 pub type Hash = sp_core::H256;
54
55 /// Digest item type.
56 pub type DigestItem = sp_runtime::generic::DigestItem;
57
58 // Aura consensus authority.
59 pub type AuraId = sp_consensus_aura::sr25519::AuthorityId;
60
61 // Aura consensus authority used by Asset Hub Polkadot.
62 //
63 // Because of registering the authorities with an ed25519 key before switching from Shell
64 // to Asset Hub Polkadot, we were required to deploy a hotfix that changed Asset Hub Polkadot's
65 // Aura keys to ed22519. In the future that may change again.
66 pub type AssetHubPolkadotAuraId = sp_consensus_aura::ed25519::AuthorityId;
67
68 // Id used for identifying assets.
69 pub type AssetIdForTrustBackedAssets = u32;
70
71 // Id used for identifying non-fungible collections.
72 pub type CollectionId = u32;
73
74 // Id used for identifying non-fungible items.
75 pub type ItemId = u32;
76}
77
78/// Common constants of parachains.
79mod constants {
80 use super::types::BlockNumber;
81 use frame_support::{
82 weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
83 PalletId,
84 };
85 use sp_runtime::Perbill;
86
87 /// This determines the average expected block time that we are targeting. Blocks will be
88 /// produced at a minimum duration defined by `SLOT_DURATION`. `SLOT_DURATION` is picked up by
89 /// `pallet_timestamp` which is in turn picked up by `pallet_aura` to implement `fn
90 /// slot_duration()`.
91 ///
92 /// Change this to adjust the block time.
93 pub const MILLISECS_PER_BLOCK: u64 = 12000;
94 pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;
95
96 // Time is measured by number of blocks.
97 pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
98 pub const HOURS: BlockNumber = MINUTES * 60;
99 pub const DAYS: BlockNumber = HOURS * 24;
100
101 /// We assume that ~5% of the block weight is consumed by `on_initialize` handlers. This is
102 /// used to limit the maximal weight of a single extrinsic.
103 pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5);
104 /// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used by
105 /// Operational extrinsics.
106 pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
107
108 /// We allow for 0.5 seconds of compute with a 6 second average block time.
109 pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(
110 WEIGHT_REF_TIME_PER_SECOND.saturating_div(2),
111 polkadot_primitives::MAX_POV_SIZE as u64,
112 );
113
114 /// We allow for 2 seconds of compute with a 6 second average block.
115 pub const MAXIMUM_BLOCK_WEIGHT_FOR_ASYNC_BACKING: Weight = Weight::from_parts(
116 WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2),
117 polkadot_primitives::MAX_POV_SIZE as u64,
118 );
119
120 /// Treasury pallet id of the local chain, used to convert into AccountId
121 pub const TREASURY_PALLET_ID: PalletId = PalletId(*b"py/trsry");
122}
123
124/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
125/// the specifics of the runtime. They can then be made to be agnostic over specific formats
126/// of data like extrinsics, allowing for them to continue syncing the network through upgrades
127/// to even the core data structures.
128pub mod opaque {
129 use super::*;
130 use sp_runtime::{generic, traits::BlakeTwo256};
131
132 pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
133 /// Opaque block header type.
134 pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
135 /// Opaque block type.
136 pub type Block = generic::Block<Header, UncheckedExtrinsic>;
137 /// Opaque block identifier type.
138 pub type BlockId = generic::BlockId<Block>;
139}