-
Notifications
You must be signed in to change notification settings - Fork 328
/
events.rs
324 lines (292 loc) · 10 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
//! Types for the IBC events emitted from Tendermint Websocket by the connection module.
use serde_derive::{Deserialize, Serialize};
use tendermint::abci::tag::Tag;
use tendermint::abci::Event as AbciEvent;
use crate::core::ics02_client::error::Error as Ics02Error;
use crate::core::ics02_client::height::Height;
use crate::core::ics03_connection::error::Error;
use crate::core::ics24_host::identifier::{ClientId, ConnectionId};
use crate::events::{IbcEvent, IbcEventType};
use crate::prelude::*;
/// The content of the `key` field for the attribute containing the connection identifier.
const HEIGHT_ATTRIBUTE_KEY: &str = "height";
const CONN_ID_ATTRIBUTE_KEY: &str = "connection_id";
const CLIENT_ID_ATTRIBUTE_KEY: &str = "client_id";
const COUNTERPARTY_CONN_ID_ATTRIBUTE_KEY: &str = "counterparty_connection_id";
const COUNTERPARTY_CLIENT_ID_ATTRIBUTE_KEY: &str = "counterparty_client_id";
pub fn try_from_tx(event: &tendermint::abci::Event) -> Option<IbcEvent> {
match event.type_str.parse() {
Ok(IbcEventType::OpenInitConnection) => extract_attributes_from_tx(event)
.map(OpenInit::from)
.map(IbcEvent::OpenInitConnection)
.ok(),
Ok(IbcEventType::OpenTryConnection) => extract_attributes_from_tx(event)
.map(OpenTry::from)
.map(IbcEvent::OpenTryConnection)
.ok(),
Ok(IbcEventType::OpenAckConnection) => extract_attributes_from_tx(event)
.map(OpenAck::from)
.map(IbcEvent::OpenAckConnection)
.ok(),
Ok(IbcEventType::OpenConfirmConnection) => extract_attributes_from_tx(event)
.map(OpenConfirm::from)
.map(IbcEvent::OpenConfirmConnection)
.ok(),
_ => None,
}
}
fn extract_attributes_from_tx(event: &tendermint::abci::Event) -> Result<Attributes, Error> {
let mut attr = Attributes::default();
for tag in &event.attributes {
let key = tag.key.as_ref();
let value = tag.value.as_ref();
match key {
HEIGHT_ATTRIBUTE_KEY => {
attr.height = value.parse().map_err(|e| {
Error::ics02_client(Ics02Error::invalid_string_as_height(value.to_string(), e))
})?;
}
CONN_ID_ATTRIBUTE_KEY => {
attr.connection_id = value.parse().ok();
}
CLIENT_ID_ATTRIBUTE_KEY => {
attr.client_id = value.parse().map_err(Error::invalid_identifier)?;
}
COUNTERPARTY_CONN_ID_ATTRIBUTE_KEY => {
attr.counterparty_connection_id = value.parse().ok();
}
COUNTERPARTY_CLIENT_ID_ATTRIBUTE_KEY => {
attr.counterparty_client_id = value.parse().map_err(Error::invalid_identifier)?;
}
_ => {}
}
}
Ok(attr)
}
#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Attributes {
pub height: Height,
pub connection_id: Option<ConnectionId>,
pub client_id: ClientId,
pub counterparty_connection_id: Option<ConnectionId>,
pub counterparty_client_id: ClientId,
}
/// Convert attributes to Tendermint ABCI tags
///
/// # Note
/// The parsing of `Key`s and `Value`s never fails, because the
/// `FromStr` instance of `tendermint::abci::tag::{Key, Value}`
/// is infallible, even if it is not represented in the error type.
/// Once tendermint-rs improves the API of the `Key` and `Value` types,
/// we will be able to remove the `.parse().unwrap()` calls.
impl From<Attributes> for Vec<Tag> {
fn from(a: Attributes) -> Self {
let mut attributes = vec![];
let height = Tag {
key: HEIGHT_ATTRIBUTE_KEY.parse().unwrap(),
value: a.height.to_string().parse().unwrap(),
};
attributes.push(height);
if let Some(conn_id) = a.connection_id {
let conn_id = Tag {
key: CONN_ID_ATTRIBUTE_KEY.parse().unwrap(),
value: conn_id.to_string().parse().unwrap(),
};
attributes.push(conn_id);
}
let client_id = Tag {
key: CLIENT_ID_ATTRIBUTE_KEY.parse().unwrap(),
value: a.client_id.to_string().parse().unwrap(),
};
attributes.push(client_id);
if let Some(conn_id) = a.counterparty_connection_id {
let conn_id = Tag {
key: COUNTERPARTY_CONN_ID_ATTRIBUTE_KEY.parse().unwrap(),
value: conn_id.to_string().parse().unwrap(),
};
attributes.push(conn_id);
}
let counterparty_client_id = Tag {
key: COUNTERPARTY_CLIENT_ID_ATTRIBUTE_KEY.parse().unwrap(),
value: a.counterparty_client_id.to_string().parse().unwrap(),
};
attributes.push(counterparty_client_id);
attributes
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct OpenInit(Attributes);
impl OpenInit {
pub fn attributes(&self) -> &Attributes {
&self.0
}
pub fn connection_id(&self) -> Option<&ConnectionId> {
self.0.connection_id.as_ref()
}
pub fn height(&self) -> Height {
self.0.height
}
pub fn set_height(&mut self, height: Height) {
self.0.height = height;
}
}
impl From<Attributes> for OpenInit {
fn from(attrs: Attributes) -> Self {
OpenInit(attrs)
}
}
impl From<OpenInit> for IbcEvent {
fn from(v: OpenInit) -> Self {
IbcEvent::OpenInitConnection(v)
}
}
impl From<OpenInit> for AbciEvent {
fn from(v: OpenInit) -> Self {
let attributes = Vec::<Tag>::from(v.0);
AbciEvent {
type_str: IbcEventType::OpenInitConnection.as_str().to_string(),
attributes,
}
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct OpenTry(Attributes);
impl OpenTry {
pub fn attributes(&self) -> &Attributes {
&self.0
}
pub fn connection_id(&self) -> Option<&ConnectionId> {
self.0.connection_id.as_ref()
}
pub fn height(&self) -> Height {
self.0.height
}
pub fn set_height(&mut self, height: Height) {
self.0.height = height;
}
}
impl From<Attributes> for OpenTry {
fn from(attrs: Attributes) -> Self {
OpenTry(attrs)
}
}
impl From<OpenTry> for IbcEvent {
fn from(v: OpenTry) -> Self {
IbcEvent::OpenTryConnection(v)
}
}
impl From<OpenTry> for AbciEvent {
fn from(v: OpenTry) -> Self {
let attributes = Vec::<Tag>::from(v.0);
AbciEvent {
type_str: IbcEventType::OpenTryConnection.as_str().to_string(),
attributes,
}
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct OpenAck(Attributes);
impl OpenAck {
pub fn attributes(&self) -> &Attributes {
&self.0
}
pub fn connection_id(&self) -> Option<&ConnectionId> {
self.0.connection_id.as_ref()
}
pub fn height(&self) -> Height {
self.0.height
}
pub fn set_height(&mut self, height: Height) {
self.0.height = height;
}
}
impl From<Attributes> for OpenAck {
fn from(attrs: Attributes) -> Self {
OpenAck(attrs)
}
}
impl From<OpenAck> for IbcEvent {
fn from(v: OpenAck) -> Self {
IbcEvent::OpenAckConnection(v)
}
}
impl From<OpenAck> for AbciEvent {
fn from(v: OpenAck) -> Self {
let attributes = Vec::<Tag>::from(v.0);
AbciEvent {
type_str: IbcEventType::OpenAckConnection.as_str().to_string(),
attributes,
}
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct OpenConfirm(Attributes);
impl OpenConfirm {
pub fn attributes(&self) -> &Attributes {
&self.0
}
pub fn connection_id(&self) -> Option<&ConnectionId> {
self.0.connection_id.as_ref()
}
pub fn height(&self) -> Height {
self.0.height
}
pub fn set_height(&mut self, height: Height) {
self.0.height = height;
}
}
impl From<Attributes> for OpenConfirm {
fn from(attrs: Attributes) -> Self {
OpenConfirm(attrs)
}
}
impl From<OpenConfirm> for IbcEvent {
fn from(v: OpenConfirm) -> Self {
IbcEvent::OpenConfirmConnection(v)
}
}
impl From<OpenConfirm> for AbciEvent {
fn from(v: OpenConfirm) -> Self {
let attributes = Vec::<Tag>::from(v.0);
AbciEvent {
type_str: IbcEventType::OpenConfirmConnection.as_str().to_string(),
attributes,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn connection_event_to_abci_event() {
let height = Height::new(1, 1);
let attributes = Attributes {
height,
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 = OpenInit::from(attributes.clone());
abci_events.push(AbciEvent::from(open_init.clone()));
let open_try = OpenTry::from(attributes.clone());
abci_events.push(AbciEvent::from(open_try.clone()));
let open_ack = OpenAck::from(attributes.clone());
abci_events.push(AbciEvent::from(open_ack.clone()));
let open_confirm = OpenConfirm::from(attributes);
abci_events.push(AbciEvent::from(open_confirm.clone()));
for event in abci_events {
match try_from_tx(&event) {
Some(e) => match e {
IbcEvent::OpenInitConnection(e) => assert_eq!(e.0, open_init.0),
IbcEvent::OpenTryConnection(e) => assert_eq!(e.0, open_try.0),
IbcEvent::OpenAckConnection(e) => assert_eq!(e.0, open_ack.0),
IbcEvent::OpenConfirmConnection(e) => assert_eq!(e.0, open_confirm.0),
_ => panic!("unexpected event type"),
},
None => panic!("converted event was wrong"),
}
}
}
}