constants.rs 4.09 KB
Newer Older
Shawn Tabrizi's avatar
Shawn Tabrizi committed
1
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

/// Money matters.
pub mod currency {
asynchronous rob's avatar
asynchronous rob committed
19
	use primitives::v0::Balance;
20

21
	pub const UNITS: Balance = 10_000_000_000;
Shawn Tabrizi's avatar
Shawn Tabrizi committed
22
23
	pub const DOLLARS: Balance = UNITS; // 10_000_000_000
	pub const CENTS: Balance = DOLLARS / 100; // 100_000_000
24
	pub const MILLICENTS: Balance = CENTS / 1_000; // 100_000
25
26

	pub const fn deposit(items: u32, bytes: u32) -> Balance {
27
		items as Balance * 20 * DOLLARS + (bytes as Balance) * 100 * MILLICENTS
28
	}
29
30
}

31
/// Time and blocks.
32
pub mod time {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
33
	use primitives::v0::{BlockNumber, Moment};
34
	pub const MILLISECS_PER_BLOCK: Moment = 6000;
35
	pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK;
36
	pub const EPOCH_DURATION_IN_SLOTS: BlockNumber = 4 * HOURS;
37
38

	// These time units are defined in number of blocks.
39
	pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
40
41
	pub const HOURS: BlockNumber = MINUTES * 60;
	pub const DAYS: BlockNumber = HOURS * 24;
42
	pub const WEEKS: BlockNumber = DAYS * 7;
43
44
45

	// 1 in 4 blocks (on average, not counting collisions) will be primary babe blocks.
	pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);
46
47
48
49
}

/// Fee-related.
pub mod fee {
50
	use frame_support::weights::{
Shawn Tabrizi's avatar
Shawn Tabrizi committed
51
		WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
52
	};
Shawn Tabrizi's avatar
Shawn Tabrizi committed
53
54
	use primitives::v0::Balance;
	use runtime_common::ExtrinsicBaseWeight;
55
	use smallvec::smallvec;
Shawn Tabrizi's avatar
Shawn Tabrizi committed
56
	pub use sp_runtime::Perbill;
57
58
59

	/// The block saturation level. Fees will be updates based on this value.
	pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);
60
61
62
63
64

	/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
	/// node's balance type.
	///
	/// This should typically create a mapping between the following ranges:
Denis_P's avatar
Denis_P committed
65
	///   - [0, `MAXIMUM_BLOCK_WEIGHT`]
66
67
68
69
70
71
	///   - [Balance::min, Balance::max]
	///
	/// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
	///   - Setting it to `0` will essentially disable the weight fee.
	///   - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
	pub struct WeightToFee;
72
73
74
	impl WeightToFeePolynomial for WeightToFee {
		type Balance = Balance;
		fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
75
			// in Polkadot, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
76
77
78
79
80
			let p = super::currency::CENTS;
			let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
			smallvec![WeightToFeeCoefficient {
				degree: 1,
				negative: false,
Kian Paimani's avatar
Kian Paimani committed
81
				coeff_frac: Perbill::from_rational(p % q, q),
82
83
				coeff_integer: p / q,
			}]
84
85
		}
	}
86
}
87
88
89

#[cfg(test)]
mod tests {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
90
91
92
93
	use super::{
		currency::{CENTS, DOLLARS, MILLICENTS},
		fee::WeightToFee,
	};
94
	use frame_support::weights::WeightToFeePolynomial;
Shawn Tabrizi's avatar
Shawn Tabrizi committed
95
	use runtime_common::{ExtrinsicBaseWeight, MAXIMUM_BLOCK_WEIGHT};
96
97

	#[test]
98
	// This function tests that the fee for `MAXIMUM_BLOCK_WEIGHT` of weight is correct
99
100
	fn full_block_fee_is_correct() {
		// A full block should cost 16 DOLLARS
101
		println!("Base: {}", ExtrinsicBaseWeight::get());
102
		let x = WeightToFee::calc(&MAXIMUM_BLOCK_WEIGHT);
103
104
		let y = 16 * DOLLARS;
		assert!(x.max(y) - x.min(y) < MILLICENTS);
105
106
107
108
109
110
	}

	#[test]
	// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
	fn extrinsic_base_fee_is_correct() {
		// `ExtrinsicBaseWeight` should cost 1/10 of a CENT
111
112
113
114
		println!("Base: {}", ExtrinsicBaseWeight::get());
		let x = WeightToFee::calc(&ExtrinsicBaseWeight::get());
		let y = CENTS / 10;
		assert!(x.max(y) - x.min(y) < MILLICENTS);
115
116
	}
}