Skip to content

Commit

Permalink
refactor packet handlers after channel types removal
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Oct 24, 2024
1 parent bbef5ed commit 33fb856
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 211 deletions.
28 changes: 1 addition & 27 deletions ibc-eureka-core/ics04-channel/src/handler/acknowledgement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use ibc_eureka_core_channel_types::events::AcknowledgePacket;
use ibc_eureka_core_channel_types::msgs::MsgAcknowledgement;
use ibc_eureka_core_client::context::prelude::*;
use ibc_eureka_core_handler_types::events::{IbcEvent, MessageEvent};
use ibc_eureka_core_host::types::path::{
AckPath, ChannelEndPath, ClientConsensusStatePath, CommitmentPath, Path, SeqAckPath,
};
use ibc_eureka_core_host::types::path::{AckPath, ClientConsensusStatePath, CommitmentPath, Path};
use ibc_eureka_core_host::{ExecutionContext, ValidationContext};
use ibc_eureka_core_router::module::Module;
use ibc_primitives::prelude::*;
Expand Down Expand Up @@ -41,8 +39,6 @@ where
let channel_id_on_a = &packet.header.source_client;
let seq_on_a = &packet.header.seq_on_a;

let chan_end_path_on_a = ChannelEndPath::new(port_id_on_a, channel_id_on_a);

// In all cases, this event is emitted
let event = IbcEvent::AcknowledgePacket(AcknowledgePacket::new(packet.clone()));
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
Expand All @@ -67,13 +63,6 @@ where
// apply state changes
{
ctx_a.delete_packet_commitment(&commitment_path_on_a)?;

if let Order::Ordered = chan_end_on_a.ordering {
// Note: in validation, we verified that `msg.packet.sequence == nextSeqRecv`
// (where `nextSeqRecv` is the value in the store)
let seq_ack_path_on_a = SeqAckPath::new(port_id_on_a, channel_id_on_a);
ctx_a.store_next_sequence_ack(&seq_ack_path_on_a, (*seq_on_a).increment())?;
}
}

// emit events and logs
Expand Down Expand Up @@ -110,10 +99,6 @@ where
let seq_on_a = &packet.header.seq_on_a;
let data = &payload.data;

let chan_end_path_on_a = ChannelEndPath::new(port_id_on_a, channel_id_on_a);

let counterparty = Counterparty::new(port_id_on_b.clone(), Some(channel_id_on_b.clone()));

let commitment_path_on_a = CommitmentPath::new(port_id_on_a, channel_id_on_a, *seq_on_a);

// Verify packet commitment
Expand All @@ -138,17 +123,6 @@ where
});
}

if let Order::Ordered = chan_end_on_a.ordering {
let seq_ack_path_on_a = SeqAckPath::new(port_id_on_a, channel_id_on_a);
let next_seq_ack = ctx_a.get_next_sequence_ack(&seq_ack_path_on_a)?;
if seq_on_a != &next_seq_ack {
return Err(ChannelError::MismatchedPacketSequence {
actual: *seq_on_a,
expected: next_seq_ack,
});
}
}

// Verify proofs
{
// TODO(rano): avoid a vs b confusion
Expand Down
94 changes: 19 additions & 75 deletions ibc-eureka-core/ics04-channel/src/handler/recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ use ibc_eureka_core_channel_types::commitment::{
use ibc_eureka_core_channel_types::error::ChannelError;
use ibc_eureka_core_channel_types::events::{ReceivePacket, WriteAcknowledgement};
use ibc_eureka_core_channel_types::msgs::MsgRecvPacket;
use ibc_eureka_core_channel_types::packet::Receipt;
use ibc_eureka_core_client::context::prelude::*;
use ibc_eureka_core_handler_types::events::{IbcEvent, MessageEvent};
use ibc_eureka_core_host::types::path::{
AckPath, ChannelEndPath, ClientConsensusStatePath, CommitmentPath, Path, ReceiptPath,
SeqRecvPath,
AckPath, ClientConsensusStatePath, CommitmentPath, Path, ReceiptPath, SeqRecvPath,
};
use ibc_eureka_core_host::{ExecutionContext, ValidationContext};
use ibc_eureka_core_router::module::Module;
Expand Down Expand Up @@ -41,26 +39,12 @@ where
let channel_id_on_b = &packet.header.target_client;
let seq_on_a = &packet.header.seq_on_a;

let chan_end_path_on_b = ChannelEndPath::new(port_id_on_b, channel_id_on_b);

// Check if another relayer already relayed the packet.
// We don't want to fail the transaction in this case.
{
let packet_already_received = match chan_end_on_b.ordering {
// Note: ibc-go doesn't make the check for `Order::None` channels
Order::None => false,
Order::Unordered => {
let receipt_path_on_b = ReceiptPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);
ctx_b.get_packet_receipt(&receipt_path_on_b)?.is_ok()
}
Order::Ordered => {
let seq_recv_path_on_b = SeqRecvPath::new(port_id_on_b, channel_id_on_b);
let next_seq_recv = ctx_b.get_next_sequence_recv(&seq_recv_path_on_b)?;

// the sequence number has already been incremented, so
// another relayer already relayed the packet
seq_on_a < &next_seq_recv
}
let packet_already_received = {
let receipt_path_on_b = ReceiptPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);
ctx_b.get_packet_receipt(&receipt_path_on_b)?.is_ok()
};

if packet_already_received {
Expand All @@ -73,22 +57,10 @@ where
// state changes
{
// `recvPacket` core handler state changes
match chan_end_on_b.ordering {
Order::Unordered => {
let receipt_path_on_b = ReceiptPath {
port_id: port_id_on_b.clone(),
channel_id: channel_id_on_b.clone(),
sequence: *seq_on_a,
};

ctx_b.store_packet_receipt(&receipt_path_on_b, Receipt::Ok)?;
}
Order::Ordered => {
let seq_recv_path_on_b = SeqRecvPath::new(port_id_on_b, channel_id_on_b);
let next_seq_recv = ctx_b.get_next_sequence_recv(&seq_recv_path_on_b)?;
ctx_b.store_next_sequence_recv(&seq_recv_path_on_b, next_seq_recv.increment())?;
}
_ => {}
{
let seq_recv_path_on_b = SeqRecvPath::new(port_id_on_b, channel_id_on_b);
let next_seq_recv = ctx_b.get_next_sequence_recv(&seq_recv_path_on_b)?;
ctx_b.store_next_sequence_recv(&seq_recv_path_on_b, next_seq_recv.increment())?;
}
let ack_path_on_b = AckPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);
// `writeAcknowledgement` handler state changes
Expand Down Expand Up @@ -134,15 +106,11 @@ where

let (_, port_id_on_a) = &payload.header.source_port;
let channel_id_on_a = &packet.header.source_client;
let (prefix_on_b, port_id_on_b) = &payload.header.target_port;
let channel_id_on_b = &packet.header.target_client;
let (prefix_on_b, _) = &payload.header.target_port;
let _ = &packet.header.target_client;
let seq_on_a = &packet.header.seq_on_a;
let data = &payload.data;

let chan_end_path_on_b = ChannelEndPath::new(port_id_on_b, channel_id_on_b);

let counterparty = Counterparty::new(port_id_on_a.clone(), Some(channel_id_on_a.clone()));

let latest_height = ctx_b.host_height()?;
if packet.header.timeout_height_on_b.has_expired(latest_height) {
return Err(ChannelError::InsufficientPacketHeight {
Expand Down Expand Up @@ -198,39 +166,15 @@ where
)?;
}

match chan_end_on_b.ordering {
Order::Ordered => {
let seq_recv_path_on_b = SeqRecvPath::new(port_id_on_b, channel_id_on_b);
let next_seq_recv = ctx_b.get_next_sequence_recv(&seq_recv_path_on_b)?;
if seq_on_a > &next_seq_recv {
return Err(ChannelError::MismatchedPacketSequence {
actual: *seq_on_a,
expected: next_seq_recv,
});
}

if seq_on_a == &next_seq_recv {
// Case where the recvPacket is successful and an
// acknowledgement will be written (not a no-op)
validate_write_acknowledgement(ctx_b, msg)?;
}
}
Order::Unordered => {
// Note: We don't check for the packet receipt here because another
// relayer may have already relayed the packet. If that's the case,
// we want to avoid failing the transaction and consuming
// unnecessary fees.

// Case where the recvPacket is successful and an
// acknowledgement will be written (not a no-op)
validate_write_acknowledgement(ctx_b, msg)?;
}
Order::None => {
return Err(ChannelError::InvalidState {
expected: "Channel ordering to not be None".to_string(),
actual: chan_end_on_b.ordering.to_string(),
})
}
{
// Note: We don't check for the packet receipt here because another
// relayer may have already relayed the packet. If that's the case,
// we want to avoid failing the transaction and consuming
// unnecessary fees.

// Case where the recvPacket is successful and an
// acknowledgement will be written (not a no-op)
validate_write_acknowledgement(ctx_b, msg)?;
}

Ok(())
Expand Down
7 changes: 1 addition & 6 deletions ibc-eureka-core/ics04-channel/src/handler/send_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use ibc_eureka_core_channel_types::events::SendPacket;
use ibc_eureka_core_channel_types::packet::Packet;
use ibc_eureka_core_client::context::prelude::*;
use ibc_eureka_core_handler_types::events::{IbcEvent, MessageEvent};
use ibc_eureka_core_host::types::path::{
ChannelEndPath, ClientConsensusStatePath, CommitmentPath, SeqSendPath,
};
use ibc_eureka_core_host::types::path::{ClientConsensusStatePath, CommitmentPath, SeqSendPath};
use ibc_primitives::prelude::*;

use crate::context::{SendPacketExecutionContext, SendPacketValidationContext};
Expand Down Expand Up @@ -120,9 +118,6 @@ pub fn send_packet_execute(

// emit events and logs
{
let chan_end_path_on_a = ChannelEndPath::new(port_id_on_a, channel_id_on_a);
let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?;

ctx_a.log_message("success: packet send".to_string())?;
let event = IbcEvent::SendPacket(SendPacket::new(packet));
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
Expand Down
75 changes: 11 additions & 64 deletions ibc-eureka-core/ics04-channel/src/handler/timeout.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use ibc_eureka_core_channel_types::commitment::compute_packet_commitment;
use ibc_eureka_core_channel_types::error::ChannelError;
use ibc_eureka_core_channel_types::events::{ChannelClosed, TimeoutPacket};
use ibc_eureka_core_channel_types::events::TimeoutPacket;
use ibc_eureka_core_channel_types::msgs::{MsgTimeout, MsgTimeoutOnClose};
use ibc_eureka_core_client::context::prelude::*;
use ibc_eureka_core_handler_types::events::{IbcEvent, MessageEvent};
use ibc_eureka_core_host::types::path::{
ChannelEndPath, ClientConsensusStatePath, CommitmentPath, Path, ReceiptPath, SeqRecvPath,
ClientConsensusStatePath, CommitmentPath, Path, ReceiptPath,
};
use ibc_eureka_core_host::{ExecutionContext, ValidationContext};
use ibc_eureka_core_router::module::Module;
Expand Down Expand Up @@ -58,8 +58,6 @@ where
let channel_id_on_a = &packet.header.source_client;
let seq_on_a = &packet.header.seq_on_a;

let chan_end_path_on_a = ChannelEndPath::new(port_id_on_a, channel_id_on_a);

// In all cases, this event is emitted
let event = IbcEvent::TimeoutPacket(TimeoutPacket::new(packet.clone()));
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
Expand All @@ -80,36 +78,10 @@ where

cb_result?;

// apply state changes
let chan_end_on_a = {
ctx_a.delete_packet_commitment(&commitment_path_on_a)?;

if let Order::Ordered = chan_end_on_a.ordering {
let mut chan_end_on_a = chan_end_on_a;
chan_end_on_a.state = State::Closed;
ctx_a.store_channel(&chan_end_path_on_a, chan_end_on_a.clone())?;

chan_end_on_a
} else {
chan_end_on_a
}
};

// emit events and logs
{
ctx_a.log_message("success: packet timeout".to_string())?;

if let Order::Ordered = chan_end_on_a.ordering {
let event = IbcEvent::ChannelClosed(ChannelClosed::new(
port_id_on_a.clone(),
channel_id_on_a.clone(),
chan_end_on_a.counterparty().port_id.clone(),
chan_end_on_a.counterparty().channel_id.clone(),
));
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
ctx_a.emit_ibc_event(event)?;
}

for module_event in extras.events {
ctx_a.emit_ibc_event(IbcEvent::Module(module_event))?;
}
Expand Down Expand Up @@ -192,40 +164,15 @@ where
});
}

let next_seq_recv_verification_result = match chan_end_on_a.ordering {
Order::Ordered => {
if seq_on_a < &msg.next_seq_recv_on_b {
return Err(ChannelError::MismatchedPacketSequence {
actual: *seq_on_a,
expected: msg.next_seq_recv_on_b,
});
}
let seq_recv_path_on_b = SeqRecvPath::new(port_id_on_b, channel_id_on_b);

client_state_of_b_on_a.verify_membership(
prefix_on_a,
&msg.proof_unreceived_on_b,
consensus_state_of_b_on_a.root(),
Path::SeqRecv(seq_recv_path_on_b),
seq_on_a.to_vec(),
)
}
Order::Unordered => {
let receipt_path_on_b = ReceiptPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);

client_state_of_b_on_a.verify_non_membership(
prefix_on_a,
&msg.proof_unreceived_on_b,
consensus_state_of_b_on_a.root(),
Path::Receipt(receipt_path_on_b),
)
}
Order::None => {
return Err(ChannelError::InvalidState {
expected: "Channel ordering to not be None".to_string(),
actual: chan_end_on_a.ordering.to_string(),
})
}
let next_seq_recv_verification_result = {
let receipt_path_on_b = ReceiptPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);

client_state_of_b_on_a.verify_non_membership(
prefix_on_a,
&msg.proof_unreceived_on_b,
consensus_state_of_b_on_a.root(),
Path::Receipt(receipt_path_on_b),
)
};

next_seq_recv_verification_result?;
Expand Down
Loading

0 comments on commit 33fb856

Please sign in to comment.