Skip to content

Commit

Permalink
feat(voyager): use PortId instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo committed Oct 28, 2023
1 parent f816c6b commit dc1dcac
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 251 deletions.
64 changes: 39 additions & 25 deletions lib/chain-utils/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ use unionlabs::{
google::protobuf::any::Any,
lightclients::{cometbls, ethereum, tendermint::fraction::Fraction, wasm},
},
id::{ChannelId, ClientId, ConnectionId},
id::{ChannelId, ClientId, ConnectionId, PortId},
traits::{Chain, ClientState},
validated::ValidateT,
EmptyString, TryFromEthAbiErrorOf, TryFromProto,
};

Expand Down Expand Up @@ -271,7 +272,7 @@ impl<C: ChainSpec> Chain for Evm<C> {
&self,
block_hash: H256,
destination_channel_id: ChannelId,
destination_port_id: String,
destination_port_id: PortId,
sequence: u64,
) -> impl Future<Output = Vec<u8>> + '_ {
async move {
Expand All @@ -291,7 +292,7 @@ impl<C: ChainSpec> Chain for Evm<C> {
destination_channel,
sequence: ack_sequence,
acknowledgement,
}) if ack_dst_port_id == destination_port_id
}) if ack_dst_port_id == destination_port_id.to_string()
&& destination_channel == destination_channel_id.to_string()
&& sequence == ack_sequence =>
{
Expand Down Expand Up @@ -413,26 +414,34 @@ impl<C: ChainSpec> Evm<C> {

pub type EvmClientId = ClientId;

#[derive(Debug)]
// TODO: Don't use debug here, instead impl Error for all error types
#[derive(Debug, thiserror::Error)]
pub enum EvmEventSourceError {
Contract(ContractError<Provider<Ws>>),
ChannelNotFound {
port_id: String,
channel_id: String,
},
#[error(transparent)]
Contract(#[from] ContractError<Provider<Ws>>),
#[error("channel `{channel_id}/{port_id}` not found")]
ChannelNotFound { port_id: String, channel_id: String },
#[error("{0:?}")]
ChannelConversion(TryFromEthAbiErrorOf<Channel>),
ConnectionNotFound {
connection_id: String,
},
#[error("channel `{connection_id}` not found")]
ConnectionNotFound { connection_id: String },
#[error("{0:?}")]
ConnectionConversion(TryFromEthAbiErrorOf<ConnectionEnd<EvmClientId, String>>),
// this is a mess, should be cleaned up
#[error("{0:?}")]
ConnectionOpenInitConnectionConversion(
TryFromEthAbiErrorOf<ConnectionEnd<EvmClientId, String, EmptyString>>,
),
#[error(transparent)]
ClientIdParse(<ClientId as FromStr>::Err),
#[error(transparent)]
ConnectionIdParse(<ConnectionId as FromStr>::Err),
#[error(transparent)]
ChannelIdParse(<ChannelId as FromStr>::Err),
EthAbi(ethers::core::abi::Error),
#[error(transparent)]
PortIdParse(<PortId as FromStr>::Err),
#[error(transparent)]
EthAbi(#[from] ethers::core::abi::Error),
}

impl<C: ChainSpec> EventSource for Evm<C> {
Expand Down Expand Up @@ -557,13 +566,15 @@ impl<C: ChainSpec> EventSource for Evm<C> {
packet_timeout_height: packet_ack.packet.timeout_height.into(),
packet_timeout_timestamp: packet_ack.packet.timeout_timestamp,
packet_sequence: packet_ack.packet.sequence,
packet_src_port: packet_ack.packet.source_port,
packet_src_port: packet_ack.packet.source_port.parse()
.map_err(EvmEventSourceError::PortIdParse)?,
packet_src_channel: packet_ack
.packet
.source_channel
.parse()
.map_err(EvmEventSourceError::ChannelIdParse)?,
packet_dst_port: packet_ack.packet.destination_port,
packet_dst_port: packet_ack.packet.destination_port.parse()
.map_err(EvmEventSourceError::PortIdParse)?,
packet_dst_channel: packet_ack
.packet
.destination_channel
Expand All @@ -581,7 +592,7 @@ impl<C: ChainSpec> EventSource for Evm<C> {
.await?;

Some(IbcEvent::ChannelOpenAck(ChannelOpenAck {
port_id: event.port_id,
port_id: event.port_id.parse().map_err(EvmEventSourceError::PortIdParse)?,
channel_id: event
.channel_id
.parse()
Expand All @@ -601,7 +612,7 @@ impl<C: ChainSpec> EventSource for Evm<C> {
.await?;

Some(IbcEvent::ChannelOpenConfirm(ChannelOpenConfirm {
port_id: event.port_id,
port_id: event.port_id.parse().map_err(EvmEventSourceError::PortIdParse)?,
channel_id: event
.channel_id
.parse()
Expand All @@ -621,14 +632,14 @@ impl<C: ChainSpec> EventSource for Evm<C> {
.await?;

Some(IbcEvent::ChannelOpenInit(ChannelOpenInit {
port_id: event.port_id,
port_id: event.port_id.parse().map_err(EvmEventSourceError::PortIdParse)?,
channel_id: event
.channel_id
.parse()
.map_err(EvmEventSourceError::ChannelIdParse)?,
// TODO: Ensure that event.counterparty_channel_id is `EmptyString`
counterparty_channel_id: EmptyString,
counterparty_port_id: event.counterparty_port_id,
counterparty_channel_id: "".to_string().validate().expect("empty string is a valid empty string; qed;"),
counterparty_port_id: event.counterparty_port_id.parse().map_err(EvmEventSourceError::PortIdParse)?,
connection_id: event
.connection_id
.parse()
Expand All @@ -642,12 +653,12 @@ impl<C: ChainSpec> EventSource for Evm<C> {
.await?;

Some(IbcEvent::ChannelOpenTry(ChannelOpenTry {
port_id: event.port_id,
port_id: event.port_id.parse().map_err(EvmEventSourceError::PortIdParse)?,
channel_id: event
.channel_id
.parse()
.map_err(EvmEventSourceError::ChannelIdParse)?,
counterparty_port_id: event.counterparty_port_id,
counterparty_port_id: event.counterparty_port_id.parse().map_err(EvmEventSourceError::PortIdParse)?,
counterparty_channel_id: channel
.counterparty
.channel_id
Expand Down Expand Up @@ -787,13 +798,15 @@ impl<C: ChainSpec> EventSource for Evm<C> {
packet_timeout_height: event.packet.timeout_height.into(),
packet_timeout_timestamp: event.packet.timeout_timestamp,
packet_sequence: event.packet.sequence,
packet_src_port: event.packet.source_port,
packet_src_port: event.packet.source_port.parse()
.map_err(EvmEventSourceError::PortIdParse)?,
packet_src_channel: event
.packet
.source_channel
.parse()
.map_err(EvmEventSourceError::ChannelIdParse)?,
packet_dst_port: event.packet.destination_port,
packet_dst_port: event.packet.destination_port.parse()
.map_err(EvmEventSourceError::PortIdParse)?,
packet_dst_channel: event
.packet
.destination_channel
Expand All @@ -815,7 +828,8 @@ impl<C: ChainSpec> EventSource for Evm<C> {
packet_timeout_height: event.timeout_height.into(),
packet_timeout_timestamp: event.timeout_timestamp,
packet_sequence: event.sequence,
packet_src_port: event.source_port,
packet_src_port: event.source_port.parse()
.map_err(EvmEventSourceError::PortIdParse)?,
packet_src_channel: event
.source_channel
.parse()
Expand Down
2 changes: 1 addition & 1 deletion lib/chain-utils/src/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl Chain for Union {
&self,
block_hash: H256,
destination_channel_id: unionlabs::id::ChannelId,
destination_port_id: String,
destination_port_id: unionlabs::id::PortId,
sequence: u64,
) -> impl Future<Output = Vec<u8>> + '_ {
async move {
Expand Down
56 changes: 37 additions & 19 deletions lib/unionlabs/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::str::FromStr;

use crate::{
ibc::core::{channel::order::Order, client::height::Height},
id::{ChannelId, ConnectionId},
id::{ChannelId, ConnectionId, PortId},
EmptyString,
};

Expand Down Expand Up @@ -257,23 +257,27 @@ event! {

#[event(tag = "channel_open_init")]
ChannelOpenInit {
port_id: String,
#[parse(PortId::from_str)]
port_id: PortId,
#[parse(ChannelId::from_str)]
channel_id: ChannelId,
#[parse(EmptyString::from_str)]
counterparty_channel_id: EmptyString,
counterparty_port_id: String,
#[parse(PortId::from_str)]
counterparty_port_id: PortId,
#[parse(ConnectionId::from_str)]
connection_id: ConnectionId,
version: String,
},

#[event(tag = "channel_open_try")]
ChannelOpenTry {
port_id: String,
#[parse(PortId::from_str)]
port_id: PortId,
#[parse(ChannelId::from_str)]
channel_id: ChannelId,
counterparty_port_id: String,
#[parse(PortId::from_str)]
counterparty_port_id: PortId,
#[parse(ChannelId::from_str)]
counterparty_channel_id: ChannelId,
#[parse(ConnectionId::from_str)]
Expand All @@ -283,10 +287,12 @@ event! {

#[event(tag = "channel_open_ack")]
ChannelOpenAck {
port_id: String,
#[parse(PortId::from_str)]
port_id: PortId,
#[parse(ChannelId::from_str)]
channel_id: ChannelId,
counterparty_port_id: String,
#[parse(PortId::from_str)]
counterparty_port_id: PortId,
#[parse(ChannelId::from_str)]
counterparty_channel_id: ChannelId,
#[parse(ConnectionId::from_str)]
Expand All @@ -295,10 +301,12 @@ event! {

#[event(tag = "channel_open_confirm")]
ChannelOpenConfirm {
port_id: String,
#[parse(PortId::from_str)]
port_id: PortId,
#[parse(ChannelId::from_str)]
channel_id: ChannelId,
counterparty_port_id: String,
#[parse(PortId::from_str)]
counterparty_port_id: PortId,
#[parse(ChannelId::from_str)]
counterparty_channel_id: ChannelId,
#[parse(ConnectionId::from_str)]
Expand All @@ -318,10 +326,12 @@ event! {
packet_timeout_timestamp: u64,
#[parse(u64::from_str)]
packet_sequence: u64,
packet_src_port: String,
#[parse(PortId::from_str)]
packet_src_port: PortId,
#[parse(ChannelId::from_str)]
packet_src_channel: ChannelId,
packet_dst_port: String,
#[parse(PortId::from_str)]
packet_dst_port: PortId,
#[parse(ChannelId::from_str)]
packet_dst_channel: ChannelId,
#[parse(hex::decode)]
Expand All @@ -344,10 +354,12 @@ event! {
packet_timeout_timestamp: u64,
#[parse(u64::from_str)]
packet_sequence: u64,
packet_src_port: String,
#[parse(PortId::from_str)]
packet_src_port: PortId,
#[parse(ChannelId::from_str)]
packet_src_channel: ChannelId,
packet_dst_port: String,
#[parse(PortId::from_str)]
packet_dst_port: PortId,
#[parse(ChannelId::from_str)]
packet_dst_channel: ChannelId,
#[parse(Order::from_str)]
Expand All @@ -371,10 +383,12 @@ event! {
packet_timeout_timestamp: u64,
#[parse(u64::from_str)]
packet_sequence: u64,
packet_src_port: String,
#[parse(PortId::from_str)]
packet_src_port: PortId,
#[parse(ChannelId::from_str)]
packet_src_channel: ChannelId,
packet_dst_port: String,
#[parse(PortId::from_str)]
packet_dst_port: PortId,
#[parse(ChannelId::from_str)]
packet_dst_channel: ChannelId,
#[parse(Order::from_str)]
Expand All @@ -394,10 +408,12 @@ event! {
packet_timeout_timestamp: u64,
#[parse(u64::from_str)]
packet_sequence: u64,
packet_src_port: String,
#[parse(PortId::from_str)]
packet_src_port: PortId,
#[parse(ChannelId::from_str)]
packet_src_channel: ChannelId,
packet_dst_port: String,
#[parse(PortId::from_str)]
packet_dst_port: PortId,
#[parse(ChannelId::from_str)]
packet_dst_channel: ChannelId,
#[parse(Order::from_str)]
Expand All @@ -414,10 +430,12 @@ event! {
packet_timeout_timestamp: u64,
#[parse(u64::from_str)]
packet_sequence: u64,
packet_src_port: String,
#[parse(PortId::from_str)]
packet_src_port: PortId,
#[parse(ChannelId::from_str)]
packet_src_channel: ChannelId,
packet_dst_port: String,
#[parse(PortId::from_str)]
packet_dst_port: PortId,
#[parse(ChannelId::from_str)]
packet_dst_channel: ChannelId,
#[parse(Order::from_str)]
Expand Down
17 changes: 14 additions & 3 deletions lib/unionlabs/src/ibc/core/channel/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use serde::{Deserialize, Serialize};

use crate::{
errors::{required, MissingField, UnknownEnumVariant},
ibc::core::channel::{counterparty::Counterparty, order::Order, state::State},
ibc::core::channel::{
counterparty::{Counterparty, TryFromChannelCounterpartyError},
order::Order,
state::State,
},
id::{ConnectionId, ConnectionIdValidator},
validated::{Validate, ValidateT},
Proto, TypeUrl,
Expand Down Expand Up @@ -38,6 +42,7 @@ impl From<Channel> for protos::ibc::core::channel::v1::Channel {
pub enum TryFromChannelError {
MissingField(MissingField),
State(UnknownEnumVariant<i32>),
Counterparty(TryFromChannelCounterpartyError),
Ordering(UnknownEnumVariant<i32>),
ConnectionHops(<ConnectionIdValidator as Validate<String>>::Error),
}
Expand All @@ -49,7 +54,9 @@ impl TryFrom<protos::ibc::core::channel::v1::Channel> for Channel {
Ok(Channel {
state: State::try_from(proto.state).map_err(TryFromChannelError::State)?,
ordering: Order::try_from(proto.ordering).map_err(TryFromChannelError::State)?,
counterparty: required!(proto.counterparty)?.into(),
counterparty: required!(proto.counterparty)?
.try_into()
.map_err(TryFromChannelError::Counterparty)?,
connection_hops: proto
.connection_hops
.into_iter()
Expand Down Expand Up @@ -91,6 +98,7 @@ impl From<Channel> for contracts::ibc_handler::IbcCoreChannelV1ChannelData {
pub enum TryFromEthAbiChannelError {
State(UnknownEnumVariant<u8>),
Ordering(UnknownEnumVariant<u8>),
Counterparty(crate::TryFromEthAbiErrorOf<Counterparty>),
ConnectionHops(<ConnectionIdValidator as Validate<String>>::Error),
}

Expand All @@ -110,7 +118,10 @@ impl TryFrom<contracts::ibc_handler::IbcCoreChannelV1ChannelData> for Channel {
.ordering
.try_into()
.map_err(TryFromEthAbiChannelError::Ordering)?,
counterparty: value.counterparty.into(),
counterparty: value
.counterparty
.try_into()
.map_err(TryFromEthAbiChannelError::Counterparty)?,
connection_hops: value
.connection_hops
.into_iter()
Expand Down
Loading

0 comments on commit dc1dcac

Please sign in to comment.