Skip to content

Commit

Permalink
feat: more type conversions in unionlabs (#502)
Browse files Browse the repository at this point in the history
also fixes an error in the naming of the `msg_connection_*` files (they
were previously called `msg_channel_*`, oops)
  • Loading branch information
benluelo authored Aug 18, 2023
2 parents a5991e2 + 8b7dbc5 commit 6eb4e74
Show file tree
Hide file tree
Showing 27 changed files with 286 additions and 54 deletions.
2 changes: 2 additions & 0 deletions dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Feegrant
GOPATH
GOPRIVATE
Getenv
HKTs
Hasher
Hyperlane
IAVL
Expand All @@ -44,6 +45,7 @@ PINGPONG
PRECOMMIT
PREVOTE
PROTOC
Parameterizable
Paramspace
Partset
Patchelf
Expand Down
49 changes: 45 additions & 4 deletions lib/unionlabs/src/ethereum_consts_traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::fmt::Debug;
use std::str::FromStr;
use std::{fmt, str::FromStr};

use serde::{Deserialize, Serialize};
use typenum::Unsigned;
Expand Down Expand Up @@ -36,14 +36,52 @@ impl FromStr for PresetBaseKind {
}
}

/// A way to emulate HKTs in the context of [`ChainSpec`]s.
///
/// # Example
///
/// ```rs
/// struct Foo<C: ChainSpec>(PhantomData<C>);
///
/// struct AnyFoo;
///
/// impl ChainSpecParameterizable for AnyFoo {
/// type T<C: ChainSpec> = Foo<C>;
/// }
///
/// struct Bar {
/// foo: AnyChainSpec<AnyFoo>,
/// }
/// ```
pub trait ChainSpecParameterizable {
type T<C: ChainSpec>;
}

pub enum AnyChainSpec<T: ChainSpecParameterizable> {
Mainnet(T::T<Mainnet>),
Minimal(T::T<Minimal>),
}

impl<T: ChainSpecParameterizable> Debug for AnyChainSpec<T>
where
T::T<Mainnet>: Debug,
T::T<Minimal>: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AnyChainSpec::Mainnet(t) => f.debug_tuple("Mainnet").field(t).finish(),
AnyChainSpec::Minimal(t) => f.debug_tuple("Minimal").field(t).finish(),
}
}
}

macro_rules! consts_traits {
($($CONST:ident $(,)?),+) => {
// Extra traits are required because the builtin derives bound all generic
// types unconditionally

$(
#[allow(non_camel_case_types)]
pub trait $CONST: 'static {
// Extra traits are required because the builtin derives bound all generic
// types unconditionally
type $CONST: Unsigned + Debug + Clone + PartialEq + Send + Sync;
}

Expand All @@ -57,12 +95,14 @@ macro_rules! consts_traits {
)+

pub trait ChainSpec: 'static + Debug + Clone + PartialEq + Send + Sync + $($CONST+)+ {
const PRESET: preset::Preset;
const PRESET_BASE_KIND: PresetBaseKind;

type PERIOD: 'static + Unsigned;
}

impl ChainSpec for Minimal {
const PRESET: preset::Preset = preset::MINIMAL;
const PRESET_BASE_KIND: PresetBaseKind = PresetBaseKind::Minimal;

type PERIOD = typenum::Prod<
Expand All @@ -72,6 +112,7 @@ macro_rules! consts_traits {
}

impl ChainSpec for Mainnet {
const PRESET: preset::Preset = preset::MAINNET;
const PRESET_BASE_KIND: PresetBaseKind = PresetBaseKind::Mainnet;

type PERIOD = typenum::Prod<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::{ibc::core::client::height::Height, CosmosAccountId, MsgIntoProto};
use crate::{ibc::core::client::height::Height, CosmosAccountId, MsgIntoProto, TypeUrl};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MsgChannelOpenConfirm {
Expand All @@ -10,6 +10,10 @@ pub struct MsgChannelOpenConfirm {
pub proof_height: Height,
}

impl TypeUrl for protos::ibc::core::channel::v1::MsgChannelOpenConfirm {
const TYPE_URL: &'static str = "/ibc.core.channel.v1.MsgChannelOpenConfirm";
}

impl MsgIntoProto for MsgChannelOpenConfirm {
type Proto = protos::ibc::core::channel::v1::MsgChannelOpenConfirm;

Expand Down
6 changes: 5 additions & 1 deletion lib/unionlabs/src/ibc/core/channel/msg_channel_open_init.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use serde::{Deserialize, Serialize};

use crate::{ibc::core::channel::channel::Channel, CosmosAccountId, MsgIntoProto};
use crate::{ibc::core::channel::channel::Channel, CosmosAccountId, MsgIntoProto, TypeUrl};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MsgChannelOpenInit {
pub port_id: String,
pub channel: Channel,
}

impl TypeUrl for protos::ibc::core::channel::v1::MsgChannelOpenInit {
const TYPE_URL: &'static str = "/ibc.core.channel.v1.MsgChannelOpenInit";
}

#[derive(Debug, Clone)]
pub struct MsgChannelOpenInitResponse {
pub channel_id: String,
Expand Down
6 changes: 5 additions & 1 deletion lib/unionlabs/src/ibc/core/channel/msg_recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};

use crate::{
ibc::core::{channel::packet::Packet, client::height::Height},
CosmosAccountId, MsgIntoProto,
CosmosAccountId, MsgIntoProto, TypeUrl,
};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand All @@ -12,6 +12,10 @@ pub struct MsgRecvPacket {
pub proof_height: Height,
}

impl TypeUrl for protos::ibc::core::channel::v1::MsgRecvPacket {
const TYPE_URL: &'static str = "/ibc.core.channel.v1.MsgRecvPacket";
}

impl MsgIntoProto for MsgRecvPacket {
type Proto = protos::ibc::core::channel::v1::MsgRecvPacket;

Expand Down
14 changes: 7 additions & 7 deletions lib/unionlabs/src/ibc/core/client/msg_update_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::{CosmosAccountId, IntoProto, MsgIntoProto, TypeUrl};
use crate::{ibc::google::protobuf::any::Any, CosmosAccountId, IntoProto, MsgIntoProto, TypeUrl};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MsgUpdateClient<Header> {
Expand All @@ -10,21 +10,21 @@ pub struct MsgUpdateClient<Header> {
pub client_message: Header,
}

impl TypeUrl for protos::ibc::core::client::v1::MsgUpdateClient {
const TYPE_URL: &'static str = "/ibc.core.client.v1.MsgUpdateClient";
}

impl<Header> MsgIntoProto for MsgUpdateClient<Header>
where
Header: IntoProto<Proto = protos::google::protobuf::Any>,
Header: IntoProto,
{
type Proto = protos::ibc::core::client::v1::MsgUpdateClient;

fn into_proto_with_signer(self, signer: &CosmosAccountId) -> Self::Proto {
Self::Proto {
client_id: self.client_id,
client_message: Some(self.client_message.into_proto()),
client_message: Some(Any(self.client_message).into_proto()),
signer: signer.to_string(),
}
}
}

impl TypeUrl for protos::ibc::core::client::v1::MsgUpdateClient {
const TYPE_URL: &'static str = "/ibc.core.client.v1.MsgUpdateClient";
}
8 changes: 4 additions & 4 deletions lib/unionlabs/src/ibc/core/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod counterparty;
pub mod state;
pub mod version;

pub mod msg_channel_open_ack;
pub mod msg_channel_open_confirm;
pub mod msg_channel_open_init;
pub mod msg_channel_open_try;
pub mod msg_connection_open_ack;
pub mod msg_connection_open_confirm;
pub mod msg_connection_open_init;
pub mod msg_connection_open_try;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};

use crate::{
ibc::core::{client::height::Height, connection::version::Version},
CosmosAccountId, IntoProto, MsgIntoProto,
CosmosAccountId, IntoProto, MsgIntoProto, TypeUrl,
};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand All @@ -20,10 +20,13 @@ pub struct MsgConnectionOpenAck<ClientState> {
pub consensus_height: Height,
}

impl TypeUrl for protos::ibc::core::connection::v1::MsgConnectionOpenAck {
const TYPE_URL: &'static str = "/ibc.core.connection.v1.MsgConnectionOpenAck";
}

impl<ClientState> MsgIntoProto for MsgConnectionOpenAck<ClientState>
where
ClientState: IntoProto<Proto = protos::google::protobuf::Any>,
// <ClientState as IntoProto>::Proto: TypeUrl,
{
type Proto = protos::ibc::core::connection::v1::MsgConnectionOpenAck;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::{ibc::core::client::height::Height, CosmosAccountId, MsgIntoProto};
use crate::{ibc::core::client::height::Height, CosmosAccountId, MsgIntoProto, TypeUrl};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MsgConnectionOpenConfirm {
Expand All @@ -9,6 +9,10 @@ pub struct MsgConnectionOpenConfirm {
pub proof_height: Height,
}

impl TypeUrl for protos::ibc::core::connection::v1::MsgConnectionOpenConfirm {
const TYPE_URL: &'static str = "/ibc.core.connection.v1.MsgConnectionOpenConfirm";
}

impl MsgIntoProto for MsgConnectionOpenConfirm {
type Proto = protos::ibc::core::connection::v1::MsgConnectionOpenConfirm;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
client::height::Height,
connection::{counterparty::Counterparty, version::Version},
},
CosmosAccountId, MsgIntoProto,
CosmosAccountId, MsgIntoProto, TypeUrl,
};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand All @@ -22,6 +22,10 @@ pub struct MsgConnectionOpenInitResponse {
pub inclusion_height: Height,
}

impl TypeUrl for protos::ibc::core::connection::v1::MsgConnectionOpenInit {
const TYPE_URL: &'static str = "/ibc.core.connection.v1.MsgConnectionOpenInit";
}

impl MsgIntoProto for MsgConnectionOpenInit {
type Proto = protos::ibc::core::connection::v1::MsgConnectionOpenInit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
client::height::Height,
connection::{counterparty::Counterparty, version::Version},
},
CosmosAccountId, IntoProto, MsgIntoProto,
CosmosAccountId, IntoProto, MsgIntoProto, TypeUrl,
};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand All @@ -22,10 +22,13 @@ pub struct MsgConnectionOpenTry<ClientState> {
pub consensus_height: Height,
}

impl TypeUrl for protos::ibc::core::connection::v1::MsgConnectionOpenTry {
const TYPE_URL: &'static str = "/ibc.core.connection.v1.MsgConnectionOpenTry";
}

impl<ClientState> MsgIntoProto for MsgConnectionOpenTry<ClientState>
where
ClientState: IntoProto<Proto = protos::google::protobuf::Any>,
// <ClientState as IntoProto>::Proto: ,
{
type Proto = protos::ibc::core::connection::v1::MsgConnectionOpenTry;

Expand Down
39 changes: 38 additions & 1 deletion lib/unionlabs/src/ibc/lightclients/cometbls/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};

use crate::{
ethereum::H256, ibc::core::client::height::Height,
tendermint::types::signed_header::SignedHeader,
tendermint::types::signed_header::SignedHeader, Proto, TypeUrl,
};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand All @@ -14,6 +14,43 @@ pub struct Header {
pub zero_knowledge_proof: Vec<u8>,
}

impl Proto for Header {
type Proto = protos::union::ibc::lightclients::cometbls::v1::Header;
}

impl TypeUrl for protos::union::ibc::lightclients::cometbls::v1::Header {
const TYPE_URL: &'static str = "/union.ibc.lightclients.cometbls.v1.Header";
}

// #[derive(Debug)]
// pub enum TryFromHeaderError {}

// impl TryFrom<protos::union::ibc::lightclients::cometbls::v1::Header> for Header {
// type Error = TryFromHeaderError;

// fn try_from(
// value: protos::union::ibc::lightclients::cometbls::v1::Header,
// ) -> Result<Self, Self::Error> {
// Ok(Self {
// signed_header: required!(value.signed_header)?.into(),
// untrusted_validator_set_root: todo!(),
// trusted_height: todo!(),
// zero_knowledge_proof: todo!(),
// })
// }
// }

impl From<Header> for protos::union::ibc::lightclients::cometbls::v1::Header {
fn from(value: Header) -> Self {
Self {
signed_header: Some(value.signed_header.into()),
untrusted_validator_set_root: value.untrusted_validator_set_root.into(),
trusted_height: Some(value.trusted_height.into()),
zero_knowledge_proof: value.zero_knowledge_proof,
}
}
}

#[cfg(feature = "ethabi")]
impl From<Header> for contracts::glue::UnionIbcLightclientsCometblsV1HeaderData {
fn from(value: Header) -> Self {
Expand Down
8 changes: 4 additions & 4 deletions lib/unionlabs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ pub trait IntoProto: Proto + Into<Self::Proto> {
}
}

impl<T> IntoProto for T where T: Proto + Into<T::Proto> {}
impl<T> FromProto for T where T: Proto + From<T::Proto> {}
impl<T> TryFromProto for T where T: Proto + TryFrom<T::Proto> {}

/// A type that can be infallibly converted from it's protobuf representation.
pub trait FromProto: Proto + From<Self::Proto> {
fn from_proto(proto: Self::Proto) -> Self {
Expand Down Expand Up @@ -149,6 +145,10 @@ pub trait TryFromProto: Proto + TryFrom<Self::Proto> {
}
}

impl<T> IntoProto for T where T: Proto + Into<T::Proto> {}
impl<T> FromProto for T where T: Proto + From<T::Proto> {}
impl<T> TryFromProto for T where T: Proto + TryFrom<T::Proto> {}

#[derive(Debug)]
pub enum TryFromProtoBytesError<E> {
TryFromProto(E),
Expand Down
19 changes: 18 additions & 1 deletion lib/unionlabs/src/tendermint/types/canonical_block_header.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
use serde::{Deserialize, Serialize};

use crate::ethereum::H256;
use crate::{ethereum::H256, Proto, TypeUrl};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CanonicalPartSetHeader {
pub total: u32,
pub hash: H256,
}

impl Proto for CanonicalPartSetHeader {
type Proto = protos::tendermint::types::CanonicalPartSetHeader;
}

impl TypeUrl for protos::tendermint::types::CanonicalPartSetHeader {
const TYPE_URL: &'static str = "/tendermint.types.CanonicalPartSetHeader";
}

impl From<CanonicalPartSetHeader> for protos::tendermint::types::CanonicalPartSetHeader {
fn from(value: CanonicalPartSetHeader) -> Self {
Self {
hash: value.hash.into(),
total: value.total,
}
}
}
Loading

0 comments on commit 6eb4e74

Please sign in to comment.