-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
df8f288
commit 6e231ae
Showing
42 changed files
with
427 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 9 additions & 12 deletions
21
crates/cosmos/cosmos-chain-components/src/encoding/type_url.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Encoding> Converter<Encoding, ProstAny, IbcAny> for ConvertIbcAny | ||
where | ||
Encoding: HasErrorType, | ||
{ | ||
fn convert(_encoding: &Encoding, from: &ProstAny) -> Result<IbcAny, Encoding::Error> { | ||
Ok(IbcAny { | ||
type_url: from.type_url.clone(), | ||
value: from.value.clone(), | ||
}) | ||
} | ||
} | ||
|
||
impl<Encoding> Converter<Encoding, IbcAny, ProstAny> for ConvertIbcAny | ||
where | ||
Encoding: HasErrorType, | ||
{ | ||
fn convert(_encoding: &Encoding, from: &IbcAny) -> Result<ProstAny, Encoding::Error> { | ||
Ok(ProstAny { | ||
type_url: from.type_url.clone(), | ||
value: from.value.clone(), | ||
}) | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
crates/cosmos/cosmos-encoding-components/src/impls/commitment_root.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Encoding, Strategy> MutEncoder<Encoding, Strategy, CommitmentRoot> for EncodeCommitmentRoot | ||
where | ||
Encoding: HasEncodeBufferType + HasErrorType, | ||
EncodeByteField<1>: for<'a> MutEncoder<Encoding, Strategy, &'a [u8]>, | ||
{ | ||
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<u8>; | ||
|
||
type To = CommitmentRoot; | ||
|
||
fn transform(from: Vec<u8>) -> CommitmentRoot { | ||
CommitmentRoot::from(from) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
pub mod any; | ||
pub mod commitment_root; | ||
pub mod height; | ||
pub mod timestamp; |
63 changes: 63 additions & 0 deletions
63
crates/cosmos/cosmos-encoding-components/src/impls/timestamp.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Encoding, Strategy> MutEncoder<Encoding, Strategy, Timestamp> for EncodeTimestamp | ||
where | ||
Encoding: HasEncodeBufferType + HasErrorType, | ||
EncoderPair<EncodeU64ProtoField<1>, EncodeU64ProtoField<2>>: | ||
MutEncoder<Encoding, Strategy, (i64, i32)>, | ||
{ | ||
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<Encoding, Strategy> MutDecoder<Encoding, Strategy, Timestamp> for EncodeTimestamp | ||
where | ||
Encoding: HasDecodeBufferType + CanRaiseError<TryFromIntError> + CanRaiseError<TimestampError>, | ||
EncoderPair<EncodeU64ProtoField<1>, EncodeU64ProtoField<2>>: | ||
MutDecoder<Encoding, Strategy, (i64, i32)>, | ||
{ | ||
fn decode_mut( | ||
encoding: &Encoding, | ||
buffer: &mut Encoding::DecodeBuffer<'_>, | ||
) -> Result<Timestamp, Encoding::Error> { | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.