-
Notifications
You must be signed in to change notification settings - Fork 352
/
events.rs
303 lines (263 loc) · 9.19 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
//! Types for the IBC events emitted from Tendermint Websocket by the client module.
use std::convert::{TryFrom, TryInto};
use anomaly::BoxError;
use serde_derive::{Deserialize, Serialize};
use subtle_encoding::hex;
use tendermint_proto::Protobuf;
use crate::attribute;
use crate::events::{IbcEvent, RawObject};
use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::header::AnyHeader;
use crate::ics02_client::height::Height;
use crate::ics24_host::identifier::ClientId;
/// The content of the `type` field for the event that a chain produces upon executing the create client transaction.
const CREATE_EVENT_TYPE: &str = "create_client";
const UPDATE_EVENT_TYPE: &str = "update_client";
const MISBEHAVIOUR_EVENT_TYPE: &str = "client_misbehaviour";
const UPGRADE_EVENT_TYPE: &str = "upgrade_client";
/// The content of the `key` field for the attribute containing the client identifier.
const CLIENT_ID_ATTRIBUTE_KEY: &str = "client_id";
/// The content of the `key` field for the attribute containing the client type.
const CLIENT_TYPE_ATTRIBUTE_KEY: &str = "client_type";
/// The content of the `key` field for the attribute containing the height.
const CONSENSUS_HEIGHT_ATTRIBUTE_KEY: &str = "consensus_height";
/// The content of the `key` field for the header in update client event.
const HEADER: &str = "header";
pub fn try_from_tx(event: &tendermint::abci::Event) -> Option<IbcEvent> {
match event.type_str.as_ref() {
CREATE_EVENT_TYPE => Some(IbcEvent::CreateClient(CreateClient(
extract_attributes_from_tx(event),
))),
UPDATE_EVENT_TYPE => Some(IbcEvent::UpdateClient(UpdateClient {
common: extract_attributes_from_tx(event),
header: extract_header_from_tx(event),
})),
MISBEHAVIOUR_EVENT_TYPE => Some(IbcEvent::ClientMisbehaviour(ClientMisbehaviour(
extract_attributes_from_tx(event),
))),
UPGRADE_EVENT_TYPE => Some(IbcEvent::UpgradeClient(UpgradeClient(
extract_attributes_from_tx(event),
))),
_ => None,
}
}
fn extract_attributes_from_tx(event: &tendermint::abci::Event) -> Attributes {
let mut attr = Attributes::default();
for tag in &event.attributes {
let key = tag.key.as_ref();
let value = tag.value.as_ref();
match key {
CLIENT_ID_ATTRIBUTE_KEY => attr.client_id = value.parse().unwrap(),
CLIENT_TYPE_ATTRIBUTE_KEY => attr.client_type = value.parse().unwrap(),
CONSENSUS_HEIGHT_ATTRIBUTE_KEY => attr.consensus_height = value.parse().unwrap(),
// TODO: `Attributes` has 4 fields and we're only parsing 3
_ => {}
}
}
attr
}
pub fn extract_header_from_tx(event: &tendermint::abci::Event) -> Option<AnyHeader> {
for tag in &event.attributes {
let key = tag.key.as_ref();
let value = tag.value.as_ref();
if let HEADER = key {
let header_bytes = hex::decode(value).unwrap();
let header: AnyHeader = Protobuf::decode(header_bytes.as_ref()).unwrap();
return Some(header);
}
}
None
}
/// NewBlock event signals the committing & execution of a new block.
// TODO - find a better place for NewBlock
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct NewBlock {
pub height: Height,
}
impl NewBlock {
pub fn new(h: Height) -> NewBlock {
NewBlock { height: h }
}
pub fn set_height(&mut self, height: Height) {
self.height = height;
}
pub fn height(&self) -> Height {
self.height
}
}
impl From<NewBlock> for IbcEvent {
fn from(v: NewBlock) -> Self {
IbcEvent::NewBlock(v)
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Attributes {
pub height: Height,
pub client_id: ClientId,
pub client_type: ClientType,
pub consensus_height: Height,
}
impl Default for Attributes {
fn default() -> Self {
Attributes {
height: Default::default(),
client_id: Default::default(),
client_type: ClientType::Tendermint,
consensus_height: Height::default(),
}
}
}
impl std::fmt::Display for Attributes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(
f,
"ev_h:{}, {}({}), ",
self.height, self.client_id, self.consensus_height
)
}
}
/// CreateClient event signals the creation of a new on-chain client (IBC client).
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct CreateClient(Attributes);
impl CreateClient {
pub fn client_id(&self) -> &ClientId {
&self.0.client_id
}
pub fn height(&self) -> Height {
self.0.height
}
pub fn set_height(&mut self, height: Height) {
self.0.height = height;
}
}
impl From<Attributes> for CreateClient {
fn from(attrs: Attributes) -> Self {
CreateClient(attrs)
}
}
impl TryFrom<RawObject> for CreateClient {
type Error = BoxError;
fn try_from(obj: RawObject) -> Result<Self, Self::Error> {
let consensus_height_str: String = attribute!(obj, "create_client.consensus_height");
Ok(CreateClient(Attributes {
height: obj.height,
client_id: attribute!(obj, "create_client.client_id"),
client_type: attribute!(obj, "create_client.client_type"),
consensus_height: consensus_height_str.as_str().try_into()?,
}))
}
}
impl From<CreateClient> for IbcEvent {
fn from(v: CreateClient) -> Self {
IbcEvent::CreateClient(v)
}
}
impl std::fmt::Display for CreateClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.0)
}
}
/// UpdateClient event signals a recent update of an on-chain client (IBC Client).
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct UpdateClient {
pub common: Attributes,
pub header: Option<AnyHeader>,
}
impl UpdateClient {
pub fn client_id(&self) -> &ClientId {
&self.common.client_id
}
pub fn client_type(&self) -> ClientType {
self.common.client_type
}
pub fn height(&self) -> Height {
self.common.height
}
pub fn set_height(&mut self, height: Height) {
self.common.height = height;
}
pub fn consensus_height(&self) -> Height {
self.common.consensus_height
}
}
impl From<Attributes> for UpdateClient {
fn from(attrs: Attributes) -> Self {
UpdateClient {
common: attrs,
header: None,
}
}
}
impl TryFrom<RawObject> for UpdateClient {
type Error = BoxError;
fn try_from(obj: RawObject) -> Result<Self, Self::Error> {
let header_str: String = attribute!(obj, "update_client.header");
let header_bytes = hex::decode(header_str).unwrap();
let header: AnyHeader = Protobuf::decode(header_bytes.as_ref()).unwrap();
let consensus_height_str: String = attribute!(obj, "update_client.consensus_height");
Ok(UpdateClient {
common: Attributes {
height: obj.height,
client_id: attribute!(obj, "update_client.client_id"),
client_type: attribute!(obj, "update_client.client_type"),
consensus_height: consensus_height_str.as_str().try_into()?,
},
header: Some(header),
})
}
}
impl From<UpdateClient> for IbcEvent {
fn from(v: UpdateClient) -> Self {
IbcEvent::UpdateClient(v)
}
}
impl std::fmt::Display for UpdateClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.common)
}
}
/// ClientMisbehaviour event signals the update of an on-chain client (IBC Client) with evidence of
/// misbehaviour.
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ClientMisbehaviour(Attributes);
impl ClientMisbehaviour {
pub fn client_id(&self) -> &ClientId {
&self.0.client_id
}
pub fn height(&self) -> Height {
self.0.height
}
pub fn set_height(&mut self, height: Height) {
self.0.height = height;
}
}
impl TryFrom<RawObject> for ClientMisbehaviour {
type Error = BoxError;
fn try_from(obj: RawObject) -> Result<Self, Self::Error> {
let consensus_height_str: String = attribute!(obj, "client_misbehaviour.consensus_height");
Ok(ClientMisbehaviour(Attributes {
height: obj.height,
client_id: attribute!(obj, "client_misbehaviour.client_id"),
client_type: attribute!(obj, "client_misbehaviour.client_type"),
consensus_height: consensus_height_str.as_str().try_into()?,
}))
}
}
impl From<ClientMisbehaviour> for IbcEvent {
fn from(v: ClientMisbehaviour) -> Self {
IbcEvent::ClientMisbehaviour(v)
}
}
/// Signals a recent upgrade of an on-chain client (IBC Client).
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
pub struct UpgradeClient(Attributes);
impl UpgradeClient {
pub fn set_height(&mut self, height: Height) {
self.0.height = height;
}
}
impl From<Attributes> for UpgradeClient {
fn from(attrs: Attributes) -> Self {
UpgradeClient(attrs)
}
}