From 6e231ae01e02c45acd3c2dd5a8fc3a40430aa130 Mon Sep 17 00:00:00 2001 From: Soares Chen Date: Wed, 18 Sep 2024 17:14:03 +0200 Subject: [PATCH] Protobuf encoding improvements (#432) * Add protobuf encoding for Height and CommitmentRoot * Relax MutEncoder constraint for EncodeU64ProtoField to use TryInto * Wire up encoding for Timestamp and CommitmentRoot * Encode Timestamp directly without using prost::Message * Fix clippy * Disable default features for ibc and ibc-proto * Redesign impl_type_url! macro to be implemented directly by schema type * Implement protobuf encoding for WasmClientMessage * Rename WrappedTendermintClientState to WasmTendermintClientState * Add ConvertIbcAny * Rename EncodeWithContext to WithContext * Implement Converter for WithContext * Add encode/decode via ClientMessage converters * Use prost Any * Add changelog --- CHANGELOG.md | 9 +++ Cargo.lock | 1 + Cargo.toml | 4 +- .../src/encoding/components.rs | 2 +- .../src/encoding/convert.rs | 10 +-- .../src/encoding/type_url.rs | 21 +++---- .../cosmos-encoding-components/Cargo.toml | 1 + .../src/components.rs | 10 +++ .../src/impls/any.rs | 30 +++++++++ .../src/impls/commitment_root.rs | 45 +++++++++++++ .../src/impls/height.rs | 9 +-- .../src/impls/mod.rs | 3 + .../src/impls/timestamp.rs | 63 +++++++++++++++++++ .../cosmos-relayer/src/contexts/encoding.rs | 7 ++- .../cosmos/cosmos-relayer/src/impls/error.rs | 5 +- .../cosmos-wasm-relayer/src/context/chain.rs | 4 +- .../src/context/encoding.rs | 12 ++-- .../src/encoding/convert.rs | 16 ++--- .../src/encoding/encode.rs | 6 +- .../src/encoding/encode_mut.rs | 2 + .../src/encoding/type_url.rs | 2 + .../src/impls/client_state.rs | 12 ++-- .../src/types/client_state.rs | 22 +++---- .../src/impls/encode_mut/pair.rs | 4 +- .../src/impls/with_context.rs | 20 ++++-- .../src/impls/encode_mut/proto_field/u64.rs | 8 +-- .../src/impls/via_any.rs | 11 ++-- .../src/macros.rs | 8 +-- .../src/encoding/components.rs | 2 +- .../src/encoding/convert.rs | 10 +-- .../src/encoding/type_url.rs | 24 +++---- .../src/types/client_state.rs | 8 ++- .../src/types/consensus_state.rs | 5 +- .../src/components.rs | 60 ++++++++++++------ .../src/impls/convert/client_message.rs | 41 ++++++++++++ .../src/impls/convert/mod.rs | 1 + .../src/impls/encode/client_message.rs | 46 ++++++++++++++ .../src/impls/encode/client_state.rs | 6 +- .../src/impls/encode/mod.rs | 1 + .../wasm-encoding-components/src/impls/mod.rs | 1 + .../src/types/client_message.rs | 3 + .../wasm-encoding-components/src/types/mod.rs | 1 + 42 files changed, 427 insertions(+), 129 deletions(-) create mode 100644 crates/cosmos/cosmos-encoding-components/src/impls/any.rs create mode 100644 crates/cosmos/cosmos-encoding-components/src/impls/commitment_root.rs create mode 100644 crates/cosmos/cosmos-encoding-components/src/impls/timestamp.rs create mode 100644 crates/wasm/wasm-encoding-components/src/impls/convert/client_message.rs create mode 100644 crates/wasm/wasm-encoding-components/src/impls/convert/mod.rs create mode 100644 crates/wasm/wasm-encoding-components/src/impls/encode/client_message.rs create mode 100644 crates/wasm/wasm-encoding-components/src/types/client_message.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index ea27c74d0..567be84ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ ## v0.2.0 (pre-release) +- Protobuf encoding improvements [#432](https://github.com/informalsystems/hermes-sdk/pull/432) + - Redesign `impl_type_url!` macro to implement `SchemaGetter` on existing component type. + - Schema components that only use `impl_type_url!` directly are no longer wrapped with `DelegateEncoding`. + - Rename `EncodeWithContext` to `WithContext`. + - Rename `WrappedTendermintClientState` to `WasmTendermintClientState`. + - Implement Protobuf encoding for `WasmClientMessage`. + - Implement `MutEncoder` for `Timestamp` and `CommitmentRoot`. + - Relax `MutEncoder` constraint for `EncodeU64ProtoField` to use `TryInto`. + - Introduce cosmos- and wasm- encoding components crates [#431](https://github.com/informalsystems/hermes-sdk/pull/431) - Add new `hermes-cosmos-encoding-components` crate. - Add new `hermes-wasm-encoding-components` crate. diff --git a/Cargo.lock b/Cargo.lock index 79656fa6d..b85fd2119 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1646,6 +1646,7 @@ dependencies = [ "hermes-encoding-components", "hermes-protobuf-encoding-components", "ibc", + "ibc-proto", "prost", "prost-types", ] diff --git a/Cargo.toml b/Cargo.toml index 4cb774f26..e0f464174 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,8 +67,8 @@ overflow-checks = true [workspace.dependencies] async-trait = { version = "0.1.82" } -ibc = { version = "0.54.0" } -ibc-proto = { version = "0.47.0" } +ibc = { version = "0.54.0", default-features = false } +ibc-proto = { version = "0.47.0", default-features = false } ibc-relayer = { version = "0.29.2" } ibc-relayer-types = { version = "0.29.2" } ibc-telemetry = { version = "0.29.2" } diff --git a/crates/cosmos/cosmos-chain-components/src/encoding/components.rs b/crates/cosmos/cosmos-chain-components/src/encoding/components.rs index cacd3fac7..6a5f0ecb7 100644 --- a/crates/cosmos/cosmos-chain-components/src/encoding/components.rs +++ b/crates/cosmos/cosmos-chain-components/src/encoding/components.rs @@ -40,6 +40,6 @@ define_components! { ]: CosmosEncodingComponents, SchemaGetterComponent: - DelegateEncoding, + CosmosTypeUrlSchemas, } } diff --git a/crates/cosmos/cosmos-chain-components/src/encoding/convert.rs b/crates/cosmos/cosmos-chain-components/src/encoding/convert.rs index 9ac273d75..29e5df3d5 100644 --- a/crates/cosmos/cosmos-chain-components/src/encoding/convert.rs +++ b/crates/cosmos/cosmos-chain-components/src/encoding/convert.rs @@ -1,6 +1,6 @@ use cgp::prelude::*; use hermes_encoding_components::impls::convert::{ConvertFrom, TryConvertFrom}; -use hermes_encoding_components::impls::with_context::EncodeWithContext; +use hermes_encoding_components::impls::with_context::WithContext; use hermes_protobuf_encoding_components::impls::any::{DecodeAsAnyProtobuf, EncodeAsAnyProtobuf}; use hermes_protobuf_encoding_components::types::strategy::ViaProtobuf; use ibc::core::commitment_types::merkle::MerkleProof; @@ -25,10 +25,10 @@ delegate_components! { (MerkleProof, ProtoMerkleProof): ConvertFrom, (ProtoMerkleProof, MerkleProof): TryConvertFrom, - (TendermintClientState, Any): EncodeAsAnyProtobuf, - (Any, TendermintClientState): DecodeAsAnyProtobuf, + (TendermintClientState, Any): EncodeAsAnyProtobuf, + (Any, TendermintClientState): DecodeAsAnyProtobuf, - (TendermintConsensusState, Any): EncodeAsAnyProtobuf, - (Any, TendermintConsensusState): DecodeAsAnyProtobuf, + (TendermintConsensusState, Any): EncodeAsAnyProtobuf, + (Any, TendermintConsensusState): DecodeAsAnyProtobuf, } } diff --git a/crates/cosmos/cosmos-chain-components/src/encoding/type_url.rs b/crates/cosmos/cosmos-chain-components/src/encoding/type_url.rs index aca882fa3..96493f054 100644 --- a/crates/cosmos/cosmos-chain-components/src/encoding/type_url.rs +++ b/crates/cosmos/cosmos-chain-components/src/encoding/type_url.rs @@ -1,23 +1,20 @@ -use cgp::prelude::*; use hermes_protobuf_encoding_components::impl_type_url; +use ibc::clients::tendermint::types::{ + TENDERMINT_CLIENT_STATE_TYPE_URL, TENDERMINT_CONSENSUS_STATE_TYPE_URL, +}; use crate::types::tendermint::{TendermintClientState, TendermintConsensusState}; pub struct CosmosTypeUrlSchemas; -delegate_components! { - CosmosTypeUrlSchemas { - TendermintClientState: TendermintClientStateUrl, - TendermintConsensusState: TendermintConsensusStateUrl, - } -} - impl_type_url!( - TendermintClientStateUrl, - "/ibc.lightclients.tendermint.v1.ClientState", + CosmosTypeUrlSchemas, + TendermintClientState, + TENDERMINT_CLIENT_STATE_TYPE_URL, ); impl_type_url!( - TendermintConsensusStateUrl, - "/ibc.lightclients.tendermint.v1.ConsensusState", + CosmosTypeUrlSchemas, + TendermintConsensusState, + TENDERMINT_CONSENSUS_STATE_TYPE_URL, ); diff --git a/crates/cosmos/cosmos-encoding-components/Cargo.toml b/crates/cosmos/cosmos-encoding-components/Cargo.toml index 89de2abb4..3d2984a4d 100644 --- a/crates/cosmos/cosmos-encoding-components/Cargo.toml +++ b/crates/cosmos/cosmos-encoding-components/Cargo.toml @@ -17,6 +17,7 @@ hermes-encoding-components = { workspace = true } hermes-protobuf-encoding-components = { workspace = true } ibc = { workspace = true } +ibc-proto = { workspace = true } prost = { workspace = true } prost-types = { workspace = true } diff --git a/crates/cosmos/cosmos-encoding-components/src/components.rs b/crates/cosmos/cosmos-encoding-components/src/components.rs index d32794cf2..553919767 100644 --- a/crates/cosmos/cosmos-encoding-components/src/components.rs +++ b/crates/cosmos/cosmos-encoding-components/src/components.rs @@ -7,9 +7,13 @@ pub use hermes_protobuf_encoding_components::components::{ }; use hermes_protobuf_encoding_components::types::strategy::ViaProtobuf; use ibc::core::client::types::Height; +use ibc::core::commitment_types::commitment::CommitmentRoot; +use ibc::primitives::Timestamp; use prost_types::Any; +use crate::impls::commitment_root::EncodeCommitmentRoot; use crate::impls::height::EncodeHeight; +use crate::impls::timestamp::EncodeTimestamp; define_components! { CosmosEncodingComponents { @@ -54,5 +58,11 @@ delegate_components! { (ViaProtobuf, Height): EncodeHeight, + + (ViaProtobuf, CommitmentRoot): + EncodeCommitmentRoot, + + (ViaProtobuf, Timestamp): + EncodeTimestamp, } } diff --git a/crates/cosmos/cosmos-encoding-components/src/impls/any.rs b/crates/cosmos/cosmos-encoding-components/src/impls/any.rs new file mode 100644 index 000000000..9e2fb17a2 --- /dev/null +++ b/crates/cosmos/cosmos-encoding-components/src/impls/any.rs @@ -0,0 +1,30 @@ +use cgp::prelude::HasErrorType; +use hermes_encoding_components::traits::convert::Converter; +use ibc::primitives::proto::Any as IbcAny; +use prost_types::Any as ProstAny; + +pub struct ConvertIbcAny; + +impl Converter for ConvertIbcAny +where + Encoding: HasErrorType, +{ + fn convert(_encoding: &Encoding, from: &ProstAny) -> Result { + Ok(IbcAny { + type_url: from.type_url.clone(), + value: from.value.clone(), + }) + } +} + +impl Converter for ConvertIbcAny +where + Encoding: HasErrorType, +{ + fn convert(_encoding: &Encoding, from: &IbcAny) -> Result { + Ok(ProstAny { + type_url: from.type_url.clone(), + value: from.value.clone(), + }) + } +} diff --git a/crates/cosmos/cosmos-encoding-components/src/impls/commitment_root.rs b/crates/cosmos/cosmos-encoding-components/src/impls/commitment_root.rs new file mode 100644 index 000000000..4c19e777f --- /dev/null +++ b/crates/cosmos/cosmos-encoding-components/src/impls/commitment_root.rs @@ -0,0 +1,45 @@ +use cgp::prelude::*; +use hermes_encoding_components::impls::encode_mut::from::DecodeFrom; +use hermes_encoding_components::traits::encode_mut::MutEncoder; +use hermes_encoding_components::traits::transform::Transformer; +use hermes_encoding_components::traits::types::encode_buffer::HasEncodeBufferType; +use hermes_protobuf_encoding_components::components::MutDecoderComponent; +use hermes_protobuf_encoding_components::impls::encode_mut::proto_field::bytes::EncodeByteField; +use ibc::core::commitment_types::commitment::CommitmentRoot; + +pub struct EncodeCommitmentRoot; + +delegate_components! { + EncodeCommitmentRoot { + MutDecoderComponent: DecodeFrom< + Self, + EncodeByteField<1> + >, + } +} + +impl MutEncoder for EncodeCommitmentRoot +where + Encoding: HasEncodeBufferType + HasErrorType, + EncodeByteField<1>: for<'a> MutEncoder, +{ + fn encode_mut( + encoding: &Encoding, + value: &CommitmentRoot, + buffer: &mut Encoding::EncodeBuffer, + ) -> Result<(), Encoding::Error> { + EncodeByteField::encode_mut(encoding, &value.as_bytes(), buffer)?; + + Ok(()) + } +} + +impl Transformer for EncodeCommitmentRoot { + type From = Vec; + + type To = CommitmentRoot; + + fn transform(from: Vec) -> CommitmentRoot { + CommitmentRoot::from(from) + } +} diff --git a/crates/cosmos/cosmos-encoding-components/src/impls/height.rs b/crates/cosmos/cosmos-encoding-components/src/impls/height.rs index 1ceaf064d..6c9db54cc 100644 --- a/crates/cosmos/cosmos-encoding-components/src/impls/height.rs +++ b/crates/cosmos/cosmos-encoding-components/src/impls/height.rs @@ -1,11 +1,9 @@ use cgp::prelude::{CanRaiseError, HasErrorType}; -use hermes_encoding_components::impls::encode_mut::combine::CombineEncoders; use hermes_encoding_components::impls::encode_mut::pair::EncoderPair; use hermes_encoding_components::traits::decode_mut::MutDecoder; use hermes_encoding_components::traits::encode_mut::MutEncoder; use hermes_encoding_components::traits::types::decode_buffer::HasDecodeBufferType; use hermes_encoding_components::traits::types::encode_buffer::HasEncodeBufferType; -use hermes_encoding_components::HList; use hermes_protobuf_encoding_components::impls::encode_mut::proto_field::u64::EncodeU64ProtoField; use ibc::core::client::types::error::ClientError; use ibc::core::client::types::Height; @@ -36,15 +34,14 @@ where impl MutDecoder for EncodeHeight where Encoding: HasDecodeBufferType + CanRaiseError, - CombineEncoders, EncodeU64ProtoField<2>,]>: - MutDecoder, + EncoderPair, EncodeU64ProtoField<2>>: + MutDecoder, { fn decode_mut( encoding: &Encoding, buffer: &mut Encoding::DecodeBuffer<'_>, ) -> Result { - let HList![revision_number, revision_height] = - CombineEncoders::decode_mut(encoding, buffer)?; + let (revision_number, revision_height) = EncoderPair::decode_mut(encoding, buffer)?; Height::new(revision_number, revision_height).map_err(Encoding::raise_error) } diff --git a/crates/cosmos/cosmos-encoding-components/src/impls/mod.rs b/crates/cosmos/cosmos-encoding-components/src/impls/mod.rs index a2c60edad..68a6fd82c 100644 --- a/crates/cosmos/cosmos-encoding-components/src/impls/mod.rs +++ b/crates/cosmos/cosmos-encoding-components/src/impls/mod.rs @@ -1 +1,4 @@ +pub mod any; +pub mod commitment_root; pub mod height; +pub mod timestamp; diff --git a/crates/cosmos/cosmos-encoding-components/src/impls/timestamp.rs b/crates/cosmos/cosmos-encoding-components/src/impls/timestamp.rs new file mode 100644 index 000000000..94e168a42 --- /dev/null +++ b/crates/cosmos/cosmos-encoding-components/src/impls/timestamp.rs @@ -0,0 +1,63 @@ +use core::num::TryFromIntError; + +use cgp::prelude::{CanRaiseError, HasErrorType}; +use hermes_encoding_components::impls::encode_mut::pair::EncoderPair; +use hermes_encoding_components::traits::decode_mut::MutDecoder; +use hermes_encoding_components::traits::encode_mut::MutEncoder; +use hermes_encoding_components::traits::types::decode_buffer::HasDecodeBufferType; +use hermes_encoding_components::traits::types::encode_buffer::HasEncodeBufferType; +use hermes_protobuf_encoding_components::impls::encode_mut::proto_field::u64::EncodeU64ProtoField; +use ibc::core::primitives::{Timestamp, TimestampError}; +use ibc_proto::google::protobuf::Timestamp as ProtoTimestamp; + +pub struct EncodeTimestamp; + +impl MutEncoder for EncodeTimestamp +where + Encoding: HasEncodeBufferType + HasErrorType, + EncoderPair, EncodeU64ProtoField<2>>: + MutEncoder, +{ + fn encode_mut( + encoding: &Encoding, + value: &Timestamp, + buffer: &mut Encoding::EncodeBuffer, + ) -> Result<(), Encoding::Error> { + // We have no choice but to use ProtoTimestamp to encode for now, + // because the Timstamp field is currently private, and it is + // impossible to get the seconds and nanoseconds without first + // converting it to ProtoTimestamp. + + let proto_timestamp = ProtoTimestamp::from(*value); + + EncoderPair::encode_mut( + encoding, + &(proto_timestamp.seconds, proto_timestamp.nanos), + buffer, + )?; + + Ok(()) + } +} + +impl MutDecoder for EncodeTimestamp +where + Encoding: HasDecodeBufferType + CanRaiseError + CanRaiseError, + EncoderPair, EncodeU64ProtoField<2>>: + MutDecoder, +{ + fn decode_mut( + encoding: &Encoding, + buffer: &mut Encoding::DecodeBuffer<'_>, + ) -> Result { + let (seconds, nanos) = EncoderPair::decode_mut(encoding, buffer)?; + + let timestamp = Timestamp::from_unix_timestamp( + seconds.try_into().map_err(Encoding::raise_error)?, + nanos.try_into().map_err(Encoding::raise_error)?, + ) + .map_err(Encoding::raise_error)?; + + Ok(timestamp) + } +} diff --git a/crates/cosmos/cosmos-relayer/src/contexts/encoding.rs b/crates/cosmos/cosmos-relayer/src/contexts/encoding.rs index c2609b243..cef31dc91 100644 --- a/crates/cosmos/cosmos-relayer/src/contexts/encoding.rs +++ b/crates/cosmos/cosmos-relayer/src/contexts/encoding.rs @@ -5,6 +5,7 @@ use hermes_cosmos_chain_components::types::tendermint::TendermintConsensusState; use hermes_encoding_components::impls::default_encoding::GetDefaultEncoding; use hermes_encoding_components::traits::convert::CanConvertBothWays; use hermes_encoding_components::traits::encode_and_decode::CanEncodeAndDecode; +use hermes_encoding_components::traits::encode_and_decode_mut::CanEncodeAndDecodeMut; use hermes_encoding_components::traits::has_encoding::{ DefaultEncodingGetter, EncodingGetterComponent, HasEncodingType, ProvideEncodingType, }; @@ -15,7 +16,9 @@ use hermes_encoding_components::types::AsBytes; use hermes_protobuf_encoding_components::impls::encode_mut::chunk::ProtoChunks; use hermes_protobuf_encoding_components::types::strategy::{ViaAny, ViaProtobuf}; use ibc::core::client::types::Height; +use ibc::core::commitment_types::commitment::CommitmentRoot; use ibc::core::commitment_types::merkle::MerkleProof; +use ibc::primitives::Timestamp; use ibc_relayer_types::clients::ics07_tendermint::client_state::ClientState as TendermintClientState; use prost::bytes::BufMut; use prost_types::Any; @@ -85,7 +88,9 @@ pub trait CheckCosmosEncoding: + CanEncodeAndDecode + CanConvertBothWays + CanConvertBothWays - + CanEncodeAndDecode + + CanEncodeAndDecodeMut + + CanEncodeAndDecodeMut + + CanEncodeAndDecodeMut { } diff --git a/crates/cosmos/cosmos-relayer/src/impls/error.rs b/crates/cosmos/cosmos-relayer/src/impls/error.rs index 8cf1dbb8c..0b768d916 100644 --- a/crates/cosmos/cosmos-relayer/src/impls/error.rs +++ b/crates/cosmos/cosmos-relayer/src/impls/error.rs @@ -1,12 +1,13 @@ use alloc::string::FromUtf8Error; use core::array::TryFromSliceError; use core::convert::Infallible; -use core::num::ParseIntError; +use core::num::{ParseIntError, TryFromIntError}; use core::str::Utf8Error; use hermes_protobuf_encoding_components::impls::encode_mut::chunk::{ InvalidWireType, UnsupportedWireType, }; use hermes_protobuf_encoding_components::impls::encode_mut::proto_field::decode_required::RequiredFieldTagNotFound; +use ibc::primitives::TimestampError; use cgp::core::error::{ DelegateErrorRaiser, ErrorRaiser, ErrorRaiserComponent, ErrorTypeComponent, @@ -121,6 +122,7 @@ delegate_components! { TendermintProtoError, TendermintRpcError, TendermintClientError, + TimestampError, Ics02Error, Ics03Error, Ics23Error, @@ -134,6 +136,7 @@ delegate_components! { ClientError, CommitmentError, Utf8Error, + TryFromIntError, TryFromSliceError, // TODO: make it retryable? diff --git a/crates/cosmos/cosmos-wasm-relayer/src/context/chain.rs b/crates/cosmos/cosmos-wasm-relayer/src/context/chain.rs index 7b63c8e94..979054761 100644 --- a/crates/cosmos/cosmos-wasm-relayer/src/context/chain.rs +++ b/crates/cosmos/cosmos-wasm-relayer/src/context/chain.rs @@ -235,7 +235,7 @@ use tendermint_rpc::{HttpClient, Url}; use crate::components::cosmos_to_wasm_cosmos::CosmosToWasmCosmosComponents; use crate::context::encoding::{ProvideWasmCosmosEncoding, WasmCosmosEncoding}; use crate::impls::client_state::ProvideWrappedTendermintClientState; -use crate::types::client_state::WrappedTendermintClientState; +use crate::types::client_state::WasmTendermintClientState; #[derive(Clone)] pub struct WasmCosmosChain { @@ -564,7 +564,7 @@ impl HasEventSubscription for WasmCosmosChain { } pub trait CanUseWasmCosmosChain: - HasClientStateType + HasClientStateType + HasConsensusStateType + CanQueryBalance // + CanIbcTransferToken diff --git a/crates/cosmos/cosmos-wasm-relayer/src/context/encoding.rs b/crates/cosmos/cosmos-wasm-relayer/src/context/encoding.rs index 5ca479de2..f63292b17 100644 --- a/crates/cosmos/cosmos-wasm-relayer/src/context/encoding.rs +++ b/crates/cosmos/cosmos-wasm-relayer/src/context/encoding.rs @@ -6,13 +6,13 @@ use hermes_encoding_components::impls::default_encoding::GetDefaultEncoding; use hermes_encoding_components::traits::convert::{CanConvert, CanConvertBothWays}; use hermes_encoding_components::traits::encode::CanEncode; use hermes_encoding_components::traits::encode_and_decode::CanEncodeAndDecode; -use hermes_encoding_components::traits::encode_and_decode_mut::CanEncodeAndDecodeMut; use hermes_encoding_components::traits::has_encoding::{ DefaultEncodingGetter, EncodingGetterComponent, HasEncodingType, ProvideEncodingType, }; use hermes_encoding_components::traits::types::encoded::HasEncodedType; use hermes_encoding_components::types::AsBytes; use hermes_protobuf_encoding_components::types::strategy::{ViaAny, ViaProtobuf}; +use hermes_wasm_encoding_components::types::client_message::WasmClientMessage; use hermes_wasm_encoding_components::types::client_state::WasmClientState; use hermes_wasm_encoding_components::types::consensus_state::WasmConsensusState; use ibc::core::client::types::Height; @@ -20,7 +20,7 @@ use ibc_relayer_types::clients::ics07_tendermint::client_state::ClientState as T use prost_types::Any; use crate::encoding::components::*; -use crate::types::client_state::WrappedTendermintClientState; +use crate::types::client_state::WasmTendermintClientState; pub struct WasmCosmosEncoding; @@ -83,14 +83,16 @@ pub trait CheckWasmCosmosEncoding: + CanEncode + CanEncodeAndDecode + CanEncodeAndDecode - + CanConvertBothWays + + CanEncodeAndDecode + + CanConvertBothWays + CanConvert + CanConvert + CanEncode + CanEncode + CanEncodeAndDecode - + CanEncodeAndDecodeMut - + CanEncodeAndDecodeMut + + CanEncodeAndDecode + + CanEncodeAndDecode + + CanEncodeAndDecode { } diff --git a/crates/cosmos/cosmos-wasm-relayer/src/encoding/convert.rs b/crates/cosmos/cosmos-wasm-relayer/src/encoding/convert.rs index 3a64c1e30..7a27fc49f 100644 --- a/crates/cosmos/cosmos-wasm-relayer/src/encoding/convert.rs +++ b/crates/cosmos/cosmos-wasm-relayer/src/encoding/convert.rs @@ -8,15 +8,14 @@ use hermes_wasm_encoding_components::components::WasmEncodingComponents; use hermes_wasm_encoding_components::impls::strategies::consensus_state::{ DecodeViaWasmConsensusState, EncodeViaWasmConsensusState, }; +use hermes_wasm_encoding_components::types::client_message::WasmClientMessage; use hermes_wasm_encoding_components::types::client_state::WasmClientState; use hermes_wasm_encoding_components::types::consensus_state::WasmConsensusState; use ibc::core::commitment_types::merkle::MerkleProof; use ibc_proto::ibc::core::commitment::v1::MerkleProof as ProtoMerkleProof; use prost_types::Any; -use crate::types::client_state::{ - EncodeWrappedTendermintClientState, WrappedTendermintClientState, -}; +use crate::types::client_state::{EncodeWasmTendermintClientState, WasmTendermintClientState}; pub struct WasmCosmosConverterComponents; @@ -35,17 +34,20 @@ delegate_components! { CosmosClientEncodingComponents, [ (WasmClientState, Any), - (Any, WasmClientState), (WasmConsensusState, Any), + (WasmClientMessage, Any), + + (Any, WasmClientState), (Any, WasmConsensusState), + (Any, WasmClientMessage), ]: WasmEncodingComponents, [ - (Any, WrappedTendermintClientState), - (WrappedTendermintClientState, Any), + (Any, WasmTendermintClientState), + (WasmTendermintClientState, Any), ]: - EncodeWrappedTendermintClientState, + EncodeWasmTendermintClientState, (TendermintConsensusState, Any): EncodeViaWasmConsensusState, diff --git a/crates/cosmos/cosmos-wasm-relayer/src/encoding/encode.rs b/crates/cosmos/cosmos-wasm-relayer/src/encoding/encode.rs index b07e1292d..a7bc8270f 100644 --- a/crates/cosmos/cosmos-wasm-relayer/src/encoding/encode.rs +++ b/crates/cosmos/cosmos-wasm-relayer/src/encoding/encode.rs @@ -7,6 +7,7 @@ use hermes_cosmos_chain_components::types::tendermint::{ use hermes_protobuf_encoding_components::types::strategy::{ViaAny, ViaProtobuf}; use hermes_relayer_components::chain::traits::types::proof::ViaCommitmentProof; use hermes_wasm_encoding_components::components::WasmEncodingComponents; +use hermes_wasm_encoding_components::types::client_message::WasmClientMessage; use hermes_wasm_encoding_components::types::client_state::WasmClientState; use hermes_wasm_encoding_components::types::consensus_state::WasmConsensusState; use ibc::core::client::types::Height; @@ -41,9 +42,12 @@ delegate_components! { CosmosClientEncodingComponents, [ (ViaAny, WasmClientState), - (ViaProtobuf, WasmClientState), (ViaAny, WasmConsensusState), + (ViaAny, WasmClientMessage), + + (ViaProtobuf, WasmClientState), (ViaProtobuf, WasmConsensusState), + (ViaProtobuf, WasmClientMessage), ]: WasmEncodingComponents, } diff --git a/crates/cosmos/cosmos-wasm-relayer/src/encoding/encode_mut.rs b/crates/cosmos/cosmos-wasm-relayer/src/encoding/encode_mut.rs index 75988f258..287a86e78 100644 --- a/crates/cosmos/cosmos-wasm-relayer/src/encoding/encode_mut.rs +++ b/crates/cosmos/cosmos-wasm-relayer/src/encoding/encode_mut.rs @@ -1,6 +1,7 @@ use cgp::prelude::*; use hermes_protobuf_encoding_components::types::strategy::ViaProtobuf; use hermes_wasm_encoding_components::components::WasmEncodingComponents; +use hermes_wasm_encoding_components::types::client_message::WasmClientMessage; use hermes_wasm_encoding_components::types::client_state::WasmClientState; use hermes_wasm_encoding_components::types::consensus_state::WasmConsensusState; use ibc::core::client::types::Height; @@ -13,6 +14,7 @@ delegate_components! { (ViaProtobuf, Height), (ViaProtobuf, WasmClientState), (ViaProtobuf, WasmConsensusState), + (ViaProtobuf, WasmClientMessage), ]: WasmEncodingComponents, } } diff --git a/crates/cosmos/cosmos-wasm-relayer/src/encoding/type_url.rs b/crates/cosmos/cosmos-wasm-relayer/src/encoding/type_url.rs index 37d99cd0d..5b4e351cb 100644 --- a/crates/cosmos/cosmos-wasm-relayer/src/encoding/type_url.rs +++ b/crates/cosmos/cosmos-wasm-relayer/src/encoding/type_url.rs @@ -4,6 +4,7 @@ use hermes_cosmos_chain_components::types::tendermint::{ TendermintClientState, TendermintConsensusState, }; use hermes_wasm_encoding_components::components::WasmEncodingComponents; +use hermes_wasm_encoding_components::types::client_message::WasmClientMessage; use hermes_wasm_encoding_components::types::client_state::WasmClientState; use hermes_wasm_encoding_components::types::consensus_state::WasmConsensusState; @@ -19,6 +20,7 @@ delegate_components! { [ WasmClientState, WasmConsensusState, + WasmClientMessage, ]: WasmEncodingComponents, diff --git a/crates/cosmos/cosmos-wasm-relayer/src/impls/client_state.rs b/crates/cosmos/cosmos-wasm-relayer/src/impls/client_state.rs index 886451a09..a2abef4f7 100644 --- a/crates/cosmos/cosmos-wasm-relayer/src/impls/client_state.rs +++ b/crates/cosmos/cosmos-wasm-relayer/src/impls/client_state.rs @@ -8,7 +8,7 @@ use hermes_relayer_components::chain::traits::types::height::HasHeightType; use ibc_relayer_types::core::ics02_client::client_state::ClientState; use ibc_relayer_types::Height; -use crate::types::client_state::WrappedTendermintClientState; +use crate::types::client_state::WasmTendermintClientState; pub struct ProvideWrappedTendermintClientState; @@ -17,25 +17,25 @@ impl ProvideClientStateType where Chain: Async, { - type ClientState = WrappedTendermintClientState; + type ClientState = WasmTendermintClientState; } impl ClientStateFieldsGetter for ProvideWrappedTendermintClientState where - Chain: HasClientStateType + Chain: HasClientStateType + HasHeightType, { - fn client_state_latest_height(client_state: &WrappedTendermintClientState) -> Height { + fn client_state_latest_height(client_state: &WasmTendermintClientState) -> Height { client_state.tendermint_client_state.latest_height } - fn client_state_is_frozen(client_state: &WrappedTendermintClientState) -> bool { + fn client_state_is_frozen(client_state: &WasmTendermintClientState) -> bool { client_state.tendermint_client_state.is_frozen() } fn client_state_has_expired( - client_state: &WrappedTendermintClientState, + client_state: &WasmTendermintClientState, elapsed: Duration, ) -> bool { elapsed > client_state.tendermint_client_state.trusting_period diff --git a/crates/cosmos/cosmos-wasm-relayer/src/types/client_state.rs b/crates/cosmos/cosmos-wasm-relayer/src/types/client_state.rs index 8df1e9af4..dc8a299ea 100644 --- a/crates/cosmos/cosmos-wasm-relayer/src/types/client_state.rs +++ b/crates/cosmos/cosmos-wasm-relayer/src/types/client_state.rs @@ -8,21 +8,21 @@ use hermes_wasm_encoding_components::types::client_state::WasmClientState; use ibc::core::client::types::Height; use prost_types::Any; -pub struct WrappedTendermintClientState { +pub struct WasmTendermintClientState { pub tendermint_client_state: TendermintClientState, pub wasm_code_hash: Vec, } -impl From for TendermintClientState { - fn from(value: WrappedTendermintClientState) -> Self { +impl From for TendermintClientState { + fn from(value: WasmTendermintClientState) -> Self { value.tendermint_client_state } } -pub struct EncodeWrappedTendermintClientState; +pub struct EncodeWasmTendermintClientState; -impl Converter - for EncodeWrappedTendermintClientState +impl Converter + for EncodeWasmTendermintClientState where Encoding: HasEncodedType> + CanEncode @@ -30,7 +30,7 @@ where { fn convert( encoding: &Encoding, - client_state: &WrappedTendermintClientState, + client_state: &WasmTendermintClientState, ) -> Result { let tendermint_client_state_bytes = encoding.encode(&client_state.tendermint_client_state)?; @@ -51,8 +51,8 @@ where } } -impl Converter - for EncodeWrappedTendermintClientState +impl Converter + for EncodeWasmTendermintClientState where Encoding: HasEncodedType> + CanDecode @@ -61,12 +61,12 @@ where fn convert( encoding: &Encoding, client_state_any: &Any, - ) -> Result { + ) -> Result { let wasm_client_state = encoding.convert(client_state_any)?; let tendermint_client_state = encoding.decode(&wasm_client_state.data)?; - let wrapped_tendermint_client_state = WrappedTendermintClientState { + let wrapped_tendermint_client_state = WasmTendermintClientState { tendermint_client_state, wasm_code_hash: wasm_client_state.checksum, }; diff --git a/crates/encoding/encoding-components/src/impls/encode_mut/pair.rs b/crates/encoding/encoding-components/src/impls/encode_mut/pair.rs index a01d1b7c3..bf968ab53 100644 --- a/crates/encoding/encoding-components/src/impls/encode_mut/pair.rs +++ b/crates/encoding/encoding-components/src/impls/encode_mut/pair.rs @@ -2,7 +2,7 @@ use core::marker::PhantomData; use cgp::core::error::HasErrorType; -use crate::impls::with_context::EncodeWithContext; +use crate::impls::with_context::WithContext; use crate::traits::decode_mut::MutDecoder; use crate::traits::encode_mut::MutEncoder; use crate::traits::types::decode_buffer::HasDecodeBufferType; @@ -10,7 +10,7 @@ use crate::traits::types::encode_buffer::HasEncodeBufferType; pub struct EncoderPair(pub PhantomData<(EncoderA, EncoderB)>); -pub type EncodeCons = EncoderPair; +pub type EncodeCons = EncoderPair; impl MutEncoder for EncoderPair diff --git a/crates/encoding/encoding-components/src/impls/with_context.rs b/crates/encoding/encoding-components/src/impls/with_context.rs index 2cd543068..468b8f643 100644 --- a/crates/encoding/encoding-components/src/impls/with_context.rs +++ b/crates/encoding/encoding-components/src/impls/with_context.rs @@ -1,11 +1,12 @@ +use crate::traits::convert::{CanConvert, Converter}; use crate::traits::decode::{CanDecode, Decoder}; use crate::traits::decode_mut::{CanDecodeMut, MutDecoder}; use crate::traits::encode::{CanEncode, Encoder}; use crate::traits::encode_mut::{CanEncodeMut, MutEncoder}; -pub struct EncodeWithContext; +pub struct WithContext; -impl Encoder for EncodeWithContext +impl Encoder for WithContext where Encoding: CanEncode, { @@ -14,7 +15,7 @@ where } } -impl Decoder for EncodeWithContext +impl Decoder for WithContext where Encoding: CanDecode, { @@ -23,7 +24,7 @@ where } } -impl MutEncoder for EncodeWithContext +impl MutEncoder for WithContext where Encoding: CanEncodeMut, { @@ -36,7 +37,7 @@ where } } -impl MutDecoder for EncodeWithContext +impl MutDecoder for WithContext where Encoding: CanDecodeMut, { @@ -47,3 +48,12 @@ where encoding.decode_mut(buffer) } } + +impl Converter for WithContext +where + Encoding: CanConvert, +{ + fn convert(encoding: &Encoding, from: &From) -> Result { + encoding.convert(from) + } +} diff --git a/crates/encoding/protobuf-encoding-components/src/impls/encode_mut/proto_field/u64.rs b/crates/encoding/protobuf-encoding-components/src/impls/encode_mut/proto_field/u64.rs index 8c45eb3e1..e855d92f0 100644 --- a/crates/encoding/protobuf-encoding-components/src/impls/encode_mut/proto_field/u64.rs +++ b/crates/encoding/protobuf-encoding-components/src/impls/encode_mut/proto_field/u64.rs @@ -1,4 +1,4 @@ -use cgp::prelude::{CanRaiseError, HasErrorType}; +use cgp::prelude::CanRaiseError; use hermes_encoding_components::traits::decode_mut::MutDecoder; use hermes_encoding_components::traits::encode_mut::MutEncoder; use hermes_encoding_components::traits::types::encode_buffer::HasEncodeBufferType; @@ -12,15 +12,15 @@ pub struct EncodeU64ProtoField; impl MutEncoder for EncodeU64ProtoField where - Encoding: HasEncodeBufferType + HasErrorType, - Value: Clone + Into, + Encoding: HasEncodeBufferType + CanRaiseError, + Value: Clone + TryInto, { fn encode_mut( _encoding: &Encoding, value: &Value, buffer: &mut Encoding::EncodeBuffer, ) -> Result<(), Encoding::Error> { - let value2 = value.clone().into(); + let value2 = value.clone().try_into().map_err(Encoding::raise_error)?; if value2 != 0 { encode_key(TAG, WireType::Varint, buffer); diff --git a/crates/encoding/protobuf-encoding-components/src/impls/via_any.rs b/crates/encoding/protobuf-encoding-components/src/impls/via_any.rs index 75dc1ddf2..87e519388 100644 --- a/crates/encoding/protobuf-encoding-components/src/impls/via_any.rs +++ b/crates/encoding/protobuf-encoding-components/src/impls/via_any.rs @@ -1,7 +1,7 @@ use core::marker::PhantomData; use cgp::prelude::*; -use hermes_encoding_components::impls::with_context::EncodeWithContext; +use hermes_encoding_components::impls::with_context::WithContext; use hermes_encoding_components::traits::decode::Decoder; use hermes_encoding_components::traits::encode::Encoder; use hermes_encoding_components::traits::types::encoded::HasEncodedType; @@ -14,11 +14,11 @@ impl Encoder for EncodeViaAny where Encoding: HasEncodedType + HasErrorType, - EncodeAsAnyProtobuf: Encoder, + EncodeAsAnyProtobuf: Encoder, InStrategy: Async, { fn encode(encoding: &Encoding, value: &Value) -> Result { - >::encode(encoding, value) + >::encode(encoding, value) } } @@ -26,12 +26,11 @@ impl Decoder for EncodeViaAny where Encoding: HasEncodedType + HasErrorType, - DecodeAsAnyProtobuf: Decoder, + DecodeAsAnyProtobuf: Decoder, InStrategy: Async, { fn decode(encoding: &Encoding, encoded: &Encoding::Encoded) -> Result { - let value = - >::decode(encoding, encoded)?; + let value = >::decode(encoding, encoded)?; Ok(value) } diff --git a/crates/encoding/protobuf-encoding-components/src/macros.rs b/crates/encoding/protobuf-encoding-components/src/macros.rs index 343169415..686f48b4e 100644 --- a/crates/encoding/protobuf-encoding-components/src/macros.rs +++ b/crates/encoding/protobuf-encoding-components/src/macros.rs @@ -1,15 +1,13 @@ #[macro_export] macro_rules! impl_type_url { - ($component:ident, $type_url:literal $(,)?) => { - pub struct $component; - - impl $crate::vendor::SchemaGetter for $component + ($component:ident, $type:ty, $type_url:tt $(,)?) => { + impl $crate::vendor::SchemaGetter for $component where Encoding: $crate::vendor::HasSchemaType, { fn schema( _encoding: &Encoding, - _phantom: core::marker::PhantomData, + _phantom: core::marker::PhantomData<$type>, ) -> &&'static str { &$type_url } diff --git a/crates/solomachine/solomachine-chain-components/src/encoding/components.rs b/crates/solomachine/solomachine-chain-components/src/encoding/components.rs index 6f6f48901..b1b97c0ff 100644 --- a/crates/solomachine/solomachine-chain-components/src/encoding/components.rs +++ b/crates/solomachine/solomachine-chain-components/src/encoding/components.rs @@ -27,6 +27,6 @@ define_components! { ]: DelegateEncoding, SchemaGetterComponent: - DelegateEncoding, + SolomachineTypeUrlSchemas, } } diff --git a/crates/solomachine/solomachine-chain-components/src/encoding/convert.rs b/crates/solomachine/solomachine-chain-components/src/encoding/convert.rs index 75e40198a..c99f21603 100644 --- a/crates/solomachine/solomachine-chain-components/src/encoding/convert.rs +++ b/crates/solomachine/solomachine-chain-components/src/encoding/convert.rs @@ -1,6 +1,6 @@ use cgp::prelude::*; use hermes_encoding_components::impls::convert::{ConvertFrom, TryConvertFrom}; -use hermes_encoding_components::impls::with_context::EncodeWithContext; +use hermes_encoding_components::impls::with_context::WithContext; use hermes_protobuf_encoding_components::impls::any::{DecodeAsAnyProtobuf, EncodeAsAnyProtobuf}; use hermes_protobuf_encoding_components::types::any::Any; use hermes_protobuf_encoding_components::types::strategy::ViaProtobuf; @@ -14,12 +14,12 @@ delegate_components! { SolomachineConverterComponents { (SolomachineClientState, ProtoSolomachineClientState): ConvertFrom, (ProtoSolomachineClientState, SolomachineClientState): TryConvertFrom, - (SolomachineClientState, Any): EncodeAsAnyProtobuf, - (Any, SolomachineClientState): DecodeAsAnyProtobuf, + (SolomachineClientState, Any): EncodeAsAnyProtobuf, + (Any, SolomachineClientState): DecodeAsAnyProtobuf, (SolomachineConsensusState, ProtoSolomachineConsensusState): ConvertFrom, (ProtoSolomachineConsensusState, SolomachineConsensusState): TryConvertFrom, - (SolomachineConsensusState, Any): EncodeAsAnyProtobuf, - (Any, SolomachineConsensusState): DecodeAsAnyProtobuf, + (SolomachineConsensusState, Any): EncodeAsAnyProtobuf, + (Any, SolomachineConsensusState): DecodeAsAnyProtobuf, } } diff --git a/crates/solomachine/solomachine-chain-components/src/encoding/type_url.rs b/crates/solomachine/solomachine-chain-components/src/encoding/type_url.rs index 1e2d233d4..2d89cdd93 100644 --- a/crates/solomachine/solomachine-chain-components/src/encoding/type_url.rs +++ b/crates/solomachine/solomachine-chain-components/src/encoding/type_url.rs @@ -1,24 +1,20 @@ -use cgp::prelude::*; use hermes_protobuf_encoding_components::impl_type_url; -use crate::types::client_state::SolomachineClientState; -use crate::types::consensus_state::SolomachineConsensusState; +use crate::types::client_state::{SolomachineClientState, SOLOMACHINE_CLIENT_STATE_TYPE_URL}; +use crate::types::consensus_state::{ + SolomachineConsensusState, SOLOMACHINE_CONSENSUS_STATE_TYPE_URL, +}; pub struct SolomachineTypeUrlSchemas; -delegate_components! { - SolomachineTypeUrlSchemas { - SolomachineClientState: SolomachineClientStateUrl, - SolomachineConsensusState: SolomachineConsensusStateUrl, - } -} - impl_type_url!( - SolomachineClientStateUrl, - "/ibc.lightclients.solomachine.v3.ClientState" + SolomachineTypeUrlSchemas, + SolomachineClientState, + SOLOMACHINE_CLIENT_STATE_TYPE_URL, ); impl_type_url!( - SolomachineConsensusStateUrl, - "/ibc.lightclients.solomachine.v3.ConsensusState" + SolomachineTypeUrlSchemas, + SolomachineConsensusState, + SOLOMACHINE_CONSENSUS_STATE_TYPE_URL, ); diff --git a/crates/solomachine/solomachine-chain-components/src/types/client_state.rs b/crates/solomachine/solomachine-chain-components/src/types/client_state.rs index c921a5f33..62eb8cd04 100644 --- a/crates/solomachine/solomachine-chain-components/src/types/client_state.rs +++ b/crates/solomachine/solomachine-chain-components/src/types/client_state.rs @@ -9,7 +9,7 @@ use prost::Message; use crate::types::consensus_state::SolomachineConsensusState; -const TYPE_URL: &str = "/ibc.lightclients.solomachine.v3.ClientState"; +pub const SOLOMACHINE_CLIENT_STATE_TYPE_URL: &str = "/ibc.lightclients.solomachine.v3.ClientState"; #[derive(Clone, Debug)] pub struct SolomachineClientState { @@ -33,7 +33,9 @@ impl TryFrom for SolomachineClientState { } match raw.type_url.as_str() { - TYPE_URL => decode_client_state(raw.value.deref()).map_err(Into::into), + SOLOMACHINE_CLIENT_STATE_TYPE_URL => { + decode_client_state(raw.value.deref()).map_err(Into::into) + } _ => Err(eyre!("unknown client state: {}", raw.type_url).into()), } } @@ -48,7 +50,7 @@ impl Msg for SolomachineClientState { } fn type_url(&self) -> String { - TYPE_URL.to_string() + SOLOMACHINE_CLIENT_STATE_TYPE_URL.to_string() } } diff --git a/crates/solomachine/solomachine-chain-components/src/types/consensus_state.rs b/crates/solomachine/solomachine-chain-components/src/types/consensus_state.rs index bc5ae5a70..8beb20a65 100644 --- a/crates/solomachine/solomachine-chain-components/src/types/consensus_state.rs +++ b/crates/solomachine/solomachine-chain-components/src/types/consensus_state.rs @@ -10,7 +10,8 @@ use crate::methods::encode::public_key::{ decode_public_key_from_any, encode_public_key, PublicKey, }; -const TYPE_URL: &str = "/ibc.lightclients.solomachine.v3.ConsensusState"; +pub const SOLOMACHINE_CONSENSUS_STATE_TYPE_URL: &str = + "/ibc.lightclients.solomachine.v3.ConsensusState"; #[derive(Clone, Debug)] pub struct SolomachineConsensusState { @@ -28,7 +29,7 @@ impl Msg for SolomachineConsensusState { } fn type_url(&self) -> String { - TYPE_URL.to_string() + SOLOMACHINE_CONSENSUS_STATE_TYPE_URL.to_string() } } diff --git a/crates/wasm/wasm-encoding-components/src/components.rs b/crates/wasm/wasm-encoding-components/src/components.rs index 0fd573049..f7d06e982 100644 --- a/crates/wasm/wasm-encoding-components/src/components.rs +++ b/crates/wasm/wasm-encoding-components/src/components.rs @@ -5,7 +5,7 @@ pub use hermes_cosmos_encoding_components::components::{ DecodeBufferTypeComponent, EncodeBufferTypeComponent, }; use hermes_encoding_components::impls::delegate::DelegateEncoding; -use hermes_encoding_components::impls::with_context::EncodeWithContext; +use hermes_encoding_components::impls::with_context::WithContext; pub use hermes_encoding_components::traits::convert::ConverterComponent; pub use hermes_encoding_components::traits::decode::DecoderComponent; pub use hermes_encoding_components::traits::decode_mut::MutDecoderComponent; @@ -20,11 +20,14 @@ use hermes_protobuf_encoding_components::impls::encode::buffer::EncodeProtoWithM use hermes_protobuf_encoding_components::impls::via_any::EncodeViaAny; pub use hermes_protobuf_encoding_components::traits::length::EncodedLengthGetterComponent; use hermes_protobuf_encoding_components::types::strategy::{ViaAny, ViaProtobuf}; +use ibc::clients::wasm_types::client_message::WASM_CLIENT_MESSAGE_TYPE_URL; use ibc::core::client::types::Height; use prost_types::Any; +use crate::impls::encode::client_message::EncodeWasmClientMessage; use crate::impls::encode::client_state::EncodeWasmClientState; use crate::impls::encode::consensus_state::EncodeWasmConsensusState; +use crate::types::client_message::WasmClientMessage; use crate::types::client_state::WasmClientState; use crate::types::consensus_state::WasmConsensusState; @@ -51,7 +54,7 @@ define_components! { ]: DelegateEncoding, SchemaGetterComponent: - DelegateEncoding, + WasmTypeUrlSchemas, } } @@ -63,10 +66,15 @@ pub struct WasmEncoderComponents; delegate_components! { WasmConverterComponents { - (WasmClientState, Any): EncodeAsAnyProtobuf, - (Any, WasmClientState): DecodeAsAnyProtobuf, - (WasmConsensusState, Any): EncodeAsAnyProtobuf, - (Any, WasmConsensusState): DecodeAsAnyProtobuf, + [ + (WasmClientState, Any), + (WasmConsensusState, Any), + ]: EncodeAsAnyProtobuf, + + [ + (Any, WasmClientState), + (Any, WasmConsensusState), + ]: DecodeAsAnyProtobuf, } } @@ -81,30 +89,44 @@ delegate_components! { (ViaProtobuf, WasmConsensusState): EncodeWasmConsensusState, + + (ViaProtobuf, WasmClientMessage): + EncodeWasmClientMessage, } } delegate_components! { WasmEncoderComponents { - (ViaAny, WasmClientState): EncodeViaAny, - (ViaProtobuf, WasmClientState): EncodeProtoWithMutBuffer, - (ViaAny, WasmConsensusState): EncodeViaAny, - (ViaProtobuf, WasmConsensusState): EncodeProtoWithMutBuffer, + [ + (ViaAny, WasmClientState), + (ViaAny, WasmConsensusState), + (ViaAny, WasmClientMessage), + ]: EncodeViaAny, + + [ + (ViaProtobuf, WasmClientState), + (ViaProtobuf, WasmConsensusState), + (ViaProtobuf, WasmClientMessage), + ]: EncodeProtoWithMutBuffer, } } pub struct WasmTypeUrlSchemas; -delegate_components! { - WasmTypeUrlSchemas { - WasmClientState: WasmClientStateUrl, - WasmConsensusState: WasmConsensusStateUrl, - } -} +impl_type_url!( + WasmTypeUrlSchemas, + WasmClientState, + "/ibc.lightclients.wasm.v1.ClientState", +); -impl_type_url!(WasmClientStateUrl, "/ibc.lightclients.wasm.v1.ClientState"); +impl_type_url!( + WasmTypeUrlSchemas, + WasmConsensusState, + "/ibc.lightclients.wasm.v1.ConsensusState", +); impl_type_url!( - WasmConsensusStateUrl, - "/ibc.lightclients.wasm.v1.ConsensusState" + WasmTypeUrlSchemas, + WasmClientMessage, + WASM_CLIENT_MESSAGE_TYPE_URL, ); diff --git a/crates/wasm/wasm-encoding-components/src/impls/convert/client_message.rs b/crates/wasm/wasm-encoding-components/src/impls/convert/client_message.rs new file mode 100644 index 000000000..d99d389e3 --- /dev/null +++ b/crates/wasm/wasm-encoding-components/src/impls/convert/client_message.rs @@ -0,0 +1,41 @@ +use hermes_encoding_components::traits::convert::{CanConvert, Converter}; +use hermes_encoding_components::traits::decode::CanDecode; +use hermes_encoding_components::traits::encode::CanEncode; +use hermes_encoding_components::traits::types::encoded::HasEncodedType; +use hermes_protobuf_encoding_components::types::strategy::ViaAny; +use ibc::clients::wasm_types::client_message::ClientMessage; +use prost_types::Any; + +pub struct EncodeViaClientMessage; + +impl Converter for EncodeViaClientMessage +where + Encoding: HasEncodedType> + + CanEncode + + CanConvert, +{ + fn convert(encoding: &Encoding, value: &Value) -> Result { + let data = encoding.encode(value)?; + + let client_message = ClientMessage { data }; + + encoding.convert(&client_message) + } +} + +pub struct DecodeViaClientMessage; + +impl Converter for DecodeViaClientMessage +where + Encoding: HasEncodedType> + + CanDecode + + CanConvert, +{ + fn convert(encoding: &Encoding, any: &Any) -> Result { + let message = encoding.convert(any)?; + + let value = encoding.decode(&message.data)?; + + Ok(value) + } +} diff --git a/crates/wasm/wasm-encoding-components/src/impls/convert/mod.rs b/crates/wasm/wasm-encoding-components/src/impls/convert/mod.rs new file mode 100644 index 000000000..99d890b2b --- /dev/null +++ b/crates/wasm/wasm-encoding-components/src/impls/convert/mod.rs @@ -0,0 +1 @@ +pub mod client_message; diff --git a/crates/wasm/wasm-encoding-components/src/impls/encode/client_message.rs b/crates/wasm/wasm-encoding-components/src/impls/encode/client_message.rs new file mode 100644 index 000000000..2622d97c3 --- /dev/null +++ b/crates/wasm/wasm-encoding-components/src/impls/encode/client_message.rs @@ -0,0 +1,46 @@ +use core::marker::PhantomData; + +use cgp::prelude::*; +use hermes_cosmos_encoding_components::components::{MutDecoderComponent, MutEncoderComponent}; +use hermes_encoding_components::impls::encode_mut::field::EncodeFieldWithGetter; +use hermes_encoding_components::impls::encode_mut::from::DecodeFrom; +use hermes_encoding_components::traits::field::FieldGetter; +use hermes_encoding_components::traits::transform::Transformer; +use hermes_protobuf_encoding_components::impls::encode_mut::proto_field::bytes::EncodeByteField; + +use crate::types::client_message::WasmClientMessage; + +pub struct EncodeWasmClientMessage; + +delegate_components! { + EncodeWasmClientMessage { + MutEncoderComponent: + EncodeFieldWithGetter< + Self, + symbol!("data"), + EncodeByteField<1>, + >, + MutDecoderComponent: DecodeFrom< + Self, + EncodeByteField<1>, + >, + } +} + +impl FieldGetter for EncodeWasmClientMessage { + type Field = Vec; + + fn get_field(message: &WasmClientMessage, _tag: PhantomData) -> &Vec { + &message.data + } +} + +impl Transformer for EncodeWasmClientMessage { + type From = Vec; + + type To = WasmClientMessage; + + fn transform(data: Self::From) -> Self::To { + WasmClientMessage { data } + } +} diff --git a/crates/wasm/wasm-encoding-components/src/impls/encode/client_state.rs b/crates/wasm/wasm-encoding-components/src/impls/encode/client_state.rs index 5f34fc307..65a47622e 100644 --- a/crates/wasm/wasm-encoding-components/src/impls/encode/client_state.rs +++ b/crates/wasm/wasm-encoding-components/src/impls/encode/client_state.rs @@ -2,7 +2,7 @@ use cgp::prelude::*; use hermes_encoding_components::impls::encode_mut::combine::CombineEncoders; use hermes_encoding_components::impls::encode_mut::field::EncodeField; use hermes_encoding_components::impls::encode_mut::from::DecodeFrom; -use hermes_encoding_components::impls::with_context::EncodeWithContext; +use hermes_encoding_components::impls::with_context::WithContext; use hermes_encoding_components::traits::decode_mut::MutDecoderComponent; use hermes_encoding_components::traits::encode_mut::MutEncoderComponent; use hermes_encoding_components::traits::transform::Transformer; @@ -30,7 +30,7 @@ delegate_components! { >, EncodeField< symbol!("latest_height"), - EncodeLengthDelimitedProtoField<3, EncodeWithContext>, + EncodeLengthDelimitedProtoField<3, WithContext>, >, ]>, MutDecoderComponent: DecodeFrom< @@ -38,7 +38,7 @@ delegate_components! { CombineEncoders, EncodeByteField<2>, - DecodeRequiredProtoField<3, EncodeWithContext>, + DecodeRequiredProtoField<3, WithContext>, ]> >, } diff --git a/crates/wasm/wasm-encoding-components/src/impls/encode/mod.rs b/crates/wasm/wasm-encoding-components/src/impls/encode/mod.rs index 620c017de..61e7fc88f 100644 --- a/crates/wasm/wasm-encoding-components/src/impls/encode/mod.rs +++ b/crates/wasm/wasm-encoding-components/src/impls/encode/mod.rs @@ -1,2 +1,3 @@ +pub mod client_message; pub mod client_state; pub mod consensus_state; diff --git a/crates/wasm/wasm-encoding-components/src/impls/mod.rs b/crates/wasm/wasm-encoding-components/src/impls/mod.rs index 21e895b20..446020396 100644 --- a/crates/wasm/wasm-encoding-components/src/impls/mod.rs +++ b/crates/wasm/wasm-encoding-components/src/impls/mod.rs @@ -1,2 +1,3 @@ +pub mod convert; pub mod encode; pub mod strategies; diff --git a/crates/wasm/wasm-encoding-components/src/types/client_message.rs b/crates/wasm/wasm-encoding-components/src/types/client_message.rs new file mode 100644 index 000000000..eb4e55a37 --- /dev/null +++ b/crates/wasm/wasm-encoding-components/src/types/client_message.rs @@ -0,0 +1,3 @@ +use ibc::clients::wasm_types::client_message::ClientMessage; + +pub type WasmClientMessage = ClientMessage; diff --git a/crates/wasm/wasm-encoding-components/src/types/mod.rs b/crates/wasm/wasm-encoding-components/src/types/mod.rs index 620c017de..61e7fc88f 100644 --- a/crates/wasm/wasm-encoding-components/src/types/mod.rs +++ b/crates/wasm/wasm-encoding-components/src/types/mod.rs @@ -1,2 +1,3 @@ +pub mod client_message; pub mod client_state; pub mod consensus_state;