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
//! Test utilities
18
use super::*;
19
use frame_support::{
20
	construct_runtime, parameter_types,
21
	traits::{EqualPrivilegeOnly, Everything},
22
	weights::Weight,
23
};
24
use frame_system::EnsureRoot;
25
use pallet_evm::{
26
	EnsureAddressNever, EnsureAddressRoot, FrameSystemAccountProvider, SubstrateBlockHashMapping,
27
};
28
use precompile_utils::{mock_account, precompile_set::*, testing::MockAccount};
29
use sp_core::{H256, U256};
30
use sp_io;
31
use sp_runtime::{
32
	traits::{BlakeTwo256, IdentityLookup},
33
	BuildStorage,
34
};
35

            
36
pub type AccountId = MockAccount;
37
pub type Balance = u128;
38

            
39
type Block = frame_system::mocking::MockBlockU32<Runtime>;
40

            
41
// Configure a mock runtime to test the pallet.
42
676
construct_runtime!(
43
168
	pub enum Runtime	{
44
168
		System: frame_system,
45
168
		Balances: pallet_balances,
46
168
		Evm: pallet_evm,
47
168
		Timestamp: pallet_timestamp,
48
168
		AuthorMapping: pallet_author_mapping,
49
168
		Scheduler: pallet_scheduler,
50
168
	}
51
697
);
52

            
53
parameter_types! {
54
	pub const BlockHashCount: u32 = 250;
55
	pub const SS58Prefix: u8 = 42;
56
}
57
impl frame_system::Config for Runtime {
58
	type BaseCallFilter = Everything;
59
	type DbWeight = ();
60
	type RuntimeOrigin = RuntimeOrigin;
61
	type RuntimeTask = RuntimeTask;
62
	type Nonce = u64;
63
	type Block = Block;
64
	type RuntimeCall = RuntimeCall;
65
	type Hash = H256;
66
	type Hashing = BlakeTwo256;
67
	type AccountId = AccountId;
68
	type Lookup = IdentityLookup<Self::AccountId>;
69
	type RuntimeEvent = RuntimeEvent;
70
	type BlockHashCount = BlockHashCount;
71
	type Version = ();
72
	type PalletInfo = PalletInfo;
73
	type AccountData = pallet_balances::AccountData<Balance>;
74
	type OnNewAccount = ();
75
	type OnKilledAccount = ();
76
	type SystemWeightInfo = ();
77
	type BlockWeights = ();
78
	type BlockLength = ();
79
	type SS58Prefix = SS58Prefix;
80
	type OnSetCode = ();
81
	type MaxConsumers = frame_support::traits::ConstU32<16>;
82
	type SingleBlockMigrations = ();
83
	type MultiBlockMigrator = ();
84
	type PreInherents = ();
85
	type PostInherents = ();
86
	type PostTransactions = ();
87
	type ExtensionsWeightInfo = ();
88
}
89
parameter_types! {
90
	pub const ExistentialDeposit: u128 = 0;
91
}
92
impl pallet_balances::Config for Runtime {
93
	type MaxReserves = ();
94
	type ReserveIdentifier = ();
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
pub type Precompiles<R> =
110
	PrecompileSetBuilder<R, (PrecompileAt<AddressU64<1>, AuthorMappingPrecompile<R>>,)>;
111

            
112
pub type PCall = AuthorMappingPrecompileCall<Runtime>;
113

            
114
6
mock_account!(AuthorMappingAccount, |_| MockAccount::from_u64(1));
115

            
116
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
117
/// Block storage limit in bytes. Set to 40 KB.
118
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;
119

            
120
parameter_types! {
121
	pub BlockGasLimit: U256 = U256::from(u64::MAX);
122
	pub PrecompilesValue: Precompiles<Runtime> = Precompiles::new();
123
	pub const WeightPerGas: Weight = Weight::from_parts(1, 0);
124
	pub GasLimitPovSizeRatio: u64 = {
125
		let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
126
		block_gas_limit.saturating_div(MAX_POV_SIZE)
127
	};
128
	pub GasLimitStorageGrowthRatio: u64 = {
129
		let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
130
		block_gas_limit.saturating_div(BLOCK_STORAGE_LIMIT)
131
	};
132
	pub SuicideQuickClearLimit: u32 = 0;
133
}
134

            
135
impl pallet_evm::Config for Runtime {
136
	type FeeCalculator = ();
137
	type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
138
	type WeightPerGas = WeightPerGas;
139
	type CallOrigin = EnsureAddressRoot<AccountId>;
140
	type WithdrawOrigin = EnsureAddressNever<AccountId>;
141
	type AddressMapping = AccountId;
142
	type Currency = Balances;
143
	type RuntimeEvent = RuntimeEvent;
144
	type Runner = pallet_evm::runner::stack::Runner<Self>;
145
	type PrecompilesType = Precompiles<Self>;
146
	type PrecompilesValue = PrecompilesValue;
147
	type ChainId = ();
148
	type OnChargeTransaction = ();
149
	type BlockGasLimit = BlockGasLimit;
150
	type BlockHashMapping = SubstrateBlockHashMapping<Self>;
151
	type FindAuthor = ();
152
	type OnCreate = ();
153
	type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
154
	type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio;
155
	type Timestamp = Timestamp;
156
	type WeightInfo = pallet_evm::weights::SubstrateWeight<Runtime>;
157
	type AccountProvider = FrameSystemAccountProvider<Runtime>;
158
}
159

            
160
parameter_types! {
161
	pub const MinimumPeriod: u64 = 5;
162
}
163
impl pallet_timestamp::Config for Runtime {
164
	type Moment = u64;
165
	type OnTimestampSet = ();
166
	type MinimumPeriod = MinimumPeriod;
167
	type WeightInfo = ();
168
}
169

            
170
parameter_types! {
171
	pub const DepositAmount: Balance = 10;
172
}
173

            
174
impl pallet_author_mapping::Config for Runtime {
175
	type RuntimeEvent = RuntimeEvent;
176
	type DepositCurrency = Balances;
177
	type DepositAmount = DepositAmount;
178
	type Keys = nimbus_primitives::NimbusId;
179
	type WeightInfo = ();
180
}
181

            
182
impl pallet_scheduler::Config for Runtime {
183
	type RuntimeEvent = RuntimeEvent;
184
	type RuntimeOrigin = RuntimeOrigin;
185
	type PalletsOrigin = OriginCaller;
186
	type RuntimeCall = RuntimeCall;
187
	type MaximumWeight = ();
188
	type ScheduleOrigin = EnsureRoot<AccountId>;
189
	type MaxScheduledPerBlock = ();
190
	type WeightInfo = ();
191
	type OriginPrivilegeCmp = EqualPrivilegeOnly; // TODO : Simplest type, maybe there is better ?
192
	type Preimages = ();
193
}
194

            
195
pub(crate) struct ExtBuilder {
196
	// endowed accounts with balances
197
	balances: Vec<(AccountId, Balance)>,
198
}
199

            
200
impl Default for ExtBuilder {
201
14
	fn default() -> ExtBuilder {
202
14
		ExtBuilder { balances: vec![] }
203
14
	}
204
}
205

            
206
impl ExtBuilder {
207
11
	pub(crate) fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self {
208
11
		self.balances = balances;
209
11
		self
210
11
	}
211

            
212
14
	pub(crate) fn build(self) -> sp_io::TestExternalities {
213
14
		let mut t = frame_system::GenesisConfig::<Runtime>::default()
214
14
			.build_storage()
215
14
			.expect("Frame system builds valid default genesis config");
216
14

            
217
14
		pallet_balances::GenesisConfig::<Runtime> {
218
14
			balances: self.balances,
219
14
		}
220
14
		.assimilate_storage(&mut t)
221
14
		.expect("Pallet balances storage can be assimilated");
222
14

            
223
14
		let mut ext = sp_io::TestExternalities::new(t);
224
14
		ext.execute_with(|| System::set_block_number(1));
225
14
		ext
226
14
	}
227
}
228

            
229
5
pub(crate) fn events() -> Vec<RuntimeEvent> {
230
5
	System::events()
231
5
		.into_iter()
232
21
		.map(|r| r.event)
233
5
		.collect::<Vec<_>>()
234
5
}