From 78cb644654129b33ebd4bceeedcbaec2ce63060a Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Tue, 6 Feb 2024 20:16:56 +0100 Subject: [PATCH] use ibc host paths in mock context --- .../src/testapp/ibc/core/client_ctx.rs | 60 +++++++++++---- ibc-testkit/src/testapp/ibc/core/core_ctx.rs | 75 +++++++++++++++++-- ibc-testkit/src/testapp/ibc/core/types.rs | 56 +++++++++----- 3 files changed, 152 insertions(+), 39 deletions(-) diff --git a/ibc-testkit/src/testapp/ibc/core/client_ctx.rs b/ibc-testkit/src/testapp/ibc/core/client_ctx.rs index e48fac748..7658ba9d4 100644 --- a/ibc-testkit/src/testapp/ibc/core/client_ctx.rs +++ b/ibc-testkit/src/testapp/ibc/core/client_ctx.rs @@ -12,7 +12,9 @@ use ibc::core::client::types::error::ClientError; use ibc::core::client::types::Height; use ibc::core::handler::types::error::ContextError; use ibc::core::host::types::identifiers::{ChannelId, ClientId, PortId}; -use ibc::core::host::types::path::{ClientConsensusStatePath, ClientStatePath, Path}; +use ibc::core::host::types::path::{ + ClientConsensusStatePath, ClientStatePath, ClientUpdateHeightPath, ClientUpdateTimePath, Path, +}; use ibc::core::host::ValidationContext; use ibc::core::primitives::Timestamp; use ibc::primitives::prelude::{format, *}; @@ -66,12 +68,15 @@ where client_id: &ClientId, height: &Height, ) -> Result { + let client_update_time_path = ClientUpdateTimePath::new( + client_id.clone(), + height.revision_number(), + height.revision_height(), + ); let processed_timestamp = self .ibc_store .client_processed_times - .lock() - .get(&(client_id.clone(), *height)) - .cloned() + .get(StoreHeight::Pending, &client_update_time_path) .ok_or(ClientError::ProcessedTimeNotFound { client_id: client_id.clone(), height: *height, @@ -85,12 +90,15 @@ where client_id: &ClientId, height: &Height, ) -> Result { + let client_update_height_path = ClientUpdateHeightPath::new( + client_id.clone(), + height.revision_number(), + height.revision_height(), + ); let processed_height = self .ibc_store .client_processed_heights - .lock() - .get(&(client_id.clone(), *height)) - .cloned() + .get(StoreHeight::Pending, &client_update_height_path) .ok_or(ClientError::ProcessedHeightNotFound { client_id: client_id.clone(), height: *height, @@ -149,10 +157,17 @@ where height: Height, timestamp: Timestamp, ) -> Result<(), ContextError> { + let client_update_time_path = ClientUpdateTimePath::new( + client_id.clone(), + height.revision_number(), + height.revision_height(), + ); self.ibc_store .client_processed_times - .lock() - .insert((client_id, height), timestamp); + .set(client_update_time_path, timestamp) + .map_err(|_| ClientError::Other { + description: "store update error".into(), + })?; Ok(()) } @@ -165,10 +180,17 @@ where height: Height, host_height: Height, ) -> Result<(), ContextError> { + let client_update_height_path = ClientUpdateHeightPath::new( + client_id.clone(), + height.revision_number(), + height.revision_height(), + ); self.ibc_store .client_processed_heights - .lock() - .insert((client_id, height), host_height); + .set(client_update_height_path, host_height) + .map_err(|_| ClientError::Other { + description: "store update error".into(), + })?; Ok(()) } @@ -178,10 +200,14 @@ where client_id: ClientId, height: Height, ) -> Result<(), ContextError> { + let client_update_time_path = ClientUpdateTimePath::new( + client_id.clone(), + height.revision_number(), + height.revision_height(), + ); self.ibc_store .client_processed_times - .lock() - .remove(&(client_id, height)); + .delete(client_update_time_path); Ok(()) } @@ -191,10 +217,14 @@ where client_id: ClientId, height: Height, ) -> Result<(), ContextError> { + let client_update_height_path = ClientUpdateHeightPath::new( + client_id.clone(), + height.revision_number(), + height.revision_height(), + ); self.ibc_store .client_processed_heights - .lock() - .remove(&(client_id, height)); + .delete(client_update_height_path); Ok(()) } diff --git a/ibc-testkit/src/testapp/ibc/core/core_ctx.rs b/ibc-testkit/src/testapp/ibc/core/core_ctx.rs index 2b484349e..bd450ff18 100644 --- a/ibc-testkit/src/testapp/ibc/core/core_ctx.rs +++ b/ibc-testkit/src/testapp/ibc/core/core_ctx.rs @@ -21,7 +21,8 @@ use ibc::core::handler::types::events::IbcEvent; use ibc::core::host::types::identifiers::{ClientId, ConnectionId, Sequence}; use ibc::core::host::types::path::{ AckPath, ChannelEndPath, ClientConnectionPath, ClientConsensusStatePath, ClientStatePath, - CommitmentPath, ConnectionPath, Path, ReceiptPath, SeqAckPath, SeqRecvPath, SeqSendPath, + CommitmentPath, ConnectionPath, NextChannelSequencePath, NextClientSequencePath, + NextConnectionSequencePath, Path, ReceiptPath, SeqAckPath, SeqRecvPath, SeqSendPath, }; use ibc::core::host::{ExecutionContext, ValidationContext}; use ibc::core::primitives::prelude::*; @@ -108,7 +109,13 @@ where } fn client_counter(&self) -> Result { - Ok(*self.ibc_store.client_counter.lock()) + Ok(self + .ibc_store + .client_counter + .get(StoreHeight::Pending, &NextClientSequencePath {}) + .ok_or(ClientError::Other { + description: "client counter not found".into(), + })?) } fn connection_end(&self, conn_id: &ConnectionId) -> Result { @@ -132,7 +139,13 @@ where } fn connection_counter(&self) -> Result { - Ok(*self.ibc_store.conn_counter.lock()) + Ok(self + .ibc_store + .conn_counter + .get(StoreHeight::Pending, &NextConnectionSequencePath {}) + .ok_or(ConnectionError::Other { + description: "connection counter not found".into(), + })?) } fn get_compatible_versions(&self) -> Vec { @@ -252,7 +265,13 @@ where /// The value of this counter should increase only via method /// `ChannelKeeper::increase_channel_counter`. fn channel_counter(&self) -> Result { - Ok(*self.ibc_store.channel_counter.lock()) + Ok(self + .ibc_store + .channel_counter + .get(StoreHeight::Pending, &NextChannelSequencePath {}) + .ok_or(ChannelError::Other { + description: "channel counter not found".into(), + })?) } /// Returns the maximum expected time per block @@ -646,7 +665,21 @@ where /// Increases the counter which keeps track of how many clients have been created. /// Should never fail. fn increase_client_counter(&mut self) -> Result<(), ContextError> { - *self.ibc_store.client_counter.lock() += 1; + let current_sequence = self + .ibc_store + .client_counter + .get(StoreHeight::Pending, &NextClientSequencePath {}) + .ok_or(ClientError::Other { + description: "client counter not found".into(), + })?; + + self.ibc_store + .client_counter + .set(NextClientSequencePath {}, current_sequence + 1) + .map_err(|e| ClientError::Other { + description: format!("client counter update failed: {e:?}"), + })?; + Ok(()) } @@ -690,7 +723,21 @@ where /// Increases the counter which keeps track of how many connections have been created. /// Should never fail. fn increase_connection_counter(&mut self) -> Result<(), ContextError> { - *self.ibc_store.conn_counter.lock() += 1; + let current_sequence = self + .ibc_store + .conn_counter + .get(StoreHeight::Pending, &NextConnectionSequencePath {}) + .ok_or(ConnectionError::Other { + description: "connection counter not found".into(), + })?; + + self.ibc_store + .conn_counter + .set(NextConnectionSequencePath {}, current_sequence + 1) + .map_err(|e| ConnectionError::Other { + description: format!("connection counter update failed: {e:?}"), + })?; + Ok(()) } @@ -792,7 +839,21 @@ where } fn increase_channel_counter(&mut self) -> Result<(), ContextError> { - *self.ibc_store.channel_counter.lock() += 1; + let current_sequence = self + .ibc_store + .channel_counter + .get(StoreHeight::Pending, &NextChannelSequencePath {}) + .ok_or(ChannelError::Other { + description: "channel counter not found".into(), + })?; + + self.ibc_store + .channel_counter + .set(NextChannelSequencePath {}, current_sequence + 1) + .map_err(|e| ChannelError::Other { + description: format!("channel counter update failed: {e:?}"), + })?; + Ok(()) } diff --git a/ibc-testkit/src/testapp/ibc/core/types.rs b/ibc-testkit/src/testapp/ibc/core/types.rs index 4a2fef5b4..63b091a63 100644 --- a/ibc-testkit/src/testapp/ibc/core/types.rs +++ b/ibc-testkit/src/testapp/ibc/core/types.rs @@ -25,7 +25,9 @@ use ibc::core::host::types::identifiers::{ }; use ibc::core::host::types::path::{ AckPath, ChannelEndPath, ClientConnectionPath, ClientConsensusStatePath, ClientStatePath, - CommitmentPath, ConnectionPath, ReceiptPath, SeqAckPath, SeqRecvPath, SeqSendPath, + ClientUpdateHeightPath, ClientUpdateTimePath, CommitmentPath, ConnectionPath, + NextChannelSequencePath, NextClientSequencePath, NextConnectionSequencePath, ReceiptPath, + SeqAckPath, SeqRecvPath, SeqSendPath, }; use ibc::core::host::{ExecutionContext, ValidationContext}; use ibc::core::primitives::prelude::*; @@ -33,6 +35,7 @@ use ibc::core::primitives::Timestamp; use ibc::core::router::router::Router; use ibc_proto::google::protobuf::Any; use ibc_proto::ibc::core::channel::v1::Channel as RawChannelEnd; +use ibc_proto::ibc::core::client::v1::Height as RawHeight; use ibc_proto::ibc::core::connection::v1::ConnectionEnd as RawConnectionEnd; use parking_lot::Mutex; use tendermint_testgen::Validator as TestgenValidator; @@ -60,18 +63,18 @@ where /// Handle to store instance. /// The module is guaranteed exclusive access to all paths in the store key-space. pub store: SharedStore, - /// Counter for clients - pub client_counter: Arc>, - /// Counter for connections - pub conn_counter: Arc>, - /// Counter for channels - pub channel_counter: Arc>, + /// A typed-store for next client counter sequence + pub client_counter: JsonStore, NextClientSequencePath, u64>, + /// A typed-store for next connection counter sequence + pub conn_counter: JsonStore, NextConnectionSequencePath, u64>, + /// A typed-store for next channel counter sequence + pub channel_counter: JsonStore, NextChannelSequencePath, u64>, /// Tracks the processed time for client updates - pub client_processed_times: Arc>>, - /// Tracks the processed height for client updates - pub client_processed_heights: Arc>>, - /// Map of host consensus states - pub consensus_states: Arc>>, + // pub client_processed_times: Arc>>, + pub client_processed_times: JsonStore, ClientUpdateTimePath, Timestamp>, + /// A typed-store to track the processed height for client updates + pub client_processed_heights: + ProtobufStore, ClientUpdateHeightPath, Height, RawHeight>, /// A typed-store for AnyClientState pub client_state_store: ProtobufStore, ClientStatePath, AnyClientState, Any>, /// A typed-store for AnyConsensusState @@ -96,6 +99,8 @@ where pub packet_receipt_store: TypedSet, ReceiptPath>, /// A typed-store for packet ack pub packet_ack_store: BinStore, AckPath, AcknowledgementCommitment>, + /// Map of host consensus states + pub consensus_states: Arc>>, /// IBC Events pub events: Arc>>, /// message logs @@ -108,12 +113,29 @@ where { pub fn new(store: S) -> Self { let shared_store = SharedStore::new(store); + + let mut client_counter = TypedStore::new(shared_store.clone()); + let mut conn_counter = TypedStore::new(shared_store.clone()); + let mut channel_counter = TypedStore::new(shared_store.clone()); + + client_counter + .set(NextClientSequencePath {}, 0) + .expect("no error"); + + conn_counter + .set(NextConnectionSequencePath {}, 0) + .expect("no error"); + + channel_counter + .set(NextChannelSequencePath {}, 0) + .expect("no error"); + Self { - client_counter: Arc::new(Mutex::new(0)), - conn_counter: Arc::new(Mutex::new(0)), - channel_counter: Arc::new(Mutex::new(0)), - client_processed_times: Arc::new(Mutex::new(Default::default())), - client_processed_heights: Arc::new(Mutex::new(Default::default())), + client_counter, + conn_counter, + channel_counter, + client_processed_times: TypedStore::new(shared_store.clone()), + client_processed_heights: TypedStore::new(shared_store.clone()), consensus_states: Arc::new(Mutex::new(Default::default())), client_state_store: TypedStore::new(shared_store.clone()), consensus_state_store: TypedStore::new(shared_store.clone()),