polkadot_runtime_parachains/
lib.rs1#![cfg_attr(feature = "runtime-benchmarks", recursion_limit = "256")]
24#![cfg_attr(not(feature = "std"), no_std)]
25
26pub mod assigner_coretime;
27pub mod configuration;
28pub mod coretime;
29pub mod disputes;
30pub mod dmp;
31pub mod hrmp;
32pub mod inclusion;
33pub mod initializer;
34pub mod metrics;
35pub mod on_demand;
36pub mod origin;
37pub mod paras;
38pub mod paras_inherent;
39pub mod reward_points;
40pub mod scheduler;
41pub mod session_info;
42pub mod shared;
43
44pub mod runtime_api_impl;
45
46mod util;
47
48#[cfg(any(feature = "runtime-benchmarks", test))]
49mod builder;
50#[cfg(test)]
51mod mock;
52#[cfg(test)]
53mod ump_tests;
54
55extern crate alloc;
56
57pub use origin::{ensure_parachain, Origin};
58pub use paras::{ParaLifecycle, UpgradeStrategy};
59use polkadot_primitives::{HeadData, Id as ParaId, ValidationCode};
60use sp_arithmetic::traits::Saturating;
61use sp_runtime::{traits::Get, DispatchResult, FixedU128};
62
63pub trait FeeTracker {
65 type Id: Copy;
67
68 const MIN_FEE_FACTOR: FixedU128 = FixedU128::from_u32(1);
70 const EXPONENTIAL_FEE_BASE: FixedU128 = FixedU128::from_rational(105, 100); const MESSAGE_SIZE_FEE_BASE: FixedU128 = FixedU128::from_rational(1, 1000); fn get_fee_factor(id: Self::Id) -> FixedU128;
78
79 fn set_fee_factor(id: Self::Id, val: FixedU128);
81
82 fn do_increase_fee_factor(fee_factor: &mut FixedU128, message_size: u128) {
83 let message_size_factor = FixedU128::from(message_size.saturating_div(1024))
84 .saturating_mul(Self::MESSAGE_SIZE_FEE_BASE);
85 *fee_factor = fee_factor
86 .saturating_mul(Self::EXPONENTIAL_FEE_BASE.saturating_add(message_size_factor));
87 }
88
89 fn increase_fee_factor(id: Self::Id, message_size: u128) {
91 let mut fee_factor = Self::get_fee_factor(id);
92 Self::do_increase_fee_factor(&mut fee_factor, message_size);
93 Self::set_fee_factor(id, fee_factor);
94 }
95
96 fn do_decrease_fee_factor(fee_factor: &mut FixedU128) -> bool {
97 const { assert!(Self::EXPONENTIAL_FEE_BASE.into_inner() >= FixedU128::from_u32(1).into_inner()) }
98
99 if *fee_factor == Self::MIN_FEE_FACTOR {
100 return false;
101 }
102
103 *fee_factor = Self::MIN_FEE_FACTOR.max(*fee_factor / Self::EXPONENTIAL_FEE_BASE);
105 true
106 }
107
108 fn decrease_fee_factor(id: Self::Id) -> bool {
114 let mut fee_factor = Self::get_fee_factor(id);
115 let res = Self::do_decrease_fee_factor(&mut fee_factor);
116 Self::set_fee_factor(id, fee_factor);
117 res
118 }
119}
120
121pub struct GetMinFeeFactor<T>(core::marker::PhantomData<T>);
123
124impl<T: FeeTracker> Get<FixedU128> for GetMinFeeFactor<T> {
125 fn get() -> FixedU128 {
126 T::MIN_FEE_FACTOR
127 }
128}
129
130pub fn schedule_para_initialize<T: paras::Config>(
132 id: ParaId,
133 genesis: paras::ParaGenesisArgs,
134) -> Result<(), ()> {
135 paras::Pallet::<T>::schedule_para_initialize(id, genesis).map_err(|_| ())
136}
137
138pub fn schedule_para_cleanup<T: paras::Config>(id: polkadot_primitives::Id) -> Result<(), ()> {
140 paras::Pallet::<T>::schedule_para_cleanup(id).map_err(|_| ())
141}
142
143pub fn schedule_parathread_upgrade<T: paras::Config>(id: ParaId) -> Result<(), ()> {
145 paras::Pallet::<T>::schedule_parathread_upgrade(id).map_err(|_| ())
146}
147
148pub fn schedule_parachain_downgrade<T: paras::Config>(id: ParaId) -> Result<(), ()> {
150 paras::Pallet::<T>::schedule_parachain_downgrade(id).map_err(|_| ())
151}
152
153pub fn schedule_code_upgrade<T: paras::Config>(
155 id: ParaId,
156 new_code: ValidationCode,
157 set_go_ahead: UpgradeStrategy,
158) -> DispatchResult {
159 paras::Pallet::<T>::schedule_code_upgrade_external(id, new_code, set_go_ahead)
160}
161
162pub fn set_current_head<T: paras::Config>(id: ParaId, new_head: HeadData) {
164 paras::Pallet::<T>::set_current_head(id, new_head)
165}
166
167#[cfg(feature = "runtime-benchmarks")]
169pub trait EnsureForParachain {
170 fn ensure(para_id: ParaId);
171}
172
173#[cfg(feature = "runtime-benchmarks")]
174#[impl_trait_for_tuples::impl_for_tuples(30)]
175impl EnsureForParachain for Tuple {
176 fn ensure(para: ParaId) {
177 for_tuples!( #(
178 Tuple::ensure(para);
179 )* );
180 }
181}