polkadot_runtime_common/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
20
21pub mod assigned_slots;
22pub mod auctions;
23pub mod claims;
24pub mod crowdloan;
25pub mod elections;
26pub mod identity_migrator;
27pub mod impls;
28pub mod paras_registrar;
29pub mod paras_sudo_wrapper;
30pub mod purchase;
31pub mod slot_range;
32pub mod slots;
33pub mod traits;
34
35#[cfg(feature = "try-runtime")]
36pub mod try_runtime;
37pub mod xcm_sender;
38
39#[cfg(test)]
40mod integration_tests;
41#[cfg(test)]
42mod mock;
43
44extern crate alloc;
45
46use frame_support::{
47 parameter_types,
48 traits::{ConstU32, OneSessionHandler},
49 weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
50};
51use frame_system::limits;
52use polkadot_primitives::{AssignmentId, Balance, BlockNumber, ValidatorId};
53use sp_runtime::{FixedPointNumber, Perbill, Perquintill};
54use static_assertions::const_assert;
55
56pub use pallet_balances::Call as BalancesCall;
57#[cfg(feature = "std")]
58pub use pallet_staking::StakerStatus;
59pub use pallet_timestamp::Call as TimestampCall;
60use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
61pub use sp_runtime::traits::Bounded;
62#[cfg(any(feature = "std", test))]
63pub use sp_runtime::BuildStorage;
64
65pub use impls::ToAuthor;
67
68pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(1);
71pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
74pub const MAXIMUM_BLOCK_WEIGHT: Weight =
77 Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), u64::MAX);
78
79const_assert!(NORMAL_DISPATCH_RATIO.deconstruct() >= AVERAGE_ON_INITIALIZE_RATIO.deconstruct());
80
81parameter_types! {
83 pub const BlockHashCount: BlockNumber = 4096;
84 pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
87 pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(75, 1000_000);
90 pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 10u128);
94 pub MaximumMultiplier: Multiplier = Bounded::max_value();
96 pub BlockLength: limits::BlockLength = limits::BlockLength::builder()
98 .max_length(5 * 1024 * 1024)
99 .modify_max_length_for_class(frame_support::dispatch::DispatchClass::Normal, |m| {
100 *m = NORMAL_DISPATCH_RATIO * *m
101 })
102 .build();
103}
104
105pub type SlowAdjustingFeeUpdate<R> = TargetedFeeAdjustment<
108 R,
109 TargetBlockFullness,
110 AdjustmentVariable,
111 MinimumMultiplier,
112 MaximumMultiplier,
113>;
114
115#[macro_export]
120macro_rules! impl_runtime_weights {
121 ($runtime:ident) => {
122 use frame_support::{dispatch::DispatchClass, weights::Weight};
123 use frame_system::limits;
124 use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
125 pub use polkadot_runtime_common::{
126 impl_elections_weights, AVERAGE_ON_INITIALIZE_RATIO, MAXIMUM_BLOCK_WEIGHT,
127 NORMAL_DISPATCH_RATIO,
128 };
129 use sp_runtime::{FixedPointNumber, Perquintill};
130
131 impl_elections_weights!($runtime);
133
134 pub use $runtime::weights::{
136 BlockExecutionWeight, ExtrinsicBaseWeight, ParityDbWeight, RocksDbWeight,
137 };
138
139 parameter_types! {
140 pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
142 .base_block($runtime::weights::BlockExecutionWeight::get())
143 .for_class(DispatchClass::all(), |weights| {
144 weights.base_extrinsic = $runtime::weights::ExtrinsicBaseWeight::get();
145 })
146 .for_class(DispatchClass::Normal, |weights| {
147 weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
148 })
149 .for_class(DispatchClass::Operational, |weights| {
150 weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
151 weights.reserved = Some(
154 MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT,
155 );
156 })
157 .avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
158 .build_or_panic();
159 }
160 };
161}
162
163pub type CurrencyToVote = sp_staking::currency_to_vote::U128CurrencyToVote;
167static_assertions::assert_eq_size!(polkadot_primitives::Balance, u128);
168
169pub struct ParachainSessionKeyPlaceholder<T>(core::marker::PhantomData<T>);
172impl<T> sp_runtime::BoundToRuntimeAppPublic for ParachainSessionKeyPlaceholder<T> {
173 type Public = ValidatorId;
174}
175
176impl<T: pallet_session::Config> OneSessionHandler<T::AccountId>
177 for ParachainSessionKeyPlaceholder<T>
178{
179 type Key = ValidatorId;
180
181 fn on_genesis_session<'a, I: 'a>(_validators: I)
182 where
183 I: Iterator<Item = (&'a T::AccountId, ValidatorId)>,
184 T::AccountId: 'a,
185 {
186 }
187
188 fn on_new_session<'a, I: 'a>(_changed: bool, _v: I, _q: I)
189 where
190 I: Iterator<Item = (&'a T::AccountId, ValidatorId)>,
191 T::AccountId: 'a,
192 {
193 }
194
195 fn on_disabled(_: u32) {}
196}
197
198pub struct AssignmentSessionKeyPlaceholder<T>(core::marker::PhantomData<T>);
201impl<T> sp_runtime::BoundToRuntimeAppPublic for AssignmentSessionKeyPlaceholder<T> {
202 type Public = AssignmentId;
203}
204
205impl<T: pallet_session::Config> OneSessionHandler<T::AccountId>
206 for AssignmentSessionKeyPlaceholder<T>
207{
208 type Key = AssignmentId;
209
210 fn on_genesis_session<'a, I: 'a>(_validators: I)
211 where
212 I: Iterator<Item = (&'a T::AccountId, AssignmentId)>,
213 T::AccountId: 'a,
214 {
215 }
216
217 fn on_new_session<'a, I: 'a>(_changed: bool, _v: I, _q: I)
218 where
219 I: Iterator<Item = (&'a T::AccountId, AssignmentId)>,
220 T::AccountId: 'a,
221 {
222 }
223
224 fn on_disabled(_: u32) {}
225}
226
227pub struct StakingBenchmarkingConfig;
229impl pallet_staking::BenchmarkingConfig for StakingBenchmarkingConfig {
230 type MaxValidators = ConstU32<1000>;
231 type MaxNominators = ConstU32<1000>;
232}
233
234pub struct BalanceToU256;
236impl sp_runtime::traits::Convert<Balance, sp_core::U256> for BalanceToU256 {
237 fn convert(n: Balance) -> sp_core::U256 {
238 n.into()
239 }
240}
241
242pub struct U256ToBalance;
244impl sp_runtime::traits::Convert<sp_core::U256, Balance> for U256ToBalance {
245 fn convert(n: sp_core::U256) -> Balance {
246 use frame_support::traits::Defensive;
247 n.try_into().defensive_unwrap_or(Balance::MAX)
248 }
249}
250
251#[macro_export]
267macro_rules! prod_or_fast {
268 ($prod:expr, $test:expr) => {
269 if cfg!(feature = "fast-runtime") {
270 $test
271 } else {
272 $prod
273 }
274 };
275 ($prod:expr, $test:expr, $env:expr) => {
276 if cfg!(feature = "fast-runtime") {
277 core::option_env!($env).map(|s| s.parse().ok()).flatten().unwrap_or($test)
278 } else {
279 $prod
280 }
281 };
282}