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

use sp_io::TestExternalities;
20
21
22
use sp_core::H256;
use sp_runtime::traits::{
	BlakeTwo256, IdentityLookup,
23
};
24
use primitives::v1::{AuthorityDiscoveryId, Balance, BlockNumber, Header, ValidatorIndex};
25
26
use frame_support::parameter_types;
use frame_support_test::TestRandomness;
27
28
use std::cell::RefCell;
use std::collections::HashMap;
29
30
use crate::{
	inclusion, scheduler, dmp, ump, hrmp, session_info, paras, configuration,
31
	initializer, shared,
32
};
33

34
35
36
37
38
39
40
41
42
43
44
45
46
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,
	{
		System: frame_system::{Module, Call, Config, Storage, Event<T>},
		Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
		Paras: paras::{Module, Origin, Call, Storage, Config<T>},
		Configuration: configuration::{Module, Call, Storage, Config<T>},
47
		Shared: shared::{Module, Call, Storage},
48
49
50
51
52
		Inclusion: inclusion::{Module, Call, Storage, Event<T>},
		Scheduler: scheduler::{Module, Call, Storage},
		Initializer: initializer::{Module, Call, Storage},
		Dmp: dmp::{Module, Call, Storage},
		Ump: ump::{Module, Call, Storage},
53
		Hrmp: hrmp::{Module, Call, Storage, Event},
54
		SessionInfo: session_info::{Module, Call, Storage},
55
	}
56
);
57

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

64
impl frame_system::Config for Test {
65
	type BaseCallFilter = ();
66
67
68
	type BlockWeights = BlockWeights;
	type BlockLength = ();
	type DbWeight = ();
69
70
71
72
73
74
75
76
77
	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;
78
	type Event = Event;
79
80
	type BlockHashCount = BlockHashCount;
	type Version = ();
81
	type PalletInfo = PalletInfo;
82
	type AccountData = pallet_balances::AccountData<u128>;
83
84
	type OnNewAccount = ();
	type OnKilledAccount = ();
85
	type SystemWeightInfo = ();
86
	type SS58Prefix = ();
87
88
}

89
90
91
92
93
94
95
parameter_types! {
	pub static ExistentialDeposit: u64 = 0;
}

impl pallet_balances::Config for Test {
	type MaxLocks = ();
	type Balance = Balance;
96
	type Event = Event;
97
98
99
100
101
102
	type DustRemoval = ();
	type ExistentialDeposit = ExistentialDeposit;
	type AccountStore = System;
	type WeightInfo = ();
}

103
impl crate::initializer::Config for Test {
104
	type Randomness = TestRandomness<Self>;
asynchronous rob's avatar
asynchronous rob committed
105
}
106

107
impl crate::configuration::Config for Test { }
108

109
110
impl crate::shared::Config for Test { }

111
impl crate::paras::Config for Test {
112
113
	type Origin = Origin;
}
114

115
impl crate::dmp::Config for Test { }
116

117
impl crate::ump::Config for Test {
118
119
120
	type UmpSink = crate::ump::mock_sink::MockUmpSink;
}

121
impl crate::hrmp::Config for Test {
122
	type Event = Event;
Sergey Pepyakin's avatar
Sergey Pepyakin committed
123
	type Origin = Origin;
124
	type Currency = pallet_balances::Module<Test>;
125
}
126

127
impl crate::scheduler::Config for Test { }
asynchronous rob's avatar
asynchronous rob committed
128

129
impl crate::inclusion::Config for Test {
130
	type Event = Event;
131
	type RewardValidators = TestRewardValidators;
132
}
asynchronous rob's avatar
asynchronous rob committed
133

134
135
impl crate::inclusion_inherent::Config for Test { }

136
impl crate::session_info::Config for Test { }
137

138
impl crate::session_info::AuthorityDiscoveryConfig for Test {
139
140
141
142
143
	fn authorities() -> Vec<AuthorityDiscoveryId> {
		Vec::new()
	}
}

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
177
178
179
180
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;
			}
		})
	}
}

181
/// Create a new set of test externalities.
182
pub fn new_test_ext(state: MockGenesisConfig) -> TestExternalities {
183
184
185
	BACKING_REWARDS.with(|r| r.borrow_mut().clear());
	AVAILABILITY_REWARDS.with(|r| r.borrow_mut().clear());

186
187
188
189
190
191
192
193
	let mut t = state.system.build_storage::<Test>().unwrap();
	state.configuration.assimilate_storage(&mut t).unwrap();
	state.paras.assimilate_storage(&mut t).unwrap();

	t.into()
}

#[derive(Default)]
194
pub struct MockGenesisConfig {
195
	pub system: frame_system::GenesisConfig,
196
197
198
	pub configuration: crate::configuration::GenesisConfig<Test>,
	pub paras: crate::paras::GenesisConfig<Test>,
}