referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Cumulus.

// Cumulus is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Cumulus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Cumulus.  If not, see <http://www.gnu.org/licenses/>.

//! Module contains predefined test-case scenarios for `Runtime` with bridging capabilities
//! with remote parachain.

use crate::{
	test_cases::{bridges_prelude::*, helpers, run_test},
	test_data,
};

use alloc::{boxed::Box, vec};
use bp_header_chain::ChainWithGrandpa;
use bp_messages::UnrewardedRelayersState;
use bp_polkadot_core::parachains::ParaHash;
use bp_relayers::{RewardsAccountOwner, RewardsAccountParams};
use bp_runtime::{Chain, Parachain};
use bp_xcm_bridge_hub::XcmAsPlainPayload;
use frame_support::traits::{OnFinalize, OnInitialize};
use frame_system::pallet_prelude::BlockNumberFor;
use pallet_bridge_messages::{BridgedChainOf, LaneIdOf, ThisChainOf};
use parachains_runtimes_test_utils::{
	AccountIdOf, BasicParachainRuntime, CollatorSessionKeys, RuntimeCallOf, SlotDurations,
};
use sp_core::Get;
use sp_keyring::Sr25519Keyring::*;
use sp_runtime::{traits::Header as HeaderT, AccountId32};
use xcm::latest::prelude::*;

/// Helper trait to test bridges with remote parachain.
///
/// This is only used to decrease amount of lines, dedicated to bounds.
pub trait WithRemoteParachainHelper {
	/// This chain runtime.
	type Runtime: BasicParachainRuntime
		+ cumulus_pallet_xcmp_queue::Config
		+ BridgeGrandpaConfig<Self::GPI>
		+ BridgeParachainsConfig<Self::PPI>
		+ BridgeMessagesConfig<
			Self::MPI,
			InboundPayload = XcmAsPlainPayload,
			OutboundPayload = XcmAsPlainPayload,
		> + pallet_bridge_relayers::Config<Self::RPI, LaneId = LaneIdOf<Self::Runtime, Self::MPI>>;
	/// All pallets of this chain, excluding system pallet.
	type AllPalletsWithoutSystem: OnInitialize<BlockNumberFor<Self::Runtime>>
		+ OnFinalize<BlockNumberFor<Self::Runtime>>;
	/// Instance of the `pallet-bridge-grandpa`, used to bridge with remote relay chain.
	type GPI: 'static;
	/// Instance of the `pallet-bridge-parachains`, used to bridge with remote parachain.
	type PPI: 'static;
	/// Instance of the `pallet-bridge-messages`, used to bridge with remote parachain.
	type MPI: 'static;
	/// Instance of the `pallet-bridge-relayers`, used to collect rewards from messages `MPI`
	/// instance.
	type RPI: 'static;
}

/// Adapter struct that implements `WithRemoteParachainHelper`.
pub struct WithRemoteParachainHelperAdapter<Runtime, AllPalletsWithoutSystem, GPI, PPI, MPI, RPI>(
	core::marker::PhantomData<(Runtime, AllPalletsWithoutSystem, GPI, PPI, MPI, RPI)>,
);

impl<Runtime, AllPalletsWithoutSystem, GPI, PPI, MPI, RPI> WithRemoteParachainHelper
	for WithRemoteParachainHelperAdapter<Runtime, AllPalletsWithoutSystem, GPI, PPI, MPI, RPI>
where
	Runtime: BasicParachainRuntime
		+ cumulus_pallet_xcmp_queue::Config
		+ BridgeGrandpaConfig<GPI>
		+ BridgeParachainsConfig<PPI>
		+ BridgeMessagesConfig<
			MPI,
			InboundPayload = XcmAsPlainPayload,
			OutboundPayload = XcmAsPlainPayload,
		> + pallet_bridge_relayers::Config<RPI, LaneId = LaneIdOf<Runtime, MPI>>,
	AllPalletsWithoutSystem:
		OnInitialize<BlockNumberFor<Runtime>> + OnFinalize<BlockNumberFor<Runtime>>,
	GPI: 'static,
	PPI: 'static,
	MPI: 'static,
	RPI: 'static,
{
	type Runtime = Runtime;
	type AllPalletsWithoutSystem = AllPalletsWithoutSystem;
	type GPI = GPI;
	type PPI = PPI;
	type MPI = MPI;
	type RPI = RPI;
}

/// Test-case makes sure that Runtime can dispatch XCM messages submitted by relayer,
/// with proofs (finality, para heads, message) independently submitted.
/// Also verifies relayer transaction signed extensions work as intended.
pub fn relayed_incoming_message_works<RuntimeHelper>(
	collator_session_key: CollatorSessionKeys<RuntimeHelper::Runtime>,
	slot_durations: SlotDurations,
	runtime_para_id: u32,
	bridged_para_id: u32,
	sibling_parachain_id: u32,
	local_relay_chain_id: NetworkId,
	prepare_configuration: impl Fn() -> LaneIdOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
	construct_and_apply_extrinsic: fn(
		sp_keyring::Sr25519Keyring,
		<RuntimeHelper::Runtime as frame_system::Config>::RuntimeCall,
	) -> sp_runtime::DispatchOutcome,
	expect_rewards: bool,
) where
	RuntimeHelper: WithRemoteParachainHelper,
	AccountIdOf<RuntimeHelper::Runtime>: From<AccountId32>,
	RuntimeCallOf<RuntimeHelper::Runtime>: From<BridgeGrandpaCall<RuntimeHelper::Runtime, RuntimeHelper::GPI>>
		+ From<BridgeParachainsCall<RuntimeHelper::Runtime, RuntimeHelper::PPI>>
		+ From<BridgeMessagesCall<RuntimeHelper::Runtime, RuntimeHelper::MPI>>,
	BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>: Chain<Hash = ParaHash> + Parachain,
	<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain:
		bp_runtime::Chain<Hash = RelayBlockHash, BlockNumber = RelayBlockNumber> + ChainWithGrandpa,
{
	helpers::relayed_incoming_message_works::<
		RuntimeHelper::Runtime,
		RuntimeHelper::AllPalletsWithoutSystem,
		RuntimeHelper::MPI,
	>(
		collator_session_key,
		slot_durations,
		runtime_para_id,
		sibling_parachain_id,
		local_relay_chain_id,
		construct_and_apply_extrinsic,
		|relayer_id_at_this_chain,
		 relayer_id_at_bridged_chain,
		 message_destination,
		 message_nonce,
		 xcm,
		 bridged_chain_id| {
			let para_header_number = 5;
			let relay_header_number = 1;

			let lane_id = prepare_configuration();

			// start with bridged relay chain block#0
			helpers::initialize_bridge_grandpa_pallet::<RuntimeHelper::Runtime, RuntimeHelper::GPI>(
				test_data::initialization_data::<RuntimeHelper::Runtime, RuntimeHelper::GPI>(0),
			);

			// generate bridged relay chain finality, parachain heads and message proofs,
			// to be submitted by relayer to this chain.
			let (
				relay_chain_header,
				grandpa_justification,
				parachain_head,
				parachain_heads,
				para_heads_proof,
				message_proof,
			) = test_data::from_parachain::make_complex_relayer_delivery_proofs::<
				<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain,
				BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
				ThisChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
				LaneIdOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
			>(
				lane_id,
				xcm.into(),
				message_nonce,
				message_destination,
				para_header_number,
				relay_header_number,
				bridged_para_id,
				false,
			);

			let parachain_head_hash = parachain_head.hash();
			let relay_chain_header_hash = relay_chain_header.hash();
			let relay_chain_header_number = *relay_chain_header.number();
			vec![
				(
					BridgeGrandpaCall::<RuntimeHelper::Runtime, RuntimeHelper::GPI>::submit_finality_proof {
						finality_target: Box::new(relay_chain_header),
						justification: grandpa_justification,
					}.into(),
					helpers::VerifySubmitGrandpaFinalityProofOutcome::<RuntimeHelper::Runtime, RuntimeHelper::GPI>::expect_best_header_hash(
						relay_chain_header_hash,
					),
				),
				(
					BridgeParachainsCall::<RuntimeHelper::Runtime, RuntimeHelper::PPI>::submit_parachain_heads {
						at_relay_block: (relay_chain_header_number, relay_chain_header_hash),
						parachains: parachain_heads,
						parachain_heads_proof: para_heads_proof,
					}.into(),
					helpers::VerifySubmitParachainHeaderProofOutcome::<RuntimeHelper::Runtime, RuntimeHelper::PPI>::expect_best_header_hash(
						bridged_para_id,
						parachain_head_hash,
					),
				),
				(
					BridgeMessagesCall::<RuntimeHelper::Runtime, RuntimeHelper::MPI>::receive_messages_proof {
						relayer_id_at_bridged_chain,
						proof: Box::new(message_proof),
						messages_count: 1,
						dispatch_weight: Weight::from_parts(1000000000, 0),
					}.into(),
					Box::new((
						helpers::VerifySubmitMessagesProofOutcome::<RuntimeHelper::Runtime, RuntimeHelper::MPI>::expect_last_delivered_nonce(
							lane_id,
							1,
						),
						if expect_rewards {
                            helpers::VerifyRelayerRewarded::<RuntimeHelper::Runtime, RuntimeHelper::RPI>::expect_relayer_reward(
                                relayer_id_at_this_chain,
                                RewardsAccountParams::new(
                                    lane_id,
                                    bridged_chain_id,
                                    RewardsAccountOwner::ThisChain,
                                ),
                            )
						} else {
							Box::new(())
						}
					)),
				),
			]
		},
	);
}

/// Test-case makes sure that Runtime can dispatch XCM messages submitted by relayer,
/// with proofs (finality, para heads, message) independently submitted.
/// Finality and para heads are submitted for free in this test.
/// Also verifies relayer transaction signed extensions work as intended.
pub fn free_relay_extrinsic_works<RuntimeHelper>(
	collator_session_key: CollatorSessionKeys<RuntimeHelper::Runtime>,
	slot_durations: SlotDurations,
	runtime_para_id: u32,
	bridged_para_id: u32,
	sibling_parachain_id: u32,
	local_relay_chain_id: NetworkId,
	prepare_configuration: impl Fn() -> LaneIdOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
	construct_and_apply_extrinsic: fn(
		sp_keyring::Sr25519Keyring,
		<RuntimeHelper::Runtime as frame_system::Config>::RuntimeCall,
	) -> sp_runtime::DispatchOutcome,
	expect_rewards: bool,
) where
	RuntimeHelper: WithRemoteParachainHelper,
	RuntimeHelper::Runtime: pallet_balances::Config,
	AccountIdOf<RuntimeHelper::Runtime>: From<AccountId32>,
	RuntimeCallOf<RuntimeHelper::Runtime>: From<BridgeGrandpaCall<RuntimeHelper::Runtime, RuntimeHelper::GPI>>
		+ From<BridgeParachainsCall<RuntimeHelper::Runtime, RuntimeHelper::PPI>>
		+ From<BridgeMessagesCall<RuntimeHelper::Runtime, RuntimeHelper::MPI>>,
	BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>: Chain<Hash = ParaHash> + Parachain,
	<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain:
		bp_runtime::Chain<Hash = RelayBlockHash, BlockNumber = RelayBlockNumber> + ChainWithGrandpa,
{
	// ensure that the runtime allows free header submissions
	let free_headers_interval = <RuntimeHelper::Runtime as BridgeGrandpaConfig<
		RuntimeHelper::GPI,
	>>::FreeHeadersInterval::get()
	.expect("this test requires runtime, configured to accept headers for free; qed");

	helpers::relayed_incoming_message_works::<
		RuntimeHelper::Runtime,
		RuntimeHelper::AllPalletsWithoutSystem,
		RuntimeHelper::MPI,
	>(
		collator_session_key,
		slot_durations,
		runtime_para_id,
		sibling_parachain_id,
		local_relay_chain_id,
		construct_and_apply_extrinsic,
		|relayer_id_at_this_chain,
		 relayer_id_at_bridged_chain,
		 message_destination,
		 message_nonce,
		 xcm,
		 bridged_chain_id| {
			let lane_id = prepare_configuration();

			// start with bridged relay chain block#0
			let initial_block_number = 0;
			helpers::initialize_bridge_grandpa_pallet::<RuntimeHelper::Runtime, RuntimeHelper::GPI>(
				test_data::initialization_data::<RuntimeHelper::Runtime, RuntimeHelper::GPI>(
					initial_block_number,
				),
			);

			// free relay chain header is `0 + free_headers_interval`
			let relay_header_number = initial_block_number + free_headers_interval;
			// first parachain header is always submitted for free
			let para_header_number = 1;

			// relayer balance shall not change after relay and para header submissions
			let initial_relayer_balance =
				pallet_balances::Pallet::<RuntimeHelper::Runtime>::free_balance(
					relayer_id_at_this_chain.clone(),
				);

			// initialize the `FreeHeadersRemaining` storage value
			pallet_bridge_grandpa::Pallet::<RuntimeHelper::Runtime, RuntimeHelper::GPI>::on_initialize(
				0u32.into(),
			);

			// generate bridged relay chain finality, parachain heads and message proofs,
			// to be submitted by relayer to this chain.
			let (
				relay_chain_header,
				grandpa_justification,
				parachain_head,
				parachain_heads,
				para_heads_proof,
				message_proof,
			) = test_data::from_parachain::make_complex_relayer_delivery_proofs::<
				<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain,
				BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
				ThisChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
				LaneIdOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
			>(
				lane_id,
				xcm.into(),
				message_nonce,
				message_destination,
				para_header_number,
				relay_header_number,
				bridged_para_id,
				true,
			);

			let parachain_head_hash = parachain_head.hash();
			let relay_chain_header_hash = relay_chain_header.hash();
			let relay_chain_header_number = *relay_chain_header.number();
			vec![
				(
					BridgeGrandpaCall::<RuntimeHelper::Runtime, RuntimeHelper::GPI>::submit_finality_proof {
						finality_target: Box::new(relay_chain_header),
						justification: grandpa_justification,
					}.into(),
					Box::new((
						helpers::VerifySubmitGrandpaFinalityProofOutcome::<RuntimeHelper::Runtime, RuntimeHelper::GPI>::expect_best_header_hash(
							relay_chain_header_hash,
						),
						helpers::VerifyRelayerBalance::<RuntimeHelper::Runtime>::expect_relayer_balance(
							relayer_id_at_this_chain.clone(),
							initial_relayer_balance,
						),
					)),
				),
				(
					BridgeParachainsCall::<RuntimeHelper::Runtime, RuntimeHelper::PPI>::submit_parachain_heads {
						at_relay_block: (relay_chain_header_number, relay_chain_header_hash),
						parachains: parachain_heads,
						parachain_heads_proof: para_heads_proof,
					}.into(),
					Box::new((
						helpers::VerifySubmitParachainHeaderProofOutcome::<RuntimeHelper::Runtime, RuntimeHelper::PPI>::expect_best_header_hash(
							bridged_para_id,
							parachain_head_hash,
						),
						helpers::VerifyRelayerBalance::<RuntimeHelper::Runtime>::expect_relayer_balance(
							relayer_id_at_this_chain.clone(),
							initial_relayer_balance,
						),
					)),
				),
				(
					BridgeMessagesCall::<RuntimeHelper::Runtime, RuntimeHelper::MPI>::receive_messages_proof {
						relayer_id_at_bridged_chain,
						proof: Box::new(message_proof),
						messages_count: 1,
						dispatch_weight: Weight::from_parts(1000000000, 0),
					}.into(),
					Box::new((
						helpers::VerifySubmitMessagesProofOutcome::<RuntimeHelper::Runtime, RuntimeHelper::MPI>::expect_last_delivered_nonce(
							lane_id,
							1,
						),
						if expect_rewards {
                            helpers::VerifyRelayerRewarded::<RuntimeHelper::Runtime, RuntimeHelper::RPI>::expect_relayer_reward(
                                relayer_id_at_this_chain,
                                RewardsAccountParams::new(
                                    lane_id,
                                    bridged_chain_id,
                                    RewardsAccountOwner::ThisChain,
                                ),
                            )
						} else {
							Box::new(())
						}
					)),
				),
			]
		},
	);
}

/// Test-case makes sure that Runtime can dispatch XCM messages submitted by relayer,
/// with proofs (finality, para heads, message) batched together in signed extrinsic.
/// Also verifies relayer transaction signed extensions work as intended.
pub fn complex_relay_extrinsic_works<RuntimeHelper>(
	collator_session_key: CollatorSessionKeys<RuntimeHelper::Runtime>,
	slot_durations: SlotDurations,
	runtime_para_id: u32,
	bridged_para_id: u32,
	sibling_parachain_id: u32,
	local_relay_chain_id: NetworkId,
	prepare_configuration: impl Fn() -> LaneIdOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
	construct_and_apply_extrinsic: fn(
		sp_keyring::Sr25519Keyring,
		<RuntimeHelper::Runtime as frame_system::Config>::RuntimeCall,
	) -> sp_runtime::DispatchOutcome,
) where
	RuntimeHelper: WithRemoteParachainHelper,
	RuntimeHelper::Runtime:
		pallet_utility::Config<RuntimeCall = RuntimeCallOf<RuntimeHelper::Runtime>>,
	AccountIdOf<RuntimeHelper::Runtime>: From<AccountId32>,
	RuntimeCallOf<RuntimeHelper::Runtime>: From<BridgeGrandpaCall<RuntimeHelper::Runtime, RuntimeHelper::GPI>>
		+ From<BridgeParachainsCall<RuntimeHelper::Runtime, RuntimeHelper::PPI>>
		+ From<BridgeMessagesCall<RuntimeHelper::Runtime, RuntimeHelper::MPI>>
		+ From<pallet_utility::Call<RuntimeHelper::Runtime>>,
	BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>: Chain<Hash = ParaHash> + Parachain,
	<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain:
		bp_runtime::Chain<Hash = RelayBlockHash, BlockNumber = RelayBlockNumber> + ChainWithGrandpa,
{
	helpers::relayed_incoming_message_works::<
		RuntimeHelper::Runtime,
		RuntimeHelper::AllPalletsWithoutSystem,
		RuntimeHelper::MPI,
	>(
		collator_session_key,
		slot_durations,
		runtime_para_id,
		sibling_parachain_id,
		local_relay_chain_id,
		construct_and_apply_extrinsic,
		|relayer_id_at_this_chain,
		 relayer_id_at_bridged_chain,
		 message_destination,
		 message_nonce,
		 xcm,
		 bridged_chain_id| {
			let para_header_number = 5;
			let relay_header_number = 1;

			let lane_id = prepare_configuration();

			// start with bridged relay chain block#0
			helpers::initialize_bridge_grandpa_pallet::<RuntimeHelper::Runtime, RuntimeHelper::GPI>(
				test_data::initialization_data::<RuntimeHelper::Runtime, RuntimeHelper::GPI>(0),
			);

			// generate bridged relay chain finality, parachain heads and message proofs,
			// to be submitted by relayer to this chain.
			let (
				relay_chain_header,
				grandpa_justification,
				parachain_head,
				parachain_heads,
				para_heads_proof,
				message_proof,
			) = test_data::from_parachain::make_complex_relayer_delivery_proofs::<
				<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain,
				BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
				ThisChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
				LaneIdOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
			>(
				lane_id,
				xcm.into(),
				message_nonce,
				message_destination,
				para_header_number,
				relay_header_number,
				bridged_para_id,
				false,
			);

			let parachain_head_hash = parachain_head.hash();
			let relay_chain_header_hash = relay_chain_header.hash();
			let relay_chain_header_number = *relay_chain_header.number();
			vec![
				(
					pallet_utility::Call::<RuntimeHelper::Runtime>::batch_all {
						calls: vec![
						BridgeGrandpaCall::<RuntimeHelper::Runtime, RuntimeHelper::GPI>::submit_finality_proof {
							finality_target: Box::new(relay_chain_header),
							justification: grandpa_justification,
						}.into(),
						BridgeParachainsCall::<RuntimeHelper::Runtime, RuntimeHelper::PPI>::submit_parachain_heads {
							at_relay_block: (relay_chain_header_number, relay_chain_header_hash),
							parachains: parachain_heads,
							parachain_heads_proof: para_heads_proof,
						}.into(),
						BridgeMessagesCall::<RuntimeHelper::Runtime, RuntimeHelper::MPI>::receive_messages_proof {
							relayer_id_at_bridged_chain,
							proof: Box::new(message_proof),
							messages_count: 1,
							dispatch_weight: Weight::from_parts(1000000000, 0),
						}.into(),
					],
					}
					.into(),
					Box::new(
						(
							helpers::VerifySubmitGrandpaFinalityProofOutcome::<
								RuntimeHelper::Runtime,
								RuntimeHelper::GPI,
							>::expect_best_header_hash(relay_chain_header_hash),
							helpers::VerifySubmitParachainHeaderProofOutcome::<
								RuntimeHelper::Runtime,
								RuntimeHelper::PPI,
							>::expect_best_header_hash(bridged_para_id, parachain_head_hash),
							helpers::VerifySubmitMessagesProofOutcome::<
								RuntimeHelper::Runtime,
								RuntimeHelper::MPI,
							>::expect_last_delivered_nonce(lane_id, 1),
							helpers::VerifyRelayerRewarded::<
								RuntimeHelper::Runtime,
								RuntimeHelper::RPI,
							>::expect_relayer_reward(
								relayer_id_at_this_chain,
								RewardsAccountParams::new(
									lane_id,
									bridged_chain_id,
									RewardsAccountOwner::ThisChain,
								),
							),
						),
					),
				),
			]
		},
	);
}

/// Estimates transaction fee for default message delivery transaction (batched with required
/// proofs) from bridged parachain.
pub fn can_calculate_fee_for_complex_message_delivery_transaction<RuntimeHelper>(
	collator_session_key: CollatorSessionKeys<RuntimeHelper::Runtime>,
	compute_extrinsic_fee: fn(pallet_utility::Call<RuntimeHelper::Runtime>) -> u128,
) -> u128
where
	RuntimeHelper: WithRemoteParachainHelper,
	RuntimeHelper::Runtime:
		pallet_utility::Config<RuntimeCall = RuntimeCallOf<RuntimeHelper::Runtime>>,
	RuntimeCallOf<RuntimeHelper::Runtime>: From<BridgeGrandpaCall<RuntimeHelper::Runtime, RuntimeHelper::GPI>>
		+ From<BridgeParachainsCall<RuntimeHelper::Runtime, RuntimeHelper::PPI>>
		+ From<BridgeMessagesCall<RuntimeHelper::Runtime, RuntimeHelper::MPI>>,
	BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>: Chain<Hash = ParaHash> + Parachain,
	<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain:
		bp_runtime::Chain<Hash = RelayBlockHash, BlockNumber = RelayBlockNumber> + ChainWithGrandpa,
{
	run_test::<RuntimeHelper::Runtime, _>(collator_session_key, 1000, vec![], || {
		// generate bridged relay chain finality, parachain heads and message proofs,
		// to be submitted by relayer to this chain.
		//
		// we don't care about parameter values here, apart from the XCM message size. But we
		// do not need to have a large message here, because we're charging for every byte of
		// the message additionally
		let (
			relay_chain_header,
			grandpa_justification,
			_,
			parachain_heads,
			para_heads_proof,
			message_proof,
		) = test_data::from_parachain::make_complex_relayer_delivery_proofs::<
			<RuntimeHelper::Runtime as pallet_bridge_grandpa::Config<RuntimeHelper::GPI>>::BridgedChain,
			BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
			ThisChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
			LaneIdOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>
		>(
			LaneIdOf::<RuntimeHelper::Runtime, RuntimeHelper::MPI>::default(),
			vec![Instruction::<()>::ClearOrigin; 1_024].into(),
			1,
			[GlobalConsensus(Polkadot), Parachain(1_000)].into(),
			1,
			5,
			1_000,
			false,
		);

		// generate batch call that provides finality for bridged relay and parachains + message
		// proof
		let batch = test_data::from_parachain::make_complex_relayer_delivery_batch::<
			RuntimeHelper::Runtime,
			RuntimeHelper::GPI,
			RuntimeHelper::PPI,
			RuntimeHelper::MPI,
		>(
			relay_chain_header,
			grandpa_justification,
			parachain_heads,
			para_heads_proof,
			message_proof,
			helpers::relayer_id_at_bridged_chain::<RuntimeHelper::Runtime, RuntimeHelper::MPI>(),
		);

		compute_extrinsic_fee(batch)
	})
}

/// Estimates transaction fee for default message confirmation transaction (batched with required
/// proofs) from bridged parachain.
pub fn can_calculate_fee_for_complex_message_confirmation_transaction<RuntimeHelper>(
	collator_session_key: CollatorSessionKeys<RuntimeHelper::Runtime>,
	compute_extrinsic_fee: fn(pallet_utility::Call<RuntimeHelper::Runtime>) -> u128,
) -> u128
where
	RuntimeHelper: WithRemoteParachainHelper,
	AccountIdOf<RuntimeHelper::Runtime>: From<AccountId32>,
	RuntimeHelper::Runtime:
		pallet_utility::Config<RuntimeCall = RuntimeCallOf<RuntimeHelper::Runtime>>,
	ThisChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>:
		Chain<AccountId = AccountIdOf<RuntimeHelper::Runtime>>,
	RuntimeCallOf<RuntimeHelper::Runtime>: From<BridgeGrandpaCall<RuntimeHelper::Runtime, RuntimeHelper::GPI>>
		+ From<BridgeParachainsCall<RuntimeHelper::Runtime, RuntimeHelper::PPI>>
		+ From<BridgeMessagesCall<RuntimeHelper::Runtime, RuntimeHelper::MPI>>,
	BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>: Chain<Hash = ParaHash> + Parachain,
	<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain:
		bp_runtime::Chain<Hash = RelayBlockHash, BlockNumber = RelayBlockNumber> + ChainWithGrandpa,
{
	run_test::<RuntimeHelper::Runtime, _>(collator_session_key, 1000, vec![], || {
		// generate bridged relay chain finality, parachain heads and message proofs,
		// to be submitted by relayer to this chain.
		let unrewarded_relayers = UnrewardedRelayersState {
			unrewarded_relayer_entries: 1,
			total_messages: 1,
			..Default::default()
		};
		let (
			relay_chain_header,
			grandpa_justification,
			_,
			parachain_heads,
			para_heads_proof,
			message_delivery_proof,
		) = test_data::from_parachain::make_complex_relayer_confirmation_proofs::<
			<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain,
			BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
			ThisChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
			LaneIdOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
		>(
			LaneIdOf::<RuntimeHelper::Runtime, RuntimeHelper::MPI>::default(),
			1,
			5,
			1_000,
			AccountId32::from(Alice.public()).into(),
			unrewarded_relayers.clone(),
		);

		// generate batch call that provides finality for bridged relay and parachains + message
		// proof
		let batch = test_data::from_parachain::make_complex_relayer_confirmation_batch::<
			RuntimeHelper::Runtime,
			RuntimeHelper::GPI,
			RuntimeHelper::PPI,
			RuntimeHelper::MPI,
		>(
			relay_chain_header,
			grandpa_justification,
			parachain_heads,
			para_heads_proof,
			message_delivery_proof,
			unrewarded_relayers,
		);

		compute_extrinsic_fee(batch)
	})
}

/// Estimates transaction fee for default message delivery transaction from bridged parachain.
pub fn can_calculate_fee_for_standalone_message_delivery_transaction<RuntimeHelper>(
	collator_session_key: CollatorSessionKeys<RuntimeHelper::Runtime>,
	compute_extrinsic_fee: fn(
		<RuntimeHelper::Runtime as frame_system::Config>::RuntimeCall,
	) -> u128,
) -> u128
where
	RuntimeHelper: WithRemoteParachainHelper,
	RuntimeCallOf<RuntimeHelper::Runtime>:
		From<BridgeMessagesCall<RuntimeHelper::Runtime, RuntimeHelper::MPI>>,
	BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>: Chain<Hash = ParaHash> + Parachain,
	<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain:
		bp_runtime::Chain<Hash = RelayBlockHash, BlockNumber = RelayBlockNumber> + ChainWithGrandpa,
{
	run_test::<RuntimeHelper::Runtime, _>(collator_session_key, 1000, vec![], || {
		// generate bridged relay chain finality, parachain heads and message proofs,
		// to be submitted by relayer to this chain.
		//
		// we don't care about parameter values here, apart from the XCM message size. But we
		// do not need to have a large message here, because we're charging for every byte of
		// the message additionally
		let (
			_,
			_,
			_,
			_,
			_,
			message_proof,
		) = test_data::from_parachain::make_complex_relayer_delivery_proofs::<
			<RuntimeHelper::Runtime as pallet_bridge_grandpa::Config<RuntimeHelper::GPI>>::BridgedChain,
			BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
			ThisChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
			LaneIdOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
		>(
			LaneIdOf::<RuntimeHelper::Runtime, RuntimeHelper::MPI>::default(),
			vec![Instruction::<()>::ClearOrigin; 1_024].into(),
			1,
			[GlobalConsensus(Polkadot), Parachain(1_000)].into(),
			1,
			5,
			1_000,
			false,
		);

		let call = test_data::from_parachain::make_standalone_relayer_delivery_call::<
			RuntimeHelper::Runtime,
			RuntimeHelper::MPI,
		>(
			message_proof,
			helpers::relayer_id_at_bridged_chain::<RuntimeHelper::Runtime, RuntimeHelper::MPI>(),
		);

		compute_extrinsic_fee(call)
	})
}

/// Estimates transaction fee for default message confirmation transaction (batched with required
/// proofs) from bridged parachain.
pub fn can_calculate_fee_for_standalone_message_confirmation_transaction<RuntimeHelper>(
	collator_session_key: CollatorSessionKeys<RuntimeHelper::Runtime>,
	compute_extrinsic_fee: fn(
		<RuntimeHelper::Runtime as frame_system::Config>::RuntimeCall,
	) -> u128,
) -> u128
where
	RuntimeHelper: WithRemoteParachainHelper,
	AccountIdOf<RuntimeHelper::Runtime>: From<AccountId32>,
	ThisChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>:
		Chain<AccountId = AccountIdOf<RuntimeHelper::Runtime>>,
	RuntimeCallOf<RuntimeHelper::Runtime>:
		From<BridgeMessagesCall<RuntimeHelper::Runtime, RuntimeHelper::MPI>>,
	BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>: Chain<Hash = ParaHash> + Parachain,
	<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain:
		bp_runtime::Chain<Hash = RelayBlockHash, BlockNumber = RelayBlockNumber> + ChainWithGrandpa,
{
	run_test::<RuntimeHelper::Runtime, _>(collator_session_key, 1000, vec![], || {
		// generate bridged relay chain finality, parachain heads and message proofs,
		// to be submitted by relayer to this chain.
		let unrewarded_relayers = UnrewardedRelayersState {
			unrewarded_relayer_entries: 1,
			total_messages: 1,
			..Default::default()
		};
		let (_, _, _, _, _, message_delivery_proof) =
			test_data::from_parachain::make_complex_relayer_confirmation_proofs::<
				<RuntimeHelper::Runtime as BridgeGrandpaConfig<RuntimeHelper::GPI>>::BridgedChain,
				BridgedChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
				ThisChainOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
				LaneIdOf<RuntimeHelper::Runtime, RuntimeHelper::MPI>,
			>(
				LaneIdOf::<RuntimeHelper::Runtime, RuntimeHelper::MPI>::default(),
				1,
				5,
				1_000,
				AccountId32::from(Alice.public()).into(),
				unrewarded_relayers.clone(),
			);

		let call = test_data::from_parachain::make_standalone_relayer_confirmation_call::<
			RuntimeHelper::Runtime,
			RuntimeHelper::MPI,
		>(message_delivery_proof, unrewarded_relayers);

		compute_extrinsic_fee(call)
	})
}