@@ -240,58 +240,6 @@ where
240240 . map_err ( Error :: UnableToSubmitCrossDomainMessage )
241241}
242242
243- fn should_relay_outbox_message < Client , Block , CBlock > (
244- api : & ApiRef < ' _ , Client :: Api > ,
245- best_hash : Block :: Hash ,
246- msg : & BlockMessageWithStorageKey ,
247- ) -> bool
248- where
249- Block : BlockT ,
250- CBlock : BlockT ,
251- Client : ProvideRuntimeApi < Block > + HeaderBackend < Block > ,
252- Client :: Api : RelayerApi < Block , NumberFor < Block > , NumberFor < CBlock > , CBlock :: Hash > ,
253- {
254- let id = msg. id ( ) ;
255- match api. should_relay_outbox_message ( best_hash, msg. dst_chain_id , id) {
256- Ok ( val) => val,
257- Err ( err) => {
258- tracing:: error!(
259- target: LOG_TARGET ,
260- ?err,
261- "Failed to fetch validity of outbox message {id:?} for domain {0:?}" ,
262- msg. dst_chain_id
263- ) ;
264- false
265- }
266- }
267- }
268-
269- fn should_relay_inbox_responses_message < Client , Block , CBlock > (
270- api : & ApiRef < ' _ , Client :: Api > ,
271- best_hash : Block :: Hash ,
272- msg : & BlockMessageWithStorageKey ,
273- ) -> bool
274- where
275- Block : BlockT ,
276- CBlock : BlockT ,
277- Client : ProvideRuntimeApi < Block > + HeaderBackend < Block > ,
278- Client :: Api : RelayerApi < Block , NumberFor < Block > , NumberFor < CBlock > , CBlock :: Hash > ,
279- {
280- let id = msg. id ( ) ;
281- match api. should_relay_inbox_message_response ( best_hash, msg. dst_chain_id , id) {
282- Ok ( val) => val,
283- Err ( err) => {
284- tracing:: error!(
285- target: LOG_TARGET ,
286- ?err,
287- "Failed to fetch validity of inbox message response {id:?} for domain {0:?}" ,
288- msg. dst_chain_id
289- ) ;
290- false
291- }
292- }
293- }
294-
295243// Fetch the XDM at the given block and filter any already relayed XDM according to the best block
296244fn fetch_and_filter_messages < Client , Block , CClient , CBlock > (
297245 client : & Arc < Client > ,
@@ -306,32 +254,17 @@ where
306254 Client : ProvideRuntimeApi < Block > + HeaderBackend < Block > ,
307255 Client :: Api : RelayerApi < Block , NumberFor < Block > , NumberFor < CBlock > , CBlock :: Hash > ,
308256{
309- let mut msgs =
310- if is_relayer_api_version_available :: < _ , Block , CBlock > ( client, 3 , fetch_message_at) {
311- fetch_messages :: < _ , _ , Block , CBlock > (
312- & * * consensus_client,
313- client,
314- fetch_message_at,
315- self_chain_id,
316- )
317- . map_err ( |_| Error :: FetchAssignedMessages ) ?
318- } else {
319- client
320- . runtime_api ( )
321- . block_messages ( fetch_message_at)
322- . map_err ( |_| Error :: FetchAssignedMessages ) ?
323- } ;
324-
325- let api = client. runtime_api ( ) ;
326- let best_hash = client. info ( ) . best_hash ;
327- msgs. outbox
328- . retain ( |msg| should_relay_outbox_message :: < Client , Block , CBlock > ( & api, best_hash, msg) ) ;
329-
330- msgs. inbox_responses . retain ( |msg| {
331- should_relay_inbox_responses_message :: < Client , Block , CBlock > ( & api, best_hash, msg)
332- } ) ;
257+ // return no messages for previous relayer version
258+ if !is_relayer_api_version_available :: < _ , Block , CBlock > ( client, 3 , fetch_message_at) {
259+ return Ok ( BlockMessagesWithStorageKey :: default ( ) ) ;
260+ }
333261
334- Ok ( msgs)
262+ fetch_messages :: < _ , _ , Block , CBlock > (
263+ & * * consensus_client,
264+ client,
265+ fetch_message_at,
266+ self_chain_id,
267+ )
335268}
336269
337270// A helper struct used when constructing XDM proof
@@ -534,6 +467,48 @@ where
534467 }
535468}
536469
470+ fn filter_block_messages < Client , Block , CBlock > (
471+ api : & ApiRef < ' _ , Client :: Api > ,
472+ best_hash : Block :: Hash ,
473+ query : BlockMessagesQuery ,
474+ messages : & mut BlockMessagesWithStorageKey ,
475+ ) -> Result < ( ) , Error >
476+ where
477+ Block : BlockT ,
478+ CBlock : BlockT ,
479+ Client : ProvideRuntimeApi < Block > + HeaderBackend < Block > ,
480+ Client :: Api : RelayerApi < Block , NumberFor < Block > , NumberFor < CBlock > , CBlock :: Hash > ,
481+ {
482+ let BlockMessagesQuery {
483+ chain_id,
484+ channel_id,
485+ outbox_from,
486+ inbox_responses_from,
487+ } = query;
488+ let maybe_outbox_nonce =
489+ api. should_relay_outbox_messages ( best_hash, chain_id, channel_id, outbox_from) ?;
490+ let maybe_inbox_response_nonce = api. should_relay_inbox_message_responses (
491+ best_hash,
492+ chain_id,
493+ channel_id,
494+ inbox_responses_from,
495+ ) ?;
496+
497+ if let Some ( nonce) = maybe_outbox_nonce {
498+ messages. outbox . retain ( |msg| msg. nonce >= nonce)
499+ } else {
500+ messages. outbox . clear ( )
501+ }
502+
503+ if let Some ( nonce) = maybe_inbox_response_nonce {
504+ messages. inbox_responses . retain ( |msg| msg. nonce >= nonce)
505+ } else {
506+ messages. inbox_responses . clear ( ) ;
507+ }
508+
509+ Ok ( ( ) )
510+ }
511+
537512// Fetch the unprocessed XDMs at a given block
538513fn fetch_messages < Backend , Client , Block , CBlock > (
539514 backend : & Backend ,
@@ -580,6 +555,8 @@ where
580555 queries
581556 } ;
582557
558+ let best_hash = client. info ( ) . best_hash ;
559+ let runtime_api_best = client. runtime_api ( ) ;
583560 let total_messages = queries
584561 . into_iter ( )
585562 . filter_map ( |query| {
@@ -588,9 +565,19 @@ where
588565 channel_id,
589566 ..
590567 } = query;
591- let messages = runtime_api
592- . block_messages_with_query ( fetch_message_at, query)
568+ let mut messages = runtime_api
569+ . block_messages_with_query ( fetch_message_at, query. clone ( ) )
593570 . ok ( ) ?;
571+
572+ // filter messages with best hash
573+ filter_block_messages :: < Client , _ , CBlock > (
574+ & runtime_api_best,
575+ best_hash,
576+ query,
577+ & mut messages,
578+ )
579+ . ok ( ) ?;
580+
594581 if !messages. outbox . is_empty ( )
595582 && let Some ( max_nonce) = messages. outbox . iter ( ) . map ( |key| key. nonce ) . max ( )
596583 {
0 commit comments