From 27592c4bebf09964eb58468083c8c00aed69e1d6 Mon Sep 17 00:00:00 2001 From: Rano | Ranadeep Date: Thu, 19 Oct 2023 20:51:59 +0300 Subject: [PATCH] refactor: packet_commitments and packet_acknowledgements return PacketState (#928) * impl PacketState domain type * refactor QueryContext method signatures * update channel query methods * add changelog entry --- ...commitments-and-packet-acknowledgements.md | 2 + crates/ibc-query/src/core/channel/query.rs | 28 ++---- crates/ibc-query/src/core/context.rs | 8 +- crates/ibc/src/core/ics04_channel/packet.rs | 87 ++++++++++++++++++- 4 files changed, 97 insertions(+), 28 deletions(-) create mode 100644 .changelog/unreleased/improvements/927-refactor-packet-commitments-and-packet-acknowledgements.md diff --git a/.changelog/unreleased/improvements/927-refactor-packet-commitments-and-packet-acknowledgements.md b/.changelog/unreleased/improvements/927-refactor-packet-commitments-and-packet-acknowledgements.md new file mode 100644 index 000000000..ad3827615 --- /dev/null +++ b/.changelog/unreleased/improvements/927-refactor-packet-commitments-and-packet-acknowledgements.md @@ -0,0 +1,2 @@ +- Return PacketStates instead of paths from packet_commitments and + packet_acknowledgements ([\#927](https://github.com/cosmos/ibc-rs/issues/927)) diff --git a/crates/ibc-query/src/core/channel/query.rs b/crates/ibc-query/src/core/channel/query.rs index 15a1db5ca..e0cf5cbf6 100644 --- a/crates/ibc-query/src/core/channel/query.rs +++ b/crates/ibc-query/src/core/channel/query.rs @@ -13,7 +13,7 @@ use ibc::core::ValidationContext; use ibc::Height; use ibc_proto::google::protobuf::Any; use ibc_proto::ibc::core::channel::v1::{ - PacketState, QueryChannelClientStateRequest, QueryChannelClientStateResponse, + QueryChannelClientStateRequest, QueryChannelClientStateResponse, QueryChannelConsensusStateRequest, QueryChannelConsensusStateResponse, QueryChannelRequest, QueryChannelResponse, QueryChannelsRequest, QueryChannelsResponse, QueryConnectionChannelsRequest, QueryConnectionChannelsResponse, @@ -274,17 +274,8 @@ where let commitments = ibc_ctx .packet_commitments(&channel_end_path)? .into_iter() - .map(|path| { - ibc_ctx - .get_packet_commitment(&path) - .map(|commitment| PacketState { - port_id: path.port_id.as_str().into(), - channel_id: path.channel_id.as_str().into(), - sequence: path.sequence.into(), - data: commitment.into_vec(), - }) - }) - .collect::>()?; + .map(Into::into) + .collect(); Ok(QueryPacketCommitmentsResponse { commitments, @@ -393,17 +384,8 @@ where let acknowledgements = ibc_ctx .packet_acknowledgements(&channel_end_path, commitment_sequences)? .into_iter() - .map(|path| { - ibc_ctx - .get_packet_acknowledgement(&path) - .map(|acknowledgement| PacketState { - port_id: path.port_id.as_str().into(), - channel_id: path.channel_id.as_str().into(), - sequence: path.sequence.into(), - data: acknowledgement.into_vec(), - }) - }) - .collect::>()?; + .map(Into::into) + .collect(); Ok(QueryPacketAcknowledgementsResponse { acknowledgements, diff --git a/crates/ibc-query/src/core/context.rs b/crates/ibc-query/src/core/context.rs index 1b87e2bd0..a71e44615 100644 --- a/crates/ibc-query/src/core/context.rs +++ b/crates/ibc-query/src/core/context.rs @@ -2,9 +2,9 @@ use ibc::core::ics03_connection::connection::IdentifiedConnectionEnd; use ibc::core::ics04_channel::channel::IdentifiedChannelEnd; -use ibc::core::ics04_channel::packet::Sequence; +use ibc::core::ics04_channel::packet::{PacketState, Sequence}; use ibc::core::ics24_host::identifier::{ClientId, ConnectionId}; -use ibc::core::ics24_host::path::{AckPath, ChannelEndPath, CommitmentPath, Path}; +use ibc::core::ics24_host::path::{ChannelEndPath, Path}; use ibc::core::{ContextError, ValidationContext}; use ibc::prelude::*; use ibc::Height; @@ -56,7 +56,7 @@ pub trait QueryContext: ProvableContext + ValidationContext { fn packet_commitments( &self, channel_end_path: &ChannelEndPath, - ) -> Result, ContextError>; + ) -> Result, ContextError>; /// Filters the list of packet sequences for the given channel end that are acknowledged. /// Returns all the packet acknowledgements if `sequences` is empty. @@ -64,7 +64,7 @@ pub trait QueryContext: ProvableContext + ValidationContext { &self, channel_end_path: &ChannelEndPath, sequences: impl ExactSizeIterator, - ) -> Result, ContextError>; + ) -> Result, ContextError>; /// Filters the packet sequences for the given channel end that are not received. fn unreceived_packets( diff --git a/crates/ibc/src/core/ics04_channel/packet.rs b/crates/ibc/src/core/ics04_channel/packet.rs index 4ae6ea5e7..b1914dcbf 100644 --- a/crates/ibc/src/core/ics04_channel/packet.rs +++ b/crates/ibc/src/core/ics04_channel/packet.rs @@ -2,7 +2,7 @@ use core::str::FromStr; -use ibc_proto::ibc::core::channel::v1::Packet as RawPacket; +use ibc_proto::ibc::core::channel::v1::{Packet as RawPacket, PacketState as RawPacketState}; use super::timeout::TimeoutHeight; use crate::core::ics04_channel::error::{ChannelError, PacketError}; @@ -287,6 +287,91 @@ impl From for RawPacket { } } +/// The packet state type. +/// +/// Each application defines the structure of the `data` field. +#[cfg_attr( + feature = "parity-scale-codec", + derive( + parity_scale_codec::Encode, + parity_scale_codec::Decode, + scale_info::TypeInfo + ) +)] +#[cfg_attr( + feature = "borsh", + derive(borsh::BorshSerialize, borsh::BorshDeserialize) +)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[derive(Clone, Default, Hash, PartialEq, Eq)] +pub struct PacketState { + pub port_id: PortId, + pub chan_id: ChannelId, + pub seq: Sequence, + #[cfg_attr( + feature = "serde", + serde(serialize_with = "crate::serializers::ser_hex_upper") + )] + pub data: Vec, +} +impl core::fmt::Debug for PacketState { + fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { + let data_wrapper = PacketData(&self.data); + + formatter + .debug_struct("PacketState") + .field("port", &self.port_id) + .field("channel", &self.chan_id) + .field("sequence", &self.seq) + .field("data", &data_wrapper) + .finish() + } +} + +/// Custom debug output to omit the packet data +impl core::fmt::Display for PacketState { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { + write!( + f, + "seq:{}, path:{}/{}", + self.seq, self.chan_id, self.port_id, + ) + } +} + +impl TryFrom for PacketState { + type Error = PacketError; + + fn try_from(raw_pkt: RawPacketState) -> Result { + if Sequence::from(raw_pkt.sequence).is_zero() { + return Err(PacketError::ZeroPacketSequence); + } + + if raw_pkt.data.is_empty() { + return Err(PacketError::ZeroPacketData); + } + + Ok(PacketState { + seq: Sequence::from(raw_pkt.sequence), + port_id: raw_pkt.port_id.parse()?, + chan_id: raw_pkt.channel_id.parse()?, + data: raw_pkt.data, + }) + } +} + +impl From for RawPacketState { + fn from(packet: PacketState) -> Self { + Self { + sequence: packet.seq.0, + port_id: packet.port_id.to_string(), + channel_id: packet.chan_id.to_string(), + data: packet.data, + } + } +} + #[cfg(test)] pub mod test_utils { use ibc_proto::ibc::core::channel::v1::Packet as RawPacket;