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
655
construct_runtime!(
43
	pub enum Runtime	{
44
		System: frame_system,
45
		Balances: pallet_balances,
46
		Evm: pallet_evm,
47
		Timestamp: pallet_timestamp,
48
		AuthorMapping: pallet_author_mapping,
49
		Scheduler: pallet_scheduler,
50
	}
51
1080
);
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
}
88
parameter_types! {
89
	pub const ExistentialDeposit: u128 = 0;
90
}
91
impl pallet_balances::Config for Runtime {
92
	type MaxReserves = ();
93
	type ReserveIdentifier = ();
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
pub type Precompiles<R> =
108
	PrecompileSetBuilder<R, (PrecompileAt<AddressU64<1>, AuthorMappingPrecompile<R>>,)>;
109

            
110
pub type PCall = AuthorMappingPrecompileCall<Runtime>;
111

            
112
6
mock_account!(AuthorMappingAccount, |_| MockAccount::from_u64(1));
113

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

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

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

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

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

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

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

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

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

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

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

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

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

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