-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathclient_def.rs
466 lines (403 loc) · 14.5 KB
/
client_def.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use serde_derive::{Deserialize, Serialize};
use crate::downcast;
use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::error;
use crate::ics02_client::header::Header;
use crate::ics02_client::state::{ClientState, ConsensusState};
use crate::ics03_connection::connection::ConnectionEnd;
use crate::ics07_tendermint as tendermint;
use crate::ics07_tendermint::client_def::TendermintClient;
use crate::ics07_tendermint::client_state::ClientState as TendermintClientState;
use crate::ics07_tendermint::consensus_state::ConsensusState as TendermintConsensusState;
use crate::ics23_commitment::commitment::{CommitmentPrefix, CommitmentProof, CommitmentRoot};
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use ::tendermint::block::Height;
use prost_types::Any;
use std::convert::TryFrom;
use tendermint_proto::{DomainType, Error, Kind};
#[cfg(test)]
use {
crate::mock_client::client_def::MockClient,
crate::mock_client::header::MockHeader,
crate::mock_client::state::{MockClientState, MockConsensusState},
};
pub trait ClientDef: Clone {
type Header: Header;
type ClientState: ClientState;
type ConsensusState: ConsensusState;
/// TODO
fn check_header_and_update_state(
&self,
client_state: Self::ClientState,
header: Self::Header,
) -> Result<(Self::ClientState, Self::ConsensusState), Box<dyn std::error::Error>>;
/// Verification functions as specified in:
/// https://github.com/cosmos/ics/tree/master/spec/ics-002-client-semantics
///
/// Verify a `proof` that the consensus state of a given client (at height `consensus_height`)
/// matches the input `consensus_state`. The parameter `counterparty_height` represent the
/// height of the counterparty chain that this proof assumes (i.e., the height at which this
/// proof was computed).
#[allow(clippy::too_many_arguments)]
fn verify_client_consensus_state(
&self,
client_state: &Self::ClientState,
height: Height,
prefix: &CommitmentPrefix,
proof: &CommitmentProof,
client_id: &ClientId,
consensus_height: Height,
expected_consensus_state: &AnyConsensusState,
) -> Result<(), Box<dyn std::error::Error>>;
/// Verify a `proof` that a connection state matches that of the input `connection_end`.
fn verify_connection_state(
&self,
client_state: &Self::ClientState,
height: Height,
prefix: &CommitmentPrefix,
proof: &CommitmentProof,
connection_id: &ConnectionId,
expected_connection_end: &ConnectionEnd,
) -> Result<(), Box<dyn std::error::Error>>;
/// Verify the client state for this chain that it is stored on the counterparty chain.
#[allow(clippy::too_many_arguments)]
fn verify_client_full_state(
&self,
_client_state: &Self::ClientState,
height: Height,
root: &CommitmentRoot,
prefix: &CommitmentPrefix,
client_id: &ClientId,
proof: &CommitmentProof,
client_state: &AnyClientState,
) -> Result<(), Box<dyn std::error::Error>>;
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] // TODO: Add Eq
#[allow(clippy::large_enum_variant)]
pub enum AnyHeader {
Tendermint(tendermint::header::Header),
#[cfg(test)]
Mock(MockHeader),
}
impl Header for AnyHeader {
fn client_type(&self) -> ClientType {
match self {
Self::Tendermint(header) => header.client_type(),
#[cfg(test)]
Self::Mock(header) => header.client_type(),
}
}
fn height(&self) -> Height {
match self {
Self::Tendermint(header) => header.height(),
#[cfg(test)]
Self::Mock(header) => header.height(),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum AnyClientState {
Tendermint(TendermintClientState),
#[cfg(test)]
Mock(MockClientState),
}
impl DomainType<Any> for AnyClientState {}
impl TryFrom<Any> for AnyClientState {
type Error = Error;
// TODO Fix type urls: avoid having hardcoded values sprinkled around the whole codebase.
fn try_from(raw: Any) -> Result<Self, Self::Error> {
match raw.type_url.as_str() {
"/ibc.tendermint.ClientState" => Ok(AnyClientState::Tendermint(
TendermintClientState::decode_vec(&raw.value)?,
)),
#[cfg(test)]
"/ibc.mock.ClientState" => Ok(AnyClientState::Mock(MockClientState::decode_vec(
&raw.value,
)?)),
_ => Err(Kind::DecodeMessage
.context(error::Kind::UnknownClientStateType(raw.type_url))
.into()),
}
}
}
impl From<AnyClientState> for Any {
fn from(value: AnyClientState) -> Self {
match value {
AnyClientState::Tendermint(value) => Any {
type_url: "/ibc.tendermint.ClientState".to_string(),
value: value.encode_vec().unwrap(),
},
#[cfg(test)]
AnyClientState::Mock(value) => Any {
type_url: "/ibc.mock.ClientState".to_string(),
value: value.encode_vec().unwrap(),
},
}
}
}
impl ClientState for AnyClientState {
fn chain_id(&self) -> String {
todo!()
}
fn client_type(&self) -> ClientType {
match self {
Self::Tendermint(state) => state.client_type(),
#[cfg(test)]
Self::Mock(state) => state.client_type(),
}
}
fn latest_height(&self) -> Height {
match self {
Self::Tendermint(tm_state) => tm_state.latest_height(),
#[cfg(test)]
Self::Mock(mock_state) => mock_state.latest_height(),
}
}
fn is_frozen(&self) -> bool {
match self {
AnyClientState::Tendermint(tm_state) => tm_state.is_frozen(),
#[cfg(test)]
AnyClientState::Mock(mock_state) => mock_state.is_frozen(),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum AnyConsensusState {
Tendermint(crate::ics07_tendermint::consensus_state::ConsensusState),
#[cfg(test)]
Mock(MockConsensusState),
}
impl DomainType<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() {
"/ibc.tendermint.ConsensusState" => Ok(AnyConsensusState::Tendermint(
TendermintConsensusState::decode_vec(&value.value)?,
)),
// TODO get this to compile! -- Add the ClientConsensusState definition in ibc-proto.
// #[cfg(test)]
// "/ibc.mock.ConsensusState" => {
// let raw = RawMockConsensusState::decode(value.value.as_ref())
// .map_err(|e| error::Kind::ProtoDecodingFailure.context(e))?;
// let client_state = MockClientState::try_from(raw)
// .map_err(|e| error::Kind::InvalidRawClientState.context(e))?;
//
// Ok(AnyClientState::Mock(client_state))
// }
_ => Err(Kind::DecodeMessage
.context(error::Kind::UnknownConsensusStateType(value.type_url))
.into()),
}
}
}
impl From<AnyConsensusState> for Any {
fn from(value: AnyConsensusState) -> Self {
match value {
AnyConsensusState::Tendermint(value) => Any {
type_url: "/ibc.tendermint.ConsensusState".to_string(),
value: value.encode_vec().unwrap(),
},
#[cfg(test)]
AnyConsensusState::Mock(value) => Any {
type_url: "/ibc.mock.ConsensusState".to_string(),
value: value.encode_vec().unwrap(),
},
}
}
}
impl ConsensusState for AnyConsensusState {
fn client_type(&self) -> ClientType {
todo!()
}
fn height(&self) -> Height {
todo!()
}
fn root(&self) -> &CommitmentRoot {
todo!()
}
fn validate_basic(&self) -> Result<(), Box<dyn std::error::Error>> {
todo!()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AnyClient {
Tendermint(TendermintClient),
#[cfg(test)]
Mock(MockClient),
}
impl AnyClient {
pub fn from_client_type(client_type: ClientType) -> AnyClient {
match client_type {
ClientType::Tendermint => Self::Tendermint(TendermintClient),
#[cfg(test)]
ClientType::Mock => Self::Mock(MockClient),
}
}
}
// ⚠️ Beware of the awful boilerplate below ⚠️
impl ClientDef for AnyClient {
type Header = AnyHeader;
type ClientState = AnyClientState;
type ConsensusState = AnyConsensusState;
fn check_header_and_update_state(
&self,
client_state: AnyClientState,
header: AnyHeader,
) -> Result<(AnyClientState, AnyConsensusState), Box<dyn std::error::Error>> {
match self {
Self::Tendermint(client) => {
let (client_state, header) = downcast!(
client_state => AnyClientState::Tendermint,
header => AnyHeader::Tendermint,
)
.ok_or_else(|| error::Kind::ClientArgsTypeMismatch(ClientType::Tendermint))?;
let (new_state, new_consensus) =
client.check_header_and_update_state(client_state, header)?;
Ok((
AnyClientState::Tendermint(new_state),
AnyConsensusState::Tendermint(new_consensus),
))
}
#[cfg(test)]
Self::Mock(client) => {
let (client_state, header) = downcast!(
client_state => AnyClientState::Mock,
header => AnyHeader::Mock,
)
.ok_or_else(|| error::Kind::ClientArgsTypeMismatch(ClientType::Mock))?;
let (new_state, new_consensus) =
client.check_header_and_update_state(client_state, header)?;
Ok((
AnyClientState::Mock(new_state),
AnyConsensusState::Mock(new_consensus),
))
}
}
}
fn verify_client_consensus_state(
&self,
client_state: &Self::ClientState,
height: Height,
prefix: &CommitmentPrefix,
proof: &CommitmentProof,
client_id: &ClientId,
consensus_height: Height,
expected_consensus_state: &AnyConsensusState,
) -> Result<(), Box<dyn std::error::Error>> {
match self {
Self::Tendermint(client) => {
let client_state = downcast!(
client_state => AnyClientState::Tendermint
)
.ok_or_else(|| error::Kind::ClientArgsTypeMismatch(ClientType::Tendermint))?;
client.verify_client_consensus_state(
client_state,
height,
prefix,
proof,
client_id,
consensus_height,
expected_consensus_state,
)
}
#[cfg(test)]
Self::Mock(client) => {
let client_state = downcast!(
client_state => AnyClientState::Mock
)
.ok_or_else(|| error::Kind::ClientArgsTypeMismatch(ClientType::Mock))?;
client.verify_client_consensus_state(
client_state,
height,
prefix,
proof,
client_id,
consensus_height,
expected_consensus_state,
)
}
}
}
fn verify_connection_state(
&self,
client_state: &AnyClientState,
height: Height,
prefix: &CommitmentPrefix,
proof: &CommitmentProof,
connection_id: &ConnectionId,
expected_connection_end: &ConnectionEnd,
) -> Result<(), Box<dyn std::error::Error>> {
match self {
Self::Tendermint(client) => {
let client_state = downcast!(client_state => AnyClientState::Tendermint)
.ok_or_else(|| error::Kind::ClientArgsTypeMismatch(ClientType::Tendermint))?;
client.verify_connection_state(
client_state,
height,
prefix,
proof,
connection_id,
expected_connection_end,
)
}
#[cfg(test)]
Self::Mock(client) => {
let client_state = downcast!(client_state => AnyClientState::Mock)
.ok_or_else(|| error::Kind::ClientArgsTypeMismatch(ClientType::Mock))?;
client.verify_connection_state(
client_state,
height,
prefix,
proof,
connection_id,
expected_connection_end,
)
}
}
}
fn verify_client_full_state(
&self,
client_state: &Self::ClientState,
height: Height,
root: &CommitmentRoot,
prefix: &CommitmentPrefix,
client_id: &ClientId,
proof: &CommitmentProof,
client_state_on_counterparty: &AnyClientState,
) -> Result<(), Box<dyn std::error::Error>> {
match self {
Self::Tendermint(client) => {
let client_state = downcast!(
client_state => AnyClientState::Tendermint
)
.ok_or_else(|| error::Kind::ClientArgsTypeMismatch(ClientType::Tendermint))?;
client.verify_client_full_state(
client_state,
height,
root,
prefix,
client_id,
proof,
client_state_on_counterparty,
)
}
#[cfg(test)]
Self::Mock(client) => {
let client_state = downcast!(
client_state => AnyClientState::Mock
)
.ok_or_else(|| error::Kind::ClientArgsTypeMismatch(ClientType::Mock))?;
client.verify_client_full_state(
client_state,
height,
root,
prefix,
client_id,
proof,
client_state_on_counterparty,
)
}
}
}
}