Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use ibc_eureka_core_connection_types::msgs::MsgConnectionOpenAck;
use ibc_eureka_core_connection_types::{ConnectionEnd, Counterparty, State};
use ibc_eureka_core_handler_types::events::{IbcEvent, MessageEvent};
use ibc_eureka_core_host::types::identifiers::ClientId;
use ibc_eureka_core_host::types::path::{ClientConsensusStatePath, ClientStatePath, ConnectionPath, Path};
use ibc_eureka_core_host::types::path::{
ClientConsensusStatePath, ClientStatePath, ConnectionPath, Path,
};
use ibc_eureka_core_host::{ExecutionContext, ValidationContext};
use ibc_primitives::prelude::*;
use ibc_primitives::proto::{Any, Protobuf};
Expand Down
55 changes: 30 additions & 25 deletions ibc-eureka-core/ics04-channel/src/handler/acknowledgement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ pub fn acknowledgement_packet_execute<ExecCtx>(
where
ExecCtx: ExecutionContext,
{
let chan_end_path_on_a =
ChannelEndPath::new(&msg.packet.port_id_on_a, &msg.packet.chan_id_on_a);
let payload = &msg.packet.payloads[0];

let port_id_on_a = &payload.header.source_port.1;
let channel_id_on_a = &msg.packet.header.source_client;
let seq_on_a = &msg.packet.header.seq_on_a;

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)?;
let conn_id_on_a = &chan_end_on_a.connection_hops()[0];

Expand All @@ -51,11 +56,8 @@ where
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
ctx_a.emit_ibc_event(event)?;

let commitment_path_on_a = CommitmentPath::new(
&msg.packet.port_id_on_a,
&msg.packet.chan_id_on_a,
msg.packet.seq_on_a,
);
let commitment_path_on_a =
CommitmentPath::new(port_id_on_a, channel_id_on_a, msg.packet.header.seq_on_a);

// check if we're in the NO-OP case
if ctx_a.get_packet_commitment(&commitment_path_on_a).is_err() {
Expand All @@ -78,9 +80,8 @@ where
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(&msg.packet.port_id_on_a, &msg.packet.chan_id_on_a);
ctx_a.store_next_sequence_ack(&seq_ack_path_on_a, msg.packet.seq_on_a.increment())?;
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())?;
}
}

Expand Down Expand Up @@ -109,15 +110,21 @@ where
ctx_a.validate_message_signer(&msg.signer)?;

let packet = &msg.packet;
let chan_end_path_on_a = ChannelEndPath::new(&packet.port_id_on_a, &packet.chan_id_on_a);
let payload = &packet.payloads[0];

let port_id_on_a = &payload.header.source_port.1;
let channel_id_on_a = &packet.header.source_client;
let port_id_on_b = &payload.header.target_port.1;
let channel_id_on_b = &packet.header.target_client;
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 chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?;

chan_end_on_a.verify_state_matches(&ChannelState::Open)?;

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

chan_end_on_a.verify_counterparty_matches(&counterparty)?;

Expand All @@ -126,8 +133,7 @@ where

conn_end_on_a.verify_state_matches(&ConnectionState::Open)?;

let commitment_path_on_a =
CommitmentPath::new(&packet.port_id_on_a, &packet.chan_id_on_a, packet.seq_on_a);
let commitment_path_on_a = CommitmentPath::new(port_id_on_a, channel_id_on_a, *seq_on_a);

// Verify packet commitment
let Ok(commitment_on_a) = ctx_a.get_packet_commitment(&commitment_path_on_a) else {
Expand All @@ -139,9 +145,9 @@ where
};

let expected_commitment_on_a = compute_packet_commitment(
&packet.data,
&packet.timeout_height_on_b,
&packet.timeout_timestamp_on_b,
data,
&packet.header.timeout_height_on_b,
&packet.header.timeout_timestamp_on_b,
);

if commitment_on_a != expected_commitment_on_a {
Expand All @@ -152,11 +158,11 @@ where
}

if let Order::Ordered = chan_end_on_a.ordering {
let seq_ack_path_on_a = SeqAckPath::new(&packet.port_id_on_a, &packet.chan_id_on_a);
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 packet.seq_on_a != next_seq_ack {
if seq_on_a != &next_seq_ack {
return Err(ChannelError::MismatchedPacketSequence {
actual: packet.seq_on_a,
actual: *seq_on_a,
expected: next_seq_ack,
});
}
Expand Down Expand Up @@ -184,8 +190,7 @@ where
let consensus_state_of_b_on_a =
client_val_ctx_a.consensus_state(&client_cons_state_path_on_a)?;
let ack_commitment = compute_ack_commitment(&msg.acknowledgement);
let ack_path_on_b =
AckPath::new(&packet.port_id_on_b, &packet.chan_id_on_b, packet.seq_on_a);
let ack_path_on_b = AckPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);

verify_conn_delay_passed(ctx_a, msg.proof_height_on_b, &conn_end_on_a)?;

Expand Down
4 changes: 3 additions & 1 deletion ibc-eureka-core/ics04-channel/src/handler/chan_open_ack.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Protocol logic specific to ICS4 messages of type `MsgChannelOpenAck`.
use ibc_eureka_core_channel_types::channel::{ChannelEnd, Counterparty, State, State as ChannelState};
use ibc_eureka_core_channel_types::channel::{
ChannelEnd, Counterparty, State, State as ChannelState,
};
use ibc_eureka_core_channel_types::error::ChannelError;
use ibc_eureka_core_channel_types::events::OpenAck;
use ibc_eureka_core_channel_types::msgs::MsgChannelOpenAck;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Protocol logic specific to ICS4 messages of type `MsgChannelOpenConfirm`.

use ibc_eureka_core_channel_types::channel::{ChannelEnd, Counterparty, State, State as ChannelState};
use ibc_eureka_core_channel_types::channel::{
ChannelEnd, Counterparty, State, State as ChannelState,
};
use ibc_eureka_core_channel_types::error::ChannelError;
use ibc_eureka_core_channel_types::events::OpenConfirm;
use ibc_eureka_core_channel_types::msgs::MsgChannelOpenConfirm;
Expand Down
8 changes: 6 additions & 2 deletions ibc-eureka-core/ics04-channel/src/handler/chan_open_init.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Protocol logic specific to ICS4 messages of type `MsgChannelOpenInit`.

use core::str::FromStr;

use ibc_eureka_core_channel_types::channel::{ChannelEnd, Counterparty, State};
use ibc_eureka_core_channel_types::error::ChannelError;
use ibc_eureka_core_channel_types::events::OpenInit;
Expand All @@ -21,7 +23,8 @@ where
ValCtx: ValidationContext,
{
validate(ctx_a, &msg)?;
let chan_id_on_a = ChannelId::new(ctx_a.channel_counter()?);
// todo(rano): hack
let chan_id_on_a = ChannelId::from_str("00-dummy-0")?;

module.on_chan_open_init_validate(
msg.ordering,
Expand All @@ -43,7 +46,8 @@ pub fn chan_open_init_execute<ExecCtx>(
where
ExecCtx: ExecutionContext,
{
let chan_id_on_a = ChannelId::new(ctx_a.channel_counter()?);
// todo(rano): hack
let chan_id_on_a = ChannelId::from_str("00-dummy-0")?;
let (extras, version) = module.on_chan_open_init_execute(
msg.ordering,
&msg.connection_hops_on_a,
Expand Down
8 changes: 6 additions & 2 deletions ibc-eureka-core/ics04-channel/src/handler/chan_open_try.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Protocol logic specific to ICS4 messages of type `MsgChannelOpenTry`.

use core::str::FromStr;

use ibc_eureka_core_channel_types::channel::{ChannelEnd, Counterparty, State as ChannelState};
use ibc_eureka_core_channel_types::error::ChannelError;
use ibc_eureka_core_channel_types::events::OpenTry;
Expand Down Expand Up @@ -27,7 +29,8 @@ where
{
validate(ctx_b, &msg)?;

let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?);
// todo(rano): hack
let chan_id_on_b = ChannelId::from_str("00-dummy-0")?;

module.on_chan_open_try_validate(
msg.ordering,
Expand All @@ -49,7 +52,8 @@ pub fn chan_open_try_execute<ExecCtx>(
where
ExecCtx: ExecutionContext,
{
let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?);
// todo(rano): hack
let chan_id_on_b = ChannelId::from_str("00-dummy-0")?;
let (extras, version) = module.on_chan_open_try_execute(
msg.ordering,
&msg.connection_hops_on_b,
Expand Down
93 changes: 49 additions & 44 deletions ibc-eureka-core/ics04-channel/src/handler/recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ pub fn recv_packet_execute<ExecCtx>(
where
ExecCtx: ExecutionContext,
{
let chan_end_path_on_b =
ChannelEndPath::new(&msg.packet.port_id_on_b, &msg.packet.chan_id_on_b);
let packet = &msg.packet;
let payload = &packet.payloads[0];

let port_id_on_b = &payload.header.target_port.1;
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);
let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?;

// Check if another relayer already relayed the packet.
Expand All @@ -48,19 +54,16 @@ where
// Note: ibc-go doesn't make the check for `Order::None` channels
Order::None => false,
Order::Unordered => {
let packet = &msg.packet;
let receipt_path_on_b =
ReceiptPath::new(&packet.port_id_on_b, &packet.chan_id_on_b, packet.seq_on_a);
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(&msg.packet.port_id_on_b, &msg.packet.chan_id_on_b);
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
msg.packet.seq_on_a < next_seq_recv
seq_on_a < &next_seq_recv
}
};

Expand All @@ -77,26 +80,21 @@ where
match chan_end_on_b.ordering {
Order::Unordered => {
let receipt_path_on_b = ReceiptPath {
port_id: msg.packet.port_id_on_b.clone(),
channel_id: msg.packet.chan_id_on_b.clone(),
sequence: msg.packet.seq_on_a,
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(&msg.packet.port_id_on_b, &msg.packet.chan_id_on_b);
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(
&msg.packet.port_id_on_b,
&msg.packet.chan_id_on_b,
msg.packet.seq_on_a,
);
let ack_path_on_b = AckPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);
// `writeAcknowledgement` handler state changes
ctx_b.store_packet_acknowledgement(
&ack_path_on_b,
Expand Down Expand Up @@ -143,16 +141,22 @@ where
{
ctx_b.validate_message_signer(&msg.signer)?;

let chan_end_path_on_b =
ChannelEndPath::new(&msg.packet.port_id_on_b, &msg.packet.chan_id_on_b);
let packet = &msg.packet;
let payload = &packet.payloads[0];

let port_id_on_a = &payload.header.source_port.1;
let channel_id_on_a = &packet.header.source_client;
let port_id_on_b = &payload.header.target_port.1;
let channel_id_on_b = &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 chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?;

chan_end_on_b.verify_state_matches(&ChannelState::Open)?;

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

chan_end_on_b.verify_counterparty_matches(&counterparty)?;

Expand All @@ -162,16 +166,16 @@ where
conn_end_on_b.verify_state_matches(&ConnectionState::Open)?;

let latest_height = ctx_b.host_height()?;
if msg.packet.timeout_height_on_b.has_expired(latest_height) {
if packet.header.timeout_height_on_b.has_expired(latest_height) {
return Err(ChannelError::InsufficientPacketHeight {
chain_height: latest_height,
timeout_height: msg.packet.timeout_height_on_b,
timeout_height: packet.header.timeout_height_on_b,
});
}

let latest_timestamp = ctx_b.host_timestamp()?;
if msg
.packet
if packet
.header
.timeout_timestamp_on_b
.has_expired(&latest_timestamp)
{
Expand Down Expand Up @@ -200,15 +204,11 @@ where
client_val_ctx_b.consensus_state(&client_cons_state_path_on_b)?;

let expected_commitment_on_a = compute_packet_commitment(
&msg.packet.data,
&msg.packet.timeout_height_on_b,
&msg.packet.timeout_timestamp_on_b,
);
let commitment_path_on_a = CommitmentPath::new(
&msg.packet.port_id_on_a,
&msg.packet.chan_id_on_a,
msg.packet.seq_on_a,
data,
&packet.header.timeout_height_on_b,
&packet.header.timeout_timestamp_on_b,
);
let commitment_path_on_a = CommitmentPath::new(port_id_on_a, channel_id_on_a, *seq_on_a);

verify_conn_delay_passed(ctx_b, msg.proof_height_on_a, &conn_end_on_b)?;

Expand All @@ -224,17 +224,16 @@ where

match chan_end_on_b.ordering {
Order::Ordered => {
let seq_recv_path_on_b =
SeqRecvPath::new(&msg.packet.port_id_on_b, &msg.packet.chan_id_on_b);
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 msg.packet.seq_on_a > next_seq_recv {
if seq_on_a > &next_seq_recv {
return Err(ChannelError::MismatchedPacketSequence {
actual: msg.packet.seq_on_a,
actual: *seq_on_a,
expected: next_seq_recv,
});
}

if msg.packet.seq_on_a == 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)?;
Expand Down Expand Up @@ -265,10 +264,16 @@ fn validate_write_acknowledgement<Ctx>(ctx_b: &Ctx, msg: &MsgRecvPacket) -> Resu
where
Ctx: ValidationContext,
{
let packet = msg.packet.clone();
let ack_path_on_b = AckPath::new(&packet.port_id_on_b, &packet.chan_id_on_b, packet.seq_on_a);
let packet = &msg.packet;
let payload = &packet.payloads[0];

let port_id_on_b = &payload.header.target_port.1;
let channel_id_on_b = &packet.header.target_client;
let seq_on_a = &packet.header.seq_on_a;

let ack_path_on_b = AckPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);
if ctx_b.get_packet_acknowledgement(&ack_path_on_b).is_ok() {
return Err(ChannelError::DuplicateAcknowledgment(msg.packet.seq_on_a));
return Err(ChannelError::DuplicateAcknowledgment(*seq_on_a));
}

Ok(())
Expand Down
Loading