1
// Copyright 2019-2022 PureStake Inc.
2
// This file is part of Moonbeam.
3

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

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

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

            
17
//! A minimal runtime including the proxy-genesis-companion pallet
18
use super::*;
19
use crate as proxy_companion;
20
use frame_support::{
21
	construct_runtime, parameter_types,
22
	traits::{Everything, InstanceFilter},
23
	weights::Weight,
24
};
25
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
26
use sp_core::H256;
27
use sp_runtime::{
28
	traits::{BlakeTwo256, IdentityLookup},
29
	BuildStorage, Perbill,
30
};
31

            
32
//TODO use TestAccount once it is in a common place (currently it lives with democracy precompiles)
33
pub type AccountId = u64;
34
pub type Balance = u128;
35

            
36
type Block = frame_system::mocking::MockBlock<Test>;
37

            
38
// Configure a mock runtime to test the pallet.
39
53
construct_runtime!(
40
	pub enum Test
41
	{
42
		System: frame_system,
43
		Balances: pallet_balances,
44
		Proxy: pallet_proxy,
45
		ProxyGenesisCompanion: proxy_companion,
46
	}
47
73
);
48

            
49
parameter_types! {
50
	pub const BlockHashCount: u32 = 250;
51
	pub const MaximumBlockWeight: Weight = Weight::from_parts(1024, 1);
52
	pub const MaximumBlockLength: u32 = 2 * 1024;
53
	pub const AvailableBlockRatio: Perbill = Perbill::one();
54
	pub const SS58Prefix: u8 = 42;
55
}
56
impl frame_system::Config for Test {
57
	type BaseCallFilter = Everything;
58
	type DbWeight = ();
59
	type RuntimeOrigin = RuntimeOrigin;
60
	type RuntimeTask = RuntimeTask;
61
	type Nonce = u64;
62
	type Block = Block;
63
	type RuntimeCall = RuntimeCall;
64
	type Hash = H256;
65
	type Hashing = BlakeTwo256;
66
	type AccountId = AccountId;
67
	type Lookup = IdentityLookup<Self::AccountId>;
68
	type RuntimeEvent = RuntimeEvent;
69
	type BlockHashCount = BlockHashCount;
70
	type Version = ();
71
	type PalletInfo = PalletInfo;
72
	type AccountData = pallet_balances::AccountData<Balance>;
73
	type OnNewAccount = ();
74
	type OnKilledAccount = ();
75
	type SystemWeightInfo = ();
76
	type BlockWeights = ();
77
	type BlockLength = ();
78
	type SS58Prefix = SS58Prefix;
79
	type OnSetCode = ();
80
	type MaxConsumers = frame_support::traits::ConstU32<16>;
81
	type SingleBlockMigrations = ();
82
	type MultiBlockMigrator = ();
83
	type PreInherents = ();
84
	type PostInherents = ();
85
	type PostTransactions = ();
86
}
87

            
88
parameter_types! {
89
	pub const ExistentialDeposit: u128 = 0;
90
}
91
impl pallet_balances::Config for Test {
92
	type MaxReserves = ();
93
	type ReserveIdentifier = [u8; 4];
94
	type MaxLocks = ();
95
	type Balance = Balance;
96
	type RuntimeEvent = RuntimeEvent;
97
	type DustRemoval = ();
98
	type ExistentialDeposit = ExistentialDeposit;
99
	type AccountStore = System;
100
	type WeightInfo = ();
101
	type RuntimeHoldReason = ();
102
	type FreezeIdentifier = ();
103
	type MaxFreezes = ();
104
	type RuntimeFreezeReason = ();
105
}
106

            
107
parameter_types! {
108
	pub const ProxyDepositBase: Balance = 1;
109
	pub const ProxyDepositFactor: Balance = 1;
110
	pub const MaxProxies: u16 = 32;
111
	pub const AnnouncementDepositBase: Balance = 1;
112
	pub const AnnouncementDepositFactor: Balance = 1;
113
	pub const MaxPending: u16 = 32;
114
}
115

            
116
/// The type used to represent the kinds of proxying allowed.
117
#[derive(
118
	Copy,
119
	Clone,
120
	Eq,
121
	PartialEq,
122
	Ord,
123
	PartialOrd,
124
	Encode,
125
	Decode,
126
	Debug,
127
	MaxEncodedLen,
128
	scale_info::TypeInfo,
129
	serde::Serialize,
130
	serde::Deserialize,
131
	Default,
132
)]
133
pub struct ProxyType;
134

            
135
impl pallet_evm_precompile_proxy::EvmProxyCallFilter for ProxyType {}
136

            
137
impl InstanceFilter<RuntimeCall> for ProxyType {
138
	fn filter(&self, _c: &RuntimeCall) -> bool {
139
		true
140
	}
141

            
142
	fn is_superset(&self, _o: &Self) -> bool {
143
		true
144
	}
145
}
146

            
147
impl pallet_proxy::Config for Test {
148
	type RuntimeEvent = RuntimeEvent;
149
	type RuntimeCall = RuntimeCall;
150
	type Currency = Balances;
151
	type ProxyType = ();
152
	type ProxyDepositBase = ProxyDepositBase;
153
	type ProxyDepositFactor = ProxyDepositFactor;
154
	type MaxProxies = MaxProxies;
155
	type WeightInfo = ();
156
	type MaxPending = MaxPending;
157
	type CallHasher = BlakeTwo256;
158
	type AnnouncementDepositBase = AnnouncementDepositBase;
159
	type AnnouncementDepositFactor = AnnouncementDepositFactor;
160
}
161

            
162
impl Config for Test {
163
	type ProxyType = ();
164
}
165

            
166
/// Externality builder for pallet maintenance mode's mock runtime
167
pub(crate) struct ExtBuilder {
168
	proxies: Vec<(AccountId, AccountId)>,
169
	balances: Vec<(AccountId, Balance)>,
170
}
171

            
172
impl Default for ExtBuilder {
173
3
	fn default() -> ExtBuilder {
174
3
		ExtBuilder {
175
3
			proxies: Vec::new(),
176
3
			balances: Vec::new(),
177
3
		}
178
3
	}
179
}
180

            
181
impl ExtBuilder {
182
1
	pub(crate) fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self {
183
1
		self.balances = balances;
184
1
		self
185
1
	}
186

            
187
2
	pub(crate) fn with_proxies(mut self, proxies: Vec<(AccountId, AccountId)>) -> Self {
188
2
		self.proxies = proxies;
189
2
		self
190
2
	}
191

            
192
3
	pub(crate) fn build(self) -> sp_io::TestExternalities {
193
3
		let mut t = frame_system::GenesisConfig::<Test>::default()
194
3
			.build_storage()
195
3
			.expect("Frame system builds valid default genesis config");
196
3

            
197
3
		pallet_balances::GenesisConfig::<Test> {
198
3
			balances: self.balances,
199
3
		}
200
3
		.assimilate_storage(&mut t)
201
3
		.expect("Pallet balances storage can be assimilated");
202
3

            
203
3
		let genesis_config = proxy_companion::GenesisConfig::<Test> {
204
3
			// Here we add the trivial proxy type and default duration.
205
3
			// This saves the test writer from having to always specify this.
206
3
			proxies: self
207
3
				.proxies
208
3
				.into_iter()
209
3
				.map(|(a, b)| (a, b, (), 100))
210
3
				.collect(),
211
3
		};
212
3
		genesis_config
213
3
			.assimilate_storage(&mut t)
214
3
			.expect("Pallet proxy genesis companion storage can be assimilated");
215
3

            
216
3
		let mut ext = sp_io::TestExternalities::new(t);
217
3
		ext.execute_with(|| System::set_block_number(1));
218
3
		ext
219
3
	}
220
}