Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[asset-hubs/bridge-hubs] Bridging Polkadot Asset Hub <-> Kusama Asset Hub to enable asset transfer of KSMs/DOTs #1352

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4617ffa
asset-hubs: transfer assets via bridge using pallet-xcm
acatangiu Aug 28, 2023
a517420
Transfer reserve asset with dynamic fees and back-pressure.
bkontur Sep 3, 2023
945ea7f
Kusama/Polkadot BridgeHub allows bridging between Kusama/Polkadot (re…
bkontur Sep 4, 2023
4af6519
Extended Kusama/Polkadot BridgeHub with dynamic fees and congestion s…
bkontur Sep 4, 2023
867321c
Update review comments from Adrian
bkontur Sep 5, 2023
11e0931
Merge branch 'master' into bko-bridging-kusama-polkadot-assets
bkontur Sep 7, 2023
a3d1264
Fixed local run for AssetHubKusama/AssetHubPolkadot asset transfer (K…
bkontur Sep 11, 2023
0aeb13a
Merge remote-tracking branch 'origin/master' into bko-bridging-kusama…
bkontur Sep 19, 2023
c20f3e4
Zombienet nits
bkontur Sep 19, 2023
80964f9
Merge remote-tracking branch 'origin/master' into bko-bridging-kusama…
bkontur Sep 19, 2023
6fe8678
Merge remote-tracking branch 'origin/master' into bko-bridging-kusama…
bkontur Sep 19, 2023
b2015dc
Fix benchmarks
bkontur Sep 19, 2023
1fd7aaa
Fix benchmarks
bkontur Sep 19, 2023
791d6dd
Backporting nits from https://github.com/paritytech/polkadot-sdk/pull…
bkontur Sep 21, 2023
2cd5e55
Merge remote-tracking branch 'origin/master' into bko-bridging-kusama…
bkontur Sep 21, 2023
344e0b0
ParaIds as constants to primitives
bkontur Sep 21, 2023
d42e481
Fix
bkontur Sep 21, 2023
76ca00b
Fix
bkontur Sep 21, 2023
03511e0
ensure_configuration -> prepare_configuration
bkontur Sep 22, 2023
fc9727c
Constants nit
bkontur Sep 22, 2023
534c718
Deps
bkontur Sep 22, 2023
58d6319
Cargo.lock
bkontur Sep 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bridges/primitives/chain-asset-hub-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ frame_support::parameter_types! {
/// (initially was calculated `170733333` + `10%` by test `BridgeHubKusama::can_calculate_weight_for_paid_export_message_with_reserve_transfer`)
pub const BridgeHubKusamaBaseFeeInDots: u128 = 187806666;
}

/// Identifier of AssetHubKusama in the Kusama relay chain.
pub const ASSET_HUB_KUSAMA_PARACHAIN_ID: u32 = 1000;
3 changes: 3 additions & 0 deletions bridges/primitives/chain-asset-hub-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ frame_support::parameter_types! {
/// (initially was calculated `51220000` + `10%` by test `BridgeHubPolkadot::can_calculate_weight_for_paid_export_message_with_reserve_transfer`)
pub const BridgeHubPolkadotBaseFeeInDots: u128 = 56342000;
}

/// Identifier of AssetHubPolkadot in the Polkadot relay chain.
pub const ASSET_HUB_POLKADOT_PARACHAIN_ID: u32 = 1000;
3 changes: 3 additions & 0 deletions bridges/primitives/chain-bridge-hub-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ pub const WITH_BRIDGE_HUB_KUSAMA_MESSAGES_PALLET_NAME: &str = "BridgeKusamaMessa
/// chains.
pub const WITH_BRIDGE_HUB_KUSAMA_RELAYERS_PALLET_NAME: &str = "BridgeRelayers";

/// Pallet index of `BridgePolkadotMessages: pallet_bridge_messages::<Instance1>`.
pub const WITH_BRIDGE_POLKADOT_MESSAGES_PALLET_INDEX: u8 = 53;

decl_bridge_finality_runtime_apis!(bridge_hub_kusama);
decl_bridge_messages_runtime_apis!(bridge_hub_kusama);
3 changes: 3 additions & 0 deletions bridges/primitives/chain-bridge-hub-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,8 @@ pub const WITH_BRIDGE_HUB_POLKADOT_MESSAGES_PALLET_NAME: &str = "BridgePolkadotM
/// chains.
pub const WITH_BRIDGE_HUB_POLKADOT_RELAYERS_PALLET_NAME: &str = "BridgeRelayers";

/// Pallet index of `BridgeKusamaMessages: pallet_bridge_messages::<Instance1>`.
pub const WITH_BRIDGE_KUSAMA_MESSAGES_PALLET_INDEX: u8 = 53;

decl_bridge_finality_runtime_apis!(bridge_hub_polkadot);
decl_bridge_messages_runtime_apis!(bridge_hub_polkadot);
5 changes: 5 additions & 0 deletions cumulus/pallets/xcmp-queue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ cumulus-primitives-core = { path = "../../primitives/core", default-features = f
# Optional import for benchmarking
frame-benchmarking = { path = "../../../substrate/frame/benchmarking", default-features = false, optional = true}

# Bridges
bp-xcm-bridge-hub-router = { path = "../../../bridges/primitives/xcm-bridge-hub-router", default-features = false, optional = true }

[dev-dependencies]

# Substrate
Expand All @@ -43,6 +46,7 @@ cumulus-pallet-parachain-system = { path = "../parachain-system", features = ["p
[features]
default = [ "std" ]
std = [
"bp-xcm-bridge-hub-router?/std",
"codec/std",
"cumulus-primitives-core/std",
"frame-benchmarking?/std",
Expand Down Expand Up @@ -77,3 +81,4 @@ try-runtime = [
"polkadot-runtime-common/try-runtime",
"sp-runtime/try-runtime",
]
bridging = [ "bp-xcm-bridge-hub-router" ]
95 changes: 95 additions & 0 deletions cumulus/pallets/xcmp-queue/src/bridging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{pallet, OutboundState};
use cumulus_primitives_core::ParaId;
use frame_support::pallet_prelude::Get;

/// Adapter implementation for `bp_xcm_bridge_hub_router::XcmChannelStatusProvider` which checks
/// both `OutboundXcmpStatus` and `InboundXcmpStatus` for defined `ParaId` if any of those is
/// suspended.
pub struct InAndOutXcmpChannelStatusProvider<SiblingBridgeHubParaId, Runtime>(
sp_std::marker::PhantomData<(SiblingBridgeHubParaId, Runtime)>,
);
impl<SiblingBridgeHubParaId: Get<ParaId>, Runtime: crate::Config>
bp_xcm_bridge_hub_router::XcmChannelStatusProvider
for InAndOutXcmpChannelStatusProvider<SiblingBridgeHubParaId, Runtime>
{
fn is_congested() -> bool {
// if the inbound channel with recipient is suspended, it means that we are unable to
// receive congestion reports from the bridge hub. So we assume the bridge pipeline is
// congested too
if pallet::Pallet::<Runtime>::is_inbound_channel_suspended(SiblingBridgeHubParaId::get()) {
return true
}

// if the outbound channel with recipient is suspended, it means that one of further
// bridge queues (e.g. bridge queue between two bridge hubs) is overloaded, so we shall
// take larger fee for our outbound messages
OutXcmpChannelStatusProvider::<SiblingBridgeHubParaId, Runtime>::is_congested()
}
}

/// Adapter implementation for `bp_xcm_bridge_hub_router::XcmChannelStatusProvider` which checks
/// only `OutboundXcmpStatus` for defined `SiblingParaId` if is suspended.
pub struct OutXcmpChannelStatusProvider<SiblingBridgeHubParaId, Runtime>(
sp_std::marker::PhantomData<(SiblingBridgeHubParaId, Runtime)>,
);
impl<SiblingBridgeHubParaId: Get<ParaId>, Runtime: crate::Config>
bp_xcm_bridge_hub_router::XcmChannelStatusProvider
for OutXcmpChannelStatusProvider<SiblingBridgeHubParaId, Runtime>
{
fn is_congested() -> bool {
let sibling_bridge_hub_id: ParaId = SiblingBridgeHubParaId::get();

// let's find the channel's state with the sibling parachain,
let Some((outbound_state, queued_pages)) = pallet::Pallet::<Runtime>::outbound_channel_state(sibling_bridge_hub_id) else {
return false
};
// suspended channel => it is congested
if outbound_state == OutboundState::Suspended {
return true
}

// TODO: https://github.com/paritytech/polkadot-sdk/pull/1556 - once this PR is merged, we may
// remove the following code.
// TODO: the following restriction is arguable, we may live without that, assuming that
// There can't be more than some `N` messages queued at the bridge queue (at the source BH)
// AND before accepting next (or next-after-next) delivery transaction, we'll receive the
// suspension signal from the target parachain and stop accepting delivery transactions.

// It takes some time for target parachain to suspend inbound channel with the target BH and
// during that we will keep accepting new message delivery transactions. Let's also reject
// new deliveries if there are too many "pages" (concatenated XCM messages) in the target BH
// -> target parachain queue.

// If the outbound channel has at least `N` pages enqueued, let's assume it is congested.
// Normally, the chain with a few opened HRMP channels, will "send" pages at every block.
// Having `N` pages means that for last `N` blocks we either have not sent any messages,
// or have sent signals.

const MAX_QUEUED_PAGES_BEFORE_DEACTIVATION: u16 = 4;
if queued_pages > MAX_QUEUED_PAGES_BEFORE_DEACTIVATION {
return true
}

false
}
}

#[cfg(feature = "runtime-benchmarks")]
pub fn suspend_channel_for_benchmarks<T: crate::Config>(target: ParaId) {
pallet::Pallet::<T>::suspend_channel(target)
}
20 changes: 20 additions & 0 deletions cumulus/pallets/xcmp-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(feature = "bridging")]
pub mod bridging;
pub mod weights;
pub use weights::WeightInfo;

Expand Down Expand Up @@ -947,6 +949,24 @@ impl<T: Config> Pallet<T> {
}
});
}

#[cfg(feature = "bridging")]
fn is_inbound_channel_suspended(sender: ParaId) -> bool {
<InboundXcmpStatus<T>>::get()
.iter()
.find(|c| c.sender == sender)
.map(|c| c.state == InboundState::Suspended)
.unwrap_or(false)
}

#[cfg(feature = "bridging")]
/// Returns tuple of `OutboundState` and number of queued pages.
fn outbound_channel_state(target: ParaId) -> Option<(OutboundState, u16)> {
<OutboundXcmpStatus<T>>::get().iter().find(|c| c.recipient == target).map(|c| {
let queued_pages = c.last_index.saturating_sub(c.first_index);
(c.state, queued_pages)
})
}
}

impl<T: Config> XcmpMessageHandler for Pallet<T> {
Expand Down
Loading