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

            
20
use frame_support::traits::Everything;
21
use frame_support::{construct_runtime, parameter_types, weights::Weight};
22
use pallet_evm::{EnsureAddressNever, EnsureAddressRoot, FrameSystemAccountProvider};
23
use precompile_utils::{mock_account, precompile_set::*, testing::MockAccount};
24
use sp_core::H256;
25
use sp_runtime::BuildStorage;
26
use sp_runtime::{
27
	traits::{BlakeTwo256, IdentityLookup},
28
	Perbill,
29
};
30

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

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

            
36
1810
construct_runtime!(
37
196
	pub enum Runtime	{
38
196
		System: frame_system,
39
196
		Balances: pallet_balances,
40
196
		Evm: pallet_evm,
41
196
		Timestamp: pallet_timestamp,
42
196
	}
43
1823
);
44

            
45
parameter_types! {
46
	pub const BlockHashCount: u32 = 250;
47
	pub const MaximumBlockWeight: Weight = Weight::from_parts(1024, 1);
48
	pub const MaximumBlockLength: u32 = 2 * 1024;
49
	pub const AvailableBlockRatio: Perbill = Perbill::one();
50
	pub const SS58Prefix: u8 = 42;
51
}
52

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

            
105
pub type Precompiles<R> = PrecompileSetBuilder<
106
	R,
107
	(
108
		PrecompileAt<
109
			AddressU64<1>,
110
			BatchPrecompile<R>,
111
			(
112
				SubcallWithMaxNesting<1>,
113
				// Batch is the only precompile allowed to call Batch.
114
				CallableByPrecompile<OnlyFrom<AddressU64<1>>>,
115
			),
116
		>,
117
		RevertPrecompile<AddressU64<2>>,
118
	),
119
>;
120

            
121
pub type PCall = BatchPrecompileCall<Runtime>;
122

            
123
76
mock_account!(Batch, |_| MockAccount::from_u64(1));
124
40
mock_account!(Revert, |_| MockAccount::from_u64(2));
125

            
126
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
127
/// Block storage limit in bytes. Set to 40 KB.
128
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;
129

            
130
parameter_types! {
131
	pub BlockGasLimit: U256 = U256::from(u64::MAX);
132
	pub PrecompilesValue: Precompiles<Runtime> = Precompiles::new();
133
	pub const WeightPerGas: Weight = Weight::from_parts(1, 0);
134
	pub GasLimitPovSizeRatio: u64 = {
135
		let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
136
		block_gas_limit.saturating_div(MAX_POV_SIZE)
137
	};
138
	pub GasLimitStorageGrowthRatio: u64 = {
139
		let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
140
		block_gas_limit.saturating_div(BLOCK_STORAGE_LIMIT)
141
	};
142
}
143

            
144
impl pallet_evm::Config for Runtime {
145
	type FeeCalculator = ();
146
	type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
147
	type WeightPerGas = WeightPerGas;
148
	type CallOrigin = EnsureAddressRoot<AccountId>;
149
	type WithdrawOrigin = EnsureAddressNever<AccountId>;
150
	type AddressMapping = AccountId;
151
	type Currency = Balances;
152
	type RuntimeEvent = RuntimeEvent;
153
	type Runner = pallet_evm::runner::stack::Runner<Self>;
154
	type PrecompilesType = Precompiles<Runtime>;
155
	type PrecompilesValue = PrecompilesValue;
156
	type ChainId = ();
157
	type OnChargeTransaction = ();
158
	type BlockGasLimit = BlockGasLimit;
159
	type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping<Self>;
160
	type FindAuthor = ();
161
	type OnCreate = ();
162
	type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
163
	type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio;
164
	type Timestamp = Timestamp;
165
	type WeightInfo = pallet_evm::weights::SubstrateWeight<Runtime>;
166
	type AccountProvider = FrameSystemAccountProvider<Runtime>;
167
}
168

            
169
parameter_types! {
170
	pub const MinimumPeriod: u64 = 5;
171
}
172
impl pallet_timestamp::Config for Runtime {
173
	type Moment = u64;
174
	type OnTimestampSet = ();
175
	type MinimumPeriod = MinimumPeriod;
176
	type WeightInfo = ();
177
}
178

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

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

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

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

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

            
207
34
		let mut ext = sp_io::TestExternalities::new(t);
208
34
		ext.execute_with(|| {
209
34
			System::set_block_number(1);
210
34
			pallet_evm::Pallet::<Runtime>::create_account(
211
34
				Revert.into(),
212
34
				hex_literal::hex!("1460006000fd").to_vec(),
213
34
			);
214
34
		});
215
34
		ext
216
34
	}
217
}
218

            
219
30
pub fn balance(account: impl Into<AccountId>) -> Balance {
220
30
	pallet_balances::Pallet::<Runtime>::usable_balance(account.into())
221
30
}