cumulus_pallet_xcmp_queue/bridging.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::{pallet, OutboundState};
17use cumulus_primitives_core::ParaId;
18use xcm::latest::prelude::*;
19
20/// Adapter implementation for `bp_xcm_bridge_hub_router::XcmChannelStatusProvider` which checks
21/// both `OutboundXcmpStatus` and `InboundXcmpStatus` for defined `Location` if any of those is
22/// suspended.
23pub struct InAndOutXcmpChannelStatusProvider<Runtime>(core::marker::PhantomData<Runtime>);
24impl<Runtime: crate::Config> bp_xcm_bridge_hub_router::XcmChannelStatusProvider
25 for InAndOutXcmpChannelStatusProvider<Runtime>
26{
27 fn is_congested(with: &Location) -> bool {
28 // handle congestion only for a sibling parachain locations.
29 let sibling_para_id: ParaId = match with.unpack() {
30 (_, [Parachain(para_id)]) => (*para_id).into(),
31 _ => return false,
32 };
33
34 // if the inbound channel with recipient is suspended, it means that we are unable to
35 // receive congestion reports from the `with` location. So we assume the pipeline is
36 // congested too.
37 if pallet::Pallet::<Runtime>::is_inbound_channel_suspended(sibling_para_id) {
38 return true
39 }
40
41 // if the outbound channel with recipient is suspended, it means that one of further
42 // queues (e.g. bridge queue between two bridge hubs) is overloaded, so we shall
43 // take larger fee for our outbound messages
44 OutXcmpChannelStatusProvider::<Runtime>::is_congested(with)
45 }
46}
47
48/// Adapter implementation for `bp_xcm_bridge::ChannelStatusProvider` and/or
49/// `bp_xcm_bridge_hub_router::XcmChannelStatusProvider` which checks only `OutboundXcmpStatus`
50/// for defined `Location` if is suspended.
51pub struct OutXcmpChannelStatusProvider<Runtime>(core::marker::PhantomData<Runtime>);
52impl<Runtime: crate::Config> OutXcmpChannelStatusProvider<Runtime> {
53 fn is_congested(with: &Location) -> bool {
54 // handle congestion only for a sibling parachain locations.
55 let sibling_para_id: ParaId = match with.unpack() {
56 (_, [Parachain(para_id)]) => (*para_id).into(),
57 _ => return false,
58 };
59
60 // let's find the channel's state with the sibling parachain,
61 let Some((outbound_state, queued_pages)) =
62 pallet::Pallet::<Runtime>::outbound_channel_state(sibling_para_id)
63 else {
64 return false
65 };
66 // suspended channel => it is congested
67 if outbound_state == OutboundState::Suspended {
68 return true
69 }
70
71 // It takes some time for target parachain to suspend inbound channel with the target BH and
72 // during that we will keep accepting new message delivery transactions. Let's also reject
73 // new deliveries if there are too many "pages" (concatenated XCM messages) in the target BH
74 // -> target parachain queue.
75
76 // If the outbound channel has at least `N` pages enqueued, let's assume it is congested.
77 // Normally, the chain with a few opened HRMP channels, will "send" pages at every block.
78 // Having `N` pages means that for last `N` blocks we either have not sent any messages,
79 // or have sent signals.
80
81 const MAX_QUEUED_PAGES_BEFORE_DEACTIVATION: u16 = 4;
82 if queued_pages > MAX_QUEUED_PAGES_BEFORE_DEACTIVATION {
83 return true
84 }
85
86 false
87 }
88}
89
90impl<Runtime: crate::Config> bp_xcm_bridge_hub_router::XcmChannelStatusProvider
91 for OutXcmpChannelStatusProvider<Runtime>
92{
93 fn is_congested(with: &Location) -> bool {
94 Self::is_congested(with)
95 }
96}
97
98#[cfg(feature = "runtime-benchmarks")]
99pub fn suspend_channel_for_benchmarks<T: crate::Config>(target: ParaId) {
100 pallet::Pallet::<T>::suspend_channel(target)
101}