Skip to content

Commit

Permalink
Minor refactoring (#428)
Browse files Browse the repository at this point in the history
* Simplify trait bounds for HasTimestampType trait

* Use prost_types::Any for CosmosUpdateClientMessage

* Generalize Cosmos build_create_client_message

* Add ProvideNoCreateClientMessageOptionsType

* Add changelog
  • Loading branch information
soareschen authored Sep 10, 2024
1 parent 2782669 commit f25d390
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 29 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

## v0.2.0 (pre-release)

- Remove unused constraints in `HasTimestampType::Timestamp`. [(#428)](https://github.com/informalsystems/hermes-sdk/pull/428)
- Update the `header` field in `CosmosUpdateClientMessage` to use `prost_types::Any` instead of `ibc_proto::Any`. [(#428)](https://github.com/informalsystems/hermes-sdk/pull/428)
- Generalize the `CreateClientMessageBuilder` instance `BuildCosmosCreateClientMessage` to `BuildAnyCreateClientMessage`, and allows generic `CreateClientPayload` that has `client_state: Counterparty::ClientState` and `consensus_state: Counterparty::ConsensusState` fields. [(#428)](https://github.com/informalsystems/hermes-sdk/pull/428)
- Rename the `ProvideCreateClientMessageOptionsType` instance in Cosmos from `ProvideCosmosCreateClientSettings` to `ProvideNoCreateClientMessageOptionsType`, with `CreateClientMessageOptions = ()`. [(#428)](https://github.com/informalsystems/hermes-sdk/pull/428)

## v0.1.0 (2024-09-03)

- Initial release of Hermes SDK crates on crates.io.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ use hermes_relayer_components::chain::traits::types::create_client::CreateClient
use hermes_relayer_components::chain::traits::types::ibc::CounterpartyMessageHeightGetterComponent;

use crate::impls::channel::channel_handshake_message::BuildCosmosChannelHandshakeMessage;
use crate::impls::client::create_client_message::BuildCosmosCreateClientMessage;
use crate::impls::client::create_client_message::BuildAnyCreateClientMessage;
use crate::impls::client::update_client_message::BuildCosmosUpdateClientMessage;
use crate::impls::connection::connection_handshake_message::BuildCosmosConnectionHandshakeMessage;
use crate::impls::message_height::GetCosmosCounterpartyMessageHeight;
use crate::impls::queries::consensus_state_height::QueryConsensusStateHeightsFromGrpc;
use crate::impls::types::create_client_options::ProvideCosmosCreateClientSettings;
use crate::impls::types::create_client_options::ProvideNoCreateClientMessageOptionsType;

define_components! {
CosmosToCosmosComponents {
Expand All @@ -44,9 +44,9 @@ define_components! {
]:
QueryAndConvertRawConsensusState,
CreateClientMessageOptionsTypeComponent:
ProvideCosmosCreateClientSettings,
ProvideNoCreateClientMessageOptionsType,
CreateClientMessageBuilderComponent:
BuildCosmosCreateClientMessage,
BuildAnyCreateClientMessage,
UpdateClientMessageBuilderComponent:
BuildCosmosUpdateClientMessage,
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use cgp::core::error::CanRaiseError;
use cgp::core::Async;
use core::marker::PhantomData;

use cgp::prelude::*;
use hermes_encoding_components::traits::convert::CanConvert;
use hermes_encoding_components::traits::has_encoding::HasDefaultEncoding;
use hermes_encoding_components::types::AsBytes;
use hermes_relayer_components::chain::traits::message_builders::create_client::CreateClientMessageBuilder;
use hermes_relayer_components::chain::traits::types::client_state::HasClientStateType;
use hermes_relayer_components::chain::traits::types::consensus_state::HasConsensusStateType;
use hermes_relayer_components::chain::traits::types::create_client::{
HasCreateClientMessageOptionsType, HasCreateClientPayloadType,
};
Expand All @@ -12,35 +15,39 @@ use prost_types::Any;

use crate::traits::message::{CosmosMessage, ToCosmosMessage};
use crate::types::messages::client::create::CosmosCreateClientMessage;
use crate::types::payloads::client::CosmosCreateClientPayload;
use crate::types::tendermint::{TendermintClientState, TendermintConsensusState};

pub struct BuildCosmosCreateClientMessage;
pub struct BuildAnyCreateClientMessage;

impl<Chain, Counterparty, Encoding> CreateClientMessageBuilder<Chain, Counterparty>
for BuildCosmosCreateClientMessage
impl<Chain, Counterparty, Encoding, Payload> CreateClientMessageBuilder<Chain, Counterparty>
for BuildAnyCreateClientMessage
where
Chain: HasMessageType<Message = CosmosMessage>
+ HasCreateClientMessageOptionsType<Counterparty>
+ CanRaiseError<Encoding::Error>,
Counterparty: HasCreateClientPayloadType<Chain, CreateClientPayload = CosmosCreateClientPayload>
Counterparty: HasCreateClientPayloadType<Chain, CreateClientPayload = Payload>
+ HasClientStateType<Chain>
+ HasConsensusStateType<Chain>
+ HasDefaultEncoding<AsBytes, Encoding = Encoding>,
Encoding:
Async + CanConvert<TendermintClientState, Any> + CanConvert<TendermintConsensusState, Any>,
Payload: Async
+ HasField<symbol!("client_state"), Field = Counterparty::ClientState>
+ HasField<symbol!("consensus_state"), Field = Counterparty::ConsensusState>,
Encoding: Async
+ CanConvert<Counterparty::ClientState, Any>
+ CanConvert<Counterparty::ConsensusState, Any>,
{
async fn build_create_client_message(
_chain: &Chain,
_options: &Chain::CreateClientMessageOptions,
payload: CosmosCreateClientPayload,
payload: Payload,
) -> Result<CosmosMessage, Chain::Error> {
let encoding = Counterparty::default_encoding();

let client_state = encoding
.convert(&payload.client_state)
.convert(payload.get_field(PhantomData::<symbol!("client_state")>))
.map_err(Chain::raise_error)?;

let consensus_state = encoding
.convert(&payload.consensus_state)
.convert(payload.get_field(PhantomData::<symbol!("consensus_state")>))
.map_err(Chain::raise_error)?;

let message = CosmosCreateClientMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use cgp::core::error::HasErrorType;
use hermes_relayer_components::chain::traits::message_builders::update_client::UpdateClientMessageBuilder;
use hermes_relayer_components::chain::traits::types::ibc::HasIbcChainTypes;
use hermes_relayer_components::chain::traits::types::update_client::HasUpdateClientPayloadType;
use ibc_proto::google::protobuf::Any as IbcProtoAny;
use ibc_relayer_types::core::ics24_host::identifier::ClientId;
use prost_types::Any;

use crate::traits::message::{CosmosMessage, ToCosmosMessage};
use crate::types::messages::client::update::CosmosUpdateClientMessage;
Expand All @@ -27,9 +29,14 @@ where
.headers
.into_iter()
.map(|header| {
let header_any: IbcProtoAny = header.into();

let message = CosmosUpdateClientMessage {
client_id: client_id.clone(),
header: header.into(),
header: Any {
type_url: header_any.type_url,
value: header_any.value,
},
};

message.to_cosmos_message()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ where
type CreateClientPayloadOptions = Settings;
}

pub struct ProvideNoCreateClientMessageOptionsType;

impl<Chain, Counterparty> ProvideCreateClientMessageOptionsType<Chain, Counterparty>
for ProvideCosmosCreateClientSettings
for ProvideNoCreateClientMessageOptionsType
where
Chain: Async,
Counterparty: Async,
{
type CreateClientMessageOptions = ();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use ibc_proto::google::protobuf::Any;
use ibc_proto::google::protobuf::Any as IbcProtoAny;
use ibc_relayer_types::core::ics24_host::identifier::ClientId;
use ibc_relayer_types::signer::Signer;
use prost::Message;
use prost_types::Any;

use crate::methods::encode::encode_to_any;
use crate::traits::message::DynCosmosMessage;
Expand All @@ -28,7 +29,7 @@ pub struct ProtoMsgUpdateClient {
}

impl DynCosmosMessage for CosmosUpdateClientMessage {
fn encode_protobuf(&self, signer: &Signer) -> Any {
fn encode_protobuf(&self, signer: &Signer) -> IbcProtoAny {
let proto_message = ProtoMsgUpdateClient {
client_id: self.client_id.to_string(),
client_message: Some(self.header.clone()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use cgp::prelude::*;

use crate::types::tendermint::{TendermintClientState, TendermintConsensusState, TendermintHeader};

#[derive(Debug)]
pub struct CosmosUpdateClientPayload {
pub headers: Vec<TendermintHeader>,
}

#[derive(Debug)]
#[derive(Debug, HasField)]
pub struct CosmosCreateClientPayload {
pub client_state: TendermintClientState,
pub consensus_state: TendermintConsensusState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use hermes_relayer_components::chain::traits::message_builders::update_client::U
use hermes_relayer_components::chain::traits::types::ibc::HasIbcChainTypes;
use hermes_relayer_components::chain::traits::types::update_client::HasUpdateClientPayloadType;
use ibc::clients::wasm_types::client_message::{ClientMessage, WASM_CLIENT_MESSAGE_TYPE_URL};
use ibc_proto::google::protobuf::Any;
use ibc_proto::google::protobuf::Any as IbcProtoAny;
use ibc_proto::ibc::lightclients::wasm::v1::ClientMessage as RawClientMessage;
use ibc_proto::Protobuf;
use ibc_relayer_types::core::ics24_host::identifier::ClientId;
use prost::Message;
use prost_types::Any;

pub struct BuildUpdateWasmTendermintClientMessage;

Expand All @@ -31,7 +32,7 @@ where
.headers
.into_iter()
.map(|header| {
let any_header = Any::from(header);
let any_header = IbcProtoAny::from(header);

let wasm_message = ClientMessage {
data: any_header.encode_to_vec(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
Trait definition for [`HasTimestampType`].
*/

use alloc::borrow::ToOwned;
use core::fmt::Display;
use core::time::Duration;

use cgp::prelude::*;
Expand Down Expand Up @@ -32,7 +30,7 @@ pub trait HasTimestampType: Async {
concrete context implementers to decide which exact time type
they would like to use.
*/
type Timestamp: Ord + Display + ToOwned<Owned = Self::Timestamp> + Async;
type Timestamp: Ord + Async;

/**
Returns the amount of time elapsed from an `earlier` instant to a `later` one,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cgp::core::error::HasErrorType;
use hermes_cosmos_chain_components::traits::message::{CosmosMessage, ToCosmosMessage};
use hermes_cosmos_chain_components::types::messages::client::update::CosmosUpdateClientMessage;
use hermes_protobuf_encoding_components::types::Any;
use hermes_relayer_components::chain::traits::message_builders::update_client::UpdateClientMessageBuilder;
use hermes_relayer_components::chain::traits::types::ibc::HasIbcChainTypes;
use hermes_relayer_components::chain::traits::types::update_client::HasUpdateClientPayloadType;
Expand Down Expand Up @@ -28,7 +29,10 @@ where

let message = CosmosUpdateClientMessage {
client_id: client_id.clone(),
header,
header: Any {
type_url: header.type_url,
value: header.value,
},
};

Ok(vec![message.to_cosmos_message()])
Expand Down

0 comments on commit f25d390

Please sign in to comment.