From 92f4cf954efa5107f529d88250d72a5d3629312e Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Tue, 10 Jan 2023 16:07:00 -0500 Subject: [PATCH 01/22] Add `Router` trait for `ValidationContext` --- crates/ibc/src/core/context.rs | 11 +- crates/ibc/src/core/ics26_routing/context.rs | 2 +- crates/ibc/src/mock/context.rs | 411 +++++++++++-------- 3 files changed, 252 insertions(+), 172 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 2383d7c5a..5313d57da 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -85,6 +85,7 @@ mod val_exec_ctx { ClientConnectionsPath, ClientConsensusStatePath, ClientStatePath, ClientTypePath, CommitmentsPath, ConnectionsPath, ReceiptsPath, }; + use crate::core::ics26_routing::context::{Module, ModuleId}; use crate::core::{ ics02_client::{ handler::{create_client, misbehaviour, update_client, upgrade_client}, @@ -103,7 +104,15 @@ mod val_exec_ctx { use super::ContextError; - pub trait ValidationContext { + pub trait Router { + /// Returns a mutable reference to a `Module` registered against the specified `ModuleId` + fn get_route_mut(&mut self, module_id: &ModuleId) -> Option<&mut dyn Module>; + + /// Returns true if the `Router` has a `Module` registered against the specified `ModuleId` + fn has_route(&self, module_id: &ModuleId) -> bool; + } + + pub trait ValidationContext: Router { /// Validation entrypoint. fn validate(&self, message: MsgEnvelope) -> Result<(), RouterError> where diff --git a/crates/ibc/src/core/ics26_routing/context.rs b/crates/ibc/src/core/ics26_routing/context.rs index 54ba93ff3..57978dbb5 100644 --- a/crates/ibc/src/core/ics26_routing/context.rs +++ b/crates/ibc/src/core/ics26_routing/context.rs @@ -110,7 +110,7 @@ impl OnRecvPacketAck { pub type ModuleOutputBuilder = HandlerOutputBuilder<(), ModuleEvent>; -pub trait Module: Send + Sync + AsAnyMut { +pub trait Module: Send + Sync + AsAnyMut + Debug { #[allow(clippy::too_many_arguments)] fn on_chan_open_init( &mut self, diff --git a/crates/ibc/src/mock/context.rs b/crates/ibc/src/mock/context.rs index d7868d402..755246209 100644 --- a/crates/ibc/src/mock/context.rs +++ b/crates/ibc/src/mock/context.rs @@ -54,11 +54,6 @@ use crate::Height; use super::client_state::MOCK_CLIENT_TYPE; -#[cfg(feature = "val_exec_ctx")] -use crate::core::context::ContextError; -#[cfg(feature = "val_exec_ctx")] -use crate::core::ValidationContext; - pub const DEFAULT_BLOCK_TIME_SECS: u64 = 3; /// A context implementing the dependencies necessary for testing any IBC module. @@ -85,6 +80,10 @@ pub struct MockContext { /// ICS26 router impl router: MockRouter, + + /// To implement ValidationContext Router + #[cfg(feature = "val_exec_ctx")] + new_router: BTreeMap>, } /// Returns a MockContext with bare minimum initialization: no clients, no connections and no channels are @@ -109,14 +108,29 @@ impl Clone for MockContext { let ibc_store = self.ibc_store.lock().clone(); Arc::new(Mutex::new(ibc_store)) }; - Self { - host_chain_type: self.host_chain_type, - host_chain_id: self.host_chain_id.clone(), - max_history_size: self.max_history_size, - history: self.history.clone(), - block_time: self.block_time, - ibc_store, - router: self.router.clone(), + cfg_if::cfg_if! { + if #[cfg(not(feature = "val_exec_ctx"))] { + Self { + host_chain_type: self.host_chain_type, + host_chain_id: self.host_chain_id.clone(), + max_history_size: self.max_history_size, + history: self.history.clone(), + block_time: self.block_time, + ibc_store, + router: self.router.clone(), + } + } else { + Self { + host_chain_type: self.host_chain_type, + host_chain_id: self.host_chain_id.clone(), + max_history_size: self.max_history_size, + history: self.history.clone(), + block_time: self.block_time, + ibc_store, + router: self.router.clone(), + new_router: self.new_router.clone(), + } + } } } } @@ -156,28 +170,57 @@ impl MockContext { let block_time = Duration::from_secs(DEFAULT_BLOCK_TIME_SECS); let next_block_timestamp = Timestamp::now().add(block_time).unwrap(); - MockContext { - host_chain_type: host_type, - host_chain_id: host_id.clone(), - max_history_size, - history: (0..n) - .rev() - .map(|i| { - // generate blocks with timestamps -> N, N - BT, N - 2BT, ... - // where N = now(), BT = block_time - HostBlock::generate_block( - host_id.clone(), - host_type, - latest_height.sub(i).unwrap().revision_height(), - next_block_timestamp - .sub(Duration::from_secs(DEFAULT_BLOCK_TIME_SECS * (i + 1))) - .unwrap(), - ) - }) - .collect(), - block_time, - ibc_store: Arc::new(Mutex::new(MockIbcStore::default())), - router: Default::default(), + cfg_if::cfg_if! { + if #[cfg(not(feature = "val_exec_ctx"))] { + MockContext { + host_chain_type: host_type, + host_chain_id: host_id.clone(), + max_history_size, + history: (0..n) + .rev() + .map(|i| { + // generate blocks with timestamps -> N, N - BT, N - 2BT, ... + // where N = now(), BT = block_time + HostBlock::generate_block( + host_id.clone(), + host_type, + latest_height.sub(i).unwrap().revision_height(), + next_block_timestamp + .sub(Duration::from_secs(DEFAULT_BLOCK_TIME_SECS * (i + 1))) + .unwrap(), + ) + }) + .collect(), + block_time, + ibc_store: Arc::new(Mutex::new(MockIbcStore::default())), + router: Default::default(), + } + } else { + MockContext { + host_chain_type: host_type, + host_chain_id: host_id.clone(), + max_history_size, + history: (0..n) + .rev() + .map(|i| { + // generate blocks with timestamps -> N, N - BT, N - 2BT, ... + // where N = now(), BT = block_time + HostBlock::generate_block( + host_id.clone(), + host_type, + latest_height.sub(i).unwrap().revision_height(), + next_block_timestamp + .sub(Duration::from_secs(DEFAULT_BLOCK_TIME_SECS * (i + 1))) + .unwrap(), + ) + }) + .collect(), + block_time, + ibc_store: Arc::new(Mutex::new(MockIbcStore::default())), + router: Default::default(), + new_router: BTreeMap::new(), + } + } } } @@ -1476,167 +1519,195 @@ impl RelayerContext for MockContext { } #[cfg(feature = "val_exec_ctx")] -impl ValidationContext for MockContext { - fn client_state(&self, client_id: &ClientId) -> Result, ContextError> { - ClientReader::client_state(self, client_id).map_err(ContextError::ClientError) - } +pub use val_exec_ctx::*; +#[cfg(feature = "val_exec_ctx")] +mod val_exec_ctx { + pub use super::*; - fn decode_client_state(&self, client_state: Any) -> Result, ContextError> { - ClientReader::decode_client_state(self, client_state).map_err(ContextError::ClientError) - } + use crate::core::context::ContextError; + use crate::core::context::Router as NewRouter; + use crate::core::ValidationContext; - fn consensus_state( - &self, - client_id: &ClientId, - height: &Height, - ) -> Result, ContextError> { - ClientReader::consensus_state(self, client_id, height).map_err(ContextError::ClientError) - } + impl NewRouter for MockContext { + fn get_route_mut(&mut self, module_id: &ModuleId) -> Option<&mut dyn Module> { + self.new_router.get_mut(module_id).and_then(Arc::get_mut) + } - fn next_consensus_state( - &self, - client_id: &ClientId, - height: &Height, - ) -> Result>, ContextError> { - ClientReader::next_consensus_state(self, client_id, height) - .map_err(ContextError::ClientError) + fn has_route(&self, module_id: &ModuleId) -> bool { + self.new_router.get(module_id).is_some() + } } - fn prev_consensus_state( - &self, - client_id: &ClientId, - height: &Height, - ) -> Result>, ContextError> { - ClientReader::prev_consensus_state(self, client_id, height) - .map_err(ContextError::ClientError) - } + impl ValidationContext for MockContext { + fn client_state(&self, client_id: &ClientId) -> Result, ContextError> { + ClientReader::client_state(self, client_id).map_err(ContextError::ClientError) + } - fn host_height(&self) -> Result { - Ok(self.latest_height()) - } + fn decode_client_state( + &self, + client_state: Any, + ) -> Result, ContextError> { + ClientReader::decode_client_state(self, client_state).map_err(ContextError::ClientError) + } - fn pending_host_consensus_state(&self) -> Result, ContextError> { - ClientReader::pending_host_consensus_state(self).map_err(ContextError::ClientError) - } + fn consensus_state( + &self, + client_id: &ClientId, + height: &Height, + ) -> Result, ContextError> { + ClientReader::consensus_state(self, client_id, height) + .map_err(ContextError::ClientError) + } - fn host_consensus_state( - &self, - height: &Height, - ) -> Result, ContextError> { - ConnectionReader::host_consensus_state(self, height).map_err(ContextError::ConnectionError) - } + fn next_consensus_state( + &self, + client_id: &ClientId, + height: &Height, + ) -> Result>, ContextError> { + ClientReader::next_consensus_state(self, client_id, height) + .map_err(ContextError::ClientError) + } - fn client_counter(&self) -> Result { - ClientReader::client_counter(self).map_err(ContextError::ClientError) - } + fn prev_consensus_state( + &self, + client_id: &ClientId, + height: &Height, + ) -> Result>, ContextError> { + ClientReader::prev_consensus_state(self, client_id, height) + .map_err(ContextError::ClientError) + } - fn connection_end(&self, conn_id: &ConnectionId) -> Result { - ConnectionReader::connection_end(self, conn_id).map_err(ContextError::ConnectionError) - } + fn host_height(&self) -> Result { + Ok(self.latest_height()) + } - fn validate_self_client(&self, _counterparty_client_state: Any) -> Result<(), ConnectionError> { - Ok(()) - } + fn pending_host_consensus_state(&self) -> Result, ContextError> { + ClientReader::pending_host_consensus_state(self).map_err(ContextError::ClientError) + } - fn commitment_prefix(&self) -> CommitmentPrefix { - ConnectionReader::commitment_prefix(self) - } + fn host_consensus_state( + &self, + height: &Height, + ) -> Result, ContextError> { + ConnectionReader::host_consensus_state(self, height) + .map_err(ContextError::ConnectionError) + } - fn connection_counter(&self) -> Result { - ConnectionReader::connection_counter(self).map_err(ContextError::ConnectionError) - } + fn client_counter(&self) -> Result { + ClientReader::client_counter(self).map_err(ContextError::ClientError) + } - 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 connection_end(&self, conn_id: &ConnectionId) -> Result { + ConnectionReader::connection_end(self, conn_id).map_err(ContextError::ConnectionError) + } - fn connection_channels( - &self, - cid: &ConnectionId, - ) -> Result, ContextError> { - ChannelReader::connection_channels(self, cid).map_err(ContextError::ChannelError) - } + fn validate_self_client( + &self, + _counterparty_client_state: Any, + ) -> Result<(), ConnectionError> { + Ok(()) + } - fn get_next_sequence_send( - &self, - port_channel_id: &(PortId, ChannelId), - ) -> Result { - ChannelReader::get_next_sequence_send(self, &port_channel_id.0, &port_channel_id.1) - .map_err(ContextError::PacketError) - } + fn commitment_prefix(&self) -> CommitmentPrefix { + ConnectionReader::commitment_prefix(self) + } - fn get_next_sequence_recv( - &self, - port_channel_id: &(PortId, ChannelId), - ) -> Result { - ChannelReader::get_next_sequence_recv(self, &port_channel_id.0, &port_channel_id.1) - .map_err(ContextError::PacketError) - } + fn connection_counter(&self) -> Result { + ConnectionReader::connection_counter(self).map_err(ContextError::ConnectionError) + } - 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 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 get_packet_commitment( - &self, - key: &(PortId, ChannelId, Sequence), - ) -> Result { - ChannelReader::get_packet_commitment(self, &key.0, &key.1, &key.2) - .map_err(ContextError::PacketError) - } + fn connection_channels( + &self, + cid: &ConnectionId, + ) -> Result, ContextError> { + ChannelReader::connection_channels(self, cid).map_err(ContextError::ChannelError) + } - 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_next_sequence_send( + &self, + port_channel_id: &(PortId, ChannelId), + ) -> Result { + ChannelReader::get_next_sequence_send(self, &port_channel_id.0, &port_channel_id.1) + .map_err(ContextError::PacketError) + } - fn get_packet_acknowledgement( - &self, - key: &(PortId, ChannelId, Sequence), - ) -> Result { - ChannelReader::get_packet_acknowledgement(self, &key.0, &key.1, &key.2) - .map_err(ContextError::PacketError) - } + fn get_next_sequence_recv( + &self, + port_channel_id: &(PortId, ChannelId), + ) -> Result { + ChannelReader::get_next_sequence_recv(self, &port_channel_id.0, &port_channel_id.1) + .map_err(ContextError::PacketError) + } - fn hash(&self, value: &[u8]) -> Vec { - sha2::Sha256::digest(value).to_vec() - } + 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 client_update_time( - &self, - client_id: &ClientId, - height: &Height, - ) -> Result { - ChannelReader::client_update_time(self, client_id, height) - .map_err(ContextError::ChannelError) - } + fn get_packet_commitment( + &self, + key: &(PortId, ChannelId, Sequence), + ) -> Result { + ChannelReader::get_packet_commitment(self, &key.0, &key.1, &key.2) + .map_err(ContextError::PacketError) + } - fn client_update_height( - &self, - client_id: &ClientId, - height: &Height, - ) -> Result { - ChannelReader::client_update_height(self, client_id, height) - .map_err(ContextError::ChannelError) - } + 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 channel_counter(&self) -> Result { - ChannelReader::channel_counter(self).map_err(ContextError::ChannelError) - } + fn get_packet_acknowledgement( + &self, + key: &(PortId, ChannelId, Sequence), + ) -> Result { + ChannelReader::get_packet_acknowledgement(self, &key.0, &key.1, &key.2) + .map_err(ContextError::PacketError) + } - fn max_expected_time_per_block(&self) -> Duration { - ChannelReader::max_expected_time_per_block(self) + fn hash(&self, value: &[u8]) -> Vec { + sha2::Sha256::digest(value).to_vec() + } + + fn client_update_time( + &self, + client_id: &ClientId, + height: &Height, + ) -> Result { + ChannelReader::client_update_time(self, client_id, height) + .map_err(ContextError::ChannelError) + } + + fn client_update_height( + &self, + client_id: &ClientId, + height: &Height, + ) -> Result { + ChannelReader::client_update_height(self, client_id, height) + .map_err(ContextError::ChannelError) + } + + fn channel_counter(&self) -> Result { + ChannelReader::channel_counter(self).map_err(ContextError::ChannelError) + } + + fn max_expected_time_per_block(&self) -> Duration { + ChannelReader::max_expected_time_per_block(self) + } } } From 61936c68d9a1d61863b0c5973a9ccfe7c28d19e0 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 13:06:41 -0500 Subject: [PATCH 02/22] Add `lookup_module_by_port()` to new Router --- crates/ibc/src/core/context.rs | 7 ++++++- crates/ibc/src/mock/context.rs | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 5313d57da..36ba874f9 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -110,6 +110,9 @@ mod val_exec_ctx { /// Returns true if the `Router` has a `Module` registered against the specified `ModuleId` fn has_route(&self, module_id: &ModuleId) -> bool; + + /// Return the module_id associated with a given port_id + fn lookup_module_by_port(&self, port_id: &PortId) -> Option; } pub trait ValidationContext: Router { @@ -135,7 +138,9 @@ mod val_exec_ctx { } } .map_err(RouterError::ContextError), - MsgEnvelope::Channel(_message) => todo!(), + MsgEnvelope::Channel(_message) => { + todo!() + }, MsgEnvelope::Packet(_message) => todo!(), } } diff --git a/crates/ibc/src/mock/context.rs b/crates/ibc/src/mock/context.rs index 755246209..178791361 100644 --- a/crates/ibc/src/mock/context.rs +++ b/crates/ibc/src/mock/context.rs @@ -1536,6 +1536,10 @@ mod val_exec_ctx { fn has_route(&self, module_id: &ModuleId) -> bool { self.new_router.get(module_id).is_some() } + + fn lookup_module_by_port(&self, port_id: &PortId) -> Option { + ::lookup_module_by_port(self, port_id).ok() + } } impl ValidationContext for MockContext { From 28113c77bd0c965cc75a7e8c713c77dda297ff11 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 13:19:50 -0500 Subject: [PATCH 03/22] Add `lookup_module()` in new`Router` --- crates/ibc/src/core/context.rs | 23 ++++++++++++++++++++-- crates/ibc/src/core/ics04_channel/error.rs | 2 ++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 36ba874f9..af4a4d90a 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -61,6 +61,7 @@ pub use val_exec_ctx::*; #[cfg(feature = "val_exec_ctx")] mod val_exec_ctx { + use super::*; use core::time::Duration; use ibc_proto::google::protobuf::Any; @@ -77,6 +78,7 @@ mod val_exec_ctx { use crate::core::ics04_channel::commitment::{AcknowledgementCommitment, PacketCommitment}; use crate::core::ics04_channel::context::calculate_block_delay; use crate::core::ics04_channel::msgs::acknowledgement::Acknowledgement; + use crate::core::ics04_channel::msgs::ChannelMsg; use crate::core::ics04_channel::packet::{Receipt, Sequence}; use crate::core::ics04_channel::timeout::TimeoutHeight; use crate::core::ics23_commitment::commitment::CommitmentPrefix; @@ -100,7 +102,7 @@ mod val_exec_ctx { }; use crate::events::IbcEvent; use crate::timestamp::Timestamp; - use crate::{prelude::*, Height}; + use crate::Height; use super::ContextError; @@ -113,6 +115,23 @@ mod val_exec_ctx { /// Return the module_id associated with a given port_id fn lookup_module_by_port(&self, port_id: &PortId) -> Option; + + fn lookup_module(&self, msg: &ChannelMsg) -> Result { + let port_id = match msg { + ChannelMsg::OpenInit(msg) => &msg.port_id_on_a, + ChannelMsg::OpenTry(msg) => &msg.port_id_on_b, + ChannelMsg::OpenAck(msg) => &msg.port_id_on_a, + ChannelMsg::OpenConfirm(msg) => &msg.port_id_on_b, + ChannelMsg::CloseInit(msg) => &msg.port_id_on_a, + ChannelMsg::CloseConfirm(msg) => &msg.port_id_on_b, + }; + let module_id = + self.lookup_module_by_port(port_id) + .ok_or(ChannelError::PortNoSource { + port_id: port_id.clone(), + })?; + Ok(module_id) + } } pub trait ValidationContext: Router { @@ -140,7 +159,7 @@ mod val_exec_ctx { .map_err(RouterError::ContextError), MsgEnvelope::Channel(_message) => { todo!() - }, + } MsgEnvelope::Packet(_message) => todo!(), } } diff --git a/crates/ibc/src/core/ics04_channel/error.rs b/crates/ibc/src/core/ics04_channel/error.rs index 5dcd9681a..240da41a2 100644 --- a/crates/ibc/src/core/ics04_channel/error.rs +++ b/crates/ibc/src/core/ics04_channel/error.rs @@ -19,6 +19,8 @@ pub enum ChannelError { Connection(connection_error::ConnectionError), /// port error: `{0}` Port(port_error::PortError), + /// port `{port_id}` is unknown + PortNoSource { port_id: PortId }, /// channel state unknown: `{state}` UnknownState { state: i32 }, /// channel order type unknown: `{type_id}` From fbf0a39d73ed38ea7f23ecb6ded5528c8d83390b Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 13:44:46 -0500 Subject: [PATCH 04/22] simplify module_id --- crates/ibc/src/core/context.rs | 10 +++++++++- crates/ibc/src/core/ics26_routing/error.rs | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index af4a4d90a..56d181c82 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -157,7 +157,15 @@ mod val_exec_ctx { } } .map_err(RouterError::ContextError), - MsgEnvelope::Channel(_message) => { + MsgEnvelope::Channel(message) => { + let module_id = self + .lookup_module(&message) + .map_err(|err| ContextError::from(err))?; + if !self.has_route(&module_id) { + return Err(ChannelError::RouteNotFound) + .map_err(|err| ContextError::ChannelError(err))?; + } + todo!() } MsgEnvelope::Packet(_message) => todo!(), diff --git a/crates/ibc/src/core/ics26_routing/error.rs b/crates/ibc/src/core/ics26_routing/error.rs index 6122b8265..425523cc9 100644 --- a/crates/ibc/src/core/ics26_routing/error.rs +++ b/crates/ibc/src/core/ics26_routing/error.rs @@ -13,6 +13,12 @@ pub enum RouterError { MalformedMessageBytes(ibc_proto::protobuf::Error), } +impl From for RouterError { + fn from(error: ContextError) -> Self { + Self::ContextError(error) + } +} + #[cfg(feature = "std")] impl std::error::Error for RouterError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { From 2748209be1e8b3dbb6ee6b457ae11810e1a20a18 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 14:04:02 -0500 Subject: [PATCH 05/22] channel router WIP --- crates/ibc/src/core/context.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 56d181c82..06d6ae612 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -78,6 +78,7 @@ mod val_exec_ctx { use crate::core::ics04_channel::commitment::{AcknowledgementCommitment, PacketCommitment}; use crate::core::ics04_channel::context::calculate_block_delay; use crate::core::ics04_channel::msgs::acknowledgement::Acknowledgement; + use crate::core::ics04_channel::msgs::chan_open_try::MsgChannelOpenTry; use crate::core::ics04_channel::msgs::ChannelMsg; use crate::core::ics04_channel::packet::{Receipt, Sequence}; use crate::core::ics04_channel::timeout::TimeoutHeight; @@ -158,15 +159,25 @@ mod val_exec_ctx { } .map_err(RouterError::ContextError), MsgEnvelope::Channel(message) => { - let module_id = self - .lookup_module(&message) - .map_err(|err| ContextError::from(err))?; + let module_id = self.lookup_module(&message).map_err(ContextError::from)?; if !self.has_route(&module_id) { return Err(ChannelError::RouteNotFound) - .map_err(|err| ContextError::ChannelError(err))?; + .map_err(ContextError::ChannelError) + .map_err(RouterError::ContextError); } - todo!() + match message { + ChannelMsg::OpenInit(_) => todo!(), + ChannelMsg::OpenTry(message) => { + validate_chan_open_try(self, module_id, message) + } + ChannelMsg::OpenAck(_) => todo!(), + ChannelMsg::OpenConfirm(_) => todo!(), + ChannelMsg::CloseInit(_) => todo!(), + ChannelMsg::CloseConfirm(_) => todo!(), + } + .map_err(ContextError::ChannelError) + .map_err(RouterError::ContextError) } MsgEnvelope::Packet(_message) => todo!(), } @@ -363,6 +374,17 @@ mod val_exec_ctx { } } + fn validate_chan_open_try( + _ctx: &ValCtx, + _module_id: ModuleId, + _message: MsgChannelOpenTry, + ) -> Result<(), ChannelError> + where + ValCtx: ValidationContext, + { + todo!() + } + pub trait ExecutionContext: ValidationContext { /// Execution entrypoint fn execute(&mut self, message: MsgEnvelope) -> Result<(), RouterError> From c6da207d649092cc5c7c86419057ab53f32772bf Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 14:28:06 -0500 Subject: [PATCH 06/22] validate step --- crates/ibc/src/core/context.rs | 10 ++++++---- .../core/ics04_channel/handler/chan_open_try.rs | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 06d6ae612..83a93cd69 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -77,6 +77,7 @@ mod val_exec_ctx { use crate::core::ics04_channel::channel::ChannelEnd; use crate::core::ics04_channel::commitment::{AcknowledgementCommitment, PacketCommitment}; use crate::core::ics04_channel::context::calculate_block_delay; + use crate::core::ics04_channel::handler::chan_open_try; use crate::core::ics04_channel::msgs::acknowledgement::Acknowledgement; use crate::core::ics04_channel::msgs::chan_open_try::MsgChannelOpenTry; use crate::core::ics04_channel::msgs::ChannelMsg; @@ -176,7 +177,6 @@ mod val_exec_ctx { ChannelMsg::CloseInit(_) => todo!(), ChannelMsg::CloseConfirm(_) => todo!(), } - .map_err(ContextError::ChannelError) .map_err(RouterError::ContextError) } MsgEnvelope::Packet(_message) => todo!(), @@ -375,13 +375,15 @@ mod val_exec_ctx { } fn validate_chan_open_try( - _ctx: &ValCtx, + ctx: &ValCtx, _module_id: ModuleId, - _message: MsgChannelOpenTry, - ) -> Result<(), ChannelError> + message: MsgChannelOpenTry, + ) -> Result<(), ContextError> where ValCtx: ValidationContext, { + let _channel_id = chan_open_try::validate(ctx, &message)?; + todo!() } 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 84990a053..154bccf3d 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 @@ -11,6 +11,20 @@ use crate::core::ics24_host::identifier::ChannelId; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; +#[cfg(feature = "val_exec_ctx")] +pub(crate) use val_exec_ctx::*; +#[cfg(feature = "val_exec_ctx")] +pub(crate) mod val_exec_ctx { + use super::*; + use crate::core::{ContextError, ValidationContext}; + + pub fn validate(_ctx_b: &Ctx, _msg: &MsgChannelOpenTry) -> Result + where + Ctx: ValidationContext, + { + todo!() + } +} /// Per our convention, this message is processed on chain B. pub(crate) fn process( ctx_b: &Ctx, From 4eea83f68cf04f59d83f8b6f5897a5f07c829266 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 14:46:55 -0500 Subject: [PATCH 07/22] add immutable route to router --- crates/ibc/src/core/context.rs | 3 +++ crates/ibc/src/mock/context.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 83a93cd69..1bb480c52 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -109,6 +109,9 @@ mod val_exec_ctx { use super::ContextError; pub trait Router { + /// Returns a reference to a `Module` registered against the specified `ModuleId` + fn get_route(&self, module_id: &ModuleId) -> Option<&dyn Module>; + /// Returns a mutable reference to a `Module` registered against the specified `ModuleId` fn get_route_mut(&mut self, module_id: &ModuleId) -> Option<&mut dyn Module>; diff --git a/crates/ibc/src/mock/context.rs b/crates/ibc/src/mock/context.rs index 178791361..f6abd29ee 100644 --- a/crates/ibc/src/mock/context.rs +++ b/crates/ibc/src/mock/context.rs @@ -1529,6 +1529,9 @@ mod val_exec_ctx { use crate::core::ValidationContext; impl NewRouter for MockContext { + fn get_route(&self, module_id: &ModuleId) -> Option<&dyn Module> { + self.new_router.get(module_id).map(Arc::as_ref) + } fn get_route_mut(&mut self, module_id: &ModuleId) -> Option<&mut dyn Module> { self.new_router.get_mut(module_id).and_then(Arc::get_mut) } From 8c393b3db99a1b6f3c1adea44606e9bdc1ac465e Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 14:51:14 -0500 Subject: [PATCH 08/22] add on_chan_open_try_validate() cb for ics20 --- .../ibc/src/applications/transfer/context.rs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/ibc/src/applications/transfer/context.rs b/crates/ibc/src/applications/transfer/context.rs index 08c25218d..0288ea613 100644 --- a/crates/ibc/src/applications/transfer/context.rs +++ b/crates/ibc/src/applications/transfer/context.rs @@ -188,6 +188,33 @@ pub fn on_chan_open_init( Ok((ModuleExtras::empty(), Version::ics20())) } +#[cfg(feature = "val_exec_ctx")] +#[allow(clippy::too_many_arguments)] +pub fn on_chan_open_try_validate( + _ctx: &impl TokenTransferContext, + order: Order, + _connection_hops: &[ConnectionId], + _port_id: &PortId, + _channel_id: &ChannelId, + _counterparty: &Counterparty, + counterparty_version: &Version, +) -> Result<(ModuleExtras, Version), TokenTransferError> { + if order != Order::Unordered { + return Err(TokenTransferError::ChannelNotUnordered { + expect_order: Order::Unordered, + got_order: order, + }); + } + if counterparty_version != &Version::ics20() { + return Err(TokenTransferError::InvalidCounterpartyVersion { + expect_version: Version::ics20(), + got_version: counterparty_version.clone(), + }); + } + + Ok((ModuleExtras::empty(), Version::ics20())) +} + #[allow(clippy::too_many_arguments)] pub fn on_chan_open_try( _ctx: &mut impl TokenTransferContext, From f4cf7e1a4bbccb36de97a1e2bb54621c7524a1d9 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 14:55:18 -0500 Subject: [PATCH 09/22] add on_chan_open_try_validate() to Module --- crates/ibc/src/core/ics26_routing/context.rs | 12 +++++++++ crates/ibc/src/mock/context.rs | 26 ++++++++++++++++++++ crates/ibc/src/test_utils.rs | 12 +++++++++ 3 files changed, 50 insertions(+) diff --git a/crates/ibc/src/core/ics26_routing/context.rs b/crates/ibc/src/core/ics26_routing/context.rs index 57978dbb5..a9c8f4782 100644 --- a/crates/ibc/src/core/ics26_routing/context.rs +++ b/crates/ibc/src/core/ics26_routing/context.rs @@ -122,6 +122,18 @@ pub trait Module: Send + Sync + AsAnyMut + Debug { version: &Version, ) -> Result<(ModuleExtras, Version), ChannelError>; + #[cfg(feature = "val_exec_ctx")] + #[allow(clippy::too_many_arguments)] + fn on_chan_open_try_validate( + &self, + order: Order, + connection_hops: &[ConnectionId], + port_id: &PortId, + channel_id: &ChannelId, + counterparty: &Counterparty, + counterparty_version: &Version, + ) -> Result<(ModuleExtras, Version), ChannelError>; + #[allow(clippy::too_many_arguments)] fn on_chan_open_try( &mut self, diff --git a/crates/ibc/src/mock/context.rs b/crates/ibc/src/mock/context.rs index f6abd29ee..fe802c37b 100644 --- a/crates/ibc/src/mock/context.rs +++ b/crates/ibc/src/mock/context.rs @@ -1913,6 +1913,19 @@ mod tests { Ok((ModuleExtras::empty(), version.clone())) } + #[cfg(feature = "val_exec_ctx")] + fn on_chan_open_try_validate( + &self, + _order: Order, + _connection_hops: &[ConnectionId], + _port_id: &PortId, + _channel_id: &ChannelId, + _counterparty: &Counterparty, + counterparty_version: &Version, + ) -> Result<(ModuleExtras, Version), ChannelError> { + Ok((ModuleExtras::empty(), counterparty_version.clone())) + } + fn on_chan_open_try( &mut self, _order: Order, @@ -1958,6 +1971,19 @@ mod tests { Ok((ModuleExtras::empty(), version.clone())) } + #[cfg(feature = "val_exec_ctx")] + fn on_chan_open_try_validate( + &self, + _order: Order, + _connection_hops: &[ConnectionId], + _port_id: &PortId, + _channel_id: &ChannelId, + _counterparty: &Counterparty, + counterparty_version: &Version, + ) -> Result<(ModuleExtras, Version), ChannelError> { + Ok((ModuleExtras::empty(), counterparty_version.clone())) + } + fn on_chan_open_try( &mut self, _order: Order, diff --git a/crates/ibc/src/test_utils.rs b/crates/ibc/src/test_utils.rs index 845da1bdc..e8067ce9d 100644 --- a/crates/ibc/src/test_utils.rs +++ b/crates/ibc/src/test_utils.rs @@ -95,6 +95,18 @@ impl Module for DummyTransferModule { version.clone(), )) } + #[cfg(feature = "val_exec_ctx")] + fn on_chan_open_try_validate( + &self, + _order: Order, + _connection_hops: &[ConnectionId], + _port_id: &PortId, + _channel_id: &ChannelId, + _counterparty: &Counterparty, + counterparty_version: &Version, + ) -> Result<(ModuleExtras, Version), ChannelError> { + Ok((ModuleExtras::empty(), counterparty_version.clone())) + } fn on_chan_open_try( &mut self, From b98540a1d13b965a19c1f05cfbc43a7b11965360 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 15:19:49 -0500 Subject: [PATCH 10/22] finish validate for chan_open_try --- crates/ibc/src/core/context.rs | 24 ++++-- .../ics04_channel/handler/chan_open_try.rs | 83 ++++++++++++++++++- 2 files changed, 99 insertions(+), 8 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 1bb480c52..d2557fbc9 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -74,7 +74,7 @@ mod val_exec_ctx { use crate::core::ics03_connection::version::{ get_compatible_versions, pick_version, Version as ConnectionVersion, }; - use crate::core::ics04_channel::channel::ChannelEnd; + use crate::core::ics04_channel::channel::{ChannelEnd, Counterparty}; use crate::core::ics04_channel::commitment::{AcknowledgementCommitment, PacketCommitment}; use crate::core::ics04_channel::context::calculate_block_delay; use crate::core::ics04_channel::handler::chan_open_try; @@ -379,15 +379,27 @@ mod val_exec_ctx { fn validate_chan_open_try( ctx: &ValCtx, - _module_id: ModuleId, - message: MsgChannelOpenTry, + module_id: ModuleId, + msg: MsgChannelOpenTry, ) -> Result<(), ContextError> where ValCtx: ValidationContext, { - let _channel_id = chan_open_try::validate(ctx, &message)?; - - todo!() + let channel_id = chan_open_try::validate(ctx, &msg)?; + + let module = ctx + .get_route(&module_id) + .ok_or(ChannelError::RouteNotFound)?; + let _ = module.on_chan_open_try_validate( + msg.ordering, + &msg.connection_hops_on_b, + &msg.port_id_on_b, + &channel_id, + &Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())), + &msg.version_supported_on_a, + )?; + + Ok(()) } pub trait ExecutionContext: ValidationContext { 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 154bccf3d..c5173340b 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 @@ -18,11 +18,90 @@ pub(crate) mod val_exec_ctx { use super::*; use crate::core::{ContextError, ValidationContext}; - pub fn validate(_ctx_b: &Ctx, _msg: &MsgChannelOpenTry) -> Result + pub fn validate(ctx_b: &Ctx, msg: &MsgChannelOpenTry) -> Result where Ctx: ValidationContext, { - todo!() + // An IBC connection running on the local (host) chain should exist. + if msg.connection_hops_on_b.len() != 1 { + return Err(ChannelError::InvalidConnectionHopsLength { + expected: 1, + actual: msg.connection_hops_on_b.len(), + }) + .map_err(ContextError::ChannelError); + } + + let conn_end_on_b = ctx_b.connection_end(&msg.connection_hops_on_b[0])?; + if !conn_end_on_b.state_matches(&ConnectionState::Open) { + return Err(ChannelError::ConnectionNotOpen { + connection_id: msg.connection_hops_on_b[0].clone(), + }) + .map_err(ContextError::ChannelError); + } + + let conn_version = match conn_end_on_b.versions() { + [version] => version, + _ => { + return Err(ChannelError::InvalidVersionLengthConnection) + .map_err(ContextError::ChannelError) + } + }; + + let channel_feature = msg.ordering.to_string(); + if !conn_version.is_supported_feature(channel_feature) { + return Err(ChannelError::ChannelFeatureNotSuportedByConnection) + .map_err(ContextError::ChannelError); + } + + // Verify proofs + { + 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 prefix_on_a = conn_end_on_b.counterparty().prefix(); + let port_id_on_a = &&msg.port_id_on_a; + 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 { + connection_id: msg.connection_hops_on_b[0].clone(), + }, + )?; + + // The client must not be frozen. + if client_state_of_a_on_b.is_frozen() { + return Err(ChannelError::FrozenClient { + client_id: client_id_on_b.clone(), + }) + .map_err(ContextError::ChannelError); + } + + let expected_chan_end_on_a = ChannelEnd::new( + State::Init, + msg.ordering, + Counterparty::new(msg.port_id_on_b.clone(), None), + vec![conn_id_on_a.clone()], + msg.version_supported_on_a.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_a_on_b + .verify_channel_state( + msg.proof_height_on_a, + prefix_on_a, + &msg.proof_chan_end_on_a, + consensus_state_of_a_on_b.root(), + port_id_on_a, + &chan_id_on_a, + &expected_chan_end_on_a, + ) + .map_err(ChannelError::VerifyChannelFailed)?; + } + + let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?); + + Ok(chan_id_on_b) } } /// Per our convention, this message is processed on chain B. From 02d7e1399f4da0843f5bb9938bb361bc9de8a026 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 17:17:09 -0500 Subject: [PATCH 11/22] chan_open_try_execute --- crates/ibc/src/core/context.rs | 84 +++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 27 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index d2557fbc9..b3963c890 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -173,7 +173,7 @@ mod val_exec_ctx { match message { ChannelMsg::OpenInit(_) => todo!(), ChannelMsg::OpenTry(message) => { - validate_chan_open_try(self, module_id, message) + chan_open_try_validate(self, module_id, message) } ChannelMsg::OpenAck(_) => todo!(), ChannelMsg::OpenConfirm(_) => todo!(), @@ -377,31 +377,6 @@ mod val_exec_ctx { } } - fn validate_chan_open_try( - ctx: &ValCtx, - module_id: ModuleId, - msg: MsgChannelOpenTry, - ) -> Result<(), ContextError> - where - ValCtx: ValidationContext, - { - let channel_id = chan_open_try::validate(ctx, &msg)?; - - let module = ctx - .get_route(&module_id) - .ok_or(ChannelError::RouteNotFound)?; - let _ = module.on_chan_open_try_validate( - msg.ordering, - &msg.connection_hops_on_b, - &msg.port_id_on_b, - &channel_id, - &Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())), - &msg.version_supported_on_a, - )?; - - Ok(()) - } - pub trait ExecutionContext: ValidationContext { /// Execution entrypoint fn execute(&mut self, message: MsgEnvelope) -> Result<(), RouterError> @@ -425,7 +400,26 @@ mod val_exec_ctx { } } .map_err(RouterError::ContextError), - MsgEnvelope::Channel(_message) => todo!(), + MsgEnvelope::Channel(message) => { + let module_id = self.lookup_module(&message).map_err(ContextError::from)?; + if !self.has_route(&module_id) { + return Err(ChannelError::RouteNotFound) + .map_err(ContextError::ChannelError) + .map_err(RouterError::ContextError); + } + + match message { + ChannelMsg::OpenInit(_) => todo!(), + ChannelMsg::OpenTry(message) => { + chan_open_try_execute(self, module_id, message) + } + ChannelMsg::OpenAck(_) => todo!(), + ChannelMsg::OpenConfirm(_) => todo!(), + ChannelMsg::CloseInit(_) => todo!(), + ChannelMsg::CloseConfirm(_) => todo!(), + } + .map_err(RouterError::ContextError) + } MsgEnvelope::Packet(_message) => todo!(), } } @@ -562,4 +556,40 @@ mod val_exec_ctx { /// Logging facility fn log_message(&mut self, message: String); } + + fn chan_open_try_validate( + ctx: &ValCtx, + module_id: ModuleId, + msg: MsgChannelOpenTry, + ) -> Result<(), ContextError> + where + ValCtx: ValidationContext, + { + let channel_id = chan_open_try::validate(ctx, &msg)?; + + let module = ctx + .get_route(&module_id) + .ok_or(ChannelError::RouteNotFound)?; + let _ = module.on_chan_open_try_validate( + msg.ordering, + &msg.connection_hops_on_b, + &msg.port_id_on_b, + &channel_id, + &Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())), + &msg.version_supported_on_a, + )?; + + Ok(()) + } + + fn chan_open_try_execute( + _ctx: &mut ExecCtx, + _module_id: ModuleId, + _msg: MsgChannelOpenTry, + ) -> Result<(), ContextError> + where + ExecCtx: ExecutionContext, + { + todo!() + } } From a31b58a5d11f823cc2baf19eb40f88253da38f27 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 17:24:42 -0500 Subject: [PATCH 12/22] execute core handler --- crates/ibc/src/core/context.rs | 4 +++- .../src/core/ics04_channel/handler/chan_open_try.rs | 10 +++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index b3963c890..499d44091 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -583,13 +583,15 @@ mod val_exec_ctx { } fn chan_open_try_execute( - _ctx: &mut ExecCtx, + ctx: &mut ExecCtx, _module_id: ModuleId, _msg: MsgChannelOpenTry, ) -> Result<(), ContextError> where ExecCtx: ExecutionContext, { + let _channel_id = chan_open_try::execute(ctx)?; + todo!() } } 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 c5173340b..807a78cab 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 @@ -16,7 +16,7 @@ pub(crate) use val_exec_ctx::*; #[cfg(feature = "val_exec_ctx")] pub(crate) mod val_exec_ctx { use super::*; - use crate::core::{ContextError, ValidationContext}; + use crate::core::{ContextError, ExecutionContext, ValidationContext}; pub fn validate(ctx_b: &Ctx, msg: &MsgChannelOpenTry) -> Result where @@ -103,6 +103,14 @@ pub(crate) mod val_exec_ctx { Ok(chan_id_on_b) } + + pub fn execute(ctx_b: &Ctx) -> Result + where + Ctx: ExecutionContext, + { + let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?); + Ok(chan_id_on_b) + } } /// Per our convention, this message is processed on chain B. pub(crate) fn process( From b887804c0b6e92532de265181ab35fa9e509ccb9 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 17:30:16 -0500 Subject: [PATCH 13/22] Add on_chan_open_try_execute --- crates/ibc/src/core/ics26_routing/context.rs | 12 +++++++++ crates/ibc/src/mock/context.rs | 26 ++++++++++++++++++++ crates/ibc/src/test_utils.rs | 13 ++++++++++ 3 files changed, 51 insertions(+) diff --git a/crates/ibc/src/core/ics26_routing/context.rs b/crates/ibc/src/core/ics26_routing/context.rs index a9c8f4782..614953de3 100644 --- a/crates/ibc/src/core/ics26_routing/context.rs +++ b/crates/ibc/src/core/ics26_routing/context.rs @@ -134,6 +134,18 @@ pub trait Module: Send + Sync + AsAnyMut + Debug { counterparty_version: &Version, ) -> Result<(ModuleExtras, Version), ChannelError>; + #[cfg(feature = "val_exec_ctx")] + #[allow(clippy::too_many_arguments)] + fn on_chan_open_try_execute( + &mut self, + order: Order, + connection_hops: &[ConnectionId], + port_id: &PortId, + channel_id: &ChannelId, + counterparty: &Counterparty, + counterparty_version: &Version, + ) -> Result<(ModuleExtras, Version), ChannelError>; + #[allow(clippy::too_many_arguments)] fn on_chan_open_try( &mut self, diff --git a/crates/ibc/src/mock/context.rs b/crates/ibc/src/mock/context.rs index fe802c37b..4a7d7e396 100644 --- a/crates/ibc/src/mock/context.rs +++ b/crates/ibc/src/mock/context.rs @@ -1926,6 +1926,19 @@ mod tests { Ok((ModuleExtras::empty(), counterparty_version.clone())) } + #[cfg(feature = "val_exec_ctx")] + fn on_chan_open_try_execute( + &mut self, + _order: Order, + _connection_hops: &[ConnectionId], + _port_id: &PortId, + _channel_id: &ChannelId, + _counterparty: &Counterparty, + counterparty_version: &Version, + ) -> Result<(ModuleExtras, Version), ChannelError> { + Ok((ModuleExtras::empty(), counterparty_version.clone())) + } + fn on_chan_open_try( &mut self, _order: Order, @@ -1984,6 +1997,19 @@ mod tests { Ok((ModuleExtras::empty(), counterparty_version.clone())) } + #[cfg(feature = "val_exec_ctx")] + fn on_chan_open_try_execute( + &mut self, + _order: Order, + _connection_hops: &[ConnectionId], + _port_id: &PortId, + _channel_id: &ChannelId, + _counterparty: &Counterparty, + counterparty_version: &Version, + ) -> Result<(ModuleExtras, Version), ChannelError> { + Ok((ModuleExtras::empty(), counterparty_version.clone())) + } + fn on_chan_open_try( &mut self, _order: Order, diff --git a/crates/ibc/src/test_utils.rs b/crates/ibc/src/test_utils.rs index e8067ce9d..556422c3a 100644 --- a/crates/ibc/src/test_utils.rs +++ b/crates/ibc/src/test_utils.rs @@ -108,6 +108,19 @@ impl Module for DummyTransferModule { Ok((ModuleExtras::empty(), counterparty_version.clone())) } + #[cfg(feature = "val_exec_ctx")] + fn on_chan_open_try_execute( + &mut self, + _order: Order, + _connection_hops: &[ConnectionId], + _port_id: &PortId, + _channel_id: &ChannelId, + _counterparty: &Counterparty, + counterparty_version: &Version, + ) -> Result<(ModuleExtras, Version), ChannelError> { + Ok((ModuleExtras::empty(), counterparty_version.clone())) + } + fn on_chan_open_try( &mut self, _order: Order, From 0586b61e9a1c57a3d218344a6d84a06f02d574f7 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 17:31:35 -0500 Subject: [PATCH 14/22] ics-20: execute --- crates/ibc/src/applications/transfer/context.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/ibc/src/applications/transfer/context.rs b/crates/ibc/src/applications/transfer/context.rs index 0288ea613..2bb736c8a 100644 --- a/crates/ibc/src/applications/transfer/context.rs +++ b/crates/ibc/src/applications/transfer/context.rs @@ -215,6 +215,20 @@ pub fn on_chan_open_try_validate( Ok((ModuleExtras::empty(), Version::ics20())) } +#[cfg(feature = "val_exec_ctx")] +#[allow(clippy::too_many_arguments)] +pub fn on_chan_open_try_execute( + _ctx: &mut impl TokenTransferContext, + _order: Order, + _connection_hops: &[ConnectionId], + _port_id: &PortId, + _channel_id: &ChannelId, + _counterparty: &Counterparty, + _counterparty_version: &Version, +) -> Result<(ModuleExtras, Version), TokenTransferError> { + Ok((ModuleExtras::empty(), Version::ics20())) +} + #[allow(clippy::too_many_arguments)] pub fn on_chan_open_try( _ctx: &mut impl TokenTransferContext, From b6eb26170221d12d4345de3585b74eb136b6cab1 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 17:51:08 -0500 Subject: [PATCH 15/22] execute: emit logs and events --- crates/ibc/src/core/context.rs | 44 ++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 499d44091..3ab40cf9a 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -77,6 +77,7 @@ mod val_exec_ctx { use crate::core::ics04_channel::channel::{ChannelEnd, Counterparty}; use crate::core::ics04_channel::commitment::{AcknowledgementCommitment, PacketCommitment}; use crate::core::ics04_channel::context::calculate_block_delay; + use crate::core::ics04_channel::events::OpenTry; use crate::core::ics04_channel::handler::chan_open_try; use crate::core::ics04_channel::msgs::acknowledgement::Acknowledgement; use crate::core::ics04_channel::msgs::chan_open_try::MsgChannelOpenTry; @@ -584,14 +585,49 @@ mod val_exec_ctx { fn chan_open_try_execute( ctx: &mut ExecCtx, - _module_id: ModuleId, - _msg: MsgChannelOpenTry, + module_id: ModuleId, + msg: MsgChannelOpenTry, ) -> Result<(), ContextError> where ExecCtx: ExecutionContext, { - let _channel_id = chan_open_try::execute(ctx)?; + let channel_id = chan_open_try::execute(ctx)?; + let module = ctx + .get_route_mut(&module_id) + .ok_or(ChannelError::RouteNotFound)?; + + let (extras, version) = module.on_chan_open_try_execute( + msg.ordering, + &msg.connection_hops_on_b, + &msg.port_id_on_b, + &channel_id, + &Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())), + &msg.version_supported_on_a, + )?; + + // emit events and logs + { + let core_event = IbcEvent::OpenTryChannel(OpenTry::new( + msg.port_id_on_b.clone(), + channel_id, + msg.port_id_on_a, + msg.chan_id_on_a, + msg.connection_hops_on_b[0].clone(), + version.clone(), + )); + ctx.emit_ibc_event(core_event); + + for module_event in extras.events { + ctx.emit_ibc_event(IbcEvent::AppModule(module_event)); + } - todo!() + for log_message in extras.log { + ctx.log_message(log_message); + } + } + + // TODO: mutate chain state + + Ok(()) } } From 380056be894a6edf8b0d9fc23175d92641407a16 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 17:54:04 -0500 Subject: [PATCH 16/22] no ModuleExtras for validate callback --- crates/ibc/src/applications/transfer/context.rs | 4 ++-- crates/ibc/src/core/context.rs | 2 +- crates/ibc/src/core/ics26_routing/context.rs | 2 +- crates/ibc/src/mock/context.rs | 8 ++++---- crates/ibc/src/test_utils.rs | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/ibc/src/applications/transfer/context.rs b/crates/ibc/src/applications/transfer/context.rs index 2bb736c8a..5c0018a66 100644 --- a/crates/ibc/src/applications/transfer/context.rs +++ b/crates/ibc/src/applications/transfer/context.rs @@ -198,7 +198,7 @@ pub fn on_chan_open_try_validate( _channel_id: &ChannelId, _counterparty: &Counterparty, counterparty_version: &Version, -) -> Result<(ModuleExtras, Version), TokenTransferError> { +) -> Result { if order != Order::Unordered { return Err(TokenTransferError::ChannelNotUnordered { expect_order: Order::Unordered, @@ -212,7 +212,7 @@ pub fn on_chan_open_try_validate( }); } - Ok((ModuleExtras::empty(), Version::ics20())) + Ok(Version::ics20()) } #[cfg(feature = "val_exec_ctx")] diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 3ab40cf9a..071435f21 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -613,7 +613,7 @@ mod val_exec_ctx { msg.port_id_on_a, msg.chan_id_on_a, msg.connection_hops_on_b[0].clone(), - version.clone(), + version, )); ctx.emit_ibc_event(core_event); diff --git a/crates/ibc/src/core/ics26_routing/context.rs b/crates/ibc/src/core/ics26_routing/context.rs index 614953de3..b8cf6bb28 100644 --- a/crates/ibc/src/core/ics26_routing/context.rs +++ b/crates/ibc/src/core/ics26_routing/context.rs @@ -132,7 +132,7 @@ pub trait Module: Send + Sync + AsAnyMut + Debug { channel_id: &ChannelId, counterparty: &Counterparty, counterparty_version: &Version, - ) -> Result<(ModuleExtras, Version), ChannelError>; + ) -> Result; #[cfg(feature = "val_exec_ctx")] #[allow(clippy::too_many_arguments)] diff --git a/crates/ibc/src/mock/context.rs b/crates/ibc/src/mock/context.rs index 4a7d7e396..e0372b5b1 100644 --- a/crates/ibc/src/mock/context.rs +++ b/crates/ibc/src/mock/context.rs @@ -1922,8 +1922,8 @@ mod tests { _channel_id: &ChannelId, _counterparty: &Counterparty, counterparty_version: &Version, - ) -> Result<(ModuleExtras, Version), ChannelError> { - Ok((ModuleExtras::empty(), counterparty_version.clone())) + ) -> Result { + Ok(counterparty_version.clone()) } #[cfg(feature = "val_exec_ctx")] @@ -1993,8 +1993,8 @@ mod tests { _channel_id: &ChannelId, _counterparty: &Counterparty, counterparty_version: &Version, - ) -> Result<(ModuleExtras, Version), ChannelError> { - Ok((ModuleExtras::empty(), counterparty_version.clone())) + ) -> Result { + Ok(counterparty_version.clone()) } #[cfg(feature = "val_exec_ctx")] diff --git a/crates/ibc/src/test_utils.rs b/crates/ibc/src/test_utils.rs index 556422c3a..ea89f536d 100644 --- a/crates/ibc/src/test_utils.rs +++ b/crates/ibc/src/test_utils.rs @@ -104,8 +104,8 @@ impl Module for DummyTransferModule { _channel_id: &ChannelId, _counterparty: &Counterparty, counterparty_version: &Version, - ) -> Result<(ModuleExtras, Version), ChannelError> { - Ok((ModuleExtras::empty(), counterparty_version.clone())) + ) -> Result { + Ok(counterparty_version.clone()) } #[cfg(feature = "val_exec_ctx")] From 1a92bcf7baeaf66d0ec2a9e26324689de28970b2 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Wed, 11 Jan 2023 18:07:07 -0500 Subject: [PATCH 17/22] chan_open_try execute --- crates/ibc/src/core/context.rs | 49 ++++++++++++++----- .../ics04_channel/handler/chan_open_try.rs | 7 ++- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 071435f21..cc1755a30 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -74,7 +74,7 @@ mod val_exec_ctx { use crate::core::ics03_connection::version::{ get_compatible_versions, pick_version, Version as ConnectionVersion, }; - use crate::core::ics04_channel::channel::{ChannelEnd, Counterparty}; + use crate::core::ics04_channel::channel::{ChannelEnd, Counterparty, State}; use crate::core::ics04_channel::commitment::{AcknowledgementCommitment, PacketCommitment}; use crate::core::ics04_channel::context::calculate_block_delay; use crate::core::ics04_channel::events::OpenTry; @@ -584,15 +584,15 @@ mod val_exec_ctx { } fn chan_open_try_execute( - ctx: &mut ExecCtx, + ctx_b: &mut ExecCtx, module_id: ModuleId, msg: MsgChannelOpenTry, ) -> Result<(), ContextError> where ExecCtx: ExecutionContext, { - let channel_id = chan_open_try::execute(ctx)?; - let module = ctx + let channel_id = chan_open_try::execute(ctx_b)?; + let module = ctx_b .get_route_mut(&module_id) .ok_or(ChannelError::RouteNotFound)?; @@ -605,28 +605,51 @@ mod val_exec_ctx { &msg.version_supported_on_a, )?; + let conn_id_on_b = msg.connection_hops_on_b[0].clone(); + let port_channel_id_on_b = (msg.port_id_on_b.clone(), channel_id.clone()); + // emit events and logs { let core_event = IbcEvent::OpenTryChannel(OpenTry::new( msg.port_id_on_b.clone(), - channel_id, - msg.port_id_on_a, - msg.chan_id_on_a, - msg.connection_hops_on_b[0].clone(), - version, + channel_id.clone(), + msg.port_id_on_a.clone(), + msg.chan_id_on_a.clone(), + conn_id_on_b.clone(), + version.clone(), )); - ctx.emit_ibc_event(core_event); + ctx_b.emit_ibc_event(core_event); for module_event in extras.events { - ctx.emit_ibc_event(IbcEvent::AppModule(module_event)); + ctx_b.emit_ibc_event(IbcEvent::AppModule(module_event)); } for log_message in extras.log { - ctx.log_message(log_message); + ctx_b.log_message(log_message); } } - // TODO: mutate chain state + { + let channel_end = ChannelEnd::new( + State::TryOpen, + msg.ordering, + Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())), + msg.connection_hops_on_b.clone(), + version, + ); + + ctx_b.store_channel(port_channel_id_on_b.clone(), channel_end)?; + + ctx_b.increase_channel_counter(); + + // Associate also the channel end to its connection. + ctx_b.store_connection_channels(conn_id_on_b, 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())?; + } Ok(()) } 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 807a78cab..6cbb97ba0 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 @@ -104,11 +104,16 @@ pub(crate) mod val_exec_ctx { Ok(chan_id_on_b) } - pub fn execute(ctx_b: &Ctx) -> Result + pub fn execute(ctx_b: &mut Ctx) -> Result where Ctx: ExecutionContext, { let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?); + + ctx_b.log_message(format!( + "success: channel open try with channel identifier: {chan_id_on_b}" + )); + Ok(chan_id_on_b) } } From c7f10adfa0024037669515b85f5936c4997fb425 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Thu, 12 Jan 2023 15:53:31 -0500 Subject: [PATCH 18/22] update error --- crates/ibc/src/core/context.rs | 11 ++++++----- crates/ibc/src/core/ics04_channel/error.rs | 2 -- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index cc1755a30..d6399dd4f 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -84,6 +84,7 @@ mod val_exec_ctx { use crate::core::ics04_channel::msgs::ChannelMsg; use crate::core::ics04_channel::packet::{Receipt, Sequence}; use crate::core::ics04_channel::timeout::TimeoutHeight; + 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::{ @@ -131,11 +132,11 @@ mod val_exec_ctx { ChannelMsg::CloseInit(msg) => &msg.port_id_on_a, ChannelMsg::CloseConfirm(msg) => &msg.port_id_on_b, }; - let module_id = - self.lookup_module_by_port(port_id) - .ok_or(ChannelError::PortNoSource { - port_id: port_id.clone(), - })?; + let module_id = self + .lookup_module_by_port(port_id) + .ok_or(ChannelError::Port(UnknownPort { + port_id: port_id.clone(), + }))?; Ok(module_id) } } diff --git a/crates/ibc/src/core/ics04_channel/error.rs b/crates/ibc/src/core/ics04_channel/error.rs index 240da41a2..5dcd9681a 100644 --- a/crates/ibc/src/core/ics04_channel/error.rs +++ b/crates/ibc/src/core/ics04_channel/error.rs @@ -19,8 +19,6 @@ pub enum ChannelError { Connection(connection_error::ConnectionError), /// port error: `{0}` Port(port_error::PortError), - /// port `{port_id}` is unknown - PortNoSource { port_id: PortId }, /// channel state unknown: `{state}` UnknownState { state: i32 }, /// channel order type unknown: `{type_id}` From 6047f02d2cce8b9661f0bcb85fd0dbc68928126e Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Thu, 12 Jan 2023 15:59:31 -0500 Subject: [PATCH 19/22] take `channel_id` out of validate() --- crates/ibc/src/core/context.rs | 3 ++- crates/ibc/src/core/ics04_channel/handler/chan_open_try.rs | 6 ++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index d6399dd4f..020387094 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -567,7 +567,8 @@ mod val_exec_ctx { where ValCtx: ValidationContext, { - let channel_id = chan_open_try::validate(ctx, &msg)?; + chan_open_try::validate(ctx, &msg)?; + let channel_id = ChannelId::new(ctx.channel_counter()?); let module = ctx .get_route(&module_id) 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 6cbb97ba0..e280e8289 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 @@ -18,7 +18,7 @@ pub(crate) mod val_exec_ctx { use super::*; use crate::core::{ContextError, ExecutionContext, ValidationContext}; - pub fn validate(ctx_b: &Ctx, msg: &MsgChannelOpenTry) -> Result + pub fn validate(ctx_b: &Ctx, msg: &MsgChannelOpenTry) -> Result<(), ContextError> where Ctx: ValidationContext, { @@ -99,9 +99,7 @@ pub(crate) mod val_exec_ctx { .map_err(ChannelError::VerifyChannelFailed)?; } - let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?); - - Ok(chan_id_on_b) + Ok(()) } pub fn execute(ctx_b: &mut Ctx) -> Result From 28d04f0fe09cd12dc31897a2b4c340c80c0d90f9 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Thu, 12 Jan 2023 16:02:08 -0500 Subject: [PATCH 20/22] chan_open_try_execute --- crates/ibc/src/core/context.rs | 12 ++++++++---- .../core/ics04_channel/handler/chan_open_try.rs | 15 +-------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 020387094..24f200a86 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -593,7 +593,11 @@ mod val_exec_ctx { where ExecCtx: ExecutionContext, { - let channel_id = chan_open_try::execute(ctx_b)?; + let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?); + ctx_b.log_message(format!( + "success: channel open try with channel identifier: {chan_id_on_b}" + )); + let module = ctx_b .get_route_mut(&module_id) .ok_or(ChannelError::RouteNotFound)?; @@ -602,19 +606,19 @@ mod val_exec_ctx { msg.ordering, &msg.connection_hops_on_b, &msg.port_id_on_b, - &channel_id, + &chan_id_on_b, &Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())), &msg.version_supported_on_a, )?; let conn_id_on_b = msg.connection_hops_on_b[0].clone(); - let port_channel_id_on_b = (msg.port_id_on_b.clone(), channel_id.clone()); + let port_channel_id_on_b = (msg.port_id_on_b.clone(), chan_id_on_b.clone()); // emit events and logs { let core_event = IbcEvent::OpenTryChannel(OpenTry::new( msg.port_id_on_b.clone(), - channel_id.clone(), + chan_id_on_b.clone(), msg.port_id_on_a.clone(), msg.chan_id_on_a.clone(), conn_id_on_b.clone(), 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 e280e8289..73d48f978 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 @@ -16,7 +16,7 @@ pub(crate) use val_exec_ctx::*; #[cfg(feature = "val_exec_ctx")] pub(crate) mod val_exec_ctx { use super::*; - use crate::core::{ContextError, ExecutionContext, ValidationContext}; + use crate::core::{ContextError, ValidationContext}; pub fn validate(ctx_b: &Ctx, msg: &MsgChannelOpenTry) -> Result<(), ContextError> where @@ -101,19 +101,6 @@ pub(crate) mod val_exec_ctx { Ok(()) } - - pub fn execute(ctx_b: &mut Ctx) -> Result - where - Ctx: ExecutionContext, - { - let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?); - - ctx_b.log_message(format!( - "success: channel open try with channel identifier: {chan_id_on_b}" - )); - - Ok(chan_id_on_b) - } } /// Per our convention, this message is processed on chain B. pub(crate) fn process( From d884e8fe3b2e24a848007620f523d3dc13cc4e9c Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Thu, 12 Jan 2023 16:02:39 -0500 Subject: [PATCH 21/22] variable naming --- crates/ibc/src/core/context.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/ibc/src/core/context.rs b/crates/ibc/src/core/context.rs index 24f200a86..bc0eb1061 100644 --- a/crates/ibc/src/core/context.rs +++ b/crates/ibc/src/core/context.rs @@ -560,24 +560,24 @@ mod val_exec_ctx { } fn chan_open_try_validate( - ctx: &ValCtx, + ctx_b: &ValCtx, module_id: ModuleId, msg: MsgChannelOpenTry, ) -> Result<(), ContextError> where ValCtx: ValidationContext, { - chan_open_try::validate(ctx, &msg)?; - let channel_id = ChannelId::new(ctx.channel_counter()?); + chan_open_try::validate(ctx_b, &msg)?; + let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?); - let module = ctx + let module = ctx_b .get_route(&module_id) .ok_or(ChannelError::RouteNotFound)?; let _ = module.on_chan_open_try_validate( msg.ordering, &msg.connection_hops_on_b, &msg.port_id_on_b, - &channel_id, + &chan_id_on_b, &Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())), &msg.version_supported_on_a, )?; From 74ac47433063cef5cf16c2f9a9ef5eb8883ca872 Mon Sep 17 00:00:00 2001 From: Philippe Laferriere Date: Thu, 12 Jan 2023 16:07:04 -0500 Subject: [PATCH 22/22] clippy --- crates/ibc/src/applications/transfer/context.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/ibc/src/applications/transfer/context.rs b/crates/ibc/src/applications/transfer/context.rs index 13df64651..dbe582612 100644 --- a/crates/ibc/src/applications/transfer/context.rs +++ b/crates/ibc/src/applications/transfer/context.rs @@ -205,14 +205,14 @@ pub fn on_chan_open_try_validate( got_order: order, }); } - if counterparty_version != &Version::ics20() { + if counterparty_version != &Version::new(VERSION.to_string()) { return Err(TokenTransferError::InvalidCounterpartyVersion { - expect_version: Version::ics20(), + expect_version: Version::new(VERSION.to_string()), got_version: counterparty_version.clone(), }); } - Ok(Version::ics20()) + Ok(Version::new(VERSION.to_string())) } #[cfg(feature = "val_exec_ctx")] @@ -226,7 +226,7 @@ pub fn on_chan_open_try_execute( _counterparty: &Counterparty, _counterparty_version: &Version, ) -> Result<(ModuleExtras, Version), TokenTransferError> { - Ok((ModuleExtras::empty(), Version::ics20())) + Ok((ModuleExtras::empty(), Version::new(VERSION.to_string()))) } #[allow(clippy::too_many_arguments)]