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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use crate::prelude::*;
use alloc::collections::btree_map::BTreeMap as HashMap;
use core::time::Duration;
use ibc_proto::ibc::core::commitment::v1::MerkleProof;
use ibc_proto::google::protobuf::Any;
use ibc_proto::ibc::mock::ClientState as RawMockClientState;
use ibc_proto::protobuf::Protobuf;
use crate::core::ics02_client::client_state::{ClientState, UpdatedState};
use crate::core::ics02_client::client_type::ClientType;
use crate::core::ics02_client::consensus_state::ConsensusState;
use crate::core::ics02_client::error::ClientError;
use crate::core::ics23_commitment::commitment::{
CommitmentPrefix, CommitmentProofBytes, CommitmentRoot,
};
use crate::core::ics24_host::identifier::{ChainId, ClientId};
use crate::core::ics24_host::Path;
use crate::mock::client_state::client_type as mock_client_type;
use crate::mock::consensus_state::MockConsensusState;
use crate::mock::header::MockHeader;
use crate::mock::misbehaviour::Misbehaviour;
use crate::Height;
use crate::core::{ContextError, ValidationContext};
pub const MOCK_CLIENT_STATE_TYPE_URL: &str = "/ibc.mock.ClientState";
pub const MOCK_CLIENT_TYPE: &str = "9999-mock";
pub fn client_type() -> ClientType {
ClientType::new(MOCK_CLIENT_TYPE.to_string())
}
#[derive(Clone, Debug)]
pub struct MockClientRecord {
pub client_type: ClientType,
pub client_state: Option<Box<dyn ClientState>>,
pub consensus_states: HashMap<Height, Box<dyn ConsensusState>>,
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct MockClientState {
pub header: MockHeader,
pub frozen_height: Option<Height>,
}
impl MockClientState {
pub fn new(header: MockHeader) -> Self {
Self {
header,
frozen_height: None,
}
}
pub fn latest_height(&self) -> Height {
self.header.height()
}
pub fn refresh_time(&self) -> Option<Duration> {
None
}
pub fn with_frozen_height(self, frozen_height: Height) -> Self {
Self {
frozen_height: Some(frozen_height),
..self
}
}
}
impl Protobuf<RawMockClientState> for MockClientState {}
impl TryFrom<RawMockClientState> for MockClientState {
type Error = ClientError;
fn try_from(raw: RawMockClientState) -> Result<Self, Self::Error> {
Ok(Self::new(raw.header.unwrap().try_into()?))
}
}
impl From<MockClientState> for RawMockClientState {
fn from(value: MockClientState) -> Self {
RawMockClientState {
header: Some(ibc_proto::ibc::mock::Header {
height: Some(value.header.height().into()),
timestamp: value.header.timestamp.nanoseconds(),
}),
}
}
}
impl Protobuf<Any> for MockClientState {}
impl TryFrom<Any> for MockClientState {
type Error = ClientError;
fn try_from(raw: Any) -> Result<Self, Self::Error> {
use bytes::Buf;
use core::ops::Deref;
use prost::Message;
fn decode_client_state<B: Buf>(buf: B) -> Result<MockClientState, ClientError> {
RawMockClientState::decode(buf)
.map_err(ClientError::Decode)?
.try_into()
}
match raw.type_url.as_str() {
MOCK_CLIENT_STATE_TYPE_URL => {
decode_client_state(raw.value.deref()).map_err(Into::into)
}
_ => Err(ClientError::UnknownClientStateType {
client_state_type: raw.type_url,
}),
}
}
}
impl From<MockClientState> for Any {
fn from(client_state: MockClientState) -> Self {
Any {
type_url: MOCK_CLIENT_STATE_TYPE_URL.to_string(),
value: Protobuf::<RawMockClientState>::encode_vec(&client_state)
.expect("encoding to `Any` from `MockClientState`"),
}
}
}
impl ClientState for MockClientState {
fn chain_id(&self) -> ChainId {
unimplemented!()
}
fn client_type(&self) -> ClientType {
mock_client_type()
}
fn latest_height(&self) -> Height {
self.header.height()
}
fn validate_proof_height(&self, proof_height: Height) -> Result<(), ClientError> {
if self.latest_height() < proof_height {
return Err(ClientError::InvalidProofHeight {
latest_height: self.latest_height(),
proof_height,
});
}
Ok(())
}
fn confirm_not_frozen(&self) -> Result<(), ClientError> {
if let Some(frozen_height) = self.frozen_height {
return Err(ClientError::ClientFrozen {
description: format!("The client is frozen at height {frozen_height}"),
});
}
Ok(())
}
fn zero_custom_fields(&mut self) {
unimplemented!()
}
fn expired(&self, _elapsed: Duration) -> bool {
false
}
fn initialise(&self, consensus_state: Any) -> Result<Box<dyn ConsensusState>, ClientError> {
MockConsensusState::try_from(consensus_state).map(MockConsensusState::into_box)
}
fn check_header_and_update_state(
&self,
_ctx: &dyn ValidationContext,
_client_id: ClientId,
header: Any,
) -> Result<UpdatedState, ClientError> {
let header = MockHeader::try_from(header)?;
if self.latest_height() >= header.height() {
return Err(ClientError::LowHeaderHeight {
header_height: header.height(),
latest_height: self.latest_height(),
});
}
Ok(UpdatedState {
client_state: MockClientState::new(header).into_box(),
consensus_state: MockConsensusState::new(header).into_box(),
})
}
fn check_misbehaviour_and_update_state(
&self,
_ctx: &dyn ValidationContext,
_client_id: ClientId,
misbehaviour: Any,
) -> Result<Box<dyn ClientState>, ContextError> {
let misbehaviour = Misbehaviour::try_from(misbehaviour)?;
let header_1 = misbehaviour.header1;
let header_2 = misbehaviour.header2;
if header_1.height() != header_2.height() {
return Err(ClientError::InvalidHeight.into());
}
if self.latest_height() >= header_1.height() {
return Err(ClientError::LowHeaderHeight {
header_height: header_1.height(),
latest_height: self.latest_height(),
}
.into());
}
let new_state =
MockClientState::new(header_1).with_frozen_height(Height::new(0, 1).unwrap());
Ok(new_state.into_box())
}
fn verify_upgrade_client(
&self,
upgraded_client_state: Any,
upgraded_consensus_state: Any,
_proof_upgrade_client: MerkleProof,
_proof_upgrade_consensus_state: MerkleProof,
_root: &CommitmentRoot,
) -> Result<(), ClientError> {
let upgraded_mock_client_state = MockClientState::try_from(upgraded_client_state)?;
MockConsensusState::try_from(upgraded_consensus_state)?;
if self.latest_height() >= upgraded_mock_client_state.latest_height() {
return Err(ClientError::LowUpgradeHeight {
upgraded_height: self.latest_height(),
client_height: upgraded_mock_client_state.latest_height(),
});
}
Ok(())
}
fn update_state_with_upgrade_client(
&self,
upgraded_client_state: Any,
upgraded_consensus_state: Any,
) -> Result<UpdatedState, ClientError> {
let mock_client_state = MockClientState::try_from(upgraded_client_state)?;
let mock_consensus_state = MockConsensusState::try_from(upgraded_consensus_state)?;
Ok(UpdatedState {
client_state: mock_client_state.into_box(),
consensus_state: mock_consensus_state.into_box(),
})
}
fn verify_membership(
&self,
_prefix: &CommitmentPrefix,
_proof: &CommitmentProofBytes,
_root: &CommitmentRoot,
_path: Path,
_value: Vec<u8>,
) -> Result<(), ClientError> {
Ok(())
}
fn verify_non_membership(
&self,
_prefix: &CommitmentPrefix,
_proof: &CommitmentProofBytes,
_root: &CommitmentRoot,
_path: Path,
) -> Result<(), ClientError> {
Ok(())
}
}
impl From<MockConsensusState> for MockClientState {
fn from(cs: MockConsensusState) -> Self {
Self::new(cs.header)
}
}