Skip to content

Commit

Permalink
use ibc host paths in mock context
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Feb 6, 2024
1 parent 7e6913a commit 78cb644
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 39 deletions.
60 changes: 45 additions & 15 deletions ibc-testkit/src/testapp/ibc/core/client_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, *};
Expand Down Expand Up @@ -66,12 +68,15 @@ where
client_id: &ClientId,
height: &Height,
) -> Result<Timestamp, ContextError> {
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,
Expand All @@ -85,12 +90,15 @@ where
client_id: &ClientId,
height: &Height,
) -> Result<Height, ContextError> {
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,
Expand Down Expand Up @@ -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(),

Check warning on line 169 in ibc-testkit/src/testapp/ibc/core/client_ctx.rs

View check run for this annotation

Codecov / codecov/patch

ibc-testkit/src/testapp/ibc/core/client_ctx.rs#L169

Added line #L169 was not covered by tests
})?;
Ok(())
}

Expand All @@ -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(),

Check warning on line 192 in ibc-testkit/src/testapp/ibc/core/client_ctx.rs

View check run for this annotation

Codecov / codecov/patch

ibc-testkit/src/testapp/ibc/core/client_ctx.rs#L192

Added line #L192 was not covered by tests
})?;
Ok(())
}

Expand All @@ -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(())
}

Expand All @@ -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(())
}

Expand Down
75 changes: 68 additions & 7 deletions ibc-testkit/src/testapp/ibc/core/core_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -108,7 +109,13 @@ where
}

fn client_counter(&self) -> Result<u64, ContextError> {
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<ConnectionEnd, ContextError> {
Expand All @@ -132,7 +139,13 @@ where
}

fn connection_counter(&self) -> Result<u64, ContextError> {
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<ConnectionVersion> {
Expand Down Expand Up @@ -252,7 +265,13 @@ where
/// The value of this counter should increase only via method
/// `ChannelKeeper::increase_channel_counter`.
fn channel_counter(&self) -> Result<u64, ContextError> {
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
Expand Down Expand Up @@ -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:?}"),

Check warning on line 680 in ibc-testkit/src/testapp/ibc/core/core_ctx.rs

View check run for this annotation

Codecov / codecov/patch

ibc-testkit/src/testapp/ibc/core/core_ctx.rs#L680

Added line #L680 was not covered by tests
})?;

Ok(())
}

Expand Down Expand Up @@ -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:?}"),

Check warning on line 738 in ibc-testkit/src/testapp/ibc/core/core_ctx.rs

View check run for this annotation

Codecov / codecov/patch

ibc-testkit/src/testapp/ibc/core/core_ctx.rs#L738

Added line #L738 was not covered by tests
})?;

Ok(())
}

Expand Down Expand Up @@ -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:?}"),

Check warning on line 854 in ibc-testkit/src/testapp/ibc/core/core_ctx.rs

View check run for this annotation

Codecov / codecov/patch

ibc-testkit/src/testapp/ibc/core/core_ctx.rs#L854

Added line #L854 was not covered by tests
})?;

Ok(())
}

Expand Down
56 changes: 39 additions & 17 deletions ibc-testkit/src/testapp/ibc/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ 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::*;
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;
Expand Down Expand Up @@ -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<S>,
/// Counter for clients
pub client_counter: Arc<Mutex<u64>>,
/// Counter for connections
pub conn_counter: Arc<Mutex<u64>>,
/// Counter for channels
pub channel_counter: Arc<Mutex<u64>>,
/// A typed-store for next client counter sequence
pub client_counter: JsonStore<SharedStore<S>, NextClientSequencePath, u64>,
/// A typed-store for next connection counter sequence
pub conn_counter: JsonStore<SharedStore<S>, NextConnectionSequencePath, u64>,
/// A typed-store for next channel counter sequence
pub channel_counter: JsonStore<SharedStore<S>, NextChannelSequencePath, u64>,
/// Tracks the processed time for client updates
pub client_processed_times: Arc<Mutex<BTreeMap<(ClientId, Height), Timestamp>>>,
/// Tracks the processed height for client updates
pub client_processed_heights: Arc<Mutex<BTreeMap<(ClientId, Height), Height>>>,
/// Map of host consensus states
pub consensus_states: Arc<Mutex<BTreeMap<u64, AnyConsensusState>>>,
// pub client_processed_times: Arc<Mutex<BTreeMap<(ClientId, Height), Timestamp>>>,
pub client_processed_times: JsonStore<SharedStore<S>, ClientUpdateTimePath, Timestamp>,
/// A typed-store to track the processed height for client updates
pub client_processed_heights:
ProtobufStore<SharedStore<S>, ClientUpdateHeightPath, Height, RawHeight>,
/// A typed-store for AnyClientState
pub client_state_store: ProtobufStore<SharedStore<S>, ClientStatePath, AnyClientState, Any>,
/// A typed-store for AnyConsensusState
Expand All @@ -96,6 +99,8 @@ where
pub packet_receipt_store: TypedSet<SharedStore<S>, ReceiptPath>,
/// A typed-store for packet ack
pub packet_ack_store: BinStore<SharedStore<S>, AckPath, AcknowledgementCommitment>,
/// Map of host consensus states
pub consensus_states: Arc<Mutex<BTreeMap<u64, AnyConsensusState>>>,
/// IBC Events
pub events: Arc<Mutex<Vec<IbcEvent>>>,
/// message logs
Expand All @@ -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()),
Expand Down

0 comments on commit 78cb644

Please sign in to comment.