Skip to content

Commit

Permalink
refactor: add chan end variants of ack commit fns
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Jan 17, 2025
1 parent 5b9cbe6 commit 1d72f31
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
40 changes: 32 additions & 8 deletions ibc-core/ics04-channel/src/handler/acknowledgement.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<ExecCtx>(
pub fn commit_packet_sequence_number_with_chan_end<ExecCtx>(
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 => {
Expand All @@ -49,6 +47,19 @@ where
Ok(())
}

pub fn commit_packet_sequence_number<ExecCtx>(
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<ExecCtx>(
ctx_b: &mut ExecCtx,
packet: &Packet,
Expand All @@ -65,16 +76,15 @@ where
Ok(())
}

pub fn emit_packet_acknowledgement_event<ExecCtx>(
pub fn emit_packet_acknowledgement_event_with_chan_end<ExecCtx>(
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())?;
Expand All @@ -90,6 +100,20 @@ where
Ok(())
}

pub fn emit_packet_acknowledgement_event<ExecCtx>(
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<ValCtx>(
ctx_a: &ValCtx,
module: &dyn Module,
Expand Down
12 changes: 9 additions & 3 deletions ibc-core/ics04-channel/src/handler/recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValCtx>(ctx_b: &ValCtx, msg: MsgRecvPacket) -> Result<(), ChannelError>
Expand Down Expand Up @@ -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)?;
Expand All @@ -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
Expand Down

0 comments on commit 1d72f31

Please sign in to comment.