-
Notifications
You must be signed in to change notification settings - Fork 332
/
event.rs
613 lines (563 loc) · 25 KB
/
event.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
use core::fmt::{Display, Error as FmtError, Formatter};
use serde::Serialize;
use tendermint::abci::Event as AbciEvent;
use ibc_relayer_types::{
applications::ics29_fee::events::{DistributeFeePacket, IncentivizedPacket},
applications::ics31_icq::events::CrossChainQueryPacket,
core::ics02_client::{
error::Error as ClientError,
events::{self as client_events, Attributes as ClientAttributes, HEADER_ATTRIBUTE_KEY},
header::Header,
height::HeightErrorDetail,
},
core::ics03_connection::{
error::Error as ConnectionError,
events::{self as connection_events, Attributes as ConnectionAttributes},
},
core::ics04_channel::{
error::Error as ChannelError,
events::{self as channel_events, Attributes as ChannelAttributes},
packet::Packet,
timeout::TimeoutHeight,
},
events::{Error as IbcEventError, IbcEvent, IbcEventType},
Height,
};
use crate::light_client::decode_header;
pub mod bus;
pub mod error;
pub mod source;
#[derive(Clone, Debug, Serialize)]
pub struct IbcEventWithHeight {
pub event: IbcEvent,
pub height: Height,
}
impl IbcEventWithHeight {
pub fn new(event: IbcEvent, height: Height) -> Self {
Self { event, height }
}
pub fn with_height(self, height: Height) -> Self {
Self {
event: self.event,
height,
}
}
}
impl Display for IbcEventWithHeight {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
write!(f, "{} at height {}", self.event, self.height)
}
}
/// Note: This function, as well as other helpers, are needed as a workaround to
/// Rust's orphan rule. That is, we want the AbciEvent -> IbcEvent to be defined
/// in the relayer crate, but can't because neither AbciEvent nor IbcEvent are
/// defined in this crate. Hence, we are forced to make an ad-hoc function for
/// it.
pub fn ibc_event_try_from_abci_event(abci_event: &AbciEvent) -> Result<IbcEvent, IbcEventError> {
match abci_event.kind.parse() {
Ok(IbcEventType::CreateClient) => Ok(IbcEvent::CreateClient(
create_client_try_from_abci_event(abci_event).map_err(IbcEventError::client)?,
)),
Ok(IbcEventType::UpdateClient) => Ok(IbcEvent::UpdateClient(
update_client_try_from_abci_event(abci_event).map_err(IbcEventError::client)?,
)),
Ok(IbcEventType::UpgradeClient) => Ok(IbcEvent::UpgradeClient(
upgrade_client_try_from_abci_event(abci_event).map_err(IbcEventError::client)?,
)),
Ok(IbcEventType::ClientMisbehaviour) => Ok(IbcEvent::ClientMisbehaviour(
client_misbehaviour_try_from_abci_event(abci_event).map_err(IbcEventError::client)?,
)),
Ok(IbcEventType::OpenInitConnection) => Ok(IbcEvent::OpenInitConnection(
connection_open_init_try_from_abci_event(abci_event)
.map_err(IbcEventError::connection)?,
)),
Ok(IbcEventType::OpenTryConnection) => Ok(IbcEvent::OpenTryConnection(
connection_open_try_try_from_abci_event(abci_event)
.map_err(IbcEventError::connection)?,
)),
Ok(IbcEventType::OpenAckConnection) => Ok(IbcEvent::OpenAckConnection(
connection_open_ack_try_from_abci_event(abci_event)
.map_err(IbcEventError::connection)?,
)),
Ok(IbcEventType::OpenConfirmConnection) => Ok(IbcEvent::OpenConfirmConnection(
connection_open_confirm_try_from_abci_event(abci_event)
.map_err(IbcEventError::connection)?,
)),
Ok(IbcEventType::OpenInitChannel) => Ok(IbcEvent::OpenInitChannel(
channel_open_init_try_from_abci_event(abci_event).map_err(IbcEventError::channel)?,
)),
Ok(IbcEventType::OpenTryChannel) => Ok(IbcEvent::OpenTryChannel(
channel_open_try_try_from_abci_event(abci_event).map_err(IbcEventError::channel)?,
)),
Ok(IbcEventType::OpenAckChannel) => Ok(IbcEvent::OpenAckChannel(
channel_open_ack_try_from_abci_event(abci_event).map_err(IbcEventError::channel)?,
)),
Ok(IbcEventType::OpenConfirmChannel) => Ok(IbcEvent::OpenConfirmChannel(
channel_open_confirm_try_from_abci_event(abci_event).map_err(IbcEventError::channel)?,
)),
Ok(IbcEventType::CloseInitChannel) => Ok(IbcEvent::CloseInitChannel(
channel_close_init_try_from_abci_event(abci_event).map_err(IbcEventError::channel)?,
)),
Ok(IbcEventType::CloseConfirmChannel) => Ok(IbcEvent::CloseConfirmChannel(
channel_close_confirm_try_from_abci_event(abci_event)
.map_err(IbcEventError::channel)?,
)),
Ok(IbcEventType::SendPacket) => Ok(IbcEvent::SendPacket(
send_packet_try_from_abci_event(abci_event).map_err(IbcEventError::channel)?,
)),
Ok(IbcEventType::WriteAck) => Ok(IbcEvent::WriteAcknowledgement(
write_acknowledgement_try_from_abci_event(abci_event)
.map_err(IbcEventError::channel)?,
)),
Ok(IbcEventType::AckPacket) => Ok(IbcEvent::AcknowledgePacket(
acknowledge_packet_try_from_abci_event(abci_event).map_err(IbcEventError::channel)?,
)),
Ok(IbcEventType::Timeout) => Ok(IbcEvent::TimeoutPacket(
timeout_packet_try_from_abci_event(abci_event).map_err(IbcEventError::channel)?,
)),
Ok(IbcEventType::IncentivizedPacket) => Ok(IbcEvent::IncentivizedPacket(
IncentivizedPacket::try_from(&abci_event.attributes[..]).map_err(IbcEventError::fee)?,
)),
Ok(IbcEventType::DistributionFee) => Ok(IbcEvent::DistributeFeePacket(
DistributeFeePacket::try_from(&abci_event.attributes[..])
.map_err(IbcEventError::fee)?,
)),
Ok(IbcEventType::CrossChainQuery) => Ok(IbcEvent::CrossChainQueryPacket(
CrossChainQueryPacket::try_from(&abci_event.attributes[..])
.map_err(IbcEventError::cross_chain_query)?,
)),
_ => Err(IbcEventError::unsupported_abci_event(
abci_event.kind.clone(),
)),
}
}
pub fn create_client_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<client_events::CreateClient, ClientError> {
client_extract_attributes_from_tx(abci_event).map(client_events::CreateClient)
}
pub fn update_client_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<client_events::UpdateClient, ClientError> {
client_extract_attributes_from_tx(abci_event).map(|attributes| client_events::UpdateClient {
common: attributes,
header: extract_header_from_tx(abci_event).ok(),
})
}
pub fn upgrade_client_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<client_events::UpgradeClient, ClientError> {
client_extract_attributes_from_tx(abci_event).map(client_events::UpgradeClient)
}
pub fn client_misbehaviour_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<client_events::ClientMisbehaviour, ClientError> {
client_extract_attributes_from_tx(abci_event).map(client_events::ClientMisbehaviour)
}
pub fn connection_open_init_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<connection_events::OpenInit, ConnectionError> {
connection_extract_attributes_from_tx(abci_event).map(connection_events::OpenInit)
}
pub fn connection_open_try_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<connection_events::OpenTry, ConnectionError> {
connection_extract_attributes_from_tx(abci_event).map(connection_events::OpenTry)
}
pub fn connection_open_ack_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<connection_events::OpenAck, ConnectionError> {
connection_extract_attributes_from_tx(abci_event).map(connection_events::OpenAck)
}
pub fn connection_open_confirm_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<connection_events::OpenConfirm, ConnectionError> {
connection_extract_attributes_from_tx(abci_event).map(connection_events::OpenConfirm)
}
pub fn channel_open_init_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<channel_events::OpenInit, ChannelError> {
match channel_extract_attributes_from_tx(abci_event) {
Ok(attrs) => channel_events::OpenInit::try_from(attrs)
.map_err(|_| ChannelError::implementation_specific()),
Err(e) => Err(e),
}
}
pub fn channel_open_try_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<channel_events::OpenTry, ChannelError> {
match channel_extract_attributes_from_tx(abci_event) {
Ok(attrs) => channel_events::OpenTry::try_from(attrs)
.map_err(|_| ChannelError::implementation_specific()),
Err(e) => Err(e),
}
}
pub fn channel_open_ack_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<channel_events::OpenAck, ChannelError> {
match channel_extract_attributes_from_tx(abci_event) {
Ok(attrs) => channel_events::OpenAck::try_from(attrs)
.map_err(|_| ChannelError::implementation_specific()),
Err(e) => Err(e),
}
}
pub fn channel_open_confirm_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<channel_events::OpenConfirm, ChannelError> {
match channel_extract_attributes_from_tx(abci_event) {
Ok(attrs) => channel_events::OpenConfirm::try_from(attrs)
.map_err(|_| ChannelError::implementation_specific()),
Err(e) => Err(e),
}
}
pub fn channel_close_init_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<channel_events::CloseInit, ChannelError> {
match channel_extract_attributes_from_tx(abci_event) {
Ok(attrs) => channel_events::CloseInit::try_from(attrs)
.map_err(|_| ChannelError::implementation_specific()),
Err(e) => Err(e),
}
}
pub fn channel_close_confirm_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<channel_events::CloseConfirm, ChannelError> {
match channel_extract_attributes_from_tx(abci_event) {
Ok(attrs) => channel_events::CloseConfirm::try_from(attrs)
.map_err(|_| ChannelError::implementation_specific()),
Err(e) => Err(e),
}
}
pub fn send_packet_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<channel_events::SendPacket, ChannelError> {
extract_packet_and_write_ack_from_tx(abci_event)
.map(|(packet, write_ack)| {
// This event should not have a write ack.
debug_assert_eq!(write_ack.len(), 0);
channel_events::SendPacket { packet }
})
.map_err(|_| ChannelError::abci_conversion_failed(abci_event.kind.clone()))
}
pub fn write_acknowledgement_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<channel_events::WriteAcknowledgement, ChannelError> {
extract_packet_and_write_ack_from_tx(abci_event)
.map(|(packet, write_ack)| channel_events::WriteAcknowledgement {
packet,
ack: write_ack,
})
.map_err(|_| ChannelError::abci_conversion_failed(abci_event.kind.clone()))
}
pub fn acknowledge_packet_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<channel_events::AcknowledgePacket, ChannelError> {
extract_packet_and_write_ack_from_tx(abci_event)
.map(|(packet, write_ack)| {
// This event should not have a write ack.
debug_assert_eq!(write_ack.len(), 0);
channel_events::AcknowledgePacket { packet }
})
.map_err(|_| ChannelError::abci_conversion_failed(abci_event.kind.clone()))
}
pub fn timeout_packet_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<channel_events::TimeoutPacket, ChannelError> {
extract_packet_and_write_ack_from_tx(abci_event)
.map(|(packet, write_ack)| {
// This event should not have a write ack.
debug_assert_eq!(write_ack.len(), 0);
channel_events::TimeoutPacket { packet }
})
.map_err(|_| ChannelError::abci_conversion_failed(abci_event.kind.clone()))
}
fn client_extract_attributes_from_tx(event: &AbciEvent) -> Result<ClientAttributes, ClientError> {
let mut attr = ClientAttributes::default();
for tag in &event.attributes {
let key = tag.key.as_str();
let value = tag.value.as_str();
match key {
client_events::CLIENT_ID_ATTRIBUTE_KEY => {
attr.client_id = value
.parse()
.map_err(ClientError::invalid_client_identifier)?
}
client_events::CLIENT_TYPE_ATTRIBUTE_KEY => {
attr.client_type = value
.parse()
.map_err(|_| ClientError::unknown_client_type(value.to_string()))?
}
client_events::CONSENSUS_HEIGHT_ATTRIBUTE_KEY => {
attr.consensus_height = value
.parse()
.map_err(|e| ClientError::invalid_string_as_height(value.to_string(), e))?
}
_ => {}
}
}
Ok(attr)
}
pub fn extract_header_from_tx(event: &AbciEvent) -> Result<Box<dyn Header>, ClientError> {
for tag in &event.attributes {
if tag.key == HEADER_ATTRIBUTE_KEY {
let header_bytes =
hex::decode(&tag.value).map_err(|_| ClientError::malformed_header())?;
return decode_header(&header_bytes);
}
}
Err(ClientError::missing_raw_header())
}
fn connection_extract_attributes_from_tx(
event: &AbciEvent,
) -> Result<ConnectionAttributes, ConnectionError> {
let mut attr = ConnectionAttributes::default();
for tag in &event.attributes {
let key = tag.key.as_str();
let value = tag.value.as_str();
match key {
connection_events::CONN_ID_ATTRIBUTE_KEY => {
attr.connection_id = value.parse().ok();
}
connection_events::CLIENT_ID_ATTRIBUTE_KEY => {
attr.client_id = value.parse().map_err(ConnectionError::invalid_identifier)?;
}
connection_events::COUNTERPARTY_CONN_ID_ATTRIBUTE_KEY => {
attr.counterparty_connection_id = value.parse().ok();
}
connection_events::COUNTERPARTY_CLIENT_ID_ATTRIBUTE_KEY => {
attr.counterparty_client_id =
value.parse().map_err(ConnectionError::invalid_identifier)?;
}
_ => {}
}
}
Ok(attr)
}
fn channel_extract_attributes_from_tx(
event: &AbciEvent,
) -> Result<ChannelAttributes, ChannelError> {
let mut attr = ChannelAttributes::default();
for tag in &event.attributes {
let key = tag.key.as_str();
let value = tag.value.as_str();
match key {
channel_events::PORT_ID_ATTRIBUTE_KEY => {
attr.port_id = value.parse().map_err(ChannelError::identifier)?
}
channel_events::CHANNEL_ID_ATTRIBUTE_KEY => {
attr.channel_id = value.parse().ok();
}
channel_events::CONNECTION_ID_ATTRIBUTE_KEY => {
attr.connection_id = value.parse().map_err(ChannelError::identifier)?;
}
channel_events::COUNTERPARTY_PORT_ID_ATTRIBUTE_KEY => {
attr.counterparty_port_id = value.parse().map_err(ChannelError::identifier)?;
}
channel_events::COUNTERPARTY_CHANNEL_ID_ATTRIBUTE_KEY => {
attr.counterparty_channel_id = value.parse().ok();
}
_ => {}
}
}
Ok(attr)
}
pub fn extract_packet_and_write_ack_from_tx(
event: &AbciEvent,
) -> Result<(Packet, Vec<u8>), ChannelError> {
let mut packet = Packet::default();
let mut write_ack: Vec<u8> = Vec::new();
for tag in &event.attributes {
let key = tag.key.as_str();
let value = tag.value.as_str();
match key {
channel_events::PKT_SRC_PORT_ATTRIBUTE_KEY => {
packet.source_port = value.parse().map_err(ChannelError::identifier)?;
}
channel_events::PKT_SRC_CHANNEL_ATTRIBUTE_KEY => {
packet.source_channel = value.parse().map_err(ChannelError::identifier)?;
}
channel_events::PKT_DST_PORT_ATTRIBUTE_KEY => {
packet.destination_port = value.parse().map_err(ChannelError::identifier)?;
}
channel_events::PKT_DST_CHANNEL_ATTRIBUTE_KEY => {
packet.destination_channel = value.parse().map_err(ChannelError::identifier)?;
}
channel_events::PKT_SEQ_ATTRIBUTE_KEY => {
packet.sequence = value
.parse::<u64>()
.map_err(|e| ChannelError::invalid_string_as_sequence(value.to_string(), e))?
.into()
}
channel_events::PKT_TIMEOUT_HEIGHT_ATTRIBUTE_KEY => {
packet.timeout_height = parse_timeout_height(value)?;
}
channel_events::PKT_TIMEOUT_TIMESTAMP_ATTRIBUTE_KEY => {
packet.timeout_timestamp = value.parse().unwrap();
}
channel_events::PKT_DATA_ATTRIBUTE_KEY => {
packet.data = Vec::from(value.as_bytes());
}
channel_events::PKT_ACK_ATTRIBUTE_KEY => {
write_ack = Vec::from(value.as_bytes());
}
_ => {}
}
}
Ok((packet, write_ack))
}
/// Parse a string into a timeout height expected to be stored in
/// `Packet.timeout_height`. We need to parse the timeout height differently
/// because of a quirk introduced in ibc-go. See comment in
/// `TryFrom<RawPacket> for Packet`.
pub fn parse_timeout_height(s: &str) -> Result<TimeoutHeight, ChannelError> {
match s.parse::<Height>() {
Ok(height) => Ok(TimeoutHeight::from(height)),
Err(e) => match e.into_detail() {
HeightErrorDetail::ZeroHeight(_) => Ok(TimeoutHeight::no_timeout()),
_ => Err(ChannelError::invalid_timeout_height()),
},
}
}
#[cfg(test)]
mod tests {
use super::*;
use ibc_proto::google::protobuf::Any;
use ibc_proto::protobuf::Protobuf;
use ibc_relayer_types::clients::ics07_tendermint::header::test_util::get_dummy_ics07_header;
use ibc_relayer_types::clients::ics07_tendermint::header::Header as TmHeader;
use ibc_relayer_types::core::ics02_client::header::downcast_header;
use ibc_relayer_types::core::ics04_channel::packet::Sequence;
use ibc_relayer_types::timestamp::Timestamp;
#[test]
fn extract_header() {
let header = get_dummy_ics07_header();
let mut header_bytes = Vec::new();
Protobuf::<Any>::encode(&header, &mut header_bytes).unwrap();
let decoded_dyn_header = decode_header(&header_bytes).unwrap();
let decoded_tm_header: &TmHeader = downcast_header(decoded_dyn_header.as_ref()).unwrap();
assert_eq!(&header, decoded_tm_header);
}
#[test]
fn connection_event_to_abci_event() {
let attributes = ConnectionAttributes {
connection_id: Some("test_connection".parse().unwrap()),
client_id: "test_client".parse().unwrap(),
counterparty_connection_id: Some("counterparty_test_conn".parse().unwrap()),
counterparty_client_id: "counterparty_test_client".parse().unwrap(),
};
let mut abci_events = vec![];
let open_init = connection_events::OpenInit::from(attributes.clone());
abci_events.push(AbciEvent::from(open_init.clone()));
let open_try = connection_events::OpenTry::from(attributes.clone());
abci_events.push(AbciEvent::from(open_try.clone()));
let open_ack = connection_events::OpenAck::from(attributes.clone());
abci_events.push(AbciEvent::from(open_ack.clone()));
let open_confirm = connection_events::OpenConfirm::from(attributes);
abci_events.push(AbciEvent::from(open_confirm.clone()));
for abci_event in abci_events {
match ibc_event_try_from_abci_event(&abci_event).ok() {
Some(ibc_event) => match ibc_event {
IbcEvent::OpenInitConnection(e) => assert_eq!(e, open_init),
IbcEvent::OpenTryConnection(e) => assert_eq!(e, open_try),
IbcEvent::OpenAckConnection(e) => assert_eq!(e, open_ack),
IbcEvent::OpenConfirmConnection(e) => assert_eq!(e, open_confirm),
_ => panic!("unexpected event type"),
},
None => panic!("converted event was wrong"),
}
}
}
#[test]
fn channel_event_to_abci_event() {
let attributes = ChannelAttributes {
port_id: "test_port".parse().unwrap(),
channel_id: Some("channel-0".parse().unwrap()),
connection_id: "test_connection".parse().unwrap(),
counterparty_port_id: "counterparty_test_port".parse().unwrap(),
counterparty_channel_id: Some("channel-1".parse().unwrap()),
};
let mut abci_events = vec![];
let open_init = channel_events::OpenInit::try_from(attributes.clone()).unwrap();
abci_events.push(AbciEvent::from(open_init.clone()));
let open_try = channel_events::OpenTry::try_from(attributes.clone()).unwrap();
abci_events.push(AbciEvent::from(open_try.clone()));
let open_ack = channel_events::OpenAck::try_from(attributes.clone()).unwrap();
abci_events.push(AbciEvent::from(open_ack.clone()));
let open_confirm = channel_events::OpenConfirm::try_from(attributes.clone()).unwrap();
abci_events.push(AbciEvent::from(open_confirm.clone()));
let close_init = channel_events::CloseInit::try_from(attributes.clone()).unwrap();
abci_events.push(AbciEvent::from(close_init.clone()));
let close_confirm = channel_events::CloseConfirm::try_from(attributes).unwrap();
abci_events.push(AbciEvent::from(close_confirm.clone()));
for abci_event in abci_events {
match ibc_event_try_from_abci_event(&abci_event).ok() {
Some(ibc_event) => match ibc_event {
IbcEvent::OpenInitChannel(e) => {
assert_eq!(ChannelAttributes::from(e), open_init.clone().into())
}
IbcEvent::OpenTryChannel(e) => {
assert_eq!(ChannelAttributes::from(e), open_try.clone().into())
}
IbcEvent::OpenAckChannel(e) => {
assert_eq!(ChannelAttributes::from(e), open_ack.clone().into())
}
IbcEvent::OpenConfirmChannel(e) => {
assert_eq!(ChannelAttributes::from(e), open_confirm.clone().into())
}
IbcEvent::CloseInitChannel(e) => {
assert_eq!(ChannelAttributes::from(e), close_init.clone().into())
}
IbcEvent::CloseConfirmChannel(e) => {
assert_eq!(ChannelAttributes::from(e), close_confirm.clone().into())
}
_ => panic!("unexpected event type"),
},
None => panic!("converted event was wrong"),
}
}
}
#[test]
fn packet_event_to_abci_event() {
let packet = Packet {
sequence: Sequence::from(10),
source_port: "a_test_port".parse().unwrap(),
source_channel: "channel-0".parse().unwrap(),
destination_port: "b_test_port".parse().unwrap(),
destination_channel: "channel-1".parse().unwrap(),
data: "test_data".as_bytes().to_vec(),
timeout_height: Height::new(1, 10).unwrap().into(),
timeout_timestamp: Timestamp::now(),
};
let mut abci_events = vec![];
let send_packet = channel_events::SendPacket {
packet: packet.clone(),
};
abci_events.push(AbciEvent::try_from(send_packet.clone()).unwrap());
let write_ack = channel_events::WriteAcknowledgement {
packet: packet.clone(),
ack: "test_ack".as_bytes().to_vec(),
};
abci_events.push(AbciEvent::try_from(write_ack.clone()).unwrap());
let ack_packet = channel_events::AcknowledgePacket {
packet: packet.clone(),
};
abci_events.push(AbciEvent::try_from(ack_packet.clone()).unwrap());
let timeout_packet = channel_events::TimeoutPacket { packet };
abci_events.push(AbciEvent::try_from(timeout_packet.clone()).unwrap());
for abci_event in abci_events {
match ibc_event_try_from_abci_event(&abci_event).ok() {
Some(ibc_event) => match ibc_event {
IbcEvent::SendPacket(e) => assert_eq!(e.packet, send_packet.packet),
IbcEvent::WriteAcknowledgement(e) => {
assert_eq!(e.packet, write_ack.packet);
assert_eq!(e.ack, write_ack.ack);
}
IbcEvent::AcknowledgePacket(e) => assert_eq!(e.packet, ack_packet.packet),
IbcEvent::TimeoutPacket(e) => assert_eq!(e.packet, timeout_packet.packet),
_ => panic!("unexpected event type"),
},
None => panic!("converted event was wrong"),
}
}
}
}