From 3def75b196395a3998b453c1ace3574bb2bc0ada Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Fri, 17 Jan 2025 10:16:10 +0000 Subject: [PATCH] refactor: add chan end variants of ack commit fns --- .../src/handler/acknowledgement.rs | 40 +++++++++++++++---- .../ics04-channel/src/handler/recv_packet.rs | 12 ++++-- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/ibc-core/ics04-channel/src/handler/acknowledgement.rs b/ibc-core/ics04-channel/src/handler/acknowledgement.rs index a6713e4fe..25ffff324 100644 --- a/ibc-core/ics04-channel/src/handler/acknowledgement.rs +++ b/ibc-core/ics04-channel/src/handler/acknowledgement.rs @@ -1,5 +1,5 @@ use ibc_core_channel_types::acknowledgement::Acknowledgement; -use ibc_core_channel_types::channel::{Counterparty, Order, State as ChannelState}; +use ibc_core_channel_types::channel::{ChannelEnd, Counterparty, Order, State as ChannelState}; use ibc_core_channel_types::commitment::{compute_ack_commitment, compute_packet_commitment}; use ibc_core_channel_types::error::ChannelError; use ibc_core_channel_types::events::{AcknowledgePacket, WriteAcknowledgement}; @@ -17,16 +17,14 @@ use ibc_core_host::{ExecutionContext, ValidationContext}; use ibc_core_router::module::Module; use ibc_primitives::prelude::*; -pub fn commit_packet_sequence_number( +pub fn commit_packet_sequence_number_with_chan_end( ctx_b: &mut ExecCtx, + chan_end_on_b: &ChannelEnd, packet: &Packet, ) -> Result<(), ChannelError> where ExecCtx: ExecutionContext, { - let chan_end_path_on_b = ChannelEndPath::new(&packet.port_id_on_b, &packet.chan_id_on_b); - let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; - // `recvPacket` core handler state changes match chan_end_on_b.ordering { Order::Unordered => { @@ -49,6 +47,19 @@ where Ok(()) } +pub fn commit_packet_sequence_number( + ctx_b: &mut ExecCtx, + packet: &Packet, +) -> Result<(), ChannelError> +where + ExecCtx: ExecutionContext, +{ + let chan_end_path_on_b = ChannelEndPath::new(&packet.port_id_on_b, &packet.chan_id_on_b); + let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; + + commit_packet_sequence_number_with_chan_end(ctx_b, &chan_end_on_b, packet) +} + pub fn commit_packet_acknowledgment( ctx_b: &mut ExecCtx, packet: &Packet, @@ -65,16 +76,15 @@ where Ok(()) } -pub fn emit_packet_acknowledgement_event( +pub fn emit_packet_acknowledgement_event_with_chan_end( ctx_b: &mut ExecCtx, + chan_end_on_b: &ChannelEnd, packet: Packet, acknowledgement: Acknowledgement, ) -> Result<(), ChannelError> where ExecCtx: ExecutionContext, { - let chan_end_path_on_b = ChannelEndPath::new(&packet.port_id_on_b, &packet.chan_id_on_b); - let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; let conn_id_on_b = &chan_end_on_b.connection_hops()[0]; ctx_b.log_message("success: packet write acknowledgement".to_string())?; @@ -90,6 +100,20 @@ where Ok(()) } +pub fn emit_packet_acknowledgement_event( + ctx_b: &mut ExecCtx, + packet: Packet, + acknowledgement: Acknowledgement, +) -> Result<(), ChannelError> +where + ExecCtx: ExecutionContext, +{ + let chan_end_path_on_b = ChannelEndPath::new(&packet.port_id_on_b, &packet.chan_id_on_b); + let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; + + emit_packet_acknowledgement_event_with_chan_end(ctx_b, &chan_end_on_b, packet, acknowledgement) +} + pub fn acknowledgement_packet_validate( ctx_a: &ValCtx, module: &dyn Module, diff --git a/ibc-core/ics04-channel/src/handler/recv_packet.rs b/ibc-core/ics04-channel/src/handler/recv_packet.rs index 7128d8a3b..eb4e12b60 100644 --- a/ibc-core/ics04-channel/src/handler/recv_packet.rs +++ b/ibc-core/ics04-channel/src/handler/recv_packet.rs @@ -16,7 +16,8 @@ use ibc_core_router::module::Module; use ibc_primitives::prelude::*; use super::acknowledgement::{ - commit_packet_acknowledgment, commit_packet_sequence_number, emit_packet_acknowledgement_event, + commit_packet_acknowledgment, commit_packet_sequence_number_with_chan_end, + emit_packet_acknowledgement_event_with_chan_end, }; pub fn recv_packet_validate(ctx_b: &ValCtx, msg: MsgRecvPacket) -> Result<(), ChannelError> @@ -73,7 +74,7 @@ where let (extras, acknowledgement) = module.on_recv_packet_execute(&msg.packet, &msg.signer); // state changes - commit_packet_sequence_number(ctx_b, &msg.packet)?; + commit_packet_sequence_number_with_chan_end(ctx_b, &chan_end_on_b, &msg.packet)?; if let Some(acknowledgement) = acknowledgement.as_ref() { commit_packet_acknowledgment(ctx_b, &msg.packet, acknowledgement)?; @@ -95,7 +96,12 @@ where // write ack events/logs if let Some(acknowledgement) = acknowledgement { - emit_packet_acknowledgement_event(ctx_b, msg.packet, acknowledgement)?; + emit_packet_acknowledgement_event_with_chan_end( + ctx_b, + &chan_end_on_b, + msg.packet, + acknowledgement, + )?; } // module specific events/logs