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
// 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/>.

//! Generating test data for bridges with remote parachains.

use super::{from_grandpa_chain::make_complex_bridged_grandpa_header_proof, prepare_inbound_xcm};

use bp_messages::{
	source_chain::FromBridgedChainMessagesDeliveryProof,
	target_chain::FromBridgedChainMessagesProof, ChainWithMessages, LaneState,
	UnrewardedRelayersState, Weight,
};
use bp_parachains::{RelayBlockHash, RelayBlockNumber};
use bp_runtime::{
	AccountIdOf, BlockNumberOf, Chain, HeaderOf, Parachain, UnverifiedStorageProofParams,
};
use bp_test_utils::prepare_parachain_heads_proof;
use bp_xcm_bridge_hub::XcmAsPlainPayload;
use codec::Encode;
use pallet_bridge_grandpa::BridgedHeader;
use sp_runtime::traits::Header as HeaderT;
use xcm::latest::prelude::*;

use crate::test_cases::helpers::InboundRelayerId;
use bp_header_chain::{justification::GrandpaJustification, ChainWithGrandpa};
use bp_messages::{DeliveredMessages, InboundLaneData, MessageNonce, UnrewardedRelayer};
use bp_polkadot_core::parachains::{ParaHash, ParaHead, ParaHeadsProof, ParaId};
use pallet_bridge_messages::{
	messages_generation::{
		encode_all_messages, encode_lane_data, prepare_message_delivery_storage_proof,
		prepare_messages_storage_proof,
	},
	BridgedChainOf, LaneIdOf,
};
use sp_runtime::SaturatedConversion;

/// Prepare a batch call with relay finality proof, parachain head proof and message proof.
pub fn make_complex_relayer_delivery_batch<Runtime, GPI, PPI, MPI>(
	relay_chain_header: BridgedHeader<Runtime, GPI>,
	grandpa_justification: GrandpaJustification<BridgedHeader<Runtime, GPI>>,
	parachain_heads: Vec<(ParaId, ParaHash)>,
	para_heads_proof: ParaHeadsProof,
	message_proof: FromBridgedChainMessagesProof<ParaHash, LaneIdOf<Runtime, MPI>>,
	relayer_id_at_bridged_chain: InboundRelayerId<Runtime, MPI>,
) -> pallet_utility::Call<Runtime>
where
	Runtime: pallet_bridge_grandpa::Config<GPI>
		+ pallet_bridge_parachains::Config<PPI>
		+ pallet_bridge_messages::Config<MPI, InboundPayload = XcmAsPlainPayload>
		+ pallet_utility::Config,
	GPI: 'static,
	PPI: 'static,
	MPI: 'static,
	ParaHash: From<
		<<Runtime as pallet_bridge_grandpa::Config<GPI>>::BridgedChain as bp_runtime::Chain>::Hash,
	>,
	<<Runtime as pallet_bridge_grandpa::Config<GPI>>::BridgedChain as bp_runtime::Chain>::Hash:
		From<ParaHash>,
	BridgedChainOf<Runtime, MPI>: Chain<Hash = ParaHash> + Parachain,
	<Runtime as pallet_utility::Config>::RuntimeCall: From<pallet_bridge_grandpa::Call<Runtime, GPI>>
		+ From<pallet_bridge_parachains::Call<Runtime, PPI>>
		+ From<pallet_bridge_messages::Call<Runtime, MPI>>,
{
	let relay_chain_header_hash = relay_chain_header.hash();
	let relay_chain_header_number = *relay_chain_header.number();
	let submit_grandpa = pallet_bridge_grandpa::Call::<Runtime, GPI>::submit_finality_proof {
		finality_target: Box::new(relay_chain_header),
		justification: grandpa_justification,
	};
	let submit_para_head = pallet_bridge_parachains::Call::<Runtime, PPI>::submit_parachain_heads {
		at_relay_block: (
			relay_chain_header_number.saturated_into(),
			relay_chain_header_hash.into(),
		),
		parachains: parachain_heads,
		parachain_heads_proof: para_heads_proof,
	};
	let submit_message = pallet_bridge_messages::Call::<Runtime, MPI>::receive_messages_proof {
		relayer_id_at_bridged_chain: relayer_id_at_bridged_chain.into(),
		proof: Box::new(message_proof),
		messages_count: 1,
		dispatch_weight: Weight::from_parts(1000000000, 0),
	};
	pallet_utility::Call::<Runtime>::batch_all {
		calls: vec![submit_grandpa.into(), submit_para_head.into(), submit_message.into()],
	}
}

/// Prepare a batch call with relay finality proof, parachain head proof and message delivery
/// proof.
pub fn make_complex_relayer_confirmation_batch<Runtime, GPI, PPI, MPI>(
	relay_chain_header: BridgedHeader<Runtime, GPI>,
	grandpa_justification: GrandpaJustification<BridgedHeader<Runtime, GPI>>,
	parachain_heads: Vec<(ParaId, ParaHash)>,
	para_heads_proof: ParaHeadsProof,
	message_delivery_proof: FromBridgedChainMessagesDeliveryProof<ParaHash, LaneIdOf<Runtime, MPI>>,
	relayers_state: UnrewardedRelayersState,
) -> pallet_utility::Call<Runtime>
where
	Runtime: pallet_bridge_grandpa::Config<GPI>
		+ pallet_bridge_parachains::Config<PPI>
		+ pallet_bridge_messages::Config<MPI, OutboundPayload = XcmAsPlainPayload>
		+ pallet_utility::Config,
	GPI: 'static,
	PPI: 'static,
	MPI: 'static,
	<Runtime as pallet_bridge_grandpa::Config<GPI>>::BridgedChain:
		bp_runtime::Chain<Hash = RelayBlockHash, BlockNumber = RelayBlockNumber> + ChainWithGrandpa,
	BridgedChainOf<Runtime, MPI>: Chain<Hash = ParaHash> + Parachain,
	<Runtime as pallet_utility::Config>::RuntimeCall: From<pallet_bridge_grandpa::Call<Runtime, GPI>>
		+ From<pallet_bridge_parachains::Call<Runtime, PPI>>
		+ From<pallet_bridge_messages::Call<Runtime, MPI>>,
{
	let relay_chain_header_hash = relay_chain_header.hash();
	let relay_chain_header_number = *relay_chain_header.number();
	let submit_grandpa = pallet_bridge_grandpa::Call::<Runtime, GPI>::submit_finality_proof {
		finality_target: Box::new(relay_chain_header),
		justification: grandpa_justification,
	};
	let submit_para_head = pallet_bridge_parachains::Call::<Runtime, PPI>::submit_parachain_heads {
		at_relay_block: (
			relay_chain_header_number.saturated_into(),
			relay_chain_header_hash.into(),
		),
		parachains: parachain_heads,
		parachain_heads_proof: para_heads_proof,
	};
	let submit_message_delivery_proof =
		pallet_bridge_messages::Call::<Runtime, MPI>::receive_messages_delivery_proof {
			proof: message_delivery_proof,
			relayers_state,
		};
	pallet_utility::Call::<Runtime>::batch_all {
		calls: vec![
			submit_grandpa.into(),
			submit_para_head.into(),
			submit_message_delivery_proof.into(),
		],
	}
}

/// Prepare a call with message proof.
pub fn make_standalone_relayer_delivery_call<Runtime, MPI>(
	message_proof: FromBridgedChainMessagesProof<ParaHash, LaneIdOf<Runtime, MPI>>,
	relayer_id_at_bridged_chain: InboundRelayerId<Runtime, MPI>,
) -> Runtime::RuntimeCall
where
	Runtime: pallet_bridge_messages::Config<MPI, InboundPayload = XcmAsPlainPayload>,
	MPI: 'static,
	Runtime::RuntimeCall: From<pallet_bridge_messages::Call<Runtime, MPI>>,
	BridgedChainOf<Runtime, MPI>: Chain<Hash = ParaHash> + Parachain,
{
	pallet_bridge_messages::Call::<Runtime, MPI>::receive_messages_proof {
		relayer_id_at_bridged_chain: relayer_id_at_bridged_chain.into(),
		proof: Box::new(message_proof),
		messages_count: 1,
		dispatch_weight: Weight::from_parts(1000000000, 0),
	}
	.into()
}

/// Prepare a call with message delivery proof.
pub fn make_standalone_relayer_confirmation_call<Runtime, MPI>(
	message_delivery_proof: FromBridgedChainMessagesDeliveryProof<ParaHash, LaneIdOf<Runtime, MPI>>,
	relayers_state: UnrewardedRelayersState,
) -> Runtime::RuntimeCall
where
	Runtime: pallet_bridge_messages::Config<MPI, OutboundPayload = XcmAsPlainPayload>,
	MPI: 'static,
	Runtime::RuntimeCall: From<pallet_bridge_messages::Call<Runtime, MPI>>,
	BridgedChainOf<Runtime, MPI>: Chain<Hash = ParaHash> + Parachain,
{
	pallet_bridge_messages::Call::<Runtime, MPI>::receive_messages_delivery_proof {
		proof: message_delivery_proof,
		relayers_state,
	}
	.into()
}

/// Prepare storage proofs of messages, stored at the source chain.
pub fn make_complex_relayer_delivery_proofs<
	BridgedRelayChain,
	BridgedParachain,
	ThisChainWithMessages,
	LaneId,
>(
	lane_id: LaneId,
	xcm_message: Xcm<()>,
	message_nonce: MessageNonce,
	message_destination: Junctions,
	para_header_number: u32,
	relay_header_number: u32,
	bridged_para_id: u32,
	is_minimal_call: bool,
) -> (
	HeaderOf<BridgedRelayChain>,
	GrandpaJustification<HeaderOf<BridgedRelayChain>>,
	ParaHead,
	Vec<(ParaId, ParaHash)>,
	ParaHeadsProof,
	FromBridgedChainMessagesProof<ParaHash, LaneId>,
)
where
	BridgedRelayChain:
		bp_runtime::Chain<Hash = RelayBlockHash, BlockNumber = RelayBlockNumber> + ChainWithGrandpa,
	BridgedParachain: bp_runtime::Chain<Hash = ParaHash> + Parachain,
	ThisChainWithMessages: ChainWithMessages,
	LaneId: Copy + Encode,
{
	// prepare message
	let message_payload = prepare_inbound_xcm(xcm_message, message_destination);
	// prepare para storage proof containing message
	let (para_state_root, para_storage_proof) =
		prepare_messages_storage_proof::<BridgedParachain, ThisChainWithMessages, LaneId>(
			lane_id,
			message_nonce..=message_nonce,
			None,
			UnverifiedStorageProofParams::from_db_size(message_payload.len() as u32),
			|_| message_payload.clone(),
			encode_all_messages,
			encode_lane_data,
			false,
			false,
		);

	let (relay_chain_header, justification, bridged_para_head, parachain_heads, para_heads_proof) =
		make_complex_bridged_parachain_heads_proof::<BridgedRelayChain, BridgedParachain>(
			para_state_root,
			para_header_number,
			relay_header_number,
			bridged_para_id,
			is_minimal_call,
		);

	let message_proof = FromBridgedChainMessagesProof {
		bridged_header_hash: bridged_para_head.hash(),
		storage_proof: para_storage_proof,
		lane: lane_id,
		nonces_start: message_nonce,
		nonces_end: message_nonce,
	};

	(
		relay_chain_header,
		justification,
		bridged_para_head,
		parachain_heads,
		para_heads_proof,
		message_proof,
	)
}

/// Prepare storage proofs of message confirmations, stored at the target parachain.
pub fn make_complex_relayer_confirmation_proofs<
	BridgedRelayChain,
	BridgedParachain,
	ThisChainWithMessages,
	LaneId,
>(
	lane_id: LaneId,
	para_header_number: u32,
	relay_header_number: u32,
	bridged_para_id: u32,
	relayer_id_at_this_chain: AccountIdOf<ThisChainWithMessages>,
	relayers_state: UnrewardedRelayersState,
) -> (
	HeaderOf<BridgedRelayChain>,
	GrandpaJustification<HeaderOf<BridgedRelayChain>>,
	ParaHead,
	Vec<(ParaId, ParaHash)>,
	ParaHeadsProof,
	FromBridgedChainMessagesDeliveryProof<ParaHash, LaneId>,
)
where
	BridgedRelayChain:
		bp_runtime::Chain<Hash = RelayBlockHash, BlockNumber = RelayBlockNumber> + ChainWithGrandpa,
	BridgedParachain: bp_runtime::Chain<Hash = ParaHash> + Parachain,
	ThisChainWithMessages: ChainWithMessages,
	LaneId: Copy + Encode,
{
	// prepare para storage proof containing message delivery proof
	let (para_state_root, para_storage_proof) =
		prepare_message_delivery_storage_proof::<BridgedParachain, ThisChainWithMessages, LaneId>(
			lane_id,
			InboundLaneData {
				state: LaneState::Opened,
				relayers: vec![
					UnrewardedRelayer {
						relayer: relayer_id_at_this_chain.into(),
						messages: DeliveredMessages::new(1)
					};
					relayers_state.unrewarded_relayer_entries as usize
				]
				.into(),
				last_confirmed_nonce: 1,
			},
			UnverifiedStorageProofParams::default(),
		);

	let (relay_chain_header, justification, bridged_para_head, parachain_heads, para_heads_proof) =
		make_complex_bridged_parachain_heads_proof::<BridgedRelayChain, BridgedParachain>(
			para_state_root,
			para_header_number,
			relay_header_number,
			bridged_para_id,
			false,
		);

	let message_delivery_proof = FromBridgedChainMessagesDeliveryProof {
		bridged_header_hash: bridged_para_head.hash(),
		storage_proof: para_storage_proof,
		lane: lane_id,
	};

	(
		relay_chain_header,
		justification,
		bridged_para_head,
		parachain_heads,
		para_heads_proof,
		message_delivery_proof,
	)
}

/// Make bridged parachain header with given state root and relay header that is finalizing it.
pub fn make_complex_bridged_parachain_heads_proof<BridgedRelayChain, BridgedParachain>(
	para_state_root: ParaHash,
	para_header_number: u32,
	relay_header_number: BlockNumberOf<BridgedRelayChain>,
	bridged_para_id: u32,
	is_minimal_call: bool,
) -> (
	HeaderOf<BridgedRelayChain>,
	GrandpaJustification<HeaderOf<BridgedRelayChain>>,
	ParaHead,
	Vec<(ParaId, ParaHash)>,
	ParaHeadsProof,
)
where
	BridgedRelayChain:
		bp_runtime::Chain<Hash = RelayBlockHash, BlockNumber = RelayBlockNumber> + ChainWithGrandpa,
	BridgedParachain: bp_runtime::Chain<Hash = ParaHash> + Parachain,
{
	let bridged_para_head = ParaHead(
		bp_test_utils::test_header_with_root::<HeaderOf<BridgedParachain>>(
			para_header_number.into(),
			para_state_root,
		)
		.encode(),
	);
	let (relay_state_root, para_heads_proof, parachain_heads) =
		prepare_parachain_heads_proof::<HeaderOf<BridgedParachain>>(vec![(
			bridged_para_id,
			bridged_para_head.clone(),
		)]);
	assert_eq!(bridged_para_head.hash(), parachain_heads[0].1);

	let (relay_chain_header, justification) =
		make_complex_bridged_grandpa_header_proof::<BridgedRelayChain>(
			relay_state_root,
			relay_header_number,
			is_minimal_call,
		);

	(relay_chain_header, justification, bridged_para_head, parachain_heads, para_heads_proof)
}