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

//! The code that allows to use the pallet (`pallet-xcm-bridge-hub`) as inbound
//! bridge messages dispatcher. Internally, it just forwards inbound blob to the
//! XCM-level blob dispatcher, which pushes message to some other queue (e.g.
//! to HRMP queue with the sibling target chain).
//!
//! This code is executed at the target bridge hub.

use crate::{Config, Pallet, LOG_TARGET};

use bp_messages::target_chain::{DispatchMessage, MessageDispatch};
use bp_runtime::messages::MessageDispatchResult;
use bp_xcm_bridge_hub::{LocalXcmChannelManager, XcmAsPlainPayload};
use codec::{Decode, Encode};
use frame_support::{weights::Weight, CloneNoBound, EqNoBound, PartialEqNoBound};
use pallet_bridge_messages::{Config as BridgeMessagesConfig, WeightInfoExt};
use scale_info::TypeInfo;
use sp_runtime::SaturatedConversion;
use xcm::prelude::*;
use xcm_builder::{DispatchBlob, DispatchBlobError};

/// Message dispatch result type for single message.
#[derive(CloneNoBound, EqNoBound, PartialEqNoBound, Encode, Decode, Debug, TypeInfo)]
pub enum XcmBlobMessageDispatchResult {
	/// We've been unable to decode message payload.
	InvalidPayload,
	/// Message has been dispatched.
	Dispatched,
	/// Message has **NOT** been dispatched because of given error.
	NotDispatched(#[codec(skip)] Option<DispatchBlobError>),
}

/// An easy way to access associated messages pallet weights.
type MessagesPalletWeights<T, I> =
	<T as BridgeMessagesConfig<<T as Config<I>>::BridgeMessagesPalletInstance>>::WeightInfo;

impl<T: Config<I>, I: 'static> MessageDispatch for Pallet<T, I>
where
	T: BridgeMessagesConfig<T::BridgeMessagesPalletInstance, InboundPayload = XcmAsPlainPayload>,
{
	type DispatchPayload = XcmAsPlainPayload;
	type DispatchLevelResult = XcmBlobMessageDispatchResult;
	type LaneId = T::LaneId;

	fn is_active(lane: Self::LaneId) -> bool {
		Pallet::<T, I>::bridge_by_lane_id(&lane)
			.and_then(|(_, bridge)| bridge.bridge_origin_relative_location.try_as().cloned().ok())
			.map(|recipient: Location| !T::LocalXcmChannelManager::is_congested(&recipient))
			.unwrap_or(false)
	}

	fn dispatch_weight(
		message: &mut DispatchMessage<Self::DispatchPayload, Self::LaneId>,
	) -> Weight {
		match message.data.payload {
			Ok(ref payload) => {
				let payload_size = payload.encoded_size().saturated_into();
				MessagesPalletWeights::<T, I>::message_dispatch_weight(payload_size)
			},
			Err(_) => Weight::zero(),
		}
	}

	fn dispatch(
		message: DispatchMessage<Self::DispatchPayload, Self::LaneId>,
	) -> MessageDispatchResult<Self::DispatchLevelResult> {
		let payload = match message.data.payload {
			Ok(payload) => payload,
			Err(e) => {
				log::error!(
					target: LOG_TARGET,
					"dispatch - payload error: {e:?} for lane_id: {:?} and message_nonce: {:?}",
					message.key.lane_id,
					message.key.nonce
				);
				return MessageDispatchResult {
					unspent_weight: Weight::zero(),
					dispatch_level_result: XcmBlobMessageDispatchResult::InvalidPayload,
				}
			},
		};
		let dispatch_level_result = match T::BlobDispatcher::dispatch_blob(payload) {
			Ok(_) => {
				log::debug!(
					target: LOG_TARGET,
					"dispatch - `DispatchBlob::dispatch_blob` was ok for lane_id: {:?} and message_nonce: {:?}",
					message.key.lane_id,
					message.key.nonce
				);
				XcmBlobMessageDispatchResult::Dispatched
			},
			Err(e) => {
				log::error!(
					target: LOG_TARGET,
					"dispatch - `DispatchBlob::dispatch_blob` failed with error: {e:?} for lane_id: {:?} and message_nonce: {:?}",
					message.key.lane_id,
					message.key.nonce
				);
				XcmBlobMessageDispatchResult::NotDispatched(Some(e))
			},
		};
		MessageDispatchResult { unspent_weight: Weight::zero(), dispatch_level_result }
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{mock::*, Bridges, LaneToBridge, LanesManagerOf};

	use bp_messages::{target_chain::DispatchMessageData, LaneIdType, MessageKey};
	use bp_xcm_bridge_hub::{Bridge, BridgeLocations, BridgeState};
	use frame_support::assert_ok;
	use pallet_bridge_messages::InboundLaneStorage;
	use xcm_executor::traits::ConvertLocation;

	fn bridge() -> (Box<BridgeLocations>, TestLaneIdType) {
		let origin = OpenBridgeOrigin::sibling_parachain_origin();
		let with = bridged_asset_hub_universal_location();
		let locations =
			XcmOverBridge::bridge_locations_from_origin(origin, Box::new(with.into())).unwrap();
		let lane_id = locations.calculate_lane_id(xcm::latest::VERSION).unwrap();
		(locations, lane_id)
	}

	fn run_test_with_opened_bridge(test: impl FnOnce()) {
		run_test(|| {
			let (bridge, lane_id) = bridge();

			if !Bridges::<TestRuntime, ()>::contains_key(bridge.bridge_id()) {
				// insert bridge
				Bridges::<TestRuntime, ()>::insert(
					bridge.bridge_id(),
					Bridge {
						bridge_origin_relative_location: Box::new(
							bridge.bridge_origin_relative_location().clone().into(),
						),
						bridge_origin_universal_location: Box::new(
							bridge.bridge_origin_universal_location().clone().into(),
						),
						bridge_destination_universal_location: Box::new(
							bridge.bridge_destination_universal_location().clone().into(),
						),
						state: BridgeState::Opened,
						bridge_owner_account: LocationToAccountId::convert_location(
							bridge.bridge_origin_relative_location(),
						)
						.expect("valid accountId"),
						deposit: 0,
						lane_id,
					},
				);
				LaneToBridge::<TestRuntime, ()>::insert(lane_id, bridge.bridge_id());

				// create lanes
				let lanes_manager = LanesManagerOf::<TestRuntime, ()>::new();
				if lanes_manager.create_inbound_lane(lane_id).is_ok() {
					assert_eq!(
						0,
						lanes_manager
							.active_inbound_lane(lane_id)
							.unwrap()
							.storage()
							.data()
							.last_confirmed_nonce
					);
				}
				if lanes_manager.create_outbound_lane(lane_id).is_ok() {
					assert!(lanes_manager
						.active_outbound_lane(lane_id)
						.unwrap()
						.queued_messages()
						.is_empty());
				}
			}
			assert_ok!(XcmOverBridge::do_try_state());

			test();
		});
	}

	fn invalid_message() -> DispatchMessage<Vec<u8>, TestLaneIdType> {
		DispatchMessage {
			key: MessageKey { lane_id: TestLaneIdType::try_new(1, 2).unwrap(), nonce: 1 },
			data: DispatchMessageData { payload: Err(codec::Error::from("test")) },
		}
	}

	fn valid_message() -> DispatchMessage<Vec<u8>, TestLaneIdType> {
		DispatchMessage {
			key: MessageKey { lane_id: TestLaneIdType::try_new(1, 2).unwrap(), nonce: 1 },
			data: DispatchMessageData { payload: Ok(vec![42]) },
		}
	}

	#[test]
	fn dispatcher_is_inactive_when_channel_with_target_chain_is_congested() {
		run_test_with_opened_bridge(|| {
			TestLocalXcmChannelManager::make_congested();
			assert!(!XcmOverBridge::is_active(bridge().1));
		});
	}

	#[test]
	fn dispatcher_is_active_when_channel_with_target_chain_is_not_congested() {
		run_test_with_opened_bridge(|| {
			assert!(XcmOverBridge::is_active(bridge().1));
		});
	}

	#[test]
	fn dispatch_weight_is_zero_if_we_have_failed_to_decode_message() {
		run_test(|| {
			assert_eq!(XcmOverBridge::dispatch_weight(&mut invalid_message()), Weight::zero());
		});
	}

	#[test]
	fn dispatch_weight_is_non_zero_if_we_have_decoded_message() {
		run_test(|| {
			assert_ne!(XcmOverBridge::dispatch_weight(&mut valid_message()), Weight::zero());
		});
	}

	#[test]
	fn message_is_not_dispatched_when_we_have_failed_to_decode_message() {
		run_test(|| {
			assert_eq!(
				XcmOverBridge::dispatch(invalid_message()),
				MessageDispatchResult {
					unspent_weight: Weight::zero(),
					dispatch_level_result: XcmBlobMessageDispatchResult::InvalidPayload,
				},
			);
			assert!(!TestBlobDispatcher::is_dispatched());
		});
	}

	#[test]
	fn message_is_dispatched_when_we_have_decoded_message() {
		run_test(|| {
			assert_eq!(
				XcmOverBridge::dispatch(valid_message()),
				MessageDispatchResult {
					unspent_weight: Weight::zero(),
					dispatch_level_result: XcmBlobMessageDispatchResult::Dispatched,
				},
			);
			assert!(TestBlobDispatcher::is_dispatched());
		});
	}
}