Skip to content

Commit

Permalink
feat!: add Proto trait as supertrait of {From,TryFrom,Into}Proto
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `{From,TryFrom,Into}Proto` no longer each define an
associated type `Proto`, instead there is a supertrait `Proto` and the
aformentioned traits are now a bounds alias/ convenience over
`T: Proto + {From,TryFrom,Into}<T::Proto>`.
  • Loading branch information
benluelo committed Aug 4, 2023
1 parent 304cd7d commit b21bef0
Show file tree
Hide file tree
Showing 30 changed files with 92 additions and 248 deletions.
4 changes: 2 additions & 2 deletions cosmwasm/cw20-ics20/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
from_binary, to_binary, Addr, Binary, Deps, DepsMut, Env, IbcChannel, IbcMsg, IbcQuery,
MessageInfo, Order, PortIdResponse, Response, StdError, StdResult,
from_binary, to_binary, Addr, Binary, Deps, DepsMut, Env, IbcMsg, IbcQuery, MessageInfo, Order,
PortIdResponse, Response, StdError, StdResult,
};
use cw2::{get_contract_version, set_contract_version};
use cw20::{Cw20Coin, Cw20ReceiveMsg};
Expand Down
2 changes: 1 addition & 1 deletion lib/unionlabs/src/ethereum/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ mod tests {

assert_proto_roundtrip(&finality_update.attested_header);

dbg!(U256::from_dec_str("77"));
dbg!(U256::from_dec_str("77").unwrap());

assert_eq!(
finality_update
Expand Down
8 changes: 2 additions & 6 deletions lib/unionlabs/src/ibc/core/channel/channel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
errors::MissingField,
ibc::core::channel::{counterparty::Counterparty, order::Order, state::State},
IntoProto, TryFromProto, TypeUrl,
Proto, TypeUrl,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -43,11 +43,7 @@ impl TryFrom<protos::ibc::core::channel::v1::Channel> for Channel {
}
}

impl IntoProto for Channel {
type Proto = protos::ibc::core::channel::v1::Channel;
}

impl TryFromProto for Channel {
impl Proto for Channel {
type Proto = protos::ibc::core::channel::v1::Channel;
}

Expand Down
10 changes: 2 additions & 8 deletions lib/unionlabs/src/ibc/core/channel/packet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::{
errors::MissingField, ibc::core::client::height::Height, IntoProto, TryFromProto, TypeUrl,
};
use crate::{errors::MissingField, ibc::core::client::height::Height, Proto, TypeUrl};

#[derive(Debug, Clone)]
pub struct Packet {
Expand All @@ -14,11 +12,7 @@ pub struct Packet {
pub timeout_timestamp: u64,
}

impl IntoProto for Packet {
type Proto = protos::ibc::core::channel::v1::Packet;
}

impl TryFromProto for Packet {
impl Proto for Packet {
type Proto = protos::ibc::core::channel::v1::Packet;
}

Expand Down
8 changes: 2 additions & 6 deletions lib/unionlabs/src/ibc/core/connection/connection_end.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
errors::{MissingField, UnknownEnumVariant},
ibc::core::connection::{counterparty::Counterparty, state::State, version::Version},
IntoProto, TryFromProto, TypeUrl,
Proto, TypeUrl,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -52,11 +52,7 @@ impl TryFrom<protos::ibc::core::connection::v1::ConnectionEnd> for ConnectionEnd
}
}

impl IntoProto for ConnectionEnd {
type Proto = protos::ibc::core::connection::v1::ConnectionEnd;
}

impl TryFromProto for ConnectionEnd {
impl Proto for ConnectionEnd {
type Proto = protos::ibc::core::connection::v1::ConnectionEnd;
}

Expand Down
26 changes: 7 additions & 19 deletions lib/unionlabs/src/ibc/google/protobuf/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use serde::{
Deserialize, Serialize,
};

use crate::{CosmosAccountId, IntoProto, MsgIntoProto, TryFromProto, TryFromProtoErrorOf, TypeUrl};
use crate::{
CosmosAccountId, IntoProto, MsgIntoProto, Proto, TryFromProto, TryFromProtoErrorOf, TypeUrl,
};

/// Wrapper type to indicate that a type is to be serialized as an Any.
#[derive(Debug, Clone)]
Expand All @@ -16,7 +18,6 @@ pub struct Any<T>(pub T);
impl<'de, T> Deserialize<'de> for Any<T>
where
T: Deserialize<'de> + TryFromProto,
<T as TryFromProto>::Proto: TypeUrl,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand All @@ -27,7 +28,6 @@ where
impl<'de, T> Visitor<'de> for AnyVisitor<T>
where
T: Deserialize<'de> + TryFromProto,
<T as TryFromProto>::Proto: TypeUrl,
{
type Value = Any<T>;

Expand Down Expand Up @@ -81,7 +81,6 @@ where
impl<T> Serialize for Any<T>
where
T: Serialize + IntoProto,
<T as IntoProto>::Proto: TypeUrl,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -97,20 +96,18 @@ where
impl<T> From<Any<T>> for protos::google::protobuf::Any
where
T: IntoProto,
<T as IntoProto>::Proto: TypeUrl,
{
fn from(val: Any<T>) -> Self {
protos::google::protobuf::Any {
type_url: <T as IntoProto>::Proto::TYPE_URL.to_string(),
type_url: <T as Proto>::Proto::TYPE_URL.to_string(),
value: val.0.into_proto().encode_to_vec(),
}
}
}

impl<T> IntoProto for Any<T>
impl<T> Proto for Any<T>
where
T: IntoProto,
<T as IntoProto>::Proto: TypeUrl,
{
type Proto = protos::google::protobuf::Any;
}
Expand Down Expand Up @@ -151,8 +148,8 @@ where
impl<T> TryFrom<protos::google::protobuf::Any> for Any<T>
where
T: TryFromProto,
T::Proto: TypeUrl + Default,
<T as TryFrom<T::Proto>>::Error: Debug,
T::Proto: Default,
TryFromProtoErrorOf<T>: Debug,
{
type Error = TryFromAnyError<T>;

Expand All @@ -171,12 +168,3 @@ where
}
}
}

impl<T> TryFromProto for Any<T>
where
T: TryFromProto,
T::Proto: TypeUrl + Default,
TryFromProtoErrorOf<T>: Debug,
{
type Proto = protos::google::protobuf::Any;
}
8 changes: 2 additions & 6 deletions lib/unionlabs/src/ibc/lightclients/cometbls/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
core::client::height::Height, google::protobuf::duration::Duration,
lightclients::tendermint::fraction::Fraction,
},
IntoProto, TryFromProto, TypeUrl,
Proto, TypeUrl,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -36,11 +36,7 @@ impl TypeUrl for protos::union::ibc::lightclients::cometbls::v1::ClientState {
const TYPE_URL: &'static str = "/union.ibc.lightclients.cometbls.v1.ClientState";
}

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

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

Expand Down
11 changes: 2 additions & 9 deletions lib/unionlabs/src/ibc/lightclients/cometbls/consensus_state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
errors::MissingField, ibc::core::commitment::merkle_root::MerkleRoot, IntoProto, TryFromProto,
TypeUrl,
};
use crate::{errors::MissingField, ibc::core::commitment::merkle_root::MerkleRoot, Proto, TypeUrl};

#[derive(Debug, Clone)]
pub struct ConsensusState {
Expand All @@ -26,11 +23,7 @@ impl TypeUrl for protos::union::ibc::lightclients::cometbls::v1::ConsensusState
const TYPE_URL: &'static str = "/union.ibc.lightclients.cometbls.v1.ConsensusState";
}

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

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

Expand Down
8 changes: 2 additions & 6 deletions lib/unionlabs/src/ibc/lightclients/ethereum/account_update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::{ibc::lightclients::ethereum::proof::Proof, FromProto, IntoProto, TypeUrl};
use crate::{ibc::lightclients::ethereum::proof::Proof, Proto, TypeUrl};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct AccountUpdate {
Expand All @@ -23,11 +23,7 @@ impl From<protos::union::ibc::lightclients::ethereum::v1::AccountUpdate> for Acc
}
}

impl IntoProto for AccountUpdate {
type Proto = protos::union::ibc::lightclients::ethereum::v1::AccountUpdate;
}

impl FromProto for AccountUpdate {
impl Proto for AccountUpdate {
type Proto = protos::union::ibc::lightclients::ethereum::v1::AccountUpdate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use ssz::{Decode, Encode};
use tree_hash::TreeHash;

use crate::{errors::InvalidLength, ethereum::H256, IntoProto, TryFromProto, TypeUrl};
use crate::{errors::InvalidLength, ethereum::H256, Proto, TypeUrl};

#[derive(Clone, Debug, PartialEq, Encode, Decode, TreeHash, Serialize, Deserialize)]
pub struct BeaconBlockHeader {
Expand Down Expand Up @@ -65,10 +65,6 @@ impl TypeUrl for protos::union::ibc::lightclients::ethereum::v1::BeaconBlockHead
const TYPE_URL: &'static str = "/union.ibc.lightclients.ethereum.v1.BeaconBlockHeader";
}

impl TryFromProto for BeaconBlockHeader {
type Proto = protos::union::ibc::lightclients::ethereum::v1::BeaconBlockHeader;
}

impl IntoProto for BeaconBlockHeader {
impl Proto for BeaconBlockHeader {
type Proto = protos::union::ibc::lightclients::ethereum::v1::BeaconBlockHeader;
}
8 changes: 2 additions & 6 deletions lib/unionlabs/src/ibc/lightclients/ethereum/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
core::client::height::Height,
lightclients::{ethereum::fork_parameters::ForkParameters, tendermint::fraction::Fraction},
},
IntoProto, TryFromProto, TryFromProtoErrorOf, TypeUrl,
Proto, TryFromProtoErrorOf, TypeUrl,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -30,7 +30,7 @@ impl TypeUrl for protos::union::ibc::lightclients::ethereum::v1::ClientState {
const TYPE_URL: &'static str = "/union.ibc.lightclients.ethereum.v1.ClientState";
}

impl IntoProto for ClientState {
impl Proto for ClientState {
type Proto = protos::union::ibc::lightclients::ethereum::v1::ClientState;
}

Expand Down Expand Up @@ -96,7 +96,3 @@ impl TryFrom<protos::union::ibc::lightclients::ethereum::v1::ClientState> for Cl
})
}
}

impl TryFromProto for ClientState {
type Proto = protos::union::ibc::lightclients::ethereum::v1::ClientState;
}
10 changes: 2 additions & 8 deletions lib/unionlabs/src/ibc/lightclients/ethereum/consensus_state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::{
bls::BlsPublicKey, errors::InvalidLength, ethereum::H256, IntoProto, TryFromProto, TypeUrl,
};
use crate::{bls::BlsPublicKey, errors::InvalidLength, ethereum::H256, Proto, TypeUrl};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ConsensusState {
Expand Down Expand Up @@ -68,14 +66,10 @@ impl TryFrom<protos::union::ibc::lightclients::ethereum::v1::ConsensusState> for
}
}

impl IntoProto for ConsensusState {
impl Proto for ConsensusState {
type Proto = protos::union::ibc::lightclients::ethereum::v1::ConsensusState;
}

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

impl TryFromProto for ConsensusState {
type Proto = protos::union::ibc::lightclients::ethereum::v1::ConsensusState;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
errors::InvalidLength,
ethereum::{Address, H256},
ethereum_consts_traits::{BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES},
IntoProto, TryFromProto, TypeUrl,
Proto, TypeUrl,
};

#[derive(Clone, Debug, PartialEq, Encode, Decode, TreeHash, Serialize, Deserialize)]
Expand Down Expand Up @@ -153,10 +153,6 @@ impl TypeUrl for protos::union::ibc::lightclients::ethereum::v1::ExecutionPayloa
const TYPE_URL: &'static str = "/union.ibc.lightclients.ethereum.v1.ExecutionPayloadHeader";
}

impl<C: BYTES_PER_LOGS_BLOOM + MAX_EXTRA_DATA_BYTES> TryFromProto for ExecutionPayloadHeader<C> {
type Proto = protos::union::ibc::lightclients::ethereum::v1::ExecutionPayloadHeader;
}

impl<C: BYTES_PER_LOGS_BLOOM + MAX_EXTRA_DATA_BYTES> IntoProto for ExecutionPayloadHeader<C> {
impl<C: BYTES_PER_LOGS_BLOOM + MAX_EXTRA_DATA_BYTES> Proto for ExecutionPayloadHeader<C> {
type Proto = protos::union::ibc::lightclients::ethereum::v1::ExecutionPayloadHeader;
}
8 changes: 2 additions & 6 deletions lib/unionlabs/src/ibc/lightclients/ethereum/fork.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use ssz::{Decode, Encode};

use crate::{errors::InvalidLength, ethereum::Version, IntoProto, TryFromProto, TypeUrl};
use crate::{errors::InvalidLength, ethereum::Version, Proto, TypeUrl};

#[derive(Debug, Clone, PartialEq, Encode, Decode, Serialize, Deserialize)]
pub struct Fork {
Expand Down Expand Up @@ -35,10 +35,6 @@ impl TypeUrl for protos::union::ibc::lightclients::ethereum::v1::Fork {
const TYPE_URL: &'static str = "/union.ibc.lightclients.ethereum.v1.Fork";
}

impl IntoProto for Fork {
type Proto = protos::union::ibc::lightclients::ethereum::v1::Fork;
}

impl TryFromProto for Fork {
impl Proto for Fork {
type Proto = protos::union::ibc::lightclients::ethereum::v1::Fork;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
errors::{InvalidLength, MissingField},
ethereum::Version,
ibc::lightclients::ethereum::fork::Fork,
IntoProto, TryFromProto, TryFromProtoErrorOf, TypeUrl,
Proto, TryFromProtoErrorOf, TypeUrl,
};

#[derive(Debug, Clone, PartialEq, Encode, Decode, Serialize, Deserialize)]
Expand Down Expand Up @@ -86,10 +86,6 @@ impl TypeUrl for protos::union::ibc::lightclients::ethereum::v1::ForkParameters
const TYPE_URL: &'static str = "/union.ibc.lightclients.ethereum.v1.ForkParameters";
}

impl IntoProto for ForkParameters {
type Proto = protos::union::ibc::lightclients::ethereum::v1::ForkParameters;
}

impl TryFromProto for ForkParameters {
impl Proto for ForkParameters {
type Proto = protos::union::ibc::lightclients::ethereum::v1::ForkParameters;
}
10 changes: 2 additions & 8 deletions lib/unionlabs/src/ibc/lightclients/ethereum/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
account_update::AccountUpdate, light_client_update::LightClientUpdate,
trusted_sync_committee::TrustedSyncCommittee,
},
IntoProto, TryFromProto, TryFromProtoErrorOf, TypeUrl,
Proto, TryFromProtoErrorOf, TypeUrl,
};

// trait alias would be nice
Expand Down Expand Up @@ -74,13 +74,7 @@ impl<C: SYNC_COMMITTEE_SIZE + BYTES_PER_LOGS_BLOOM + MAX_EXTRA_DATA_BYTES>
}
}

impl<C: SYNC_COMMITTEE_SIZE + BYTES_PER_LOGS_BLOOM + MAX_EXTRA_DATA_BYTES> IntoProto for Header<C> {
type Proto = protos::union::ibc::lightclients::ethereum::v1::Header;
}

impl<C: SYNC_COMMITTEE_SIZE + BYTES_PER_LOGS_BLOOM + MAX_EXTRA_DATA_BYTES> TryFromProto
for Header<C>
{
impl<C: SYNC_COMMITTEE_SIZE + BYTES_PER_LOGS_BLOOM + MAX_EXTRA_DATA_BYTES> Proto for Header<C> {
type Proto = protos::union::ibc::lightclients::ethereum::v1::Header;
}

Expand Down
Loading

0 comments on commit b21bef0

Please sign in to comment.