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
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.

// Parity Bridges Common 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.

// Parity Bridges Common 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 Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.

//! Primitives of the xcm-bridge-hub pallet.

#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]

use bp_messages::LaneIdType;
use bp_runtime::{AccountIdOf, BalanceOf, Chain};
pub use call_info::XcmBridgeHubCall;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
	ensure, sp_runtime::RuntimeDebug, CloneNoBound, PalletError, PartialEqNoBound,
	RuntimeDebugNoBound,
};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_core::H256;
use sp_io::hashing::blake2_256;
use sp_std::boxed::Box;
use xcm::{
	latest::prelude::*, prelude::XcmVersion, IntoVersion, VersionedInteriorLocation,
	VersionedLocation,
};

mod call_info;

/// Encoded XCM blob. We expect the bridge messages pallet to use this blob type for both inbound
/// and outbound payloads.
pub type XcmAsPlainPayload = sp_std::vec::Vec<u8>;

/// Bridge identifier - used **only** for communicating with sibling/parent chains in the same
/// consensus.
///
/// For example, `SendXcm` implementations (which use the `latest` XCM) can use it to identify a
/// bridge and the corresponding `LaneId` that is used for over-consensus communication between
/// bridge hubs.
///
/// This identifier is constructed from the `latest` XCM, so it is expected to ensure migration to
/// the `latest` XCM version. This could change the `BridgeId`, but it will not affect the `LaneId`.
/// In other words, `LaneId` will never change, while `BridgeId` could change with (every) XCM
/// upgrade.
#[derive(
	Clone,
	Copy,
	Decode,
	Encode,
	Eq,
	Ord,
	PartialOrd,
	PartialEq,
	TypeInfo,
	MaxEncodedLen,
	Serialize,
	Deserialize,
)]
pub struct BridgeId(H256);

impl BridgeId {
	/// Create bridge identifier from two universal locations.
	///
	/// Note: The `BridgeId` is constructed from `latest` XCM, so if stored, you need to ensure
	/// compatibility with newer XCM versions.
	pub fn new(
		universal_source: &InteriorLocation,
		universal_destination: &InteriorLocation,
	) -> Self {
		const VALUES_SEPARATOR: [u8; 33] = *b"bridges-bridge-id-value-separator";

		BridgeId(
			(universal_source, VALUES_SEPARATOR, universal_destination)
				.using_encoded(blake2_256)
				.into(),
		)
	}

	/// Access the inner representation.
	pub fn inner(&self) -> H256 {
		self.0
	}
}

impl core::fmt::Debug for BridgeId {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		core::fmt::Debug::fmt(&self.0, f)
	}
}

/// Local XCM channel manager.
pub trait LocalXcmChannelManager {
	/// Error that may be returned when suspending/resuming the bridge.
	type Error: sp_std::fmt::Debug;

	/// Returns true if the channel with given location is currently congested.
	///
	/// The `with` is guaranteed to be in the same consensus. However, it may point to something
	/// below the chain level - like the contract or pallet instance, for example.
	fn is_congested(with: &Location) -> bool;

	/// Suspend the bridge, opened by given origin.
	///
	/// The `local_origin` is guaranteed to be in the same consensus. However, it may point to
	/// something below the chain level - like the contract or pallet instance, for example.
	fn suspend_bridge(local_origin: &Location, bridge: BridgeId) -> Result<(), Self::Error>;

	/// Resume the previously suspended bridge, opened by given origin.
	///
	/// The `local_origin` is guaranteed to be in the same consensus. However, it may point to
	/// something below the chain level - like the contract or pallet instance, for example.
	fn resume_bridge(local_origin: &Location, bridge: BridgeId) -> Result<(), Self::Error>;
}

impl LocalXcmChannelManager for () {
	type Error = ();

	fn is_congested(_with: &Location) -> bool {
		false
	}

	fn suspend_bridge(_local_origin: &Location, _bridge: BridgeId) -> Result<(), Self::Error> {
		Ok(())
	}

	fn resume_bridge(_local_origin: &Location, _bridge: BridgeId) -> Result<(), Self::Error> {
		Ok(())
	}
}

/// Bridge state.
#[derive(Clone, Copy, Decode, Encode, Eq, PartialEq, TypeInfo, MaxEncodedLen, RuntimeDebug)]
pub enum BridgeState {
	/// Bridge is opened. Associated lanes are also opened.
	Opened,
	/// Bridge is suspended. Associated lanes are opened.
	///
	/// We keep accepting messages to the bridge. The only difference with the `Opened` state
	/// is that we have sent the "Suspended" message/signal to the local bridge origin.
	Suspended,
	/// Bridge is closed. Associated lanes are also closed.
	/// After all outbound messages will be pruned, the bridge will vanish without any traces.
	Closed,
}

/// Bridge metadata.
#[derive(
	CloneNoBound, Decode, Encode, Eq, PartialEqNoBound, TypeInfo, MaxEncodedLen, RuntimeDebugNoBound,
)]
#[scale_info(skip_type_params(ThisChain, LaneId))]
pub struct Bridge<ThisChain: Chain, LaneId: LaneIdType> {
	/// Relative location of the bridge origin chain. This is expected to be **convertible** to the
	/// `latest` XCM, so the check and migration needs to be ensured.
	pub bridge_origin_relative_location: Box<VersionedLocation>,

	/// See [`BridgeLocations::bridge_origin_universal_location`].
	/// Stored for `BridgeId` sanity check.
	pub bridge_origin_universal_location: Box<VersionedInteriorLocation>,
	/// See [`BridgeLocations::bridge_destination_universal_location`].
	/// Stored for `BridgeId` sanity check.
	pub bridge_destination_universal_location: Box<VersionedInteriorLocation>,

	/// Current bridge state.
	pub state: BridgeState,
	/// Account with the reserved funds. Derived from `self.bridge_origin_relative_location`.
	pub bridge_owner_account: AccountIdOf<ThisChain>,
	/// Reserved amount on the sovereign account of the sibling bridge origin.
	pub deposit: BalanceOf<ThisChain>,

	/// Mapping to the unique `LaneId`.
	pub lane_id: LaneId,
}

/// Locations of bridge endpoints at both sides of the bridge.
#[derive(Clone, RuntimeDebug, PartialEq, Eq)]
pub struct BridgeLocations {
	/// Relative (to this bridge hub) location of this side of the bridge.
	bridge_origin_relative_location: Location,
	/// Universal (unique) location of this side of the bridge.
	bridge_origin_universal_location: InteriorLocation,
	/// Universal (unique) location of other side of the bridge.
	bridge_destination_universal_location: InteriorLocation,
	/// An identifier of the dedicated bridge message lane.
	bridge_id: BridgeId,
}

/// Errors that may happen when we check bridge locations.
#[derive(Encode, Decode, RuntimeDebug, PartialEq, Eq, PalletError, TypeInfo)]
pub enum BridgeLocationsError {
	/// Origin or destination locations are not universal.
	NonUniversalLocation,
	/// Bridge origin location is not supported.
	InvalidBridgeOrigin,
	/// Bridge destination is not supported (in general).
	InvalidBridgeDestination,
	/// Destination location is within the same global consensus.
	DestinationIsLocal,
	/// Destination network is not the network we are bridged with.
	UnreachableDestination,
	/// Destination location is unsupported. We only support bridges with relay
	/// chain or its parachains.
	UnsupportedDestinationLocation,
	/// The version of XCM location argument is unsupported.
	UnsupportedXcmVersion,
	/// The `LaneIdType` generator is not supported.
	UnsupportedLaneIdType,
}

impl BridgeLocations {
	/// Given XCM locations, generate lane id and universal locations of bridge endpoints.
	///
	/// The `here_universal_location` is the universal location of the bridge hub runtime.
	///
	/// The `bridge_origin_relative_location` is the relative (to the `here_universal_location`)
	/// location of the bridge endpoint at this side of the bridge. It may be the parent relay
	/// chain or the sibling parachain. All junctions below parachain level are dropped.
	///
	/// The `bridge_destination_universal_location` is the universal location of the bridge
	/// destination. It may be the parent relay or the sibling parachain of the **bridged**
	/// bridge hub. All junctions below parachain level are dropped.
	///
	/// Why we drop all junctions between parachain level - that's because the lane is a bridge
	/// between two chains. All routing under this level happens when the message is delivered
	/// to the bridge destination. So at bridge level we don't care about low level junctions.
	///
	/// Returns error if `bridge_origin_relative_location` is outside of `here_universal_location`
	/// local consensus OR if `bridge_destination_universal_location` is not a universal location.
	pub fn bridge_locations(
		here_universal_location: InteriorLocation,
		bridge_origin_relative_location: Location,
		bridge_destination_universal_location: InteriorLocation,
		expected_remote_network: NetworkId,
	) -> Result<Box<Self>, BridgeLocationsError> {
		fn strip_low_level_junctions(
			location: InteriorLocation,
		) -> Result<InteriorLocation, BridgeLocationsError> {
			let mut junctions = location.into_iter();

			let global_consensus = junctions
				.next()
				.filter(|junction| matches!(junction, GlobalConsensus(_)))
				.ok_or(BridgeLocationsError::NonUniversalLocation)?;

			// we only expect `Parachain` junction here. There are other junctions that
			// may need to be supported (like `GeneralKey` and `OnlyChild`), but now we
			// only support bridges with relay and parachans
			//
			// if there's something other than parachain, let's strip it
			let maybe_parachain =
				junctions.next().filter(|junction| matches!(junction, Parachain(_)));
			Ok(match maybe_parachain {
				Some(parachain) => [global_consensus, parachain].into(),
				None => [global_consensus].into(),
			})
		}

		// ensure that the `here_universal_location` and `bridge_destination_universal_location`
		// are universal locations within different consensus systems
		let local_network = here_universal_location
			.global_consensus()
			.map_err(|_| BridgeLocationsError::NonUniversalLocation)?;
		let remote_network = bridge_destination_universal_location
			.global_consensus()
			.map_err(|_| BridgeLocationsError::NonUniversalLocation)?;
		ensure!(local_network != remote_network, BridgeLocationsError::DestinationIsLocal);
		ensure!(
			remote_network == expected_remote_network,
			BridgeLocationsError::UnreachableDestination
		);

		// get universal location of endpoint, located at this side of the bridge
		let bridge_origin_universal_location = here_universal_location
			.within_global(bridge_origin_relative_location.clone())
			.map_err(|_| BridgeLocationsError::InvalidBridgeOrigin)?;
		// strip low-level junctions within universal locations
		let bridge_origin_universal_location =
			strip_low_level_junctions(bridge_origin_universal_location)?;
		let bridge_destination_universal_location =
			strip_low_level_junctions(bridge_destination_universal_location)?;

		// we know that the `bridge_destination_universal_location` starts from the
		// `GlobalConsensus` and we know that the `bridge_origin_universal_location`
		// is also within the `GlobalConsensus`. So we know that the lane id will be
		// the same on both ends of the bridge
		let bridge_id = BridgeId::new(
			&bridge_origin_universal_location,
			&bridge_destination_universal_location,
		);

		Ok(Box::new(BridgeLocations {
			bridge_origin_relative_location,
			bridge_origin_universal_location,
			bridge_destination_universal_location,
			bridge_id,
		}))
	}

	/// Getter for `bridge_origin_relative_location`
	pub fn bridge_origin_relative_location(&self) -> &Location {
		&self.bridge_origin_relative_location
	}

	/// Getter for `bridge_origin_universal_location`
	pub fn bridge_origin_universal_location(&self) -> &InteriorLocation {
		&self.bridge_origin_universal_location
	}

	/// Getter for `bridge_destination_universal_location`
	pub fn bridge_destination_universal_location(&self) -> &InteriorLocation {
		&self.bridge_destination_universal_location
	}

	/// Getter for `bridge_id`
	pub fn bridge_id(&self) -> &BridgeId {
		&self.bridge_id
	}

	/// Generates the exact same `LaneId` on the both bridge hubs.
	///
	/// Note: Use this **only** when opening a new bridge.
	pub fn calculate_lane_id<LaneId: LaneIdType>(
		&self,
		xcm_version: XcmVersion,
	) -> Result<LaneId, BridgeLocationsError> {
		// a tricky helper struct that adds required `Ord` support for
		// `VersionedInteriorLocation`
		#[derive(Eq, PartialEq, Ord, PartialOrd)]
		struct EncodedVersionedInteriorLocation(sp_std::vec::Vec<u8>);
		impl Encode for EncodedVersionedInteriorLocation {
			fn encode(&self) -> sp_std::vec::Vec<u8> {
				self.0.clone()
			}
		}

		let universal_location1 =
			VersionedInteriorLocation::from(self.bridge_origin_universal_location.clone())
				.into_version(xcm_version)
				.map_err(|_| BridgeLocationsError::UnsupportedXcmVersion);
		let universal_location2 =
			VersionedInteriorLocation::from(self.bridge_destination_universal_location.clone())
				.into_version(xcm_version)
				.map_err(|_| BridgeLocationsError::UnsupportedXcmVersion);

		LaneId::try_new(
			EncodedVersionedInteriorLocation(universal_location1.encode()),
			EncodedVersionedInteriorLocation(universal_location2.encode()),
		)
		.map_err(|_| BridgeLocationsError::UnsupportedLaneIdType)
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use xcm::latest::ROCOCO_GENESIS_HASH;

	const LOCAL_NETWORK: NetworkId = Kusama;
	const REMOTE_NETWORK: NetworkId = Polkadot;
	const UNREACHABLE_NETWORK: NetworkId = NetworkId::ByGenesis(ROCOCO_GENESIS_HASH);
	const SIBLING_PARACHAIN: u32 = 1000;
	const LOCAL_BRIDGE_HUB: u32 = 1001;
	const REMOTE_PARACHAIN: u32 = 2000;

	struct SuccessfulTest {
		here_universal_location: InteriorLocation,
		bridge_origin_relative_location: Location,

		bridge_origin_universal_location: InteriorLocation,
		bridge_destination_universal_location: InteriorLocation,

		expected_remote_network: NetworkId,
	}

	fn run_successful_test(test: SuccessfulTest) -> BridgeLocations {
		let locations = BridgeLocations::bridge_locations(
			test.here_universal_location,
			test.bridge_origin_relative_location.clone(),
			test.bridge_destination_universal_location.clone(),
			test.expected_remote_network,
		);
		assert_eq!(
			locations,
			Ok(Box::new(BridgeLocations {
				bridge_origin_relative_location: test.bridge_origin_relative_location,
				bridge_origin_universal_location: test.bridge_origin_universal_location.clone(),
				bridge_destination_universal_location: test
					.bridge_destination_universal_location
					.clone(),
				bridge_id: BridgeId::new(
					&test.bridge_origin_universal_location,
					&test.bridge_destination_universal_location,
				),
			})),
		);

		*locations.unwrap()
	}

	// successful tests that with various origins and destinations

	#[test]
	fn at_relay_from_local_relay_to_remote_relay_works() {
		run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_origin_relative_location: Here.into(),

			bridge_origin_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_destination_universal_location: [GlobalConsensus(REMOTE_NETWORK)].into(),

			expected_remote_network: REMOTE_NETWORK,
		});
	}

	#[test]
	fn at_relay_from_sibling_parachain_to_remote_relay_works() {
		run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_origin_relative_location: [Parachain(SIBLING_PARACHAIN)].into(),

			bridge_origin_universal_location: [
				GlobalConsensus(LOCAL_NETWORK),
				Parachain(SIBLING_PARACHAIN),
			]
			.into(),
			bridge_destination_universal_location: [GlobalConsensus(REMOTE_NETWORK)].into(),

			expected_remote_network: REMOTE_NETWORK,
		});
	}

	#[test]
	fn at_relay_from_local_relay_to_remote_parachain_works() {
		run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_origin_relative_location: Here.into(),

			bridge_origin_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_destination_universal_location: [
				GlobalConsensus(REMOTE_NETWORK),
				Parachain(REMOTE_PARACHAIN),
			]
			.into(),

			expected_remote_network: REMOTE_NETWORK,
		});
	}

	#[test]
	fn at_relay_from_sibling_parachain_to_remote_parachain_works() {
		run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_origin_relative_location: [Parachain(SIBLING_PARACHAIN)].into(),

			bridge_origin_universal_location: [
				GlobalConsensus(LOCAL_NETWORK),
				Parachain(SIBLING_PARACHAIN),
			]
			.into(),
			bridge_destination_universal_location: [
				GlobalConsensus(REMOTE_NETWORK),
				Parachain(REMOTE_PARACHAIN),
			]
			.into(),

			expected_remote_network: REMOTE_NETWORK,
		});
	}

	#[test]
	fn at_bridge_hub_from_local_relay_to_remote_relay_works() {
		run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK), Parachain(LOCAL_BRIDGE_HUB)]
				.into(),
			bridge_origin_relative_location: Parent.into(),

			bridge_origin_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_destination_universal_location: [GlobalConsensus(REMOTE_NETWORK)].into(),

			expected_remote_network: REMOTE_NETWORK,
		});
	}

	#[test]
	fn at_bridge_hub_from_sibling_parachain_to_remote_relay_works() {
		run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK), Parachain(LOCAL_BRIDGE_HUB)]
				.into(),
			bridge_origin_relative_location: ParentThen([Parachain(SIBLING_PARACHAIN)].into())
				.into(),

			bridge_origin_universal_location: [
				GlobalConsensus(LOCAL_NETWORK),
				Parachain(SIBLING_PARACHAIN),
			]
			.into(),
			bridge_destination_universal_location: [GlobalConsensus(REMOTE_NETWORK)].into(),

			expected_remote_network: REMOTE_NETWORK,
		});
	}

	#[test]
	fn at_bridge_hub_from_local_relay_to_remote_parachain_works() {
		run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK), Parachain(LOCAL_BRIDGE_HUB)]
				.into(),
			bridge_origin_relative_location: Parent.into(),

			bridge_origin_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_destination_universal_location: [
				GlobalConsensus(REMOTE_NETWORK),
				Parachain(REMOTE_PARACHAIN),
			]
			.into(),

			expected_remote_network: REMOTE_NETWORK,
		});
	}

	#[test]
	fn at_bridge_hub_from_sibling_parachain_to_remote_parachain_works() {
		run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK), Parachain(LOCAL_BRIDGE_HUB)]
				.into(),
			bridge_origin_relative_location: ParentThen([Parachain(SIBLING_PARACHAIN)].into())
				.into(),

			bridge_origin_universal_location: [
				GlobalConsensus(LOCAL_NETWORK),
				Parachain(SIBLING_PARACHAIN),
			]
			.into(),
			bridge_destination_universal_location: [
				GlobalConsensus(REMOTE_NETWORK),
				Parachain(REMOTE_PARACHAIN),
			]
			.into(),

			expected_remote_network: REMOTE_NETWORK,
		});
	}

	// successful tests that show that we are ignoring low-level junctions of bridge origins

	#[test]
	fn low_level_junctions_at_bridge_origin_are_stripped() {
		let locations1 = run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_origin_relative_location: Here.into(),

			bridge_origin_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_destination_universal_location: [GlobalConsensus(REMOTE_NETWORK)].into(),

			expected_remote_network: REMOTE_NETWORK,
		});
		let locations2 = run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_origin_relative_location: [PalletInstance(0)].into(),

			bridge_origin_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_destination_universal_location: [GlobalConsensus(REMOTE_NETWORK)].into(),

			expected_remote_network: REMOTE_NETWORK,
		});

		assert_eq!(locations1.bridge_id, locations2.bridge_id);
	}

	#[test]
	fn low_level_junctions_at_bridge_destination_are_stripped() {
		let locations1 = run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_origin_relative_location: Here.into(),

			bridge_origin_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_destination_universal_location: [GlobalConsensus(REMOTE_NETWORK)].into(),

			expected_remote_network: REMOTE_NETWORK,
		});
		let locations2 = run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_origin_relative_location: Here.into(),

			bridge_origin_universal_location: [GlobalConsensus(LOCAL_NETWORK)].into(),
			bridge_destination_universal_location: [GlobalConsensus(REMOTE_NETWORK)].into(),

			expected_remote_network: REMOTE_NETWORK,
		});

		assert_eq!(locations1.bridge_id, locations2.bridge_id);
	}

	#[test]
	fn calculate_lane_id_works() {
		type TestLaneId = bp_messages::HashedLaneId;

		let from_local_to_remote = run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(LOCAL_NETWORK), Parachain(LOCAL_BRIDGE_HUB)]
				.into(),
			bridge_origin_relative_location: ParentThen([Parachain(SIBLING_PARACHAIN)].into())
				.into(),

			bridge_origin_universal_location: [
				GlobalConsensus(LOCAL_NETWORK),
				Parachain(SIBLING_PARACHAIN),
			]
			.into(),
			bridge_destination_universal_location: [
				GlobalConsensus(REMOTE_NETWORK),
				Parachain(REMOTE_PARACHAIN),
			]
			.into(),

			expected_remote_network: REMOTE_NETWORK,
		});

		let from_remote_to_local = run_successful_test(SuccessfulTest {
			here_universal_location: [GlobalConsensus(REMOTE_NETWORK), Parachain(LOCAL_BRIDGE_HUB)]
				.into(),
			bridge_origin_relative_location: ParentThen([Parachain(REMOTE_PARACHAIN)].into())
				.into(),

			bridge_origin_universal_location: [
				GlobalConsensus(REMOTE_NETWORK),
				Parachain(REMOTE_PARACHAIN),
			]
			.into(),
			bridge_destination_universal_location: [
				GlobalConsensus(LOCAL_NETWORK),
				Parachain(SIBLING_PARACHAIN),
			]
			.into(),

			expected_remote_network: LOCAL_NETWORK,
		});

		assert_ne!(
			from_local_to_remote.calculate_lane_id::<TestLaneId>(xcm::latest::VERSION),
			from_remote_to_local.calculate_lane_id::<TestLaneId>(xcm::latest::VERSION - 1),
		);
		assert_eq!(
			from_local_to_remote.calculate_lane_id::<TestLaneId>(xcm::latest::VERSION),
			from_remote_to_local.calculate_lane_id::<TestLaneId>(xcm::latest::VERSION),
		);
	}

	// negative tests

	#[test]
	fn bridge_locations_fails_when_here_is_not_universal_location() {
		assert_eq!(
			BridgeLocations::bridge_locations(
				[Parachain(1000)].into(),
				Here.into(),
				[GlobalConsensus(REMOTE_NETWORK)].into(),
				REMOTE_NETWORK,
			),
			Err(BridgeLocationsError::NonUniversalLocation),
		);
	}

	#[test]
	fn bridge_locations_fails_when_computed_destination_is_not_universal_location() {
		assert_eq!(
			BridgeLocations::bridge_locations(
				[GlobalConsensus(LOCAL_NETWORK)].into(),
				Here.into(),
				[OnlyChild].into(),
				REMOTE_NETWORK,
			),
			Err(BridgeLocationsError::NonUniversalLocation),
		);
	}

	#[test]
	fn bridge_locations_fails_when_computed_destination_is_local() {
		assert_eq!(
			BridgeLocations::bridge_locations(
				[GlobalConsensus(LOCAL_NETWORK)].into(),
				Here.into(),
				[GlobalConsensus(LOCAL_NETWORK), OnlyChild].into(),
				REMOTE_NETWORK,
			),
			Err(BridgeLocationsError::DestinationIsLocal),
		);
	}

	#[test]
	fn bridge_locations_fails_when_computed_destination_is_unreachable() {
		assert_eq!(
			BridgeLocations::bridge_locations(
				[GlobalConsensus(LOCAL_NETWORK)].into(),
				Here.into(),
				[GlobalConsensus(UNREACHABLE_NETWORK)].into(),
				REMOTE_NETWORK,
			),
			Err(BridgeLocationsError::UnreachableDestination),
		);
	}
}