subspace_runtime_primitives/
lib.rs1#![feature(let_chains)]
3#![cfg_attr(not(feature = "std"), no_std)]
4
5pub mod extension;
6pub mod utility;
7
8#[cfg(not(feature = "std"))]
9extern crate alloc;
10
11use crate::time::{BLOCKS_IN_A_DAY, BLOCKS_IN_AN_MINUTE};
12#[cfg(not(feature = "std"))]
13use alloc::vec::Vec;
14use core::marker::PhantomData;
15use frame_support::pallet_prelude::Weight;
16use frame_support::traits::tokens;
17use frame_support::weights::WeightToFee;
18use frame_support::weights::constants::WEIGHT_REF_TIME_PER_SECOND;
19use frame_support::{Deserialize, Serialize};
20use frame_system::limits::BlockLength;
21use frame_system::offchain::CreateTransactionBase;
22use pallet_transaction_payment::{
23 Multiplier, NextFeeMultiplier, OnChargeTransaction, TargetedFeeAdjustment,
24};
25use parity_scale_codec::{Codec, Decode, Encode, MaxEncodedLen};
26use scale_info::TypeInfo;
27use sp_core::parameter_types;
28use sp_runtime::traits::{Block as BlockT, Bounded, Header as HeaderT, IdentifyAccount, Verify};
29use sp_runtime::{FixedPointNumber, MultiSignature, Perbill, Perquintill};
30pub use subspace_core_primitives::BlockNumber;
31
32pub const MIN_REPLICATION_FACTOR: u16 = 25;
35
36pub const SHANNON: Balance = 1;
38pub const DECIMAL_PLACES: u8 = 18;
40pub const AI3: Balance = (10 * SHANNON).pow(DECIMAL_PLACES as u32);
42pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
44pub const SLOT_PROBABILITY: (u64, u64) = (1, 6);
47pub const BLOCK_WEIGHT_FOR_2_SEC: Weight =
49 Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), u64::MAX);
50
51pub const MAX_BLOCK_LENGTH: u32 = 5 * 1024 * 1024;
53
54pub const DOMAINS_PRUNING_DEPTH_MULTIPLIER: u32 = 2;
56
57pub const DOMAINS_BLOCK_PRUNING_DEPTH: u32 = 14_400;
59
60pub fn maximum_normal_block_length() -> BlockLength {
62 BlockLength::max_with_normal_ratio(MAX_BLOCK_LENGTH, NORMAL_DISPATCH_RATIO)
63}
64
65pub const MAX_CALL_RECURSION_DEPTH: u32 = 10;
71
72pub type Signature = MultiSignature;
74
75pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
81
82pub type Balance = u128;
84
85pub type Nonce = u32;
87
88pub type Hash = sp_core::H256;
90
91pub type Moment = u64;
93
94pub type ExtrinsicFor<Block> = <Block as BlockT>::Extrinsic;
96
97pub type BlockHashFor<Block> = <Block as BlockT>::Hash;
99
100pub type HeaderFor<Block> = <Block as BlockT>::Header;
102
103pub type BlockHashingFor<Block> = <HeaderFor<Block> as HeaderT>::Hashing;
105
106parameter_types! {
107 pub const ConsensusEventSegmentSize: u32 = 0;
109 pub const DomainEventSegmentSize: u32 = 100;
111}
112
113pub mod opaque {
120 use super::BlockNumber;
121 pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
122 use sp_runtime::generic;
123 use sp_runtime::traits::BlakeTwo256;
124
125 pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
127 pub type Block = generic::Block<Header, UncheckedExtrinsic>;
129}
130
131pub mod time {
132 pub const MILLISECS_PER_BLOCK: u64 = 6000;
149 pub const BLOCKS_IN_AN_MINUTE: u32 = (60 * 1000) / MILLISECS_PER_BLOCK as u32;
151 pub const BLOCKS_IN_AN_HOUR: u32 = 60 * BLOCKS_IN_AN_MINUTE;
153 pub const BLOCKS_IN_A_DAY: u32 = 24 * BLOCKS_IN_AN_HOUR;
155}
156
157#[derive(Copy, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, MaxEncodedLen, Debug)]
158pub struct CouncilDemocracyConfigParams<BlockNumber> {
159 pub council_motion_duration: BlockNumber,
161 pub democracy_cooloff_period: BlockNumber,
163 pub democracy_enactment_period: BlockNumber,
165 pub democracy_fast_track_voting_period: BlockNumber,
167 pub democracy_launch_period: BlockNumber,
169 pub democracy_vote_locking_period: BlockNumber,
171 pub democracy_voting_period: BlockNumber,
173}
174
175impl<BlockNumber: From<u32>> Default for CouncilDemocracyConfigParams<BlockNumber> {
176 fn default() -> Self {
177 Self {
178 council_motion_duration: BLOCKS_IN_A_DAY.into(),
179 democracy_cooloff_period: BLOCKS_IN_A_DAY.into(),
180 democracy_enactment_period: BLOCKS_IN_A_DAY.into(),
181 democracy_fast_track_voting_period: (2 * BLOCKS_IN_A_DAY).into(),
182 democracy_launch_period: (2 * BLOCKS_IN_A_DAY).into(),
183 democracy_vote_locking_period: BLOCKS_IN_A_DAY.into(),
184 democracy_voting_period: BLOCKS_IN_A_DAY.into(),
185 }
186 }
187}
188
189impl<BlockNumber: From<u32>> CouncilDemocracyConfigParams<BlockNumber> {
190 pub fn production_params() -> Self {
192 Self::default()
193 }
194
195 pub fn fast_params() -> Self {
197 Self {
198 council_motion_duration: (15 * BLOCKS_IN_AN_MINUTE).into(),
199 democracy_cooloff_period: (5 * BLOCKS_IN_AN_MINUTE).into(),
200 democracy_enactment_period: (15 * BLOCKS_IN_AN_MINUTE).into(),
201 democracy_fast_track_voting_period: (5 * BLOCKS_IN_AN_MINUTE).into(),
202 democracy_launch_period: (15 * BLOCKS_IN_AN_MINUTE).into(),
203 democracy_vote_locking_period: BLOCKS_IN_AN_MINUTE.into(),
204 democracy_voting_period: (15 * BLOCKS_IN_AN_MINUTE).into(),
205 }
206 }
207}
208
209pub trait RewardsEnabled {
211 fn rewards_enabled() -> bool;
213}
214
215pub trait FindBlockRewardAddress<RewardAddress> {
217 fn find_block_reward_address() -> Option<RewardAddress>;
219}
220
221pub trait FindVotingRewardAddresses<RewardAddress> {
223 fn find_voting_reward_addresses() -> Vec<RewardAddress>;
225}
226
227pub trait StorageFee<Balance> {
228 fn transaction_byte_fee() -> Balance;
230
231 fn note_storage_fees(fee: Balance);
233}
234
235parameter_types! {
236 pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(50);
239 pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(75, 1_000_000);
242 pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 10u128);
246 pub MaximumMultiplier: Multiplier = Bounded::max_value();
248}
249
250pub type SlowAdjustingFeeUpdate<R, TargetBlockFullness> = TargetedFeeAdjustment<
253 R,
254 TargetBlockFullness,
255 AdjustmentVariable,
256 MinimumMultiplier,
257 MaximumMultiplier,
258>;
259
260#[derive(Encode, Decode, TypeInfo)]
261pub struct BlockTransactionByteFee<Balance: Codec> {
262 pub current: Balance,
264 pub next: Balance,
266}
267
268impl<Balance: Codec + tokens::Balance> Default for BlockTransactionByteFee<Balance> {
269 fn default() -> Self {
270 BlockTransactionByteFee {
271 current: Balance::max_value(),
272 next: Balance::max_value(),
273 }
274 }
275}
276
277parameter_types! {
278 pub const XdmFeeMultipler: u32 = 5;
279}
280
281pub type OnChargeTransactionBalance<T> = <<T as pallet_transaction_payment::Config>::OnChargeTransaction as OnChargeTransaction<
283 T,
284>>::Balance;
285
286pub struct XdmAdjustedWeightToFee<T>(PhantomData<T>);
288impl<T: pallet_transaction_payment::Config> WeightToFee for XdmAdjustedWeightToFee<T> {
289 type Balance = OnChargeTransactionBalance<T>;
290
291 fn weight_to_fee(weight: &Weight) -> Self::Balance {
292 let unadjusted_weight_fee = pallet_transaction_payment::Pallet::<T>::weight_to_fee(*weight);
294 let multiplier = NextFeeMultiplier::<T>::get();
295 multiplier.saturating_mul_int(unadjusted_weight_fee)
297 }
298}
299
300#[derive(
301 PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
302)]
303pub enum HoldIdentifier {
304 DomainStaking,
305 DomainInstantiation,
306 DomainStorageFund,
307 MessengerChannel,
308 Preimage,
309}
310
311pub trait CreateUnsigned<LocalCall>: CreateTransactionBase<LocalCall> {
313 fn create_unsigned(call: Self::RuntimeCall) -> Self::Extrinsic;
315}
316
317#[cfg(feature = "testing")]
318pub mod tests_utils {
319 use frame_support::dispatch::DispatchClass;
320 use frame_support::weights::Weight;
321 use frame_system::limits::BlockWeights;
322 use pallet_transaction_payment::{Multiplier, MultiplierUpdate};
323 use sp_runtime::BuildStorage;
324 use sp_runtime::traits::{Convert, Get};
325 use std::marker::PhantomData;
326
327 pub struct FeeMultiplierUtils<Runtime, BlockWeightsGetter>(
328 PhantomData<(Runtime, BlockWeightsGetter)>,
329 );
330
331 impl<Runtime, BlockWeightsGetter> FeeMultiplierUtils<Runtime, BlockWeightsGetter>
332 where
333 Runtime: frame_system::Config + pallet_transaction_payment::Config,
334 BlockWeightsGetter: Get<BlockWeights>,
335 {
336 fn max_normal() -> Weight {
337 let block_weights = BlockWeightsGetter::get();
338 block_weights
339 .get(DispatchClass::Normal)
340 .max_total
341 .unwrap_or(block_weights.max_block)
342 }
343
344 fn min_multiplier() -> Multiplier {
345 <Runtime as pallet_transaction_payment::Config>::FeeMultiplierUpdate::min()
346 }
347
348 fn target() -> Weight {
349 <Runtime as pallet_transaction_payment::Config>::FeeMultiplierUpdate::target()
350 * Self::max_normal()
351 }
352
353 fn runtime_multiplier_update(fm: Multiplier) -> Multiplier {
355 <Runtime as pallet_transaction_payment::Config>::FeeMultiplierUpdate::convert(fm)
356 }
357
358 fn run_with_system_weight<F>(w: Weight, assertions: F)
359 where
360 F: Fn(),
361 {
362 let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
363 .build_storage()
364 .unwrap()
365 .into();
366 t.execute_with(|| {
367 frame_system::Pallet::<Runtime>::set_block_consumed_resources(w, 0);
368 assertions()
369 });
370 }
371
372 pub fn multiplier_can_grow_from_zero()
376 where
377 Runtime: pallet_transaction_payment::Config,
378 BlockWeightsGetter: Get<BlockWeights>,
379 {
380 Self::run_with_system_weight(
383 Self::target().set_ref_time((Self::target().ref_time() / 100) * 101),
384 || {
385 let next = Self::runtime_multiplier_update(Self::min_multiplier());
386 assert!(
387 next > Self::min_multiplier(),
388 "{:?} !> {:?}",
389 next,
390 Self::min_multiplier()
391 );
392 },
393 );
394
395 Self::run_with_system_weight(
397 Self::target().set_proof_size((Self::target().proof_size() / 100) * 101),
398 || {
399 let next = Self::runtime_multiplier_update(Self::min_multiplier());
400 assert!(
401 next > Self::min_multiplier(),
402 "{:?} !> {:?}",
403 next,
404 Self::min_multiplier()
405 );
406 },
407 )
408 }
409 }
410}