1
// Copyright 2024 Moonbeam foundation
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
//! A minimal runtime including the multi block migrations pallet
18

            
19
use super::*;
20
use crate as pallet_moonbeam_lazy_migrations;
21
use frame_support::{
22
	construct_runtime, ord_parameter_types, parameter_types,
23
	traits::{EqualPrivilegeOnly, Everything, SortedMembers},
24
	weights::{RuntimeDbWeight, Weight},
25
};
26
use frame_system::EnsureRoot;
27
use pallet_evm::{AddressMapping, EnsureAddressTruncated};
28
use sp_core::{ConstU32, H160, H256, U256};
29
use sp_runtime::{
30
	traits::{BlakeTwo256, IdentityLookup},
31
	AccountId32, BuildStorage, Perbill,
32
};
33

            
34
pub type Balance = u128;
35
type Block = frame_system::mocking::MockBlock<Test>;
36

            
37
// Configure a mock runtime to test the pallet.
38
13574
construct_runtime!(
39
	pub enum Test
40
	{
41
		System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
42
		Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
43
		Timestamp: pallet_timestamp,
44
		EVM: pallet_evm,
45
		LazyMigrations: pallet_moonbeam_lazy_migrations::{Pallet, Call},
46
		Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>},
47
	}
48
52458
);
49

            
50
parameter_types! {
51
	pub const BlockHashCount: u32 = 250;
52
	pub const MaximumBlockWeight: Weight = Weight::from_parts(1024, 1);
53
	pub const MaximumBlockLength: u32 = 2 * 1024;
54
	pub const AvailableBlockRatio: Perbill = Perbill::one();
55
	pub const SS58Prefix: u8 = 42;
56
}
57

            
58
parameter_types! {
59
	pub const MockDbWeight: RuntimeDbWeight = RuntimeDbWeight {
60
		read: 1_000_000,
61
		write: 1,
62
	};
63
}
64

            
65
impl frame_system::Config for Test {
66
	type BaseCallFilter = Everything;
67
	type DbWeight = MockDbWeight;
68
	type RuntimeOrigin = RuntimeOrigin;
69
	type RuntimeTask = RuntimeTask;
70
	type Nonce = u64;
71
	type Block = Block;
72
	type RuntimeCall = RuntimeCall;
73
	type Hash = H256;
74
	type Hashing = BlakeTwo256;
75
	type AccountId = AccountId32;
76
	type Lookup = IdentityLookup<Self::AccountId>;
77
	type RuntimeEvent = RuntimeEvent;
78
	type BlockHashCount = BlockHashCount;
79
	type Version = ();
80
	type PalletInfo = PalletInfo;
81
	type AccountData = pallet_balances::AccountData<Balance>;
82
	type OnNewAccount = ();
83
	type OnKilledAccount = ();
84
	type SystemWeightInfo = ();
85
	type BlockWeights = ();
86
	type BlockLength = ();
87
	type SS58Prefix = SS58Prefix;
88
	type OnSetCode = ();
89
	type MaxConsumers = frame_support::traits::ConstU32<16>;
90
	type SingleBlockMigrations = ();
91
	type MultiBlockMigrator = ();
92
	type PreInherents = ();
93
	type PostInherents = ();
94
	type PostTransactions = ();
95
}
96

            
97
parameter_types! {
98
	pub const ExistentialDeposit: u128 = 0;
99
}
100
impl pallet_balances::Config for Test {
101
	type MaxReserves = ();
102
	type ReserveIdentifier = ();
103
	type MaxLocks = ();
104
	type Balance = Balance;
105
	type RuntimeEvent = RuntimeEvent;
106
	type DustRemoval = ();
107
	type ExistentialDeposit = ExistentialDeposit;
108
	type AccountStore = System;
109
	type WeightInfo = ();
110
	type RuntimeHoldReason = ();
111
	type FreezeIdentifier = ();
112
	type MaxFreezes = ();
113
	type RuntimeFreezeReason = ();
114
}
115

            
116
impl pallet_scheduler::Config for Test {
117
	type RuntimeEvent = RuntimeEvent;
118
	type RuntimeOrigin = RuntimeOrigin;
119
	type PalletsOrigin = OriginCaller;
120
	type RuntimeCall = RuntimeCall;
121
	type MaximumWeight = ();
122
	type ScheduleOrigin = EnsureRoot<Self::AccountId>;
123
	type MaxScheduledPerBlock = ConstU32<100>;
124
	type WeightInfo = ();
125
	type OriginPrivilegeCmp = EqualPrivilegeOnly;
126
	type Preimages = ();
127
}
128

            
129
ord_parameter_types! {
130
	pub const One: u64 = 1;
131
	pub const Two: u64 = 2;
132
	pub const Three: u64 = 3;
133
	pub const Four: u64 = 4;
134
	pub const Five: u64 = 5;
135
	pub const Six: u64 = 6;
136
}
137
pub struct OneToFive;
138
impl SortedMembers<u64> for OneToFive {
139
	fn sorted_members() -> Vec<u64> {
140
		vec![1, 2, 3, 4, 5]
141
	}
142
	#[cfg(feature = "runtime-benchmarks")]
143
	fn add(_m: &u64) {}
144
}
145

            
146
parameter_types! {
147
	pub const MinimumPeriod: u64 = 6000 / 2;
148
}
149

            
150
impl pallet_timestamp::Config for Test {
151
	type Moment = u64;
152
	type OnTimestampSet = ();
153
	type MinimumPeriod = MinimumPeriod;
154
	type WeightInfo = ();
155
}
156

            
157
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
158
/// Block Storage Limit in bytes. Set to 40KB.
159
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;
160

            
161
parameter_types! {
162
	pub BlockGasLimit: U256 = U256::from(u64::MAX);
163
	pub WeightPerGas: Weight = Weight::from_parts(1, 0);
164
	pub GasLimitPovSizeRatio: u64 = {
165
		let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
166
		block_gas_limit.saturating_div(MAX_POV_SIZE)
167
	};
168
	pub GasLimitStorageGrowthRatio: u64 = {
169
		let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
170
		block_gas_limit.saturating_div(BLOCK_STORAGE_LIMIT)
171
	};
172
	pub SuicideQuickClearLimit: u32 = 0;
173
}
174
pub struct HashedAddressMapping;
175

            
176
impl AddressMapping<AccountId32> for HashedAddressMapping {
177
113
	fn into_account_id(address: H160) -> AccountId32 {
178
113
		let mut data = [0u8; 32];
179
113
		data[0..20].copy_from_slice(&address[..]);
180
113
		AccountId32::from(Into::<[u8; 32]>::into(data))
181
113
	}
182
}
183

            
184
impl pallet_evm::Config for Test {
185
	type FeeCalculator = ();
186
	type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
187
	type WeightPerGas = WeightPerGas;
188
	type CallOrigin = EnsureAddressTruncated;
189
	type WithdrawOrigin = EnsureAddressTruncated;
190
	type AddressMapping = HashedAddressMapping;
191
	type Currency = Balances;
192
	type RuntimeEvent = RuntimeEvent;
193
	type Runner = pallet_evm::runner::stack::Runner<Self>;
194
	type PrecompilesType = ();
195
	type PrecompilesValue = ();
196
	type ChainId = ();
197
	type OnChargeTransaction = ();
198
	type BlockGasLimit = BlockGasLimit;
199
	type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping<Self>;
200
	type FindAuthor = ();
201
	type OnCreate = ();
202
	type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
203
	type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio;
204
	type Timestamp = Timestamp;
205
	type WeightInfo = pallet_evm::weights::SubstrateWeight<Test>;
206
	type SuicideQuickClearLimit = SuicideQuickClearLimit;
207
}
208

            
209
impl Config for Test {
210
	type WeightInfo = ();
211
}
212

            
213
/// Externality builder for pallet migration's mock runtime
214
pub(crate) struct ExtBuilder {
215
	// endowed accounts with balances
216
	balances: Vec<(AccountId32, Balance)>,
217
}
218

            
219
impl Default for ExtBuilder {
220
16
	fn default() -> ExtBuilder {
221
16
		ExtBuilder { balances: vec![] }
222
16
	}
223
}
224

            
225
impl ExtBuilder {
226
16
	pub(crate) fn build(self) -> sp_io::TestExternalities {
227
16
		let mut storage = frame_system::GenesisConfig::<Test>::default()
228
16
			.build_storage()
229
16
			.expect("Frame system builds valid default genesis config");
230
16

            
231
16
		pallet_balances::GenesisConfig::<Test> {
232
16
			balances: self.balances,
233
16
		}
234
16
		.assimilate_storage(&mut storage)
235
16
		.expect("Pallet balances storage can be assimilated");
236
16

            
237
16
		let mut ext = sp_io::TestExternalities::new(storage);
238
16
		ext.execute_with(|| System::set_block_number(1));
239
16
		ext
240
16
	}
241
}