Skip to content

Bridges - Add improved congestion control mechanism#6231

Open
bkontur wants to merge 174 commits intomasterfrom
bko-bridges-congestion
Open

Bridges - Add improved congestion control mechanism#6231
bkontur wants to merge 174 commits intomasterfrom
bko-bridges-congestion

Conversation

@bkontur
Copy link
Copy Markdown
Contributor

@bkontur bkontur commented Oct 25, 2024

Closes: #5551
Closes: #5550

Context

Before permissionless lanes, bridges only supported hard-coded, static lanes. The congestion mechanism was based on sending Transact(report_bridge_status(is_congested)) from pallet-xcm-bridge-hub to pallet-xcm-bridge-hub-router. Depending on is_congested, we adjusted the fee factor to increase or decrease fees. This congestion mechanism relied on monitoring XCMP queues, which could cause issues like suspending the entire XCMP queue rather than just the affected bridge.

Additionally, we are progressing with deploying bridge message pallets/routing directly on AssetHub, where we don’t interact with XCMP to perform ExportXcm locally.

Description

This PR introduces two new pallets pallet-xcm-bridge and pallet-xcm-bridge-router. These pallets are designed to improve congestion handling for bridges. The main objective of this PR is to enhance the routing and management of messages between chains.

This PR re-introduces and improves congestion for bridges:

  • Enhanced Bridge Congestion Mechanism: The bridge queue mechanism has been restructured to operate independently of XCMP, with a refined protocol for congestion detection and suspension management (congested_threshold and stop_threshold).
  • Bridge-Specific Channel Suspension: pallet-xcm-bridge and pallet-xcm-bridge-router now use BridgeId to identify specific bridges, enabling selective suspension and resumption of individual bridge channels.
  • Dynamic Congestion Detection: pallet-xcm-bridge now includes callbacks for fn suspend_bridge and fn resume_bridge based on congestion status:
    • For sibling chains, the pallet-xcm-bridges exporter can send xcm::Transact(update_bridge_status(bridge_id, is_congested)) using the stored callback information.
    • For local chain deployments, the pallet-xcm-bridge's exporter manages state directly.
  • New Stop Threshold: A stop_threshold limit in pallet-xcm-bridge enables or disables ExportXcm::validate, providing a fallback mechanism when the router does not adhere to the suspend signal.
  • Flexible Message Routing: pallet-xcm-bridge-router has support for message routing for both sibling chains (ExportMessage - ViaRemoteBridgeExporter) and local deployment (ExportXcm - ViaLocalBridgeExporter).

Open questions

  • when the router receives is_congested = false with fn do_update_bridge_status(, can we directly remove it (its fee factor) from Bridges and the next message fee wont be affected by increased fee factor or slowly start decreasing fee factor on idle (this is how it is implemented now)? Original comment
  • Report congestion detection uses bool update_bridge_status(bool), but shouldn't we better send the enum BridgeState {Opened, SoftSuspended, HardSuspended, Closed}? And on HardSuspended stop the pallet-xcm-bridge-router?

@bkontur bkontur added the T15-bridges This PR/Issue is related to bridges. label Oct 25, 2024
@bkontur bkontur self-assigned this Oct 25, 2024
@bkontur
Copy link
Copy Markdown
Contributor Author

bkontur commented Oct 25, 2024

bot fmt
/cmd prdoc --audience runtime_dev --bump patch

@bkontur bkontur force-pushed the bko-bridges-congestion branch 2 times, most recently from 659be89 to b48b8a5 Compare October 25, 2024 21:14
@bkontur bkontur force-pushed the bko-bridges-congestion branch from a663bc2 to cbc6ae7 Compare October 26, 2024 20:29
@bkontur
Copy link
Copy Markdown
Contributor Author

bkontur commented Oct 26, 2024

bot fmt

@bkontur bkontur force-pushed the bko-bridges-congestion branch from c78f9bc to 501a5c0 Compare October 28, 2024 15:01
@bkontur
Copy link
Copy Markdown
Contributor Author

bkontur commented Oct 28, 2024

bot fmt

@bkontur bkontur force-pushed the bko-bridges-congestion branch 7 times, most recently from c78e707 to 152389a Compare November 5, 2024 12:33
@bkontur
Copy link
Copy Markdown
Contributor Author

bkontur commented Nov 5, 2024

bot fmt

@bkontur bkontur force-pushed the bko-bridges-congestion branch 3 times, most recently from edd9c5c to 38f1bb3 Compare November 7, 2024 13:33
@bkontur
Copy link
Copy Markdown
Contributor Author

bkontur commented Nov 7, 2024

/cmd bench --runtime asset-hub-westend asset-hub-rococo --pallet pallet_xcm_bridge_hub_router

@bkontur
Copy link
Copy Markdown
Contributor Author

bkontur commented Nov 7, 2024

bot bench cumulus-assets --runtime=asset-hub-westend --pallet=pallet_xcm_bridge_hub_router
bot bench cumulus-assets --runtime=asset-hub-rococo --pallet=pallet_xcm_bridge_hub_router

@bkontur bkontur force-pushed the bko-bridges-congestion branch from f06433a to d329dec Compare November 7, 2024 16:44
@bkontur
Copy link
Copy Markdown
Contributor Author

bkontur commented Nov 7, 2024

bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-assets --runtime=asset-hub-westend --pallet=pallet_xcm_bridge_hub_router
bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-assets --runtime=asset-hub-rococo --pallet=pallet_xcm_bridge_hub_router

bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --runtime=bridge-hub-rococo --pallet=pallet_bridge_messages
bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --runtime=bridge-hub-westend --pallet=pallet_bridge_messages

@bkontur
Copy link
Copy Markdown
Contributor Author

bkontur commented Nov 7, 2024

bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --runtime=bridge-hub-rococo --pallet=pallet_bridge_messages
bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --runtime=bridge-hub-westend --pallet=pallet_bridge_messages
bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --subcommand=xcm --runtime=bridge-hub-rococo --pallet=pallet_xcm_benchmarks::generic
bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --subcommand=xcm --runtime=bridge-hub-westend --pallet=pallet_xcm_benchmarks::generic

@bkontur bkontur force-pushed the bko-bridges-congestion branch from 21baa7f to c00ff6b Compare November 8, 2024 17:44
@bkontur
Copy link
Copy Markdown
Contributor Author

bkontur commented Nov 8, 2024

bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --runtime=bridge-hub-rococo --pallet=pallet_bridge_messages
bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --runtime=bridge-hub-westend --pallet=pallet_bridge_messages
bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --runtime=bridge-hub-rococo --pallet=pallet_xcm_bridge_hub
bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --runtime=bridge-hub-westend --pallet=pallet_xcm_bridge_hub

bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --subcommand=xcm --runtime=bridge-hub-rococo --pallet=pallet_xcm_benchmarks::generic
bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-bridge-hubs --subcommand=xcm --runtime=bridge-hub-westend --pallet=pallet_xcm_benchmarks::generic

bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-assets --runtime=asset-hub-westend --pallet=pallet_xcm_bridge_hub_router
bot bench -v PIPELINE_SCRIPTS_REF=bko-fix cumulus-assets --runtime=asset-hub-rococo --pallet=pallet_xcm_bridge_hub_router

@bkontur
Copy link
Copy Markdown
Contributor Author

bkontur commented May 7, 2025

The 2 comments so far seem like they can be addressed separately, so if you think they make sense, I would propose, for the beginning to address them in a separate PR which precedes this. And to rebase this PR on top of that one. After that I will continue with the review.

@serban300 I created separate issue for those comments and they are up to you :)

Personally, I would prefer to review and merge this PR incrementally since it's very large. My concern is that reviewing 9k lines at once can lead to missing things.

Let me try to split this big PR, but till that time, please you could help with review for other smaller ones:

Especially, the 8324/8325/8326/8368 could possibly go directly to the master (+backports)

Fixing #8215 based on
#8185: Improve
try-state for pallet-xcm-bridge

It removes try_as and uses try_into implementation instead.

After adding test case where VersionedLocation* to have (XCM_VERSION -
1) is , the test case fails.


![after-adding-test-before-fix](https://github.com/user-attachments/assets/0672c677-fc50-4b1e-b636-7b6f23e4092d)


After adding the fix, by removing try_as and replacing with try_into,
test case passed:

![after-adding-fix](https://github.com/user-attachments/assets/18452881-23e5-4563-b08f-47d4e0ef4be3)
rosarp and others added 2 commits May 22, 2025 21:57
Fixing #8215 based on
#8185: Improve
try-state for pallet-xcm-bridge

It removes try_as and uses try_into implementation instead.

---------

Co-authored-by: Branislav Kontur <[email protected]>
@bkontur
Copy link
Copy Markdown
Contributor Author

bkontur commented May 26, 2025

/cmd fmt

Comment on lines +228 to +235
let message_size_factor =
FixedU128::from_u32(message_size.saturating_div(1024))
.saturating_mul(MESSAGE_SIZE_FEE_BASE);
let total_factor = EXPONENTIAL_FEE_BASE.saturating_add(message_size_factor);

let previous_factor = bridge_state.delivery_fee_factor;
bridge_state.delivery_fee_factor =
bridge_state.delivery_fee_factor.saturating_mul(total_factor);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will integrate the refactored FeeTracker here after we sort out all the other comments related to this pallet.


// handle congestion and fee factor (if detected)
Bridges::<T, I>::mutate_exists(&bridge_id, |bridge_state| match bridge_state {
Some(ref mut bridge_state) if bridge_state.is_congested => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is is_congested actually needed ? Looks like we could just increase the fee factor here and set it to MINIMAL_DELIVERY_FEE_FACTOR on do_update_bridge_status(is_congested: false).

I'm just wondering if it would be good to have it for the future or for easier tracking of the bridge status.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is is_congested actually needed ? Looks like we could just increase the fee factor here and set it to MINIMAL_DELIVERY_FEE_FACTOR on do_update_bridge_status(is_congested: false).

I'm just wondering if it would be good to have it for the future or for easier tracking of the bridge status.

If you check description, there is an open question about changing that bool to BridgeState.

  • Report congestion detection uses bool update_bridge_status(bool), but shouldn't we better send the enum BridgeState {Opened, SoftSuspended, HardSuspended, Closed}? And on HardSuspended stop the pallet-xcm-bridge-router?

So, if we want to stop the router (and prevent dropping), we need to change bool to BridgeState. But if we don't want to stop the router and only rely on increasing the fee factor, then yes — we don't even need to store is_congested.

@serban300 what do you think and/or suggest?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm I don't know what would be best. Thinking.

The relevant change, which allows us to do dynamic pricing in
`pallet-xcm-bridge` based on the router configuration and bridge status:

```rust
-		/// Price of single message export to the bridged consensus (`Self::BridgedNetwork`).
-		type MessageExportPrice: Get<Assets>;
+		/// Price of a single message export to the bridged consensus (`Self::BridgedNetwork`).
+		type MessageExportPrice: PriceForMessageDelivery<Id = BridgeId>;
```



Basically, with message bridging on AssetHub, the same dynamic price
(based on the bridge congestion status) is calculated for exporting
either via:
- local routing using `ToRococoOverAssetHubRococoXcmRouter` /
`ToWestendOverAssetHubWestendXcmRouter` (AH<>AH transfer)
- executing `ExportMessage` from a sibling chain.

It is just backport of xcm-bridge/xcm-bridge-router stuff from this
commit:
8ada805
(where you can see also the real usage and setup)

cc: @serban300
rosarp and others added 2 commits July 22, 2025 00:00
Fixing comments in PR
#6231

- changed DeliveryFeeFactorUpdated
#6231 (comment)
- changed Bridges with ValueQuery with defaults
#6231 (comment)
- updated message_size to be u32
#6231 (comment)

---------

Co-authored-by: Branislav Kontur <[email protected]>
@paritytech-workflow-stopper
Copy link
Copy Markdown

All GitHub workflows were cancelled due to failure one of the required jobs.
Failed workflow url: https://github.com/paritytech/polkadot-sdk/actions/runs/16429704449
Failed job name: fmt

serban300 and others added 2 commits July 30, 2025 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T15-bridges This PR/Issue is related to bridges.

Projects

Status: In Progress
Status: In Progress
Status: Backlog

Development

Successfully merging this pull request may close these issues.

Add LocalXcmChannelManager impls for XcmpQueue and BridgeHubs Add benchmarks for pallet-xcm-bridge-hub

9 participants