1
// Copyright 2019-2025 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
#![allow(dead_code)]
18

            
19
use cumulus_primitives_parachain_inherent::ParachainInherentData;
20
use fp_evm::GenesisAccount;
21
use frame_support::{
22
	assert_ok,
23
	traits::{OnFinalize, OnInitialize},
24
};
25
pub use moonriver_runtime::{
26
	currency::MOVR, AccountId, AsyncBacking, AuthorInherent, Balance, Ethereum, InflationInfo,
27
	ParachainStaking, Range, Runtime, RuntimeCall, RuntimeEvent, System, TransactionConverter,
28
	UncheckedExtrinsic, HOURS,
29
};
30
use nimbus_primitives::{NimbusId, NIMBUS_ENGINE_ID};
31
use polkadot_parachain::primitives::HeadData;
32
use sp_consensus_slots::Slot;
33
use sp_core::{Encode, H160};
34
use sp_runtime::{traits::Dispatchable, BuildStorage, Digest, DigestItem, Perbill, Percent};
35

            
36
use cumulus_pallet_parachain_system::{MessagingStateSnapshot, ValidationData};
37
use cumulus_primitives_core::AbridgedHrmpChannel;
38
use fp_rpc::ConvertTransaction;
39
use moonriver_runtime::bridge_config::XcmOverPolkadotInstance;
40
use moonriver_runtime::{EvmForeignAssets, XcmWeightTrader};
41
use pallet_transaction_payment::Multiplier;
42
use std::collections::BTreeMap;
43
use xcm::latest::{InteriorLocation, Location};
44

            
45
10
pub fn existential_deposit() -> u128 {
46
10
	<Runtime as pallet_balances::Config>::ExistentialDeposit::get()
47
10
}
48

            
49
// A valid signed Alice transfer.
50
pub const VALID_ETH_TX: &str =
51
	"02f86d8205018085174876e80085e8d4a5100082520894f24ff3a9cf04c71dbc94d0b566f7a27b9456\
52
	6cac8080c001a0e1094e1a52520a75c0255db96132076dd0f1263089f838bea548cbdbfc64a4d19f031c\
53
	92a8cb04e2d68d20a6158d542a07ac440cc8d07b6e36af02db046d92df";
54

            
55
// An invalid signed Alice transfer with a gas limit artifically set to 0.
56
pub const INVALID_ETH_TX: &str =
57
	"f86180843b9aca00809412cb274aad8251c875c0bf6872b67d9983e53fdd01801ca00e28ba2dd3c5a\
58
	3fd467d4afd7aefb4a34b373314fff470bb9db743a84d674a0aa06e5994f2d07eafe1c37b4ce5471ca\
59
	ecec29011f6f5bf0b1a552c55ea348df35f";
60

            
61
3
pub fn rpc_run_to_block(n: u32) {
62
6
	while System::block_number() < n {
63
3
		Ethereum::on_finalize(System::block_number());
64
3
		System::set_block_number(System::block_number() + 1);
65
3
		Ethereum::on_initialize(System::block_number());
66
3
	}
67
3
}
68

            
69
/// Utility function that advances the chain to the desired block number.
70
/// If an author is provided, that author information is injected to all the blocks in the meantime.
71
9
pub fn run_to_block(n: u32, author: Option<NimbusId>) {
72
6609
	while System::block_number() < n {
73
		// Set the new block number and author
74
6600
		match author {
75
6600
			Some(ref author) => {
76
6600
				let pre_digest = Digest {
77
6600
					logs: vec![DigestItem::PreRuntime(NIMBUS_ENGINE_ID, author.encode())],
78
6600
				};
79
6600
				System::reset_events();
80
6600
				System::initialize(
81
6600
					&(System::block_number() + 1),
82
6600
					&System::parent_hash(),
83
6600
					&pre_digest,
84
6600
				);
85
6600
			}
86
			None => {
87
				System::set_block_number(System::block_number() + 1);
88
			}
89
		}
90

            
91
6600
		increase_last_relay_slot_number(1);
92
6600

            
93
6600
		// Initialize the new block
94
6600
		AuthorInherent::on_initialize(System::block_number());
95
6600
		ParachainStaking::on_initialize(System::block_number());
96
6600

            
97
6600
		// Finalize the block
98
6600
		ParachainStaking::on_finalize(System::block_number());
99
	}
100
9
}
101

            
102
1
pub fn last_event() -> RuntimeEvent {
103
1
	System::events().pop().expect("Event expected").event
104
1
}
105

            
106
// Helper function to give a simple evm context suitable for tests.
107
// We can remove this once https://github.com/rust-blockchain/evm/pull/35
108
// is in our dependency graph.
109
pub fn evm_test_context() -> fp_evm::Context {
110
	fp_evm::Context {
111
		address: Default::default(),
112
		caller: Default::default(),
113
		apparent_value: From::from(0),
114
	}
115
}
116

            
117
// Test struct with the purpose of initializing xcm assets
118
#[derive(Clone)]
119
pub struct XcmAssetInitialization {
120
	pub asset_id: u128,
121
	pub xcm_location: xcm::v5::Location,
122
	pub decimals: u8,
123
	pub name: &'static str,
124
	pub symbol: &'static str,
125
	pub balances: Vec<(AccountId, Balance)>,
126
}
127

            
128
pub struct ExtBuilder {
129
	asset_hub_migration_started: bool,
130
	// endowed accounts with balances
131
	balances: Vec<(AccountId, Balance)>,
132
	// [collator, amount]
133
	collators: Vec<(AccountId, Balance)>,
134
	// [delegator, collator, nomination_amount]
135
	delegations: Vec<(AccountId, AccountId, Balance, Percent)>,
136
	// per-round inflation config
137
	inflation: InflationInfo<Balance>,
138
	// AuthorId -> AccoutId mappings
139
	mappings: Vec<(NimbusId, AccountId)>,
140
	// Crowdloan fund
141
	crowdloan_fund: Balance,
142
	// Chain id
143
	chain_id: u64,
144
	// EVM genesis accounts
145
	evm_accounts: BTreeMap<H160, GenesisAccount>,
146
	// [assettype, metadata, Vec<Account, Balance,>, is_sufficient]
147
	xcm_assets: Vec<XcmAssetInitialization>,
148
	safe_xcm_version: Option<u32>,
149
	opened_bridges: Vec<(Location, InteriorLocation, Option<bp_moonbeam::LaneId>)>,
150
}
151

            
152
impl Default for ExtBuilder {
153
60
	fn default() -> ExtBuilder {
154
60
		ExtBuilder {
155
60
			asset_hub_migration_started: false,
156
60
			balances: vec![],
157
60
			delegations: vec![],
158
60
			collators: vec![],
159
60
			inflation: InflationInfo {
160
60
				expect: Range {
161
60
					min: 100_000 * MOVR,
162
60
					ideal: 200_000 * MOVR,
163
60
					max: 500_000 * MOVR,
164
60
				},
165
60
				// not used
166
60
				annual: Range {
167
60
					min: Perbill::from_percent(50),
168
60
					ideal: Perbill::from_percent(50),
169
60
					max: Perbill::from_percent(50),
170
60
				},
171
60
				// unrealistically high parameterization, only for testing
172
60
				round: Range {
173
60
					min: Perbill::from_percent(5),
174
60
					ideal: Perbill::from_percent(5),
175
60
					max: Perbill::from_percent(5),
176
60
				},
177
60
			},
178
60
			mappings: vec![],
179
60
			crowdloan_fund: 0,
180
60
			chain_id: CHAIN_ID,
181
60
			evm_accounts: BTreeMap::new(),
182
60
			xcm_assets: vec![],
183
60
			safe_xcm_version: None,
184
60
			opened_bridges: vec![],
185
60
		}
186
60
	}
187
}
188

            
189
impl ExtBuilder {
190
1
	pub fn asset_hub_migration_has_started(mut self) -> Self {
191
1
		self.asset_hub_migration_started = true;
192
1
		self
193
1
	}
194

            
195
2
	pub fn with_evm_accounts(mut self, accounts: BTreeMap<H160, GenesisAccount>) -> Self {
196
2
		self.evm_accounts = accounts;
197
2
		self
198
2
	}
199

            
200
37
	pub fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self {
201
37
		self.balances = balances;
202
37
		self
203
37
	}
204

            
205
9
	pub fn with_collators(mut self, collators: Vec<(AccountId, Balance)>) -> Self {
206
9
		self.collators = collators;
207
9
		self
208
9
	}
209

            
210
7
	pub fn with_delegations(mut self, delegations: Vec<(AccountId, AccountId, Balance)>) -> Self {
211
7
		self.delegations = delegations
212
7
			.into_iter()
213
8
			.map(|d| (d.0, d.1, d.2, Percent::zero()))
214
7
			.collect();
215
7
		self
216
7
	}
217

            
218
	pub fn with_crowdloan_fund(mut self, crowdloan_fund: Balance) -> Self {
219
		self.crowdloan_fund = crowdloan_fund;
220
		self
221
	}
222

            
223
8
	pub fn with_mappings(mut self, mappings: Vec<(NimbusId, AccountId)>) -> Self {
224
8
		self.mappings = mappings;
225
8
		self
226
8
	}
227

            
228
	#[allow(dead_code)]
229
	pub fn with_inflation(mut self, inflation: InflationInfo<Balance>) -> Self {
230
		self.inflation = inflation;
231
		self
232
	}
233

            
234
6
	pub fn with_xcm_assets(mut self, xcm_assets: Vec<XcmAssetInitialization>) -> Self {
235
6
		self.xcm_assets = xcm_assets;
236
6
		self
237
6
	}
238

            
239
7
	pub fn with_safe_xcm_version(mut self, safe_xcm_version: u32) -> Self {
240
7
		self.safe_xcm_version = Some(safe_xcm_version);
241
7
		self
242
7
	}
243

            
244
2
	pub fn with_open_bridges(
245
2
		mut self,
246
2
		opened_bridges: Vec<(Location, InteriorLocation, Option<bp_moonbeam::LaneId>)>,
247
2
	) -> Self {
248
2
		self.opened_bridges = opened_bridges;
249
2
		self
250
2
	}
251

            
252
60
	pub fn build(self) -> sp_io::TestExternalities {
253
60
		let mut t = frame_system::GenesisConfig::<Runtime>::default()
254
60
			.build_storage()
255
60
			.unwrap();
256
60

            
257
60
		parachain_info::GenesisConfig::<Runtime> {
258
60
			parachain_id: <bp_moonriver::Moonriver as bp_runtime::Parachain>::PARACHAIN_ID.into(),
259
60
			_config: Default::default(),
260
60
		}
261
60
		.assimilate_storage(&mut t)
262
60
		.unwrap();
263
60

            
264
60
		pallet_balances::GenesisConfig::<Runtime> {
265
60
			balances: self.balances,
266
60
			dev_accounts: None,
267
60
		}
268
60
		.assimilate_storage(&mut t)
269
60
		.unwrap();
270
60

            
271
60
		pallet_parachain_staking::GenesisConfig::<Runtime> {
272
60
			candidates: self.collators,
273
60
			delegations: self.delegations,
274
60
			inflation_config: self.inflation,
275
60
			collator_commission: Perbill::from_percent(20),
276
60
			parachain_bond_reserve_percent: Percent::from_percent(30),
277
60
			blocks_per_round: 2 * HOURS,
278
60
			num_selected_candidates: 8,
279
60
		}
280
60
		.assimilate_storage(&mut t)
281
60
		.unwrap();
282
60

            
283
60
		pallet_author_mapping::GenesisConfig::<Runtime> {
284
60
			mappings: self.mappings,
285
60
		}
286
60
		.assimilate_storage(&mut t)
287
60
		.unwrap();
288
60

            
289
60
		let genesis_config = pallet_evm_chain_id::GenesisConfig::<Runtime> {
290
60
			chain_id: self.chain_id,
291
60
			..Default::default()
292
60
		};
293
60
		genesis_config.assimilate_storage(&mut t).unwrap();
294
60

            
295
60
		let genesis_config = pallet_evm::GenesisConfig::<Runtime> {
296
60
			accounts: self.evm_accounts,
297
60
			..Default::default()
298
60
		};
299
60
		genesis_config.assimilate_storage(&mut t).unwrap();
300
60

            
301
60
		let genesis_config = pallet_ethereum::GenesisConfig::<Runtime> {
302
60
			..Default::default()
303
60
		};
304
60
		genesis_config.assimilate_storage(&mut t).unwrap();
305
60

            
306
60
		let genesis_config = pallet_xcm::GenesisConfig::<Runtime> {
307
60
			safe_xcm_version: self.safe_xcm_version,
308
60
			..Default::default()
309
60
		};
310
60
		genesis_config.assimilate_storage(&mut t).unwrap();
311
60

            
312
60
		let genesis_config = pallet_transaction_payment::GenesisConfig::<Runtime> {
313
60
			multiplier: Multiplier::from(10u128),
314
60
			..Default::default()
315
60
		};
316
60
		genesis_config.assimilate_storage(&mut t).unwrap();
317
60

            
318
60
		let genesis_config = pallet_xcm_bridge::GenesisConfig::<Runtime, XcmOverPolkadotInstance> {
319
60
			opened_bridges: self.opened_bridges,
320
60
			_phantom: Default::default(),
321
60
		};
322
60
		genesis_config.assimilate_storage(&mut t).unwrap();
323
60

            
324
60
		let mut ext = sp_io::TestExternalities::new(t);
325
60
		let xcm_assets = self.xcm_assets.clone();
326
60
		ext.execute_with(|| {
327
60
			if self.asset_hub_migration_started {
328
1
				// Indicate that the asset-hub migration has already started
329
1
				moonriver_runtime::xcm_config::AssetHubMigrationStartsAtRelayBlock::set(&0);
330
1

            
331
1
				let mut validation_data = ValidationData::<Runtime>::get().unwrap_or_default();
332
1

            
333
1
				validation_data.relay_parent_number =
334
1
					moonriver_runtime::xcm_config::AssetHubMigrationStartsAtRelayBlock::get();
335
1
				ValidationData::<Runtime>::set(Some(validation_data));
336
59
			}
337

            
338
			// Mock hrmp egress_channels
339
60
			cumulus_pallet_parachain_system::RelevantMessagingState::<Runtime>::put(
340
60
				MessagingStateSnapshot {
341
60
					dmq_mqc_head: Default::default(),
342
60
					relay_dispatch_queue_remaining_capacity: Default::default(),
343
60
					ingress_channels: vec![],
344
60
					egress_channels: vec![(
345
60
						1_000.into(),
346
60
						AbridgedHrmpChannel {
347
60
							max_capacity: u32::MAX,
348
60
							max_total_size: u32::MAX,
349
60
							max_message_size: u32::MAX,
350
60
							msg_count: 0,
351
60
							total_size: 0,
352
60
							mqc_head: None,
353
60
						},
354
60
					)],
355
60
				},
356
60
			);
357

            
358
			// If any xcm assets specified, we register them here
359
66
			for xcm_asset_initialization in xcm_assets {
360
6
				let asset_id = xcm_asset_initialization.asset_id;
361
6
				EvmForeignAssets::create_foreign_asset(
362
6
					root_origin(),
363
6
					asset_id,
364
6
					xcm_asset_initialization.xcm_location.clone(),
365
6
					xcm_asset_initialization.decimals,
366
6
					xcm_asset_initialization
367
6
						.symbol
368
6
						.as_bytes()
369
6
						.to_vec()
370
6
						.try_into()
371
6
						.expect("too long"),
372
6
					xcm_asset_initialization
373
6
						.name
374
6
						.as_bytes()
375
6
						.to_vec()
376
6
						.try_into()
377
6
						.expect("too long"),
378
6
				)
379
6
				.expect("fail to create foreign asset");
380
6

            
381
6
				XcmWeightTrader::add_asset(
382
6
					root_origin(),
383
6
					xcm_asset_initialization.xcm_location,
384
6
					MOVR,
385
6
				)
386
6
				.expect("register evm native foreign asset as sufficient");
387

            
388
12
				for (account, balance) in xcm_asset_initialization.balances {
389
6
					if EvmForeignAssets::mint_into(asset_id, account, balance.into()).is_err() {
390
						panic!("fail to mint foreign asset");
391
6
					}
392
				}
393
			}
394
60
			System::set_block_number(1);
395
60
		});
396
60
		ext
397
60
	}
398
}
399

            
400
pub const CHAIN_ID: u64 = 1281;
401
pub const ALICE: [u8; 20] = [4u8; 20];
402
pub const ALICE_NIMBUS: [u8; 32] = [4u8; 32];
403
pub const BOB: [u8; 20] = [5u8; 20];
404
pub const CHARLIE: [u8; 20] = [6u8; 20];
405
pub const DAVE: [u8; 20] = [7u8; 20];
406
pub const EVM_CONTRACT: [u8; 20] = [8u8; 20];
407

            
408
20
pub fn origin_of(account_id: AccountId) -> <Runtime as frame_system::Config>::RuntimeOrigin {
409
20
	<Runtime as frame_system::Config>::RuntimeOrigin::signed(account_id)
410
20
}
411

            
412
11
pub fn inherent_origin() -> <Runtime as frame_system::Config>::RuntimeOrigin {
413
11
	<Runtime as frame_system::Config>::RuntimeOrigin::none()
414
11
}
415

            
416
23
pub fn root_origin() -> <Runtime as frame_system::Config>::RuntimeOrigin {
417
23
	<Runtime as frame_system::Config>::RuntimeOrigin::root()
418
23
}
419

            
420
/// Mock the inherent that sets validation data in ParachainSystem, which
421
/// contains the `relay_chain_block_number`, which is used in `author-filter` as a
422
/// source of randomness to filter valid authors at each block.
423
11
pub fn set_parachain_inherent_data() {
424
	use cumulus_primitives_core::PersistedValidationData;
425
	use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
426

            
427
11
	let author = AccountId::from(<pallet_evm::Pallet<Runtime>>::find_author());
428
11
	pallet_author_inherent::Author::<Runtime>::put(author);
429
11

            
430
11
	let mut relay_sproof = RelayStateSproofBuilder::default();
431
11
	relay_sproof.para_id = bp_moonriver::PARACHAIN_ID.into();
432
11
	relay_sproof.included_para_head = Some(HeadData(vec![1, 2, 3]));
433
11

            
434
11
	let additional_key_values = vec![];
435
11

            
436
11
	relay_sproof.additional_key_values = additional_key_values;
437
11

            
438
11
	let (relay_parent_storage_root, relay_chain_state) = relay_sproof.into_state_root_and_proof();
439
11

            
440
11
	let vfp = PersistedValidationData {
441
11
		relay_parent_number: 1u32,
442
11
		relay_parent_storage_root,
443
11
		..Default::default()
444
11
	};
445
11
	let parachain_inherent_data = ParachainInherentData {
446
11
		validation_data: vfp,
447
11
		relay_chain_state: relay_chain_state,
448
11
		downward_messages: Default::default(),
449
11
		horizontal_messages: Default::default(),
450
11
	};
451
11
	assert_ok!(RuntimeCall::ParachainSystem(
452
11
		cumulus_pallet_parachain_system::Call::<Runtime>::set_validation_data {
453
11
			data: parachain_inherent_data
454
11
		}
455
11
	)
456
11
	.dispatch(inherent_origin()));
457
11
}
458

            
459
7
pub fn unchecked_eth_tx(raw_hex_tx: &str) -> UncheckedExtrinsic {
460
7
	let converter = TransactionConverter;
461
7
	converter.convert_transaction(ethereum_transaction(raw_hex_tx))
462
7
}
463

            
464
9
pub fn ethereum_transaction(raw_hex_tx: &str) -> pallet_ethereum::Transaction {
465
9
	let bytes = hex::decode(raw_hex_tx).expect("Transaction bytes.");
466
9
	let transaction = ethereum::EnvelopedDecodable::decode(&bytes[..]);
467
9
	assert!(transaction.is_ok());
468
9
	transaction.unwrap()
469
9
}
470

            
471
6602
pub(crate) fn increase_last_relay_slot_number(amount: u64) {
472
6602
	let last_relay_slot = u64::from(AsyncBacking::slot_info().unwrap_or_default().0);
473
6602
	frame_support::storage::unhashed::put(
474
6602
		&frame_support::storage::storage_prefix(b"AsyncBacking", b"SlotInfo"),
475
6602
		&((Slot::from(last_relay_slot + amount), 0)),
476
6602
	);
477
6602
}