Skip to content

Commit b2e635d

Browse files
committed
ibc-types-core-channel: emit and parse packet_ack and packet_ack_hex correctly
1 parent 99d1484 commit b2e635d

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

crates/ibc-types-core-channel/src/events/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub enum Error {
6262
ParseSequence { key: &'static str, e: ChannelError },
6363
/// Two different encodings of the same packet data were supplied, but they don't match.
6464
MismatchedPacketData,
65+
/// Two different encodings of the same acknowledgements were supplied, but they don't match.
66+
MismatchedAcks,
6567
}
6668

6769
#[cfg(feature = "std")]

crates/ibc-types-core-channel/src/events/packet.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -590,16 +590,14 @@ impl From<WriteAcknowledgement> for Event {
590590
attrs.push(("packet_src_channel", event.src_channel_id.0));
591591
attrs.push(("packet_dst_port", event.dst_port_id.0));
592592
attrs.push(("packet_dst_channel", event.dst_channel_id.0));
593-
// TODO: do we need to support any relayers who only know
594-
// about packet_ack and not packet_ack_hex
595-
// > Note: this attribute forces us to assume that Packet data
596-
// > is valid UTF-8, even though the standard doesn't require
597-
// > it. It has been deprecated in ibc-go. It will be removed
598-
// > in the future.
599593
attrs.push((
600594
"packet_ack_hex",
601595
String::from_utf8(hex::encode(&event.acknowledgement)).unwrap(),
602596
));
597+
// Like packet_data, conditionally include packet_ack only if UTF-8 encodable.
598+
if let Ok(utf8_ack_data) = String::from_utf8(event.acknowledgement) {
599+
attrs.push(("packet_ack", utf8_ack_data));
600+
}
603601
attrs.push(("packet_connection", event.dst_connection_id.0));
604602

605603
Event::new(WriteAcknowledgement::TYPE_STR, attrs)
@@ -705,12 +703,29 @@ impl TryFrom<Event> for WriteAcknowledgement {
705703
e,
706704
})?);
707705
}
706+
"packet_ack" => {
707+
let new_ack = attr.value.into_bytes();
708+
if let Some(existing_ack) = acknowledgement {
709+
if new_ack != existing_ack {
710+
return Err(Error::MismatchedAcks);
711+
} else {
712+
acknowledgement = Some(new_ack);
713+
}
714+
}
715+
}
708716
"packet_ack_hex" => {
709-
acknowledgement =
710-
Some(hex::decode(&attr.value).map_err(|e| Error::ParseHex {
711-
key: "packet_data_hex",
712-
e,
713-
})?);
717+
let new_ack = hex::decode(&attr.value).map_err(|e| Error::ParseHex {
718+
key: "packet_ack_hex",
719+
e,
720+
})?;
721+
722+
if let Some(existing_ack) = acknowledgement {
723+
if new_ack != existing_ack {
724+
return Err(Error::MismatchedAcks);
725+
} else {
726+
acknowledgement = Some(new_ack);
727+
}
728+
}
714729
}
715730
"packet_connection" => {
716731
dst_connection_id =
@@ -735,7 +750,8 @@ impl TryFrom<Event> for WriteAcknowledgement {
735750
dst_port_id: dst_port_id.ok_or(Error::MissingAttribute("packet_dst_port"))?,
736751
src_channel_id: src_channel_id.ok_or(Error::MissingAttribute("packet_src_channel"))?,
737752
dst_channel_id: dst_channel_id.ok_or(Error::MissingAttribute("packet_dst_channel"))?,
738-
acknowledgement: acknowledgement.ok_or(Error::MissingAttribute("packet_ack_hex"))?,
753+
acknowledgement: acknowledgement
754+
.ok_or(Error::MissingAttribute("packet_ack_hex or packet_ack"))?,
739755
dst_connection_id: dst_connection_id
740756
.ok_or(Error::MissingAttribute("packet_connection"))?,
741757
})

0 commit comments

Comments
 (0)