ibc_testkit/testapp/ibc/clients/
mod.rs

1pub mod mock;
2
3use alloc::fmt::Debug;
4
5use basecoin_store::context::ProvableStore;
6use derive_more::From;
7use ibc::clients::tendermint::client_state::ClientState as TmClientState;
8use ibc::clients::tendermint::consensus_state::ConsensusState as TmConsensusState;
9use ibc::clients::tendermint::types::{
10    ClientState as ClientStateType, ConsensusState as ConsensusStateType,
11    TENDERMINT_CLIENT_STATE_TYPE_URL, TENDERMINT_CONSENSUS_STATE_TYPE_URL,
12};
13use ibc::core::client::types::Height;
14use ibc::core::host::types::error::DecodingError;
15use ibc::core::primitives::prelude::*;
16use ibc::derive::{ClientState, ConsensusState};
17use ibc::primitives::proto::{Any, Protobuf};
18
19use super::core::types::MockIbcStore;
20use crate::testapp::ibc::clients::mock::client_state::{
21    MockClientState, MOCK_CLIENT_STATE_TYPE_URL,
22};
23use crate::testapp::ibc::clients::mock::consensus_state::{
24    MockConsensusState, MOCK_CONSENSUS_STATE_TYPE_URL,
25};
26
27#[derive(Debug, Clone, From, PartialEq, ClientState)]
28#[validation(MockIbcStore<S: ProvableStore + Debug>)]
29#[execution(MockIbcStore<S: ProvableStore + Debug>)]
30pub enum AnyClientState {
31    Tendermint(TmClientState),
32    Mock(MockClientState),
33}
34
35impl AnyClientState {
36    pub fn latest_height(&self) -> Height {
37        match self {
38            Self::Tendermint(cs) => cs.inner().latest_height,
39            Self::Mock(cs) => cs.latest_height(),
40        }
41    }
42
43    pub fn is_frozen(&self) -> bool {
44        match self {
45            Self::Tendermint(cs) => cs.inner().is_frozen(),
46            Self::Mock(cs) => cs.is_frozen(),
47        }
48    }
49}
50
51impl Protobuf<Any> for AnyClientState {}
52
53impl TryFrom<Any> for AnyClientState {
54    type Error = DecodingError;
55
56    fn try_from(raw: Any) -> Result<Self, Self::Error> {
57        if raw.type_url == TENDERMINT_CLIENT_STATE_TYPE_URL {
58            Ok(TmClientState::try_from(raw)?.into())
59        } else if raw.type_url == MOCK_CLIENT_STATE_TYPE_URL {
60            MockClientState::try_from(raw).map(Into::into)
61        } else {
62            Err(DecodingError::UnknownTypeUrl(raw.type_url))
63        }
64    }
65}
66
67impl From<AnyClientState> for Any {
68    fn from(host_client_state: AnyClientState) -> Self {
69        match host_client_state {
70            AnyClientState::Tendermint(cs) => cs.into(),
71            AnyClientState::Mock(cs) => cs.into(),
72        }
73    }
74}
75
76impl From<ClientStateType> for AnyClientState {
77    fn from(client_state: ClientStateType) -> Self {
78        Self::Tendermint(client_state.into())
79    }
80}
81
82impl From<ConsensusStateType> for AnyConsensusState {
83    fn from(consensus_state: ConsensusStateType) -> Self {
84        Self::Tendermint(consensus_state.into())
85    }
86}
87
88#[derive(Debug, Clone, From, PartialEq, Eq, ConsensusState)]
89pub enum AnyConsensusState {
90    Tendermint(TmConsensusState),
91    Mock(MockConsensusState),
92}
93
94impl TryFrom<Any> for AnyConsensusState {
95    type Error = DecodingError;
96
97    fn try_from(raw: Any) -> Result<Self, Self::Error> {
98        if raw.type_url == TENDERMINT_CONSENSUS_STATE_TYPE_URL {
99            Ok(TmConsensusState::try_from(raw)?.into())
100        } else if raw.type_url == MOCK_CONSENSUS_STATE_TYPE_URL {
101            MockConsensusState::try_from(raw).map(Into::into)
102        } else {
103            Err(DecodingError::UnknownTypeUrl(raw.type_url))
104        }
105    }
106}
107
108impl From<AnyConsensusState> for Any {
109    fn from(host_consensus_state: AnyConsensusState) -> Self {
110        match host_consensus_state {
111            AnyConsensusState::Tendermint(cs) => cs.into(),
112            AnyConsensusState::Mock(cs) => cs.into(),
113        }
114    }
115}
116
117impl TryFrom<AnyConsensusState> for ConsensusStateType {
118    type Error = DecodingError;
119
120    fn try_from(value: AnyConsensusState) -> Result<Self, Self::Error> {
121        match value {
122            AnyConsensusState::Tendermint(cs) => Ok(cs.inner().clone()),
123            _ => Err(DecodingError::invalid_raw_data(
124                "AnyConsensusState could not be converted to TmConsensusState",
125            )),
126        }
127    }
128}
129
130impl TryFrom<AnyConsensusState> for MockConsensusState {
131    type Error = DecodingError;
132
133    fn try_from(value: AnyConsensusState) -> Result<Self, Self::Error> {
134        match value {
135            AnyConsensusState::Mock(cs) => Ok(cs),
136            _ => Err(DecodingError::invalid_raw_data(
137                "AnyConsensusState could not be converted to MockConsensusState",
138            )),
139        }
140    }
141}