1
// Copyright 2019-2025 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
6
	pub enum Test
41
6
	{
42
6
		System: frame_system,
43
6
		Balances: pallet_balances,
44
6
		Proxy: pallet_proxy,
45
6
		ProxyGenesisCompanion: proxy_companion,
46
6
	}
47
54
);
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
	type ExtensionsWeightInfo = ();
87
}
88

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

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

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

            
137
impl pallet_evm_precompile_proxy::EvmProxyCallFilter for ProxyType {}
138

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

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

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

            
164
impl Config for Test {
165
	type ProxyType = ();
166
}
167

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

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

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

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

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

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

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

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