-
Notifications
You must be signed in to change notification settings - Fork 87
/
client_state.rs
409 lines (348 loc) · 13.2 KB
/
client_state.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use core::str::FromStr;
use core::time::Duration;
use ibc::core::client::context::client_state::{
ClientStateCommon, ClientStateExecution, ClientStateValidation,
};
use ibc::core::client::context::{ClientExecutionContext, ClientValidationContext};
use ibc::core::client::types::error::{ClientError, UpgradeClientError};
use ibc::core::client::types::{Height, Status, UpdateKind};
use ibc::core::commitment_types::commitment::{
CommitmentPrefix, CommitmentProofBytes, CommitmentRoot,
};
use ibc::core::handler::types::error::ContextError;
use ibc::core::host::types::identifiers::{ClientId, ClientType};
use ibc::core::host::types::path::{ClientConsensusStatePath, ClientStatePath, Path};
use ibc::core::primitives::prelude::*;
use ibc::core::primitives::Timestamp;
use ibc::primitives::proto::{Any, Protobuf};
use crate::testapp::ibc::clients::mock::client_state::client_type as mock_client_type;
use crate::testapp::ibc::clients::mock::consensus_state::MockConsensusState;
use crate::testapp::ibc::clients::mock::header::MockHeader;
use crate::testapp::ibc::clients::mock::misbehaviour::Misbehaviour;
use crate::testapp::ibc::clients::mock::proto::{
ClientState as RawMockClientState, Header as RawMockHeader,
};
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::from_str(MOCK_CLIENT_TYPE).expect("never fails because it's valid client type")
}
/// A mock of a client state. For an example of a real structure that this mocks, you can see
/// `ClientState` of ics07_tendermint/client_state.rs.
#[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
}
}
pub fn is_frozen(&self) -> bool {
self.frozen_height.is_some()
}
fn expired(&self, _elapsed: Duration) -> bool {
false
}
}
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.expect("Never fails").try_into()?))
}
}
impl From<MockClientState> for RawMockClientState {
fn from(value: MockClientState) -> Self {
RawMockClientState {
header: Some(RawMockHeader {
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> {
fn decode_client_state(value: &[u8]) -> Result<MockClientState, ClientError> {
let client_state =
Protobuf::<RawMockClientState>::decode(value).map_err(|e| ClientError::Other {
description: e.to_string(),
})?;
Ok(client_state)
}
match raw.type_url.as_str() {
MOCK_CLIENT_STATE_TYPE_URL => decode_client_state(&raw.value),
_ => 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),
}
}
}
pub trait MockClientContext {
type ConversionError: ToString;
type AnyConsensusState: TryInto<MockConsensusState, Error = Self::ConversionError>;
/// Returns the current timestamp of the local chain.
fn host_timestamp(&self) -> Result<Timestamp, ContextError>;
/// Returns the current height of the local chain.
fn host_height(&self) -> Result<Height, ContextError>;
/// Retrieve the consensus state for the given client ID at the specified
/// height.
///
/// Returns an error if no such state exists.
fn consensus_state(
&self,
client_cons_state_path: &ClientConsensusStatePath,
) -> Result<Self::AnyConsensusState, ContextError>;
}
impl ClientStateCommon for MockClientState {
fn verify_consensus_state(&self, consensus_state: Any) -> Result<(), ClientError> {
let _mock_consensus_state = MockConsensusState::try_from(consensus_state)?;
Ok(())
}
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 verify_upgrade_client(
&self,
upgraded_client_state: Any,
upgraded_consensus_state: Any,
_proof_upgrade_client: CommitmentProofBytes,
_proof_upgrade_consensus_state: CommitmentProofBytes,
_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(UpgradeClientError::LowUpgradeHeight {
upgraded_height: self.latest_height(),
client_height: upgraded_mock_client_state.latest_height(),
})?;
}
Ok(())
}
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<V> ClientStateValidation<V> for MockClientState
where
V: ClientValidationContext + MockClientContext,
V::AnyConsensusState: TryInto<MockConsensusState>,
ClientError: From<<V::AnyConsensusState as TryInto<MockConsensusState>>::Error>,
{
fn verify_client_message(
&self,
_ctx: &V,
_client_id: &ClientId,
client_message: Any,
update_kind: &UpdateKind,
) -> Result<(), ClientError> {
match update_kind {
UpdateKind::UpdateClient => {
let header = MockHeader::try_from(client_message)?;
if self.latest_height() >= header.height() {
return Err(ClientError::LowHeaderHeight {
header_height: header.height(),
latest_height: self.latest_height(),
});
}
}
UpdateKind::SubmitMisbehaviour => {
let _misbehaviour = Misbehaviour::try_from(client_message)?;
}
}
Ok(())
}
fn check_for_misbehaviour(
&self,
_ctx: &V,
_client_id: &ClientId,
client_message: Any,
update_kind: &UpdateKind,
) -> Result<bool, ClientError> {
match update_kind {
UpdateKind::UpdateClient => Ok(false),
UpdateKind::SubmitMisbehaviour => {
let misbehaviour = Misbehaviour::try_from(client_message)?;
let header_1 = misbehaviour.header1;
let header_2 = misbehaviour.header2;
let header_heights_equal = header_1.height() == header_2.height();
let headers_are_in_future = self.latest_height() < header_1.height();
Ok(header_heights_equal && headers_are_in_future)
}
}
}
fn status(&self, ctx: &V, client_id: &ClientId) -> Result<Status, ClientError> {
if self.is_frozen() {
return Ok(Status::Frozen);
}
let latest_consensus_state: MockConsensusState = {
let any_latest_consensus_state =
match ctx.consensus_state(&ClientConsensusStatePath::new(
client_id.clone(),
self.latest_height().revision_number(),
self.latest_height().revision_height(),
)) {
Ok(cs) => cs,
// if the client state does not have an associated consensus state for its latest height
// then it must be expired
Err(_) => return Ok(Status::Expired),
};
any_latest_consensus_state.try_into()?
};
let now = ctx.host_timestamp()?;
let elapsed_since_latest_consensus_state = now
.duration_since(&latest_consensus_state.timestamp())
.ok_or(ClientError::Other {
description: format!("latest consensus state is in the future. now: {now}, latest consensus state: {}", latest_consensus_state.timestamp()),
})?;
if self.expired(elapsed_since_latest_consensus_state) {
return Ok(Status::Expired);
}
Ok(Status::Active)
}
}
impl<E> ClientStateExecution<E> for MockClientState
where
E: ClientExecutionContext + MockClientContext,
<E as ClientExecutionContext>::AnyClientState: From<MockClientState>,
<E as ClientExecutionContext>::AnyConsensusState: From<MockConsensusState>,
{
fn initialise(
&self,
ctx: &mut E,
client_id: &ClientId,
consensus_state: Any,
) -> Result<(), ClientError> {
let mock_consensus_state = MockConsensusState::try_from(consensus_state)?;
ctx.store_client_state(ClientStatePath::new(client_id), (*self).into())?;
ctx.store_consensus_state(
ClientConsensusStatePath::new(
client_id.clone(),
self.latest_height().revision_number(),
self.latest_height().revision_height(),
),
mock_consensus_state.into(),
)?;
Ok(())
}
fn update_state(
&self,
ctx: &mut E,
client_id: &ClientId,
header: Any,
) -> Result<Vec<Height>, ClientError> {
let header = MockHeader::try_from(header)?;
let header_height = header.height;
let new_client_state = MockClientState::new(header);
let new_consensus_state = MockConsensusState::new(header);
ctx.store_consensus_state(
ClientConsensusStatePath::new(
client_id.clone(),
new_client_state.latest_height().revision_number(),
new_client_state.latest_height().revision_height(),
),
new_consensus_state.into(),
)?;
ctx.store_client_state(ClientStatePath::new(client_id), new_client_state.into())?;
ctx.store_update_time(client_id.clone(), header_height, ctx.host_timestamp()?)?;
ctx.store_update_height(client_id.clone(), header_height, ctx.host_height()?)?;
Ok(vec![header_height])
}
fn update_state_on_misbehaviour(
&self,
ctx: &mut E,
client_id: &ClientId,
_client_message: Any,
_update_kind: &UpdateKind,
) -> Result<(), ClientError> {
let frozen_client_state = self.with_frozen_height(Height::min(0));
ctx.store_client_state(ClientStatePath::new(client_id), frozen_client_state.into())?;
Ok(())
}
fn update_state_on_upgrade(
&self,
ctx: &mut E,
client_id: &ClientId,
upgraded_client_state: Any,
upgraded_consensus_state: Any,
) -> Result<Height, ClientError> {
let new_client_state = MockClientState::try_from(upgraded_client_state)?;
let new_consensus_state = MockConsensusState::try_from(upgraded_consensus_state)?;
let latest_height = new_client_state.latest_height();
ctx.store_consensus_state(
ClientConsensusStatePath::new(
client_id.clone(),
latest_height.revision_number(),
latest_height.revision_height(),
),
new_consensus_state.into(),
)?;
ctx.store_client_state(ClientStatePath::new(client_id), new_client_state.into())?;
let host_timestamp = ctx.host_timestamp()?;
let host_height = ctx.host_height()?;
ctx.store_update_time(client_id.clone(), latest_height, host_timestamp)?;
ctx.store_update_height(client_id.clone(), latest_height, host_height)?;
Ok(latest_height)
}
}
impl From<MockConsensusState> for MockClientState {
fn from(cs: MockConsensusState) -> Self {
Self::new(cs.header)
}
}