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
use std::collections::HashMap;
use anomaly::BoxError;
use serde_derive::{Deserialize, Serialize};
use crate::ics02_client::events as ClientEvents;
use crate::ics02_client::events::NewBlock;
use crate::ics03_connection::events as ConnectionEvents;
use crate::ics04_channel::events as ChannelEvents;
use crate::ics04_channel::events::Attributes as ChannelAttributes;
use crate::Height;
use prost::alloc::fmt::Formatter;
use std::fmt;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum IbcEventType {
CreateClient,
UpdateClient,
SendPacket,
WriteAck,
}
impl IbcEventType {
pub fn as_str(&self) -> &'static str {
match *self {
IbcEventType::CreateClient => "create_client",
IbcEventType::UpdateClient => "update_client",
IbcEventType::SendPacket => "send_packet",
IbcEventType::WriteAck => "write_acknowledgement",
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
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),
Empty(String),
ChainError(String),
}
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::Empty(ev) => write!(f, "EmptyEv({})", ev),
IbcEvent::ChainError(ev) => write!(f, "ChainErrorEv({})", ev),
}
}
}
pub fn from_tx_response_event(height: Height, event: &tendermint::abci::Event) -> Option<IbcEvent> {
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),
}
}
pub fn height(&self) -> Height {
match self {
IbcEvent::NewBlock(bl) => bl.height(),
IbcEvent::CreateClient(ev) => ev.height(),
IbcEvent::UpdateClient(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(),
_ => 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 channel_attributes(&self) -> Option<&ChannelAttributes> {
match self {
IbcEvent::OpenInitChannel(ev) => Some(ev.attributes()),
IbcEvent::OpenTryChannel(ev) => Some(ev.attributes()),
IbcEvent::OpenAckChannel(ev) => Some(ev.attributes()),
IbcEvent::OpenConfirmChannel(ev) => Some(ev.attributes()),
_ => None,
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RawObject {
pub height: Height,
pub action: String,
pub idx: usize,
pub events: HashMap<String, Vec<String>>,
}
impl RawObject {
pub fn new(
height: Height,
action: String,
idx: usize,
events: HashMap<String, Vec<String>>,
) -> RawObject {
RawObject {
height,
action,
idx,
events,
}
}
}
pub fn extract_events<S: ::std::hash::BuildHasher>(
events: &HashMap<String, Vec<String>, S>,
action_string: &str,
) -> Result<(), BoxError> {
if let Some(message_action) = events.get("message.action") {
if message_action.contains(&action_string.to_owned()) {
return Ok(());
}
return Err("Missing action string".into());
}
Err("Incorrect Event Type".into())
}
#[macro_export]
macro_rules! make_event {
($a:ident, $b:literal) => {
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct $a {
pub data: ::std::collections::HashMap<String, Vec<String>>,
}
impl ::std::convert::TryFrom<$crate::events::RawObject> for $a {
type Error = ::anomaly::BoxError;
fn try_from(result: $crate::events::RawObject) -> Result<Self, Self::Error> {
match $crate::events::extract_events(&result.events, $b) {
Ok(()) => Ok($a {
data: result.events.clone(),
}),
Err(e) => Err(e),
}
}
}
};
}
#[macro_export]
macro_rules! attribute {
($a:ident, $b:literal) => {
$a.events.get($b).ok_or($b)?[$a.idx].parse()?
};
}
#[macro_export]
macro_rules! some_attribute {
($a:ident, $b:literal) => {
$a.events
.get($b)
.map_or_else(|| None, |tags| tags[$a.idx].parse().ok())
};
}