1
// Copyright 2019-2022 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};
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
1844
construct_runtime!(
37
	pub enum Runtime	{
38
		System: frame_system,
39
		Balances: pallet_balances,
40
		Evm: pallet_evm,
41
		Timestamp: pallet_timestamp,
42
	}
43
2450
);
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
}
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
}
102

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

            
119
pub type PCall = BatchPrecompileCall<Runtime>;
120

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

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

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

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

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

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

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

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

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

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

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

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