referrerpolicy=no-referrer-when-downgrade

bridge_hub_test_utils/test_data/
mod.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17//! Generating test data, used by all tests.
18
19pub mod from_grandpa_chain;
20pub mod from_parachain;
21
22use bp_messages::{
23	target_chain::{DispatchMessage, DispatchMessageData},
24	MessageKey,
25};
26use codec::Encode;
27use frame_support::traits::Get;
28use pallet_bridge_grandpa::BridgedHeader;
29use xcm::latest::prelude::*;
30
31use bp_messages::MessageNonce;
32use bp_runtime::BasicOperatingMode;
33use bp_test_utils::authority_list;
34use xcm::GetVersion;
35use xcm_builder::{BridgeMessage, HaulBlob, HaulBlobError, HaulBlobExporter};
36use xcm_executor::traits::{validate_export, ExportXcm};
37
38pub(crate) type XcmAsPlainPayload = sp_std::vec::Vec<u8>;
39
40pub fn prepare_inbound_xcm(xcm_message: Xcm<()>, destination: InteriorLocation) -> Vec<u8> {
41	let location = xcm::VersionedInteriorLocation::from(destination);
42	let xcm = xcm::VersionedXcm::<()>::from(xcm_message);
43
44	// (double encoding, because `.encode()` is called on original Xcm BLOB when it is pushed to the
45	// storage)
46	BridgeMessage { universal_dest: location, message: xcm }.encode().encode()
47}
48
49/// Helper that creates InitializationData mock data, that can be used to initialize bridge
50/// GRANDPA pallet
51pub fn initialization_data<
52	Runtime: pallet_bridge_grandpa::Config<GrandpaPalletInstance>,
53	GrandpaPalletInstance: 'static,
54>(
55	block_number: u32,
56) -> bp_header_chain::InitializationData<BridgedHeader<Runtime, GrandpaPalletInstance>> {
57	bp_header_chain::InitializationData {
58		header: Box::new(bp_test_utils::test_header(block_number.into())),
59		authority_list: authority_list(),
60		set_id: 1,
61		operating_mode: BasicOperatingMode::Normal,
62	}
63}
64
65/// Dummy xcm
66pub(crate) fn dummy_xcm() -> Xcm<()> {
67	vec![Trap(42)].into()
68}
69
70pub(crate) fn dispatch_message<LaneId: Encode>(
71	lane_id: LaneId,
72	nonce: MessageNonce,
73	payload: Vec<u8>,
74) -> DispatchMessage<Vec<u8>, LaneId> {
75	DispatchMessage {
76		key: MessageKey { lane_id, nonce },
77		data: DispatchMessageData { payload: Ok(payload) },
78	}
79}
80
81/// Macro used for simulate_export_message and capturing bytes
82macro_rules! grab_haul_blob (
83	($name:ident, $grabbed_payload:ident) => {
84		std::thread_local! {
85			static $grabbed_payload: std::cell::RefCell<Option<Vec<u8>>> = std::cell::RefCell::new(None);
86		}
87
88		struct $name;
89		impl HaulBlob for $name {
90			fn haul_blob(blob: Vec<u8>) -> Result<(), HaulBlobError>{
91				$grabbed_payload.with(|rm| *rm.borrow_mut() = Some(blob));
92				Ok(())
93			}
94		}
95	}
96);
97
98/// Simulates `HaulBlobExporter` and all its wrapping and captures generated plain bytes,
99/// which are transferred over bridge.
100pub(crate) fn simulate_message_exporter_on_bridged_chain<
101	SourceNetwork: Get<NetworkId>,
102	DestinationNetwork: Get<Location>,
103	DestinationVersion: GetVersion,
104>(
105	(destination_network, destination_junctions): (NetworkId, Junctions),
106) -> Vec<u8> {
107	grab_haul_blob!(GrabbingHaulBlob, GRABBED_HAUL_BLOB_PAYLOAD);
108
109	// lets pretend that some parachain on bridged chain exported the message
110	let universal_source_on_bridged_chain: Junctions =
111		[GlobalConsensus(SourceNetwork::get()), Parachain(5678)].into();
112	let channel = 1_u32;
113
114	// simulate XCM message export
115	let (ticket, fee) = validate_export::<
116		HaulBlobExporter<GrabbingHaulBlob, DestinationNetwork, DestinationVersion, ()>,
117	>(
118		destination_network,
119		channel,
120		universal_source_on_bridged_chain,
121		destination_junctions,
122		dummy_xcm(),
123	)
124	.expect("validate_export to pass");
125	tracing::info!(
126		target: "simulate_message_exporter_on_bridged_chain",
127		?fee,
128		"HaulBlobExporter::validate"
129	);
130	let xcm_hash =
131		HaulBlobExporter::<GrabbingHaulBlob, DestinationNetwork, DestinationVersion, ()>::deliver(
132			ticket,
133		)
134		.expect("deliver to pass");
135	tracing::info!(
136		target: "simulate_message_exporter_on_bridged_chain",
137		?xcm_hash,
138		"HaulBlobExporter::deliver"
139	);
140
141	GRABBED_HAUL_BLOB_PAYLOAD.with(|r| r.take().expect("Encoded message should be here"))
142}