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::{Block as BlockT, 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
93pub type ExtrinsicFor<Block> = <Block as BlockT>::Extrinsic;
95
96pub type BlockHashFor<Block> = <Block as BlockT>::Hash;
98
99pub type HeaderFor<Block> = <Block as BlockT>::Header;
101
102parameter_types! {
103 pub const ConsensusEventSegmentSize: u32 = 0;
105 pub const DomainEventSegmentSize: u32 = 100;
107}
108
109pub mod opaque {
116 use super::BlockNumber;
117 use sp_runtime::generic;
118 use sp_runtime::traits::BlakeTwo256;
119 pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
120
121 pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
123 pub type Block = generic::Block<Header, UncheckedExtrinsic>;
125}
126
127pub mod time {
128 pub const MILLISECS_PER_BLOCK: u64 = 6000;
145 pub const BLOCKS_IN_AN_MINUTE: u32 = (60 * 1000) / MILLISECS_PER_BLOCK as u32;
147 pub const BLOCKS_IN_AN_HOUR: u32 = 60 * BLOCKS_IN_AN_MINUTE;
149 pub const BLOCKS_IN_A_DAY: u32 = 24 * BLOCKS_IN_AN_HOUR;
151}
152
153#[derive(Copy, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, MaxEncodedLen, Debug)]
154pub struct CouncilDemocracyConfigParams<BlockNumber> {
155 pub council_motion_duration: BlockNumber,
157 pub democracy_cooloff_period: BlockNumber,
159 pub democracy_enactment_period: BlockNumber,
161 pub democracy_fast_track_voting_period: BlockNumber,
163 pub democracy_launch_period: BlockNumber,
165 pub democracy_vote_locking_period: BlockNumber,
167 pub democracy_voting_period: BlockNumber,
169}
170
171impl<BlockNumber: From<u32>> Default for CouncilDemocracyConfigParams<BlockNumber> {
172 fn default() -> Self {
173 Self {
174 council_motion_duration: BLOCKS_IN_A_DAY.into(),
175 democracy_cooloff_period: BLOCKS_IN_A_DAY.into(),
176 democracy_enactment_period: BLOCKS_IN_A_DAY.into(),
177 democracy_fast_track_voting_period: (2 * BLOCKS_IN_A_DAY).into(),
178 democracy_launch_period: (2 * BLOCKS_IN_A_DAY).into(),
179 democracy_vote_locking_period: BLOCKS_IN_A_DAY.into(),
180 democracy_voting_period: BLOCKS_IN_A_DAY.into(),
181 }
182 }
183}
184
185impl<BlockNumber: From<u32>> CouncilDemocracyConfigParams<BlockNumber> {
186 pub fn production_params() -> Self {
188 Self::default()
189 }
190
191 pub fn fast_params() -> Self {
193 Self {
194 council_motion_duration: (15 * BLOCKS_IN_AN_MINUTE).into(),
195 democracy_cooloff_period: (5 * BLOCKS_IN_AN_MINUTE).into(),
196 democracy_enactment_period: (15 * BLOCKS_IN_AN_MINUTE).into(),
197 democracy_fast_track_voting_period: (5 * BLOCKS_IN_AN_MINUTE).into(),
198 democracy_launch_period: (15 * BLOCKS_IN_AN_MINUTE).into(),
199 democracy_vote_locking_period: BLOCKS_IN_AN_MINUTE.into(),
200 democracy_voting_period: (15 * BLOCKS_IN_AN_MINUTE).into(),
201 }
202 }
203}
204
205pub trait RewardsEnabled {
207 fn rewards_enabled() -> bool;
209}
210
211pub trait FindBlockRewardAddress<RewardAddress> {
213 fn find_block_reward_address() -> Option<RewardAddress>;
215}
216
217pub trait FindVotingRewardAddresses<RewardAddress> {
219 fn find_voting_reward_addresses() -> Vec<RewardAddress>;
221}
222
223pub trait StorageFee<Balance> {
224 fn transaction_byte_fee() -> Balance;
226
227 fn note_storage_fees(fee: Balance);
229}
230
231parameter_types! {
232 pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(50);
235 pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(75, 1_000_000);
238 pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 10u128);
242 pub MaximumMultiplier: Multiplier = Bounded::max_value();
244}
245
246pub type SlowAdjustingFeeUpdate<R, TargetBlockFullness> = TargetedFeeAdjustment<
249 R,
250 TargetBlockFullness,
251 AdjustmentVariable,
252 MinimumMultiplier,
253 MaximumMultiplier,
254>;
255
256#[derive(Encode, Decode, TypeInfo)]
257pub struct BlockTransactionByteFee<Balance: Codec> {
258 pub current: Balance,
260 pub next: Balance,
262}
263
264impl<Balance: Codec + tokens::Balance> Default for BlockTransactionByteFee<Balance> {
265 fn default() -> Self {
266 BlockTransactionByteFee {
267 current: Balance::max_value(),
268 next: Balance::max_value(),
269 }
270 }
271}
272
273parameter_types! {
274 pub const XdmFeeMultipler: u32 = 5;
275}
276
277pub type OnChargeTransactionBalance<T> = <<T as pallet_transaction_payment::Config>::OnChargeTransaction as OnChargeTransaction<
279 T,
280>>::Balance;
281
282pub struct XdmAdjustedWeightToFee<T>(PhantomData<T>);
284impl<T: pallet_transaction_payment::Config> WeightToFee for XdmAdjustedWeightToFee<T> {
285 type Balance = OnChargeTransactionBalance<T>;
286
287 fn weight_to_fee(weight: &Weight) -> Self::Balance {
288 let unadjusted_weight_fee = pallet_transaction_payment::Pallet::<T>::weight_to_fee(*weight);
290 let multiplier = NextFeeMultiplier::<T>::get();
291 multiplier.saturating_mul_int(unadjusted_weight_fee)
293 }
294}
295
296#[derive(
297 PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
298)]
299pub enum HoldIdentifier {
300 DomainStaking,
301 DomainInstantiation,
302 DomainStorageFund,
303 MessengerChannel,
304 Preimage,
305}
306
307pub trait CreateUnsigned<LocalCall>: CreateTransactionBase<LocalCall> {
309 fn create_unsigned(call: Self::RuntimeCall) -> Self::Extrinsic;
311}
312
313#[cfg(feature = "testing")]
314pub mod tests_utils {
315 use frame_support::dispatch::DispatchClass;
316 use frame_support::weights::Weight;
317 use frame_system::limits::BlockWeights;
318 use pallet_transaction_payment::{Multiplier, MultiplierUpdate};
319 use sp_runtime::traits::{Convert, Get};
320 use sp_runtime::BuildStorage;
321 use std::marker::PhantomData;
322
323 pub struct FeeMultiplierUtils<Runtime, BlockWeightsGetter>(
324 PhantomData<(Runtime, BlockWeightsGetter)>,
325 );
326
327 impl<Runtime, BlockWeightsGetter> FeeMultiplierUtils<Runtime, BlockWeightsGetter>
328 where
329 Runtime: frame_system::Config + pallet_transaction_payment::Config,
330 BlockWeightsGetter: Get<BlockWeights>,
331 {
332 fn max_normal() -> Weight {
333 let block_weights = BlockWeightsGetter::get();
334 block_weights
335 .get(DispatchClass::Normal)
336 .max_total
337 .unwrap_or(block_weights.max_block)
338 }
339
340 fn min_multiplier() -> Multiplier {
341 <Runtime as pallet_transaction_payment::Config>::FeeMultiplierUpdate::min()
342 }
343
344 fn target() -> Weight {
345 <Runtime as pallet_transaction_payment::Config>::FeeMultiplierUpdate::target()
346 * Self::max_normal()
347 }
348
349 fn runtime_multiplier_update(fm: Multiplier) -> Multiplier {
351 <Runtime as pallet_transaction_payment::Config>::FeeMultiplierUpdate::convert(fm)
352 }
353
354 fn run_with_system_weight<F>(w: Weight, assertions: F)
355 where
356 F: Fn(),
357 {
358 let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
359 .build_storage()
360 .unwrap()
361 .into();
362 t.execute_with(|| {
363 frame_system::Pallet::<Runtime>::set_block_consumed_resources(w, 0);
364 assertions()
365 });
366 }
367
368 pub fn multiplier_can_grow_from_zero()
372 where
373 Runtime: pallet_transaction_payment::Config,
374 BlockWeightsGetter: Get<BlockWeights>,
375 {
376 Self::run_with_system_weight(
379 Self::target().set_ref_time((Self::target().ref_time() / 100) * 101),
380 || {
381 let next = Self::runtime_multiplier_update(Self::min_multiplier());
382 assert!(
383 next > Self::min_multiplier(),
384 "{:?} !> {:?}",
385 next,
386 Self::min_multiplier()
387 );
388 },
389 );
390
391 Self::run_with_system_weight(
393 Self::target().set_proof_size((Self::target().proof_size() / 100) * 101),
394 || {
395 let next = Self::runtime_multiplier_update(Self::min_multiplier());
396 assert!(
397 next > Self::min_multiplier(),
398 "{:?} !> {:?}",
399 next,
400 Self::min_multiplier()
401 );
402 },
403 )
404 }
405 }
406}