Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get events from RPC event's data instead of events #1172

Merged
merged 15 commits into from
Aug 23, 2021
Merged
62 changes: 0 additions & 62 deletions modules/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,30 +210,6 @@ impl IbcEvent {
}
}

#[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,
Expand All @@ -246,41 +222,3 @@ pub fn extract_events<S: ::std::hash::BuildHasher>(
}
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())
};
}
55 changes: 1 addition & 54 deletions modules/src/ics02_client/events.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
//! 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::events::{IbcEvent, RawObject};
use crate::events::IbcEvent;
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;
use crate::{attribute, some_attribute};

/// 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";
Expand Down Expand Up @@ -157,19 +154,6 @@ impl From<Attributes> for CreateClient {
}
}

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)
Expand Down Expand Up @@ -219,30 +203,6 @@ impl From<Attributes> for UpdateClient {
}
}

impl TryFrom<RawObject> for UpdateClient {
type Error = BoxError;
fn try_from(obj: RawObject) -> Result<Self, Self::Error> {
let header_str: Option<String> = some_attribute!(obj, "update_client.header");
let header: Option<AnyHeader> = match header_str {
Some(str) => {
let header_bytes = hex::decode(str)?;
Some(Protobuf::decode(header_bytes.as_ref())?)
}
None => None,
};
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,
})
}
}

impl From<UpdateClient> for IbcEvent {
fn from(v: UpdateClient) -> Self {
IbcEvent::UpdateClient(v)
Expand Down Expand Up @@ -272,19 +232,6 @@ impl ClientMisbehaviour {
}
}

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)
Expand Down
72 changes: 1 addition & 71 deletions modules/src/ics03_connection/events.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//! Types for the IBC events emitted from Tendermint Websocket by the connection module.
use crate::events::{IbcEvent, RawObject};
use crate::events::IbcEvent;
use crate::ics02_client::height::Height;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use crate::{attribute, some_attribute};
use anomaly::BoxError;
use serde_derive::{Deserialize, Serialize};
use std::convert::TryFrom;

/// The content of the `type` field for the event that a chain produces upon executing a connection handshake transaction.
const INIT_EVENT_TYPE: &str = "connection_open_init";
Expand Down Expand Up @@ -105,22 +102,6 @@ impl From<Attributes> for OpenInit {
}
}

impl TryFrom<RawObject> for OpenInit {
type Error = BoxError;
fn try_from(obj: RawObject) -> Result<Self, Self::Error> {
Ok(OpenInit(Attributes {
height: obj.height,
connection_id: some_attribute!(obj, "connection_open_init.connection_id"),
client_id: attribute!(obj, "connection_open_init.client_id"),
counterparty_connection_id: some_attribute!(
obj,
"connection_open_init.counterparty_connection_id"
),
counterparty_client_id: attribute!(obj, "connection_open_init.counterparty_client_id"),
}))
}
}

impl From<OpenInit> for IbcEvent {
fn from(v: OpenInit) -> Self {
IbcEvent::OpenInitConnection(v)
Expand Down Expand Up @@ -151,22 +132,6 @@ impl From<Attributes> for OpenTry {
}
}

impl TryFrom<RawObject> for OpenTry {
type Error = BoxError;
fn try_from(obj: RawObject) -> Result<Self, Self::Error> {
Ok(OpenTry(Attributes {
height: obj.height,
connection_id: some_attribute!(obj, "connection_open_try.connection_id"),
client_id: attribute!(obj, "connection_open_try.client_id"),
counterparty_connection_id: some_attribute!(
obj,
"connection_open_try.counterparty_connection_id"
),
counterparty_client_id: attribute!(obj, "connection_open_try.counterparty_client_id"),
}))
}
}

impl From<OpenTry> for IbcEvent {
fn from(v: OpenTry) -> Self {
IbcEvent::OpenTryConnection(v)
Expand Down Expand Up @@ -197,22 +162,6 @@ impl From<Attributes> for OpenAck {
}
}

impl TryFrom<RawObject> for OpenAck {
type Error = BoxError;
fn try_from(obj: RawObject) -> Result<Self, Self::Error> {
Ok(OpenAck(Attributes {
height: obj.height,
connection_id: some_attribute!(obj, "connection_open_ack.connection_id"),
client_id: attribute!(obj, "connection_open_ack.client_id"),
counterparty_connection_id: some_attribute!(
obj,
"connection_open_ack.counterparty_connection_id"
),
counterparty_client_id: attribute!(obj, "connection_open_ack.counterparty_client_id"),
}))
}
}

impl From<OpenAck> for IbcEvent {
fn from(v: OpenAck) -> Self {
IbcEvent::OpenAckConnection(v)
Expand Down Expand Up @@ -243,25 +192,6 @@ impl From<Attributes> for OpenConfirm {
}
}

impl TryFrom<RawObject> for OpenConfirm {
type Error = BoxError;
fn try_from(obj: RawObject) -> Result<Self, Self::Error> {
Ok(OpenConfirm(Attributes {
height: obj.height,
connection_id: some_attribute!(obj, "connection_open_confirm.connection_id"),
client_id: attribute!(obj, "connection_open_confirm.client_id"),
counterparty_connection_id: some_attribute!(
obj,
"connection_open_confirm.counterparty_connection_id"
),
counterparty_client_id: attribute!(
obj,
"connection_open_confirm.counterparty_client_id"
),
}))
}
}

impl From<OpenConfirm> for IbcEvent {
fn from(v: OpenConfirm) -> Self {
IbcEvent::OpenConfirmConnection(v)
Expand Down
Loading