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
use crate::prelude::*;
use core::convert::{TryFrom, TryInto};
use core::str::FromStr;
use flex_error::{define_error, TraceError};
use serde_derive::{Deserialize, Serialize};
use tendermint::abci;
use crate::core::ics02_client::error as client_error;
use crate::core::ics02_client::events::{self as ClientEvents};
use crate::core::ics03_connection::error as connection_error;
use crate::core::ics03_connection::events as ConnectionEvents;
use crate::core::ics04_channel::error as channel_error;
use crate::core::ics04_channel::events as ChannelEvents;
use crate::core::ics24_host::error::ValidationError;
use crate::core::ics26_routing::context::ModuleId;
use crate::timestamp::ParseTimestampError;
define_error! {
Error {
Height
| _ | { "error parsing height" },
Parse
[ ValidationError ]
| _ | { "parse error" },
Client
[ client_error::Error ]
| _ | { "ICS02 client error" },
Connection
[ connection_error::Error ]
| _ | { "connection 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) },
UnsupportedAbciEvent
{event_type: String}
|e| { format_args!("Unable to parse abci event type '{}' into IbcEvent", e.event_type)}
}
}
#[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 APP_MODULE_EVENT: &str = "app_module";
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";
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";
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";
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 CHANNEL_CLOSED_EVENT: &str = "channel_close";
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum IbcEventType {
CreateClient,
UpdateClient,
UpgradeClient,
ClientMisbehaviour,
OpenInitConnection,
OpenTryConnection,
OpenAckConnection,
OpenConfirmConnection,
OpenInitChannel,
OpenTryChannel,
OpenAckChannel,
OpenConfirmChannel,
CloseInitChannel,
CloseConfirmChannel,
ChannelClosed,
SendPacket,
ReceivePacket,
WriteAck,
AckPacket,
Timeout,
AppModule,
}
impl IbcEventType {
pub fn as_str(&self) -> &'static str {
match *self {
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::ChannelClosed => CHANNEL_CLOSED_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::AppModule => APP_MODULE_EVENT,
}
}
}
impl FromStr for IbcEventType {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
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),
CHANNEL_CLOSED_EVENT => Ok(IbcEventType::ChannelClosed),
_ => Err(Error::incorrect_event_type(s.to_string())),
}
}
}
#[derive(Debug)]
pub enum IbcEvent {
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),
ChannelClosed(ChannelEvents::ChannelClosed),
AppModule(ModuleEvent),
}
impl TryFrom<IbcEvent> for abci::Event {
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::ChannelClosed(event) => event.into(),
IbcEvent::AppModule(event) => event.try_into()?,
})
}
}
impl IbcEvent {
pub fn event_type(&self) -> IbcEventType {
match self {
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::ChannelClosed(_) => IbcEventType::ChannelClosed,
IbcEvent::AppModule(_) => IbcEventType::AppModule,
}
}
}
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ModuleEvent {
pub kind: String,
pub module_name: ModuleId,
pub attributes: Vec<ModuleEventAttribute>,
}
impl TryFrom<ModuleEvent> for abci::Event {
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(abci::Event {
kind: 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 abci::EventAttribute {
fn from(attr: ModuleEventAttribute) -> Self {
(attr.key, attr.value).into()
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use alloc::vec;
use crate::core::{
ics04_channel::{
channel::Order,
events::SendPacket,
packet::{test_utils::get_dummy_raw_packet, Packet},
},
ics24_host::identifier::ConnectionId,
};
#[test]
pub fn test_packet_data_non_utf8() {
let mut packet = Packet::try_from(get_dummy_raw_packet(1, 1)).unwrap();
packet.data = vec![128];
let ibc_event = IbcEvent::SendPacket(SendPacket::new(
packet,
Order::Unordered,
ConnectionId::default(),
));
let _ = abci::Event::try_from(ibc_event);
}
}