-
Notifications
You must be signed in to change notification settings - Fork 332
/
events.rs
596 lines (536 loc) · 23.5 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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use crate::prelude::*;
use alloc::collections::btree_map::BTreeMap as HashMap;
use core::convert::{TryFrom, TryInto};
use core::fmt;
use core::str::FromStr;
use flex_error::{define_error, TraceError};
use prost::alloc::fmt::Formatter;
use serde_derive::{Deserialize, Serialize};
use tendermint::abci::tag::Tag;
use tendermint::abci::Event as AbciEvent;
use crate::core::ics02_client::error as client_error;
use crate::core::ics02_client::events as ClientEvents;
use crate::core::ics02_client::events::NewBlock;
use crate::core::ics02_client::height::HeightError;
use crate::core::ics03_connection::events as ConnectionEvents;
use crate::core::ics03_connection::events::Attributes as ConnectionAttributes;
use crate::core::ics04_channel::error as channel_error;
use crate::core::ics04_channel::events as ChannelEvents;
use crate::core::ics04_channel::events::Attributes as ChannelAttributes;
use crate::core::ics04_channel::packet::Packet;
use crate::core::ics24_host::error::ValidationError;
use crate::core::ics26_routing::context::ModuleId;
use crate::timestamp::ParseTimestampError;
use crate::Height;
define_error! {
Error {
Height
[ HeightError ]
| _ | { "error parsing height" },
Parse
[ ValidationError ]
| _ | { "parse error" },
Client
[ client_error::Error ]
| _ | { "ICS02 client error" },
Channel
[ channel_error::Error ]
| _ | { "channel error" },
Timestamp
[ ParseTimestampError ]
| _ | { "error parsing timestamp" },
MissingKey
{ key: String }
| e | { format_args!("missing event key {}", e.key) },
Decode
[ TraceError<prost::DecodeError> ]
| _ | { "error decoding protobuf" },
SubtleEncoding
[ TraceError<subtle_encoding::Error> ]
| _ | { "error decoding hex" },
MissingActionString
| _ | { "missing action string" },
IncorrectEventType
{ event: String }
| e | { format_args!("incorrect event type: {}", e.event) },
MalformedModuleEvent
{ event: ModuleEvent }
| e | { format_args!("module event cannot use core event types: {:?}", e.event) },
}
}
/// Events whose data is not included in the app state and must be extracted using tendermint RPCs
/// (i.e. /tx_search or /block_search)
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum WithBlockDataType {
CreateClient,
UpdateClient,
SendPacket,
WriteAck,
}
impl WithBlockDataType {
pub fn as_str(&self) -> &'static str {
match *self {
WithBlockDataType::CreateClient => "create_client",
WithBlockDataType::UpdateClient => "update_client",
WithBlockDataType::SendPacket => "send_packet",
WithBlockDataType::WriteAck => "write_acknowledgement",
}
}
}
const NEW_BLOCK_EVENT: &str = "new_block";
const EMPTY_EVENT: &str = "empty";
const CHAIN_ERROR_EVENT: &str = "chain_error";
const APP_MODULE_EVENT: &str = "app_module";
/// 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";
/// Connection event types
const CONNECTION_INIT_EVENT: &str = "connection_open_init";
const CONNECTION_TRY_EVENT: &str = "connection_open_try";
const CONNECTION_ACK_EVENT: &str = "connection_open_ack";
const CONNECTION_CONFIRM_EVENT: &str = "connection_open_confirm";
/// Channel event types
const CHANNEL_OPEN_INIT_EVENT: &str = "channel_open_init";
const CHANNEL_OPEN_TRY_EVENT: &str = "channel_open_try";
const CHANNEL_OPEN_ACK_EVENT: &str = "channel_open_ack";
const CHANNEL_OPEN_CONFIRM_EVENT: &str = "channel_open_confirm";
const CHANNEL_CLOSE_INIT_EVENT: &str = "channel_close_init";
const CHANNEL_CLOSE_CONFIRM_EVENT: &str = "channel_close_confirm";
/// Packet event types
const SEND_PACKET_EVENT: &str = "send_packet";
const RECEIVE_PACKET_EVENT: &str = "receive_packet";
const WRITE_ACK_EVENT: &str = "write_acknowledgement";
const ACK_PACKET_EVENT: &str = "acknowledge_packet";
const TIMEOUT_EVENT: &str = "timeout_packet";
const TIMEOUT_ON_CLOSE_EVENT: &str = "timeout_packet_on_close";
/// Events types
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum IbcEventType {
NewBlock,
CreateClient,
UpdateClient,
UpgradeClient,
ClientMisbehaviour,
OpenInitConnection,
OpenTryConnection,
OpenAckConnection,
OpenConfirmConnection,
OpenInitChannel,
OpenTryChannel,
OpenAckChannel,
OpenConfirmChannel,
CloseInitChannel,
CloseConfirmChannel,
SendPacket,
ReceivePacket,
WriteAck,
AckPacket,
Timeout,
TimeoutOnClose,
AppModule,
Empty,
ChainError,
}
impl IbcEventType {
pub fn as_str(&self) -> &'static str {
match *self {
IbcEventType::NewBlock => NEW_BLOCK_EVENT,
IbcEventType::CreateClient => CREATE_CLIENT_EVENT,
IbcEventType::UpdateClient => UPDATE_CLIENT_EVENT,
IbcEventType::UpgradeClient => UPGRADE_CLIENT_EVENT,
IbcEventType::ClientMisbehaviour => CLIENT_MISBEHAVIOUR_EVENT,
IbcEventType::OpenInitConnection => CONNECTION_INIT_EVENT,
IbcEventType::OpenTryConnection => CONNECTION_TRY_EVENT,
IbcEventType::OpenAckConnection => CONNECTION_ACK_EVENT,
IbcEventType::OpenConfirmConnection => CONNECTION_CONFIRM_EVENT,
IbcEventType::OpenInitChannel => CHANNEL_OPEN_INIT_EVENT,
IbcEventType::OpenTryChannel => CHANNEL_OPEN_TRY_EVENT,
IbcEventType::OpenAckChannel => CHANNEL_OPEN_ACK_EVENT,
IbcEventType::OpenConfirmChannel => CHANNEL_OPEN_CONFIRM_EVENT,
IbcEventType::CloseInitChannel => CHANNEL_CLOSE_INIT_EVENT,
IbcEventType::CloseConfirmChannel => CHANNEL_CLOSE_CONFIRM_EVENT,
IbcEventType::SendPacket => SEND_PACKET_EVENT,
IbcEventType::ReceivePacket => RECEIVE_PACKET_EVENT,
IbcEventType::WriteAck => WRITE_ACK_EVENT,
IbcEventType::AckPacket => ACK_PACKET_EVENT,
IbcEventType::Timeout => TIMEOUT_EVENT,
IbcEventType::TimeoutOnClose => TIMEOUT_ON_CLOSE_EVENT,
IbcEventType::AppModule => APP_MODULE_EVENT,
IbcEventType::Empty => EMPTY_EVENT,
IbcEventType::ChainError => CHAIN_ERROR_EVENT,
}
}
}
impl FromStr for IbcEventType {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
NEW_BLOCK_EVENT => Ok(IbcEventType::NewBlock),
CREATE_CLIENT_EVENT => Ok(IbcEventType::CreateClient),
UPDATE_CLIENT_EVENT => Ok(IbcEventType::UpdateClient),
UPGRADE_CLIENT_EVENT => Ok(IbcEventType::UpgradeClient),
CLIENT_MISBEHAVIOUR_EVENT => Ok(IbcEventType::ClientMisbehaviour),
CONNECTION_INIT_EVENT => Ok(IbcEventType::OpenInitConnection),
CONNECTION_TRY_EVENT => Ok(IbcEventType::OpenTryConnection),
CONNECTION_ACK_EVENT => Ok(IbcEventType::OpenAckConnection),
CONNECTION_CONFIRM_EVENT => Ok(IbcEventType::OpenConfirmConnection),
CHANNEL_OPEN_INIT_EVENT => Ok(IbcEventType::OpenInitChannel),
CHANNEL_OPEN_TRY_EVENT => Ok(IbcEventType::OpenTryChannel),
CHANNEL_OPEN_ACK_EVENT => Ok(IbcEventType::OpenAckChannel),
CHANNEL_OPEN_CONFIRM_EVENT => Ok(IbcEventType::OpenConfirmChannel),
CHANNEL_CLOSE_INIT_EVENT => Ok(IbcEventType::CloseInitChannel),
CHANNEL_CLOSE_CONFIRM_EVENT => Ok(IbcEventType::CloseConfirmChannel),
SEND_PACKET_EVENT => Ok(IbcEventType::SendPacket),
RECEIVE_PACKET_EVENT => Ok(IbcEventType::ReceivePacket),
WRITE_ACK_EVENT => Ok(IbcEventType::WriteAck),
ACK_PACKET_EVENT => Ok(IbcEventType::AckPacket),
TIMEOUT_EVENT => Ok(IbcEventType::Timeout),
TIMEOUT_ON_CLOSE_EVENT => Ok(IbcEventType::TimeoutOnClose),
EMPTY_EVENT => Ok(IbcEventType::Empty),
CHAIN_ERROR_EVENT => Ok(IbcEventType::ChainError),
// from_str() for `APP_MODULE_EVENT` MUST fail because a `ModuleEvent`'s type isn't constant
_ => Err(Error::incorrect_event_type(s.to_string())),
}
}
}
/// Events created by the IBC component of a chain, destined for a relayer.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum IbcEvent {
NewBlock(NewBlock),
CreateClient(ClientEvents::CreateClient),
UpdateClient(ClientEvents::UpdateClient),
UpgradeClient(ClientEvents::UpgradeClient),
ClientMisbehaviour(ClientEvents::ClientMisbehaviour),
OpenInitConnection(ConnectionEvents::OpenInit),
OpenTryConnection(ConnectionEvents::OpenTry),
OpenAckConnection(ConnectionEvents::OpenAck),
OpenConfirmConnection(ConnectionEvents::OpenConfirm),
OpenInitChannel(ChannelEvents::OpenInit),
OpenTryChannel(ChannelEvents::OpenTry),
OpenAckChannel(ChannelEvents::OpenAck),
OpenConfirmChannel(ChannelEvents::OpenConfirm),
CloseInitChannel(ChannelEvents::CloseInit),
CloseConfirmChannel(ChannelEvents::CloseConfirm),
SendPacket(ChannelEvents::SendPacket),
ReceivePacket(ChannelEvents::ReceivePacket),
WriteAcknowledgement(ChannelEvents::WriteAcknowledgement),
AcknowledgePacket(ChannelEvents::AcknowledgePacket),
TimeoutPacket(ChannelEvents::TimeoutPacket),
TimeoutOnClosePacket(ChannelEvents::TimeoutOnClosePacket),
AppModule(ModuleEvent),
Empty(String), // Special event, signifying empty response
ChainError(String), // Special event, signifying an error on CheckTx or DeliverTx
}
impl Default for IbcEvent {
fn default() -> Self {
Self::Empty("".to_string())
}
}
/// For use in debug messages
pub struct PrettyEvents<'a>(pub &'a [IbcEvent]);
impl<'a> fmt::Display for PrettyEvents<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
writeln!(f, "events:")?;
for v in self.0 {
writeln!(f, "\t{}", v)?;
}
Ok(())
}
}
impl fmt::Display for IbcEvent {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
IbcEvent::NewBlock(ev) => write!(f, "NewBlock({})", ev.height),
IbcEvent::CreateClient(ev) => write!(f, "CreateClientEv({})", ev),
IbcEvent::UpdateClient(ev) => write!(f, "UpdateClientEv({})", ev),
IbcEvent::UpgradeClient(ev) => write!(f, "UpgradeClientEv({:?})", ev),
IbcEvent::ClientMisbehaviour(ev) => write!(f, "ClientMisbehaviourEv({:?})", ev),
IbcEvent::OpenInitConnection(ev) => write!(f, "OpenInitConnectionEv({:?})", ev),
IbcEvent::OpenTryConnection(ev) => write!(f, "OpenTryConnectionEv({:?})", ev),
IbcEvent::OpenAckConnection(ev) => write!(f, "OpenAckConnectionEv({:?})", ev),
IbcEvent::OpenConfirmConnection(ev) => write!(f, "OpenConfirmConnectionEv({:?})", ev),
IbcEvent::OpenInitChannel(ev) => write!(f, "OpenInitChannelEv({:?})", ev),
IbcEvent::OpenTryChannel(ev) => write!(f, "OpenTryChannelEv({:?})", ev),
IbcEvent::OpenAckChannel(ev) => write!(f, "OpenAckChannelEv({:?})", ev),
IbcEvent::OpenConfirmChannel(ev) => write!(f, "OpenConfirmChannelEv({:?})", ev),
IbcEvent::CloseInitChannel(ev) => write!(f, "CloseInitChannelEv({})", ev),
IbcEvent::CloseConfirmChannel(ev) => write!(f, "CloseConfirmChannelEv({:?})", ev),
IbcEvent::SendPacket(ev) => write!(f, "SendPacketEv({})", ev),
IbcEvent::ReceivePacket(ev) => write!(f, "ReceivePacketEv({})", ev),
IbcEvent::WriteAcknowledgement(ev) => write!(f, "WriteAcknowledgementEv({})", ev),
IbcEvent::AcknowledgePacket(ev) => write!(f, "AcknowledgePacketEv({})", ev),
IbcEvent::TimeoutPacket(ev) => write!(f, "TimeoutPacketEv({})", ev),
IbcEvent::TimeoutOnClosePacket(ev) => write!(f, "TimeoutOnClosePacketEv({})", ev),
IbcEvent::AppModule(ev) => write!(f, "AppModuleEv({:?})", ev),
IbcEvent::Empty(ev) => write!(f, "EmptyEv({})", ev),
IbcEvent::ChainError(ev) => write!(f, "ChainErrorEv({})", ev),
}
}
}
impl TryFrom<IbcEvent> for AbciEvent {
type Error = Error;
fn try_from(event: IbcEvent) -> Result<Self, Self::Error> {
Ok(match event {
IbcEvent::CreateClient(event) => event.into(),
IbcEvent::UpdateClient(event) => event.into(),
IbcEvent::UpgradeClient(event) => event.into(),
IbcEvent::ClientMisbehaviour(event) => event.into(),
IbcEvent::OpenInitConnection(event) => event.into(),
IbcEvent::OpenTryConnection(event) => event.into(),
IbcEvent::OpenAckConnection(event) => event.into(),
IbcEvent::OpenConfirmConnection(event) => event.into(),
IbcEvent::OpenInitChannel(event) => event.into(),
IbcEvent::OpenTryChannel(event) => event.into(),
IbcEvent::OpenAckChannel(event) => event.into(),
IbcEvent::OpenConfirmChannel(event) => event.into(),
IbcEvent::CloseInitChannel(event) => event.into(),
IbcEvent::CloseConfirmChannel(event) => event.into(),
IbcEvent::SendPacket(event) => event.try_into().map_err(Error::channel)?,
IbcEvent::ReceivePacket(event) => event.try_into().map_err(Error::channel)?,
IbcEvent::WriteAcknowledgement(event) => event.try_into().map_err(Error::channel)?,
IbcEvent::AcknowledgePacket(event) => event.try_into().map_err(Error::channel)?,
IbcEvent::TimeoutPacket(event) => event.try_into().map_err(Error::channel)?,
IbcEvent::TimeoutOnClosePacket(event) => event.try_into().map_err(Error::channel)?,
IbcEvent::AppModule(event) => event.try_into()?,
IbcEvent::NewBlock(_) | IbcEvent::Empty(_) | IbcEvent::ChainError(_) => {
return Err(Error::incorrect_event_type(event.to_string()))
}
})
}
}
// This is tendermint specific
pub fn from_tx_response_event(height: Height, event: &tendermint::abci::Event) -> Option<IbcEvent> {
// Return the first hit we find
if let Some(mut client_res) = ClientEvents::try_from_tx(event) {
client_res.set_height(height);
Some(client_res)
} else if let Some(mut conn_res) = ConnectionEvents::try_from_tx(event) {
conn_res.set_height(height);
Some(conn_res)
} else if let Some(mut chan_res) = ChannelEvents::try_from_tx(event) {
chan_res.set_height(height);
Some(chan_res)
} else {
None
}
}
impl IbcEvent {
pub fn to_json(&self) -> String {
match serde_json::to_string(self) {
Ok(value) => value,
Err(_) => format!("{:?}", self), // Fallback to debug printing
}
}
pub fn height(&self) -> Height {
match self {
IbcEvent::NewBlock(bl) => bl.height(),
IbcEvent::CreateClient(ev) => ev.height(),
IbcEvent::UpdateClient(ev) => ev.height(),
IbcEvent::UpgradeClient(ev) => ev.height(),
IbcEvent::ClientMisbehaviour(ev) => ev.height(),
IbcEvent::OpenInitConnection(ev) => ev.height(),
IbcEvent::OpenTryConnection(ev) => ev.height(),
IbcEvent::OpenAckConnection(ev) => ev.height(),
IbcEvent::OpenConfirmConnection(ev) => ev.height(),
IbcEvent::OpenInitChannel(ev) => ev.height(),
IbcEvent::OpenTryChannel(ev) => ev.height(),
IbcEvent::OpenAckChannel(ev) => ev.height(),
IbcEvent::OpenConfirmChannel(ev) => ev.height(),
IbcEvent::CloseInitChannel(ev) => ev.height(),
IbcEvent::CloseConfirmChannel(ev) => ev.height(),
IbcEvent::SendPacket(ev) => ev.height(),
IbcEvent::ReceivePacket(ev) => ev.height(),
IbcEvent::WriteAcknowledgement(ev) => ev.height(),
IbcEvent::AcknowledgePacket(ev) => ev.height(),
IbcEvent::TimeoutPacket(ev) => ev.height(),
IbcEvent::TimeoutOnClosePacket(ev) => ev.height(),
_ => unimplemented!(),
}
}
pub fn set_height(&mut self, height: Height) {
match self {
IbcEvent::NewBlock(ev) => ev.set_height(height),
IbcEvent::CreateClient(ev) => ev.set_height(height),
IbcEvent::UpdateClient(ev) => ev.set_height(height),
IbcEvent::UpgradeClient(ev) => ev.set_height(height),
IbcEvent::ClientMisbehaviour(ev) => ev.set_height(height),
IbcEvent::OpenInitConnection(ev) => ev.set_height(height),
IbcEvent::OpenTryConnection(ev) => ev.set_height(height),
IbcEvent::OpenAckConnection(ev) => ev.set_height(height),
IbcEvent::OpenConfirmConnection(ev) => ev.set_height(height),
IbcEvent::OpenInitChannel(ev) => ev.set_height(height),
IbcEvent::OpenTryChannel(ev) => ev.set_height(height),
IbcEvent::OpenAckChannel(ev) => ev.set_height(height),
IbcEvent::OpenConfirmChannel(ev) => ev.set_height(height),
IbcEvent::CloseInitChannel(ev) => ev.set_height(height),
IbcEvent::CloseConfirmChannel(ev) => ev.set_height(height),
IbcEvent::SendPacket(ev) => ev.set_height(height),
IbcEvent::ReceivePacket(ev) => ev.set_height(height),
IbcEvent::WriteAcknowledgement(ev) => ev.set_height(height),
IbcEvent::AcknowledgePacket(ev) => ev.set_height(height),
IbcEvent::TimeoutPacket(ev) => ev.set_height(height),
_ => unimplemented!(),
}
}
pub fn event_type(&self) -> IbcEventType {
match self {
IbcEvent::NewBlock(_) => IbcEventType::NewBlock,
IbcEvent::CreateClient(_) => IbcEventType::CreateClient,
IbcEvent::UpdateClient(_) => IbcEventType::UpdateClient,
IbcEvent::ClientMisbehaviour(_) => IbcEventType::ClientMisbehaviour,
IbcEvent::UpgradeClient(_) => IbcEventType::UpgradeClient,
IbcEvent::OpenInitConnection(_) => IbcEventType::OpenInitConnection,
IbcEvent::OpenTryConnection(_) => IbcEventType::OpenTryConnection,
IbcEvent::OpenAckConnection(_) => IbcEventType::OpenAckConnection,
IbcEvent::OpenConfirmConnection(_) => IbcEventType::OpenConfirmConnection,
IbcEvent::OpenInitChannel(_) => IbcEventType::OpenInitChannel,
IbcEvent::OpenTryChannel(_) => IbcEventType::OpenTryChannel,
IbcEvent::OpenAckChannel(_) => IbcEventType::OpenAckChannel,
IbcEvent::OpenConfirmChannel(_) => IbcEventType::OpenConfirmChannel,
IbcEvent::CloseInitChannel(_) => IbcEventType::CloseInitChannel,
IbcEvent::CloseConfirmChannel(_) => IbcEventType::CloseConfirmChannel,
IbcEvent::SendPacket(_) => IbcEventType::SendPacket,
IbcEvent::ReceivePacket(_) => IbcEventType::ReceivePacket,
IbcEvent::WriteAcknowledgement(_) => IbcEventType::WriteAck,
IbcEvent::AcknowledgePacket(_) => IbcEventType::AckPacket,
IbcEvent::TimeoutPacket(_) => IbcEventType::Timeout,
IbcEvent::TimeoutOnClosePacket(_) => IbcEventType::TimeoutOnClose,
IbcEvent::AppModule(_) => IbcEventType::AppModule,
IbcEvent::Empty(_) => IbcEventType::Empty,
IbcEvent::ChainError(_) => IbcEventType::ChainError,
}
}
pub fn channel_attributes(self) -> Option<ChannelAttributes> {
match self {
IbcEvent::OpenInitChannel(ev) => Some(ev.into()),
IbcEvent::OpenTryChannel(ev) => Some(ev.into()),
IbcEvent::OpenAckChannel(ev) => Some(ev.into()),
IbcEvent::OpenConfirmChannel(ev) => Some(ev.into()),
_ => None,
}
}
pub fn connection_attributes(&self) -> Option<&ConnectionAttributes> {
match self {
IbcEvent::OpenInitConnection(ev) => Some(ev.attributes()),
IbcEvent::OpenTryConnection(ev) => Some(ev.attributes()),
IbcEvent::OpenAckConnection(ev) => Some(ev.attributes()),
IbcEvent::OpenConfirmConnection(ev) => Some(ev.attributes()),
_ => None,
}
}
pub fn packet(&self) -> Option<&Packet> {
match self {
IbcEvent::SendPacket(ev) => Some(&ev.packet),
IbcEvent::ReceivePacket(ev) => Some(&ev.packet),
IbcEvent::WriteAcknowledgement(ev) => Some(&ev.packet),
IbcEvent::AcknowledgePacket(ev) => Some(&ev.packet),
IbcEvent::TimeoutPacket(ev) => Some(&ev.packet),
IbcEvent::TimeoutOnClosePacket(ev) => Some(&ev.packet),
_ => None,
}
}
pub fn ack(&self) -> Option<&[u8]> {
match self {
IbcEvent::WriteAcknowledgement(ev) => Some(&ev.ack),
_ => None,
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct ModuleEvent {
pub kind: String,
pub module_name: ModuleId,
pub attributes: Vec<ModuleEventAttribute>,
}
impl TryFrom<ModuleEvent> for AbciEvent {
type Error = Error;
fn try_from(event: ModuleEvent) -> Result<Self, Self::Error> {
if IbcEventType::from_str(event.kind.as_str()).is_ok() {
return Err(Error::malformed_module_event(event));
}
let attributes = event.attributes.into_iter().map(Into::into).collect();
Ok(AbciEvent {
type_str: event.kind,
attributes,
})
}
}
impl From<ModuleEvent> for IbcEvent {
fn from(e: ModuleEvent) -> Self {
IbcEvent::AppModule(e)
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct ModuleEventAttribute {
pub key: String,
pub value: String,
}
impl<K: ToString, V: ToString> From<(K, V)> for ModuleEventAttribute {
fn from((k, v): (K, V)) -> Self {
Self {
key: k.to_string(),
value: v.to_string(),
}
}
}
impl From<ModuleEventAttribute> for Tag {
fn from(attr: ModuleEventAttribute) -> Self {
Self {
key: attr
.key
.parse()
.expect("Key::from_str() impl is infallible"),
value: attr
.key
.parse()
.expect("Value::from_str() impl is infallible"),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct RawObject<'a> {
pub height: Height,
pub action: String,
pub idx: usize,
pub events: &'a HashMap<String, Vec<String>>,
}
impl<'a> RawObject<'a> {
pub fn new(
height: Height,
action: String,
idx: usize,
events: &'a HashMap<String, Vec<String>>,
) -> RawObject<'a> {
RawObject {
height,
action,
idx,
events,
}
}
}
pub fn extract_events(
events: &HashMap<String, Vec<String>>,
action_string: &str,
) -> Result<(), Error> {
if let Some(message_action) = events.get("message.action") {
if message_action.contains(&action_string.to_owned()) {
return Ok(());
}
return Err(Error::missing_action_string());
}
Err(Error::incorrect_event_type(action_string.to_string()))
}
pub fn extract_attribute(object: &RawObject<'_>, key: &str) -> Result<String, Error> {
let value = object
.events
.get(key)
.ok_or_else(|| Error::missing_key(key.to_string()))?[object.idx]
.clone();
Ok(value)
}
pub fn maybe_extract_attribute(object: &RawObject<'_>, key: &str) -> Option<String> {
object.events.get(key).map(|tags| tags[object.idx].clone())
}