kusama_runtime_constants/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17#![cfg_attr(not(feature = "std"), no_std)]
18
19pub mod weights;
20
21/// Money matters.
22pub mod currency {
23	use primitives::Balance;
24
25	/// The existential deposit.
26	pub const EXISTENTIAL_DEPOSIT: Balance = 1 * CENTS;
27
28	pub const UNITS: Balance = 1_000_000_000_000;
29	pub const QUID: Balance = UNITS / 30;
30	pub const CENTS: Balance = QUID / 100;
31	pub const GRAND: Balance = QUID * 1_000;
32	pub const MILLICENTS: Balance = CENTS / 1_000;
33
34	pub const fn deposit(items: u32, bytes: u32) -> Balance {
35		items as Balance * 2_000 * CENTS + (bytes as Balance) * 100 * MILLICENTS
36	}
37}
38
39/// Time and blocks.
40pub mod time {
41	use primitives::{BlockNumber, Moment};
42	use runtime_common::prod_or_fast;
43	pub const MILLISECS_PER_BLOCK: Moment = 6000;
44	pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK;
45	pub const EPOCH_DURATION_IN_SLOTS: BlockNumber = prod_or_fast!(1 * HOURS, 1 * MINUTES);
46
47	// These time units are defined in number of blocks.
48	pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
49	pub const HOURS: BlockNumber = MINUTES * 60;
50	pub const DAYS: BlockNumber = HOURS * 24;
51	pub const WEEKS: BlockNumber = DAYS * 7;
52
53	// 1 in 4 blocks (on average, not counting collisions) will be primary babe blocks.
54	// The choice of is done in accordance to the slot duration and expected target
55	// block time, for safely resisting network delays of maximum two seconds.
56	// <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
57	pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);
58}
59
60/// Fee-related.
61pub mod fee {
62	use crate::weights::ExtrinsicBaseWeight;
63	use frame_support::weights::{
64		WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
65	};
66	use primitives::Balance;
67	use smallvec::smallvec;
68	pub use sp_runtime::Perbill;
69
70	/// The block saturation level. Fees will be updates based on this value.
71	pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);
72
73	/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
74	/// node's balance type.
75	///
76	/// This should typically create a mapping between the following ranges:
77	///   - [0, `MAXIMUM_BLOCK_WEIGHT`]
78	///   - [Balance::min, Balance::max]
79	///
80	/// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
81	///   - Setting it to `0` will essentially disable the weight fee.
82	///   - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
83	pub struct WeightToFee;
84	impl WeightToFeePolynomial for WeightToFee {
85		type Balance = Balance;
86		fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
87			// in Kusama, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
88			let p = super::currency::CENTS;
89			let q = 10 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
90			smallvec![WeightToFeeCoefficient {
91				degree: 1,
92				negative: false,
93				coeff_frac: Perbill::from_rational(p % q, q),
94				coeff_integer: p / q,
95			}]
96		}
97	}
98}
99
100#[cfg(test)]
101mod tests {
102	use super::{
103		currency::{CENTS, MILLICENTS},
104		fee::WeightToFee,
105	};
106	use crate::weights::ExtrinsicBaseWeight;
107	use frame_support::weights::WeightToFee as WeightToFeeT;
108	use runtime_common::MAXIMUM_BLOCK_WEIGHT;
109
110	#[test]
111	// Test that the fee for `MAXIMUM_BLOCK_WEIGHT` of weight has sane bounds.
112	fn full_block_fee_is_correct() {
113		// A full block should cost between 1,000 and 10,000 CENTS.
114		let full_block = WeightToFee::weight_to_fee(&MAXIMUM_BLOCK_WEIGHT);
115		assert!(full_block >= 1_000 * CENTS);
116		assert!(full_block <= 10_000 * CENTS);
117	}
118
119	#[test]
120	// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
121	fn extrinsic_base_fee_is_correct() {
122		// `ExtrinsicBaseWeight` should cost 1/10 of a CENT
123		println!("Base: {}", ExtrinsicBaseWeight::get());
124		let x = WeightToFee::weight_to_fee(&ExtrinsicBaseWeight::get());
125		let y = CENTS / 10;
126		assert!(x.max(y) - x.min(y) < MILLICENTS);
127	}
128}