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
//! Moonriver Runtime Api Integration Tests
18

            
19
mod common;
20
use common::*;
21

            
22
use fp_evm::{FeeCalculator, GenesisAccount};
23
use frame_support::assert_ok;
24
use nimbus_primitives::NimbusId;
25
use pallet_evm::{Account as EVMAccount, AddressMapping};
26
use sp_core::{ByteArray, H160, H256, U256};
27

            
28
use fp_rpc::runtime_decl_for_ethereum_runtime_rpc_api::EthereumRuntimeRPCApi;
29
use moonbeam_core_primitives::Header;
30
use moonbeam_rpc_primitives_txpool::runtime_decl_for_tx_pool_runtime_api::TxPoolRuntimeApi;
31
use moonriver_runtime::{Executive, TransactionPaymentAsGasPrice};
32
use nimbus_primitives::runtime_decl_for_nimbus_api::NimbusApi;
33
use std::{collections::BTreeMap, str::FromStr};
34

            
35
#[test]
36
1
fn ethereum_runtime_rpc_api_chain_id() {
37
1
	ExtBuilder::default().build().execute_with(|| {
38
1
		assert_eq!(Runtime::chain_id(), CHAIN_ID);
39
1
	});
40
1
}
41

            
42
#[test]
43
1
fn ethereum_runtime_rpc_api_account_basic() {
44
1
	ExtBuilder::default()
45
1
		.with_balances(vec![(
46
1
			AccountId::from(ALICE),
47
1
			2_000 * MOVR + existential_deposit(),
48
1
		)])
49
1
		.build()
50
1
		.execute_with(|| {
51
1
			assert_eq!(
52
1
				Runtime::account_basic(H160::from(ALICE)),
53
1
				EVMAccount {
54
1
					balance: U256::from(2_000 * MOVR),
55
1
					nonce: U256::zero()
56
1
				}
57
1
			);
58
1
		});
59
1
}
60

            
61
#[test]
62
1
fn ethereum_runtime_rpc_api_gas_price() {
63
1
	ExtBuilder::default().build().execute_with(|| {
64
1
		assert_eq!(
65
1
			Runtime::gas_price(),
66
1
			TransactionPaymentAsGasPrice::min_gas_price().0
67
1
		);
68
1
	});
69
1
}
70

            
71
#[test]
72
1
fn ethereum_runtime_rpc_api_account_code_at() {
73
1
	let address = H160::from(EVM_CONTRACT);
74
1
	let code: Vec<u8> = vec![1, 2, 3, 4, 5];
75
1
	ExtBuilder::default()
76
1
		.with_evm_accounts({
77
1
			let mut map = BTreeMap::new();
78
1
			map.insert(
79
1
				address,
80
1
				GenesisAccount {
81
1
					balance: U256::zero(),
82
1
					code: code.clone(),
83
1
					nonce: Default::default(),
84
1
					storage: Default::default(),
85
1
				},
86
1
			);
87
1
			map
88
1
		})
89
1
		.build()
90
1
		.execute_with(|| {
91
1
			assert_eq!(Runtime::account_code_at(address), code);
92
1
		});
93
1
}
94

            
95
#[test]
96
1
fn ethereum_runtime_rpc_api_author() {
97
1
	ExtBuilder::default()
98
1
		.with_collators(vec![(AccountId::from(ALICE), 1_000 * MOVR)])
99
1
		.with_mappings(vec![(
100
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
101
1
			AccountId::from(ALICE),
102
1
		)])
103
1
		.with_balances(vec![
104
1
			(AccountId::from(ALICE), 2_000 * MOVR),
105
1
			(AccountId::from(BOB), 1_000 * MOVR),
106
1
		])
107
1
		.with_delegations(vec![(
108
1
			AccountId::from(BOB),
109
1
			AccountId::from(ALICE),
110
1
			500 * MOVR,
111
1
		)])
112
1
		.build()
113
1
		.execute_with(|| {
114
1
			set_parachain_inherent_data();
115
1
			run_to_block(2, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
116
1
			assert_eq!(Runtime::author(), H160::from(ALICE));
117
1
		});
118
1
}
119

            
120
#[test]
121
1
fn ethereum_runtime_rpc_api_storage_at() {
122
1
	let address = H160::from(EVM_CONTRACT);
123
1
	let mut key = [0u8; 32];
124
1
	key[31..32].copy_from_slice(&[6u8][..]);
125
1
	let mut value = [0u8; 32];
126
1
	value[31..32].copy_from_slice(&[7u8][..]);
127
1
	let item = H256::from_slice(&key[..]);
128
1
	let mut storage: BTreeMap<H256, H256> = BTreeMap::new();
129
1
	storage.insert(H256::from_slice(&key[..]), item);
130
1
	ExtBuilder::default()
131
1
		.with_evm_accounts({
132
1
			let mut map = BTreeMap::new();
133
1
			map.insert(
134
1
				address,
135
1
				GenesisAccount {
136
1
					balance: U256::zero(),
137
1
					code: Vec::new(),
138
1
					nonce: Default::default(),
139
1
					storage: storage.clone(),
140
1
				},
141
1
			);
142
1
			map
143
1
		})
144
1
		.build()
145
1
		.execute_with(|| {
146
1
			assert_eq!(Runtime::storage_at(address, U256::from(6)), item);
147
1
		});
148
1
}
149

            
150
#[test]
151
1
fn ethereum_runtime_rpc_api_call() {
152
1
	ExtBuilder::default()
153
1
		.with_balances(vec![
154
1
			(AccountId::from(ALICE), 2_000 * MOVR),
155
1
			(AccountId::from(BOB), 2_000 * MOVR),
156
1
		])
157
1
		.build()
158
1
		.execute_with(|| {
159
1
			let execution_result = Runtime::call(
160
1
				H160::from(ALICE),     // from
161
1
				H160::from(BOB),       // to
162
1
				Vec::new(),            // data
163
1
				U256::from(1000u64),   // value
164
1
				U256::from(100000u64), // gas_limit
165
1
				None,                  // max_fee_per_gas
166
1
				None,                  // max_priority_fee_per_gas
167
1
				None,                  // nonce
168
1
				false,                 // estimate
169
1
				None,                  // access_list
170
1
				None,                  // authorization_list
171
1
			);
172
1
			assert!(execution_result.is_ok());
173
1
		});
174
1
}
175

            
176
#[test]
177
1
fn ethereum_runtime_rpc_api_create() {
178
1
	ExtBuilder::default()
179
1
		.with_balances(vec![(AccountId::from(ALICE), 2_000 * MOVR)])
180
1
		.build()
181
1
		.execute_with(|| {
182
1
			let execution_result = Runtime::create(
183
1
				H160::from(ALICE),     // from
184
1
				vec![0, 1, 1, 0],      // data
185
1
				U256::zero(),          // value
186
1
				U256::from(100000u64), // gas_limit
187
1
				None,                  // max_fee_per_gas
188
1
				None,                  // max_priority_fee_per_gas
189
1
				None,                  // nonce
190
1
				false,                 // estimate
191
1
				None,                  // access_list
192
1
				None,                  // authorization_list
193
1
			);
194
1
			assert!(execution_result.is_ok());
195
1
		});
196
1
}
197

            
198
#[test]
199
1
fn ethereum_runtime_rpc_api_current_transaction_statuses() {
200
1
	let alith = <Runtime as pallet_evm::Config>::AddressMapping::into_account_id(
201
1
		H160::from_str("f24ff3a9cf04c71dbc94d0b566f7a27b94566cac")
202
1
			.expect("internal H160 is valid; qed"),
203
1
	);
204
1
	ExtBuilder::default()
205
1
		.with_collators(vec![(AccountId::from(ALICE), 1_000 * MOVR)])
206
1
		.with_mappings(vec![(
207
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
208
1
			AccountId::from(ALICE),
209
1
		)])
210
1
		.with_balances(vec![
211
1
			(alith, 2_000 * MOVR),
212
1
			(AccountId::from(ALICE), 2_000 * MOVR),
213
1
			(AccountId::from(BOB), 1_000 * MOVR),
214
1
		])
215
1
		.with_delegations(vec![(
216
1
			AccountId::from(BOB),
217
1
			AccountId::from(ALICE),
218
1
			500 * MOVR,
219
1
		)])
220
1
		.build()
221
1
		.execute_with(|| {
222
1
			set_parachain_inherent_data();
223
1
			// set_author(NimbusId::from_slice(&ALICE_NIMBUS));
224
1
			let result =
225
1
				Executive::apply_extrinsic(unchecked_eth_tx(VALID_ETH_TX)).expect("Apply result.");
226
1
			assert_eq!(result, Ok(()));
227
1
			rpc_run_to_block(2);
228
1
			let statuses =
229
1
				Runtime::current_transaction_statuses().expect("Transaction statuses result.");
230
1
			assert_eq!(statuses.len(), 1);
231
1
		});
232
1
}
233

            
234
#[test]
235
1
fn ethereum_runtime_rpc_api_current_block() {
236
1
	ExtBuilder::default()
237
1
		.with_collators(vec![(AccountId::from(ALICE), 1_000 * MOVR)])
238
1
		.with_mappings(vec![(
239
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
240
1
			AccountId::from(ALICE),
241
1
		)])
242
1
		.with_balances(vec![
243
1
			(AccountId::from(ALICE), 2_000 * MOVR),
244
1
			(AccountId::from(BOB), 1_000 * MOVR),
245
1
		])
246
1
		.with_delegations(vec![(
247
1
			AccountId::from(BOB),
248
1
			AccountId::from(ALICE),
249
1
			500 * MOVR,
250
1
		)])
251
1
		.build()
252
1
		.execute_with(|| {
253
1
			set_parachain_inherent_data();
254
1
			// set_author(NimbusId::from_slice(&ALICE_NIMBUS));
255
1
			rpc_run_to_block(2);
256
1
			let block = Runtime::current_block().expect("Block result.");
257
1
			assert_eq!(block.header.number, U256::from(1u8));
258
1
		});
259
1
}
260

            
261
#[test]
262
1
fn ethereum_runtime_rpc_api_current_receipts() {
263
1
	let alith = <Runtime as pallet_evm::Config>::AddressMapping::into_account_id(
264
1
		H160::from_str("f24ff3a9cf04c71dbc94d0b566f7a27b94566cac")
265
1
			.expect("internal H160 is valid; qed"),
266
1
	);
267
1
	ExtBuilder::default()
268
1
		.with_collators(vec![(AccountId::from(ALICE), 1_000 * MOVR)])
269
1
		.with_mappings(vec![(
270
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
271
1
			AccountId::from(ALICE),
272
1
		)])
273
1
		.with_balances(vec![
274
1
			(alith, 2_000 * MOVR),
275
1
			(AccountId::from(ALICE), 2_000 * MOVR),
276
1
			(AccountId::from(BOB), 1_000 * MOVR),
277
1
		])
278
1
		.with_delegations(vec![(
279
1
			AccountId::from(BOB),
280
1
			AccountId::from(ALICE),
281
1
			500 * MOVR,
282
1
		)])
283
1
		.build()
284
1
		.execute_with(|| {
285
1
			set_parachain_inherent_data();
286
1
			// set_author(NimbusId::from_slice(&ALICE_NIMBUS));
287
1
			let result =
288
1
				Executive::apply_extrinsic(unchecked_eth_tx(VALID_ETH_TX)).expect("Apply result.");
289
1
			assert_eq!(result, Ok(()));
290
1
			rpc_run_to_block(2);
291
1
			let receipts = Runtime::current_receipts().expect("Receipts result.");
292
1
			assert_eq!(receipts.len(), 1);
293
1
		});
294
1
}
295

            
296
#[test]
297
1
fn txpool_runtime_api_extrinsic_filter() {
298
1
	ExtBuilder::default().build().execute_with(|| {
299
1
		let non_eth_uxt = UncheckedExtrinsic::new_bare(
300
1
			pallet_balances::Call::<Runtime>::transfer_allow_death {
301
1
				dest: AccountId::from(BOB),
302
1
				value: 1 * MOVR,
303
1
			}
304
1
			.into(),
305
1
		);
306
1
		let eth_uxt = unchecked_eth_tx(VALID_ETH_TX);
307
1
		let txpool = <Runtime as TxPoolRuntimeApi<moonriver_runtime::Block>>::extrinsic_filter(
308
1
			vec![eth_uxt.clone(), non_eth_uxt.clone()],
309
1
			vec![unchecked_eth_tx(VALID_ETH_TX), non_eth_uxt],
310
1
		);
311
1
		assert_eq!(txpool.ready.len(), 1);
312
1
		assert_eq!(txpool.future.len(), 1);
313
1
	});
314
1
}
315

            
316
#[test]
317
1
fn can_author_when_selected_is_empty() {
318
1
	ExtBuilder::default()
319
1
		.with_balances(vec![
320
1
			(AccountId::from(ALICE), 20_000_000 * MOVR),
321
1
			(AccountId::from(BOB), 10_000_000 * MOVR),
322
1
		])
323
1
		.with_collators(vec![(AccountId::from(ALICE), 2_000_000 * MOVR)])
324
1
		.with_mappings(vec![(
325
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
326
1
			AccountId::from(ALICE),
327
1
		)])
328
1
		.build()
329
1
		.execute_with(|| {
330
1
			set_parachain_inherent_data();
331
1
			run_to_block(2, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
332
1

            
333
1
			assert_eq!(ParachainStaking::candidate_pool().0.len(), 1);
334

            
335
1
			let slot_number = 0;
336
1
			let parent = Header {
337
1
				digest: Default::default(),
338
1
				extrinsics_root: Default::default(),
339
1
				number: Default::default(),
340
1
				parent_hash: Default::default(),
341
1
				state_root: Default::default(),
342
1
			};
343
1

            
344
1
			// Base case: ALICE can author blocks when she is the only candidate
345
1
			let can_author_block = Runtime::can_author(
346
1
				NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
347
1
				slot_number,
348
1
				&parent,
349
1
			);
350
1

            
351
1
			assert!(can_author_block);
352

            
353
			// Remove ALICE from candidate pool, leaving the candidate_pool empty
354
1
			assert_ok!(ParachainStaking::go_offline(origin_of(AccountId::from(
355
1
				ALICE
356
1
			))));
357

            
358
			// Need to fast forward to right before the next session, which is when selected candidates
359
			// will be updated. We want to test the creation of the first block of the next session.
360
1
			run_to_block(1799, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
361
1

            
362
1
			assert_eq!(ParachainStaking::candidate_pool().0.len(), 0);
363

            
364
1
			let slot_number = 0;
365
1
			let parent = Header {
366
1
				digest: Default::default(),
367
1
				extrinsics_root: Default::default(),
368
1
				number: 1799,
369
1
				parent_hash: Default::default(),
370
1
				state_root: Default::default(),
371
1
			};
372
1

            
373
1
			let can_author_block = Runtime::can_author(
374
1
				NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
375
1
				slot_number,
376
1
				&parent,
377
1
			);
378
1

            
379
1
			assert!(can_author_block);
380

            
381
			// Check that it works as expected after session update
382
1
			run_to_block(1800, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
383
1

            
384
1
			assert_eq!(ParachainStaking::candidate_pool().0.len(), 0);
385

            
386
1
			let slot_number = 0;
387
1
			let parent = Header {
388
1
				digest: Default::default(),
389
1
				extrinsics_root: Default::default(),
390
1
				number: 1800,
391
1
				parent_hash: Default::default(),
392
1
				state_root: Default::default(),
393
1
			};
394
1

            
395
1
			let can_author_block = Runtime::can_author(
396
1
				NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
397
1
				slot_number,
398
1
				&parent,
399
1
			);
400
1

            
401
1
			assert!(can_author_block);
402
1
		});
403
1
}