@@ -98,12 +98,6 @@ pub(crate) enum CloseChannelBy<AccountId> {
9898 Sudo ,
9999}
100100
101- /// Parameters for a new channel between two chains.
102- #[ derive( Default , Debug , Encode , Decode , Clone , Eq , PartialEq , TypeInfo , Copy ) ]
103- pub struct InitiateChannelParams {
104- pub max_outgoing_messages : u32 ,
105- }
106-
107101/// Hold identifier trait for messenger specific balance holds
108102pub trait HoldIdentifier < T : Config > {
109103 fn messenger_channel ( ) -> FungibleHoldId < T > ;
@@ -114,8 +108,8 @@ mod pallet {
114108 use crate :: weights:: WeightInfo ;
115109 use crate :: {
116110 BalanceOf , ChainAllowlistUpdate , Channel , ChannelId , ChannelState , CloseChannelBy ,
117- FeeModel , HoldIdentifier , InitiateChannelParams , Nonce , OutboxMessageResult , StateRootOf ,
118- ValidatedRelayMessage , U256 ,
111+ FeeModel , HoldIdentifier , Nonce , OutboxMessageResult , StateRootOf , ValidatedRelayMessage ,
112+ U256 ,
119113 } ;
120114 #[ cfg( not( feature = "std" ) ) ]
121115 use alloc:: boxed:: Box ;
@@ -191,6 +185,8 @@ mod pallet {
191185 type DomainRegistration : DomainRegistration ;
192186 /// Channels fee model
193187 type ChannelFeeModel : Get < FeeModel < BalanceOf < Self > > > ;
188+ /// Maximum outgoing messages from a given channel
189+ type MaxOutgoingMessages : Get < u32 > ;
194190 }
195191
196192 /// Pallet messenger used to communicate between chains and other blockchains.
@@ -243,25 +239,21 @@ mod pallet {
243239 /// Used by the dst_chains to verify the message response.
244240 #[ pallet:: storage]
245241 #[ pallet:: getter( fn inbox_responses) ]
246- pub ( super ) type InboxResponses < T : Config > = CountedStorageMap <
247- _ ,
248- Identity ,
249- ( ChainId , ChannelId , Nonce ) ,
250- Message < BalanceOf < T > > ,
251- OptionQuery ,
252- > ;
242+ pub ( super ) type InboxResponses < T : Config > =
243+ StorageMap < _ , Identity , ( ChainId , ChannelId , Nonce ) , Message < BalanceOf < T > > , OptionQuery > ;
253244
254245 /// Stores the outgoing messages that are awaiting message responses from the dst_chain.
255246 /// Messages are processed in the outbox nonce order of chain's channel.
256247 #[ pallet:: storage]
257248 #[ pallet:: getter( fn outbox) ]
258- pub ( super ) type Outbox < T : Config > = CountedStorageMap <
259- _ ,
260- Identity ,
261- ( ChainId , ChannelId , Nonce ) ,
262- Message < BalanceOf < T > > ,
263- OptionQuery ,
264- > ;
249+ pub ( super ) type Outbox < T : Config > =
250+ StorageMap < _ , Identity , ( ChainId , ChannelId , Nonce ) , Message < BalanceOf < T > > , OptionQuery > ;
251+
252+ /// Stores the outgoing messages count that are awaiting message responses from the dst_chain.
253+ #[ pallet:: storage]
254+ #[ pallet:: getter( fn outbox_message_count) ]
255+ pub ( super ) type OutboxMessageCount < T : Config > =
256+ StorageMap < _ , Identity , ( ChainId , ChannelId ) , u32 , ValueQuery > ;
265257
266258 /// A temporary storage for storing decoded outbox response message between `pre_dispatch_relay_message_response`
267259 /// and `relay_message_response`.
@@ -545,6 +537,15 @@ mod pallet {
545537
546538 /// Invalid channel reserve fee
547539 InvalidChannelReserveFee ,
540+
541+ /// Invalid max outgoing messages
542+ InvalidMaxOutgoingMessages ,
543+
544+ /// Message count overflow
545+ MessageCountOverflow ,
546+
547+ /// Message count underflow
548+ MessageCountUnderflow ,
548549 }
549550
550551 #[ pallet:: call]
@@ -554,11 +555,7 @@ mod pallet {
554555 /// Channel is set to initiated and do not accept or receive any messages.
555556 #[ pallet:: call_index( 0 ) ]
556557 #[ pallet:: weight( T :: WeightInfo :: initiate_channel( ) ) ]
557- pub fn initiate_channel (
558- origin : OriginFor < T > ,
559- dst_chain_id : ChainId ,
560- params : InitiateChannelParams ,
561- ) -> DispatchResult {
558+ pub fn initiate_channel ( origin : OriginFor < T > , dst_chain_id : ChainId ) -> DispatchResult {
562559 let owner = ensure_signed ( origin) ?;
563560
564561 // reserve channel open fees
@@ -575,7 +572,7 @@ mod pallet {
575572
576573 // initiate the channel config
577574 let channel_open_params = ChannelOpenParams {
578- max_outgoing_messages : params . max_outgoing_messages ,
575+ max_outgoing_messages : T :: MaxOutgoingMessages :: get ( ) ,
579576 fee_model : T :: ChannelFeeModel :: get ( ) ,
580577 } ;
581578 let channel_id = Self :: do_init_channel (
@@ -907,8 +904,11 @@ mod pallet {
907904 // loop through channels in descending order until open channel is found.
908905 // we always prefer latest opened channel.
909906 while let Some ( channel_id) = next_channel_id. checked_sub ( ChannelId :: one ( ) ) {
907+ let message_count = OutboxMessageCount :: < T > :: get ( ( dst_chain_id, channel_id) ) ;
910908 if let Some ( channel) = Channels :: < T > :: get ( dst_chain_id, channel_id) {
911- if channel. state == ChannelState :: Open {
909+ if channel. state == ChannelState :: Open
910+ && message_count < channel. max_outgoing_messages
911+ {
912912 return Some ( ( channel_id, channel. fee ) ) ;
913913 }
914914 }
@@ -1006,6 +1006,12 @@ mod pallet {
10061006 Error :: <T >:: InvalidChain ,
10071007 ) ;
10081008
1009+ // ensure max outgoing messages is at least 1
1010+ ensure ! (
1011+ init_params. max_outgoing_messages >= 1u32 ,
1012+ Error :: <T >:: InvalidMaxOutgoingMessages
1013+ ) ;
1014+
10091015 // If the channel owner is in this chain then the channel reserve fee
10101016 // must not be empty
10111017 ensure ! (
0 commit comments