weights.rs 8.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Copyright 2020 Parity Technologies (UK) Ltd.
// 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/>.

//! Tests to make sure that Polkadot's weights and fees match what we
//! expect from Substrate.
//!
//! These test are not meant to be exhaustive, as it is inevitable that
//! weights in Substrate will change. Instead they are supposed to provide
22
//! some sort of indicator that calls we consider important (e.g pallet_balances::transfer)
23
//! have not suddenly changed from under us.
24
25
26
27
28
29
//!
//! Some of the tests in this crate print insightful logs. Run with:
//!
//! ```
//! $ cargo test -p polkadot-runtime -- --nocapture --test-threads=1
//! ```
30

31
use codec::Encode;
32
33
use frame_support::{
	traits::ContainsLengthBound,
34
	weights::{constants::*, GetDispatchInfo, Weight, DispatchInfo},
35
36
37
38
};
use keyring::AccountKeyring;
use polkadot_runtime::constants::currency::*;
use polkadot_runtime::{self, Runtime};
asynchronous rob's avatar
asynchronous rob committed
39
use primitives::v0::AccountId;
40
41
use runtime_common::MaximumBlockWeight;

42
43
44
45
46
use pallet_elections_phragmen::Call as PhragmenCall;
use pallet_session::Call as SessionCall;
use pallet_staking::Call as StakingCall;
use frame_system::Call as SystemCall;
use pallet_treasury::Call as TreasuryCall;
47

48
type DbWeight = <Runtime as frame_system::Trait>::DbWeight;
49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

fn report_portion(name: &'static str, info: DispatchInfo, len: usize) {
	let maximum_weight = <Runtime as frame_system::Trait>::MaximumBlockWeight::get();
	let fee = sp_io::TestExternalities::new(Default::default()).execute_with(|| {
		<pallet_transaction_payment::Module<Runtime>>::compute_fee(len as u32, &info, 0)
	});

	let portion = info.weight as f64 / maximum_weight as f64;

	if portion > 0.5 {
		panic!("Weight of some call seem to have exceeded half of the block. Probably something is wrong.");
	}

	println!("\nCall {} (with default args) takes {} of the block weight, pays {} in fee.", name, portion, fee);
}

66
67
68
#[test]
fn sanity_check_weight_per_time_constants_are_as_expected() {
	// These values comes from Substrate, we want to make sure that if it
69
	// ever changes we don't accidentally break Polkadot
70
71
72
73
74
75
76
77
78
79
	assert_eq!(WEIGHT_PER_SECOND, 1_000_000_000_000);
	assert_eq!(WEIGHT_PER_MILLIS, WEIGHT_PER_SECOND / 1000);
	assert_eq!(WEIGHT_PER_MICROS, WEIGHT_PER_MILLIS / 1000);
	assert_eq!(WEIGHT_PER_NANOS, WEIGHT_PER_MICROS / 1000);
}

#[test]
fn weight_of_staking_bond_is_correct() {
	let controller: AccountId = AccountKeyring::Alice.into();

80
81
82
83
84
85
	// (144278000 as Weight)
	// 	.saturating_add(DbWeight::get().reads(5 as Weight))
	// 	.saturating_add(DbWeight::get().writes(4 as Weight))
	let expected_weight = 144278000 + (DbWeight::get().read * 5) + (DbWeight::get().write * 4);
	let call = StakingCall::bond::<Runtime>(controller, 1 * DOLLARS, Default::default());
	let info = call.get_dispatch_info();
86

87
88
	assert_eq!(info.weight, expected_weight);
	report_portion("staking_bond", info, call.encode().len())
89
90
91
92
}

#[test]
fn weight_of_staking_validate_is_correct() {
93
94
95
96
97
98
99
100
101
	// (35539000 as Weight)
	// 	.saturating_add(DbWeight::get().reads(2 as Weight))
	// 	.saturating_add(DbWeight::get().writes(2 as Weight))
	let expected_weight = 35539000 + (DbWeight::get().read * 2) + (DbWeight::get().write * 2);
	let call = StakingCall::validate::<Runtime>(Default::default());
	let info = call.get_dispatch_info();

	assert_eq!(info.weight, expected_weight);
	report_portion("staking_validate", info, call.encode().len())
102
103
104
105
106
107
}

#[test]
fn weight_of_staking_nominate_is_correct() {
	let targets: Vec<AccountId> = vec![Default::default(), Default::default(), Default::default()];

108
109
110
111
	// (48596000 as Weight)
	// 	.saturating_add((308000 as Weight).saturating_mul(n as Weight))
	// 	.saturating_add(DbWeight::get().reads(3 as Weight))
	// 	.saturating_add(DbWeight::get().writes(2 as Weight))
112
	let db_weight = (DbWeight::get().read * 3) + (DbWeight::get().write * 2);
113
	let targets_weight = (308 * WEIGHT_PER_NANOS).saturating_mul(targets.len() as Weight);
114

115
116
117
	let expected_weight = db_weight.saturating_add(48596000).saturating_add(targets_weight);
	let call = StakingCall::nominate::<Runtime>(targets);
	let info = call.get_dispatch_info();
118

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
	assert_eq!(info.weight, expected_weight);
	report_portion("staking_nominate", info, call.encode().len())
}

#[test]
fn weight_of_staking_payout_staker_is_correct() {
	// (0 as Weight)
	// 	.saturating_add((117324000 as Weight).saturating_mul(n as Weight))
	// 	.saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight)))
	// 	.saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight)))
	let call = StakingCall::payout_stakers::<Runtime>(Default::default(), 0u32);
	let info = call.get_dispatch_info();

	let n = <Runtime as pallet_staking::Trait>::MaxNominatorRewardedPerValidator::get() as Weight;
	let mut expected_weight = (117324000 as Weight).saturating_mul(n as Weight);
	expected_weight += (DbWeight::get().read * 5 * n) + (DbWeight::get().write * 3 * n);

	assert_eq!(info.weight, expected_weight);
	report_portion("staking_payout_stakers", info, call.encode().len())
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
}

#[test]
fn weight_of_system_set_code_is_correct() {
	// #[weight = (T::MaximumBlockWeight::get(), DispatchClass::Operational)]
	let expected_weight = MaximumBlockWeight::get();
	let weight = SystemCall::set_code::<Runtime>(vec![]).get_dispatch_info().weight;

	assert_eq!(weight, expected_weight);
}

#[test]
fn weight_of_session_set_keys_is_correct() {
	// #[weight = 200_000_000
	// 	+ T::DbWeight::get().reads(2 + T::Keys::key_ids().len() as Weight)
	// 	+ T::DbWeight::get().writes(1 + T::Keys::key_ids().len() as Weight)]
	//
	// Polkadot has five possible session keys, so we default to key_ids.len() = 5
	let expected_weight = 200_000_000 + (DbWeight::get().read * (2 + 5)) + (DbWeight::get().write * (1 + 5));
	let weight = SessionCall::set_keys::<Runtime>(Default::default(), Default::default()).get_dispatch_info().weight;

	assert_eq!(weight, expected_weight);
}

#[test]
fn weight_of_session_purge_keys_is_correct() {
	// #[weight = 120_000_000
	// 	+ T::DbWeight::get().reads_writes(2, 1 + T::Keys::key_ids().len() as Weight)]
	//
	// Polkadot has five possible session keys, so we default to key_ids.len() = 5
	let expected_weight = 120_000_000 + (DbWeight::get().read * 2) + (DbWeight::get().write * (1 + 5));
	let weight = SessionCall::purge_keys::<Runtime>().get_dispatch_info().weight;

	assert_eq!(weight, expected_weight);
}

#[test]
fn weight_of_phragmen_vote_is_correct() {
	// #[weight = 100_000_000]
177
	let expected_weight = 350_000_000;
178
179
180
181
182
183
184
	let weight = PhragmenCall::vote::<Runtime>(Default::default(), Default::default()).get_dispatch_info().weight;

	assert_eq!(weight, expected_weight);
}

#[test]
fn weight_of_phragmen_submit_candidacy_is_correct() {
185
186
	let expected_weight = WEIGHT_PER_MICROS * 35 + 1 * 375 * WEIGHT_PER_NANOS + DbWeight::get().reads_writes(4, 1);
	let weight = PhragmenCall::submit_candidacy::<Runtime>(1).get_dispatch_info().weight;
187
188
189
190
191
192

	assert_eq!(weight, expected_weight);
}

#[test]
fn weight_of_phragmen_renounce_candidacy_is_correct() {
193
	let expected_weight = 46 * WEIGHT_PER_MICROS + DbWeight::get().reads_writes(2, 2);
194
	let weight = PhragmenCall::renounce_candidacy::<Runtime>(pallet_elections_phragmen::Renouncing::Member)
195
		.get_dispatch_info().weight;
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

	assert_eq!(weight, expected_weight);
}

#[test]
fn weight_of_treasury_propose_spend_is_correct() {
	// #[weight = 120_000_000 + T::DbWeight::get().reads_writes(1, 2)]
	let expected_weight = 120_000_000 + DbWeight::get().read + 2 * DbWeight::get().write;
	let weight =
		TreasuryCall::propose_spend::<Runtime>(Default::default(), Default::default()).get_dispatch_info().weight;

	assert_eq!(weight, expected_weight);
}

#[test]
fn weight_of_treasury_approve_proposal_is_correct() {
	// #[weight = (34_000_000 + T::DbWeight::get().reads_writes(2, 1), DispatchClass::Operational)]
	let expected_weight = 34_000_000 + 2 * DbWeight::get().read + DbWeight::get().write;
	let weight = TreasuryCall::approve_proposal::<Runtime>(Default::default()).get_dispatch_info().weight;

	assert_eq!(weight, expected_weight);
}

#[test]
fn weight_of_treasury_tip_is_correct() {
221
	let max_len: Weight = <Runtime as pallet_treasury::Trait>::Tippers::max_len() as Weight;
222
223
224
225
226
227
228
229

	// #[weight = 68_000_000 + 2_000_000 * T::Tippers::max_len() as Weight
	// 	+ T::DbWeight::get().reads_writes(2, 1)]
	let expected_weight = 68_000_000 + 2_000_000 * max_len + 2 * DbWeight::get().read + DbWeight::get().write;
	let weight = TreasuryCall::tip::<Runtime>(Default::default(), Default::default()).get_dispatch_info().weight;

	assert_eq!(weight, expected_weight);
}