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, pallet_prelude::*, parameter_types};
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
340
construct_runtime!(
37
	pub enum Runtime	{
38
		System: frame_system,
39
		Balances: pallet_balances,
40
		Evm: pallet_evm,
41
		Timestamp: pallet_timestamp,
42
	}
43
529
);
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
16
mock_account!(Registry, |_| MockAccount::from_u64(1));
104
3
mock_account!(Removed, |_| MockAccount::from_u64(2));
105
16
mock_account!(SmartContract, |_| MockAccount::from_u64(3));
106

            
107
pub type Precompiles<R> = PrecompileSetBuilder<
108
	R,
109
	(
110
		PrecompileAt<AddressU64<1>, PrecompileRegistry<R>>,
111
		RemovedPrecompileAt<AddressU64<2>>,
112
	),
113
>;
114

            
115
pub type PCall = PrecompileRegistryCall<Runtime>;
116

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

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

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

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

            
162
impl Default for ExtBuilder {
163
13
	fn default() -> ExtBuilder {
164
13
		ExtBuilder { balances: vec![] }
165
13
	}
166
}
167

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

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

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

            
185
13
		let mut ext = sp_io::TestExternalities::new(t);
186
13
		ext.execute_with(|| {
187
13
			System::set_block_number(1);
188
13
			pallet_evm::Pallet::<Runtime>::create_account(
189
13
				SmartContract.into(),
190
13
				b"SmartContract".to_vec(),
191
13
			);
192
13
		});
193
13
		ext
194
13
	}
195
}