Skip to content

Commit

Permalink
feat(ucs01-relay): create pfm events st hubble can track outgoing hop…
Browse files Browse the repository at this point in the history
… packets (#2057)

- Add event for PFM hops (expected format below)
- Cleanup existing PFM events
- Remove uses of unwrap relevant to PFM

## PFM Hop Event Structure
```
packet_forward_hop:
  - recv_sequence: <u64>
  - dest_channel: <String>
  - dest_port: <String>
  - sent_sequence: <u64>
  - src_channel: <String>
  - src_port: <String>
```

## Real Example
Osmosis -> Union -> Ethereum
```
- attributes:
  - index: true
    key: _contract_address
    value: union1uvagqv5qy0d6udhj3wq74jxelqs8nl7rxdun4fdtymhv24d4zrcs9n97rr
  - index: true
    key: recv_sequence
    value: "1"
  - index: true
    key: dest_channel
    value: channel-0
  - index: true
    key: dest_port
    value: wasm.union1uvagqv5qy0d6udhj3wq74jxelqs8nl7rxdun4fdtymhv24d4zrcs9n97rr
  - index: true
    key: sent_sequence
    value: "1"
  - index: true
    key: src_channel
    value: channel-0
  - index: true
    key: src_port
    value: transfer
  - index: true
    key: msg_index
    value: "0"
  type: wasm-packet_forward_hop
  ```
  • Loading branch information
PoisonPhang authored Jun 7, 2024
2 parents e795c11 + 76c523a commit b12c744
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
23 changes: 22 additions & 1 deletion cosmwasm/ucs01-relay-api/src/middleware.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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}")]
Expand Down Expand Up @@ -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)]
Expand Down
3 changes: 1 addition & 2 deletions cosmwasm/ucs01-relay/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 10 additions & 5 deletions cosmwasm/ucs01-relay/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()))
}
};

Expand All @@ -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(),
};

Expand Down Expand Up @@ -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()))
}
};

Expand All @@ -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(),
};

Expand Down

0 comments on commit b12c744

Please sign in to comment.