From 01bc1816fbd352917cda6ab53e103b7e5393fd67 Mon Sep 17 00:00:00 2001 From: Cosmin Damian <17934949+cdamian@users.noreply.github.com> Date: Fri, 16 Aug 2024 22:24:27 +0300 Subject: [PATCH] lp-gateway: Add more events for inbound and outbound message processing --- libs/traits/src/liquidity_pools.rs | 5 +- pallets/liquidity-pools-gateway/src/lib.rs | 41 +++++++++++----- .../src/message_processing.rs | 49 ++++++++++++------- pallets/liquidity-pools-gateway/src/mock.rs | 13 ++--- pallets/liquidity-pools-gateway/src/tests.rs | 20 ++++---- pallets/liquidity-pools/src/message.rs | 17 ++++--- 6 files changed, 91 insertions(+), 54 deletions(-) diff --git a/libs/traits/src/liquidity_pools.rs b/libs/traits/src/liquidity_pools.rs index 1baede4c3e..5b274599b3 100644 --- a/libs/traits/src/liquidity_pools.rs +++ b/libs/traits/src/liquidity_pools.rs @@ -35,8 +35,11 @@ pub trait LPMessage: Sized { /// It's the identity message for composing messages with pack_with fn empty() -> Self; + /// Returns whether the message is a proof or not. + fn is_proof_message(&self) -> bool; + /// Retrieves the message hash, if the message is a proof type. - fn get_message_hash(&self) -> Option; + fn get_message_hash(&self) -> MessageHash; /// Converts the message into a message proof type. fn to_proof_message(&self) -> Self; diff --git a/pallets/liquidity-pools-gateway/src/lib.rs b/pallets/liquidity-pools-gateway/src/lib.rs index 531575bfd9..6c7ca03ba3 100644 --- a/pallets/liquidity-pools-gateway/src/lib.rs +++ b/pallets/liquidity-pools-gateway/src/lib.rs @@ -44,7 +44,10 @@ use sp_arithmetic::traits::{BaseArithmetic, EnsureAddAssign, One}; use sp_runtime::SaturatedConversion; use sp_std::{convert::TryInto, vec::Vec}; -use crate::{message_processing::InboundEntry, weights::WeightInfo}; +use crate::{ + message_processing::{InboundEntry, ProofEntry}, + weights::WeightInfo, +}; mod origin; pub use origin::*; @@ -63,7 +66,6 @@ mod tests; #[frame_support::pallet] pub mod pallet { use super::*; - use crate::message_processing::ProofEntry; const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); @@ -162,6 +164,27 @@ pub mod pallet { hook_address: [u8; 20], }, + /// An inbound message was processed. + InboundMessageProcessed { + message_hash: MessageHash, + router_id: T::RouterId, + }, + + /// An inbound message proof was processed. + InboundProofProcessed { + message_hash: MessageHash, + router_id: T::RouterId, + }, + + /// An inbound message was executed. + InboundMessageExecuted { message_hash: MessageHash }, + + /// An outbound message was sent. + OutboundMessageSent { + message_hash: MessageHash, + router_id: T::RouterId, + }, + /// Message recovery was executed. MessageRecoveryExecuted { message_hash: MessageHash, @@ -183,11 +206,6 @@ pub mod pallet { recovery_router: [u8; 32], messaging_router: T::RouterId, }, - - /// A message has been processed. - ProcessedMessage { - message: GatewayMessage, - }, } /// Storage for routers. @@ -616,10 +634,6 @@ pub mod pallet { type Message = GatewayMessage; fn process(msg: Self::Message) -> (DispatchResult, Weight) { - Self::deposit_event(Event::::ProcessedMessage { - message: msg.clone(), - }); - match msg { GatewayMessage::Inbound { domain_address, @@ -629,6 +643,11 @@ pub mod pallet { GatewayMessage::Outbound { message, router_id } => { let weight = LP_DEFENSIVE_WEIGHT; + Self::deposit_event(Event::::OutboundMessageSent { + message_hash: message.get_message_hash(), + router_id: router_id.clone(), + }); + match T::MessageSender::send(router_id, T::Sender::get(), message.serialize()) { Ok(_) => (Ok(()), weight), Err(e) => (Err(e), weight), diff --git a/pallets/liquidity-pools-gateway/src/message_processing.rs b/pallets/liquidity-pools-gateway/src/message_processing.rs index bc9742ee0a..c47e568ed0 100644 --- a/pallets/liquidity-pools-gateway/src/message_processing.rs +++ b/pallets/liquidity-pools-gateway/src/message_processing.rs @@ -15,7 +15,8 @@ use sp_runtime::DispatchError; use sp_std::vec::Vec; use crate::{ - message::GatewayMessage, Config, Error, Pallet, PendingInboundEntries, Routers, SessionIdStore, + message::GatewayMessage, Config, Error, Event, Pallet, PendingInboundEntries, Routers, + SessionIdStore, }; /// Type that holds the information needed for inbound message entries. @@ -94,17 +95,18 @@ impl InboundEntry { domain_address: DomainAddress, expected_proof_count: u32, ) -> Self { - match message.get_message_hash() { - None => InboundEntry::Message(MessageEntry { + if message.is_proof_message() { + InboundEntry::Message(MessageEntry { session_id, domain_address, message, expected_proof_count, - }), - Some(_) => InboundEntry::Proof(ProofEntry { + }) + } else { + InboundEntry::Proof(ProofEntry { session_id, current_count: 1, - }), + }) } } @@ -292,17 +294,6 @@ impl Pallet { Ok(expected_proof_count.saturated_into()) } - /// Gets the message proof for a message. - pub(crate) fn get_message_hash(message: T::Message) -> MessageHash { - match message.get_message_hash() { - None => message - .to_proof_message() - .get_message_hash() - .expect("message hash ensured by 'to_proof_message'"), - Some(proof) => proof, - } - } - /// Upserts an inbound entry for a particular message, increasing the /// relevant counts accordingly. pub(crate) fn upsert_pending_entry( @@ -362,6 +353,8 @@ impl Pallet { Self::execute_post_voting_dispatch(message_hash, router_ids, expected_proof_count)?; T::InboundMessageHandler::handle(domain_address, msg)?; + + Self::deposit_event(Event::::InboundMessageExecuted { message_hash }) } Ok(()) @@ -428,7 +421,7 @@ impl Pallet { return (Err(e.into()), weight.saturating_mul(count)); } - let message_hash = Self::get_message_hash(submessage.clone()); + let message_hash = submessage.get_message_hash(); let inbound_entry: InboundEntry = InboundEntry::create( submessage, @@ -445,6 +438,8 @@ impl Pallet { return (Err(e), weight.saturating_mul(count)); } + Self::deposit_processing_event(message.clone(), message_hash, router_id.clone()); + match Self::execute_if_requirements_are_met( message_hash, &router_ids, @@ -460,6 +455,24 @@ impl Pallet { (Ok(()), weight.saturating_mul(count)) } + fn deposit_processing_event( + message: T::Message, + message_hash: MessageHash, + router_id: T::RouterId, + ) { + if message.is_proof_message() { + Self::deposit_event(Event::::InboundProofProcessed { + message_hash, + router_id, + }) + } else { + Self::deposit_event(Event::::InboundMessageProcessed { + message_hash, + router_id, + }) + } + } + /// Retrieves the IDs of the routers set for a domain and queues the /// message and proofs accordingly. pub(crate) fn queue_outbound_message( diff --git a/pallets/liquidity-pools-gateway/src/mock.rs b/pallets/liquidity-pools-gateway/src/mock.rs index 8f5c2db475..acd3d052ce 100644 --- a/pallets/liquidity-pools-gateway/src/mock.rs +++ b/pallets/liquidity-pools-gateway/src/mock.rs @@ -102,17 +102,18 @@ impl LPMessage for Message { Self::Pack(vec![]) } - fn get_message_hash(&self) -> Option { - match self { - Message::Proof(p) => Some(p.clone()), - _ => None, - } + fn is_proof_message(&self) -> bool { + matches!(self, Message::Proof(..)) + } + + fn get_message_hash(&self) -> MessageHash { + MESSAGE_HASH } fn to_proof_message(&self) -> Self { match self { Message::Proof(_) => self.clone(), - _ => Message::Proof(MESSAGE_HASH), + _ => Message::Proof(self.get_message_hash()), } } diff --git a/pallets/liquidity-pools-gateway/src/tests.rs b/pallets/liquidity-pools-gateway/src/tests.rs index 455b5bdb32..b8ca112b5e 100644 --- a/pallets/liquidity-pools-gateway/src/tests.rs +++ b/pallets/liquidity-pools-gateway/src/tests.rs @@ -836,7 +836,7 @@ mod extrinsics { } #[test] - fn expected_message_proof_type() { + fn expected_message_hash_type() { new_test_ext().execute_with(|| { let domain_address = TEST_DOMAIN_ADDRESS; let session_id = 1; @@ -1166,7 +1166,7 @@ mod implementations { let domain = Domain::Evm(0); let sender = get_test_account_id(); let msg = Message::Simple; - let message_proof = msg.to_proof_message().get_message_hash().unwrap(); + let message_hash = msg.get_message_hash(); assert_ok!(LiquidityPoolsGateway::set_routers( RuntimeOrigin::root(), @@ -1180,7 +1180,7 @@ mod implementations { } GatewayMessage::Outbound { message, .. } => match message { Message::Proof(p) => { - assert_eq!(p, message_proof); + assert_eq!(p, message_hash); } _ => {} }, @@ -1416,7 +1416,7 @@ mod implementations { fn success() { new_test_ext().execute_with(|| { let message = Message::Simple; - let message_proof = message.to_proof_message().get_message_hash().unwrap(); + let message_hash = message.get_message_hash(); let session_id = 1; let domain_address = DomainAddress::Evm(1, H160::repeat_byte(1)); let router_id = ROUTER_ID_1; @@ -1445,7 +1445,7 @@ mod implementations { assert_eq!(handler.times(), 1); assert!( - PendingInboundEntries::::get(message_proof, router_id) + PendingInboundEntries::::get(message_hash, router_id) .is_none() ); }); @@ -1494,10 +1494,10 @@ mod implementations { } #[test] - fn expected_message_proof_type() { + fn expected_message_hash_type() { new_test_ext().execute_with(|| { let message = Message::Simple; - let message_proof = message.to_proof_message().get_message_hash().unwrap(); + let message_hash = message.get_message_hash(); let session_id = 1; let domain_address = DomainAddress::Evm(1, H160::repeat_byte(1)); let router_id = ROUTER_ID_1; @@ -1512,7 +1512,7 @@ mod implementations { ); SessionIdStore::::set(session_id); PendingInboundEntries::::insert( - message_proof, + message_hash, router_id, InboundEntry::Proof(ProofEntry { session_id, @@ -3844,7 +3844,7 @@ mod implementations { } #[test] - fn expected_message_proof_type() { + fn expected_message_hash_type() { new_test_ext().execute_with(|| { let inbound_entry: InboundEntry = ProofEntry { session_id: 1, @@ -4221,7 +4221,7 @@ mod inbound_entry { } #[test] - fn expected_message_proof_type() { + fn expected_message_hash_type() { new_test_ext().execute_with(|| { let mut inbound_entry = InboundEntry::::Message(MessageEntry { session_id: 1, diff --git a/pallets/liquidity-pools/src/message.rs b/pallets/liquidity-pools/src/message.rs index 3017e96800..1b52ca3e23 100644 --- a/pallets/liquidity-pools/src/message.rs +++ b/pallets/liquidity-pools/src/message.rs @@ -562,17 +562,18 @@ impl LPMessage for Message { Message::Batch(BatchMessages::default()) } - fn get_message_hash(&self) -> Option { - match self { - Message::MessageProof { hash } => Some(*hash), - _ => None, - } + fn is_proof_message(&self) -> bool { + matches!(self, Message::MessageProof { .. }) } - fn to_proof_message(&self) -> Self { - let hash = keccak_256(&LPMessage::serialize(self)); + fn get_message_hash(&self) -> MessageHash { + keccak_256(&LPMessage::serialize(self)) + } - Message::MessageProof { hash } + fn to_proof_message(&self) -> Self { + Message::MessageProof { + hash: self.get_message_hash(), + } } fn initiate_recovery_message(hash: [u8; 32], router: [u8; 32]) -> Self {