-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathevents.rs
511 lines (458 loc) · 14.9 KB
/
events.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! Types for the IBC events emitted from Tendermint Websocket by the client module.
use crate::prelude::*;
use derive_more::From;
use ibc_proto::google::protobuf::Any;
use subtle_encoding::hex;
use tendermint::abci;
use crate::core::ics02_client::client_type::ClientType;
use crate::core::ics02_client::height::Height;
use crate::core::ics24_host::identifier::ClientId;
/// Client event types
const CREATE_CLIENT_EVENT: &str = "create_client";
const UPDATE_CLIENT_EVENT: &str = "update_client";
const CLIENT_MISBEHAVIOUR_EVENT: &str = "client_misbehaviour";
const UPGRADE_CLIENT_EVENT: &str = "upgrade_client";
/// The content of the `key` field for the attribute containing the client identifier.
pub const CLIENT_ID_ATTRIBUTE_KEY: &str = "client_id";
/// The content of the `key` field for the attribute containing the client type.
pub const CLIENT_TYPE_ATTRIBUTE_KEY: &str = "client_type";
/// The content of the `key` field for the attribute containing the height.
pub const CONSENSUS_HEIGHT_ATTRIBUTE_KEY: &str = "consensus_height";
/// The content of the `key` field for the attribute containing the heights of consensus states that were processed.
pub const CONSENSUS_HEIGHTS_ATTRIBUTE_KEY: &str = "consensus_heights";
/// The content of the `key` field for the header in update client event.
pub const HEADER_ATTRIBUTE_KEY: &str = "header";
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, From, PartialEq, Eq)]
struct ClientIdAttribute {
client_id: ClientId,
}
impl From<ClientIdAttribute> for abci::EventAttribute {
fn from(attr: ClientIdAttribute) -> Self {
(CLIENT_ID_ATTRIBUTE_KEY, attr.client_id.as_str()).into()
}
}
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, From, PartialEq, Eq)]
struct ClientTypeAttribute {
client_type: ClientType,
}
impl From<ClientTypeAttribute> for abci::EventAttribute {
fn from(attr: ClientTypeAttribute) -> Self {
(CLIENT_TYPE_ATTRIBUTE_KEY, attr.client_type.as_str()).into()
}
}
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, From, PartialEq, Eq)]
struct ConsensusHeightAttribute {
consensus_height: Height,
}
impl From<ConsensusHeightAttribute> for abci::EventAttribute {
fn from(attr: ConsensusHeightAttribute) -> Self {
(CONSENSUS_HEIGHT_ATTRIBUTE_KEY, attr.consensus_height).into()
}
}
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, From, PartialEq, Eq)]
struct ConsensusHeightsAttribute {
consensus_heights: Vec<Height>,
}
impl From<ConsensusHeightsAttribute> for abci::EventAttribute {
fn from(attr: ConsensusHeightsAttribute) -> Self {
let consensus_heights: Vec<String> = attr
.consensus_heights
.into_iter()
.map(|consensus_height| consensus_height.to_string())
.collect();
(CONSENSUS_HEIGHTS_ATTRIBUTE_KEY, consensus_heights.join(",")).into()
}
}
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, From, PartialEq, Eq)]
struct HeaderAttribute {
header: Any,
}
impl From<HeaderAttribute> for abci::EventAttribute {
fn from(attr: HeaderAttribute) -> Self {
(
HEADER_ATTRIBUTE_KEY,
String::from_utf8(hex::encode(attr.header.value)).unwrap(),
)
.into()
}
}
/// CreateClient event signals the creation of a new on-chain client (IBC client).
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CreateClient {
client_id: ClientIdAttribute,
client_type: ClientTypeAttribute,
consensus_height: ConsensusHeightAttribute,
}
impl CreateClient {
pub fn new(client_id: ClientId, client_type: ClientType, consensus_height: Height) -> Self {
Self {
client_id: ClientIdAttribute::from(client_id),
client_type: ClientTypeAttribute::from(client_type),
consensus_height: ConsensusHeightAttribute::from(consensus_height),
}
}
pub fn client_id(&self) -> &ClientId {
&self.client_id.client_id
}
pub fn client_type(&self) -> &ClientType {
&self.client_type.client_type
}
pub fn consensus_height(&self) -> &Height {
&self.consensus_height.consensus_height
}
pub fn event_type(&self) -> &str {
CREATE_CLIENT_EVENT
}
}
impl From<CreateClient> for abci::Event {
fn from(c: CreateClient) -> Self {
Self {
kind: CREATE_CLIENT_EVENT.to_owned(),
attributes: vec![
c.client_id.into(),
c.client_type.into(),
c.consensus_height.into(),
],
}
}
}
/// UpdateClient event signals a recent update of an on-chain client (IBC Client).
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UpdateClient {
client_id: ClientIdAttribute,
client_type: ClientTypeAttribute,
// Deprecated: consensus_height is deprecated and will be removed in a future release.
// Please use consensus_heights instead.
consensus_height: ConsensusHeightAttribute,
consensus_heights: ConsensusHeightsAttribute,
header: HeaderAttribute,
}
impl UpdateClient {
pub fn new(
client_id: ClientId,
client_type: ClientType,
consensus_height: Height,
consensus_heights: Vec<Height>,
header: Any,
) -> Self {
Self {
client_id: ClientIdAttribute::from(client_id),
client_type: ClientTypeAttribute::from(client_type),
consensus_height: ConsensusHeightAttribute::from(consensus_height),
consensus_heights: ConsensusHeightsAttribute::from(consensus_heights),
header: HeaderAttribute::from(header),
}
}
pub fn client_id(&self) -> &ClientId {
&self.client_id.client_id
}
pub fn client_type(&self) -> &ClientType {
&self.client_type.client_type
}
pub fn consensus_height(&self) -> &Height {
&self.consensus_height.consensus_height
}
pub fn consensus_heights(&self) -> &[Height] {
self.consensus_heights.consensus_heights.as_ref()
}
pub fn header(&self) -> &Any {
&self.header.header
}
pub fn event_type(&self) -> &str {
UPDATE_CLIENT_EVENT
}
}
impl From<UpdateClient> for abci::Event {
fn from(u: UpdateClient) -> Self {
Self {
kind: UPDATE_CLIENT_EVENT.to_owned(),
attributes: vec![
u.client_id.into(),
u.client_type.into(),
u.consensus_height.into(),
u.consensus_heights.into(),
u.header.into(),
],
}
}
}
/// ClientMisbehaviour event signals the update of an on-chain client (IBC Client) with evidence of
/// misbehaviour.
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClientMisbehaviour {
client_id: ClientIdAttribute,
client_type: ClientTypeAttribute,
}
impl ClientMisbehaviour {
pub fn new(client_id: ClientId, client_type: ClientType) -> Self {
Self {
client_id: ClientIdAttribute::from(client_id),
client_type: ClientTypeAttribute::from(client_type),
}
}
pub fn client_id(&self) -> &ClientId {
&self.client_id.client_id
}
pub fn client_type(&self) -> &ClientType {
&self.client_type.client_type
}
pub fn event_type(&self) -> &str {
CLIENT_MISBEHAVIOUR_EVENT
}
}
impl From<ClientMisbehaviour> for abci::Event {
fn from(c: ClientMisbehaviour) -> Self {
Self {
kind: CLIENT_MISBEHAVIOUR_EVENT.to_owned(),
attributes: vec![c.client_id.into(), c.client_type.into()],
}
}
}
/// Signals a recent upgrade of an on-chain client (IBC Client).
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UpgradeClient {
client_id: ClientIdAttribute,
client_type: ClientTypeAttribute,
consensus_height: ConsensusHeightAttribute,
}
impl UpgradeClient {
pub fn new(client_id: ClientId, client_type: ClientType, consensus_height: Height) -> Self {
Self {
client_id: ClientIdAttribute::from(client_id),
client_type: ClientTypeAttribute::from(client_type),
consensus_height: ConsensusHeightAttribute::from(consensus_height),
}
}
pub fn client_id(&self) -> &ClientId {
&self.client_id.client_id
}
pub fn client_type(&self) -> &ClientType {
&self.client_type.client_type
}
pub fn consensus_height(&self) -> &Height {
&self.consensus_height.consensus_height
}
pub fn event_type(&self) -> &str {
UPGRADE_CLIENT_EVENT
}
}
impl From<UpgradeClient> for abci::Event {
fn from(u: UpgradeClient) -> Self {
Self {
kind: UPGRADE_CLIENT_EVENT.to_owned(),
attributes: vec![
u.client_id.into(),
u.client_type.into(),
u.consensus_height.into(),
],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::timestamp::Timestamp;
use crate::mock::header::MockHeader;
use ibc_proto::google::protobuf::Any;
use tendermint::abci::Event as AbciEvent;
#[test]
fn ibc_to_abci_client_events() {
struct Test {
event_kind: &'static str,
event: AbciEvent,
expected_keys: Vec<&'static str>,
expected_values: Vec<&'static str>,
}
let client_type = ClientType::from("07-tendermint".to_string());
let client_id = ClientId::new(client_type.clone(), 0).unwrap();
let consensus_height = Height::new(0, 5).unwrap();
let consensus_heights = vec![Height::new(0, 5).unwrap(), Height::new(0, 7).unwrap()];
let header: Any = MockHeader::new(consensus_height)
.with_timestamp(Timestamp::none())
.into();
let expected_keys = vec![
"client_id",
"client_type",
"consensus_height",
"consensus_heights",
"header",
];
let expected_values = vec![
"07-tendermint-0",
"07-tendermint",
"0-5",
"0-5,0-7",
"0a021005",
];
let tests: Vec<Test> = vec![
Test {
event_kind: CREATE_CLIENT_EVENT,
event: CreateClient::new(client_id.clone(), client_type.clone(), consensus_height)
.into(),
expected_keys: expected_keys[0..3].to_vec(),
expected_values: expected_values[0..3].to_vec(),
},
Test {
event_kind: UPDATE_CLIENT_EVENT,
event: UpdateClient::new(
client_id.clone(),
client_type.clone(),
consensus_height,
consensus_heights,
header,
)
.into(),
expected_keys: expected_keys.clone(),
expected_values: expected_values.clone(),
},
Test {
event_kind: UPGRADE_CLIENT_EVENT,
event: UpgradeClient::new(client_id.clone(), client_type.clone(), consensus_height)
.into(),
expected_keys: expected_keys[0..3].to_vec(),
expected_values: expected_values[0..3].to_vec(),
},
Test {
event_kind: CLIENT_MISBEHAVIOUR_EVENT,
event: ClientMisbehaviour::new(client_id, client_type).into(),
expected_keys: expected_keys[0..2].to_vec(),
expected_values: expected_values[0..2].to_vec(),
},
];
for t in tests {
assert_eq!(t.event.kind, t.event_kind);
assert_eq!(t.expected_keys.len(), t.event.attributes.len());
for (i, e) in t.event.attributes.iter().enumerate() {
assert_eq!(
e.key, t.expected_keys[i],
"key mismatch for {:?}",
t.event_kind
);
}
for (i, e) in t.event.attributes.iter().enumerate() {
assert_eq!(
e.value, t.expected_values[i],
"value mismatch for {:?}",
t.event_kind
);
}
}
}
}