Skip to content

Commit

Permalink
Merge 0db9943 into 453c1da
Browse files Browse the repository at this point in the history
  • Loading branch information
adizere authored Jan 6, 2021
2 parents 453c1da + 0db9943 commit f165f26
Show file tree
Hide file tree
Showing 51 changed files with 561 additions and 408 deletions.
14 changes: 9 additions & 5 deletions modules/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::convert::TryFrom;
use anomaly::{BoxError, Context};
use bech32::FromBase32;
use bech32::ToBase32;

use tendermint::account::Id as AccountId;

pub fn account_to_string(addr: AccountId) -> Result<String, BoxError> {
Expand All @@ -14,10 +13,15 @@ pub fn account_to_string(addr: AccountId) -> Result<String, BoxError> {
}

pub fn string_to_account(raw: String) -> Result<AccountId, BoxError> {
let (_hrp, data) =
bech32::decode(&raw).map_err(|e| Context::new("bad signer", Some(e.into())))?;
let (_hrp, data) = bech32::decode(&raw).map_err(|e| {
Context::new(
"error decoding signer string into bech32 {}",
Some(e.into()),
)
})?;
let addr_bytes =
Vec::<u8>::from_base32(&data).map_err(|e| Context::new("bad signer", Some(e.into())))?;
Vec::<u8>::from_base32(&data).map_err(|e| Context::new("bad signer {}", Some(e.into())))?;

Ok(AccountId::try_from(addr_bytes).map_err(|e| Context::new("bad signer", Some(e)))?)
Ok(AccountId::try_from(addr_bytes)
.map_err(|e| Context::new("error converting into AccountdId {}", Some(e)))?)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::convert::TryFrom;

use serde_derive::{Deserialize, Serialize};

use crate::events::EventsError;
use crate::events::{IBCEvent, RawObject};
use crate::make_event;
use anomaly::BoxError;
use serde_derive::{Deserialize, Serialize};
use std::convert::TryFrom;

// TODO - extract attributes
make_event!(Timeout, "timeout");
Expand Down
80 changes: 63 additions & 17 deletions modules/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};

use serde_derive::{Deserialize, Serialize};
use tendermint::abci::Event;
use tendermint::block::Height;
use tendermint_rpc::event::{Event as RpcEvent, EventData as RpcEventData};
use thiserror::Error;
use tracing::warn;

use crate::application::ics20_fungible_token_transfer::events as TransferEvents;
use crate::ics02_client::events as ClientEvents;
use crate::ics02_client::events::NewBlock;
use crate::ics02_client::Error as ICS2Error;
use crate::ics03_connection::events as ConnectionEvents;
use crate::ics03_connection::Kind as ICS3Error;
use crate::ics04_channel::error::Kind as ICS4Error;
use crate::ics04_channel::events as ChannelEvents;
use crate::Height as ICSHeight;

use tendermint_rpc::event::{Event as RpcEvent, EventData as RpcEventData};

use anomaly::BoxError;
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use tendermint::block::Height;

use tendermint::abci::Event;
use tracing::warn;

/// Events types
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum IBCEventType {
Expand All @@ -24,6 +26,49 @@ pub enum IBCEventType {
WriteAck,
}

/// Errors that may occur during event parsing
#[derive(Clone, Debug, Error)]
pub enum EventsError {
#[error("Missing action string")]
MissingActionString,

#[error("Incorrect event type")]
IncorrectEventType,

#[error("Unknown action in raw event")]
UnknownEventType,

#[error("Error converting into ICS2 event {0}")]
ICS2EventConversionError(ICS2Error),

#[error("Error converting into ICS3 event {0}")]
ICS3EventConversionError(ICS3Error),

#[error("Error converting into ICS4 event {0}")]
ICS4EventConversionError(ICS4Error),
}

// Client events conversion may throw ICS2 errors, so we nest them into an `EventsError`
impl From<ICS2Error> for EventsError {
fn from(e: ICS2Error) -> Self {
Self::ICS2EventConversionError(e)
}
}

// Connection events conversion may throw ICS3 errors, so we nest them into an `EventsError`
impl From<ICS3Error> for EventsError {
fn from(e: ICS3Error) -> Self {
Self::ICS3EventConversionError(e)
}
}

// ICS4 events conversion may throw errors, so we nest them into an `EventsError`
impl From<ICS4Error> for EventsError {
fn from(e: ICS4Error) -> Self {
Self::ICS4EventConversionError(e)
}
}

impl IBCEventType {
pub fn as_str(&self) -> &'static str {
match *self {
Expand Down Expand Up @@ -155,14 +200,14 @@ impl RawObject {
pub fn extract_events<S: ::std::hash::BuildHasher>(
events: &HashMap<String, Vec<String>, S>,
action_string: &str,
) -> Result<(), BoxError> {
) -> Result<(), EventsError> {
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());
return Err(EventsError::MissingActionString);
}
Err("Incorrect Event Type".into())
Err(EventsError::IncorrectEventType)
}

//Takes events in the form
Expand Down Expand Up @@ -238,7 +283,7 @@ pub fn get_all_events(result: RpcEvent) -> Result<Vec<(Height, IBCEvent)>, Strin
Ok(vals)
}

pub fn build_event(mut object: RawObject) -> Result<IBCEvent, BoxError> {
pub fn build_event(mut object: RawObject) -> Result<IBCEvent, EventsError> {
match object.action.as_str() {
// Client events
"create_client" => Ok(IBCEvent::from(ClientEvents::CreateClient::try_from(
Expand Down Expand Up @@ -297,7 +342,7 @@ pub fn build_event(mut object: RawObject) -> Result<IBCEvent, BoxError> {
object,
)?)),

_ => Err("Incorrect Event Type".into()),
_ => Err(EventsError::UnknownEventType),
}
}

Expand All @@ -309,7 +354,8 @@ macro_rules! make_event {
pub data: std::collections::HashMap<String, Vec<String>>,
}
impl TryFrom<RawObject> for $a {
type Error = BoxError;
type Error = EventsError;

fn try_from(result: RawObject) -> Result<Self, Self::Error> {
match crate::events::extract_events(&result.events, $b) {
Ok(()) => Ok($a {
Expand Down
1 change: 1 addition & 0 deletions modules/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl Event {
}
}

/// A `HandlerResult` is just a result over a parametrized `HandlerOutput` and an error type.
pub type HandlerResult<T, E> = Result<HandlerOutput<T>, E>;

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
Loading

0 comments on commit f165f26

Please sign in to comment.