From 004e7c1bd8ae5649e2b76d0851bebcda1a71df93 Mon Sep 17 00:00:00 2001 From: Cosmin Damian <17934949+cdamian@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:59:35 +0300 Subject: [PATCH] wip --- pallets/liquidity-pools-gateway/src/lib.rs | 10 ++--- .../src/message_processing.rs | 41 ++++++++++--------- pallets/liquidity-pools-gateway/src/tests.rs | 4 +- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/pallets/liquidity-pools-gateway/src/lib.rs b/pallets/liquidity-pools-gateway/src/lib.rs index c8ee139785..f2a7925f46 100644 --- a/pallets/liquidity-pools-gateway/src/lib.rs +++ b/pallets/liquidity-pools-gateway/src/lib.rs @@ -277,8 +277,8 @@ pub mod pallet { /// Inbound domain session not found. InboundDomainSessionNotFound, - /// The router that sent the inbound message is unknown. - UnknownInboundMessageRouter, + /// Unknown router. + UnknownRouter, /// The router that sent the message is not the first one. MessageExpectedFromFirstRouter, @@ -443,7 +443,7 @@ pub mod pallet { match PackedMessage::::take((&sender, &destination)) { Some(msg) if msg.submessages().is_empty() => Ok(()), //No-op - Some(message) => Self::queue_message(destination, message), + Some(message) => Self::queue_outbound_message(destination, message), None => Err(Error::::MessagePackingNotStarted.into()), } } @@ -467,7 +467,7 @@ pub mod pallet { ensure!( routers.iter().any(|x| x == &router_id), - Error::::UnknownInboundMessageRouter + Error::::UnknownRouter ); PendingInboundEntries::::try_mutate( @@ -513,7 +513,7 @@ pub mod pallet { PackedMessage::::mutate((&from, destination.clone()), |batch| match batch { Some(batch) => batch.pack_with(message), - None => Self::queue_message(destination, message), + None => Self::queue_outbound_message(destination, message), }) } } diff --git a/pallets/liquidity-pools-gateway/src/message_processing.rs b/pallets/liquidity-pools-gateway/src/message_processing.rs index 3edfa10774..acf99a5572 100644 --- a/pallets/liquidity-pools-gateway/src/message_processing.rs +++ b/pallets/liquidity-pools-gateway/src/message_processing.rs @@ -41,7 +41,7 @@ pub enum InboundEntry { #[derive(Clone)] pub struct InboundProcessingInfo { domain_address: DomainAddress, - routers: BoundedVec, + router_ids: Vec, current_session_id: T::SessionId, expected_proof_count_per_message: u32, } @@ -49,10 +49,10 @@ pub struct InboundProcessingInfo { impl Pallet { /// Calculates and returns the proof count required for processing one /// inbound message. - fn get_expected_proof_count() -> Result { - let routers = Routers::::get(); + fn get_expected_proof_count(domain: Domain) -> Result { + let routers_count = T::RouterId::for_domain(domain).len(); - let expected_proof_count = routers.len().ensure_sub(1)?; + let expected_proof_count = routers_count.ensure_sub(1)?; Ok(expected_proof_count as u32) } @@ -96,17 +96,17 @@ impl Pallet { router_id: &T::RouterId, inbound_entry: &InboundEntry, ) -> DispatchResult { - let routers = inbound_processing_info.routers.clone(); + let router_ids = inbound_processing_info.router_ids.clone(); ensure!( - routers.iter().any(|x| x == router_id), - Error::::UnknownInboundMessageRouter + router_ids.iter().any(|x| x == router_id), + Error::::UnknownRouter ); match inbound_entry { InboundEntry::Message { .. } => { ensure!( - routers.get(0) == Some(&router_id), + router_ids.get(0) == Some(&router_id), Error::::MessageExpectedFromFirstRouter ); @@ -114,7 +114,7 @@ impl Pallet { } InboundEntry::Proof { .. } => { ensure!( - routers.get(0) != Some(&router_id), + router_ids.get(0) != Some(&router_id), Error::::ProofNotExpectedFromFirstRouter ); @@ -204,12 +204,12 @@ impl Pallet { let mut message = None; let mut votes = 0; - for router in &inbound_processing_info.routers { + for router_id in &inbound_processing_info.router_ids { weight.saturating_accrue(T::DbWeight::get().reads(1)); match PendingInboundEntries::::get( inbound_processing_info.current_session_id, - (message_proof, router), + (message_proof, router_id), ) { // We expected one InboundEntry for each router, if that's not the case, // we can return. @@ -242,12 +242,12 @@ impl Pallet { message_proof: Proof, weight: &mut Weight, ) -> DispatchResult { - for router in &inbound_processing_info.routers { + for router_id in &inbound_processing_info.router_ids { weight.saturating_accrue(T::DbWeight::get().writes(1)); match PendingInboundEntries::::try_mutate( inbound_processing_info.current_session_id, - (message_proof, router), + (message_proof, router_id), |storage_entry| match storage_entry { None => Err(Error::::PendingInboundEntryNotFound.into()), Some(stored_inbound_entry) => match stored_inbound_entry { @@ -292,10 +292,10 @@ impl Pallet { /// Retrieves the information required for processing an inbound /// message. fn get_inbound_processing_info( - domain_address: DomainAddress, + domain: Domain, weight: &mut Weight, ) -> Result, DispatchError> { - let routers = Routers::::get(); + let router_ids = T::RouterId::for_domain(domain.clone()); weight.saturating_accrue(T::DbWeight::get().reads(1)); @@ -303,13 +303,13 @@ impl Pallet { weight.saturating_accrue(T::DbWeight::get().reads(1)); - let expected_proof_count = Self::get_expected_proof_count()?; + let expected_proof_count = Self::get_expected_proof_count(domain)?; weight.saturating_accrue(T::DbWeight::get().reads(1)); Ok(InboundProcessingInfo { domain_address, - routers, + router_ids, current_session_id, expected_proof_count_per_message: expected_proof_count, }) @@ -325,7 +325,7 @@ impl Pallet { let mut weight = Default::default(); let inbound_processing_info = - match Self::get_inbound_processing_info(domain_address.clone(), &mut weight) { + match Self::get_inbound_processing_info(domain_address.domain(), &mut weight) { Ok(i) => i, Err(e) => return (Err(e), weight), }; @@ -392,7 +392,10 @@ impl Pallet { /// Retrieves the IDs of the routers set for a domain and queues the /// message and proofs accordingly. - pub(crate) fn queue_message(destination: Domain, message: T::Message) -> DispatchResult { + pub(crate) fn queue_outbound_message( + destination: Domain, + message: T::Message, + ) -> DispatchResult { let router_ids = T::RouterId::for_domain(destination); let message_proof = message.to_message_proof(); diff --git a/pallets/liquidity-pools-gateway/src/tests.rs b/pallets/liquidity-pools-gateway/src/tests.rs index 9fddd7e154..1fcbb1a7fd 100644 --- a/pallets/liquidity-pools-gateway/src/tests.rs +++ b/pallets/liquidity-pools-gateway/src/tests.rs @@ -884,7 +884,7 @@ mod message_processor_impl { InboundMessageSessions::::insert(domain_address.domain(), session_id); let (res, _) = LiquidityPoolsGateway::process(gateway_message); - assert_noop!(res, Error::::UnknownInboundMessageRouter); + assert_noop!(res, Error::::UnknownRouter); }); } @@ -2967,7 +2967,7 @@ mod execute_message_recovery { MESSAGE_PROOF, router_id_2 ), - Error::::UnknownInboundMessageRouter + Error::::UnknownRouter ); }); }