Skip to content

Commit

Permalink
feat(ucs01-relay): correctly forward ack
Browse files Browse the repository at this point in the history
  • Loading branch information
PoisonPhang authored and aeryz committed Jun 5, 2024
1 parent 2bff111 commit 373a498
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 161 deletions.
24 changes: 21 additions & 3 deletions 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, IbcTimeout};
use cosmwasm_std::{Addr, Binary, IbcPacket, IbcTimeout};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use unionlabs::{
Expand Down Expand Up @@ -52,17 +52,35 @@ pub struct PacketId {
/// Information about an in flight packet, used to process retries and refunds.
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
pub struct InFlightPfmPacket {
pub nonrefundable: bool,
pub original_sender_addr: Addr,
pub packet_data: Binary,
pub packet_src_channel_id: String,
pub packet_src_port_id: String,
pub refund_channel_id: String,
pub refund_port_id: String,
pub refund_id: PacketId,
pub packet_sequence: u64,
pub timeout: u64,
pub src_packet_timeout: IbcTimeout,
pub forward_channel_id: String,
pub forward_port_id: String,
}

impl InFlightPfmPacket {
pub fn new(original_sender_addr: Addr, original_packet: IbcPacket, timeout: u64, forward_channel_id: String, forward_port_id: String) -> Self {
Self {
original_sender_addr,
packet_data: original_packet.data,
packet_src_channel_id: original_packet.src.channel_id,
packet_src_port_id: original_packet.src.port_id,
refund_channel_id: original_packet.dest.channel_id,
refund_port_id: original_packet.dest.port_id,
timeout,
src_packet_timeout: original_packet.timeout,
packet_sequence: original_packet.sequence,
forward_channel_id,
forward_port_id,
}
}
}

#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
Expand Down
33 changes: 26 additions & 7 deletions cosmwasm/ucs01-relay-api/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ use cosmwasm_std::{
use thiserror::Error;

use crate::{
middleware::{Memo, PacketForward, PacketReturnInfo},
middleware::{Memo, PacketForward},
types::{EncodingError, GenericAck, TransferPacket, TransferPacketCommon, TransferToken},
};

// TODO: REMOVE
/// Used for indexing in flight packets for refunds.
#[derive(Debug, Clone)]
pub struct PfmRefundPacketKey {
pub channel_id: String,
pub port_id: String,
pub sequence: u64,
}

// https://github.com/cosmos/ibc-go/blob/8218aeeef79d556852ec62a773f2bc1a013529d4/modules/apps/transfer/types/keys.go#L12
pub const TRANSFER_MODULE: &str = "transfer";

Expand Down Expand Up @@ -194,7 +203,7 @@ pub trait TransferProtocol {
let ack: GenericAck = Self::Ack::try_from(ibc_packet.acknowledgement.data.clone())?.into();
let memo: String = packet.extension().clone().into();

let (ack_msgs, ack_attr) = if let Some((ack_msgs, ack_attr)) = self.pfm_ack(
let (ack_msgs, mut ack_attr) = if let Some((ack_msgs, ack_attr)) = self.pfm_ack(
ack.clone(),
ibc_packet.original_packet.clone(),
packet.sender(),
Expand All @@ -212,7 +221,9 @@ pub trait TransferProtocol {
packet.receiver(),
packet.tokens(),
)?,
(!value_string.is_empty()).then_some((ATTR_SUCCESS, value_string)),
Vec::from_iter(
(!value_string.is_empty()).then_some((ATTR_SUCCESS, value_string)),
),
)
}
Err(error) => {
Expand All @@ -223,7 +234,9 @@ pub trait TransferProtocol {
packet.receiver(),
packet.tokens(),
)?,
(!error_string.is_empty()).then_some((ATTR_ERROR, error_string)),
Vec::from_iter(
(!error_string.is_empty()).then_some((ATTR_ERROR, error_string)),
),
)
}
}
Expand All @@ -234,6 +247,14 @@ pub trait TransferProtocol {
.add_attributes((!memo.is_empty()).then_some((ATTR_MEMO, &memo)))
};

let refund_key = PfmRefundPacketKey {
channel_id: ibc_packet.original_packet.src.channel_id,
port_id: ibc_packet.original_packet.src.port_id,
sequence: ibc_packet.original_packet.sequence,
};

ack_attr.append(vec![("pfm_key", format!("{refund_key:?}"))].as_mut());

Ok(IbcBasicResponse::new()
.add_event(
packet_event
Expand Down Expand Up @@ -379,8 +400,6 @@ pub trait TransferProtocol {
original_packet: IbcPacket,
forward: PacketForward,
receiver: Addr,
nonrefundable: bool,
return_info: PacketReturnInfo,
) -> Result<IbcReceiveResponse<Self::CustomMsg>, Self::Error>;

fn pfm_ack(
Expand All @@ -390,7 +409,7 @@ pub trait TransferProtocol {
sender: &<Self::Packet as TransferPacket>::Addr,
tokens: Vec<TransferToken>,
sequence: u64,
) -> Result<Option<(Vec<CosmosMsg<Self::CustomMsg>>, Option<(&str, String)>)>, Self::Error>;
) -> Result<Option<(Vec<CosmosMsg<Self::CustomMsg>>, Vec<(&str, String)>)>, Self::Error>;
}

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions cosmwasm/ucs01-relay/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ pub fn reply(
serde_json_wasm::from_slice(reply.payload.as_slice()).expect("binary is type");

let refund_packet_key = PfmRefundPacketKey {
channel_id: in_flight_packet.clone().refund_channel_id,
port_id: in_flight_packet.clone().refund_port_id,
channel_id: in_flight_packet.clone().forward_channel_id,
port_id: in_flight_packet.clone().forward_port_id,
sequence: send_res.sequence,
};

IN_FLIGHT_PFM_PACKETS
.save(deps.storage, refund_packet_key, &in_flight_packet)
.save(deps.storage, refund_packet_key.clone(), &in_flight_packet)
.expect("infallible update");

Ok(Response::new()
.add_attribute("pfm_store_inclusion", format!("{in_flight_packet:?}")))
.add_attribute("pfm_store_inclusion", format!("{refund_packet_key:?}")))
}
(_, result) => Err(ContractError::UnknownReply {
id: reply.id,
Expand Down
Loading

0 comments on commit 373a498

Please sign in to comment.