ibc_testkit/testapp/ibc/clients/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
pub mod mock;

use alloc::fmt::Debug;

use basecoin_store::context::ProvableStore;
use derive_more::From;
use ibc::clients::tendermint::client_state::ClientState as TmClientState;
use ibc::clients::tendermint::consensus_state::ConsensusState as TmConsensusState;
use ibc::clients::tendermint::types::{
    ClientState as ClientStateType, ConsensusState as ConsensusStateType,
    TENDERMINT_CLIENT_STATE_TYPE_URL, TENDERMINT_CONSENSUS_STATE_TYPE_URL,
};
use ibc::core::client::types::Height;
use ibc::core::host::types::error::DecodingError;
use ibc::core::primitives::prelude::*;
use ibc::derive::{ClientState, ConsensusState};
use ibc::primitives::proto::{Any, Protobuf};

use super::core::types::MockIbcStore;
use crate::testapp::ibc::clients::mock::client_state::{
    MockClientState, MOCK_CLIENT_STATE_TYPE_URL,
};
use crate::testapp::ibc::clients::mock::consensus_state::{
    MockConsensusState, MOCK_CONSENSUS_STATE_TYPE_URL,
};

#[derive(Debug, Clone, From, PartialEq, ClientState)]
#[validation(MockIbcStore<S: ProvableStore + Debug>)]
#[execution(MockIbcStore<S: ProvableStore + Debug>)]
pub enum AnyClientState {
    Tendermint(TmClientState),
    Mock(MockClientState),
}

impl AnyClientState {
    pub fn latest_height(&self) -> Height {
        match self {
            Self::Tendermint(cs) => cs.inner().latest_height,
            Self::Mock(cs) => cs.latest_height(),
        }
    }

    pub fn is_frozen(&self) -> bool {
        match self {
            Self::Tendermint(cs) => cs.inner().is_frozen(),
            Self::Mock(cs) => cs.is_frozen(),
        }
    }
}

impl Protobuf<Any> for AnyClientState {}

impl TryFrom<Any> for AnyClientState {
    type Error = DecodingError;

    fn try_from(raw: Any) -> Result<Self, Self::Error> {
        if raw.type_url == TENDERMINT_CLIENT_STATE_TYPE_URL {
            Ok(TmClientState::try_from(raw)?.into())
        } else if raw.type_url == MOCK_CLIENT_STATE_TYPE_URL {
            MockClientState::try_from(raw).map(Into::into)
        } else {
            Err(DecodingError::UnknownTypeUrl(raw.type_url))
        }
    }
}

impl From<AnyClientState> for Any {
    fn from(host_client_state: AnyClientState) -> Self {
        match host_client_state {
            AnyClientState::Tendermint(cs) => cs.into(),
            AnyClientState::Mock(cs) => cs.into(),
        }
    }
}

impl From<ClientStateType> for AnyClientState {
    fn from(client_state: ClientStateType) -> Self {
        Self::Tendermint(client_state.into())
    }
}

impl From<ConsensusStateType> for AnyConsensusState {
    fn from(consensus_state: ConsensusStateType) -> Self {
        Self::Tendermint(consensus_state.into())
    }
}

#[derive(Debug, Clone, From, PartialEq, Eq, ConsensusState)]
pub enum AnyConsensusState {
    Tendermint(TmConsensusState),
    Mock(MockConsensusState),
}

impl TryFrom<Any> for AnyConsensusState {
    type Error = DecodingError;

    fn try_from(raw: Any) -> Result<Self, Self::Error> {
        if raw.type_url == TENDERMINT_CONSENSUS_STATE_TYPE_URL {
            Ok(TmConsensusState::try_from(raw)?.into())
        } else if raw.type_url == MOCK_CONSENSUS_STATE_TYPE_URL {
            MockConsensusState::try_from(raw).map(Into::into)
        } else {
            Err(DecodingError::UnknownTypeUrl(raw.type_url))
        }
    }
}

impl From<AnyConsensusState> for Any {
    fn from(host_consensus_state: AnyConsensusState) -> Self {
        match host_consensus_state {
            AnyConsensusState::Tendermint(cs) => cs.into(),
            AnyConsensusState::Mock(cs) => cs.into(),
        }
    }
}

impl TryFrom<AnyConsensusState> for ConsensusStateType {
    type Error = DecodingError;

    fn try_from(value: AnyConsensusState) -> Result<Self, Self::Error> {
        match value {
            AnyConsensusState::Tendermint(cs) => Ok(cs.inner().clone()),
            _ => Err(DecodingError::invalid_raw_data(
                "AnyConsensusState could not be converted to TmConsensusState",
            )),
        }
    }
}

impl TryFrom<AnyConsensusState> for MockConsensusState {
    type Error = DecodingError;

    fn try_from(value: AnyConsensusState) -> Result<Self, Self::Error> {
        match value {
            AnyConsensusState::Mock(cs) => Ok(cs),
            _ => Err(DecodingError::invalid_raw_data(
                "AnyConsensusState could not be converted to MockConsensusState",
            )),
        }
    }
}