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, parameter_types,
23
	traits::Everything,
24
	weights::{RuntimeDbWeight, Weight},
25
};
26
use pallet_evm::{AddressMapping, EnsureAddressTruncated};
27
use sp_core::{H160, H256, U256};
28
use sp_runtime::{
29
	traits::{BlakeTwo256, IdentityLookup},
30
	AccountId32, BuildStorage, Perbill,
31
};
32

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

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

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

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

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

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

            
114
parameter_types! {
115
	pub const MinimumPeriod: u64 = 6000 / 2;
116
}
117

            
118
impl pallet_timestamp::Config for Test {
119
	type Moment = u64;
120
	type OnTimestampSet = ();
121
	type MinimumPeriod = MinimumPeriod;
122
	type WeightInfo = ();
123
}
124

            
125
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
126
/// Block Storage Limit in bytes. Set to 40KB.
127
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;
128

            
129
parameter_types! {
130
	pub BlockGasLimit: U256 = U256::from(u64::MAX);
131
	pub 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
	pub SuicideQuickClearLimit: u32 = 0;
141
}
142
pub struct HashedAddressMapping;
143

            
144
impl AddressMapping<AccountId32> for HashedAddressMapping {
145
100
	fn into_account_id(address: H160) -> AccountId32 {
146
100
		let mut data = [0u8; 32];
147
100
		data[0..20].copy_from_slice(&address[..]);
148
100
		AccountId32::from(Into::<[u8; 32]>::into(data))
149
100
	}
150
}
151

            
152
impl pallet_evm::Config for Test {
153
	type FeeCalculator = ();
154
	type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
155
	type WeightPerGas = WeightPerGas;
156
	type CallOrigin = EnsureAddressTruncated;
157
	type WithdrawOrigin = EnsureAddressTruncated;
158
	type AddressMapping = HashedAddressMapping;
159
	type Currency = Balances;
160
	type RuntimeEvent = RuntimeEvent;
161
	type Runner = pallet_evm::runner::stack::Runner<Self>;
162
	type PrecompilesType = ();
163
	type PrecompilesValue = ();
164
	type ChainId = ();
165
	type OnChargeTransaction = ();
166
	type BlockGasLimit = BlockGasLimit;
167
	type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping<Self>;
168
	type FindAuthor = ();
169
	type OnCreate = ();
170
	type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
171
	type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio;
172
	type Timestamp = Timestamp;
173
	type WeightInfo = pallet_evm::weights::SubstrateWeight<Test>;
174
	type SuicideQuickClearLimit = SuicideQuickClearLimit;
175
}
176

            
177
impl Config for Test {
178
	type WeightInfo = ();
179
}
180

            
181
/// Externality builder for pallet migration's mock runtime
182
pub(crate) struct ExtBuilder {
183
	// endowed accounts with balances
184
	balances: Vec<(AccountId32, Balance)>,
185
}
186

            
187
impl Default for ExtBuilder {
188
10
	fn default() -> ExtBuilder {
189
10
		ExtBuilder { balances: vec![] }
190
10
	}
191
}
192

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

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

            
205
10
		let mut ext = sp_io::TestExternalities::new(storage);
206
10
		ext.execute_with(|| System::set_block_number(1));
207
10
		ext
208
10
	}
209
}