subspace_runtime_primitives/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
4
5pub mod utility;
6
7#[cfg(not(feature = "std"))]
8extern crate alloc;
9
10use crate::time::{BLOCKS_IN_AN_MINUTE, BLOCKS_IN_A_DAY};
11#[cfg(not(feature = "std"))]
12use alloc::vec::Vec;
13use core::marker::PhantomData;
14use frame_support::pallet_prelude::Weight;
15use frame_support::traits::tokens;
16use frame_support::weights::constants::WEIGHT_REF_TIME_PER_SECOND;
17use frame_support::weights::WeightToFee;
18use frame_support::{Deserialize, Serialize};
19use frame_system::limits::BlockLength;
20use frame_system::offchain::CreateTransactionBase;
21use pallet_transaction_payment::{
22 Multiplier, NextFeeMultiplier, OnChargeTransaction, TargetedFeeAdjustment,
23};
24use parity_scale_codec::{Codec, Decode, Encode, MaxEncodedLen};
25use scale_info::TypeInfo;
26use sp_core::parameter_types;
27use sp_runtime::traits::{Bounded, IdentifyAccount, Verify};
28use sp_runtime::{FixedPointNumber, MultiSignature, Perbill, Perquintill};
29pub use subspace_core_primitives::BlockNumber;
30
31pub const MIN_REPLICATION_FACTOR: u16 = 25;
34
35pub const SHANNON: Balance = 1;
37pub const DECIMAL_PLACES: u8 = 18;
39pub const SSC: Balance = (10 * SHANNON).pow(DECIMAL_PLACES as u32);
41pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
43pub const SLOT_PROBABILITY: (u64, u64) = (1, 6);
46pub const BLOCK_WEIGHT_FOR_2_SEC: Weight =
48 Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), u64::MAX);
49
50pub const MAX_BLOCK_LENGTH: u32 = 5 * 1024 * 1024;
52
53pub const DOMAINS_PRUNING_DEPTH_MULTIPLIER: u32 = 2;
55
56pub const DOMAINS_BLOCK_PRUNING_DEPTH: u32 = 14_400;
58
59pub fn maximum_normal_block_length() -> BlockLength {
61 BlockLength::max_with_normal_ratio(MAX_BLOCK_LENGTH, NORMAL_DISPATCH_RATIO)
62}
63
64pub const MAX_CALL_RECURSION_DEPTH: u32 = 10;
70
71pub type Signature = MultiSignature;
73
74pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
80
81pub type Balance = u128;
83
84pub type Nonce = u32;
86
87pub type Hash = sp_core::H256;
89
90pub type Moment = u64;
92
93parameter_types! {
94 pub const ConsensusEventSegmentSize: u32 = 0;
96 pub const DomainEventSegmentSize: u32 = 100;
98}
99
100pub mod opaque {
107 use super::BlockNumber;
108 use sp_runtime::generic;
109 use sp_runtime::traits::BlakeTwo256;
110 pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
111
112 pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
114 pub type Block = generic::Block<Header, UncheckedExtrinsic>;
116}
117
118pub mod time {
119 pub const MILLISECS_PER_BLOCK: u64 = 6000;
136 pub const BLOCKS_IN_AN_MINUTE: u32 = (60 * 1000) / MILLISECS_PER_BLOCK as u32;
138 pub const BLOCKS_IN_AN_HOUR: u32 = 60 * BLOCKS_IN_AN_MINUTE;
140 pub const BLOCKS_IN_A_DAY: u32 = 24 * BLOCKS_IN_AN_HOUR;
142}
143
144#[derive(Copy, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, MaxEncodedLen, Debug)]
145pub struct CouncilDemocracyConfigParams<BlockNumber> {
146 pub council_motion_duration: BlockNumber,
148 pub democracy_cooloff_period: BlockNumber,
150 pub democracy_enactment_period: BlockNumber,
152 pub democracy_fast_track_voting_period: BlockNumber,
154 pub democracy_launch_period: BlockNumber,
156 pub democracy_vote_locking_period: BlockNumber,
158 pub democracy_voting_period: BlockNumber,
160}
161
162impl<BlockNumber: From<u32>> Default for CouncilDemocracyConfigParams<BlockNumber> {
163 fn default() -> Self {
164 Self {
165 council_motion_duration: BLOCKS_IN_A_DAY.into(),
166 democracy_cooloff_period: BLOCKS_IN_A_DAY.into(),
167 democracy_enactment_period: BLOCKS_IN_A_DAY.into(),
168 democracy_fast_track_voting_period: (2 * BLOCKS_IN_A_DAY).into(),
169 democracy_launch_period: (2 * BLOCKS_IN_A_DAY).into(),
170 democracy_vote_locking_period: BLOCKS_IN_A_DAY.into(),
171 democracy_voting_period: BLOCKS_IN_A_DAY.into(),
172 }
173 }
174}
175
176impl<BlockNumber: From<u32>> CouncilDemocracyConfigParams<BlockNumber> {
177 pub fn production_params() -> Self {
179 Self::default()
180 }
181
182 pub fn fast_params() -> Self {
184 Self {
185 council_motion_duration: (15 * BLOCKS_IN_AN_MINUTE).into(),
186 democracy_cooloff_period: (5 * BLOCKS_IN_AN_MINUTE).into(),
187 democracy_enactment_period: (15 * BLOCKS_IN_AN_MINUTE).into(),
188 democracy_fast_track_voting_period: (5 * BLOCKS_IN_AN_MINUTE).into(),
189 democracy_launch_period: (15 * BLOCKS_IN_AN_MINUTE).into(),
190 democracy_vote_locking_period: BLOCKS_IN_AN_MINUTE.into(),
191 democracy_voting_period: (15 * BLOCKS_IN_AN_MINUTE).into(),
192 }
193 }
194}
195
196pub trait RewardsEnabled {
198 fn rewards_enabled() -> bool;
200}
201
202pub trait FindBlockRewardAddress<RewardAddress> {
204 fn find_block_reward_address() -> Option<RewardAddress>;
206}
207
208pub trait FindVotingRewardAddresses<RewardAddress> {
210 fn find_voting_reward_addresses() -> Vec<RewardAddress>;
212}
213
214pub trait StorageFee<Balance> {
215 fn transaction_byte_fee() -> Balance;
217
218 fn note_storage_fees(fee: Balance);
220}
221
222parameter_types! {
223 pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(50);
226 pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(75, 1_000_000);
229 pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 10u128);
233 pub MaximumMultiplier: Multiplier = Bounded::max_value();
235}
236
237pub type SlowAdjustingFeeUpdate<R, TargetBlockFullness> = TargetedFeeAdjustment<
240 R,
241 TargetBlockFullness,
242 AdjustmentVariable,
243 MinimumMultiplier,
244 MaximumMultiplier,
245>;
246
247#[derive(Encode, Decode, TypeInfo)]
248pub struct BlockTransactionByteFee<Balance: Codec> {
249 pub current: Balance,
251 pub next: Balance,
253}
254
255impl<Balance: Codec + tokens::Balance> Default for BlockTransactionByteFee<Balance> {
256 fn default() -> Self {
257 BlockTransactionByteFee {
258 current: Balance::max_value(),
259 next: Balance::max_value(),
260 }
261 }
262}
263
264parameter_types! {
265 pub const XdmFeeMultipler: u32 = 5;
266}
267
268pub type OnChargeTransactionBalance<T> = <<T as pallet_transaction_payment::Config>::OnChargeTransaction as OnChargeTransaction<
270 T,
271>>::Balance;
272
273pub struct XdmAdjustedWeightToFee<T>(PhantomData<T>);
275impl<T: pallet_transaction_payment::Config> WeightToFee for XdmAdjustedWeightToFee<T> {
276 type Balance = OnChargeTransactionBalance<T>;
277
278 fn weight_to_fee(weight: &Weight) -> Self::Balance {
279 let unadjusted_weight_fee = pallet_transaction_payment::Pallet::<T>::weight_to_fee(*weight);
281 let multiplier = NextFeeMultiplier::<T>::get();
282 multiplier.saturating_mul_int(unadjusted_weight_fee)
284 }
285}
286
287#[derive(
288 PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
289)]
290pub enum HoldIdentifier {
291 DomainStaking,
292 DomainInstantiation,
293 DomainStorageFund,
294 MessengerChannel,
295 Preimage,
296}
297
298pub trait CreateUnsigned<LocalCall>: CreateTransactionBase<LocalCall> {
300 fn create_unsigned(call: Self::RuntimeCall) -> Self::Extrinsic;
302}
303
304#[cfg(feature = "testing")]
305pub mod tests_utils {
306 use frame_support::dispatch::DispatchClass;
307 use frame_support::weights::Weight;
308 use frame_system::limits::BlockWeights;
309 use pallet_transaction_payment::{Multiplier, MultiplierUpdate};
310 use sp_runtime::traits::{Convert, Get};
311 use sp_runtime::BuildStorage;
312 use std::marker::PhantomData;
313
314 pub struct FeeMultiplierUtils<Runtime, BlockWeightsGetter>(
315 PhantomData<(Runtime, BlockWeightsGetter)>,
316 );
317
318 impl<Runtime, BlockWeightsGetter> FeeMultiplierUtils<Runtime, BlockWeightsGetter>
319 where
320 Runtime: frame_system::Config + pallet_transaction_payment::Config,
321 BlockWeightsGetter: Get<BlockWeights>,
322 {
323 fn max_normal() -> Weight {
324 let block_weights = BlockWeightsGetter::get();
325 block_weights
326 .get(DispatchClass::Normal)
327 .max_total
328 .unwrap_or(block_weights.max_block)
329 }
330
331 fn min_multiplier() -> Multiplier {
332 <Runtime as pallet_transaction_payment::Config>::FeeMultiplierUpdate::min()
333 }
334
335 fn target() -> Weight {
336 <Runtime as pallet_transaction_payment::Config>::FeeMultiplierUpdate::target()
337 * Self::max_normal()
338 }
339
340 fn runtime_multiplier_update(fm: Multiplier) -> Multiplier {
342 <Runtime as pallet_transaction_payment::Config>::FeeMultiplierUpdate::convert(fm)
343 }
344
345 fn run_with_system_weight<F>(w: Weight, assertions: F)
346 where
347 F: Fn(),
348 {
349 let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
350 .build_storage()
351 .unwrap()
352 .into();
353 t.execute_with(|| {
354 frame_system::Pallet::<Runtime>::set_block_consumed_resources(w, 0);
355 assertions()
356 });
357 }
358
359 pub fn multiplier_can_grow_from_zero()
363 where
364 Runtime: pallet_transaction_payment::Config,
365 BlockWeightsGetter: Get<BlockWeights>,
366 {
367 Self::run_with_system_weight(
370 Self::target().set_ref_time((Self::target().ref_time() / 100) * 101),
371 || {
372 let next = Self::runtime_multiplier_update(Self::min_multiplier());
373 assert!(
374 next > Self::min_multiplier(),
375 "{:?} !> {:?}",
376 next,
377 Self::min_multiplier()
378 );
379 },
380 );
381
382 Self::run_with_system_weight(
384 Self::target().set_proof_size((Self::target().proof_size() / 100) * 101),
385 || {
386 let next = Self::runtime_multiplier_update(Self::min_multiplier());
387 assert!(
388 next > Self::min_multiplier(),
389 "{:?} !> {:?}",
390 next,
391 Self::min_multiplier()
392 );
393 },
394 )
395 }
396 }
397}