Skip to content

Commit

Permalink
Add BlobDispatcherWithChannelStatus adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Nov 5, 2024
1 parent a561a6f commit f8db8a5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 17 additions & 1 deletion bridges/modules/xcm-bridge-hub/src/congestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use codec::Encode;
use bp_xcm_bridge_hub::{BridgeId, LocalXcmChannelManager, Receiver};
use sp_runtime::traits::Convert;
use xcm::latest::{send_xcm, Location, SendXcm, Xcm};
use crate::{Config, Bridges, LOG_TARGET};
use xcm_builder::{DispatchBlob, DispatchBlobError};
use crate::{Config, Bridges, LOG_TARGET, DispatchChannelStatusProvider};

/// Switches the implementation of `LocalXcmChannelManager` based on the `local_origin`.
///
Expand Down Expand Up @@ -149,3 +150,18 @@ impl<T: Config<I>, I: 'static, XcmProvider: Convert<Vec<u8>, Xcm<()>>, XcmSender
Self::report_bridge_status(local_origin, bridge, false)
}
}

/// Adapter that ties together the `DispatchBlob` trait with the `DispatchChannelStatusProvider` trait.
/// The idea is that `DispatchBlob` triggers message dispatch/delivery on the receiver side,
/// while `DispatchChannelStatusProvider` provides an status check to ensure the dispatch channel is active (not congested).
pub struct BlobDispatcherWithChannelStatus<ChannelDispatch, ChannelStatus>(PhantomData<(ChannelDispatch, ChannelStatus)>);
impl<ChannelDispatch: DispatchBlob, ChannelStatus> DispatchBlob for BlobDispatcherWithChannelStatus<ChannelDispatch, ChannelStatus> {
fn dispatch_blob(blob: Vec<u8>) -> Result<(), DispatchBlobError> {
ChannelDispatch::dispatch_blob(blob)
}
}
impl<ChannelDispatch, ChannelStatus: DispatchChannelStatusProvider> DispatchChannelStatusProvider for BlobDispatcherWithChannelStatus<ChannelDispatch, ChannelStatus> {
fn is_congested(with: &Location) -> bool {
ChannelStatus::is_congested(with)
}
}
3 changes: 2 additions & 1 deletion bridges/modules/xcm-bridge-hub/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use xcm_builder::{
SiblingParachainConvertsVia, SovereignPaidRemoteExporter, UnpaidLocalExporter, ensure_is_remote,
};
use xcm_executor::{traits::{ConvertLocation, ConvertOrigin}, XcmExecutor};
use crate::congestion::BlobDispatcherWithChannelStatus;

pub type AccountId = AccountId32;
pub type Balance = u64;
Expand Down Expand Up @@ -231,7 +232,7 @@ impl pallet_xcm_bridge_hub::Config for TestRuntime {

type LocalXcmChannelManager = TestLocalXcmChannelManager;

type BlobDispatcher = TestBlobDispatcher;
type BlobDispatcher = BlobDispatcherWithChannelStatus<TestBlobDispatcher, TestBlobDispatcher>;
}

/// A router instance simulates a scenario where the router is deployed on a different chain than
Expand Down

0 comments on commit f8db8a5

Please sign in to comment.