Skip to content

Commit 9203695

Browse files
committed
generalize MockIbcStore
1 parent 9b21c33 commit 9203695

File tree

8 files changed

+78
-59
lines changed

8 files changed

+78
-59
lines changed

ibc-testkit/src/context.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ where
4040
H: TestHost,
4141
AnyClientState: From<HostClientState<H>>,
4242
AnyConsensusState: From<HostConsensusState<H>>,
43-
HostClientState<H>: ClientStateValidation<MockIbcStore<S>>,
43+
HostClientState<H>: ClientStateValidation<MockIbcStore<S, AnyClientState, AnyConsensusState>>,
4444
{
4545
/// The multi store of the context.
4646
/// This is where the IBC store root is stored at IBC commitment prefix.
@@ -50,7 +50,7 @@ where
5050
pub host: H,
5151

5252
/// An object that stores all IBC related data.
53-
pub ibc_store: MockIbcStore<S>,
53+
pub ibc_store: MockIbcStore<S, AnyClientState, AnyConsensusState>,
5454

5555
/// A router that can route messages to the appropriate IBC application.
5656
pub ibc_router: MockRouter,
@@ -74,7 +74,7 @@ where
7474
H: TestHost,
7575
AnyClientState: From<HostClientState<H>>,
7676
AnyConsensusState: From<HostConsensusState<H>>,
77-
HostClientState<H>: ClientStateValidation<MockIbcStore<S>>,
77+
HostClientState<H>: ClientStateValidation<MockIbcStore<S, AnyClientState, AnyConsensusState>>,
7878
{
7979
fn default() -> Self {
8080
TestContextConfig::builder().build()
@@ -87,17 +87,17 @@ impl<S, H> StoreGenericTestContext<S, H>
8787
where
8888
S: ProvableStore + Debug,
8989
H: TestHost,
90-
HostClientState<H>: ClientStateValidation<MockIbcStore<S>>,
90+
HostClientState<H>: ClientStateValidation<MockIbcStore<S, AnyClientState, AnyConsensusState>>,
9191
AnyClientState: From<HostClientState<H>>,
9292
AnyConsensusState: From<HostConsensusState<H>>,
9393
{
9494
/// Returns an immutable reference to the IBC store.
95-
pub fn ibc_store(&self) -> &MockIbcStore<S> {
95+
pub fn ibc_store(&self) -> &MockIbcStore<S, AnyClientState, AnyConsensusState> {
9696
&self.ibc_store
9797
}
9898

9999
/// Returns a mutable reference to the IBC store.
100-
pub fn ibc_store_mut(&mut self) -> &mut MockIbcStore<S> {
100+
pub fn ibc_store_mut(&mut self) -> &mut MockIbcStore<S, AnyClientState, AnyConsensusState> {
101101
&mut self.ibc_store
102102
}
103103

ibc-testkit/src/fixtures/core/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ where
4444
H: TestHost,
4545
AnyClientState: From<HostClientState<H>>,
4646
AnyConsensusState: From<HostConsensusState<H>>,
47-
HostClientState<H>: ClientStateValidation<MockIbcStore<S>>,
47+
HostClientState<H>: ClientStateValidation<MockIbcStore<S, AnyClientState, AnyConsensusState>>,
4848
{
4949
fn from(params: TestContextConfig<H>) -> Self {
5050
assert_ne!(

ibc-testkit/src/testapp/ibc/clients/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ use crate::testapp::ibc::clients::mock::consensus_state::{
2424
MockConsensusState, MOCK_CONSENSUS_STATE_TYPE_URL,
2525
};
2626

27+
type AnyMockIbcStore<S> = MockIbcStore<S, AnyClientState, AnyConsensusState>;
28+
2729
#[derive(Debug, Clone, From, PartialEq, ClientState)]
28-
#[validation(MockIbcStore<S: ProvableStore + Debug>)]
29-
#[execution(MockIbcStore<S: ProvableStore + Debug>)]
30+
#[validation(AnyMockIbcStore<S: ProvableStore + Debug>)]
31+
#[execution(AnyMockIbcStore<S: ProvableStore + Debug>)]
3032
pub enum AnyClientState {
3133
Tendermint(TmClientState),
3234
Mock(MockClientState),

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use core::fmt::Debug;
22

33
use basecoin_store::context::{ProvableStore, Store};
44
use basecoin_store::types::Height as StoreHeight;
5+
use ibc::core::client::context::client_state::{ClientStateExecution, ClientStateValidation};
6+
use ibc::core::client::context::consensus_state::ConsensusState;
57
use ibc::core::client::context::{
68
ClientExecutionContext, ClientValidationContext, ExtClientValidationContext,
79
};
@@ -18,24 +20,25 @@ use ibc::primitives::prelude::*;
1820

1921
use super::types::MockIbcStore;
2022
use crate::testapp::ibc::clients::mock::client_state::MockClientContext;
21-
use crate::testapp::ibc::clients::{AnyClientState, AnyConsensusState};
2223

2324
pub type PortChannelIdMap<V> = BTreeMap<PortId, BTreeMap<ChannelId, V>>;
2425

2526
/// A mock of an IBC client record as it is stored in a mock context.
2627
/// For testing ICS02 handlers mostly, cf. `MockClientContext`.
2728
#[derive(Clone, Debug)]
28-
pub struct MockClientRecord {
29+
pub struct MockClientRecord<ACL, ACS> {
2930
/// The client state (representing only the latest height at the moment).
30-
pub client_state: Option<AnyClientState>,
31+
pub client_state: Option<ACL>,
3132

3233
/// Mapping of heights to consensus states for this client.
33-
pub consensus_states: BTreeMap<Height, AnyConsensusState>,
34+
pub consensus_states: BTreeMap<Height, ACS>,
3435
}
3536

36-
impl<S> MockClientContext for MockIbcStore<S>
37+
impl<S, ACL, ACS> MockClientContext for MockIbcStore<S, ACL, ACS>
3738
where
3839
S: ProvableStore + Debug,
40+
ACL: ClientStateValidation<Self> + Clone,
41+
ACS: ConsensusState + Clone,
3942
{
4043
fn host_timestamp(&self) -> Result<Timestamp, ContextError> {
4144
ValidationContext::host_timestamp(self)
@@ -46,9 +49,11 @@ where
4649
}
4750
}
4851

49-
impl<S> ExtClientValidationContext for MockIbcStore<S>
52+
impl<S, ACL, ACS> ExtClientValidationContext for MockIbcStore<S, ACL, ACS>
5053
where
5154
S: ProvableStore + Debug,
55+
ACL: ClientStateValidation<Self> + Clone,
56+
ACS: ConsensusState + Clone,
5257
{
5358
fn host_timestamp(&self) -> Result<Timestamp, ContextError> {
5459
ValidationContext::host_timestamp(self)
@@ -152,12 +157,14 @@ where
152157
}
153158
}
154159

155-
impl<S> ClientValidationContext for MockIbcStore<S>
160+
impl<S, ACL, ACS> ClientValidationContext for MockIbcStore<S, ACL, ACS>
156161
where
157162
S: ProvableStore + Debug,
163+
ACL: ClientStateValidation<Self> + Clone,
164+
ACS: ConsensusState + Clone,
158165
{
159-
type ClientStateRef = AnyClientState;
160-
type ConsensusStateRef = AnyConsensusState;
166+
type ClientStateRef = ACL;
167+
type ConsensusStateRef = ACS;
161168

162169
fn client_state(&self, client_id: &ClientId) -> Result<Self::ClientStateRef, ContextError> {
163170
Ok(self
@@ -171,7 +178,7 @@ where
171178
fn consensus_state(
172179
&self,
173180
client_cons_state_path: &ClientConsensusStatePath,
174-
) -> Result<AnyConsensusState, ContextError> {
181+
) -> Result<ACS, ContextError> {
175182
let height = Height::new(
176183
client_cons_state_path.revision_number,
177184
client_cons_state_path.revision_height,
@@ -224,11 +231,13 @@ where
224231
}
225232
}
226233

227-
impl<S> ClientExecutionContext for MockIbcStore<S>
234+
impl<S, ACL, ACS> ClientExecutionContext for MockIbcStore<S, ACL, ACS>
228235
where
229236
S: ProvableStore + Debug,
237+
ACL: ClientStateExecution<Self> + Clone,
238+
ACS: ConsensusState + Clone,
230239
{
231-
type ClientStateMut = AnyClientState;
240+
type ClientStateMut = ACL;
232241

233242
/// Called upon successful client creation and update
234243
fn store_client_state(

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use ibc::core::channel::types::channel::{ChannelEnd, IdentifiedChannelEnd};
99
use ibc::core::channel::types::commitment::{AcknowledgementCommitment, PacketCommitment};
1010
use ibc::core::channel::types::error::{ChannelError, PacketError};
1111
use ibc::core::channel::types::packet::{PacketState, Receipt};
12+
use ibc::core::client::context::client_state::{ClientStateExecution, ClientStateValidation};
1213
use ibc::core::client::context::consensus_state::ConsensusState;
1314
use ibc::core::client::types::error::ClientError;
1415
use ibc::core::client::types::Height;
@@ -32,15 +33,16 @@ use ibc_proto::ibc::core::commitment::v1::MerkleProof as RawMerkleProof;
3233
use ibc_query::core::context::{ProvableContext, QueryContext};
3334

3435
use super::types::{MockIbcStore, DEFAULT_BLOCK_TIME_SECS};
35-
use crate::testapp::ibc::clients::{AnyClientState, AnyConsensusState};
3636

37-
impl<S> ValidationContext for MockIbcStore<S>
37+
impl<S, ACL, ACS> ValidationContext for MockIbcStore<S, ACL, ACS>
3838
where
3939
S: ProvableStore + Debug,
40+
ACL: ClientStateValidation<Self> + Clone,
41+
ACS: ConsensusState + Clone,
4042
{
4143
type V = Self;
42-
type HostClientState = AnyClientState;
43-
type HostConsensusState = AnyConsensusState;
44+
type HostClientState = ACL;
45+
type HostConsensusState = ACS;
4446

4547
fn host_height(&self) -> Result<Height, ContextError> {
4648
Ok(Height::new(
@@ -79,12 +81,13 @@ where
7981
&self,
8082
client_state_of_host_on_counterparty: Self::HostClientState,
8183
) -> Result<(), ContextError> {
82-
if client_state_of_host_on_counterparty.is_frozen() {
83-
return Err(ClientError::ClientFrozen {
84-
description: String::new(),
85-
}
86-
.into());
87-
}
84+
// // FIXME(rano): is_frozen is specific to Tendermint or Mock client state.
85+
// if client_state_of_host_on_counterparty.is_frozen() {
86+
// return Err(ClientError::ClientFrozen {
87+
// description: String::new(),
88+
// }
89+
// .into());
90+
// }
8891

8992
let latest_height = self.host_height()?;
9093

@@ -269,9 +272,11 @@ where
269272
}
270273

271274
/// Trait to provide proofs in gRPC service blanket implementations.
272-
impl<S> ProvableContext for MockIbcStore<S>
275+
impl<S, ACL, ACS> ProvableContext for MockIbcStore<S, ACL, ACS>
273276
where
274277
S: ProvableStore + Debug,
278+
ACL: ClientStateValidation<Self> + Clone,
279+
ACS: ConsensusState + Clone,
275280
{
276281
/// Returns the proof for the given [`Height`] and [`Path`]
277282
fn get_proof(&self, height: Height, path: &Path) -> Option<Vec<u8>> {
@@ -294,9 +299,11 @@ where
294299
}
295300

296301
/// Trait to complete the gRPC service blanket implementations.
297-
impl<S> QueryContext for MockIbcStore<S>
302+
impl<S, ACL, ACS> QueryContext for MockIbcStore<S, ACL, ACS>
298303
where
299304
S: ProvableStore + Debug,
305+
ACL: ClientStateValidation<Self> + Clone,
306+
ACS: ConsensusState + Clone,
300307
{
301308
/// Returns the list of all client states.
302309
fn client_states(&self) -> Result<Vec<(ClientId, ClientStateRef<Self>)>, ContextError> {
@@ -636,9 +643,11 @@ where
636643
}
637644
}
638645

639-
impl<S> ExecutionContext for MockIbcStore<S>
646+
impl<S, ACL, ACS> ExecutionContext for MockIbcStore<S, ACL, ACS>
640647
where
641648
S: ProvableStore + Debug,
649+
ACL: ClientStateExecution<Self> + Clone,
650+
ACS: ConsensusState + Clone,
642651
{
643652
type E = Self;
644653

ibc-testkit/src/testapp/ibc/core/types.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use basecoin_store::types::{BinStore, JsonStore, ProtobufStore, TypedSet, TypedS
99
use ibc::core::channel::types::channel::ChannelEnd;
1010
use ibc::core::channel::types::commitment::{AcknowledgementCommitment, PacketCommitment};
1111
use ibc::core::client::context::client_state::ClientStateValidation;
12+
use ibc::core::client::context::consensus_state::ConsensusState;
1213
use ibc::core::client::types::Height;
1314
use ibc::core::connection::types::ConnectionEnd;
1415
use ibc::core::handler::types::events::IbcEvent;
@@ -32,17 +33,18 @@ use typed_builder::TypedBuilder;
3233
use crate::context::{MockStore, TestContext};
3334
use crate::fixtures::core::context::TestContextConfig;
3435
use crate::hosts::{HostClientState, HostConsensusState, TestBlock, TestHeader, TestHost};
35-
use crate::testapp::ibc::clients::mock::header::MockHeader;
3636
use crate::testapp::ibc::clients::{AnyClientState, AnyConsensusState};
3737
pub const DEFAULT_BLOCK_TIME_SECS: u64 = 3;
3838

39-
pub type DefaultIbcStore = MockIbcStore<MockStore>;
39+
pub type DefaultIbcStore = MockIbcStore<MockStore, AnyClientState, AnyConsensusState>;
4040

4141
/// An object that stores all IBC related data.
4242
#[derive(Debug)]
43-
pub struct MockIbcStore<S>
43+
pub struct MockIbcStore<S, ACL, ACS>
4444
where
4545
S: ProvableStore + Debug,
46+
ACL: ClientStateValidation<Self> + Clone,
47+
ACS: ConsensusState + Clone,
4648
{
4749
/// chain revision number,
4850
pub revision_number: Arc<Mutex<u64>>,
@@ -62,10 +64,9 @@ where
6264
pub client_processed_heights:
6365
ProtobufStore<SharedStore<S>, ClientUpdateHeightPath, Height, RawHeight>,
6466
/// A typed-store for AnyClientState
65-
pub client_state_store: ProtobufStore<SharedStore<S>, ClientStatePath, AnyClientState, Any>,
67+
pub client_state_store: ProtobufStore<SharedStore<S>, ClientStatePath, ACL, Any>,
6668
/// A typed-store for AnyConsensusState
67-
pub consensus_state_store:
68-
ProtobufStore<SharedStore<S>, ClientConsensusStatePath, AnyConsensusState, Any>,
69+
pub consensus_state_store: ProtobufStore<SharedStore<S>, ClientConsensusStatePath, ACS, Any>,
6970
/// A typed-store for ConnectionEnd
7071
pub connection_end_store:
7172
ProtobufStore<SharedStore<S>, ConnectionPath, ConnectionEnd, RawConnectionEnd>,
@@ -86,7 +87,7 @@ where
8687
/// A typed-store for packet ack
8788
pub packet_ack_store: BinStore<SharedStore<S>, AckPath, AcknowledgementCommitment>,
8889
/// Map of host consensus states
89-
pub host_consensus_states: Arc<Mutex<BTreeMap<u64, AnyConsensusState>>>,
90+
pub host_consensus_states: Arc<Mutex<BTreeMap<u64, ACS>>>,
9091
/// Map of older ibc commitment proofs
9192
pub ibc_commiment_proofs: Arc<Mutex<BTreeMap<u64, CommitmentProof>>>,
9293
/// IBC Events
@@ -95,9 +96,11 @@ where
9596
pub logs: Arc<Mutex<Vec<String>>>,
9697
}
9798

98-
impl<S> MockIbcStore<S>
99+
impl<S, ACL, ACS> MockIbcStore<S, ACL, ACS>
99100
where
100101
S: ProvableStore + Debug,
102+
ACL: ClientStateValidation<Self> + Clone,
103+
ACS: ConsensusState + Clone,
101104
{
102105
pub fn new(revision_number: u64, store: S) -> Self {
103106
let shared_store = SharedStore::new(store);
@@ -144,7 +147,7 @@ where
144147
}
145148
}
146149

147-
fn store_host_consensus_state(&mut self, height: u64, consensus_state: AnyConsensusState) {
150+
fn store_host_consensus_state(&mut self, height: u64, consensus_state: ACS) {
148151
self.host_consensus_states
149152
.lock()
150153
.insert(height, consensus_state);
@@ -154,12 +157,7 @@ where
154157
self.ibc_commiment_proofs.lock().insert(height, proof);
155158
}
156159

157-
pub fn begin_block(
158-
&mut self,
159-
height: u64,
160-
consensus_state: AnyConsensusState,
161-
proof: CommitmentProof,
162-
) {
160+
pub fn begin_block(&mut self, height: u64, consensus_state: ACS, proof: CommitmentProof) {
163161
assert_eq!(self.store.current_height(), height);
164162
self.store_host_consensus_state(height, consensus_state);
165163
self.store_ibc_commitment_proof(height, proof);
@@ -178,18 +176,20 @@ where
178176
}
179177
}
180178

181-
impl<S> Default for MockIbcStore<S>
179+
impl<S, ACL, ACS> Default for MockIbcStore<S, ACL, ACS>
182180
where
183181
S: ProvableStore + Debug + Default,
182+
ACL: ClientStateValidation<Self> + Clone,
183+
ACS: ConsensusState + Clone,
184184
{
185185
fn default() -> Self {
186-
// Note: this creates a MockIbcStore which has MockConsensusState as Host ConsensusState
187186
let mut ibc_store = Self::new(0, S::default());
188187
ibc_store.store.commit().expect("no error");
189-
ibc_store.store_host_consensus_state(
190-
ibc_store.store.current_height(),
191-
MockHeader::default().into_consensus_state().into(),
192-
);
188+
// // FIXME(rano): store a consensus state.
189+
// ibc_store.store_host_consensus_state(
190+
// ibc_store.store.current_height(),
191+
// MockHeader::default().into_consensus_state().into(),
192+
// );
193193
ibc_store.store_ibc_commitment_proof(
194194
ibc_store.store.current_height(),
195195
CommitmentProof::default(),

tests-integration/tests/core/ics02_client/create_client.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use basecoin_store::impls::InMemoryStore;
21
use ibc::clients::tendermint::types::{
32
client_type as tm_client_type, ConsensusState as TmConsensusState,
43
};
@@ -28,7 +27,7 @@ use ibc_testkit::testapp::ibc::clients::mock::consensus_state::MockConsensusStat
2827
use ibc_testkit::testapp::ibc::clients::mock::header::MockHeader;
2928
use ibc_testkit::testapp::ibc::clients::{AnyClientState, AnyConsensusState};
3029
use ibc_testkit::testapp::ibc::core::router::MockRouter;
31-
use ibc_testkit::testapp::ibc::core::types::{DefaultIbcStore, LightClientBuilder, MockIbcStore};
30+
use ibc_testkit::testapp::ibc::core::types::{DefaultIbcStore, LightClientBuilder};
3231
use test_log::test;
3332

3433
#[test]
@@ -96,7 +95,7 @@ fn test_tm_create_client_ok() {
9695
assert!(res.is_ok(), "tendermint client execution happy path");
9796

9897
let expected_client_state =
99-
ClientStateRef::<MockIbcStore<InMemoryStore>>::try_from(msg.client_state).unwrap();
98+
ClientStateRef::<DefaultIbcStore>::try_from(msg.client_state).unwrap();
10099
assert_eq!(expected_client_state.client_type(), client_type);
101100
assert_eq!(ctx.client_state(&client_id).unwrap(), expected_client_state);
102101
}

0 commit comments

Comments
 (0)