From ce251fd6a31a3b24cb94f0a824bcf86e074ca8d1 Mon Sep 17 00:00:00 2001 From: poisonphang <17688291+PoisonPhang@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:41:31 -0500 Subject: [PATCH 1/2] feat(ucs01-relay): emit event on pfm hop --- cosmwasm/ucs01-relay-api/src/middleware.rs | 23 +++++++++++++++++++++- cosmwasm/ucs01-relay/src/ibc.rs | 3 +-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/cosmwasm/ucs01-relay-api/src/middleware.rs b/cosmwasm/ucs01-relay-api/src/middleware.rs index 048711925d..9720f778f3 100644 --- a/cosmwasm/ucs01-relay-api/src/middleware.rs +++ b/cosmwasm/ucs01-relay-api/src/middleware.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::{Addr, Binary, IbcPacket, IbcTimeout}; +use cosmwasm_std::{Addr, Binary, Event, IbcPacket, IbcTimeout}; use serde::{Deserialize, Serialize}; use thiserror::Error; use unionlabs::{ @@ -8,8 +8,19 @@ use unionlabs::{ pub const DEFAULT_PFM_TIMEOUT: &str = "1m"; pub const DEFAULT_PFM_RETRIES: u8 = 0; + pub const PFM_MODULE_NAME: &str = "packetforwardmiddleware"; +pub const PFM_ERROR_EVENT: &str = "packet_forward_error"; +pub const PFM_HOP_EVENT: &str = "packet_forward_hop"; + +pub const RECV_SEQUENCE_ATTR: &str = "recv_sequence"; +pub const SENT_SEQUENCE_ATTR: &str = "sent_sequence"; +pub const DEST_CHANNEL_ATTR: &str = "dest_channel"; +pub const DEST_PORT_ATTR: &str = "dest_port"; +pub const SRC_CHANNEL_ATTR: &str = "src_channel"; +pub const SRC_PORT_ATTR: &str = "src_port"; + #[derive(Error, Debug, PartialEq)] pub enum MiddlewareError { #[error("{0}")] @@ -87,6 +98,16 @@ impl InFlightPfmPacket { forward_port_id, } } + + pub fn create_hop_event(&self, sent_sequence: u64) -> Event { + Event::new(PFM_HOP_EVENT) + .add_attribute(RECV_SEQUENCE_ATTR, self.packet_sequence.to_string()) + .add_attribute(DEST_CHANNEL_ATTR, self.forward_channel_id.clone()) + .add_attribute(DEST_PORT_ATTR, self.forward_port_id.clone()) + .add_attribute(SENT_SEQUENCE_ATTR, sent_sequence.to_string()) + .add_attribute(SRC_CHANNEL_ATTR, self.packet_src_channel_id.clone()) + .add_attribute(SRC_PORT_ATTR, self.packet_src_port_id.clone()) + } } #[derive(Clone, PartialEq, Debug, Serialize, Deserialize)] diff --git a/cosmwasm/ucs01-relay/src/ibc.rs b/cosmwasm/ucs01-relay/src/ibc.rs index f03768871f..f4fa948274 100644 --- a/cosmwasm/ucs01-relay/src/ibc.rs +++ b/cosmwasm/ucs01-relay/src/ibc.rs @@ -83,8 +83,7 @@ pub fn reply( .save(deps.storage, refund_packet_key.clone(), &in_flight_packet) .expect("infallible update"); - Ok(Response::new() - .add_attribute("pfm_store_inclusion", format!("{refund_packet_key:?}"))) + Ok(Response::new().add_event(in_flight_packet.create_hop_event(send_res.sequence))) } (_, result) => Err(ContractError::UnknownReply { id: reply.id, From 76c523ab292374f31dd067adb8eba0546355d68d Mon Sep 17 00:00:00 2001 From: poisonphang <17688291+PoisonPhang@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:41:53 -0500 Subject: [PATCH 2/2] chore(ucs01-relay): clean up existing pfm events --- cosmwasm/ucs01-relay/src/protocol.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cosmwasm/ucs01-relay/src/protocol.rs b/cosmwasm/ucs01-relay/src/protocol.rs index be40cc9710..00137dcffd 100644 --- a/cosmwasm/ucs01-relay/src/protocol.rs +++ b/cosmwasm/ucs01-relay/src/protocol.rs @@ -10,7 +10,10 @@ use protos::{ use sha2::{Digest, Sha256}; use token_factory_api::TokenFactoryMsg; use ucs01_relay_api::{ - middleware::{InFlightPfmPacket, Memo, MiddlewareError, PacketForward, PacketForwardError}, + middleware::{ + InFlightPfmPacket, Memo, MiddlewareError, PacketForward, PacketForwardError, + PFM_ERROR_EVENT, + }, protocol::{TransferProtocol, ATTR_ERROR, ATTR_SUCCESS, IBC_SEND_ID}, types::{ make_foreign_denom, DenomOrigin, EncodingError, GenericAck, Ics20Ack, Ics20Packet, @@ -554,7 +557,7 @@ impl<'a> TransferProtocol for Ics20Protocol<'a> { return IbcReceiveResponse::new( Binary::try_from(Self::ack_failure(e.to_string())).expect("impossible"), ) - .add_event(Event::new("forward_err").add_attribute("error", e.to_string())) + .add_event(Event::new(PFM_ERROR_EVENT).add_attribute("error", e.to_string())) } }; @@ -581,7 +584,8 @@ impl<'a> TransferProtocol for Ics20Protocol<'a> { // TODO: persist full memo let memo = match forward.next { - Some(next) => serde_json_wasm::to_string(&Memo::Forward { forward: *next }).unwrap(), + Some(next) => serde_json_wasm::to_string(&Memo::Forward { forward: *next }) + .expect("can convert pfm memo to json string"), None => "".to_owned(), }; @@ -920,7 +924,7 @@ impl<'a> TransferProtocol for Ucs01Protocol<'a> { return IbcReceiveResponse::new( Binary::try_from(Self::ack_failure(e.to_string())).expect("impossible"), ) - .add_event(Event::new("forward_err").add_attribute("error", e.to_string())) + .add_event(Event::new(PFM_ERROR_EVENT).add_attribute("error", e.to_string())) } }; @@ -947,7 +951,8 @@ impl<'a> TransferProtocol for Ucs01Protocol<'a> { // TODO: persist full memo let memo = match forward.next { - Some(next) => serde_json_wasm::to_string(&Memo::Forward { forward: *next }).unwrap(), + Some(next) => serde_json_wasm::to_string(&Memo::Forward { forward: *next }) + .expect("can convert pfm memo to json string"), None => "".to_owned(), };