Skip to content

Commit

Permalink
lp-gateway: Add more events for inbound and outbound message processing
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian committed Aug 16, 2024
1 parent 94e591b commit 01bc181
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 54 deletions.
5 changes: 4 additions & 1 deletion libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageHash>;
fn get_message_hash(&self) -> MessageHash;

/// Converts the message into a message proof type.
fn to_proof_message(&self) -> Self;
Expand Down
41 changes: 30 additions & 11 deletions pallets/liquidity-pools-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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);

Expand Down Expand Up @@ -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,
Expand All @@ -183,11 +206,6 @@ pub mod pallet {
recovery_router: [u8; 32],
messaging_router: T::RouterId,
},

/// A message has been processed.
ProcessedMessage {
message: GatewayMessage<T::Message, T::RouterId>,
},
}

/// Storage for routers.
Expand Down Expand Up @@ -616,10 +634,6 @@ pub mod pallet {
type Message = GatewayMessage<T::Message, T::RouterId>;

fn process(msg: Self::Message) -> (DispatchResult, Weight) {
Self::deposit_event(Event::<T>::ProcessedMessage {
message: msg.clone(),
});

match msg {
GatewayMessage::Inbound {
domain_address,
Expand All @@ -629,6 +643,11 @@ pub mod pallet {
GatewayMessage::Outbound { message, router_id } => {
let weight = LP_DEFENSIVE_WEIGHT;

Self::deposit_event(Event::<T>::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),
Expand Down
49 changes: 31 additions & 18 deletions pallets/liquidity-pools-gateway/src/message_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -94,17 +95,18 @@ impl<T: Config> InboundEntry<T> {
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,
}),
})
}
}

Expand Down Expand Up @@ -292,17 +294,6 @@ impl<T: Config> Pallet<T> {
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(
Expand Down Expand Up @@ -362,6 +353,8 @@ impl<T: Config> Pallet<T> {
Self::execute_post_voting_dispatch(message_hash, router_ids, expected_proof_count)?;

T::InboundMessageHandler::handle(domain_address, msg)?;

Self::deposit_event(Event::<T>::InboundMessageExecuted { message_hash })
}

Ok(())
Expand Down Expand Up @@ -428,7 +421,7 @@ impl<T: Config> Pallet<T> {
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<T> = InboundEntry::create(
submessage,
Expand All @@ -445,6 +438,8 @@ impl<T: Config> Pallet<T> {
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,
Expand All @@ -460,6 +455,24 @@ impl<T: Config> Pallet<T> {
(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::<T>::InboundProofProcessed {
message_hash,
router_id,
})
} else {
Self::deposit_event(Event::<T>::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(
Expand Down
13 changes: 7 additions & 6 deletions pallets/liquidity-pools-gateway/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,18 @@ impl LPMessage for Message {
Self::Pack(vec![])
}

fn get_message_hash(&self) -> Option<MessageHash> {
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()),
}
}

Expand Down
20 changes: 10 additions & 10 deletions pallets/liquidity-pools-gateway/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -1180,7 +1180,7 @@ mod implementations {
}
GatewayMessage::Outbound { message, .. } => match message {
Message::Proof(p) => {
assert_eq!(p, message_proof);
assert_eq!(p, message_hash);
}
_ => {}
},
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1445,7 +1445,7 @@ mod implementations {
assert_eq!(handler.times(), 1);

assert!(
PendingInboundEntries::<Runtime>::get(message_proof, router_id)
PendingInboundEntries::<Runtime>::get(message_hash, router_id)
.is_none()
);
});
Expand Down Expand Up @@ -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;
Expand All @@ -1512,7 +1512,7 @@ mod implementations {
);
SessionIdStore::<Runtime>::set(session_id);
PendingInboundEntries::<Runtime>::insert(
message_proof,
message_hash,
router_id,
InboundEntry::Proof(ProofEntry {
session_id,
Expand Down Expand Up @@ -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<Runtime> = ProofEntry {
session_id: 1,
Expand Down Expand Up @@ -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::<Runtime>::Message(MessageEntry {
session_id: 1,
Expand Down
17 changes: 9 additions & 8 deletions pallets/liquidity-pools/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,17 +562,18 @@ impl LPMessage for Message {
Message::Batch(BatchMessages::default())
}

fn get_message_hash(&self) -> Option<MessageHash> {
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 {
Expand Down

0 comments on commit 01bc181

Please sign in to comment.