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
use frame_support::{construct_runtime, parameter_types, traits::Everything, weights::Weight};
20
use frame_system::EnsureRoot;
21
use pallet_evm::{EnsureAddressNever, EnsureAddressRoot};
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
126
construct_runtime!(
35
	pub enum Runtime	{
36
		System: frame_system,
37
		Balances: pallet_balances,
38
		Evm: pallet_evm,
39
		Timestamp: pallet_timestamp,
40
		Preimage: pallet_preimage,
41
	}
42
215
);
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
}
83
parameter_types! {
84
	pub const ExistentialDeposit: u128 = 0;
85
}
86
impl pallet_balances::Config for Runtime {
87
	type MaxReserves = ();
88
	type ReserveIdentifier = [u8; 4];
89
	type MaxLocks = ();
90
	type Balance = Balance;
91
	type RuntimeEvent = RuntimeEvent;
92
	type DustRemoval = ();
93
	type ExistentialDeposit = ExistentialDeposit;
94
	type AccountStore = System;
95
	type WeightInfo = ();
96
	type RuntimeHoldReason = ();
97
	type FreezeIdentifier = ();
98
	type MaxFreezes = ();
99
	type RuntimeFreezeReason = ();
100
}
101

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

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

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

            
123
pub type PCall = PreimagePrecompileCall<Runtime>;
124

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

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

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

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

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

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

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

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

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

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