Skip to content

Commit

Permalink
fix(ucs01): convert pfm ack
Browse files Browse the repository at this point in the history
Signed-off-by: aeryz <abdullaheryz@protonmail.com>
  • Loading branch information
aeryz committed Jun 7, 2024
1 parent b12c744 commit beededc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cosmwasm/ucs01-relay-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct InFlightPfmPacket {
pub src_packet_timeout: IbcTimeout,
pub forward_channel_id: String,
pub forward_port_id: String,
pub original_protocol_version: String,
}

impl InFlightPfmPacket {
Expand All @@ -83,6 +84,7 @@ impl InFlightPfmPacket {
timeout: u64,
forward_channel_id: String,
forward_port_id: String,
original_protocol_version: String,
) -> Self {
Self {
original_sender_addr,
Expand All @@ -96,6 +98,7 @@ impl InFlightPfmPacket {
packet_sequence: original_packet.sequence,
forward_channel_id,
forward_port_id,
original_protocol_version,
}
}

Expand Down
6 changes: 6 additions & 0 deletions cosmwasm/ucs01-relay-api/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ pub trait TransferProtocol {
tokens: Vec<TransferToken>,
sequence: u64,
) -> Result<Option<(Vec<CosmosMsg<Self::CustomMsg>>, Vec<(&str, String)>)>, Self::Error>;

fn convert_foreign_protocol_ack(
&self,
foreign_protocol: &str,
ack: Binary,
) -> Result<Binary, Self::Error>;
}

#[cfg(test)]
Expand Down
5 changes: 4 additions & 1 deletion cosmwasm/ucs01-relay/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::string::FromUtf8Error;

use cosmwasm_std::{IbcOrder, OverflowError, StdError, SubMsgResult};
use cosmwasm_std::{Binary, IbcOrder, OverflowError, StdError, SubMsgResult};
use cw_controllers::AdminError;
use thiserror::Error;
use ucs01_relay_api::{middleware::MiddlewareError, protocol::ProtocolError, types::EncodingError};
Expand Down Expand Up @@ -65,6 +65,9 @@ pub enum ContractError {

#[error("{0}")]
MiddlewareError(#[from] MiddlewareError),

#[error("invalid ack ({0})")]
InvalidAck(Binary),
}

impl From<FromUtf8Error> for ContractError {
Expand Down
54 changes: 54 additions & 0 deletions cosmwasm/ucs01-relay/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ impl<'a> TransferProtocol for Ics20Protocol<'a> {
timeout,
forward.channel.value(),
forward.port.value(),
Self::VERSION.to_string(),
);

if let Some(reply_sub) = transfer
Expand Down Expand Up @@ -653,6 +654,8 @@ impl<'a> TransferProtocol for Ics20Protocol<'a> {

let (mut ack_msgs, mut ack_attr, ack_def) = match ack {
Ok(value) => {
let value = self
.convert_foreign_protocol_ack(&refund_info.original_protocol_version, value)?;
let value_string = value.to_string();
(
self.send_tokens_success(sender, &String::new(), tokens)?,
Expand Down Expand Up @@ -713,6 +716,30 @@ impl<'a> TransferProtocol for Ics20Protocol<'a> {

Ok(Some((ack_msgs, ack_attr)))
}

fn convert_foreign_protocol_ack(
&self,
foreign_protocol: &str,
ack: Binary,
) -> Result<Binary, ContractError> {
match foreign_protocol {
Ucs01Protocol::VERSION => {
let ack: Ucs01Ack = ack
.clone()
.try_into()
.map_err(|_| ContractError::InvalidAck(ack))?;

match ack {
Ucs01Ack::Failure => Self::ack_success(),
Ucs01Ack::Success => Self::ack_failure("ucs01 ack failure".to_string()),
}
.try_into()
.map_err(Into::into)
}
Ics20Protocol::VERSION => Ok(ack),
_ => Err(ContractError::InvalidAck(ack)),
}
}
}

pub struct Ucs01Protocol<'a> {
Expand Down Expand Up @@ -977,6 +1004,7 @@ impl<'a> TransferProtocol for Ucs01Protocol<'a> {
timeout,
forward.channel.value(),
forward.port.value(),
Self::VERSION.to_string(),
);

if let Some(reply_sub) = transfer
Expand Down Expand Up @@ -1021,6 +1049,8 @@ impl<'a> TransferProtocol for Ucs01Protocol<'a> {
let (mut ack_msgs, mut ack_attr, ack_def) = match ack {
Ok(value) => {
let value_string = value.to_string();
let value = self
.convert_foreign_protocol_ack(&refund_info.original_protocol_version, value)?;
(
self.send_tokens_success(sender, &String::new().as_bytes().into(), tokens)?,
Vec::from_iter(
Expand Down Expand Up @@ -1080,6 +1110,30 @@ impl<'a> TransferProtocol for Ucs01Protocol<'a> {

Ok(Some((ack_msgs, ack_attr)))
}

fn convert_foreign_protocol_ack(
&self,
foreign_protocol: &str,
ack: Binary,
) -> Result<Binary, Self::Error> {
match foreign_protocol {
Ucs01Protocol::VERSION => Ok(ack),
Ics20Protocol::VERSION => {
let ack: Ics20Ack = ack
.clone()
.try_into()
.map_err(|_| ContractError::InvalidAck(ack))?;

match ack {
Ics20Ack::Result(_) => Self::ack_success(),
Ics20Ack::Error(_) => Self::ack_failure(String::new()),
}
.try_into()
.map_err(Into::into)
}
_ => Err(ContractError::InvalidAck(ack)),
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit beededc

Please sign in to comment.