From d5a44165c880b520bfb410ee693aa44ca8e6be7d Mon Sep 17 00:00:00 2001 From: Farhad Shabani Date: Tue, 14 Feb 2023 01:34:20 +0330 Subject: [PATCH] Refactor store_* and get_* methods to take *Path structs instead (#419) * Implement *Path for get_* and store_* methods * Refactor Path structure to be singular * cargo fmt * Add changelog entry * Fix some args * Fix some other path args in ics04 * Mend some naming mistakes * Pass path args by reference into store_* methods --- .../382-refactor-get-and-store-methods.md | 2 + .../transfer/relay/send_transfer.rs | 8 +- .../clients/ics07_tendermint/client_state.rs | 259 ++++++++---------- crates/ibc/src/core/context.rs | 241 +++++++--------- .../ibc/src/core/ics02_client/client_state.rs | 53 ++-- crates/ibc/src/core/ics02_client/context.rs | 10 +- .../ics02_client/handler/create_client.rs | 6 +- .../core/ics02_client/handler/misbehaviour.rs | 2 +- .../ics02_client/handler/update_client.rs | 22 +- .../ics02_client/handler/upgrade_client.rs | 12 +- .../ibc/src/core/ics03_connection/context.rs | 4 +- .../ics03_connection/handler/conn_open_ack.rs | 34 ++- .../handler/conn_open_confirm.rs | 16 +- .../handler/conn_open_init.rs | 6 +- .../ics03_connection/handler/conn_open_try.rs | 36 ++- crates/ibc/src/core/ics04_channel/context.rs | 83 ++---- .../ics04_channel/handler/acknowledgement.rs | 56 ++-- .../handler/chan_close_confirm.rs | 24 +- .../ics04_channel/handler/chan_close_init.rs | 7 +- .../ics04_channel/handler/chan_open_ack.rs | 24 +- .../handler/chan_open_confirm.rs | 24 +- .../ics04_channel/handler/chan_open_try.rs | 22 +- .../core/ics04_channel/handler/recv_packet.rs | 68 +++-- .../core/ics04_channel/handler/send_packet.rs | 14 +- .../src/core/ics04_channel/handler/timeout.rs | 69 +++-- .../ics04_channel/handler/timeout_on_close.rs | 86 +++--- .../handler/write_acknowledgement.rs | 8 +- crates/ibc/src/core/ics24_host/path.rs | 192 +++++++++---- crates/ibc/src/core/ics26_routing/handler.rs | 7 +- crates/ibc/src/mock/client_state.rs | 55 ++-- crates/ibc/src/mock/context.rs | 242 ++++++++-------- crates/ibc/src/test_utils.rs | 51 ++-- 32 files changed, 892 insertions(+), 851 deletions(-) create mode 100644 .changelog/unreleased/breaking-changes/382-refactor-get-and-store-methods.md diff --git a/.changelog/unreleased/breaking-changes/382-refactor-get-and-store-methods.md b/.changelog/unreleased/breaking-changes/382-refactor-get-and-store-methods.md new file mode 100644 index 000000000..b647069fe --- /dev/null +++ b/.changelog/unreleased/breaking-changes/382-refactor-get-and-store-methods.md @@ -0,0 +1,2 @@ +- Refactor get_* and store_* methods to take *Path structs instead + ([#382](https://github.com/cosmos/ibc-rs/issues/382)) diff --git a/crates/ibc/src/applications/transfer/relay/send_transfer.rs b/crates/ibc/src/applications/transfer/relay/send_transfer.rs index 9aad00c7c..b243f264a 100644 --- a/crates/ibc/src/applications/transfer/relay/send_transfer.rs +++ b/crates/ibc/src/applications/transfer/relay/send_transfer.rs @@ -6,6 +6,7 @@ use crate::applications::transfer::packet::PacketData; use crate::applications::transfer::{is_sender_chain_source, Coin, PrefixedCoin}; use crate::core::ics04_channel::handler::send_packet::send_packet; use crate::core::ics04_channel::packet::Packet; +use crate::core::ics24_host::path::{ChannelEndPath, SeqSendPath}; use crate::events::ModuleEvent; use crate::handler::{HandlerOutput, HandlerOutputBuilder}; use crate::prelude::*; @@ -25,9 +26,9 @@ where if !ctx.is_send_enabled() { return Err(TokenTransferError::SendDisabled); } - + let chan_end_path_on_a = ChannelEndPath::new(&msg.port_on_a, &msg.chan_on_a); let chan_end_on_a = ctx - .channel_end(&msg.port_on_a, &msg.chan_on_a) + .channel_end(&chan_end_path_on_a) .map_err(TokenTransferError::PacketError)?; let port_on_b = chan_end_on_a.counterparty().port_id().clone(); @@ -41,8 +42,9 @@ where .clone(); // get the next sequence + let seq_send_path_on_a = SeqSendPath::new(&msg.port_on_a, &msg.chan_on_a); let sequence = ctx - .get_next_sequence_send(&msg.port_on_a, &msg.chan_on_a) + .get_next_sequence_send(&seq_send_path_on_a) .map_err(TokenTransferError::PacketError)?; let token = msg diff --git a/crates/ibc/src/clients/ics07_tendermint/client_state.rs b/crates/ibc/src/clients/ics07_tendermint/client_state.rs index 35c4a5e79..0eb8f671e 100644 --- a/crates/ibc/src/clients/ics07_tendermint/client_state.rs +++ b/crates/ibc/src/clients/ics07_tendermint/client_state.rs @@ -29,6 +29,7 @@ use crate::core::ics02_client::context::ClientReader; use crate::core::ics02_client::error::ClientError; use crate::core::ics02_client::trust_threshold::TrustThreshold; use crate::core::ics03_connection::connection::ConnectionEnd; +use crate::core::ics04_channel::channel::ChannelEnd; use crate::core::ics04_channel::commitment::{AcknowledgementCommitment, PacketCommitment}; use crate::core::ics04_channel::context::ChannelReader; use crate::core::ics04_channel::packet::Sequence; @@ -37,10 +38,10 @@ use crate::core::ics23_commitment::commitment::{ }; use crate::core::ics23_commitment::merkle::{apply_prefix, MerkleProof}; use crate::core::ics23_commitment::specs::ProofSpecs; -use crate::core::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId}; +use crate::core::ics24_host::identifier::{ChainId, ClientId}; use crate::core::ics24_host::path::{ - AcksPath, ChannelEndsPath, ClientConsensusStatePath, ClientStatePath, ClientUpgradePath, - CommitmentsPath, ConnectionsPath, ReceiptsPath, SeqRecvsPath, + AckPath, ChannelEndPath, ClientConsensusStatePath, ClientStatePath, ClientUpgradePath, + CommitmentPath, ConnectionPath, ReceiptPath, SeqRecvPath, }; use crate::core::ics24_host::Path; use crate::timestamp::{Timestamp, ZERO_DURATION}; @@ -394,10 +395,9 @@ impl Ics2ClientState for ClientState { ) -> Result { fn maybe_consensus_state( ctx: &dyn ClientReader, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result>, ClientError> { - match ctx.consensus_state(client_id, height) { + match ctx.consensus_state(client_cons_state_path) { Ok(cs) => Ok(Some(cs)), Err(e) => match e { ClientError::ConsensusStateNotFound { @@ -425,27 +425,29 @@ impl Ics2ClientState for ClientState { // Check if a consensus state is already installed; if so it should // match the untrusted header. let header_consensus_state = TmConsensusState::from(header.clone()); - let existing_consensus_state = - match maybe_consensus_state(ctx, &client_id, &header.height())? { - Some(cs) => { - let cs = downcast_tm_consensus_state(cs.as_ref())?; - // If this consensus state matches, skip verification - // (optimization) - if cs == header_consensus_state { - // Header is already installed and matches the incoming - // header (already verified) - return Ok(UpdatedState { - client_state: client_state.into_box(), - consensus_state: cs.into_box(), - }); - } - Some(cs) + let client_cons_state_path = ClientConsensusStatePath::new(&client_id, &header.height()); + let existing_consensus_state = match maybe_consensus_state(ctx, &client_cons_state_path)? { + Some(cs) => { + let cs = downcast_tm_consensus_state(cs.as_ref())?; + // If this consensus state matches, skip verification + // (optimization) + if cs == header_consensus_state { + // Header is already installed and matches the incoming + // header (already verified) + return Ok(UpdatedState { + client_state: client_state.into_box(), + consensus_state: cs.into_box(), + }); } - None => None, - }; + Some(cs) + } + None => None, + }; + let trusted_client_cons_state_path = + ClientConsensusStatePath::new(&client_id, &header.trusted_height); let trusted_consensus_state = downcast_tm_consensus_state( - ctx.consensus_state(&client_id, &header.trusted_height)? + ctx.consensus_state(&trusted_client_cons_state_path)? .as_ref(), )?; @@ -497,12 +499,12 @@ impl Ics2ClientState for ClientState { }); } } - + let client_cons_state_path = ClientConsensusStatePath::new(&client_id, &header.height()); // Monotonicity checks for timestamps for in-the-middle updates // (cs-new, cs-next, cs-latest) if header.height() < client_state.latest_height() { let maybe_next_cs = ctx - .next_consensus_state(&client_id, &header.height())? + .next_consensus_state(&client_cons_state_path)? .as_ref() .map(|cs| downcast_tm_consensus_state(cs.as_ref())) .transpose()?; @@ -525,7 +527,7 @@ impl Ics2ClientState for ClientState { // (cs-trusted, cs-prev, cs-new) if header.trusted_height < header.height() { let maybe_prev_cs = ctx - .prev_consensus_state(&client_id, &header.height())? + .prev_consensus_state(&client_cons_state_path)? .as_ref() .map(|cs| downcast_tm_consensus_state(cs.as_ref())) .transpose()?; @@ -574,13 +576,17 @@ impl Ics2ClientState for ClientState { return Err(Error::MisbehaviourHeadersNotAtSameHeight.into()); } } - + let client_cons_state_path_1 = + ClientConsensusStatePath::new(&client_id, &header_1.trusted_height); let consensus_state_1 = { - let cs = ctx.consensus_state(&client_id, &header_1.trusted_height)?; + let cs = ctx.consensus_state(&client_cons_state_path_1)?; downcast_tm_consensus_state(cs.as_ref()) }?; + + let client_cons_state_path_2 = + ClientConsensusStatePath::new(&client_id, &header_2.trusted_height); let consensus_state_2 = { - let cs = ctx.consensus_state(&client_id, &header_2.trusted_height)?; + let cs = ctx.consensus_state(&client_cons_state_path_2)?; downcast_tm_consensus_state(cs.as_ref()) }?; @@ -637,13 +643,17 @@ impl Ics2ClientState for ClientState { )); } } - + let client_cons_state_path_1 = + ClientConsensusStatePath::new(&client_id, &header_1.trusted_height); let consensus_state_1 = { - let cs = ctx.consensus_state(&client_id, &header_1.trusted_height)?; + let cs = ctx.consensus_state(&client_cons_state_path_1)?; downcast_tm_consensus_state(cs.as_ref()) }?; + + let client_cons_state_path_2 = + ClientConsensusStatePath::new(&client_id, &header_2.trusted_height); let consensus_state_2 = { - let cs = ctx.consensus_state(&client_id, &header_2.trusted_height)?; + let cs = ctx.consensus_state(&client_cons_state_path_2)?; downcast_tm_consensus_state(cs.as_ref()) }?; @@ -683,10 +693,9 @@ impl Ics2ClientState for ClientState { ) -> Result { fn maybe_consensus_state( ctx: &dyn ValidationContext, - client_id: &ClientId, - height: Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result>, ClientError> { - match ctx.consensus_state(client_id, &height) { + match ctx.consensus_state(client_cons_state_path) { Ok(cs) => Ok(Some(cs)), Err(e) => match e { ContextError::ClientError(ClientError::ConsensusStateNotFound { @@ -717,27 +726,29 @@ impl Ics2ClientState for ClientState { // Check if a consensus state is already installed; if so it should // match the untrusted header. let header_consensus_state = TmConsensusState::from(header.clone()); - let existing_consensus_state = - match maybe_consensus_state(ctx, &client_id, header.height())? { - Some(cs) => { - let cs = downcast_tm_consensus_state(cs.as_ref())?; - // If this consensus state matches, skip verification - // (optimization) - if cs == header_consensus_state { - // Header is already installed and matches the incoming - // header (already verified) - return Ok(UpdatedState { - client_state: client_state.into_box(), - consensus_state: cs.into_box(), - }); - } - Some(cs) + let client_cons_state_path = ClientConsensusStatePath::new(&client_id, &header.height()); + let existing_consensus_state = match maybe_consensus_state(ctx, &client_cons_state_path)? { + Some(cs) => { + let cs = downcast_tm_consensus_state(cs.as_ref())?; + // If this consensus state matches, skip verification + // (optimization) + if cs == header_consensus_state { + // Header is already installed and matches the incoming + // header (already verified) + return Ok(UpdatedState { + client_state: client_state.into_box(), + consensus_state: cs.into_box(), + }); } - None => None, - }; + Some(cs) + } + None => None, + }; + let trusted_client_cons_state_path = + ClientConsensusStatePath::new(&client_id, &header.trusted_height); let trusted_consensus_state = downcast_tm_consensus_state( - ctx.consensus_state(&client_id, &header.trusted_height) + ctx.consensus_state(&trusted_client_cons_state_path) .map_err(|e| match e { ContextError::ClientError(e) => e, _ => todo!(), @@ -796,11 +807,12 @@ impl Ics2ClientState for ClientState { } } + let client_cons_state_path = ClientConsensusStatePath::new(&client_id, &header.height()); // Monotonicity checks for timestamps for in-the-middle updates // (cs-new, cs-next, cs-latest) if header.height() < client_state.latest_height() { let maybe_next_cs = ctx - .next_consensus_state(&client_id, &header.height()) + .next_consensus_state(&client_cons_state_path) .map_err(|e| match e { ContextError::ClientError(e) => e, _ => todo!(), @@ -827,7 +839,7 @@ impl Ics2ClientState for ClientState { // (cs-trusted, cs-prev, cs-new) if header.trusted_height < header.height() { let maybe_prev_cs = ctx - .prev_consensus_state(&client_id, &header.height()) + .prev_consensus_state(&client_cons_state_path) .map_err(|e| match e { ContextError::ClientError(e) => e, _ => todo!(), @@ -1013,23 +1025,24 @@ impl Ics2ClientState for ClientState { prefix: &CommitmentPrefix, proof: &CommitmentProofBytes, root: &CommitmentRoot, - client_id: &ClientId, - consensus_height: Height, + client_cons_state_path: &ClientConsensusStatePath, expected_consensus_state: &dyn ConsensusState, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; client_state.verify_height(height)?; - let path = ClientConsensusStatePath { - client_id: client_id.clone(), - epoch: consensus_height.revision_number(), - height: consensus_height.revision_height(), - }; let value = expected_consensus_state .encode_vec() .map_err(ClientError::InvalidAnyConsensusState)?; - verify_membership(client_state, prefix, proof, root, path, value) + verify_membership( + client_state, + prefix, + proof, + root, + client_cons_state_path.clone(), + value, + ) } fn verify_connection_state( @@ -1038,17 +1051,16 @@ impl Ics2ClientState for ClientState { prefix: &CommitmentPrefix, proof: &CommitmentProofBytes, root: &CommitmentRoot, - connection_id: &ConnectionId, + conn_path: &ConnectionPath, expected_connection_end: &ConnectionEnd, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; client_state.verify_height(height)?; - let path = ConnectionsPath(connection_id.clone()); let value = expected_connection_end .encode_vec() .map_err(ClientError::InvalidConnectionEnd)?; - verify_membership(client_state, prefix, proof, root, path, value) + verify_membership(client_state, prefix, proof, root, conn_path.clone(), value) } fn verify_channel_state( @@ -1057,18 +1069,23 @@ impl Ics2ClientState for ClientState { prefix: &CommitmentPrefix, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - expected_channel_end: &crate::core::ics04_channel::channel::ChannelEnd, + channel_end_path: &ChannelEndPath, + expected_channel_end: &ChannelEnd, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; client_state.verify_height(height)?; - - let path = ChannelEndsPath(port_id.clone(), channel_id.clone()); let value = expected_channel_end .encode_vec() .map_err(ClientError::InvalidChannelEnd)?; - verify_membership(client_state, prefix, proof, root, path, value) + + verify_membership( + client_state, + prefix, + proof, + root, + channel_end_path.clone(), + value, + ) } fn verify_client_full_state( @@ -1077,15 +1094,21 @@ impl Ics2ClientState for ClientState { prefix: &CommitmentPrefix, proof: &CommitmentProofBytes, root: &CommitmentRoot, - client_id: &ClientId, + client_state_path: &ClientStatePath, expected_client_state: Any, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; client_state.verify_height(height)?; - - let path = ClientStatePath(client_id.clone()); let value = expected_client_state.encode_to_vec(); - verify_membership(client_state, prefix, proof, root, path, value) + + verify_membership( + client_state, + prefix, + proof, + root, + client_state_path.clone(), + value, + ) } fn new_verify_packet_data( @@ -1095,27 +1118,19 @@ impl Ics2ClientState for ClientState { connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + commitment_path: &CommitmentPath, commitment: PacketCommitment, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; client_state.verify_height(height)?; new_verify_delay_passed(ctx, height, connection_end)?; - let commitment_path = CommitmentsPath { - port_id: port_id.clone(), - channel_id: channel_id.clone(), - sequence, - }; - verify_membership( client_state, connection_end.counterparty().prefix(), proof, root, - commitment_path, + commitment_path.clone(), commitment.into_vec(), ) } @@ -1127,27 +1142,19 @@ impl Ics2ClientState for ClientState { connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + commitment_path: &CommitmentPath, commitment: PacketCommitment, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; client_state.verify_height(height)?; verify_delay_passed(ctx, height, connection_end)?; - let commitment_path = CommitmentsPath { - port_id: port_id.clone(), - channel_id: channel_id.clone(), - sequence, - }; - verify_membership( client_state, connection_end.counterparty().prefix(), proof, root, - commitment_path, + commitment_path.clone(), commitment.into_vec(), ) } @@ -1159,26 +1166,19 @@ impl Ics2ClientState for ClientState { connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + ack_path: &AckPath, ack: AcknowledgementCommitment, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; client_state.verify_height(height)?; new_verify_delay_passed(ctx, height, connection_end)?; - let ack_path = AcksPath { - port_id: port_id.clone(), - channel_id: channel_id.clone(), - sequence, - }; verify_membership( client_state, connection_end.counterparty().prefix(), proof, root, - ack_path, + ack_path.clone(), ack.into_vec(), ) } @@ -1190,26 +1190,19 @@ impl Ics2ClientState for ClientState { connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + ack_path: &AckPath, ack_commitment: AcknowledgementCommitment, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; client_state.verify_height(height)?; verify_delay_passed(ctx, height, connection_end)?; - let ack_path = AcksPath { - port_id: port_id.clone(), - channel_id: channel_id.clone(), - sequence, - }; verify_membership( client_state, connection_end.counterparty().prefix(), proof, root, - ack_path, + ack_path.clone(), ack_commitment.into_vec(), ) } @@ -1222,8 +1215,7 @@ impl Ics2ClientState for ClientState { connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, + seq_recv_path: &SeqRecvPath, sequence: Sequence, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; @@ -1235,14 +1227,12 @@ impl Ics2ClientState for ClientState { .encode(&mut seq_bytes) .expect("buffer size too small"); - let seq_path = SeqRecvsPath(port_id.clone(), channel_id.clone()); - verify_membership( client_state, connection_end.counterparty().prefix(), proof, root, - seq_path, + seq_recv_path.clone(), seq_bytes, ) } @@ -1254,8 +1244,7 @@ impl Ics2ClientState for ClientState { connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, + seq_recv_path: &SeqRecvPath, sequence: Sequence, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; @@ -1267,14 +1256,12 @@ impl Ics2ClientState for ClientState { .encode(&mut seq_bytes) .expect("buffer size too small"); - let seq_path = SeqRecvsPath(port_id.clone(), channel_id.clone()); - verify_membership( client_state, connection_end.counterparty().prefix(), proof, root, - seq_path, + seq_recv_path.clone(), seq_bytes, ) } @@ -1287,25 +1274,18 @@ impl Ics2ClientState for ClientState { connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + receipt_path: &ReceiptPath, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; client_state.verify_height(height)?; new_verify_delay_passed(ctx, height, connection_end)?; - let receipt_path = ReceiptsPath { - port_id: port_id.clone(), - channel_id: channel_id.clone(), - sequence, - }; verify_non_membership( client_state, connection_end.counterparty().prefix(), proof, root, - receipt_path, + receipt_path.clone(), ) } @@ -1316,25 +1296,18 @@ impl Ics2ClientState for ClientState { connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + receipt_path: &ReceiptPath, ) -> Result<(), ClientError> { let client_state = downcast_tm_client_state(self)?; client_state.verify_height(height)?; verify_delay_passed(ctx, height, connection_end)?; - let receipt_path = ReceiptsPath { - port_id: port_id.clone(), - channel_id: channel_id.clone(), - sequence, - }; verify_non_membership( client_state, connection_end.counterparty().prefix(), proof, root, - receipt_path, + receipt_path.clone(), ) } } diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index d1a7e1e9f..f306f25bc 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -44,8 +44,9 @@ use crate::core::ics05_port::error::PortError::UnknownPort; use crate::core::ics23_commitment::commitment::CommitmentPrefix; use crate::core::ics24_host::identifier::{ChannelId, ConnectionId, PortId}; use crate::core::ics24_host::path::{ - ClientConnectionsPath, ClientConsensusStatePath, ClientStatePath, ClientTypePath, - CommitmentsPath, ConnectionsPath, ReceiptsPath, + AckPath, ChannelEndPath, ClientConnectionPath, ClientConsensusStatePath, ClientStatePath, + ClientTypePath, CommitmentPath, ConnectionPath, ReceiptPath, SeqAckPath, SeqRecvPath, + SeqSendPath, }; use crate::core::ics26_routing::context::{Module, ModuleId}; use crate::core::{ @@ -244,22 +245,19 @@ pub trait ValidationContext: Router { /// Returns an error if no such state exists. fn consensus_state( &self, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result, ContextError>; /// Search for the lowest consensus state higher than `height`. fn next_consensus_state( &self, - client_id: &ClientId, - height: &Height, + next_client_cons_state_path: &ClientConsensusStatePath, ) -> Result>, ContextError>; /// Search for the highest consensus state lower than `height`. fn prev_consensus_state( &self, - client_id: &ClientId, - height: &Height, + prev_client_cons_state_path: &ClientConsensusStatePath, ) -> Result>, ContextError>; /// Returns the current height of the local chain. @@ -316,44 +314,31 @@ pub trait ValidationContext: Router { } /// Returns the ChannelEnd for the given `port_id` and `chan_id`. - fn channel_end( - &self, - port_channel_id: &(PortId, ChannelId), - ) -> Result; + fn channel_end(&self, channel_end_path: &ChannelEndPath) -> Result; fn connection_channels( &self, cid: &ConnectionId, ) -> Result, ContextError>; - fn get_next_sequence_send( - &self, - port_channel_id: &(PortId, ChannelId), - ) -> Result; + fn get_next_sequence_send(&self, seq_send_path: &SeqSendPath) + -> Result; - fn get_next_sequence_recv( - &self, - port_channel_id: &(PortId, ChannelId), - ) -> Result; + fn get_next_sequence_recv(&self, seq_recv_path: &SeqRecvPath) + -> Result; - fn get_next_sequence_ack( - &self, - port_channel_id: &(PortId, ChannelId), - ) -> Result; + fn get_next_sequence_ack(&self, seq_ack_path: &SeqAckPath) -> Result; fn get_packet_commitment( &self, - key: &(PortId, ChannelId, Sequence), + commitment_path: &CommitmentPath, ) -> Result; - fn get_packet_receipt( - &self, - key: &(PortId, ChannelId, Sequence), - ) -> Result; + fn get_packet_receipt(&self, receipt_path: &ReceiptPath) -> Result; fn get_packet_acknowledgement( &self, - key: &(PortId, ChannelId, Sequence), + ack_path: &AckPath, ) -> Result; /// Compute the commitment for a packet. @@ -534,14 +519,14 @@ pub trait ExecutionContext: ValidationContext { /// Stores the given connection_end at path fn store_connection( &mut self, - connections_path: ConnectionsPath, + connection_path: &ConnectionPath, connection_end: ConnectionEnd, ) -> Result<(), ContextError>; /// Stores the given connection_id at a path associated with the client_id. fn store_connection_to_client( &mut self, - client_connections_path: ClientConnectionsPath, + client_connection_path: &ClientConnectionPath, conn_id: ConnectionId, ) -> Result<(), ContextError>; @@ -552,57 +537,51 @@ pub trait ExecutionContext: ValidationContext { fn store_packet_commitment( &mut self, - commitments_path: CommitmentsPath, + commitment_path: &CommitmentPath, commitment: PacketCommitment, ) -> Result<(), ContextError>; - fn delete_packet_commitment(&mut self, key: CommitmentsPath) -> Result<(), ContextError>; + fn delete_packet_commitment( + &mut self, + commitment_path: &CommitmentPath, + ) -> Result<(), ContextError>; fn store_packet_receipt( &mut self, - path: ReceiptsPath, + receipt_path: &ReceiptPath, receipt: Receipt, ) -> Result<(), ContextError>; fn store_packet_acknowledgement( &mut self, - key: (PortId, ChannelId, Sequence), + ack_path: &AckPath, ack_commitment: AcknowledgementCommitment, ) -> Result<(), ContextError>; - fn delete_packet_acknowledgement( - &mut self, - key: (PortId, ChannelId, Sequence), - ) -> Result<(), ContextError>; - - fn store_connection_channels( - &mut self, - conn_id: ConnectionId, - port_channel_id: (PortId, ChannelId), - ) -> Result<(), ContextError>; + fn delete_packet_acknowledgement(&mut self, ack_path: &AckPath) -> Result<(), ContextError>; /// Stores the given channel_end at a path associated with the port_id and channel_id. fn store_channel( &mut self, - port_channel_id: (PortId, ChannelId), + channel_end_path: &ChannelEndPath, channel_end: ChannelEnd, ) -> Result<(), ContextError>; fn store_next_sequence_send( &mut self, - port_channel_id: (PortId, ChannelId), + seq_send_path: &SeqSendPath, seq: Sequence, ) -> Result<(), ContextError>; fn store_next_sequence_recv( &mut self, - port_channel_id: (PortId, ChannelId), + seq_recv_path: &SeqRecvPath, seq: Sequence, ) -> Result<(), ContextError>; fn store_next_sequence_ack( &mut self, - port_channel_id: (PortId, ChannelId), + seq_ack_path: &SeqAckPath, seq: Sequence, ) -> Result<(), ContextError>; @@ -669,7 +648,6 @@ where // state changes { - let port_channel_id_on_a = (msg.port_id_on_a.clone(), chan_id_on_a.clone()); let chan_end_on_a = ChannelEnd::new( State::Init, msg.ordering, @@ -677,18 +655,20 @@ where msg.connection_hops_on_a.clone(), msg.version_proposal.clone(), ); - - ctx_a.store_channel(port_channel_id_on_a.clone(), chan_end_on_a)?; + let chan_end_path_on_a = ChannelEndPath::new(&msg.port_id_on_a, &chan_id_on_a); + ctx_a.store_channel(&chan_end_path_on_a, chan_end_on_a)?; ctx_a.increase_channel_counter(); - // Associate also the channel end to its connection. - ctx_a.store_connection_channels(conn_id_on_a.clone(), port_channel_id_on_a.clone())?; - // Initialize send, recv, and ack sequence numbers. - ctx_a.store_next_sequence_send(port_channel_id_on_a.clone(), 1.into())?; - ctx_a.store_next_sequence_recv(port_channel_id_on_a.clone(), 1.into())?; - ctx_a.store_next_sequence_ack(port_channel_id_on_a, 1.into())?; + let seq_send_path = SeqSendPath::new(&msg.port_id_on_a, &chan_id_on_a); + ctx_a.store_next_sequence_send(&seq_send_path, 1.into())?; + + let seq_recv_path = SeqRecvPath::new(&msg.port_id_on_a, &chan_id_on_a); + ctx_a.store_next_sequence_recv(&seq_recv_path, 1.into())?; + + let seq_ack_path = SeqAckPath::new(&msg.port_id_on_a, &chan_id_on_a); + ctx_a.store_next_sequence_ack(&seq_ack_path, 1.into())?; } // emit events and logs @@ -769,7 +749,6 @@ where // state changes { - let port_channel_id_on_b = (msg.port_id_on_b.clone(), chan_id_on_b.clone()); let chan_end_on_b = ChannelEnd::new( State::TryOpen, msg.ordering, @@ -778,17 +757,19 @@ where version.clone(), ); - ctx_b.store_channel(port_channel_id_on_b.clone(), chan_end_on_b)?; - + let chan_end_path_on_b = ChannelEndPath::new(&msg.port_id_on_b, &chan_id_on_b); + ctx_b.store_channel(&chan_end_path_on_b, chan_end_on_b)?; ctx_b.increase_channel_counter(); - // Associate also the channel end to its connection. - ctx_b.store_connection_channels(conn_id_on_b.clone(), port_channel_id_on_b.clone())?; - // Initialize send, recv, and ack sequence numbers. - ctx_b.store_next_sequence_send(port_channel_id_on_b.clone(), 1.into())?; - ctx_b.store_next_sequence_recv(port_channel_id_on_b.clone(), 1.into())?; - ctx_b.store_next_sequence_ack(port_channel_id_on_b, 1.into())?; + let seq_send_path = SeqSendPath::new(&msg.port_id_on_b, &chan_id_on_b); + ctx_b.store_next_sequence_send(&seq_send_path, 1.into())?; + + let seq_recv_path = SeqRecvPath::new(&msg.port_id_on_b, &chan_id_on_b); + ctx_b.store_next_sequence_recv(&seq_recv_path, 1.into())?; + + let seq_ack_path = SeqAckPath::new(&msg.port_id_on_b, &chan_id_on_b); + ctx_b.store_next_sequence_ack(&seq_ack_path, 1.into())?; } // emit events and logs @@ -850,12 +831,11 @@ where .ok_or(ChannelError::RouteNotFound)?; let extras = module.on_chan_open_ack_execute(&msg.port_id_on_a, &msg.chan_id_on_a, &msg.version_on_b)?; - - let chan_end_on_a = ctx_a.channel_end(&(msg.port_id_on_a.clone(), msg.chan_id_on_a.clone()))?; + let chan_end_path_on_a = ChannelEndPath::new(&msg.port_id_on_a, &msg.chan_id_on_a); + let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?; // state changes { - let port_channel_id_on_a = (msg.port_id_on_a.clone(), msg.chan_id_on_a.clone()); let chan_end_on_a = { let mut chan_end_on_a = chan_end_on_a.clone(); @@ -865,8 +845,7 @@ where chan_end_on_a }; - - ctx_a.store_channel(port_channel_id_on_a, chan_end_on_a)?; + ctx_a.store_channel(&chan_end_path_on_a, chan_end_on_a)?; } // emit events and logs @@ -930,20 +909,18 @@ where .ok_or(ChannelError::RouteNotFound)?; let extras = module.on_chan_open_confirm_execute(&msg.port_id_on_b, &msg.chan_id_on_b)?; - - let chan_end_on_b = ctx_b.channel_end(&(msg.port_id_on_b.clone(), msg.chan_id_on_b.clone()))?; + let chan_end_path_on_b = ChannelEndPath::new(&msg.port_id_on_b, &msg.chan_id_on_b); + let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; // state changes { - let port_channel_id_on_b = (msg.port_id_on_b.clone(), msg.chan_id_on_b.clone()); let chan_end_on_b = { let mut chan_end_on_b = chan_end_on_b.clone(); chan_end_on_b.set_state(State::Open); chan_end_on_b }; - - ctx_b.store_channel(port_channel_id_on_b, chan_end_on_b)?; + ctx_b.store_channel(&chan_end_path_on_b, chan_end_on_b)?; } // emit events and logs @@ -1013,8 +990,8 @@ where .get_route_mut(&module_id) .ok_or(ChannelError::RouteNotFound)?; let extras = module.on_chan_close_init_execute(&msg.port_id_on_a, &msg.chan_id_on_a)?; - - let chan_end_on_a = ctx_a.channel_end(&(msg.port_id_on_a.clone(), msg.chan_id_on_a.clone()))?; + let chan_end_path_on_a = ChannelEndPath::new(&msg.port_id_on_a, &msg.chan_id_on_a); + let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?; // state changes { @@ -1024,8 +1001,7 @@ where chan_end_on_a }; - let port_channel_id_on_a = (msg.port_id_on_a.clone(), msg.chan_id_on_a.clone()); - ctx_a.store_channel(port_channel_id_on_a, chan_end_on_a)?; + ctx_a.store_channel(&chan_end_path_on_a, chan_end_on_a)?; } // emit events and logs @@ -1097,8 +1073,8 @@ where .get_route_mut(&module_id) .ok_or(ChannelError::RouteNotFound)?; let extras = module.on_chan_close_confirm_execute(&msg.port_id_on_b, &msg.chan_id_on_b)?; - - let chan_end_on_b = ctx_b.channel_end(&(msg.port_id_on_b.clone(), msg.chan_id_on_b.clone()))?; + let chan_end_path_on_b = ChannelEndPath::new(&msg.port_id_on_b, &msg.chan_id_on_b); + let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; // state changes { @@ -1107,9 +1083,7 @@ where chan_end_on_b.set_state(State::Closed); chan_end_on_b }; - - let port_channel_id_on_b = (msg.port_id_on_b.clone(), msg.chan_id_on_b.clone()); - ctx_b.store_channel(port_channel_id_on_b, chan_end_on_b)?; + ctx_b.store_channel(&chan_end_path_on_b, chan_end_on_b)?; } // emit events and logs @@ -1169,8 +1143,8 @@ fn recv_packet_execute( where ExecCtx: ExecutionContext, { - let chan_port_id_on_b = (msg.packet.port_on_b.clone(), msg.packet.chan_on_b.clone()); - let chan_end_on_b = ctx_b.channel_end(&chan_port_id_on_b)?; + let chan_end_path_on_b = ChannelEndPath::new(&msg.packet.port_on_b, &msg.packet.chan_on_b); + let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; // Check if another relayer already relayed the packet. // We don't want to fail the transaction in this case. @@ -1180,13 +1154,14 @@ where Order::None => false, Order::Unordered => { let packet = msg.packet.clone(); - - ctx_b - .get_packet_receipt(&(packet.port_on_b, packet.chan_on_b, packet.sequence)) - .is_ok() + let receipt_path_on_b = + ReceiptPath::new(&packet.port_on_b, &packet.chan_on_b, packet.sequence); + ctx_b.get_packet_receipt(&receipt_path_on_b).is_ok() } Order::Ordered => { - let next_seq_recv = ctx_b.get_next_sequence_recv(&chan_port_id_on_b)?; + let seq_recv_path_on_b = + SeqRecvPath::new(&msg.packet.port_on_b, &msg.packet.chan_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 @@ -1210,33 +1185,30 @@ where // `recvPacket` core handler state changes match chan_end_on_b.ordering { Order::Unordered => { - let path = ReceiptsPath { + let receipt_path_on_b = ReceiptPath { port_id: msg.packet.port_on_b.clone(), channel_id: msg.packet.chan_on_b.clone(), sequence: msg.packet.sequence, }; - ctx_b.store_packet_receipt(path, Receipt::Ok)?; + ctx_b.store_packet_receipt(&receipt_path_on_b, Receipt::Ok)?; } Order::Ordered => { - let port_chan_id_on_b = - (msg.packet.port_on_b.clone(), msg.packet.chan_on_b.clone()); - let next_seq_recv = ctx_b.get_next_sequence_recv(&port_chan_id_on_b)?; - - ctx_b.store_next_sequence_recv(port_chan_id_on_b, next_seq_recv.increment())?; + let seq_recv_path_on_b = + SeqRecvPath::new(&msg.packet.port_on_b, &msg.packet.chan_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_on_b, + &msg.packet.chan_on_b, + msg.packet.sequence, + ); // `writeAcknowledgement` handler state changes - ctx_b.store_packet_acknowledgement( - ( - msg.packet.port_on_b.clone(), - msg.packet.chan_on_b.clone(), - msg.packet.sequence, - ), - ctx_b.ack_commitment(&acknowledgement), - )?; + ctx_b + .store_packet_acknowledgement(&ack_path_on_b, ctx_b.ack_commitment(&acknowledgement))?; } // emit events and logs @@ -1295,8 +1267,8 @@ fn acknowledgement_packet_execute( where ExecCtx: ExecutionContext, { - let port_chan_id_on_a = (msg.packet.port_on_a.clone(), msg.packet.chan_on_a.clone()); - let chan_end_on_a = ctx_a.channel_end(&port_chan_id_on_a)?; + let chan_end_path_on_a = ChannelEndPath::new(&msg.packet.port_on_a, &msg.packet.chan_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 @@ -1306,15 +1278,14 @@ where conn_id_on_a.clone(), ))); + let commitment_path_on_a = CommitmentPath::new( + &msg.packet.port_on_a, + &msg.packet.chan_on_a, + msg.packet.sequence, + ); + // check if we're in the NO-OP case - if ctx_a - .get_packet_commitment(&( - msg.packet.port_on_a.clone(), - msg.packet.chan_on_a.clone(), - msg.packet.sequence, - )) - .is_err() - { + if ctx_a.get_packet_commitment(&commitment_path_on_a).is_err() { // This error indicates that the timeout has already been relayed // or there is a misconfigured relayer attempting to prove a timeout // for a packet never sent. Core IBC will treat this error as a no-op in order to @@ -1333,17 +1304,18 @@ where // apply state changes { - let commitment_path = CommitmentsPath { + let commitment_path_on_a = CommitmentPath { port_id: msg.packet.port_on_a.clone(), channel_id: msg.packet.chan_on_a.clone(), sequence: msg.packet.sequence, }; - ctx_a.delete_packet_commitment(commitment_path)?; + 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) - ctx_a.store_next_sequence_ack(port_chan_id_on_a, msg.packet.sequence.increment())?; + let seq_ack_path_on_a = SeqAckPath::new(&msg.packet.port_on_a, &msg.packet.chan_on_a); + ctx_a.store_next_sequence_ack(&seq_ack_path_on_a, msg.packet.sequence.increment())?; } } @@ -1409,9 +1381,8 @@ where TimeoutMsgType::Timeout(msg) => (msg.packet, msg.signer), TimeoutMsgType::TimeoutOnClose(msg) => (msg.packet, msg.signer), }; - - let port_chan_id_on_a = (packet.port_on_a.clone(), packet.chan_on_a.clone()); - let chan_end_on_a = ctx_a.channel_end(&port_chan_id_on_a)?; + let chan_end_path_on_a = ChannelEndPath::new(&packet.port_on_a, &packet.chan_on_a); + let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?; // In all cases, this event is emitted ctx_a.emit_ibc_event(IbcEvent::TimeoutPacket(TimeoutPacket::new( @@ -1419,15 +1390,11 @@ where chan_end_on_a.ordering, ))); + let commitment_path_on_a = + CommitmentPath::new(&packet.port_on_a, &packet.chan_on_a, packet.sequence); + // check if we're in the NO-OP case - if ctx_a - .get_packet_commitment(&( - packet.port_on_a.clone(), - packet.chan_on_a.clone(), - packet.sequence, - )) - .is_err() - { + if ctx_a.get_packet_commitment(&commitment_path_on_a).is_err() { // This error indicates that the timeout has already been relayed // or there is a misconfigured relayer attempting to prove a timeout // for a packet never sent. Core IBC will treat this error as a no-op in order to @@ -1445,17 +1412,17 @@ where // apply state changes let chan_end_on_a = { - let commitment_path = CommitmentsPath { + let commitment_path_on_a = CommitmentPath { port_id: packet.port_on_a.clone(), channel_id: packet.chan_on_a.clone(), sequence: packet.sequence, }; - ctx_a.delete_packet_commitment(commitment_path)?; + 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(port_chan_id_on_a, chan_end_on_a.clone())?; + ctx_a.store_channel(&chan_end_path_on_a, chan_end_on_a.clone())?; chan_end_on_a } else { diff --git a/crates/ibc/src/core/ics02_client/client_state.rs b/crates/ibc/src/core/ics02_client/client_state.rs index d641e21b8..53978de50 100644 --- a/crates/ibc/src/core/ics02_client/client_state.rs +++ b/crates/ibc/src/core/ics02_client/client_state.rs @@ -16,7 +16,11 @@ use crate::core::ics04_channel::packet::Sequence; use crate::core::ics23_commitment::commitment::{ CommitmentPrefix, CommitmentProofBytes, CommitmentRoot, }; -use crate::core::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId}; +use crate::core::ics24_host::identifier::{ChainId, ClientId}; +use crate::core::ics24_host::path::{ + AckPath, ChannelEndPath, ClientConsensusStatePath, ClientStatePath, CommitmentPath, + ConnectionPath, ReceiptPath, SeqRecvPath, +}; use crate::dynamic_typing::AsAny; use crate::erased::ErasedSerialize; use crate::prelude::*; @@ -139,52 +143,46 @@ pub trait ClientState: /// matches the input `consensus_state`. The parameter `counterparty_height` represent the /// height of the counterparty chain that this proof assumes (i.e., the height at which this /// proof was computed). - #[allow(clippy::too_many_arguments)] fn verify_client_consensus_state( &self, proof_height: Height, counterparty_prefix: &CommitmentPrefix, proof: &CommitmentProofBytes, root: &CommitmentRoot, - counterparty_client_id: &ClientId, - consensus_height: Height, + client_cons_state_path: &ClientConsensusStatePath, expected_consensus_state: &dyn ConsensusState, ) -> Result<(), ClientError>; /// Verify a `proof` that a connection state matches that of the input `connection_end`. - #[allow(clippy::too_many_arguments)] fn verify_connection_state( &self, proof_height: Height, counterparty_prefix: &CommitmentPrefix, proof: &CommitmentProofBytes, root: &CommitmentRoot, - counterparty_connection_id: &ConnectionId, + counterparty_conn_path: &ConnectionPath, expected_counterparty_connection_end: &ConnectionEnd, ) -> Result<(), ClientError>; /// Verify a `proof` that a channel state matches that of the input `channel_end`. - #[allow(clippy::too_many_arguments)] fn verify_channel_state( &self, proof_height: Height, counterparty_prefix: &CommitmentPrefix, proof: &CommitmentProofBytes, root: &CommitmentRoot, - counterparty_port_id: &PortId, - counterparty_channel_id: &ChannelId, + counterparty_chan_end_path: &ChannelEndPath, expected_counterparty_channel_end: &ChannelEnd, ) -> Result<(), ClientError>; /// Verify the client state for this chain that it is stored on the counterparty chain. - #[allow(clippy::too_many_arguments)] fn verify_client_full_state( &self, proof_height: Height, counterparty_prefix: &CommitmentPrefix, proof: &CommitmentProofBytes, root: &CommitmentRoot, - client_id: &ClientId, + client_state_path: &ClientStatePath, expected_client_state: Any, ) -> Result<(), ClientError>; @@ -198,9 +196,7 @@ pub trait ClientState: connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + commitment_path: &CommitmentPath, commitment: PacketCommitment, ) -> Result<(), ClientError>; @@ -213,9 +209,7 @@ pub trait ClientState: connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + commitment_path: &CommitmentPath, commitment: PacketCommitment, ) -> Result<(), ClientError>; @@ -229,9 +223,7 @@ pub trait ClientState: connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + ack_path: &AckPath, ack: AcknowledgementCommitment, ) -> Result<(), ClientError>; @@ -244,9 +236,7 @@ pub trait ClientState: connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + ack_path: &AckPath, ack: AcknowledgementCommitment, ) -> Result<(), ClientError>; @@ -260,8 +250,7 @@ pub trait ClientState: connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, + seq_recv_path: &SeqRecvPath, sequence: Sequence, ) -> Result<(), ClientError>; @@ -274,14 +263,11 @@ pub trait ClientState: connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, + seq_recv_path: &SeqRecvPath, sequence: Sequence, ) -> Result<(), ClientError>; /// Verify a `proof` that a packet has not been received. - - #[allow(clippy::too_many_arguments)] fn new_verify_packet_receipt_absence( &self, ctx: &dyn ValidationContext, @@ -289,13 +275,10 @@ pub trait ClientState: connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + receipt_path: &ReceiptPath, ) -> Result<(), ClientError>; /// Verify a `proof` that a packet has not been received. - #[allow(clippy::too_many_arguments)] fn verify_packet_receipt_absence( &self, ctx: &dyn ChannelReader, @@ -303,9 +286,7 @@ pub trait ClientState: connection_end: &ConnectionEnd, proof: &CommitmentProofBytes, root: &CommitmentRoot, - port_id: &PortId, - channel_id: &ChannelId, - sequence: Sequence, + receipt_path: &ReceiptPath, ) -> Result<(), ClientError>; } diff --git a/crates/ibc/src/core/ics02_client/context.rs b/crates/ibc/src/core/ics02_client/context.rs index c9225e62d..434d3ae3e 100644 --- a/crates/ibc/src/core/ics02_client/context.rs +++ b/crates/ibc/src/core/ics02_client/context.rs @@ -12,6 +12,7 @@ use crate::core::ics02_client::consensus_state::ConsensusState; use crate::core::ics02_client::error::ClientError; use crate::core::ics02_client::handler::ClientResult::{self, Create, Update, Upgrade}; use crate::core::ics24_host::identifier::ClientId; +use crate::core::ics24_host::path::ClientConsensusStatePath; use crate::timestamp::Timestamp; use crate::Height; @@ -32,22 +33,19 @@ pub trait ClientReader { /// Returns an error if no such state exists. fn consensus_state( &self, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result, ClientError>; /// Search for the lowest consensus state higher than `height`. fn next_consensus_state( &self, - client_id: &ClientId, - height: &Height, + next_client_cons_state_path: &ClientConsensusStatePath, ) -> Result>, ClientError>; /// Search for the highest consensus state lower than `height`. fn prev_consensus_state( &self, - client_id: &ClientId, - height: &Height, + prev_client_cons_state_path: &ClientConsensusStatePath, ) -> Result>, ClientError>; /// Returns the current height of the local chain. diff --git a/crates/ibc/src/core/ics02_client/handler/create_client.rs b/crates/ibc/src/core/ics02_client/handler/create_client.rs index 278db2f13..d170e77b7 100644 --- a/crates/ibc/src/core/ics02_client/handler/create_client.rs +++ b/crates/ibc/src/core/ics02_client/handler/create_client.rs @@ -93,10 +93,10 @@ where })?; let consensus_state = client_state.initialise(consensus_state)?; - ctx.store_client_type(ClientTypePath(client_id.clone()), client_type.clone())?; - ctx.store_client_state(ClientStatePath(client_id.clone()), client_state.clone())?; + ctx.store_client_type(ClientTypePath::new(&client_id), client_type.clone())?; + ctx.store_client_state(ClientStatePath::new(&client_id), client_state.clone())?; ctx.store_consensus_state( - ClientConsensusStatePath::new(client_id.clone(), client_state.latest_height()), + ClientConsensusStatePath::new(&client_id, &client_state.latest_height()), consensus_state, )?; ctx.increase_client_counter(); diff --git a/crates/ibc/src/core/ics02_client/handler/misbehaviour.rs b/crates/ibc/src/core/ics02_client/handler/misbehaviour.rs index 45ce51d6c..c46d085bf 100644 --- a/crates/ibc/src/core/ics02_client/handler/misbehaviour.rs +++ b/crates/ibc/src/core/ics02_client/handler/misbehaviour.rs @@ -77,7 +77,7 @@ where client_state.client_type(), ))); - ctx.store_client_state(ClientStatePath(client_id), client_state) + ctx.store_client_state(ClientStatePath::new(&client_id), client_state) } pub(crate) fn process( diff --git a/crates/ibc/src/core/ics02_client/handler/update_client.rs b/crates/ibc/src/core/ics02_client/handler/update_client.rs index 5d5f36a3d..f3e1044fd 100644 --- a/crates/ibc/src/core/ics02_client/handler/update_client.rs +++ b/crates/ibc/src/core/ics02_client/handler/update_client.rs @@ -52,8 +52,10 @@ where } // Read consensus state from the host chain store. + let latest_client_cons_state_path = + ClientConsensusStatePath::new(&client_id, &client_state.latest_height()); let latest_consensus_state = ctx - .consensus_state(&client_id, &client_state.latest_height()) + .consensus_state(&latest_client_cons_state_path) .map_err(|_| ClientError::ConsensusStateNotFound { client_id: client_id.clone(), height: client_state.latest_height(), @@ -109,9 +111,9 @@ where reason: e.to_string(), })?; - ctx.store_client_state(ClientStatePath(client_id.clone()), client_state.clone())?; + ctx.store_client_state(ClientStatePath::new(&client_id), client_state.clone())?; ctx.store_consensus_state( - ClientConsensusStatePath::new(client_id.clone(), client_state.latest_height()), + ClientConsensusStatePath::new(&client_id, &client_state.latest_height()), consensus_state, )?; ctx.store_update_time( @@ -161,13 +163,13 @@ pub(crate) fn process( } // Read consensus state from the host chain store. - let latest_consensus_state = - ClientReader::consensus_state(ctx, &client_id, &client_state.latest_height()).map_err( - |_| ClientError::ConsensusStateNotFound { - client_id: client_id.clone(), - height: client_state.latest_height(), - }, - )?; + let client_cons_state_path = + ClientConsensusStatePath::new(&client_id, &client_state.latest_height()); + let latest_consensus_state = ClientReader::consensus_state(ctx, &client_cons_state_path) + .map_err(|_| ClientError::ConsensusStateNotFound { + client_id: client_id.clone(), + height: client_state.latest_height(), + })?; debug!("latest consensus state: {:?}", latest_consensus_state); diff --git a/crates/ibc/src/core/ics02_client/handler/upgrade_client.rs b/crates/ibc/src/core/ics02_client/handler/upgrade_client.rs index 4ca8d1277..b4ecff92d 100644 --- a/crates/ibc/src/core/ics02_client/handler/upgrade_client.rs +++ b/crates/ibc/src/core/ics02_client/handler/upgrade_client.rs @@ -51,8 +51,10 @@ where } // Read the latest consensus state from the host chain store. + let old_client_cons_state_path = + ClientConsensusStatePath::new(&client_id, &old_client_state.latest_height()); let old_consensus_state = ctx - .consensus_state(&client_id, &old_client_state.latest_height()) + .consensus_state(&old_client_cons_state_path) .map_err(|_| ClientError::ConsensusStateNotFound { client_id: client_id.clone(), height: old_client_state.latest_height(), @@ -102,9 +104,9 @@ where } = old_client_state .update_state_with_upgrade_client(msg.client_state.clone(), msg.consensus_state)?; - ctx.store_client_state(ClientStatePath(client_id.clone()), client_state.clone())?; + ctx.store_client_state(ClientStatePath::new(&client_id), client_state.clone())?; ctx.store_consensus_state( - ClientConsensusStatePath::new(client_id.clone(), client_state.latest_height()), + ClientConsensusStatePath::new(&client_id, &client_state.latest_height()), consensus_state, )?; @@ -140,8 +142,10 @@ pub(crate) fn process( } // Read the latest consensus state from the host chain store. + let old_client_cons_state_path = + ClientConsensusStatePath::new(&client_id, &old_client_state.latest_height()); let old_consensus_state = ctx - .consensus_state(&client_id, &old_client_state.latest_height()) + .consensus_state(&old_client_cons_state_path) .map_err(|_| ClientError::ConsensusStateNotFound { client_id: client_id.clone(), height: old_client_state.latest_height(), diff --git a/crates/ibc/src/core/ics03_connection/context.rs b/crates/ibc/src/core/ics03_connection/context.rs index 09229a72f..e4f2b404a 100644 --- a/crates/ibc/src/core/ics03_connection/context.rs +++ b/crates/ibc/src/core/ics03_connection/context.rs @@ -10,6 +10,7 @@ use crate::core::ics03_connection::handler::ConnectionResult; use crate::core::ics03_connection::version::{get_compatible_versions, pick_version, Version}; use crate::core::ics23_commitment::commitment::CommitmentPrefix; use crate::core::ics24_host::identifier::{ClientId, ConnectionId}; +use crate::core::ics24_host::path::ClientConsensusStatePath; use crate::prelude::*; use crate::Height; use ibc_proto::google::protobuf::Any; @@ -43,8 +44,7 @@ pub trait ConnectionReader { /// Returns the ConsensusState that the given client stores at a specific height. fn client_consensus_state( &self, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result, ConnectionError>; /// Returns the ConsensusState of the host (local) chain at a specific height. diff --git a/crates/ibc/src/core/ics03_connection/handler/conn_open_ack.rs b/crates/ibc/src/core/ics03_connection/handler/conn_open_ack.rs index e4314c7df..f333a2f61 100644 --- a/crates/ibc/src/core/ics03_connection/handler/conn_open_ack.rs +++ b/crates/ibc/src/core/ics03_connection/handler/conn_open_ack.rs @@ -14,7 +14,7 @@ use crate::core::context::ContextError; use crate::core::ics24_host::identifier::ClientId; -use crate::core::ics24_host::path::ConnectionsPath; +use crate::core::ics24_host::path::{ClientConsensusStatePath, ClientStatePath, ConnectionPath}; use crate::core::{ExecutionContext, ValidationContext}; @@ -66,8 +66,10 @@ where .map_err(|_| ConnectionError::Other { description: "failed to fetch client state".to_string(), })?; + let client_cons_state_path_on_a = + ClientConsensusStatePath::new(vars.client_id_on_a(), &msg.proofs_height_on_b); let consensus_state_of_b_on_a = ctx_a - .consensus_state(vars.client_id_on_a(), &msg.proofs_height_on_b) + .consensus_state(&client_cons_state_path_on_a) .map_err(|_| ConnectionError::Other { description: "failed to fetch client consensus state".to_string(), })?; @@ -94,7 +96,7 @@ where prefix_on_b, &msg.proof_conn_end_on_b, consensus_state_of_b_on_a.root(), - &msg.conn_id_on_b, + &ConnectionPath::new(&msg.conn_id_on_b), &expected_conn_end_on_b, ) .map_err(ConnectionError::VerifyConnectionState)?; @@ -106,7 +108,7 @@ where prefix_on_b, &msg.proof_client_state_of_a_on_b, consensus_state_of_b_on_a.root(), - vars.client_id_on_b(), + &ClientStatePath::new(vars.client_id_on_a()), msg.client_state_of_a_on_b.clone(), ) .map_err(|e| ConnectionError::ClientStateVerificationFailure { @@ -120,14 +122,16 @@ where description: "failed to fetch host consensus state".to_string(), })?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(vars.client_id_on_b(), &msg.consensus_height_of_a_on_b); + client_state_of_b_on_a .verify_client_consensus_state( msg.proofs_height_on_b, prefix_on_b, &msg.proof_consensus_state_of_a_on_b, consensus_state_of_b_on_a.root(), - vars.client_id_on_b(), - msg.consensus_height_of_a_on_b, + &client_cons_state_path_on_b, expected_consensus_state_of_a_on_b.as_ref(), ) .map_err(|e| ConnectionError::ConsensusStateVerificationFailure { @@ -176,7 +180,7 @@ where new_conn_end_on_a }; - ctx_a.store_connection(ConnectionsPath(msg.conn_id_on_a), new_conn_end_on_a)?; + ctx_a.store_connection(&ConnectionPath::new(&msg.conn_id_on_a), new_conn_end_on_a)?; } Ok(()) @@ -236,8 +240,10 @@ pub(crate) fn process( // Proof verification. { let client_state_of_b_on_a = ctx_a.client_state(client_id_on_a)?; + let client_cons_state_path_on_a = + ClientConsensusStatePath::new(conn_end_on_a.client_id(), &msg.proofs_height_on_b); let consensus_state_of_b_on_a = - ctx_a.client_consensus_state(conn_end_on_a.client_id(), &msg.proofs_height_on_b)?; + ctx_a.client_consensus_state(&client_cons_state_path_on_a)?; let prefix_on_a = ctx_a.commitment_prefix(); let prefix_on_b = conn_end_on_a.counterparty().prefix(); @@ -261,7 +267,7 @@ pub(crate) fn process( prefix_on_b, &msg.proof_conn_end_on_b, consensus_state_of_b_on_a.root(), - &msg.conn_id_on_b, + &ConnectionPath::new(&msg.conn_id_on_b), &expected_conn_end_on_b, ) .map_err(ConnectionError::VerifyConnectionState)?; @@ -273,7 +279,7 @@ pub(crate) fn process( prefix_on_b, &msg.proof_client_state_of_a_on_b, consensus_state_of_b_on_a.root(), - client_id_on_b, + &ClientStatePath::new(client_id_on_b), msg.client_state_of_a_on_b, ) .map_err(|e| ConnectionError::ClientStateVerificationFailure { @@ -283,14 +289,18 @@ pub(crate) fn process( let expected_consensus_state_of_a_on_b = ctx_a.host_consensus_state(&msg.consensus_height_of_a_on_b)?; + + let client_cons_state_path_on_b = ClientConsensusStatePath::new( + conn_end_on_a.counterparty().client_id(), + &msg.consensus_height_of_a_on_b, + ); client_state_of_b_on_a .verify_client_consensus_state( msg.proofs_height_on_b, prefix_on_b, &msg.proof_consensus_state_of_a_on_b, consensus_state_of_b_on_a.root(), - conn_end_on_a.counterparty().client_id(), - msg.consensus_height_of_a_on_b, + &client_cons_state_path_on_b, expected_consensus_state_of_a_on_b.as_ref(), ) .map_err(|e| ConnectionError::ConsensusStateVerificationFailure { diff --git a/crates/ibc/src/core/ics03_connection/handler/conn_open_confirm.rs b/crates/ibc/src/core/ics03_connection/handler/conn_open_confirm.rs index 6f28b3c14..486b0efbd 100644 --- a/crates/ibc/src/core/ics03_connection/handler/conn_open_confirm.rs +++ b/crates/ibc/src/core/ics03_connection/handler/conn_open_confirm.rs @@ -14,7 +14,7 @@ use crate::core::context::ContextError; use crate::core::ics24_host::identifier::{ClientId, ConnectionId}; -use crate::core::ics24_host::path::ConnectionsPath; +use crate::core::ics24_host::path::{ClientConsensusStatePath, ConnectionPath}; use crate::core::{ExecutionContext, ValidationContext}; @@ -54,8 +54,10 @@ where .map_err(|_| ConnectionError::Other { description: "failed to fetch client state".to_string(), })?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(client_id_on_b, &msg.proof_height_on_a); let consensus_state_of_a_on_b = ctx_b - .consensus_state(client_id_on_b, &msg.proof_height_on_a) + .consensus_state(&client_cons_state_path_on_b) .map_err(|_| ConnectionError::Other { description: "failed to fetch client consensus state".to_string(), })?; @@ -81,7 +83,7 @@ where prefix_on_a, &msg.proof_conn_end_on_a, consensus_state_of_a_on_b.root(), - conn_id_on_a, + &ConnectionPath::new(conn_id_on_a), &expected_conn_end_on_a, ) .map_err(ConnectionError::VerifyConnectionState)?; @@ -129,7 +131,7 @@ where new_conn_end_on_b }; - ctx_b.store_connection(ConnectionsPath(msg.conn_id_on_b.clone()), new_conn_end_on_b)?; + ctx_b.store_connection(&ConnectionPath(msg.conn_id_on_b.clone()), new_conn_end_on_b)?; } Ok(()) @@ -192,8 +194,10 @@ pub(crate) fn process( // Verify proofs { let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(client_id_on_b, &msg.proof_height_on_a); let consensus_state_of_a_on_b = - ctx_b.client_consensus_state(client_id_on_b, &msg.proof_height_on_a)?; + ctx_b.client_consensus_state(&client_cons_state_path_on_b)?; let prefix_on_a = conn_end_on_b.counterparty().prefix(); let prefix_on_b = ctx_b.commitment_prefix(); @@ -216,7 +220,7 @@ pub(crate) fn process( prefix_on_a, &msg.proof_conn_end_on_a, consensus_state_of_a_on_b.root(), - conn_id_on_a, + &ConnectionPath::new(conn_id_on_a), &expected_conn_end_on_a, ) .map_err(ConnectionError::VerifyConnectionState)?; diff --git a/crates/ibc/src/core/ics03_connection/handler/conn_open_init.rs b/crates/ibc/src/core/ics03_connection/handler/conn_open_init.rs index a1624ae25..0810db3ec 100644 --- a/crates/ibc/src/core/ics03_connection/handler/conn_open_init.rs +++ b/crates/ibc/src/core/ics03_connection/handler/conn_open_init.rs @@ -13,7 +13,7 @@ use crate::handler::{HandlerOutput, HandlerResult}; use crate::core::context::ContextError; -use crate::core::ics24_host::path::{ClientConnectionsPath, ConnectionsPath}; +use crate::core::ics24_host::path::{ClientConnectionPath, ConnectionPath}; use crate::core::{ExecutionContext, ValidationContext}; @@ -81,10 +81,10 @@ where ctx_a.increase_connection_counter(); ctx_a.store_connection_to_client( - ClientConnectionsPath(msg.client_id_on_a), + &ClientConnectionPath::new(&msg.client_id_on_a), conn_id_on_a.clone(), )?; - ctx_a.store_connection(ConnectionsPath(conn_id_on_a), conn_end_on_a)?; + ctx_a.store_connection(&ConnectionPath::new(&conn_id_on_a), conn_end_on_a)?; Ok(()) } diff --git a/crates/ibc/src/core/ics03_connection/handler/conn_open_try.rs b/crates/ibc/src/core/ics03_connection/handler/conn_open_try.rs index df2023500..f111c19c9 100644 --- a/crates/ibc/src/core/ics03_connection/handler/conn_open_try.rs +++ b/crates/ibc/src/core/ics03_connection/handler/conn_open_try.rs @@ -17,7 +17,9 @@ use crate::core::context::ContextError; use crate::core::ics24_host::identifier::ClientId; -use crate::core::ics24_host::path::{ClientConnectionsPath, ConnectionsPath}; +use crate::core::ics24_host::path::{ + ClientConnectionPath, ClientConsensusStatePath, ClientStatePath, ConnectionPath, +}; use crate::core::{ExecutionContext, ValidationContext}; @@ -61,8 +63,10 @@ where .map_err(|_| ConnectionError::Other { description: "failed to fetch client state".to_string(), })?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(&msg.client_id_on_b, &msg.proofs_height_on_a); let consensus_state_of_a_on_b = ctx_b - .consensus_state(&msg.client_id_on_b, &msg.proofs_height_on_a) + .consensus_state(&client_cons_state_path_on_b) .map_err(|_| ConnectionError::Other { description: "failed to fetch client consensus state".to_string(), })?; @@ -85,7 +89,7 @@ where prefix_on_a, &msg.proof_conn_end_on_a, consensus_state_of_a_on_b.root(), - &vars.conn_id_on_a, + &ConnectionPath::new(&vars.conn_id_on_a), &expected_conn_end_on_a, ) .map_err(ConnectionError::VerifyConnectionState)?; @@ -97,7 +101,7 @@ where prefix_on_a, &msg.proof_client_state_of_b_on_a, consensus_state_of_a_on_b.root(), - client_id_on_a, + &ClientStatePath::new(client_id_on_a), msg.client_state_of_b_on_a.clone(), ) .map_err(|e| ConnectionError::ClientStateVerificationFailure { @@ -110,14 +114,16 @@ where .map_err(|_| ConnectionError::Other { description: "failed to fetch host consensus state".to_string(), })?; + + let client_cons_state_path_on_a = + ClientConsensusStatePath::new(&msg.client_id_on_b, &msg.consensus_height_of_b_on_a); client_state_of_a_on_b .verify_client_consensus_state( msg.proofs_height_on_a, prefix_on_a, &msg.proof_consensus_state_of_b_on_a, consensus_state_of_a_on_b.root(), - client_id_on_a, - msg.consensus_height_of_b_on_a, + &client_cons_state_path_on_a, expected_consensus_state_of_b_on_a.as_ref(), ) .map_err(|e| ConnectionError::ClientStateVerificationFailure { @@ -160,10 +166,10 @@ where ctx_b.increase_connection_counter(); ctx_b.store_connection_to_client( - ClientConnectionsPath(msg.client_id_on_b), + &ClientConnectionPath::new(&msg.client_id_on_b), vars.conn_id_on_b.clone(), )?; - ctx_b.store_connection(ConnectionsPath(vars.conn_id_on_b), vars.conn_end_on_b)?; + ctx_b.store_connection(&ConnectionPath::new(&vars.conn_id_on_b), vars.conn_end_on_b)?; Ok(()) } @@ -240,8 +246,10 @@ pub(crate) fn process( // Verify proofs { let client_state_of_a_on_b = ctx_b.client_state(conn_end_on_b.client_id())?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(&msg.client_id_on_b, &msg.proofs_height_on_a); let consensus_state_of_a_on_b = - ctx_b.client_consensus_state(&msg.client_id_on_b, &msg.proofs_height_on_a)?; + ctx_b.client_consensus_state(&client_cons_state_path_on_b)?; let prefix_on_a = conn_end_on_b.counterparty().prefix(); let prefix_on_b = ctx_b.commitment_prefix(); @@ -262,7 +270,7 @@ pub(crate) fn process( prefix_on_a, &msg.proof_conn_end_on_a, consensus_state_of_a_on_b.root(), - conn_id_on_a, + &ConnectionPath::new(conn_id_on_a), &expected_conn_end_on_a, ) .map_err(ConnectionError::VerifyConnectionState)?; @@ -274,7 +282,7 @@ pub(crate) fn process( prefix_on_a, &msg.proof_client_state_of_b_on_a, consensus_state_of_a_on_b.root(), - client_id_on_a, + &ClientStatePath::new(client_id_on_a), msg.client_state_of_b_on_a, ) .map_err(|e| ConnectionError::ClientStateVerificationFailure { @@ -284,14 +292,16 @@ pub(crate) fn process( let expected_consensus_state_of_b_on_a = ctx_b.host_consensus_state(&msg.consensus_height_of_b_on_a)?; + + let client_cons_state_path_on_a = + ClientConsensusStatePath::new(client_id_on_a, &msg.consensus_height_of_b_on_a); client_state_of_a_on_b .verify_client_consensus_state( msg.proofs_height_on_a, prefix_on_a, &msg.proof_consensus_state_of_b_on_a, consensus_state_of_a_on_b.root(), - client_id_on_a, - msg.consensus_height_of_b_on_a, + &client_cons_state_path_on_a, expected_consensus_state_of_b_on_a.as_ref(), ) .map_err(|e| ConnectionError::ConsensusStateVerificationFailure { diff --git a/crates/ibc/src/core/ics04_channel/context.rs b/crates/ibc/src/core/ics04_channel/context.rs index ffe4d3db3..638eb2466 100644 --- a/crates/ibc/src/core/ics04_channel/context.rs +++ b/crates/ibc/src/core/ics04_channel/context.rs @@ -2,6 +2,10 @@ //! the interface that any host chain must implement to be able to process any `ChannelMsg`. //! use crate::core::ics02_client::client_state::ClientState; +use crate::core::ics24_host::path::{ + AckPath, ChannelEndPath, ClientConsensusStatePath, CommitmentPath, ReceiptPath, SeqAckPath, + SeqRecvPath, SeqSendPath, +}; use core::time::Duration; use num_traits::float::FloatCore; @@ -27,11 +31,7 @@ use super::timeout::TimeoutHeight; /// A context supplying all the necessary read-only dependencies for processing any `ChannelMsg`. pub trait ChannelReader { /// Returns the ChannelEnd for the given `port_id` and `chan_id`. - fn channel_end( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result; + fn channel_end(&self, chan_end_path: &ChannelEndPath) -> Result; /// Returns the ConnectionState for the given identifier `connection_id`. fn connection_end(&self, connection_id: &ConnectionId) -> Result; @@ -47,47 +47,25 @@ pub trait ChannelReader { fn client_consensus_state( &self, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result, ChannelError>; - fn get_next_sequence_send( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result; + fn get_next_sequence_send(&self, seq_send_path: &SeqSendPath) -> Result; - fn get_next_sequence_recv( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result; + fn get_next_sequence_recv(&self, seq_recv_path: &SeqRecvPath) -> Result; - fn get_next_sequence_ack( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result; + fn get_next_sequence_ack(&self, seq_ack_path: &SeqAckPath) -> Result; fn get_packet_commitment( &self, - port_id: &PortId, - channel_id: &ChannelId, - sequence: &Sequence, + commitment_path: &CommitmentPath, ) -> Result; - fn get_packet_receipt( - &self, - port_id: &PortId, - channel_id: &ChannelId, - sequence: &Sequence, - ) -> Result; + fn get_packet_receipt(&self, receipt_path: &ReceiptPath) -> Result; fn get_packet_acknowledgement( &self, - port_id: &PortId, - channel_id: &ChannelId, - sequence: &Sequence, + ack_path: &AckPath, ) -> Result; /// Compute the commitment for a packet. @@ -173,11 +151,7 @@ pub trait ChannelReader { pub trait SendPacketReader { /// Returns the ChannelEnd for the given `port_id` and `chan_id`. - fn channel_end( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result; + fn channel_end(&self, channel_end_path: &ChannelEndPath) -> Result; /// Returns the ConnectionState for the given identifier `connection_id`. fn connection_end(&self, connection_id: &ConnectionId) -> Result; @@ -188,15 +162,10 @@ pub trait SendPacketReader { fn client_consensus_state( &self, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result, PacketError>; - fn get_next_sequence_send( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result; + fn get_next_sequence_send(&self, seq_send_path: &SeqSendPath) -> Result; fn hash(&self, value: &[u8]) -> Vec; @@ -225,12 +194,8 @@ impl SendPacketReader for T where T: ChannelReader, { - fn channel_end( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result { - ChannelReader::channel_end(self, port_id, channel_id).map_err(PacketError::Channel) + fn channel_end(&self, chan_end_path: &ChannelEndPath) -> Result { + ChannelReader::channel_end(self, chan_end_path).map_err(PacketError::Channel) } fn connection_end(&self, connection_id: &ConnectionId) -> Result { @@ -243,18 +208,14 @@ where fn client_consensus_state( &self, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result, PacketError> { - ChannelReader::client_consensus_state(self, client_id, height).map_err(PacketError::Channel) + ChannelReader::client_consensus_state(self, client_cons_state_path) + .map_err(PacketError::Channel) } - fn get_next_sequence_send( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result { - ChannelReader::get_next_sequence_send(self, port_id, channel_id) + fn get_next_sequence_send(&self, seq_send_path: &SeqSendPath) -> Result { + ChannelReader::get_next_sequence_send(self, seq_send_path) } fn hash(&self, value: &[u8]) -> Vec { diff --git a/crates/ibc/src/core/ics04_channel/handler/acknowledgement.rs b/crates/ibc/src/core/ics04_channel/handler/acknowledgement.rs index 2f3917281..e4148230e 100644 --- a/crates/ibc/src/core/ics04_channel/handler/acknowledgement.rs +++ b/crates/ibc/src/core/ics04_channel/handler/acknowledgement.rs @@ -7,6 +7,9 @@ use crate::core::ics04_channel::msgs::acknowledgement::MsgAcknowledgement; use crate::core::ics04_channel::packet::{PacketResult, Sequence}; use crate::core::ics04_channel::{context::ChannelReader, error::PacketError}; use crate::core::ics24_host::identifier::{ChannelId, PortId}; +use crate::core::ics24_host::path::{ + AckPath, ChannelEndPath, ClientConsensusStatePath, CommitmentPath, SeqAckPath, +}; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -18,9 +21,8 @@ where Ctx: ValidationContext, { let packet = &msg.packet; - - let port_chan_id_on_a = &(msg.packet.port_on_a.clone(), msg.packet.chan_on_a.clone()); - let chan_end_on_a = ctx_a.channel_end(port_chan_id_on_a)?; + let chan_end_path_on_a = ChannelEndPath::new(&packet.port_on_a, &packet.chan_on_a); + let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?; if !chan_end_on_a.state_matches(&State::Open) { return Err(PacketError::ChannelClosed { @@ -49,12 +51,11 @@ where .into()); } + let commitment_path_on_a = + CommitmentPath::new(&packet.port_on_a, &packet.chan_on_a, packet.sequence); + // Verify packet commitment - let commitment_on_a = match ctx_a.get_packet_commitment(&( - msg.packet.port_on_a.clone(), - msg.packet.chan_on_a.clone(), - msg.packet.sequence, - )) { + let commitment_on_a = match ctx_a.get_packet_commitment(&commitment_path_on_a) { Ok(commitment_on_a) => commitment_on_a, // This error indicates that the timeout has already been relayed @@ -78,8 +79,8 @@ where } if let Order::Ordered = chan_end_on_a.ordering { - let next_seq_ack = ctx_a.get_next_sequence_ack(port_chan_id_on_a)?; - + let seq_ack_path_on_a = SeqAckPath::new(&packet.port_on_a, &packet.chan_on_a); + let next_seq_ack = ctx_a.get_next_sequence_ack(&seq_ack_path_on_a)?; if packet.sequence != next_seq_ack { return Err(PacketError::InvalidPacketSequence { given_sequence: packet.sequence, @@ -101,11 +102,11 @@ where } .into()); } - - let consensus_state = ctx_a.consensus_state(client_id_on_a, &msg.proof_height_on_b)?; - + let client_cons_state_path_on_a = + ClientConsensusStatePath::new(client_id_on_a, &msg.proof_height_on_b); + let consensus_state = ctx_a.consensus_state(&client_cons_state_path_on_a)?; let ack_commitment = ctx_a.ack_commitment(&msg.acknowledgement); - + let ack_path_on_b = AckPath::new(&packet.port_on_b, &packet.chan_on_b, packet.sequence); // Verify the proof for the packet against the chain store. client_state_on_a .new_verify_packet_acknowledgement( @@ -114,9 +115,7 @@ where &conn_end_on_a, &msg.proof_acked_on_b, consensus_state.root(), - &packet.port_on_b, - &packet.chan_on_b, - packet.sequence, + &ack_path_on_b, ack_commitment, ) .map_err(|e| ChannelError::PacketVerificationFailed { @@ -143,9 +142,9 @@ pub(crate) fn process( let mut output = HandlerOutput::builder(); let packet = &msg.packet; - + let chan_end_path_on_a = ChannelEndPath::new(&packet.port_on_a, &packet.chan_on_a); let chan_end_on_a = ctx_a - .channel_end(&packet.port_on_a, &packet.chan_on_a) + .channel_end(&chan_end_path_on_a) .map_err(PacketError::Channel)?; if !chan_end_on_a.state_matches(&State::Open) { @@ -174,9 +173,10 @@ pub(crate) fn process( }); } + let commitment_path_on_a = + CommitmentPath::new(&packet.port_on_a, &packet.chan_on_a, packet.sequence); // Verify packet commitment - let packet_commitment = - ctx_a.get_packet_commitment(&packet.port_on_a, &packet.chan_on_a, &packet.sequence)?; + let packet_commitment = ctx_a.get_packet_commitment(&commitment_path_on_a)?; if packet_commitment != ctx_a.packet_commitment( @@ -204,12 +204,13 @@ pub(crate) fn process( }); } + let client_cons_state_path_on_a = + ClientConsensusStatePath::new(client_id_on_a, &msg.proof_height_on_b); let consensus_state = ctx_a - .client_consensus_state(client_id_on_a, &msg.proof_height_on_b) + .client_consensus_state(&client_cons_state_path_on_a) .map_err(PacketError::Channel)?; - let ack_commitment = ctx_a.ack_commitment(&msg.acknowledgement); - + let ack_path_on_b = AckPath::new(&packet.port_on_b, &packet.chan_on_b, packet.sequence); // Verify the proof for the packet against the chain store. client_state_on_a .verify_packet_acknowledgement( @@ -218,9 +219,7 @@ pub(crate) fn process( &conn_end_on_a, &msg.proof_acked_on_b, consensus_state.root(), - &packet.port_on_b, - &packet.chan_on_b, - packet.sequence, + &ack_path_on_b, ack_commitment, ) .map_err(|e| ChannelError::PacketVerificationFailed { @@ -231,7 +230,8 @@ pub(crate) fn process( } let result = if chan_end_on_a.order_matches(&Order::Ordered) { - let next_seq_ack = ctx_a.get_next_sequence_ack(&packet.port_on_a, &packet.chan_on_a)?; + let ack_path_on_a = SeqAckPath::new(&packet.port_on_a, &packet.chan_on_a); + let next_seq_ack = ctx_a.get_next_sequence_ack(&ack_path_on_a)?; if packet.sequence != next_seq_ack { return Err(PacketError::InvalidPacketSequence { diff --git a/crates/ibc/src/core/ics04_channel/handler/chan_close_confirm.rs b/crates/ibc/src/core/ics04_channel/handler/chan_close_confirm.rs index 8856e2d19..1b1eaa9c1 100644 --- a/crates/ibc/src/core/ics04_channel/handler/chan_close_confirm.rs +++ b/crates/ibc/src/core/ics04_channel/handler/chan_close_confirm.rs @@ -5,6 +5,7 @@ use crate::core::ics04_channel::context::ChannelReader; use crate::core::ics04_channel::error::ChannelError; use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult}; use crate::core::ics04_channel::msgs::chan_close_confirm::MsgChannelCloseConfirm; +use crate::core::ics24_host::path::{ChannelEndPath, ClientConsensusStatePath}; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -15,7 +16,8 @@ where Ctx: ValidationContext, { // Retrieve the old channel end and validate it against the message. - let chan_end_on_b = ctx_b.channel_end(&(msg.port_id_on_b.clone(), msg.chan_id_on_b.clone()))?; + let chan_end_path_on_b = ChannelEndPath::new(&msg.port_id_on_b, &msg.chan_id_on_b); + let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; // Validate that the channel end is in a state where it can be closed. if chan_end_on_b.state_matches(&State::Closed) { @@ -47,8 +49,9 @@ where { let client_id_on_b = conn_end_on_b.client_id(); let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?; - let consensus_state_of_a_on_b = - ctx_b.consensus_state(client_id_on_b, &msg.proof_height_on_a)?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(client_id_on_b, &msg.proof_height_on_a); + let consensus_state_of_a_on_b = ctx_b.consensus_state(&client_cons_state_path_on_b)?; let prefix_on_a = conn_end_on_b.counterparty().prefix(); let port_id_on_a = &chan_end_on_b.counterparty().port_id; let chan_id_on_a = chan_end_on_b @@ -76,6 +79,7 @@ where 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); // Verify the proof for the channel state against the expected channel end. // A counterparty channel id of None in not possible, and is checked by validate_basic in msg. @@ -85,8 +89,7 @@ where prefix_on_a, &msg.proof_chan_end_on_a, consensus_state_of_a_on_b.root(), - port_id_on_a, - chan_id_on_a, + &chan_end_path_on_a, &expected_chan_end_on_a, ) .map_err(ChannelError::VerifyChannelFailed)?; @@ -103,7 +106,8 @@ pub(crate) fn process( let mut output = HandlerOutput::builder(); // Retrieve the old channel end and validate it against the message. - let chan_end_on_b = ctx_b.channel_end(&msg.port_id_on_b, &msg.chan_id_on_b)?; + let chan_end_path_on_b = ChannelEndPath::new(&msg.port_id_on_b, &msg.chan_id_on_b); + let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; // Validate that the channel end is in a state where it can be closed. if chan_end_on_b.state_matches(&State::Closed) { @@ -132,8 +136,10 @@ pub(crate) fn process( { let client_id_on_b = conn_end_on_b.client_id(); let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(client_id_on_b, &msg.proof_height_on_a); let consensus_state_of_a_on_b = - ctx_b.client_consensus_state(client_id_on_b, &msg.proof_height_on_a)?; + ctx_b.client_consensus_state(&client_cons_state_path_on_b)?; let prefix_on_a = conn_end_on_b.counterparty().prefix(); let port_id_on_a = &chan_end_on_b.counterparty().port_id; let chan_id_on_a = chan_end_on_b @@ -160,6 +166,7 @@ pub(crate) fn process( 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); // Verify the proof for the channel state against the expected channel end. // A counterparty channel id of None in not possible, and is checked by validate_basic in msg. @@ -169,8 +176,7 @@ pub(crate) fn process( prefix_on_a, &msg.proof_chan_end_on_a, consensus_state_of_a_on_b.root(), - port_id_on_a, - chan_id_on_a, + &chan_end_path_on_a, &expected_chan_end_on_a, ) .map_err(ChannelError::VerifyChannelFailed)?; diff --git a/crates/ibc/src/core/ics04_channel/handler/chan_close_init.rs b/crates/ibc/src/core/ics04_channel/handler/chan_close_init.rs index e24376beb..947227029 100644 --- a/crates/ibc/src/core/ics04_channel/handler/chan_close_init.rs +++ b/crates/ibc/src/core/ics04_channel/handler/chan_close_init.rs @@ -5,6 +5,7 @@ use crate::core::ics04_channel::context::ChannelReader; use crate::core::ics04_channel::error::ChannelError; use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult}; use crate::core::ics04_channel::msgs::chan_close_init::MsgChannelCloseInit; +use crate::core::ics24_host::path::ChannelEndPath; use crate::handler::{HandlerOutput, HandlerResult}; use crate::core::{ContextError, ValidationContext}; @@ -13,7 +14,8 @@ pub fn validate(ctx_a: &Ctx, msg: &MsgChannelCloseInit) -> Result<(), Conte where Ctx: ValidationContext, { - let chan_end_on_a = ctx_a.channel_end(&(msg.port_id_on_a.clone(), msg.chan_id_on_a.clone()))?; + let chan_end_path_on_a = ChannelEndPath::new(&msg.port_id_on_a, &msg.chan_id_on_a); + let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?; // Validate that the channel end is in a state where it can be closed. if chan_end_on_a.state_matches(&State::Closed) { @@ -52,7 +54,8 @@ pub(crate) fn process( ) -> HandlerResult { let mut output = HandlerOutput::builder(); - let chan_end_on_a = ctx_a.channel_end(&msg.port_id_on_a, &msg.chan_id_on_a)?; + let chan_end_path_on_a = ChannelEndPath::new(&msg.port_id_on_a, &msg.chan_id_on_a); + let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?; // Validate that the channel end is in a state where it can be closed. if chan_end_on_a.state_matches(&State::Closed) { diff --git a/crates/ibc/src/core/ics04_channel/handler/chan_open_ack.rs b/crates/ibc/src/core/ics04_channel/handler/chan_open_ack.rs index b0cef63f9..e3ed159d6 100644 --- a/crates/ibc/src/core/ics04_channel/handler/chan_open_ack.rs +++ b/crates/ibc/src/core/ics04_channel/handler/chan_open_ack.rs @@ -5,6 +5,7 @@ use crate::core::ics04_channel::context::ChannelReader; use crate::core::ics04_channel::error::ChannelError; use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult}; use crate::core::ics04_channel::msgs::chan_open_ack::MsgChannelOpenAck; +use crate::core::ics24_host::path::{ChannelEndPath, ClientConsensusStatePath}; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -14,7 +15,8 @@ pub fn validate(ctx_a: &Ctx, msg: &MsgChannelOpenAck) -> Result<(), Context where Ctx: ValidationContext, { - let chan_end_on_a = ctx_a.channel_end(&(msg.port_id_on_a.clone(), msg.chan_id_on_a.clone()))?; + let chan_end_path_on_a = ChannelEndPath::new(&msg.port_id_on_a, &msg.chan_id_on_a); + let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?; // Validate that the channel end is in a state where it can be ack. if !chan_end_on_a.state_matches(&State::Init) { @@ -48,8 +50,9 @@ where { let client_id_on_a = conn_end_on_a.client_id(); let client_state_of_b_on_a = ctx_a.client_state(client_id_on_a)?; - let consensus_state_of_b_on_a = - ctx_a.consensus_state(client_id_on_a, &msg.proof_height_on_b)?; + let client_cons_state_path_on_a = + ClientConsensusStatePath::new(client_id_on_a, &msg.proof_height_on_b); + let consensus_state_of_b_on_a = ctx_a.consensus_state(&client_cons_state_path_on_a)?; let prefix_on_b = conn_end_on_a.counterparty().prefix(); let port_id_on_b = &chan_end_on_a.counterparty().port_id; let conn_id_on_b = conn_end_on_a.counterparty().connection_id().ok_or( @@ -75,6 +78,7 @@ where 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); // Verify the proof for the channel state against the expected channel end. // A counterparty channel id of None in not possible, and is checked by validate_basic in msg. @@ -84,8 +88,7 @@ where prefix_on_b, &msg.proof_chan_end_on_b, consensus_state_of_b_on_a.root(), - port_id_on_b, - &msg.chan_id_on_b, + &chan_end_path_on_b, &expected_chan_end_on_b, ) .map_err(ChannelError::VerifyChannelFailed)?; @@ -102,7 +105,8 @@ pub(crate) fn process( let mut output = HandlerOutput::builder(); // Unwrap the old channel end and validate it against the message. - let chan_end_on_a = ctx_a.channel_end(&msg.port_id_on_a, &msg.chan_id_on_a)?; + let chan_end_path_on_a = &ChannelEndPath::new(&msg.port_id_on_a, &msg.chan_id_on_a); + let chan_end_on_a = ctx_a.channel_end(chan_end_path_on_a)?; // Validate that the channel end is in a state where it can be ack. if !chan_end_on_a.state_matches(&State::Init) { @@ -133,8 +137,10 @@ pub(crate) fn process( { let client_id_on_a = conn_end_on_a.client_id(); let client_state_of_b_on_a = ctx_a.client_state(client_id_on_a)?; + let client_cons_state_path_on_a = + ClientConsensusStatePath::new(client_id_on_a, &msg.proof_height_on_b); let consensus_state_of_b_on_a = - ctx_a.client_consensus_state(client_id_on_a, &msg.proof_height_on_b)?; + ctx_a.client_consensus_state(&client_cons_state_path_on_a)?; let prefix_on_b = conn_end_on_a.counterparty().prefix(); let port_id_on_b = &chan_end_on_a.counterparty().port_id; let conn_id_on_b = conn_end_on_a.counterparty().connection_id().ok_or( @@ -159,6 +165,7 @@ pub(crate) fn process( 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); // Verify the proof for the channel state against the expected channel end. // A counterparty channel id of None in not possible, and is checked by validate_basic in msg. @@ -168,8 +175,7 @@ pub(crate) fn process( prefix_on_b, &msg.proof_chan_end_on_b, consensus_state_of_b_on_a.root(), - port_id_on_b, - &msg.chan_id_on_b, + &chan_end_path_on_b, &expected_chan_end_on_b, ) .map_err(ChannelError::VerifyChannelFailed)?; diff --git a/crates/ibc/src/core/ics04_channel/handler/chan_open_confirm.rs b/crates/ibc/src/core/ics04_channel/handler/chan_open_confirm.rs index 59e1b0be3..e4b8bdfa9 100644 --- a/crates/ibc/src/core/ics04_channel/handler/chan_open_confirm.rs +++ b/crates/ibc/src/core/ics04_channel/handler/chan_open_confirm.rs @@ -5,6 +5,7 @@ use crate::core::ics04_channel::context::ChannelReader; use crate::core::ics04_channel::error::ChannelError; use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult}; use crate::core::ics04_channel::msgs::chan_open_confirm::MsgChannelOpenConfirm; +use crate::core::ics24_host::path::{ChannelEndPath, ClientConsensusStatePath}; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -15,7 +16,8 @@ where Ctx: ValidationContext, { // Unwrap the old channel end and validate it against the message. - let chan_end_on_b = ctx_b.channel_end(&(msg.port_id_on_b.clone(), msg.chan_id_on_b.clone()))?; + let chan_end_path_on_b = ChannelEndPath::new(&msg.port_id_on_b, &msg.chan_id_on_b); + let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; // Validate that the channel end is in a state where it can be confirmed. if !chan_end_on_b.state_matches(&State::TryOpen) { @@ -48,8 +50,9 @@ where { let client_id_on_b = conn_end_on_b.client_id(); let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?; - let consensus_state_of_a_on_b = - ctx_b.consensus_state(client_id_on_b, &msg.proof_height_on_a)?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(client_id_on_b, &msg.proof_height_on_a); + let consensus_state_of_a_on_b = ctx_b.consensus_state(&client_cons_state_path_on_b)?; let prefix_on_a = conn_end_on_b.counterparty().prefix(); let port_id_on_a = &chan_end_on_b.counterparty().port_id; let chan_id_on_a = chan_end_on_b @@ -77,6 +80,7 @@ where 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); // Verify the proof for the channel state against the expected channel end. // A counterparty channel id of None in not possible, and is checked in msg. @@ -86,8 +90,7 @@ where prefix_on_a, &msg.proof_chan_end_on_a, consensus_state_of_a_on_b.root(), - port_id_on_a, - chan_id_on_a, + &chan_end_path_on_a, &expected_chan_end_on_a, ) .map_err(ChannelError::VerifyChannelFailed)?; @@ -104,7 +107,8 @@ pub(crate) fn process( let mut output = HandlerOutput::builder(); // Unwrap the old channel end and validate it against the message. - let mut chan_end_on_b = ctx_b.channel_end(&msg.port_id_on_b, &msg.chan_id_on_b)?; + let chan_end_path_on_b = ChannelEndPath::new(&msg.port_id_on_b, &msg.chan_id_on_b); + let mut chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; // Validate that the channel end is in a state where it can be confirmed. if !chan_end_on_b.state_matches(&State::TryOpen) { @@ -134,8 +138,10 @@ pub(crate) fn process( { let client_id_on_b = conn_end_on_b.client_id(); let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(client_id_on_b, &msg.proof_height_on_a); let consensus_state_of_a_on_b = - ctx_b.client_consensus_state(client_id_on_b, &msg.proof_height_on_a)?; + ctx_b.client_consensus_state(&client_cons_state_path_on_b)?; let prefix_on_a = conn_end_on_b.counterparty().prefix(); let port_id_on_a = &chan_end_on_b.counterparty().port_id; let chan_id_on_a = chan_end_on_b @@ -162,6 +168,7 @@ pub(crate) fn process( 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); // Verify the proof for the channel state against the expected channel end. // A counterparty channel id of None in not possible, and is checked in msg. @@ -171,8 +178,7 @@ pub(crate) fn process( prefix_on_a, &msg.proof_chan_end_on_a, consensus_state_of_a_on_b.root(), - port_id_on_a, - chan_id_on_a, + &chan_end_path_on_a, &expected_chan_end_on_a, ) .map_err(ChannelError::VerifyChannelFailed)?; diff --git a/crates/ibc/src/core/ics04_channel/handler/chan_open_try.rs b/crates/ibc/src/core/ics04_channel/handler/chan_open_try.rs index e07dfbf27..eba1adb40 100644 --- a/crates/ibc/src/core/ics04_channel/handler/chan_open_try.rs +++ b/crates/ibc/src/core/ics04_channel/handler/chan_open_try.rs @@ -8,6 +8,7 @@ use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult}; use crate::core::ics04_channel::msgs::chan_open_try::MsgChannelOpenTry; use crate::core::ics04_channel::Version; use crate::core::ics24_host::identifier::ChannelId; +use crate::core::ics24_host::path::{ChannelEndPath, ClientConsensusStatePath}; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -52,10 +53,11 @@ where { let client_id_on_b = conn_end_on_b.client_id(); let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?; - let consensus_state_of_a_on_b = - ctx_b.consensus_state(client_id_on_b, &msg.proof_height_on_a)?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(client_id_on_b, &msg.proof_height_on_a); + let consensus_state_of_a_on_b = ctx_b.consensus_state(&client_cons_state_path_on_b)?; let prefix_on_a = conn_end_on_b.counterparty().prefix(); - let port_id_on_a = &&msg.port_id_on_a; + let port_id_on_a = msg.port_id_on_a.clone(); let chan_id_on_a = msg.chan_id_on_a.clone(); let conn_id_on_a = conn_end_on_b.counterparty().connection_id().ok_or( ChannelError::UndefinedConnectionCounterparty { @@ -78,6 +80,7 @@ where 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); // Verify the proof for the channel state against the expected channel end. // A counterparty channel id of None in not possible, and is checked by validate_basic in msg. @@ -87,8 +90,7 @@ where prefix_on_a, &msg.proof_chan_end_on_a, consensus_state_of_a_on_b.root(), - port_id_on_a, - &chan_id_on_a, + &chan_end_path_on_a, &expected_chan_end_on_a, ) .map_err(ChannelError::VerifyChannelFailed)?; @@ -133,10 +135,12 @@ pub(crate) fn process( { let client_id_on_b = conn_end_on_b.client_id(); let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(client_id_on_b, &msg.proof_height_on_a); let consensus_state_of_a_on_b = - ctx_b.client_consensus_state(client_id_on_b, &msg.proof_height_on_a)?; + ctx_b.client_consensus_state(&client_cons_state_path_on_b)?; let prefix_on_a = conn_end_on_b.counterparty().prefix(); - let port_id_on_a = &&msg.port_id_on_a; + let port_id_on_a = msg.port_id_on_a.clone(); let chan_id_on_a = msg.chan_id_on_a.clone(); let conn_id_on_a = conn_end_on_b.counterparty().connection_id().ok_or( ChannelError::UndefinedConnectionCounterparty { @@ -158,6 +162,7 @@ pub(crate) fn process( 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); // Verify the proof for the channel state against the expected channel end. // A counterparty channel id of None in not possible, and is checked by validate_basic in msg. @@ -167,8 +172,7 @@ pub(crate) fn process( prefix_on_a, &msg.proof_chan_end_on_a, consensus_state_of_a_on_b.root(), - port_id_on_a, - &chan_id_on_a, + &chan_end_path_on_a, &expected_chan_end_on_a, ) .map_err(ChannelError::VerifyChannelFailed)?; diff --git a/crates/ibc/src/core/ics04_channel/handler/recv_packet.rs b/crates/ibc/src/core/ics04_channel/handler/recv_packet.rs index 6bd985b0c..7af276a25 100644 --- a/crates/ibc/src/core/ics04_channel/handler/recv_packet.rs +++ b/crates/ibc/src/core/ics04_channel/handler/recv_packet.rs @@ -7,6 +7,9 @@ use crate::core::ics04_channel::events::ReceivePacket; use crate::core::ics04_channel::msgs::recv_packet::MsgRecvPacket; use crate::core::ics04_channel::packet::{PacketResult, Receipt, Sequence}; use crate::core::ics24_host::identifier::{ChannelId, PortId}; +use crate::core::ics24_host::path::{ + AckPath, ChannelEndPath, ClientConsensusStatePath, CommitmentPath, ReceiptPath, SeqRecvPath, +}; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::timestamp::Expiry; @@ -17,8 +20,8 @@ pub fn validate(ctx_b: &Ctx, msg: &MsgRecvPacket) -> Result<(), ContextErro where Ctx: ValidationContext, { - let port_chan_id_on_b = &(msg.packet.port_on_b.clone(), msg.packet.chan_on_b.clone()); - let chan_end_on_b = ctx_b.channel_end(port_chan_id_on_b)?; + let chan_end_path_on_b = ChannelEndPath::new(&msg.packet.port_on_b, &msg.packet.chan_on_b); + let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?; if !chan_end_on_b.state_matches(&State::Open) { return Err(PacketError::InvalidChannelState { @@ -78,14 +81,21 @@ where .into()); } - let consensus_state_of_a_on_b = - ctx_b.consensus_state(client_id_on_b, &msg.proof_height_on_a)?; + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(client_id_on_b, &msg.proof_height_on_a); + let consensus_state_of_a_on_b = ctx_b.consensus_state(&client_cons_state_path_on_b)?; let expected_commitment_on_a = ctx_b.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_on_a, + &msg.packet.chan_on_a, + msg.packet.sequence, + ); + // Verify the proof for the packet against the chain store. client_state_of_a_on_b .new_verify_packet_data( @@ -94,9 +104,7 @@ where &conn_end_on_b, &msg.proof_commitment_on_a, consensus_state_of_a_on_b.root(), - &msg.packet.port_on_a, - &msg.packet.chan_on_a, - msg.packet.sequence, + &commitment_path_on_a, expected_commitment_on_a, ) .map_err(|e| ChannelError::PacketVerificationFailed { @@ -107,7 +115,8 @@ where } if chan_end_on_b.order_matches(&Order::Ordered) { - let next_seq_recv = ctx_b.get_next_sequence_recv(port_chan_id_on_b)?; + let seq_recv_path_on_b = SeqRecvPath::new(&msg.packet.port_on_b, &msg.packet.chan_on_b); + let next_seq_recv = ctx_b.get_next_sequence_recv(&seq_recv_path_on_b)?; if msg.packet.sequence > next_seq_recv { return Err(PacketError::InvalidPacketSequence { given_sequence: msg.packet.sequence, @@ -122,11 +131,12 @@ where validate_write_acknowledgement(ctx_b, msg)?; } } else { - let packet_rec = ctx_b.get_packet_receipt(&( - msg.packet.port_on_b.clone(), - msg.packet.chan_on_b.clone(), + let receipt_path_on_b = ReceiptPath::new( + &msg.packet.port_on_a, + &msg.packet.chan_on_a, msg.packet.sequence, - )); + ); + let packet_rec = ctx_b.get_packet_receipt(&receipt_path_on_b); match packet_rec { Ok(_receipt) => {} Err(ContextError::PacketError(PacketError::PacketReceiptNotFound { sequence })) @@ -146,10 +156,8 @@ where Ctx: ValidationContext, { let packet = msg.packet.clone(); - if ctx_b - .get_packet_acknowledgement(&(packet.port_on_b, packet.chan_on_b, packet.sequence)) - .is_ok() - { + let ack_path_on_b = AckPath::new(&packet.port_on_b, &packet.chan_on_b, packet.sequence); + if ctx_b.get_packet_acknowledgement(&ack_path_on_b).is_ok() { return Err(PacketError::AcknowledgementExists { sequence: msg.packet.sequence, } @@ -182,8 +190,9 @@ pub(crate) fn process( ) -> HandlerResult { let mut output = HandlerOutput::builder(); + let chan_end_path_on_b = ChannelEndPath::new(&msg.packet.port_on_b, &msg.packet.chan_on_b); let chan_end_on_b = ctx_b - .channel_end(&msg.packet.port_on_b, &msg.packet.chan_on_b) + .channel_end(&chan_end_path_on_b) .map_err(PacketError::Channel)?; if !chan_end_on_b.state_matches(&State::Open) { @@ -243,8 +252,10 @@ pub(crate) fn process( }); } + let client_cons_state_path_on_b = + ClientConsensusStatePath::new(client_id_on_b, &msg.proof_height_on_a); let consensus_state_of_a_on_b = ctx_b - .client_consensus_state(client_id_on_b, &msg.proof_height_on_a) + .client_consensus_state(&client_cons_state_path_on_b) .map_err(PacketError::Channel)?; let expected_commitment_on_a = ctx_b.packet_commitment( @@ -252,6 +263,13 @@ pub(crate) fn process( &msg.packet.timeout_height_on_b, &msg.packet.timeout_timestamp_on_b, ); + + let commitment_path_on_a = CommitmentPath::new( + &msg.packet.port_on_a, + &msg.packet.chan_on_a, + msg.packet.sequence, + ); + // Verify the proof for the packet against the chain store. client_state_of_a_on_b .verify_packet_data( @@ -260,9 +278,7 @@ pub(crate) fn process( &conn_end_on_b, &msg.proof_commitment_on_a, consensus_state_of_a_on_b.root(), - &msg.packet.port_on_a, - &msg.packet.chan_on_a, - msg.packet.sequence, + &commitment_path_on_a, expected_commitment_on_a, ) .map_err(|e| ChannelError::PacketVerificationFailed { @@ -273,8 +289,8 @@ pub(crate) fn process( } let result = if chan_end_on_b.order_matches(&Order::Ordered) { - let next_seq_recv = - ctx_b.get_next_sequence_recv(&msg.packet.port_on_b, &msg.packet.chan_on_b)?; + let seq_recv_path_on_b = SeqRecvPath::new(&msg.packet.port_on_b, &msg.packet.chan_on_b); + let next_seq_recv = ctx_b.get_next_sequence_recv(&seq_recv_path_on_b)?; if msg.packet.sequence > next_seq_recv { return Err(PacketError::InvalidPacketSequence { given_sequence: msg.packet.sequence, @@ -292,12 +308,12 @@ pub(crate) fn process( }) } } else { - let packet_rec = ctx_b.get_packet_receipt( + let receipt_path_on_b = ReceiptPath::new( &msg.packet.port_on_b, &msg.packet.chan_on_b, - &msg.packet.sequence, + msg.packet.sequence, ); - + let packet_rec = ctx_b.get_packet_receipt(&receipt_path_on_b); match packet_rec { Ok(_receipt) => PacketResult::Recv(RecvPacketResult::NoOp), Err(PacketError::PacketReceiptNotFound { sequence }) diff --git a/crates/ibc/src/core/ics04_channel/handler/send_packet.rs b/crates/ibc/src/core/ics04_channel/handler/send_packet.rs index 4ed2f3b88..723ad0e57 100644 --- a/crates/ibc/src/core/ics04_channel/handler/send_packet.rs +++ b/crates/ibc/src/core/ics04_channel/handler/send_packet.rs @@ -5,6 +5,9 @@ use crate::core::ics04_channel::events::SendPacket; use crate::core::ics04_channel::packet::Sequence; use crate::core::ics04_channel::{context::SendPacketReader, error::PacketError, packet::Packet}; use crate::core::ics24_host::identifier::{ChannelId, PortId}; +use crate::core::ics24_host::path::ChannelEndPath; +use crate::core::ics24_host::path::ClientConsensusStatePath; +use crate::core::ics24_host::path::SeqSendPath; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -26,7 +29,8 @@ pub fn send_packet( ) -> HandlerResult { let mut output = HandlerOutput::builder(); - let chan_end_on_a = ctx_a.channel_end(&packet.port_on_a, &packet.chan_on_a)?; + let chan_end_path_on_a = ChannelEndPath::new(&packet.port_on_a, &packet.chan_on_a); + let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?; if chan_end_on_a.state_matches(&State::Closed) { return Err(PacketError::ChannelClosed { @@ -65,15 +69,17 @@ pub fn send_packet( }); } - let consensus_state_of_b_on_a = - ctx_a.client_consensus_state(client_id_on_a, &latest_height_on_a)?; + let client_cons_state_path_on_a = + ClientConsensusStatePath::new(client_id_on_a, &latest_height_on_a); + let consensus_state_of_b_on_a = ctx_a.client_consensus_state(&client_cons_state_path_on_a)?; let latest_timestamp = consensus_state_of_b_on_a.timestamp(); let packet_timestamp = packet.timeout_timestamp_on_b; if let Expiry::Expired = latest_timestamp.check_expiry(&packet_timestamp) { return Err(PacketError::LowPacketTimestamp); } - let next_seq_send_on_a = ctx_a.get_next_sequence_send(&packet.port_on_a, &packet.chan_on_a)?; + let seq_send_path_on_a = SeqSendPath::new(&packet.port_on_a, &packet.chan_on_a); + let next_seq_send_on_a = ctx_a.get_next_sequence_send(&seq_send_path_on_a)?; if packet.sequence != next_seq_send_on_a { return Err(PacketError::InvalidPacketSequence { diff --git a/crates/ibc/src/core/ics04_channel/handler/timeout.rs b/crates/ibc/src/core/ics04_channel/handler/timeout.rs index e5400b65f..75241b0cb 100644 --- a/crates/ibc/src/core/ics04_channel/handler/timeout.rs +++ b/crates/ibc/src/core/ics04_channel/handler/timeout.rs @@ -6,6 +6,9 @@ use crate::core::ics04_channel::msgs::timeout::MsgTimeout; use crate::core::ics04_channel::packet::{PacketResult, Sequence}; use crate::core::ics04_channel::{context::ChannelReader, error::PacketError}; use crate::core::ics24_host::identifier::{ChannelId, PortId}; +use crate::core::ics24_host::path::{ + ChannelEndPath, ClientConsensusStatePath, CommitmentPath, ReceiptPath, SeqRecvPath, +}; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -17,8 +20,10 @@ pub fn validate(ctx_a: &Ctx, msg: &MsgTimeout) -> Result<(), ContextError> where Ctx: ValidationContext, { - let port_chan_id_on_a = &(msg.packet.port_on_a.clone(), msg.packet.chan_on_a.clone()); - let chan_end_on_a = ctx_a.channel_end(port_chan_id_on_a)?; + let chan_end_on_a = ctx_a.channel_end(&ChannelEndPath::new( + &msg.packet.port_on_a, + &msg.packet.chan_on_a, + ))?; if !chan_end_on_a.state_matches(&State::Open) { return Err(PacketError::ChannelClosed { @@ -44,11 +49,12 @@ where let conn_end_on_a = ctx_a.connection_end(&conn_id_on_a)?; //verify packet commitment - let commitment_on_a = match ctx_a.get_packet_commitment(&( - msg.packet.port_on_a.clone(), - msg.packet.chan_on_a.clone(), + let commitment_path_on_a = CommitmentPath::new( + &msg.packet.port_on_a, + &msg.packet.chan_on_a, msg.packet.sequence, - )) { + ); + let commitment_on_a = match ctx_a.get_packet_commitment(&commitment_path_on_a) { Ok(commitment_on_a) => commitment_on_a, // This error indicates that the timeout has already been relayed @@ -87,9 +93,9 @@ where } .into()); } - - let consensus_state_of_b_on_a = - ctx_a.consensus_state(client_id_on_a, &msg.proof_height_on_b)?; + let client_cons_state_path_on_a = + ClientConsensusStatePath::new(client_id_on_a, &msg.proof_height_on_b); + let consensus_state_of_b_on_a = ctx_a.consensus_state(&client_cons_state_path_on_a)?; let timestamp_of_b = consensus_state_of_b_on_a.timestamp(); if let Expiry::Expired = msg @@ -111,26 +117,29 @@ where } .into()); } + let seq_recv_path_on_b = SeqRecvPath::new(&msg.packet.port_on_b, &msg.packet.chan_on_b); client_state_of_b_on_a.new_verify_next_sequence_recv( ctx_a, msg.proof_height_on_b, &conn_end_on_a, &msg.proof_unreceived_on_b, consensus_state_of_b_on_a.root(), - &msg.packet.port_on_b, - &msg.packet.chan_on_b, + &seq_recv_path_on_b, msg.packet.sequence, ) } else { + let receipt_path_on_b = ReceiptPath::new( + &msg.packet.port_on_b, + &msg.packet.chan_on_b, + msg.packet.sequence, + ); client_state_of_b_on_a.new_verify_packet_receipt_absence( ctx_a, msg.proof_height_on_b, &conn_end_on_a, &msg.proof_unreceived_on_b, consensus_state_of_b_on_a.root(), - &msg.packet.port_on_b, - &msg.packet.chan_on_b, - msg.packet.sequence, + &receipt_path_on_b, ) }; next_seq_recv_verification_result @@ -163,9 +172,9 @@ pub(crate) fn process( msg: &MsgTimeout, ) -> HandlerResult { let mut output = HandlerOutput::builder(); - + let chan_end_path_on_a = ChannelEndPath::new(&msg.packet.port_on_a, &msg.packet.chan_on_a); let mut chan_end_on_a = ctx_a - .channel_end(&msg.packet.port_on_a, &msg.packet.chan_on_a) + .channel_end(&chan_end_path_on_a) .map_err(PacketError::Channel)?; if !chan_end_on_a.state_matches(&State::Open) { @@ -192,12 +201,12 @@ pub(crate) fn process( .map_err(PacketError::Channel)?; //verify packet commitment - let commitment_on_a = ctx_a.get_packet_commitment( + let commitment_path_on_a = CommitmentPath::new( &msg.packet.port_on_a, &msg.packet.chan_on_a, - &msg.packet.sequence, - )?; - + msg.packet.sequence, + ); + let commitment_on_a = ctx_a.get_packet_commitment(&commitment_path_on_a)?; let expected_commitment_on_a = ctx_a.packet_commitment( &msg.packet.data, &msg.packet.timeout_height_on_b, @@ -228,8 +237,10 @@ pub(crate) fn process( }); } + let client_cons_state_path = + ClientConsensusStatePath::new(client_id_on_a, &msg.proof_height_on_b); let consensus_state_of_b_on_a = ctx_a - .client_consensus_state(client_id_on_a, &msg.proof_height_on_b) + .client_consensus_state(&client_cons_state_path) .map_err(PacketError::Channel)?; let timestamp_of_b = consensus_state_of_b_on_a.timestamp(); @@ -250,26 +261,29 @@ pub(crate) fn process( next_sequence: msg.next_seq_recv_on_b, }); } + let seq_recv_path_on_b = SeqRecvPath::new(&msg.packet.port_on_b, &msg.packet.chan_on_b); client_state_of_b_on_a.verify_next_sequence_recv( ctx_a, msg.proof_height_on_b, &conn_end_on_a, &msg.proof_unreceived_on_b, consensus_state_of_b_on_a.root(), - &msg.packet.port_on_b, - &msg.packet.chan_on_b, + &seq_recv_path_on_b, msg.packet.sequence, ) } else { + let receipt_path_on_b = ReceiptPath::new( + &msg.packet.port_on_b, + &msg.packet.chan_on_b, + msg.packet.sequence, + ); client_state_of_b_on_a.verify_packet_receipt_absence( ctx_a, msg.proof_height_on_b, &conn_end_on_a, &msg.proof_unreceived_on_b, consensus_state_of_b_on_a.root(), - &msg.packet.port_on_b, - &msg.packet.chan_on_b, - msg.packet.sequence, + &receipt_path_on_b, ) }; next_seq_recv_verification_result @@ -328,6 +342,7 @@ mod tests { use crate::core::ics04_channel::msgs::timeout::MsgTimeout; use crate::core::ics04_channel::Version; use crate::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId}; + use crate::core::ics24_host::path::ChannelEndPath; use crate::events::IbcEvent; use crate::mock::context::MockContext; use crate::prelude::*; @@ -485,7 +500,7 @@ mod tests { let events = proto_output.events; let src_channel_end = test .ctx - .channel_end(&packet.port_on_a, &packet.chan_on_a) + .channel_end(&ChannelEndPath::new(&packet.port_on_a, &packet.chan_on_a)) .unwrap(); if src_channel_end.order_matches(&Order::Ordered) { diff --git a/crates/ibc/src/core/ics04_channel/handler/timeout_on_close.rs b/crates/ibc/src/core/ics04_channel/handler/timeout_on_close.rs index c86c6b40c..785ee1418 100644 --- a/crates/ibc/src/core/ics04_channel/handler/timeout_on_close.rs +++ b/crates/ibc/src/core/ics04_channel/handler/timeout_on_close.rs @@ -7,6 +7,9 @@ use crate::core::ics04_channel::packet::PacketResult; use crate::core::ics04_channel::{ context::ChannelReader, error::PacketError, handler::timeout::TimeoutPacketResult, }; +use crate::core::ics24_host::path::{ + ChannelEndPath, ClientConsensusStatePath, CommitmentPath, ReceiptPath, SeqRecvPath, +}; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -18,9 +21,8 @@ where Ctx: ValidationContext, { let packet = &msg.packet; - - let port_chan_id_on_a = &(msg.packet.port_on_a.clone(), msg.packet.chan_on_a.clone()); - let chan_end_on_a = ctx_a.channel_end(port_chan_id_on_a)?; + let chan_end_path_on_a = ChannelEndPath::new(&packet.port_on_a, &packet.chan_on_a); + let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?; let counterparty = Counterparty::new(packet.port_on_b.clone(), Some(packet.chan_on_b.clone())); @@ -32,12 +34,14 @@ where .into()); } - //verify the packet was sent, check the store - let commitment_on_a = match ctx_a.get_packet_commitment(&( - msg.packet.port_on_a.clone(), - msg.packet.chan_on_a.clone(), + let commitment_path_on_a = CommitmentPath::new( + &msg.packet.port_on_a, + &msg.packet.chan_on_a, msg.packet.sequence, - )) { + ); + + //verify the packet was sent, check the store + let commitment_on_a = match ctx_a.get_packet_commitment(&commitment_path_on_a) { Ok(commitment_on_a) => commitment_on_a, // This error indicates that the timeout has already been relayed @@ -74,11 +78,11 @@ where } .into()); } - - let consensus_state_of_b_on_a = - ctx_a.consensus_state(client_id_on_a, &msg.proof_height_on_b)?; + let client_cons_state_path_on_a = + ClientConsensusStatePath::new(client_id_on_a, &msg.proof_height_on_b); + let consensus_state_of_b_on_a = ctx_a.consensus_state(&client_cons_state_path_on_a)?; let prefix_on_b = conn_end_on_a.counterparty().prefix(); - let port_id_on_b = &chan_end_on_a.counterparty().port_id; + let port_id_on_b = chan_end_on_a.counterparty().port_id.clone(); let chan_id_on_b = chan_end_on_a .counterparty() @@ -102,6 +106,8 @@ where chan_end_on_a.version().clone(), ); + let chan_end_path_on_b = ChannelEndPath(port_id_on_b, chan_id_on_b.clone()); + // Verify the proof for the channel state against the expected channel end. // A counterparty channel id of None in not possible, and is checked by validate_basic in msg. client_state_of_b_on_a @@ -110,8 +116,7 @@ where prefix_on_b, &msg.proof_unreceived_on_b, consensus_state_of_b_on_a.root(), - port_id_on_b, - chan_id_on_b, + &chan_end_path_on_b, &expected_chan_end_on_b, ) .map_err(ChannelError::VerifyChannelFailed) @@ -125,26 +130,29 @@ where } .into()); } + let seq_recv_path_on_b = SeqRecvPath::new(&packet.port_on_b, &packet.chan_on_b); client_state_of_b_on_a.new_verify_next_sequence_recv( ctx_a, msg.proof_height_on_b, &conn_end_on_a, &msg.proof_unreceived_on_b, consensus_state_of_b_on_a.root(), - &packet.port_on_b, - &packet.chan_on_b, + &seq_recv_path_on_b, packet.sequence, ) } else { + let receipt_path_on_b = ReceiptPath::new( + &msg.packet.port_on_b, + &msg.packet.chan_on_b, + msg.packet.sequence, + ); client_state_of_b_on_a.new_verify_packet_receipt_absence( ctx_a, msg.proof_height_on_b, &conn_end_on_a, &msg.proof_unreceived_on_b, consensus_state_of_b_on_a.root(), - &packet.port_on_b, - &packet.chan_on_b, - packet.sequence, + &receipt_path_on_b, ) }; next_seq_recv_verification_result @@ -167,8 +175,9 @@ pub(crate) fn process( let packet = &msg.packet; + let chan_end_path_on_a = ChannelEndPath::new(&packet.port_on_a, &packet.chan_on_a); let chan_end_on_a = ctx_a - .channel_end(&packet.port_on_a, &packet.chan_on_a) + .channel_end(&chan_end_path_on_a) .map_err(PacketError::Channel)?; let counterparty = Counterparty::new(packet.port_on_b.clone(), Some(packet.chan_on_b.clone())); @@ -180,9 +189,11 @@ pub(crate) fn process( }); } + let commitment_path_on_a = + CommitmentPath::new(&packet.port_on_a, &packet.chan_on_a, packet.sequence); + //verify the packet was sent, check the store - let commitment_on_a = - ctx_a.get_packet_commitment(&packet.port_on_a, &packet.chan_on_a, &packet.sequence)?; + let commitment_on_a = ctx_a.get_packet_commitment(&commitment_path_on_a)?; let expected_commitment_on_a = ctx_a.packet_commitment( &packet.data, @@ -213,12 +224,13 @@ pub(crate) fn process( client_id: client_id_on_a.clone(), }); } - + let client_cons_state_on_a = + ClientConsensusStatePath::new(client_id_on_a, &msg.proof_height_on_b); let consensus_state_of_b_on_a = ctx_a - .client_consensus_state(client_id_on_a, &msg.proof_height_on_b) + .client_consensus_state(&client_cons_state_on_a) .map_err(PacketError::Channel)?; let prefix_on_b = conn_end_on_a.counterparty().prefix(); - let port_id_on_b = &chan_end_on_a.counterparty().port_id; + let port_id_on_b = chan_end_on_a.counterparty().port_id.clone(); let chan_id_on_b = chan_end_on_a .counterparty() @@ -242,6 +254,8 @@ pub(crate) fn process( chan_end_on_a.version().clone(), ); + let chan_end_path_on_b = ChannelEndPath::new(&port_id_on_b, chan_id_on_b); + // Verify the proof for the channel state against the expected channel end. // A counterparty channel id of None in not possible, and is checked by validate_basic in msg. client_state_of_b_on_a @@ -250,8 +264,7 @@ pub(crate) fn process( prefix_on_b, &msg.proof_unreceived_on_b, consensus_state_of_b_on_a.root(), - port_id_on_b, - chan_id_on_b, + &chan_end_path_on_b, &expected_chan_end_on_b, ) .map_err(ChannelError::VerifyChannelFailed) @@ -264,26 +277,29 @@ pub(crate) fn process( next_sequence: msg.next_seq_recv_on_b, }); } + let seq_recv_path_on_b = SeqRecvPath::new(&packet.port_on_b, &packet.chan_on_b); client_state_of_b_on_a.verify_next_sequence_recv( ctx_a, msg.proof_height_on_b, &conn_end_on_a, &msg.proof_unreceived_on_b, consensus_state_of_b_on_a.root(), - &packet.port_on_b, - &packet.chan_on_b, + &seq_recv_path_on_b, packet.sequence, ) } else { + let receipt_path_on_b = ReceiptPath::new( + &msg.packet.port_on_b, + &msg.packet.chan_on_b, + msg.packet.sequence, + ); client_state_of_b_on_a.verify_packet_receipt_absence( ctx_a, msg.proof_height_on_b, &conn_end_on_a, &msg.proof_unreceived_on_b, consensus_state_of_b_on_a.root(), - &packet.port_on_b, - &packet.chan_on_b, - packet.sequence, + &receipt_path_on_b, ) }; next_seq_recv_verification_result @@ -327,6 +343,7 @@ pub(crate) fn process( #[cfg(test)] mod tests { + use crate::core::ics24_host::path::ChannelEndPath; use crate::prelude::*; use test_log::test; @@ -450,7 +467,10 @@ mod tests { let events = proto_output.events; let src_channel_end = test .ctx - .channel_end(&msg.packet.port_on_a, &msg.packet.chan_on_a) + .channel_end(&ChannelEndPath::new( + &msg.packet.port_on_a, + &msg.packet.chan_on_a, + )) .unwrap(); if src_channel_end.order_matches(&Order::Ordered) { diff --git a/crates/ibc/src/core/ics04_channel/handler/write_acknowledgement.rs b/crates/ibc/src/core/ics04_channel/handler/write_acknowledgement.rs index 5565fd64f..9e24ca2d5 100644 --- a/crates/ibc/src/core/ics04_channel/handler/write_acknowledgement.rs +++ b/crates/ibc/src/core/ics04_channel/handler/write_acknowledgement.rs @@ -5,6 +5,7 @@ use crate::core::ics04_channel::msgs::acknowledgement::Acknowledgement; use crate::core::ics04_channel::packet::{Packet, PacketResult, Sequence}; use crate::core::ics04_channel::{context::ChannelReader, error::PacketError}; use crate::core::ics24_host::identifier::{ChannelId, PortId}; +use crate::core::ics24_host::path::{AckPath, ChannelEndPath}; use crate::prelude::*; use crate::{ events::IbcEvent, @@ -26,9 +27,9 @@ pub fn process( ack: Acknowledgement, ) -> HandlerResult { let mut output = HandlerOutput::builder(); - + let chan_end_path_on_b = ChannelEndPath::new(&packet.port_on_b, &packet.chan_on_b); let chan_end_on_b = ctx_b - .channel_end(&packet.port_on_b, &packet.chan_on_b) + .channel_end(&chan_end_path_on_b) .map_err(PacketError::Channel)?; if !chan_end_on_b.state_matches(&State::Open) { @@ -41,7 +42,8 @@ pub fn process( // NOTE: IBC app modules might have written the acknowledgement synchronously on // the OnRecvPacket callback so we need to check if the acknowledgement is already // set on the store and return an error if so. - match ctx_b.get_packet_acknowledgement(&packet.port_on_b, &packet.chan_on_b, &packet.sequence) { + let ack_path_on_b = AckPath::new(&packet.port_on_b, &packet.chan_on_b, packet.sequence); + match ctx_b.get_packet_acknowledgement(&ack_path_on_b) { Ok(_) => { return Err(PacketError::AcknowledgementExists { sequence: packet.sequence, diff --git a/crates/ibc/src/core/ics24_host/path.rs b/crates/ibc/src/core/ics24_host/path.rs index 5e0775ec9..8abeae066 100644 --- a/crates/ibc/src/core/ics24_host/path.rs +++ b/crates/ibc/src/core/ics24_host/path.rs @@ -32,16 +32,16 @@ pub enum Path { ClientType(ClientTypePath), ClientState(ClientStatePath), ClientConsensusState(ClientConsensusStatePath), - ClientConnections(ClientConnectionsPath), - Connections(ConnectionsPath), - Ports(PortsPath), - ChannelEnds(ChannelEndsPath), - SeqSends(SeqSendsPath), - SeqRecvs(SeqRecvsPath), - SeqAcks(SeqAcksPath), - Commitments(CommitmentsPath), - Acks(AcksPath), - Receipts(ReceiptsPath), + ClientConnection(ClientConnectionPath), + Connection(ConnectionPath), + Ports(PortPath), + ChannelEnd(ChannelEndPath), + SeqSend(SeqSendPath), + SeqRecv(SeqRecvPath), + SeqAck(SeqAckPath), + Commitment(CommitmentPath), + Ack(AckPath), + Receipt(ReceiptPath), Upgrade(ClientUpgradePath), } @@ -49,10 +49,22 @@ pub enum Path { #[display(fmt = "clients/{_0}/clientType")] pub struct ClientTypePath(pub ClientId); +impl ClientTypePath { + pub fn new(client_id: &ClientId) -> ClientTypePath { + ClientTypePath(client_id.clone()) + } +} + #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "clients/{_0}/clientState")] pub struct ClientStatePath(pub ClientId); +impl ClientStatePath { + pub fn new(client_id: &ClientId) -> ClientStatePath { + ClientStatePath(client_id.clone()) + } +} + #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "clients/{client_id}/consensusStates/{epoch}-{height}")] pub struct ClientConsensusStatePath { @@ -62,9 +74,9 @@ pub struct ClientConsensusStatePath { } impl ClientConsensusStatePath { - pub fn new(client_id: ClientId, height: Height) -> ClientConsensusStatePath { + pub fn new(client_id: &ClientId, height: &Height) -> ClientConsensusStatePath { ClientConsensusStatePath { - client_id, + client_id: client_id.clone(), epoch: height.revision_number(), height: height.revision_height(), } @@ -73,56 +85,122 @@ impl ClientConsensusStatePath { #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "clients/{_0}/connections")] -pub struct ClientConnectionsPath(pub ClientId); +pub struct ClientConnectionPath(pub ClientId); + +impl ClientConnectionPath { + pub fn new(client_id: &ClientId) -> ClientConnectionPath { + ClientConnectionPath(client_id.clone()) + } +} #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "connections/{_0}")] -pub struct ConnectionsPath(pub ConnectionId); +pub struct ConnectionPath(pub ConnectionId); + +impl ConnectionPath { + pub fn new(connection_id: &ConnectionId) -> ConnectionPath { + ConnectionPath(connection_id.clone()) + } +} #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "ports/{_0}")] -pub struct PortsPath(pub PortId); +pub struct PortPath(pub PortId); #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "channelEnds/ports/{_0}/channels/{_1}")] -pub struct ChannelEndsPath(pub PortId, pub ChannelId); +pub struct ChannelEndPath(pub PortId, pub ChannelId); + +impl ChannelEndPath { + pub fn new(port_id: &PortId, channel_id: &ChannelId) -> ChannelEndPath { + ChannelEndPath(port_id.clone(), channel_id.clone()) + } +} #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "nextSequenceSend/ports/{_0}/channels/{_1}")] -pub struct SeqSendsPath(pub PortId, pub ChannelId); +pub struct SeqSendPath(pub PortId, pub ChannelId); + +impl SeqSendPath { + pub fn new(port_id: &PortId, channel_id: &ChannelId) -> SeqSendPath { + SeqSendPath(port_id.clone(), channel_id.clone()) + } +} #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "nextSequenceRecv/ports/{_0}/channels/{_1}")] -pub struct SeqRecvsPath(pub PortId, pub ChannelId); +pub struct SeqRecvPath(pub PortId, pub ChannelId); + +impl SeqRecvPath { + pub fn new(port_id: &PortId, channel_id: &ChannelId) -> SeqRecvPath { + SeqRecvPath(port_id.clone(), channel_id.clone()) + } +} #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "nextSequenceAck/ports/{_0}/channels/{_1}")] -pub struct SeqAcksPath(pub PortId, pub ChannelId); +pub struct SeqAckPath(pub PortId, pub ChannelId); + +impl SeqAckPath { + pub fn new(port_id: &PortId, channel_id: &ChannelId) -> SeqAckPath { + SeqAckPath(port_id.clone(), channel_id.clone()) + } +} #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "commitments/ports/{port_id}/channels/{channel_id}/sequences/{sequence}")] -pub struct CommitmentsPath { +pub struct CommitmentPath { pub port_id: PortId, pub channel_id: ChannelId, pub sequence: Sequence, } +impl CommitmentPath { + pub fn new(port_id: &PortId, channel_id: &ChannelId, sequence: Sequence) -> CommitmentPath { + CommitmentPath { + port_id: port_id.clone(), + channel_id: channel_id.clone(), + sequence, + } + } +} + #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "acks/ports/{port_id}/channels/{channel_id}/sequences/{sequence}")] -pub struct AcksPath { +pub struct AckPath { pub port_id: PortId, pub channel_id: ChannelId, pub sequence: Sequence, } +impl AckPath { + pub fn new(port_id: &PortId, channel_id: &ChannelId, sequence: Sequence) -> AckPath { + AckPath { + port_id: port_id.clone(), + channel_id: channel_id.clone(), + sequence, + } + } +} + #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] #[display(fmt = "receipts/ports/{port_id}/channels/{channel_id}/sequences/{sequence}")] -pub struct ReceiptsPath { +pub struct ReceiptPath { pub port_id: PortId, pub channel_id: ChannelId, pub sequence: Sequence, } +impl ReceiptPath { + pub fn new(port_id: &PortId, channel_id: &ChannelId, sequence: Sequence) -> ReceiptPath { + ReceiptPath { + port_id: port_id.clone(), + channel_id: channel_id.clone(), + sequence, + } + } +} + /// Paths that are specific for client upgrades. #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)] pub enum ClientUpgradePath { @@ -143,7 +221,7 @@ enum SubPath { impl Path { /// Indication if the path is provable. pub fn is_provable(&self) -> bool { - !matches!(&self, Path::ClientConnections(_) | Path::Ports(_)) + !matches!(&self, Path::ClientConnection(_) | Path::Ports(_)) } /// into_bytes implementation @@ -202,7 +280,7 @@ fn parse_client_paths(components: &[&str]) -> Option { match components[2] { "clientType" => Some(ClientTypePath(client_id).into()), "clientState" => Some(ClientStatePath(client_id).into()), - "connections" => Some(ClientConnectionsPath(client_id).into()), + "connections" => Some(ClientConnectionPath(client_id).into()), _ => None, } } else if components.len() == 4 { @@ -271,7 +349,7 @@ fn parse_connections(components: &[&str]) -> Option { Err(_) => return None, }; - Some(ConnectionsPath(connection_id).into()) + Some(ConnectionPath(connection_id).into()) } fn parse_ports(components: &[&str]) -> Option { @@ -298,7 +376,7 @@ fn parse_ports(components: &[&str]) -> Option { Err(_) => return None, }; - Some(PortsPath(port_id).into()) + Some(PortPath(port_id).into()) } fn parse_channels(components: &[&str]) -> Option { @@ -370,7 +448,7 @@ fn parse_channel_ends(components: &[&str]) -> Option { let port = parse_ports(&components[1..=2]); let channel = parse_channels(&components[3..=4]); - let port_id = if let Some(Path::Ports(PortsPath(port_id))) = port { + let port_id = if let Some(Path::Ports(PortPath(port_id))) = port { port_id } else { return None; @@ -382,7 +460,7 @@ fn parse_channel_ends(components: &[&str]) -> Option { return None; }; - Some(ChannelEndsPath(port_id, channel_id).into()) + Some(ChannelEndPath(port_id, channel_id).into()) } fn parse_seqs(components: &[&str]) -> Option { @@ -398,7 +476,7 @@ fn parse_seqs(components: &[&str]) -> Option { let port = parse_ports(&components[1..=2]); let channel = parse_channels(&components[3..=4]); - let port_id = if let Some(Path::Ports(PortsPath(port_id))) = port { + let port_id = if let Some(Path::Ports(PortPath(port_id))) = port { port_id } else { return None; @@ -411,9 +489,9 @@ fn parse_seqs(components: &[&str]) -> Option { }; match first { - "nextSequenceSend" => Some(SeqSendsPath(port_id, channel_id).into()), - "nextSequenceRecv" => Some(SeqRecvsPath(port_id, channel_id).into()), - "nextSequenceAck" => Some(SeqAcksPath(port_id, channel_id).into()), + "nextSequenceSend" => Some(SeqSendPath(port_id, channel_id).into()), + "nextSequenceRecv" => Some(SeqRecvPath(port_id, channel_id).into()), + "nextSequenceAck" => Some(SeqAckPath(port_id, channel_id).into()), _ => None, } } @@ -436,7 +514,7 @@ fn parse_commitments(components: &[&str]) -> Option { let channel = parse_channels(&components[3..=4]); let sequence = parse_sequences(&components[5..]); - let port_id = if let Some(Path::Ports(PortsPath(port_id))) = port { + let port_id = if let Some(Path::Ports(PortPath(port_id))) = port { port_id } else { return None; @@ -455,7 +533,7 @@ fn parse_commitments(components: &[&str]) -> Option { }; Some( - CommitmentsPath { + CommitmentPath { port_id, channel_id, sequence, @@ -482,7 +560,7 @@ fn parse_acks(components: &[&str]) -> Option { let channel = parse_channels(&components[3..=4]); let sequence = parse_sequences(&components[5..]); - let port_id = if let Some(Path::Ports(PortsPath(port_id))) = port { + let port_id = if let Some(Path::Ports(PortPath(port_id))) = port { port_id } else { return None; @@ -501,7 +579,7 @@ fn parse_acks(components: &[&str]) -> Option { }; Some( - AcksPath { + AckPath { port_id, channel_id, sequence, @@ -528,7 +606,7 @@ fn parse_receipts(components: &[&str]) -> Option { let channel = parse_channels(&components[3..=4]); let sequence = parse_sequences(&components[5..]); - let port_id = if let Some(Path::Ports(PortsPath(port_id))) = port { + let port_id = if let Some(Path::Ports(PortPath(port_id))) = port { port_id } else { return None; @@ -547,7 +625,7 @@ fn parse_receipts(components: &[&str]) -> Option { }; Some( - ReceiptsPath { + ReceiptPath { port_id, channel_id, sequence, @@ -680,7 +758,7 @@ mod tests { assert!(path.is_ok()); assert_eq!( path.unwrap(), - Path::ClientConnections(ClientConnectionsPath(ClientId::default())) + Path::ClientConnection(ClientConnectionPath(ClientId::default())) ); } @@ -691,7 +769,7 @@ mod tests { assert_eq!( parse_connections(&components), - Some(Path::Connections(ConnectionsPath(ConnectionId::new(0)))), + Some(Path::Connection(ConnectionPath(ConnectionId::new(0)))), ); } @@ -703,7 +781,7 @@ mod tests { assert!(path.is_ok()); assert_eq!( path.unwrap(), - Path::Connections(ConnectionsPath(ConnectionId::new(0))) + Path::Connection(ConnectionPath(ConnectionId::new(0))) ); } @@ -714,7 +792,7 @@ mod tests { assert_eq!( parse_ports(&components), - Some(Path::Ports(PortsPath(PortId::default()))), + Some(Path::Ports(PortPath(PortId::default()))), ); } @@ -724,7 +802,7 @@ mod tests { let path = Path::from_str(path); assert!(path.is_ok()); - assert_eq!(path.unwrap(), Path::Ports(PortsPath(PortId::default()))); + assert_eq!(path.unwrap(), Path::Ports(PortPath(PortId::default()))); } #[test] @@ -772,7 +850,7 @@ mod tests { assert_eq!( parse_channel_ends(&components), - Some(Path::ChannelEnds(ChannelEndsPath( + Some(Path::ChannelEnd(ChannelEndPath( PortId::default(), ChannelId::default() ))), @@ -787,7 +865,7 @@ mod tests { assert!(path.is_ok()); assert_eq!( path.unwrap(), - Path::ChannelEnds(ChannelEndsPath(PortId::default(), ChannelId::default())), + Path::ChannelEnd(ChannelEndPath(PortId::default(), ChannelId::default())), ); } @@ -798,7 +876,7 @@ mod tests { assert_eq!( parse_seqs(&components), - Some(Path::SeqSends(SeqSendsPath( + Some(Path::SeqSend(SeqSendPath( PortId::default(), ChannelId::default() ))), @@ -809,7 +887,7 @@ mod tests { assert_eq!( parse_seqs(&components), - Some(Path::SeqRecvs(SeqRecvsPath( + Some(Path::SeqRecv(SeqRecvPath( PortId::default(), ChannelId::default() ))), @@ -820,7 +898,7 @@ mod tests { assert_eq!( parse_seqs(&components), - Some(Path::SeqAcks(SeqAcksPath( + Some(Path::SeqAck(SeqAckPath( PortId::default(), ChannelId::default() ))), @@ -835,7 +913,7 @@ mod tests { assert!(path.is_ok()); assert_eq!( path.unwrap(), - Path::SeqSends(SeqSendsPath(PortId::default(), ChannelId::default())), + Path::SeqSend(SeqSendPath(PortId::default(), ChannelId::default())), ); } @@ -847,7 +925,7 @@ mod tests { assert!(path.is_ok()); assert_eq!( path.unwrap(), - Path::SeqRecvs(SeqRecvsPath(PortId::default(), ChannelId::default())), + Path::SeqRecv(SeqRecvPath(PortId::default(), ChannelId::default())), ); } @@ -859,7 +937,7 @@ mod tests { assert!(path.is_ok()); assert_eq!( path.unwrap(), - Path::SeqAcks(SeqAcksPath(PortId::default(), ChannelId::default())), + Path::SeqAck(SeqAckPath(PortId::default(), ChannelId::default())), ); } @@ -870,7 +948,7 @@ mod tests { assert_eq!( parse_commitments(&components), - Some(Path::Commitments(CommitmentsPath { + Some(Path::Commitment(CommitmentPath { port_id: PortId::default(), channel_id: ChannelId::default(), sequence: Sequence::default(), @@ -886,7 +964,7 @@ mod tests { assert!(path.is_ok()); assert_eq!( path.unwrap(), - Path::Commitments(CommitmentsPath { + Path::Commitment(CommitmentPath { port_id: PortId::default(), channel_id: ChannelId::default(), sequence: Sequence::default(), @@ -901,7 +979,7 @@ mod tests { assert_eq!( parse_acks(&components), - Some(Path::Acks(AcksPath { + Some(Path::Ack(AckPath { port_id: PortId::default(), channel_id: ChannelId::default(), sequence: Sequence::default(), @@ -917,7 +995,7 @@ mod tests { assert!(path.is_ok()); assert_eq!( path.unwrap(), - Path::Acks(AcksPath { + Path::Ack(AckPath { port_id: PortId::default(), channel_id: ChannelId::default(), sequence: Sequence::default(), @@ -932,7 +1010,7 @@ mod tests { assert_eq!( parse_receipts(&components), - Some(Path::Receipts(ReceiptsPath { + Some(Path::Receipt(ReceiptPath { port_id: PortId::default(), channel_id: ChannelId::default(), sequence: Sequence::default(), @@ -948,7 +1026,7 @@ mod tests { assert!(path.is_ok()); assert_eq!( path.unwrap(), - Path::Receipts(ReceiptsPath { + Path::Receipt(ReceiptPath { port_id: PortId::default(), channel_id: ChannelId::default(), sequence: Sequence::default(), diff --git a/crates/ibc/src/core/ics26_routing/handler.rs b/crates/ibc/src/core/ics26_routing/handler.rs index fbb7c9705..399ce2f6b 100644 --- a/crates/ibc/src/core/ics26_routing/handler.rs +++ b/crates/ibc/src/core/ics26_routing/handler.rs @@ -204,6 +204,7 @@ mod tests { use crate::core::ics23_commitment::commitment::test_util::get_dummy_merkle_proof; use crate::core::ics23_commitment::commitment::CommitmentPrefix; use crate::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId}; + use crate::core::ics24_host::path::CommitmentPath; use crate::core::ics26_routing::context::{ModuleId, Router, RouterBuilder, RouterContext}; use crate::core::ics26_routing::error::RouterError; use crate::core::ics26_routing::handler::dispatch; @@ -514,11 +515,11 @@ mod tests { msg: MsgEnvelope::Packet(PacketMsg::Ack(msg_ack_packet.clone())).into(), want_pass: true, state_check: Some(Box::new(move |ctx| { - ctx.get_packet_commitment( + ctx.get_packet_commitment(&CommitmentPath::new( &msg_ack_packet.packet.port_on_a, &msg_ack_packet.packet.chan_on_a, - &msg_ack_packet.packet.sequence, - ) + msg_ack_packet.packet.sequence, + )) .is_err() })), }, diff --git a/crates/ibc/src/mock/client_state.rs b/crates/ibc/src/mock/client_state.rs index f06b14682..52f7b57cd 100644 --- a/crates/ibc/src/mock/client_state.rs +++ b/crates/ibc/src/mock/client_state.rs @@ -22,8 +22,11 @@ use crate::core::ics23_commitment::commitment::{ CommitmentPrefix, CommitmentProofBytes, CommitmentRoot, }; use crate::core::ics23_commitment::merkle::apply_prefix; -use crate::core::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId}; -use crate::core::ics24_host::path::ClientConsensusStatePath; +use crate::core::ics24_host::identifier::{ChainId, ClientId}; +use crate::core::ics24_host::path::{ + AckPath, ChannelEndPath, ClientConsensusStatePath, ClientStatePath, CommitmentPath, + ConnectionPath, ReceiptPath, SeqRecvPath, +}; use crate::core::ics24_host::Path; use crate::mock::client_state::client_type as mock_client_type; use crate::mock::consensus_state::MockConsensusState; @@ -312,16 +315,11 @@ impl ClientState for MockClientState { prefix: &CommitmentPrefix, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - client_id: &ClientId, - consensus_height: Height, + client_cons_state_path: &ClientConsensusStatePath, _expected_consensus_state: &dyn ConsensusState, ) -> Result<(), ClientError> { - let client_prefixed_path = Path::ClientConsensusState(ClientConsensusStatePath { - client_id: client_id.clone(), - epoch: consensus_height.revision_number(), - height: consensus_height.revision_height(), - }) - .to_string(); + let client_prefixed_path = + Path::ClientConsensusState(client_cons_state_path.clone()).to_string(); let _path = apply_prefix(prefix, vec![client_prefixed_path]); @@ -334,7 +332,7 @@ impl ClientState for MockClientState { _prefix: &CommitmentPrefix, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - _connection_id: &ConnectionId, + _conn_path: &ConnectionPath, _expected_connection_end: &ConnectionEnd, ) -> Result<(), ClientError> { Ok(()) @@ -346,8 +344,7 @@ impl ClientState for MockClientState { _prefix: &CommitmentPrefix, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - _port_id: &PortId, - _channel_id: &ChannelId, + _chan_end_path: &ChannelEndPath, _expected_channel_end: &ChannelEnd, ) -> Result<(), ClientError> { Ok(()) @@ -359,7 +356,7 @@ impl ClientState for MockClientState { _prefix: &CommitmentPrefix, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - _client_id: &ClientId, + _client_state_path: &ClientStatePath, _expected_client_state: Any, ) -> Result<(), ClientError> { Ok(()) @@ -372,9 +369,7 @@ impl ClientState for MockClientState { _connection_end: &ConnectionEnd, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - _port_id: &PortId, - _channel_id: &ChannelId, - _sequence: Sequence, + _commitment_path: &CommitmentPath, _commitment: PacketCommitment, ) -> Result<(), ClientError> { Ok(()) @@ -387,9 +382,7 @@ impl ClientState for MockClientState { _connection_end: &ConnectionEnd, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - _port_id: &PortId, - _channel_id: &ChannelId, - _sequence: Sequence, + _commitment_path: &CommitmentPath, _commitment: PacketCommitment, ) -> Result<(), ClientError> { Ok(()) @@ -402,9 +395,7 @@ impl ClientState for MockClientState { _connection_end: &ConnectionEnd, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - _port_id: &PortId, - _channel_id: &ChannelId, - _sequence: Sequence, + _ack_path: &AckPath, _ack: AcknowledgementCommitment, ) -> Result<(), ClientError> { Ok(()) @@ -417,8 +408,7 @@ impl ClientState for MockClientState { _connection_end: &ConnectionEnd, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - _port_id: &PortId, - _channel_id: &ChannelId, + _seq_recv_path: &SeqRecvPath, _sequence: Sequence, ) -> Result<(), ClientError> { Ok(()) @@ -431,8 +421,7 @@ impl ClientState for MockClientState { _connection_end: &ConnectionEnd, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - _port_id: &PortId, - _channel_id: &ChannelId, + _seq_recv_path: &SeqRecvPath, _sequence: Sequence, ) -> Result<(), ClientError> { Ok(()) @@ -445,9 +434,7 @@ impl ClientState for MockClientState { _connection_end: &ConnectionEnd, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - _port_id: &PortId, - _channel_id: &ChannelId, - _sequence: Sequence, + _receipt_path: &ReceiptPath, ) -> Result<(), ClientError> { Ok(()) } @@ -459,9 +446,7 @@ impl ClientState for MockClientState { _connection_end: &ConnectionEnd, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - _port_id: &PortId, - _channel_id: &ChannelId, - _sequence: Sequence, + _receipt_path: &ReceiptPath, ) -> Result<(), ClientError> { Ok(()) } @@ -473,9 +458,7 @@ impl ClientState for MockClientState { _connection_end: &ConnectionEnd, _proof: &CommitmentProofBytes, _root: &CommitmentRoot, - _port_id: &PortId, - _channel_id: &ChannelId, - _sequence: Sequence, + _ack_path: &AckPath, _ack: AcknowledgementCommitment, ) -> Result<(), ClientError> { Ok(()) diff --git a/crates/ibc/src/mock/context.rs b/crates/ibc/src/mock/context.rs index c6498a6d1..0c542c08b 100644 --- a/crates/ibc/src/mock/context.rs +++ b/crates/ibc/src/mock/context.rs @@ -1,6 +1,10 @@ //! Implementation of a global context mock. Used in testing handlers of all IBC modules. use crate::clients::ics07_tendermint::TENDERMINT_CLIENT_TYPE; +use crate::core::ics24_host::path::{ + AckPath, ChannelEndPath, ClientConsensusStatePath, CommitmentPath, ReceiptPath, SeqAckPath, + SeqRecvPath, SeqSendPath, +}; use crate::prelude::*; use alloc::collections::btree_map::BTreeMap; @@ -707,22 +711,18 @@ impl PortReader for MockContext { } impl ChannelReader for MockContext { - fn channel_end( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result { + fn channel_end(&self, chan_end_path: &ChannelEndPath) -> Result { match self .ibc_store .lock() .channels - .get(port_id) - .and_then(|map| map.get(channel_id)) + .get(&chan_end_path.0) + .and_then(|map| map.get(&chan_end_path.1)) { Some(channel_end) => Ok(channel_end.clone()), None => Err(ChannelError::ChannelNotFound { - port_id: port_id.clone(), - channel_id: channel_id.clone(), + port_id: chan_end_path.0.clone(), + channel_id: chan_end_path.1.clone(), }), } } @@ -748,127 +748,111 @@ impl ChannelReader for MockContext { fn client_consensus_state( &self, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result, ChannelError> { - ClientReader::consensus_state(self, client_id, height) + ClientReader::consensus_state(self, client_cons_state_path) .map_err(|e| ChannelError::Connection(ConnectionError::Client(e))) } - fn get_next_sequence_send( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result { + fn get_next_sequence_send(&self, seq_send_path: &SeqSendPath) -> Result { match self .ibc_store .lock() .next_sequence_send - .get(port_id) - .and_then(|map| map.get(channel_id)) + .get(&seq_send_path.0) + .and_then(|map| map.get(&seq_send_path.1)) { Some(sequence) => Ok(*sequence), None => Err(PacketError::MissingNextSendSeq { - port_id: port_id.clone(), - channel_id: channel_id.clone(), + port_id: seq_send_path.0.clone(), + channel_id: seq_send_path.1.clone(), }), } } - fn get_next_sequence_recv( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result { + fn get_next_sequence_recv(&self, seq_recv_path: &SeqRecvPath) -> Result { match self .ibc_store .lock() .next_sequence_recv - .get(port_id) - .and_then(|map| map.get(channel_id)) + .get(&seq_recv_path.0) + .and_then(|map| map.get(&seq_recv_path.1)) { Some(sequence) => Ok(*sequence), None => Err(PacketError::MissingNextRecvSeq { - port_id: port_id.clone(), - channel_id: channel_id.clone(), + port_id: seq_recv_path.0.clone(), + channel_id: seq_recv_path.1.clone(), }), } } - fn get_next_sequence_ack( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result { + fn get_next_sequence_ack(&self, seq_acks_path: &SeqAckPath) -> Result { match self .ibc_store .lock() .next_sequence_ack - .get(port_id) - .and_then(|map| map.get(channel_id)) + .get(&seq_acks_path.0) + .and_then(|map| map.get(&seq_acks_path.1)) { Some(sequence) => Ok(*sequence), None => Err(PacketError::MissingNextAckSeq { - port_id: port_id.clone(), - channel_id: channel_id.clone(), + port_id: seq_acks_path.0.clone(), + channel_id: seq_acks_path.1.clone(), }), } } fn get_packet_commitment( &self, - port_id: &PortId, - channel_id: &ChannelId, - seq: &Sequence, + commitment_path: &CommitmentPath, ) -> Result { match self .ibc_store .lock() .packet_commitment - .get(port_id) - .and_then(|map| map.get(channel_id)) - .and_then(|map| map.get(seq)) + .get(&commitment_path.port_id) + .and_then(|map| map.get(&commitment_path.channel_id)) + .and_then(|map| map.get(&commitment_path.sequence)) { Some(commitment) => Ok(commitment.clone()), - None => Err(PacketError::PacketCommitmentNotFound { sequence: *seq }), + None => Err(PacketError::PacketCommitmentNotFound { + sequence: commitment_path.sequence, + }), } } - fn get_packet_receipt( - &self, - port_id: &PortId, - channel_id: &ChannelId, - seq: &Sequence, - ) -> Result { + fn get_packet_receipt(&self, receipt_path: &ReceiptPath) -> Result { match self .ibc_store .lock() .packet_receipt - .get(port_id) - .and_then(|map| map.get(channel_id)) - .and_then(|map| map.get(seq)) + .get(&receipt_path.port_id) + .and_then(|map| map.get(&receipt_path.channel_id)) + .and_then(|map| map.get(&receipt_path.sequence)) { Some(receipt) => Ok(receipt.clone()), - None => Err(PacketError::PacketReceiptNotFound { sequence: *seq }), + None => Err(PacketError::PacketReceiptNotFound { + sequence: receipt_path.sequence, + }), } } fn get_packet_acknowledgement( &self, - port_id: &PortId, - channel_id: &ChannelId, - seq: &Sequence, + ack_path: &AckPath, ) -> Result { match self .ibc_store .lock() .packet_acknowledgement - .get(port_id) - .and_then(|map| map.get(channel_id)) - .and_then(|map| map.get(seq)) + .get(&ack_path.port_id) + .and_then(|map| map.get(&ack_path.channel_id)) + .and_then(|map| map.get(&ack_path.sequence)) { Some(ack) => Ok(ack.clone()), - None => Err(PacketError::PacketAcknowledgementNotFound { sequence: *seq }), + None => Err(PacketError::PacketAcknowledgementNotFound { + sequence: ack_path.sequence, + }), } } @@ -1147,11 +1131,10 @@ impl ConnectionReader for MockContext { fn client_consensus_state( &self, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result, ConnectionError> { // Forward method call to the Ics2Client-specific method. - ClientReader::consensus_state(self, client_id, height).map_err(ConnectionError::Client) + ClientReader::consensus_state(self, client_cons_state_path).map_err(ConnectionError::Client) } fn host_consensus_state( @@ -1240,20 +1223,26 @@ impl ClientReader for MockContext { fn consensus_state( &self, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result, ClientError> { - match self.ibc_store.lock().clients.get(client_id) { - Some(client_record) => match client_record.consensus_states.get(height) { + let height = + Height::new(client_cons_state_path.epoch, client_cons_state_path.height).unwrap(); + match self + .ibc_store + .lock() + .clients + .get(&client_cons_state_path.client_id) + { + Some(client_record) => match client_record.consensus_states.get(&height) { Some(consensus_state) => Ok(consensus_state.clone()), None => Err(ClientError::ConsensusStateNotFound { - client_id: client_id.clone(), - height: *height, + client_id: client_cons_state_path.client_id.clone(), + height, }), }, None => Err(ClientError::ConsensusStateNotFound { - client_id: client_id.clone(), - height: *height, + client_id: client_cons_state_path.client_id.clone(), + height, }), } } @@ -1261,17 +1250,21 @@ impl ClientReader for MockContext { /// Search for the lowest consensus state higher than `height`. fn next_consensus_state( &self, - client_id: &ClientId, - height: &Height, + next_client_cons_state_path: &ClientConsensusStatePath, ) -> Result>, ClientError> { let ibc_store = self.ibc_store.lock(); - let client_record = - ibc_store - .clients - .get(client_id) - .ok_or_else(|| ClientError::ClientNotFound { - client_id: client_id.clone(), - })?; + let client_record = ibc_store + .clients + .get(&next_client_cons_state_path.client_id) + .ok_or_else(|| ClientError::ClientNotFound { + client_id: next_client_cons_state_path.client_id.clone(), + })?; + + let height = Height::new( + next_client_cons_state_path.epoch, + next_client_cons_state_path.height, + ) + .unwrap(); // Get the consensus state heights and sort them in ascending order. let mut heights: Vec = client_record.consensus_states.keys().cloned().collect(); @@ -1279,7 +1272,7 @@ impl ClientReader for MockContext { // Search for next state. for h in heights { - if h > *height { + if h > height { // unwrap should never happen, as the consensus state for h must exist return Ok(Some( client_record.consensus_states.get(&h).unwrap().clone(), @@ -1292,17 +1285,21 @@ impl ClientReader for MockContext { /// Search for the highest consensus state lower than `height`. fn prev_consensus_state( &self, - client_id: &ClientId, - height: &Height, + prev_client_cons_state_path: &ClientConsensusStatePath, ) -> Result>, ClientError> { let ibc_store = self.ibc_store.lock(); - let client_record = - ibc_store - .clients - .get(client_id) - .ok_or_else(|| ClientError::ClientNotFound { - client_id: client_id.clone(), - })?; + let client_record = ibc_store + .clients + .get(&prev_client_cons_state_path.client_id) + .ok_or_else(|| ClientError::ClientNotFound { + client_id: prev_client_cons_state_path.client_id.clone(), + })?; + + let height = Height::new( + prev_client_cons_state_path.epoch, + prev_client_cons_state_path.height, + ) + .unwrap(); // Get the consensus state heights and sort them in descending order. let mut heights: Vec = client_record.consensus_states.keys().cloned().collect(); @@ -1310,7 +1307,7 @@ impl ClientReader for MockContext { // Search for previous state. for h in heights { - if h < *height { + if h < height { // unwrap should never happen, as the consensus state for h must exist return Ok(Some( client_record.consensus_states.get(&h).unwrap().clone(), @@ -1507,27 +1504,25 @@ impl ValidationContext for MockContext { fn consensus_state( &self, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result, ContextError> { - ClientReader::consensus_state(self, client_id, height).map_err(ContextError::ClientError) + ClientReader::consensus_state(self, client_cons_state_path) + .map_err(ContextError::ClientError) } fn next_consensus_state( &self, - client_id: &ClientId, - height: &Height, + next_client_cons_state_path: &ClientConsensusStatePath, ) -> Result>, ContextError> { - ClientReader::next_consensus_state(self, client_id, height) + ClientReader::next_consensus_state(self, next_client_cons_state_path) .map_err(ContextError::ClientError) } fn prev_consensus_state( &self, - client_id: &ClientId, - height: &Height, + prev_client_cons_state_path: &ClientConsensusStatePath, ) -> Result>, ContextError> { - ClientReader::prev_consensus_state(self, client_id, height) + ClientReader::prev_consensus_state(self, prev_client_cons_state_path) .map_err(ContextError::ClientError) } @@ -1566,12 +1561,8 @@ impl ValidationContext for MockContext { ConnectionReader::connection_counter(self).map_err(ContextError::ConnectionError) } - fn channel_end( - &self, - port_channel_id: &(PortId, ChannelId), - ) -> Result { - ChannelReader::channel_end(self, &port_channel_id.0, &port_channel_id.1) - .map_err(ContextError::ChannelError) + fn channel_end(&self, chan_end_path: &ChannelEndPath) -> Result { + ChannelReader::channel_end(self, chan_end_path).map_err(ContextError::ChannelError) } fn connection_channels( @@ -1583,50 +1574,41 @@ impl ValidationContext for MockContext { fn get_next_sequence_send( &self, - port_channel_id: &(PortId, ChannelId), + seq_send_path: &SeqSendPath, ) -> Result { - ChannelReader::get_next_sequence_send(self, &port_channel_id.0, &port_channel_id.1) + ChannelReader::get_next_sequence_send(self, seq_send_path) .map_err(ContextError::PacketError) } fn get_next_sequence_recv( &self, - port_channel_id: &(PortId, ChannelId), + seq_recv_path: &SeqRecvPath, ) -> Result { - ChannelReader::get_next_sequence_recv(self, &port_channel_id.0, &port_channel_id.1) + ChannelReader::get_next_sequence_recv(self, seq_recv_path) .map_err(ContextError::PacketError) } - fn get_next_sequence_ack( - &self, - port_channel_id: &(PortId, ChannelId), - ) -> Result { - ChannelReader::get_next_sequence_ack(self, &port_channel_id.0, &port_channel_id.1) - .map_err(ContextError::PacketError) + fn get_next_sequence_ack(&self, seq_ack_path: &SeqAckPath) -> Result { + ChannelReader::get_next_sequence_ack(self, seq_ack_path).map_err(ContextError::PacketError) } fn get_packet_commitment( &self, - key: &(PortId, ChannelId, Sequence), + commitment_path: &CommitmentPath, ) -> Result { - ChannelReader::get_packet_commitment(self, &key.0, &key.1, &key.2) + ChannelReader::get_packet_commitment(self, commitment_path) .map_err(ContextError::PacketError) } - fn get_packet_receipt( - &self, - key: &(PortId, ChannelId, Sequence), - ) -> Result { - ChannelReader::get_packet_receipt(self, &key.0, &key.1, &key.2) - .map_err(ContextError::PacketError) + fn get_packet_receipt(&self, receipt_path: &ReceiptPath) -> Result { + ChannelReader::get_packet_receipt(self, receipt_path).map_err(ContextError::PacketError) } fn get_packet_acknowledgement( &self, - key: &(PortId, ChannelId, Sequence), + ack_path: &AckPath, ) -> Result { - ChannelReader::get_packet_acknowledgement(self, &key.0, &key.1, &key.2) - .map_err(ContextError::PacketError) + ChannelReader::get_packet_acknowledgement(self, ack_path).map_err(ContextError::PacketError) } fn hash(&self, value: &[u8]) -> Vec { diff --git a/crates/ibc/src/test_utils.rs b/crates/ibc/src/test_utils.rs index 5033cebff..18f11b16f 100644 --- a/crates/ibc/src/test_utils.rs +++ b/crates/ibc/src/test_utils.rs @@ -23,6 +23,7 @@ use crate::core::ics04_channel::msgs::acknowledgement::Acknowledgement; use crate::core::ics04_channel::packet::{Packet, Sequence}; use crate::core::ics04_channel::Version; use crate::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId}; +use crate::core::ics24_host::path::{ChannelEndPath, ClientConsensusStatePath, SeqSendPath}; use crate::core::ics26_routing::context::{Module, ModuleOutputBuilder}; use crate::mock::context::MockIbcStore; use crate::prelude::*; @@ -308,22 +309,18 @@ impl TokenTransferReader for DummyTransferModule { } impl SendPacketReader for DummyTransferModule { - fn channel_end( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result { + fn channel_end(&self, chan_end_path: &ChannelEndPath) -> Result { match self .ibc_store .lock() .channels - .get(port_id) - .and_then(|map| map.get(channel_id)) + .get(&chan_end_path.0) + .and_then(|map| map.get(&chan_end_path.1)) { Some(channel_end) => Ok(channel_end.clone()), None => Err(PacketError::ChannelNotFound { - port_id: port_id.clone(), - channel_id: channel_id.clone(), + port_id: chan_end_path.0.clone(), + channel_id: chan_end_path.1.clone(), }), } } @@ -357,41 +354,43 @@ impl SendPacketReader for DummyTransferModule { fn client_consensus_state( &self, - client_id: &ClientId, - height: &Height, + client_cons_state_path: &ClientConsensusStatePath, ) -> Result, PacketError> { - match self.ibc_store.lock().clients.get(client_id) { - Some(client_record) => match client_record.consensus_states.get(height) { + let height = + Height::new(client_cons_state_path.epoch, client_cons_state_path.height).unwrap(); + match self + .ibc_store + .lock() + .clients + .get(&client_cons_state_path.client_id) + { + Some(client_record) => match client_record.consensus_states.get(&height) { Some(consensus_state) => Ok(consensus_state.clone()), None => Err(ClientError::ConsensusStateNotFound { - client_id: client_id.clone(), - height: *height, + client_id: client_cons_state_path.client_id.clone(), + height, }), }, None => Err(ClientError::ConsensusStateNotFound { - client_id: client_id.clone(), - height: *height, + client_id: client_cons_state_path.client_id.clone(), + height, }), } .map_err(|e| PacketError::Connection(ConnectionError::Client(e))) } - fn get_next_sequence_send( - &self, - port_id: &PortId, - channel_id: &ChannelId, - ) -> Result { + fn get_next_sequence_send(&self, seq_send_path: &SeqSendPath) -> Result { match self .ibc_store .lock() .next_sequence_send - .get(port_id) - .and_then(|map| map.get(channel_id)) + .get(&seq_send_path.0) + .and_then(|map| map.get(&seq_send_path.1)) { Some(sequence) => Ok(*sequence), None => Err(PacketError::MissingNextSendSeq { - port_id: port_id.clone(), - channel_id: channel_id.clone(), + port_id: seq_send_path.0.clone(), + channel_id: seq_send_path.1.clone(), }), } }