@@ -16,11 +16,13 @@ use alloc::collections::BTreeMap;
1616use frame_support:: ensure;
1717use sp_messenger:: endpoint:: { EndpointHandler , EndpointRequest , EndpointResponse } ;
1818use sp_messenger:: messages:: {
19- BlockMessageWithStorageKey , BlockMessagesWithStorageKey , ChainId , ChannelOpenParamsV1 , Message ,
20- MessageId , MessageWeightTag , PayloadV1 , ProtocolMessageRequest , ProtocolMessageResponse ,
21- RequestResponse , VersionedPayload ,
19+ BlockMessageWithStorageKey , BlockMessagesQuery , BlockMessagesWithStorageKey , ChainId ,
20+ ChannelOpenParamsV1 , Message , MessageId , MessageKey , MessageWeightTag , PayloadV1 ,
21+ ProtocolMessageRequest , ProtocolMessageResponse , RequestResponse , VersionedPayload ,
2222} ;
23- use sp_runtime:: traits:: Get ;
23+
24+ use sp_messenger:: MAX_FUTURE_ALLOWED_NONCES ;
25+ use sp_runtime:: traits:: { Get , One } ;
2426use sp_runtime:: { ArithmeticError , DispatchError , DispatchResult } ;
2527#[ cfg( feature = "std" ) ]
2628use std:: collections:: BTreeMap ;
@@ -387,11 +389,23 @@ impl<T: Config> Pallet<T> {
387389 Ok ( ( ) )
388390 }
389391
390- pub fn get_block_messages ( ) -> BlockMessagesWithStorageKey {
391- let inbox_responses_weight_tags: BTreeMap < ( ChainId , MessageId ) , MessageWeightTag > =
392- InboxResponseMessageWeightTags :: < T > :: iter ( ) . collect ( ) ;
393- let outbox_weight_tags: BTreeMap < ( ChainId , MessageId ) , MessageWeightTag > =
394- OutboxMessageWeightTags :: < T > :: iter ( ) . collect ( ) ;
392+ pub fn get_block_messages ( query : BlockMessagesQuery ) -> BlockMessagesWithStorageKey {
393+ let BlockMessagesQuery {
394+ chain_id,
395+ channel_id,
396+ outbox_from,
397+ inbox_responses_from,
398+ } = query;
399+
400+ let inbox_responses_weight_tags = Self :: get_weight_tags (
401+ ( chain_id, channel_id, inbox_responses_from) ,
402+ InboxResponseMessageWeightTags :: < T > :: get,
403+ ) ;
404+
405+ let outbox_weight_tags = Self :: get_weight_tags (
406+ ( chain_id, channel_id, outbox_from) ,
407+ OutboxMessageWeightTags :: < T > :: get,
408+ ) ;
395409
396410 if outbox_weight_tags. is_empty ( ) && inbox_responses_weight_tags. is_empty ( ) {
397411 return Default :: default ( ) ;
@@ -436,4 +450,31 @@ impl<T: Config> Pallet<T> {
436450
437451 messages_with_storage_key
438452 }
453+
454+ fn get_weight_tags < WTG > (
455+ from : MessageKey ,
456+ weight_tag_getter : WTG ,
457+ ) -> BTreeMap < ( ChainId , MessageId ) , MessageWeightTag >
458+ where
459+ WTG : Fn ( ( ChainId , MessageId ) ) -> Option < MessageWeightTag > ,
460+ {
461+ let ( chain_id, channel_id, mut nonce) = from;
462+ let mut weight_tags = BTreeMap :: new ( ) ;
463+ while weight_tags. len ( ) as u32 <= MAX_FUTURE_ALLOWED_NONCES {
464+ match weight_tag_getter ( ( chain_id, ( channel_id, nonce) ) ) {
465+ // if the nonce is already processed, short circuit and return
466+ None => return weight_tags,
467+ Some ( weight_tag) => {
468+ weight_tags. insert ( ( chain_id, ( channel_id, nonce) ) , weight_tag) ;
469+ }
470+ } ;
471+
472+ nonce = match nonce. checked_add ( One :: one ( ) ) {
473+ None => return weight_tags,
474+ Some ( nonce) => nonce,
475+ }
476+ }
477+
478+ weight_tags
479+ }
439480}
0 commit comments