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::{construct_runtime, parameter_types, traits::Everything, weights::Weight};
20
use pallet_evm::{
21
	EnsureAddressNever, EnsureAddressRoot, FrameSystemAccountProvider, SubstrateBlockHashMapping,
22
};
23
use precompile_utils::{mock_account, precompile_set::*, testing::MockAccount};
24
use sp_core::{H256, U256};
25
use sp_io;
26
use sp_runtime::{
27
	traits::{BlakeTwo256, IdentityLookup},
28
	BuildStorage,
29
};
30

            
31
pub type AccountId = MockAccount;
32
pub type Balance = u128;
33

            
34
type Block = frame_system::mocking::MockBlockU32<Runtime>;
35

            
36
// Configure a mock runtime to test the pallet.
37
168
construct_runtime!(
38
168
	pub enum Runtime	{
39
168
		System: frame_system,
40
168
		Balances: pallet_balances,
41
168
		Evm: pallet_evm,
42
168
		Timestamp: pallet_timestamp,
43
168
		AuthorMapping: pallet_author_mapping,
44
168
	}
45
168
);
46

            
47
parameter_types! {
48
	pub const BlockHashCount: u32 = 250;
49
	pub const SS58Prefix: u8 = 42;
50
}
51
impl frame_system::Config for Runtime {
52
	type BaseCallFilter = Everything;
53
	type DbWeight = ();
54
	type RuntimeOrigin = RuntimeOrigin;
55
	type RuntimeTask = RuntimeTask;
56
	type Nonce = u64;
57
	type Block = Block;
58
	type RuntimeCall = RuntimeCall;
59
	type Hash = H256;
60
	type Hashing = BlakeTwo256;
61
	type AccountId = AccountId;
62
	type Lookup = IdentityLookup<Self::AccountId>;
63
	type RuntimeEvent = RuntimeEvent;
64
	type BlockHashCount = BlockHashCount;
65
	type Version = ();
66
	type PalletInfo = PalletInfo;
67
	type AccountData = pallet_balances::AccountData<Balance>;
68
	type OnNewAccount = ();
69
	type OnKilledAccount = ();
70
	type SystemWeightInfo = ();
71
	type BlockWeights = ();
72
	type BlockLength = ();
73
	type SS58Prefix = SS58Prefix;
74
	type OnSetCode = ();
75
	type MaxConsumers = frame_support::traits::ConstU32<16>;
76
	type SingleBlockMigrations = ();
77
	type MultiBlockMigrator = ();
78
	type PreInherents = ();
79
	type PostInherents = ();
80
	type PostTransactions = ();
81
	type ExtensionsWeightInfo = ();
82
}
83
parameter_types! {
84
	pub const ExistentialDeposit: u128 = 0;
85
}
86
impl pallet_balances::Config for Runtime {
87
	type MaxReserves = ();
88
	type ReserveIdentifier = ();
89
	type MaxLocks = ();
90
	type Balance = Balance;
91
	type RuntimeEvent = RuntimeEvent;
92
	type DustRemoval = ();
93
	type ExistentialDeposit = ExistentialDeposit;
94
	type AccountStore = System;
95
	type WeightInfo = ();
96
	type RuntimeHoldReason = ();
97
	type FreezeIdentifier = ();
98
	type MaxFreezes = ();
99
	type RuntimeFreezeReason = ();
100
	type DoneSlashHandler = ();
101
}
102

            
103
pub type Precompiles<R> =
104
	PrecompileSetBuilder<R, (PrecompileAt<AddressU64<1>, AuthorMappingPrecompile<R>>,)>;
105

            
106
pub type PCall = AuthorMappingPrecompileCall<Runtime>;
107

            
108
6
mock_account!(AuthorMappingAccount, |_| MockAccount::from_u64(1));
109

            
110
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
111
/// Block storage limit in bytes. Set to 40 KB.
112
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;
113

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

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

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

            
166
parameter_types! {
167
	pub const DepositAmount: Balance = 10;
168
}
169

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

            
178
pub(crate) struct ExtBuilder {
179
	// endowed accounts with balances
180
	balances: Vec<(AccountId, Balance)>,
181
}
182

            
183
impl Default for ExtBuilder {
184
14
	fn default() -> ExtBuilder {
185
14
		ExtBuilder { balances: vec![] }
186
14
	}
187
}
188

            
189
impl ExtBuilder {
190
11
	pub(crate) fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self {
191
11
		self.balances = balances;
192
11
		self
193
11
	}
194

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

            
200
14
		pallet_balances::GenesisConfig::<Runtime> {
201
14
			balances: self.balances,
202
14
			dev_accounts: Default::default(),
203
14
		}
204
14
		.assimilate_storage(&mut t)
205
14
		.expect("Pallet balances storage can be assimilated");
206
14

            
207
14
		let mut ext = sp_io::TestExternalities::new(t);
208
14
		ext.execute_with(|| System::set_block_number(1));
209
14
		ext
210
14
	}
211
}
212

            
213
5
pub(crate) fn events() -> Vec<RuntimeEvent> {
214
5
	System::events()
215
5
		.into_iter()
216
21
		.map(|r| r.event)
217
5
		.collect::<Vec<_>>()
218
5
}