Skip to content

Commit

Permalink
refactor in handlers for ConnectionId removal
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Oct 23, 2024
1 parent d1a17dd commit c7964f9
Show file tree
Hide file tree
Showing 26 changed files with 97 additions and 549 deletions.
10 changes: 1 addition & 9 deletions ibc-eureka-core/ics04-channel/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
use ibc_eureka_core_channel_types::channel::ChannelEnd;
use ibc_eureka_core_channel_types::commitment::PacketCommitment;
use ibc_eureka_core_client::context::prelude::*;
use ibc_eureka_core_connection::types::ConnectionEnd;
use ibc_eureka_core_handler_types::events::IbcEvent;
use ibc_eureka_core_host::types::error::HostError;
use ibc_eureka_core_host::types::identifiers::{ConnectionId, Sequence};
use ibc_eureka_core_host::types::identifiers::Sequence;
use ibc_eureka_core_host::types::path::{ChannelEndPath, CommitmentPath, SeqSendPath};
use ibc_eureka_core_host::{ExecutionContext, ValidationContext};
use ibc_primitives::prelude::*;
Expand All @@ -21,9 +20,6 @@ pub trait SendPacketValidationContext {
/// Returns the ChannelEnd for the given `port_id` and `chan_id`.
fn channel_end(&self, channel_end_path: &ChannelEndPath) -> Result<ChannelEnd, HostError>;

/// Returns the ConnectionState for the given identifier `connection_id`.
fn connection_end(&self, connection_id: &ConnectionId) -> Result<ConnectionEnd, HostError>;

fn get_next_sequence_send(&self, seq_send_path: &SeqSendPath) -> Result<Sequence, HostError>;
}

Expand All @@ -41,10 +37,6 @@ where
self.channel_end(channel_end_path)
}

fn connection_end(&self, connection_id: &ConnectionId) -> Result<ConnectionEnd, HostError> {
self.connection_end(connection_id)
}

fn get_next_sequence_send(&self, seq_send_path: &SeqSendPath) -> Result<Sequence, HostError> {
self.get_next_sequence_send(seq_send_path)
}
Expand Down
20 changes: 5 additions & 15 deletions ibc-eureka-core/ics04-channel/src/handler/acknowledgement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use ibc_eureka_core_channel_types::error::ChannelError;
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_connection::delay::verify_conn_delay_passed;
use ibc_eureka_core_connection::types::State as ConnectionState;
use ibc_eureka_core_handler_types::events::{IbcEvent, MessageEvent};
use ibc_eureka_core_host::types::path::{
AckPath, ChannelEndPath, ClientConsensusStatePath, CommitmentPath, Path, SeqAckPath,
Expand Down Expand Up @@ -45,13 +43,11 @@ where

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];

// In all cases, this event is emitted
let event = IbcEvent::AcknowledgePacket(AcknowledgePacket::new(
msg.packet.clone(),
chan_end_on_a.ordering,
conn_id_on_a.clone(),
));
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
ctx_a.emit_ibc_event(event)?;
Expand Down Expand Up @@ -112,9 +108,9 @@ where
let packet = &msg.packet;
let payload = &packet.payloads[0];

let port_id_on_a = &payload.header.source_port.1;
let (prefix_on_a, port_id_on_a) = &payload.header.source_port;
let channel_id_on_a = &packet.header.source_client;
let port_id_on_b = &payload.header.target_port.1;
let (_, port_id_on_b) = &payload.header.target_port;
let channel_id_on_b = &packet.header.target_client;
let seq_on_a = &packet.header.seq_on_a;
let data = &payload.data;
Expand All @@ -128,11 +124,6 @@ where

chan_end_on_a.verify_counterparty_matches(&counterparty)?;

let conn_id_on_a = &chan_end_on_a.connection_hops()[0];
let conn_end_on_a = ctx_a.connection_end(conn_id_on_a)?;

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

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

// Verify packet commitment
Expand Down Expand Up @@ -170,7 +161,8 @@ where

// Verify proofs
{
let client_id_on_a = conn_end_on_a.client_id();
// TODO(rano): avoid a vs b confusion
let client_id_on_a = channel_id_on_b.as_ref();

let client_val_ctx_a = ctx_a.get_client_validation_context();

Expand All @@ -192,11 +184,9 @@ where
let ack_commitment = compute_ack_commitment(&msg.acknowledgement);
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)?;

// Verify the proof for the packet against the chain store.
client_state_of_b_on_a.verify_membership(
conn_end_on_a.counterparty().prefix(),
prefix_on_a,
&msg.proof_acked_on_b,
consensus_state_of_b_on_a.root(),
Path::Ack(ack_path_on_b),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use ibc_eureka_core_channel_types::error::ChannelError;
use ibc_eureka_core_channel_types::events::CloseConfirm;
use ibc_eureka_core_channel_types::msgs::MsgChannelCloseConfirm;
use ibc_eureka_core_client::context::prelude::*;
use ibc_eureka_core_connection::types::error::ConnectionError;
use ibc_eureka_core_connection::types::State as ConnectionState;
use ibc_eureka_core_handler_types::events::{IbcEvent, MessageEvent};
use ibc_eureka_core_host::types::path::{ChannelEndPath, ClientConsensusStatePath, Path};
use ibc_eureka_core_host::{ExecutionContext, ValidationContext};
Expand Down Expand Up @@ -64,14 +62,12 @@ where
.channel_id
.clone()
.ok_or(ChannelError::MissingCounterparty)?;
let conn_id_on_b = chan_end_on_b.connection_hops[0].clone();

IbcEvent::CloseConfirmChannel(CloseConfirm::new(
msg.port_id_on_b.clone(),
msg.chan_id_on_b.clone(),
port_id_on_a,
chan_id_on_a,
conn_id_on_b,
))
};
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
Expand Down Expand Up @@ -102,10 +98,6 @@ where
// Validate that the channel end is in a state where it can be closed.
chan_end_on_b.verify_not_closed()?;

let conn_end_on_b = ctx_b.connection_end(&chan_end_on_b.connection_hops()[0])?;

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

// Verify proofs
{
let client_id_on_b = conn_end_on_b.client_id();
Expand Down Expand Up @@ -142,7 +134,6 @@ where
ChannelState::Closed,
*chan_end_on_b.ordering(),
Counterparty::new(msg.port_id_on_b.clone(), Some(msg.chan_id_on_b.clone())),
vec![conn_id_on_a.clone()],
chan_end_on_b.version().clone(),
)?;
let chan_end_path_on_a = ChannelEndPath::new(port_id_on_a, chan_id_on_a);
Expand Down
10 changes: 0 additions & 10 deletions ibc-eureka-core/ics04-channel/src/handler/chan_close_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use ibc_eureka_core_channel_types::error::ChannelError;
use ibc_eureka_core_channel_types::events::CloseInit;
use ibc_eureka_core_channel_types::msgs::MsgChannelCloseInit;
use ibc_eureka_core_client::context::prelude::*;
use ibc_eureka_core_connection::types::State as ConnectionState;
use ibc_eureka_core_handler_types::events::{IbcEvent, MessageEvent};
use ibc_eureka_core_host::types::path::ChannelEndPath;
use ibc_eureka_core_host::{ExecutionContext, ValidationContext};
Expand Down Expand Up @@ -60,14 +59,12 @@ where
.channel_id
.clone()
.ok_or(ChannelError::MissingCounterparty)?;
let conn_id_on_a = chan_end_on_a.connection_hops[0].clone();

IbcEvent::CloseInitChannel(CloseInit::new(
msg.port_id_on_a.clone(),
msg.chan_id_on_a.clone(),
port_id_on_b,
chan_id_on_b,
conn_id_on_a,
))
};
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
Expand Down Expand Up @@ -97,13 +94,6 @@ where
// Validate that the channel end is in a state where it can be closed.
chan_end_on_a.verify_not_closed()?;

// An OPEN IBC connection running on the local (host) chain should exist.
chan_end_on_a.verify_connection_hops_length()?;

let conn_end_on_a = ctx_a.connection_end(&chan_end_on_a.connection_hops()[0])?;

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

let client_id_on_a = conn_end_on_a.client_id();

let client_val_ctx_a = ctx_a.get_client_validation_context();
Expand Down
9 changes: 0 additions & 9 deletions ibc-eureka-core/ics04-channel/src/handler/chan_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ where
msg.chan_id_on_a.clone(),
port_id_on_b,
msg.chan_id_on_b,
conn_id_on_a,
))
};
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
Expand Down Expand Up @@ -100,13 +99,6 @@ where
// Validate that the channel end is in a state where it can be ack.
chan_end_on_a.verify_state_matches(&ChannelState::Init)?;

// An OPEN IBC connection running on the local (host) chain should exist.
chan_end_on_a.verify_connection_hops_length()?;

let conn_end_on_a = ctx_a.connection_end(&chan_end_on_a.connection_hops()[0])?;

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

// Verify proofs
{
let client_id_on_a = conn_end_on_a.client_id();
Expand Down Expand Up @@ -139,7 +131,6 @@ where
// fine to use A's ordering here
*chan_end_on_a.ordering(),
Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())),
vec![conn_id_on_b.clone()],
msg.version_on_b.clone(),
)?;
let chan_end_path_on_b = ChannelEndPath::new(port_id_on_b, &msg.chan_id_on_b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ where
msg.chan_id_on_b.clone(),
port_id_on_a,
chan_id_on_a,
conn_id_on_b,
));
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
ctx_b.emit_ibc_event(core_event)?;
Expand Down Expand Up @@ -142,7 +141,6 @@ where
ChannelState::Open,
*chan_end_on_b.ordering(),
Counterparty::new(msg.port_id_on_b.clone(), Some(msg.chan_id_on_b.clone())),
vec![conn_id_on_a.clone()],
chan_end_on_b.version.clone(),
)?;
let chan_end_path_on_a = ChannelEndPath::new(port_id_on_a, chan_id_on_a);
Expand Down
12 changes: 0 additions & 12 deletions ibc-eureka-core/ics04-channel/src/handler/chan_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ where

module.on_chan_open_init_validate(
msg.ordering,
&msg.connection_hops_on_a,
&msg.port_id_on_a,
&chan_id_on_a,
&Counterparty::new(msg.port_id_on_b.clone(), None),
Expand All @@ -50,7 +49,6 @@ where
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,
&msg.port_id_on_a,
&chan_id_on_a,
&Counterparty::new(msg.port_id_on_b.clone(), None),
Expand All @@ -65,7 +63,6 @@ where
State::Init,
msg.ordering,
Counterparty::new(msg.port_id_on_b.clone(), None),
msg.connection_hops_on_a.clone(),
msg.version_proposal.clone(),
)?;
let chan_end_path_on_a = ChannelEndPath::new(&msg.port_id_on_a, &chan_id_on_a);
Expand Down Expand Up @@ -93,7 +90,6 @@ where
msg.port_id_on_a.clone(),
chan_id_on_a.clone(),
msg.port_id_on_b,
conn_id_on_a,
version,
));
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
Expand All @@ -117,10 +113,6 @@ where
{
ctx_a.validate_message_signer(&msg.signer)?;

msg.verify_connection_hops_length()?;
// An IBC connection running on the local (host) chain should exist.
let conn_end_on_a = ctx_a.connection_end(&msg.connection_hops_on_a[0])?;

// Note: Not needed check if the connection end is OPEN. Optimistic channel handshake is allowed.

let client_id_on_a = conn_end_on_a.client_id();
Expand All @@ -131,9 +123,5 @@ where
.status(ctx_a.get_client_validation_context(), client_id_on_a)?
.verify_is_active()?;

let conn_version = conn_end_on_a.versions();

conn_version[0].verify_feature_supported(msg.ordering.to_string())?;

Ok(())
}
17 changes: 0 additions & 17 deletions ibc-eureka-core/ics04-channel/src/handler/chan_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ where

module.on_chan_open_try_validate(
msg.ordering,
&msg.connection_hops_on_b,
&msg.port_id_on_b,
&chan_id_on_b,
&Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())),
Expand All @@ -56,22 +55,18 @@ where
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,
&msg.port_id_on_b,
&chan_id_on_b,
&Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())),
&msg.version_supported_on_a,
)?;

let conn_id_on_b = msg.connection_hops_on_b[0].clone();

// state changes
{
let chan_end_on_b = ChannelEnd::new(
ChannelState::TryOpen,
msg.ordering,
Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())),
msg.connection_hops_on_b.clone(),
version.clone(),
)?;

Expand Down Expand Up @@ -101,7 +96,6 @@ where
chan_id_on_b.clone(),
msg.port_id_on_a.clone(),
msg.chan_id_on_a.clone(),
conn_id_on_b,
version,
));
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
Expand All @@ -125,16 +119,6 @@ where
{
ctx_b.validate_message_signer(&msg.signer)?;

msg.verify_connection_hops_length()?;

let conn_end_on_b = ctx_b.connection_end(&msg.connection_hops_on_b[0])?;

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

let conn_version = conn_end_on_b.versions();

conn_version[0].verify_feature_supported(msg.ordering.to_string())?;

// Verify proofs
{
let client_id_on_b = conn_end_on_b.client_id();
Expand Down Expand Up @@ -166,7 +150,6 @@ where
ChannelState::Init,
msg.ordering,
Counterparty::new(msg.port_id_on_b.clone(), None),
vec![conn_id_on_a.clone()],
msg.version_supported_on_a.clone(),
)?;
let chan_end_path_on_a = ChannelEndPath::new(&port_id_on_a, &chan_id_on_a);
Expand Down
Loading

0 comments on commit c7964f9

Please sign in to comment.