mock.rs 8.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 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/>.

//! Mocks for all the traits.

Shawn Tabrizi's avatar
Shawn Tabrizi committed
19
use crate::{
20
21
	configuration, disputes, dmp, hrmp, inclusion, initializer, paras, paras_inherent, scheduler,
	session_info, shared, ump,
asynchronous rob's avatar
asynchronous rob committed
22
};
Shawn Tabrizi's avatar
Shawn Tabrizi committed
23
use frame_support::{parameter_types, traits::GenesisBuild};
24
use frame_support_test::TestRandomness;
Shawn Tabrizi's avatar
Shawn Tabrizi committed
25
26
use primitives::v1::{
	AuthorityDiscoveryId, Balance, BlockNumber, Header, SessionIndex, ValidatorIndex,
27
};
Shawn Tabrizi's avatar
Shawn Tabrizi committed
28
29
30
31
use sp_core::H256;
use sp_io::TestExternalities;
use sp_runtime::traits::{BlakeTwo256, IdentityLookup};
use std::{cell::RefCell, collections::HashMap};
32

33
34
35
36
37
38
39
40
41
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
	pub enum Test where
		Block = Block,
		NodeBlock = Block,
		UncheckedExtrinsic = UncheckedExtrinsic,
	{
42
43
		System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
		Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
ferrell-code's avatar
ferrell-code committed
44
		Paras: paras::{Pallet, Origin, Call, Storage, Event, Config},
45
		Configuration: configuration::{Pallet, Call, Storage, Config<T>},
46
		ParasShared: shared::{Pallet, Call, Storage},
47
		ParaInclusion: inclusion::{Pallet, Call, Storage, Event<T>},
48
		ParaInherent: paras_inherent::{Pallet, Call, Storage},
49
		Scheduler: scheduler::{Pallet, Storage},
50
51
		Initializer: initializer::{Pallet, Call, Storage},
		Dmp: dmp::{Pallet, Call, Storage},
52
		Ump: ump::{Pallet, Call, Storage, Event},
53
		Hrmp: hrmp::{Pallet, Call, Storage, Event<T>},
54
		SessionInfo: session_info::{Pallet, Storage},
asynchronous rob's avatar
asynchronous rob committed
55
		Disputes: disputes::{Pallet, Storage, Event<T>},
56
	}
57
);
58

59
60
parameter_types! {
	pub const BlockHashCount: u32 = 250;
61
62
	pub BlockWeights: frame_system::limits::BlockWeights =
		frame_system::limits::BlockWeights::simple_max(4 * 1024 * 1024);
63
64
}

asynchronous rob's avatar
asynchronous rob committed
65
66
pub type AccountId = u64;

67
impl frame_system::Config for Test {
68
	type BaseCallFilter = frame_support::traits::Everything;
69
70
71
	type BlockWeights = BlockWeights;
	type BlockLength = ();
	type DbWeight = ();
72
73
74
75
76
77
78
79
80
	type Origin = Origin;
	type Call = Call;
	type Index = u64;
	type BlockNumber = BlockNumber;
	type Hash = H256;
	type Hashing = BlakeTwo256;
	type AccountId = u64;
	type Lookup = IdentityLookup<u64>;
	type Header = Header;
81
	type Event = Event;
82
83
	type BlockHashCount = BlockHashCount;
	type Version = ();
84
	type PalletInfo = PalletInfo;
85
	type AccountData = pallet_balances::AccountData<u128>;
86
87
	type OnNewAccount = ();
	type OnKilledAccount = ();
88
	type SystemWeightInfo = ();
89
	type SS58Prefix = ();
90
	type OnSetCode = ();
91
92
}

93
94
95
96
97
98
parameter_types! {
	pub static ExistentialDeposit: u64 = 0;
}

impl pallet_balances::Config for Test {
	type MaxLocks = ();
Gavin Wood's avatar
Gavin Wood committed
99
100
	type MaxReserves = ();
	type ReserveIdentifier = [u8; 8];
101
	type Balance = Balance;
102
	type Event = Event;
103
104
105
106
107
108
	type DustRemoval = ();
	type ExistentialDeposit = ExistentialDeposit;
	type AccountStore = System;
	type WeightInfo = ();
}

109
impl crate::initializer::Config for Test {
110
	type Randomness = TestRandomness<Self>;
111
	type ForceOrigin = frame_system::EnsureRoot<u64>;
asynchronous rob's avatar
asynchronous rob committed
112
}
113

Shawn Tabrizi's avatar
Shawn Tabrizi committed
114
impl crate::configuration::Config for Test {}
115

Shawn Tabrizi's avatar
Shawn Tabrizi committed
116
impl crate::shared::Config for Test {}
117

118
impl crate::paras::Config for Test {
119
	type Origin = Origin;
120
	type Event = Event;
121
}
122

Shawn Tabrizi's avatar
Shawn Tabrizi committed
123
impl crate::dmp::Config for Test {}
124

Gavin Wood's avatar
Gavin Wood committed
125
126
127
128
parameter_types! {
	pub const FirstMessageFactorPercent: u64 = 100;
}

129
impl crate::ump::Config for Test {
130
	type Event = Event;
131
	type UmpSink = crate::ump::mock_sink::MockUmpSink;
Gavin Wood's avatar
Gavin Wood committed
132
	type FirstMessageFactorPercent = FirstMessageFactorPercent;
133
134
}

135
impl crate::hrmp::Config for Test {
136
	type Event = Event;
Sergey Pepyakin's avatar
Sergey Pepyakin committed
137
	type Origin = Origin;
138
	type Currency = pallet_balances::Pallet<Test>;
139
}
140

asynchronous rob's avatar
asynchronous rob committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
impl crate::disputes::Config for Test {
	type Event = Event;
	type RewardValidators = Self;
	type PunishValidators = Self;
}

thread_local! {
	pub static REWARD_VALIDATORS: RefCell<Vec<(SessionIndex, Vec<ValidatorIndex>)>> = RefCell::new(Vec::new());
	pub static PUNISH_VALIDATORS_FOR: RefCell<Vec<(SessionIndex, Vec<ValidatorIndex>)>> = RefCell::new(Vec::new());
	pub static PUNISH_VALIDATORS_AGAINST: RefCell<Vec<(SessionIndex, Vec<ValidatorIndex>)>> = RefCell::new(Vec::new());
	pub static PUNISH_VALIDATORS_INCONCLUSIVE: RefCell<Vec<(SessionIndex, Vec<ValidatorIndex>)>> = RefCell::new(Vec::new());
}

impl crate::disputes::RewardValidators for Test {
	fn reward_dispute_statement(
		session: SessionIndex,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
157
		validators: impl IntoIterator<Item = ValidatorIndex>,
asynchronous rob's avatar
asynchronous rob committed
158
159
160
161
162
163
164
165
	) {
		REWARD_VALIDATORS.with(|r| r.borrow_mut().push((session, validators.into_iter().collect())))
	}
}

impl crate::disputes::PunishValidators for Test {
	fn punish_for_invalid(
		session: SessionIndex,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
166
		validators: impl IntoIterator<Item = ValidatorIndex>,
asynchronous rob's avatar
asynchronous rob committed
167
168
169
170
171
172
173
	) {
		PUNISH_VALIDATORS_FOR
			.with(|r| r.borrow_mut().push((session, validators.into_iter().collect())))
	}

	fn punish_against_valid(
		session: SessionIndex,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
174
		validators: impl IntoIterator<Item = ValidatorIndex>,
asynchronous rob's avatar
asynchronous rob committed
175
176
177
178
179
180
181
	) {
		PUNISH_VALIDATORS_AGAINST
			.with(|r| r.borrow_mut().push((session, validators.into_iter().collect())))
	}

	fn punish_inconclusive(
		session: SessionIndex,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
182
		validators: impl IntoIterator<Item = ValidatorIndex>,
asynchronous rob's avatar
asynchronous rob committed
183
184
185
186
187
188
	) {
		PUNISH_VALIDATORS_INCONCLUSIVE
			.with(|r| r.borrow_mut().push((session, validators.into_iter().collect())))
	}
}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
189
impl crate::scheduler::Config for Test {}
asynchronous rob's avatar
asynchronous rob committed
190

191
impl crate::inclusion::Config for Test {
192
	type Event = Event;
asynchronous rob's avatar
asynchronous rob committed
193
	type DisputesHandler = Disputes;
194
	type RewardValidators = TestRewardValidators;
195
}
asynchronous rob's avatar
asynchronous rob committed
196

Shawn Tabrizi's avatar
Shawn Tabrizi committed
197
impl crate::paras_inherent::Config for Test {}
198

Shawn Tabrizi's avatar
Shawn Tabrizi committed
199
impl crate::session_info::Config for Test {}
200

201
202
203
204
205
206
207
208
209
210
211
212
thread_local! {
	pub static DISCOVERY_AUTHORITIES: RefCell<Vec<AuthorityDiscoveryId>> = RefCell::new(Vec::new());
}

pub fn discovery_authorities() -> Vec<AuthorityDiscoveryId> {
	DISCOVERY_AUTHORITIES.with(|r| r.borrow().clone())
}

pub fn set_discovery_authorities(new: Vec<AuthorityDiscoveryId>) {
	DISCOVERY_AUTHORITIES.with(|r| *r.borrow_mut() = new);
}

213
impl crate::session_info::AuthorityDiscoveryConfig for Test {
214
	fn authorities() -> Vec<AuthorityDiscoveryId> {
215
		discovery_authorities()
216
217
218
	}
}

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
thread_local! {
	pub static BACKING_REWARDS: RefCell<HashMap<ValidatorIndex, usize>>
		= RefCell::new(HashMap::new());

	pub static AVAILABILITY_REWARDS: RefCell<HashMap<ValidatorIndex, usize>>
		= RefCell::new(HashMap::new());
}

pub fn backing_rewards() -> HashMap<ValidatorIndex, usize> {
	BACKING_REWARDS.with(|r| r.borrow().clone())
}

pub fn availability_rewards() -> HashMap<ValidatorIndex, usize> {
	AVAILABILITY_REWARDS.with(|r| r.borrow().clone())
}

pub struct TestRewardValidators;

impl inclusion::RewardValidators for TestRewardValidators {
	fn reward_backing(v: impl IntoIterator<Item = ValidatorIndex>) {
		BACKING_REWARDS.with(|r| {
			let mut r = r.borrow_mut();
			for i in v {
				*r.entry(i).or_insert(0) += 1;
			}
		})
	}
	fn reward_bitfields(v: impl IntoIterator<Item = ValidatorIndex>) {
		AVAILABILITY_REWARDS.with(|r| {
			let mut r = r.borrow_mut();
			for i in v {
				*r.entry(i).or_insert(0) += 1;
			}
		})
	}
}

256
/// Create a new set of test externalities.
257
pub fn new_test_ext(state: MockGenesisConfig) -> TestExternalities {
258
259
260
	BACKING_REWARDS.with(|r| r.borrow_mut().clear());
	AVAILABILITY_REWARDS.with(|r| r.borrow_mut().clear());

261
262
	let mut t = state.system.build_storage::<Test>().unwrap();
	state.configuration.assimilate_storage(&mut t).unwrap();
ferrell-code's avatar
ferrell-code committed
263
	GenesisBuild::<Test>::assimilate_storage(&state.paras, &mut t).unwrap();
264
265
266
267
268

	t.into()
}

#[derive(Default)]
269
pub struct MockGenesisConfig {
270
	pub system: frame_system::GenesisConfig,
271
	pub configuration: crate::configuration::GenesisConfig<Test>,
ferrell-code's avatar
ferrell-code committed
272
	pub paras: crate::paras::GenesisConfig,
273
}