2222//! `XcmRouter` <- `MessageDispatch` <- `InboundMessageQueue`
2323
2424use bp_messages:: {
25- source_chain:: { MessagesBridge , OnMessagesDelivered } ,
25+ source_chain:: OnMessagesDelivered ,
2626 target_chain:: { DispatchMessage , MessageDispatch } ,
2727 LaneId , MessageNonce ,
2828} ;
2929use bp_runtime:: messages:: MessageDispatchResult ;
30+ pub use bp_xcm_bridge_hub:: XcmAsPlainPayload ;
3031use bp_xcm_bridge_hub_router:: XcmChannelStatusProvider ;
3132use codec:: { Decode , Encode } ;
3233use frame_support:: { traits:: Get , weights:: Weight , CloneNoBound , EqNoBound , PartialEqNoBound } ;
3334use pallet_bridge_messages:: {
34- Config as MessagesConfig , OutboundLanesCongestedSignals , Pallet as MessagesPallet ,
35- WeightInfoExt as MessagesPalletWeights ,
35+ Config as MessagesConfig , OutboundLanesCongestedSignals , WeightInfoExt as MessagesPalletWeights ,
3636} ;
3737use scale_info:: TypeInfo ;
3838use sp_runtime:: SaturatedConversion ;
3939use sp_std:: { fmt:: Debug , marker:: PhantomData } ;
4040use xcm:: prelude:: * ;
41- use xcm_builder:: { DispatchBlob , DispatchBlobError , HaulBlob , HaulBlobError } ;
42-
43- /// Plain "XCM" payload, which we transfer through bridge
44- pub type XcmAsPlainPayload = sp_std:: prelude:: Vec < u8 > ;
41+ use xcm_builder:: { DispatchBlob , DispatchBlobError } ;
4542
4643/// Message dispatch result type for single message
4744#[ derive( CloneNoBound , EqNoBound , PartialEqNoBound , Encode , Decode , Debug , TypeInfo ) ]
@@ -123,6 +120,7 @@ impl<
123120
124121/// A pair of sending chain location and message lane, used by this chain to send messages
125122/// over the bridge.
123+ #[ cfg_attr( feature = "std" , derive( Debug , Eq , PartialEq ) ) ]
126124pub struct SenderAndLane {
127125 /// Sending chain relative location.
128126 pub location : MultiLocation ,
@@ -144,8 +142,6 @@ pub trait XcmBlobHauler {
144142 type Runtime : MessagesConfig < Self :: MessagesInstance > ;
145143 /// Instance of the messages pallet that is used to send messages.
146144 type MessagesInstance : ' static ;
147- /// Returns lane used by this hauler.
148- type SenderAndLane : Get < SenderAndLane > ;
149145
150146 /// Actual XCM message sender (`HRMP` or `UMP`) to the source chain
151147 /// location (`Self::SenderAndLane::get().location`).
@@ -166,54 +162,25 @@ pub trait XcmBlobHauler {
166162/// makes sure that XCM blob is sent to the outbound lane to be relayed.
167163///
168164/// It needs to be used at the source bridge hub.
169- pub struct XcmBlobHaulerAdapter < XcmBlobHauler > ( sp_std:: marker:: PhantomData < XcmBlobHauler > ) ;
165+ pub struct XcmBlobHaulerAdapter < XcmBlobHauler , Lanes > (
166+ sp_std:: marker:: PhantomData < ( XcmBlobHauler , Lanes ) > ,
167+ ) ;
170168
171- impl < H : XcmBlobHauler > HaulBlob for XcmBlobHaulerAdapter < H >
172- where
173- H :: Runtime : MessagesConfig < H :: MessagesInstance , OutboundPayload = XcmAsPlainPayload > ,
169+ impl <
170+ H : XcmBlobHauler ,
171+ Lanes : Get < sp_std:: vec:: Vec < ( SenderAndLane , ( NetworkId , InteriorMultiLocation ) ) > > ,
172+ > OnMessagesDelivered for XcmBlobHaulerAdapter < H , Lanes >
174173{
175- fn haul_blob ( blob : sp_std:: prelude:: Vec < u8 > ) -> Result < ( ) , HaulBlobError > {
176- let sender_and_lane = H :: SenderAndLane :: get ( ) ;
177- MessagesPallet :: < H :: Runtime , H :: MessagesInstance > :: send_message ( sender_and_lane. lane , blob)
178- . map ( |artifacts| {
179- log:: info!(
180- target: crate :: LOG_TARGET_BRIDGE_DISPATCH ,
181- "haul_blob result - ok: {:?} on lane: {:?}. Enqueued messages: {}" ,
182- artifacts. nonce,
183- sender_and_lane. lane,
184- artifacts. enqueued_messages,
185- ) ;
186-
187- // notify XCM queue manager about updated lane state
188- LocalXcmQueueManager :: < H > :: on_bridge_message_enqueued (
189- & sender_and_lane,
190- artifacts. enqueued_messages ,
191- ) ;
192- } )
193- . map_err ( |error| {
194- log:: error!(
195- target: crate :: LOG_TARGET_BRIDGE_DISPATCH ,
196- "haul_blob result - error: {:?} on lane: {:?}" ,
197- error,
198- sender_and_lane. lane,
199- ) ;
200- HaulBlobError :: Transport ( "MessageSenderError" )
201- } )
202- }
203- }
204-
205- impl < H : XcmBlobHauler > OnMessagesDelivered for XcmBlobHaulerAdapter < H > {
206174 fn on_messages_delivered ( lane : LaneId , enqueued_messages : MessageNonce ) {
207- let sender_and_lane = H :: SenderAndLane :: get ( ) ;
208- if sender_and_lane. lane != lane {
209- return
175+ if let Some ( sender_and_lane) =
176+ Lanes :: get ( ) . iter ( ) . find ( |link| link. 0 . lane == lane) . map ( |link| & link. 0 )
177+ {
178+ // notify XCM queue manager about updated lane state
179+ LocalXcmQueueManager :: < H > :: on_bridge_messages_delivered (
180+ sender_and_lane,
181+ enqueued_messages,
182+ ) ;
210183 }
211-
212- // notify XCM queue manager about updated lane state
213- LocalXcmQueueManager :: < H > :: on_bridge_messages_delivered (
214- & sender_and_lane,
215- enqueued_messages,
216- ) ;
217184 }
218185}
219186
@@ -356,6 +323,9 @@ mod tests {
356323 location: MultiLocation :: new( 1 , X1 ( Parachain ( 1000 ) ) ) ,
357324 lane: TEST_LANE_ID ,
358325 } ;
326+ pub TestLanes : sp_std:: vec:: Vec <( SenderAndLane , ( NetworkId , InteriorMultiLocation ) ) > = sp_std:: vec![
327+ ( TestSenderAndLane :: get( ) , ( NetworkId :: ByGenesis ( [ 0 ; 32 ] ) , InteriorMultiLocation :: Here ) )
328+ ] ;
359329 pub DummyXcmMessage : Xcm <( ) > = Xcm :: new( ) ;
360330 }
361331
@@ -389,56 +359,69 @@ mod tests {
389359 impl XcmBlobHauler for TestBlobHauler {
390360 type Runtime = TestRuntime ;
391361 type MessagesInstance = ( ) ;
392- type SenderAndLane = TestSenderAndLane ;
393362
394363 type ToSourceChainSender = DummySendXcm ;
395364 type CongestedMessage = DummyXcmMessage ;
396365 type UncongestedMessage = DummyXcmMessage ;
397366 }
398367
399- type TestBlobHaulerAdapter = XcmBlobHaulerAdapter < TestBlobHauler > ;
368+ type TestBlobHaulerAdapter = XcmBlobHaulerAdapter < TestBlobHauler , TestLanes > ;
400369
401- fn fill_up_lane_to_congestion ( ) {
370+ fn fill_up_lane_to_congestion ( ) -> MessageNonce {
371+ let latest_generated_nonce = OUTBOUND_LANE_CONGESTED_THRESHOLD ;
402372 OutboundLanes :: < TestRuntime , ( ) > :: insert (
403373 TEST_LANE_ID ,
404374 OutboundLaneData {
405375 oldest_unpruned_nonce : 0 ,
406376 latest_received_nonce : 0 ,
407- latest_generated_nonce : OUTBOUND_LANE_CONGESTED_THRESHOLD ,
377+ latest_generated_nonce,
408378 } ,
409379 ) ;
380+ latest_generated_nonce
410381 }
411382
412383 #[ test]
413384 fn congested_signal_is_not_sent_twice ( ) {
414385 run_test ( || {
415- fill_up_lane_to_congestion ( ) ;
386+ let enqueued = fill_up_lane_to_congestion ( ) ;
416387
417388 // next sent message leads to congested signal
418- TestBlobHaulerAdapter :: haul_blob ( vec ! [ 42 ] ) . unwrap ( ) ;
389+ LocalXcmQueueManager :: < TestBlobHauler > :: on_bridge_message_enqueued (
390+ & TestSenderAndLane :: get ( ) ,
391+ enqueued + 1 ,
392+ ) ;
419393 assert_eq ! ( DummySendXcm :: messages_sent( ) , 1 ) ;
420394
421395 // next sent message => we don't sent another congested signal
422- TestBlobHaulerAdapter :: haul_blob ( vec ! [ 42 ] ) . unwrap ( ) ;
396+ LocalXcmQueueManager :: < TestBlobHauler > :: on_bridge_message_enqueued (
397+ & TestSenderAndLane :: get ( ) ,
398+ enqueued,
399+ ) ;
423400 assert_eq ! ( DummySendXcm :: messages_sent( ) , 1 ) ;
424401 } ) ;
425402 }
426403
427404 #[ test]
428405 fn congested_signal_is_not_sent_when_outbound_lane_is_not_congested ( ) {
429406 run_test ( || {
430- TestBlobHaulerAdapter :: haul_blob ( vec ! [ 42 ] ) . unwrap ( ) ;
407+ LocalXcmQueueManager :: < TestBlobHauler > :: on_bridge_message_enqueued (
408+ & TestSenderAndLane :: get ( ) ,
409+ 1 ,
410+ ) ;
431411 assert_eq ! ( DummySendXcm :: messages_sent( ) , 0 ) ;
432412 } ) ;
433413 }
434414
435415 #[ test]
436416 fn congested_signal_is_sent_when_outbound_lane_is_congested ( ) {
437417 run_test ( || {
438- fill_up_lane_to_congestion ( ) ;
418+ let enqueued = fill_up_lane_to_congestion ( ) ;
439419
440420 // next sent message leads to congested signal
441- TestBlobHaulerAdapter :: haul_blob ( vec ! [ 42 ] ) . unwrap ( ) ;
421+ LocalXcmQueueManager :: < TestBlobHauler > :: on_bridge_message_enqueued (
422+ & TestSenderAndLane :: get ( ) ,
423+ enqueued + 1 ,
424+ ) ;
442425 assert_eq ! ( DummySendXcm :: messages_sent( ) , 1 ) ;
443426 assert ! ( LocalXcmQueueManager :: <TestBlobHauler >:: is_congested_signal_sent( TEST_LANE_ID ) ) ;
444427 } ) ;
0 commit comments