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, pallet_prelude::*, parameter_types};
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
179
construct_runtime!(
37
17
	pub enum Runtime	{
38
17
		System: frame_system,
39
17
		Balances: pallet_balances,
40
17
		Evm: pallet_evm,
41
17
		Timestamp: pallet_timestamp,
42
17
	}
43
180
);
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
18
mock_account!(CallPermit, |_| MockAccount::from_u64(1));
106
7
mock_account!(Revert, |_| MockAccount::from_u64(2));
107

            
108
pub type Precompiles<R> = PrecompileSetBuilder<
109
	R,
110
	(
111
		PrecompileAt<AddressU64<1>, CallPermitPrecompile<R>, SubcallWithMaxNesting<0>>,
112
		RevertPrecompile<AddressU64<2>>,
113
	),
114
>;
115

            
116
pub type PCall = CallPermitPrecompileCall<Runtime>;
117

            
118
parameter_types! {
119
	pub PrecompilesValue: Precompiles<Runtime> = Precompiles::new();
120
	pub const WeightPerGas: Weight = Weight::from_parts(1, 0);
121
	pub const SuicideQuickClearLimit: u32 = 0;
122
}
123

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

            
149
parameter_types! {
150
	pub const MinimumPeriod: u64 = 5;
151
}
152
impl pallet_timestamp::Config for Runtime {
153
	type Moment = u64;
154
	type OnTimestampSet = ();
155
	type MinimumPeriod = MinimumPeriod;
156
	type WeightInfo = ();
157
}
158

            
159
pub(crate) struct ExtBuilder {
160
	// endowed accounts with balances
161
	balances: Vec<(AccountId, Balance)>,
162
}
163

            
164
impl Default for ExtBuilder {
165
7
	fn default() -> ExtBuilder {
166
7
		ExtBuilder { balances: vec![] }
167
7
	}
168
}
169

            
170
impl ExtBuilder {
171
7
	pub(crate) fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self {
172
7
		self.balances = balances;
173
7
		self
174
7
	}
175

            
176
7
	pub(crate) fn build(self) -> sp_io::TestExternalities {
177
7
		let mut t = frame_system::GenesisConfig::<Runtime>::default()
178
7
			.build_storage()
179
7
			.expect("Frame system builds valid default genesis config");
180
7

            
181
7
		pallet_balances::GenesisConfig::<Runtime> {
182
7
			balances: self.balances,
183
7
		}
184
7
		.assimilate_storage(&mut t)
185
7
		.expect("Pallet balances storage can be assimilated");
186
7

            
187
7
		let mut ext = sp_io::TestExternalities::new(t);
188
7
		ext.execute_with(|| {
189
7
			System::set_block_number(1);
190
7
			pallet_evm::Pallet::<Runtime>::create_account(
191
7
				Revert.into(),
192
7
				hex_literal::hex!("1460006000fd").to_vec(),
193
7
			);
194
7
		});
195
7
		ext
196
7
	}
197
}
198

            
199
// pub fn balance(account: impl Into<Account>) -> Balance {
200
// 	pallet_balances::Pallet::<Runtime>::usable_balance(account.into())
201
// }