lib.rs 7.14 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
19
20
21
22
23
24
// 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/>.

//! Common runtime code for Polkadot and Kusama.

#![cfg_attr(not(feature = "std"), no_std)]

pub mod claims;
pub mod slot_range;
pub mod slots;
pub mod crowdfund;
Shawn Tabrizi's avatar
Shawn Tabrizi committed
25
pub mod purchase;
26
pub mod impls;
27
pub mod paras_sudo_wrapper;
28
pub mod paras_registrar;
29

30
use primitives::v1::{BlockNumber, ValidatorId};
31
use sp_runtime::{Perquintill, Perbill, FixedPointNumber, traits::Saturating};
32
use frame_support::{
33
	parameter_types, traits::{Currency},
34
	weights::{Weight, constants::WEIGHT_PER_SECOND},
35
};
36
use pallet_transaction_payment::{TargetedFeeAdjustment, Multiplier};
37
use static_assertions::const_assert;
38
39
pub use frame_support::weights::constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};

40
#[cfg(feature = "std")]
41
pub use pallet_staking::StakerStatus;
42
43
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
44
45
pub use pallet_timestamp::Call as TimestampCall;
pub use pallet_balances::Call as BalancesCall;
46
47

/// Implementations of some helper traits passed into runtime modules as associated types.
48
pub use impls::ToAuthor;
49

50
pub type NegativeImbalance<T> = <pallet_balances::Module<T> as Currency<<T as frame_system::Trait>::AccountId>>::NegativeImbalance;
51

52
53
/// We assume that an on-initialize consumes 10% of the weight on average, hence a single extrinsic
/// will not be allowed to consume more than `AvailableBlockRatio - 10%`.
54
pub const AVERAGE_ON_INITIALIZE_WEIGHT: Perbill = Perbill::from_percent(10);
55
56

// Common constants used in all runtimes.
57
parameter_types! {
58
	pub const BlockHashCount: BlockNumber = 2400;
59
	/// Block time that can be used by weights.
60
	pub const MaximumBlockWeight: Weight = 2 * WEIGHT_PER_SECOND;
61
	/// Portion of the block available to normal class of dispatches.
62
	pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
63
	/// Maximum weight that a _single_ extrinsic can take.
64
65
66
	pub MaximumExtrinsicWeight: Weight =
		AvailableBlockRatio::get().saturating_sub(AVERAGE_ON_INITIALIZE_WEIGHT)
		* MaximumBlockWeight::get();
67
	/// Maximum length of block. 5MB.
68
	pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;
69
70
71
72
73
74
75
76
77
78
	/// The portion of the `AvailableBlockRatio` that we adjust the fees with. Blocks filled less
	/// than this will decrease the weight and more will increase.
	pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
	/// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to
	/// change the fees more rapidly.
	pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 100_000);
	/// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure
	/// that combined with `AdjustmentVariable`, we can recover from the minimum.
	/// See `multiplier_can_grow_from_zero`.
	pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000_000u128);
79
}
Gavin Wood's avatar
Gavin Wood committed
80

81
const_assert!(AvailableBlockRatio::get().deconstruct() >= AVERAGE_ON_INITIALIZE_WEIGHT.deconstruct());
82
83
84
85
86
87
88
89
90
91

/// Parameterized slow adjusting fee updated based on
/// https://w3f-research.readthedocs.io/en/latest/polkadot/Token%20Economics.html#-2.-slow-adjusting-mechanism
pub type SlowAdjustingFeeUpdate<R> = TargetedFeeAdjustment<
	R,
	TargetBlockFullness,
	AdjustmentVariable,
	MinimumMultiplier
>;

92
93
94
95
96
97
/// The type used for currency conversion.
///
/// This must only be used as long as the balance type is u128.
pub type CurrencyToVote = frame_support::traits::U128CurrencyToVote;
static_assertions::assert_eq_size!(primitives::v1::Balance, u128);

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/// A placeholder since there is currently no provided session key handler for parachain validator
/// keys.
pub struct ParachainSessionKeyPlaceholder<T>(sp_std::marker::PhantomData<T>);
impl<T> sp_runtime::BoundToRuntimeAppPublic for ParachainSessionKeyPlaceholder<T> {
	type Public = ValidatorId;
}

impl<T: pallet_session::Trait>
	pallet_session::OneSessionHandler<T::AccountId> for ParachainSessionKeyPlaceholder<T>
{
	type Key = ValidatorId;

	fn on_genesis_session<'a, I: 'a>(_validators: I) where
		I: Iterator<Item = (&'a T::AccountId, ValidatorId)>,
		T::AccountId: 'a
	{

	}

	fn on_new_session<'a, I: 'a>(_changed: bool, _v: I, _q: I) where
		I: Iterator<Item = (&'a T::AccountId, ValidatorId)>,
		T::AccountId: 'a
	{

	}

	fn on_disabled(_: usize) { }
}

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#[cfg(test)]
mod multiplier_tests {
	use super::*;
	use frame_support::{impl_outer_origin, parameter_types, weights::Weight};
	use sp_core::H256;
	use sp_runtime::{
		testing::Header,
		traits::{BlakeTwo256, IdentityLookup, Convert},
		Perbill,
	};

	#[derive(Clone, PartialEq, Eq, Debug)]
	pub struct Runtime;

	impl_outer_origin!{
142
		pub enum Origin for Runtime {}
143
144
145
146
147
148
149
150
151
152
	}

	parameter_types! {
		pub const BlockHashCount: u64 = 250;
		pub const ExtrinsicBaseWeight: u64 = 100;
		pub const MaximumBlockWeight: Weight = 1024;
		pub const MaximumBlockLength: u32 = 2 * 1024;
		pub const AvailableBlockRatio: Perbill = Perbill::one();
	}

153
	impl frame_system::Trait for Runtime {
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
		type BaseCallFilter = ();
		type Origin = Origin;
		type Index = u64;
		type BlockNumber = u64;
		type Call = ();
		type Hash = H256;
		type Hashing = BlakeTwo256;
		type AccountId = u64;
		type Lookup = IdentityLookup<Self::AccountId>;
		type Header = Header;
		type Event = ();
		type BlockHashCount = BlockHashCount;
		type MaximumBlockWeight = MaximumBlockWeight;
		type DbWeight = ();
		type BlockExecutionWeight = ();
		type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
		type MaximumExtrinsicWeight = MaximumBlockWeight;
		type MaximumBlockLength = MaximumBlockLength;
		type AvailableBlockRatio = AvailableBlockRatio;
		type Version = ();
174
		type PalletInfo = ();
175
176
177
		type AccountData = ();
		type OnNewAccount = ();
		type OnKilledAccount = ();
178
		type SystemWeightInfo = ();
179
180
	}

181
	type System = frame_system::Module<Runtime>;
182
183
184

	fn run_with_system_weight<F>(w: Weight, assertions: F) where F: Fn() -> () {
		let mut t: sp_io::TestExternalities =
185
			frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap().into();
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
		t.execute_with(|| {
			System::set_block_limits(w, 0);
			assertions()
		});
	}

	#[test]
	fn multiplier_can_grow_from_zero() {
		let minimum_multiplier = MinimumMultiplier::get();
		let target = TargetBlockFullness::get() * (AvailableBlockRatio::get() * MaximumBlockWeight::get());
		// if the min is too small, then this will not change, and we are doomed forever.
		// the weight is 1/10th bigger than target.
		run_with_system_weight(target * 101 / 100, || {
			let next = SlowAdjustingFeeUpdate::<Runtime>::convert(minimum_multiplier);
			assert!(next > minimum_multiplier, "{:?} !>= {:?}", next, minimum_multiplier);
		})
	}
}