Skip to content

Commit

Permalink
Use flex-error to define errors (#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
soareschen authored Jul 26, 2021
1 parent 1824735 commit b18f33f
Show file tree
Hide file tree
Showing 166 changed files with 5,351 additions and 3,947 deletions.
44 changes: 38 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exclude = [
"proto-compiler"
]

# [patch.crates-io]
[patch.crates-io]
# tendermint = { git = "https://github.com/informalsystems/tendermint-rs", branch = "master" }
# tendermint-rpc = { git = "https://github.com/informalsystems/tendermint-rs", branch = "master" }
# tendermint-proto = { git = "https://github.com/informalsystems/tendermint-rs", branch = "master" }
Expand Down
7 changes: 6 additions & 1 deletion modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ description = """
[features]
# This feature grants access to development-time mocking libraries, such as `MockContext` or `MockHeader`.
# Depends on the `testgen` suite for generating Tendermint light blocks.
default = ["std", "eyre_tracer"]
std = [
"flex-error/std"
]
eyre_tracer = ["flex-error/eyre_tracer"]
mocks = [ "tendermint-testgen", "sha2" ]

[dependencies]
# Proto definitions for all IBC-related interfaces, e.g., connections or channels.
ibc-proto = { version = "0.9.0", path = "../proto" }
ics23 = "0.6.5"
anomaly = "0.2.0"
chrono = "0.4.19"
thiserror = "1.0.26"
serde_derive = "1.0.104"
Expand All @@ -36,6 +40,7 @@ dyn-clonable = "0.9.0"
regex = "1"
subtle-encoding = "0.5"
sha2 = { version = "0.9.3", optional = true }
flex-error = { version = "0.4.1", default-features = false }

[dependencies.tendermint]
version = "=0.21.0"
Expand Down
84 changes: 43 additions & 41 deletions modules/src/application/ics20_fungible_token_transfer/error.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
use anomaly::{BoxError, Context};
use thiserror::Error;

use crate::ics04_channel::error as channel_error;
use crate::ics24_host::error::ValidationError;
use crate::ics24_host::identifier::{ChannelId, PortId};

pub type Error = anomaly::Error<Kind>;

#[derive(Clone, Debug, Error, PartialEq, Eq)]
pub enum Kind {
#[error("unrecognized ICS-20 transfer message type URL {0}")]
UnknownMessageTypeUrl(String),

#[error("error raised by message handler")]
HandlerRaisedError,

#[error("sending sequence number not found for port {0} and channel {1}")]
SequenceSendNotFound(PortId, ChannelId),

#[error("missing channel for port_id {0} and channel_id {1} ")]
ChannelNotFound(PortId, ChannelId),

#[error(
"destination channel not found in the counterparty of port_id {0} and channel_id {1} "
)]
DestinationChannelNotFound(PortId, ChannelId),

#[error("invalid port identifier")]
InvalidPortId(String),

#[error("invalid channel identifier")]
InvalidChannelId(String),

#[error("invalid packet timeout height value")]
InvalidPacketTimeoutHeight(String),

#[error("invalid packet timeout timestamp value")]
InvalidPacketTimeoutTimestamp(u64),
}

impl Kind {
pub fn context(self, source: impl Into<BoxError>) -> Context<Self> {
Context::new(self, Some(source.into()))
use flex_error::define_error;

define_error! {
Error {
UnknowMessageTypeUrl
{ url: String }
| e | { format_args!("unrecognized ICS-20 transfer message type URL {0}", e.url) },

Ics04Channel
[ channel_error::Error ]
|_ | { "Ics04 channel error" },

SequenceSendNotFound
{ port_id: PortId, channel_id: ChannelId }
| e | { format_args!("sending sequence number not found for port {0} and channel {1}", e.port_id, e.channel_id) },

ChannelNotFound
{ port_id: PortId, channel_id: ChannelId }
| e | { format_args!("sending sequence number not found for port {0} and channel {1}", e.port_id, e.channel_id) },

DestinationChannelNotFound
{ port_id: PortId, channel_id: ChannelId }
| e | { format_args!("destination channel not found in the counterparty of port_id {0} and channel_id {1} ", e.port_id, e.channel_id) },

InvalidPortId
{ context: String }
[ ValidationError ]
| _ | { "invalid port identifier" },

InvalidChannelId
{ context: String }
[ ValidationError ]
| _ | { "invalid channel identifier" },

InvalidPacketTimeoutHeight
{ context: String }
| _ | { "invalid packet timeout height value" },

InvalidPacketTimeoutTimestamp
{ timestamp: u64 }
| _ | { "invalid packet timeout timestamp value" },
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tendermint_proto::Protobuf;

use ibc_proto::ibc::apps::transfer::v1::MsgTransfer as RawMsgTransfer;

use crate::application::ics20_fungible_token_transfer::error::{Error, Kind};
use crate::application::ics20_fungible_token_transfer::error::Error;
use crate::ics02_client::height::Height;
use crate::ics24_host::identifier::{ChannelId, PortId};
use crate::signer::Signer;
Expand Down Expand Up @@ -54,28 +54,28 @@ impl Msg for MsgTransfer {
impl Protobuf<RawMsgTransfer> for MsgTransfer {}

impl TryFrom<RawMsgTransfer> for MsgTransfer {
type Error = Kind;
type Error = Error;

fn try_from(raw_msg: RawMsgTransfer) -> Result<Self, Self::Error> {
let timeout_timestamp = Timestamp::from_nanoseconds(raw_msg.timeout_timestamp)
.map_err(|_| Kind::InvalidPacketTimeoutTimestamp(raw_msg.timeout_timestamp))?;
.map_err(|_| Error::invalid_packet_timeout_timestamp(raw_msg.timeout_timestamp))?;

let timeout_height = match raw_msg.timeout_height.clone() {
None => Height::zero(),
Some(raw_height) => raw_height.try_into().map_err(|e| {
Kind::InvalidPacketTimeoutHeight(format!("invalid timeout height {}", e))
Error::invalid_packet_timeout_height(format!("invalid timeout height {}", e))
})?,
};

Ok(MsgTransfer {
source_port: raw_msg
.source_port
.parse()
.map_err(|_| Kind::InvalidPortId(raw_msg.source_port.clone()))?,
.map_err(|e| Error::invalid_port_id(raw_msg.source_port.clone(), e))?,
source_channel: raw_msg
.source_channel
.parse()
.map_err(|_| Kind::InvalidChannelId(raw_msg.source_channel.clone()))?,
.map_err(|e| Error::invalid_channel_id(raw_msg.source_channel.clone(), e))?,
token: raw_msg.token,
sender: raw_msg.sender.into(),
receiver: raw_msg.receiver.into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::application::ics20_fungible_token_transfer::context::Ics20Context;
use crate::application::ics20_fungible_token_transfer::error::{Error, Kind};
use crate::application::ics20_fungible_token_transfer::error::Error;
use crate::application::ics20_fungible_token_transfer::msgs::transfer::MsgTransfer;
use crate::handler::HandlerOutput;
use crate::ics04_channel::handler::send_packet::send_packet;
Expand All @@ -16,22 +16,25 @@ where
let source_channel_end = ctx
.channel_end(&(msg.source_port.clone(), msg.source_channel.clone()))
.ok_or_else(|| {
Kind::ChannelNotFound(msg.source_port.clone(), msg.source_channel.clone())
Error::channel_not_found(msg.source_port.clone(), msg.source_channel.clone())
})?;

let destination_port = source_channel_end.counterparty().port_id().clone();
let destination_channel = source_channel_end
.counterparty()
.channel_id()
.ok_or_else(|| {
Kind::DestinationChannelNotFound(msg.source_port.clone(), msg.source_channel.clone())
Error::destination_channel_not_found(
msg.source_port.clone(),
msg.source_channel.clone(),
)
})?;

// get the next sequence
let sequence = ctx
.get_next_sequence_send(&(msg.source_port.clone(), msg.source_channel.clone()))
.ok_or_else(|| {
Kind::SequenceSendNotFound(msg.source_port.clone(), msg.source_channel.clone())
Error::sequence_send_not_found(msg.source_port.clone(), msg.source_channel.clone())
})?;

//TODO: Application LOGIC.
Expand All @@ -47,8 +50,7 @@ where
timeout_timestamp: msg.timeout_timestamp,
};

let handler_output =
send_packet(ctx, packet).map_err(|e| Kind::HandlerRaisedError.context(e))?;
let handler_output = send_packet(ctx, packet).map_err(Error::ics04_channel)?;

//TODO: add event/atributes and writes to the store issued by the application logic for packet sending.
Ok(handler_output)
Expand Down
Loading

0 comments on commit b18f33f

Please sign in to comment.