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 frame_system::EnsureRoot;
21
use pallet_evm::{EnsureAddressNever, EnsureAddressRoot, FrameSystemAccountProvider};
22
use precompile_utils::{precompile_set::*, testing::MockAccount};
23
use sp_core::{H256, U256};
24
use sp_runtime::{
25
	traits::{BlakeTwo256, IdentityLookup},
26
	BuildStorage, Perbill,
27
};
28

            
29
pub type AccountId = MockAccount;
30
pub type Balance = u128;
31

            
32
type Block = frame_system::mocking::MockBlockU32<Runtime>;
33

            
34
138
construct_runtime!(
35
48
	pub enum Runtime	{
36
48
		System: frame_system,
37
48
		Balances: pallet_balances,
38
48
		Evm: pallet_evm,
39
48
		Timestamp: pallet_timestamp,
40
48
		Preimage: pallet_preimage,
41
48
	}
42
147
);
43

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

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

            
104
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
105
/// Block storage limit in bytes. Set to 40 KB.
106
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;
107

            
108
parameter_types! {
109
	pub BlockGasLimit: U256 = U256::from(u64::MAX);
110
	pub PrecompilesValue: Precompiles<Runtime> = Precompiles::new();
111
	pub const WeightPerGas: Weight = Weight::from_parts(1, 0);
112
	pub GasLimitPovSizeRatio: u64 = {
113
		let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
114
		block_gas_limit.saturating_div(MAX_POV_SIZE)
115
	};
116
	pub GasLimitStorageGrowthRatio: u64 = {
117
		let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
118
		block_gas_limit.saturating_div(BLOCK_STORAGE_LIMIT)
119
	};
120
}
121

            
122
pub type Precompiles<R> =
123
	PrecompileSetBuilder<R, (PrecompileAt<AddressU64<1>, PreimagePrecompile<R>>,)>;
124

            
125
pub type PCall = PreimagePrecompileCall<Runtime>;
126

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

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

            
162
impl pallet_preimage::Config for Runtime {
163
	type WeightInfo = ();
164
	type RuntimeEvent = RuntimeEvent;
165
	type Currency = Balances;
166
	type ManagerOrigin = EnsureRoot<AccountId>;
167
	type Consideration = ();
168
}
169

            
170
pub(crate) struct ExtBuilder {
171
	// endowed accounts with balances
172
	balances: Vec<(AccountId, Balance)>,
173
}
174

            
175
impl Default for ExtBuilder {
176
2
	fn default() -> ExtBuilder {
177
2
		ExtBuilder { balances: vec![] }
178
2
	}
179
}
180

            
181
impl ExtBuilder {
182
2
	pub(crate) fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self {
183
2
		self.balances = balances;
184
2
		self
185
2
	}
186

            
187
2
	pub(crate) fn build(self) -> sp_io::TestExternalities {
188
2
		let mut t = frame_system::GenesisConfig::<Runtime>::default()
189
2
			.build_storage()
190
2
			.expect("Frame system builds valid default genesis config");
191
2

            
192
2
		pallet_balances::GenesisConfig::<Runtime> {
193
2
			balances: self.balances,
194
2
		}
195
2
		.assimilate_storage(&mut t)
196
2
		.expect("Pallet balances storage can be assimilated");
197
2

            
198
2
		let mut ext = sp_io::TestExternalities::new(t);
199
2
		ext.execute_with(|| System::set_block_number(1));
200
2
		ext
201
2
	}
202
}
203

            
204
3
pub(crate) fn events() -> Vec<RuntimeEvent> {
205
3
	System::events()
206
3
		.into_iter()
207
12
		.map(|r| r.event)
208
3
		.collect::<Vec<_>>()
209
3
}