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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use core::marker::{Send, Sync};
use std::convert::{TryFrom, TryInto};
use chrono::{DateTime, Utc};
use prost_types::Any;
use serde::Serialize;
use tendermint_proto::Protobuf;
use ibc_proto::ibc::core::client::v1::ConsensusStateWithHeight;
use crate::events::IbcEventType;
use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::error::{Error, Kind};
use crate::ics02_client::height::Height;
use crate::ics07_tendermint::consensus_state;
use crate::ics23_commitment::commitment::CommitmentRoot;
use crate::ics24_host::identifier::ClientId;
use crate::timestamp::Timestamp;
use crate::utils::UnwrapInfallible;
#[cfg(any(test, feature = "mocks"))]
use crate::mock::client_state::MockConsensusState;
pub const TENDERMINT_CONSENSUS_STATE_TYPE_URL: &str =
"/ibc.lightclients.tendermint.v1.ConsensusState";
pub const MOCK_CONSENSUS_STATE_TYPE_URL: &str = "/ibc.mock.ConsensusState";
#[dyn_clonable::clonable]
pub trait ConsensusState: Clone + std::fmt::Debug + Send + Sync {
fn client_type(&self) -> ClientType;
fn root(&self) -> &CommitmentRoot;
fn validate_basic(&self) -> Result<(), Box<dyn std::error::Error>>;
fn wrap_any(self) -> AnyConsensusState;
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(tag = "type")]
pub enum AnyConsensusState {
Tendermint(consensus_state::ConsensusState),
#[cfg(any(test, feature = "mocks"))]
Mock(MockConsensusState),
}
impl AnyConsensusState {
pub fn timestamp(&self) -> Timestamp {
match self {
Self::Tendermint(cs_state) => {
let date: DateTime<Utc> = cs_state.timestamp.into();
Timestamp::from_datetime(date)
}
#[cfg(any(test, feature = "mocks"))]
Self::Mock(mock_state) => mock_state.timestamp(),
}
}
pub fn client_type(&self) -> ClientType {
match self {
AnyConsensusState::Tendermint(_cs) => ClientType::Tendermint,
#[cfg(any(test, feature = "mocks"))]
AnyConsensusState::Mock(_cs) => ClientType::Mock,
}
}
}
impl Protobuf<Any> for AnyConsensusState {}
impl TryFrom<Any> for AnyConsensusState {
type Error = Error;
fn try_from(value: Any) -> Result<Self, Self::Error> {
match value.type_url.as_str() {
"" => Err(Kind::EmptyConsensusStateResponse.into()),
TENDERMINT_CONSENSUS_STATE_TYPE_URL => Ok(AnyConsensusState::Tendermint(
consensus_state::ConsensusState::decode_vec(&value.value)
.map_err(|e| Kind::InvalidRawConsensusState.context(e))?,
)),
#[cfg(any(test, feature = "mocks"))]
MOCK_CONSENSUS_STATE_TYPE_URL => Ok(AnyConsensusState::Mock(
MockConsensusState::decode_vec(&value.value)
.map_err(|e| Kind::InvalidRawConsensusState.context(e))?,
)),
_ => Err(Kind::UnknownConsensusStateType(value.type_url).into()),
}
}
}
impl From<AnyConsensusState> for Any {
fn from(value: AnyConsensusState) -> Self {
match value {
AnyConsensusState::Tendermint(value) => Any {
type_url: TENDERMINT_CONSENSUS_STATE_TYPE_URL.to_string(),
value: value
.encode_vec()
.expect("encoding to `Any` from `AnyConsensusState::Tendermint`"),
},
#[cfg(any(test, feature = "mocks"))]
AnyConsensusState::Mock(value) => Any {
type_url: MOCK_CONSENSUS_STATE_TYPE_URL.to_string(),
value: value
.encode_vec()
.expect("encoding to `Any` from `AnyConsensusState::Mock`"),
},
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct AnyConsensusStateWithHeight {
pub height: Height,
pub consensus_state: AnyConsensusState,
}
impl Protobuf<ConsensusStateWithHeight> for AnyConsensusStateWithHeight {}
impl TryFrom<ConsensusStateWithHeight> for AnyConsensusStateWithHeight {
type Error = Kind;
fn try_from(value: ConsensusStateWithHeight) -> Result<Self, Self::Error> {
let state = value
.consensus_state
.map(AnyConsensusState::try_from)
.transpose()
.map_err(|_| Kind::InvalidRawConsensusState)?
.ok_or(Kind::EmptyConsensusStateResponse)?;
Ok(AnyConsensusStateWithHeight {
height: value
.height
.ok_or(Kind::MissingHeight)?
.try_into()
.unwrap_infallible(),
consensus_state: state,
})
}
}
impl From<AnyConsensusStateWithHeight> for ConsensusStateWithHeight {
fn from(value: AnyConsensusStateWithHeight) -> Self {
ConsensusStateWithHeight {
height: Some(value.height.into()),
consensus_state: Some(value.consensus_state.into()),
}
}
}
impl ConsensusState for AnyConsensusState {
fn client_type(&self) -> ClientType {
self.client_type()
}
fn root(&self) -> &CommitmentRoot {
todo!()
}
fn validate_basic(&self) -> Result<(), Box<dyn std::error::Error>> {
todo!()
}
fn wrap_any(self) -> AnyConsensusState {
self
}
}
#[derive(Clone, Debug)]
pub struct QueryClientEventRequest {
pub height: crate::Height,
pub event_id: IbcEventType,
pub client_id: ClientId,
pub consensus_height: crate::Height,
}