From e2c3d1334f6af142f726837c6f632e5c515ef47c Mon Sep 17 00:00:00 2001 From: Davirain Date: Mon, 26 Jun 2023 22:28:36 +0800 Subject: [PATCH] Remove Header trait (#617) from IBC crate. (#730) * Remove Header trait (#617) from IBC crate. * Remove query_latest_header() definition from MockContext and related code cleanup. --- .../improvement/617-remove-header-trait.md | 1 + .../src/clients/ics07_tendermint/header.rs | 14 +--- crates/ibc/src/core/ics02_client/header.rs | 77 ------------------- crates/ibc/src/core/ics02_client/mod.rs | 1 - crates/ibc/src/mock/context.rs | 11 ++- crates/ibc/src/mock/header.rs | 11 --- crates/ibc/src/mock/host.rs | 11 --- crates/ibc/src/mock/ics18_relayer/context.rs | 26 ++----- 8 files changed, 16 insertions(+), 136 deletions(-) create mode 100644 .changelog/unreleased/improvement/617-remove-header-trait.md delete mode 100644 crates/ibc/src/core/ics02_client/header.rs diff --git a/.changelog/unreleased/improvement/617-remove-header-trait.md b/.changelog/unreleased/improvement/617-remove-header-trait.md new file mode 100644 index 000000000..54670bd48 --- /dev/null +++ b/.changelog/unreleased/improvement/617-remove-header-trait.md @@ -0,0 +1 @@ +- Remove Header trait ([#617](https://github.com/cosmos/ibc-rs/issues/617)) \ No newline at end of file diff --git a/crates/ibc/src/clients/ics07_tendermint/header.rs b/crates/ibc/src/clients/ics07_tendermint/header.rs index db5745eed..b85dbdac5 100644 --- a/crates/ibc/src/clients/ics07_tendermint/header.rs +++ b/crates/ibc/src/clients/ics07_tendermint/header.rs @@ -48,6 +48,10 @@ impl Display for Header { } impl Header { + pub fn timestamp(&self) -> Timestamp { + self.signed_header.header.time.into() + } + pub fn height(&self) -> Height { Height::new( ChainId::chain_version(self.signed_header.header.chain_id.as_str()), @@ -131,16 +135,6 @@ impl Header { } } -impl crate::core::ics02_client::header::Header for Header { - fn height(&self) -> Height { - self.height() - } - - fn timestamp(&self) -> Timestamp { - self.signed_header.header.time.into() - } -} - impl Protobuf for Header {} impl TryFrom for Header { diff --git a/crates/ibc/src/core/ics02_client/header.rs b/crates/ibc/src/core/ics02_client/header.rs deleted file mode 100644 index 8637d1936..000000000 --- a/crates/ibc/src/core/ics02_client/header.rs +++ /dev/null @@ -1,77 +0,0 @@ -//! Defines the trait to be implemented by concrete header types - -use crate::prelude::*; - -use dyn_clone::DynClone; -use ibc_proto::google::protobuf::Any; -use ibc_proto::protobuf::Protobuf as ErasedProtobuf; - -use crate::clients::AsAny; -use crate::core::ics02_client::error::ClientError; -use crate::core::timestamp::Timestamp; -use crate::erased::ErasedSerialize; -use crate::Height; - -/// Abstract of consensus state update information -/// -/// Users are not expected to implement sealed::ErasedPartialEqHeader. -/// Effectively, that trait bound mandates implementors to derive PartialEq, -/// after which our blanket implementation will implement -/// `ErasedPartialEqHeader` for their type. -pub trait Header: - AsAny - + sealed::ErasedPartialEqHeader - + DynClone - + ErasedSerialize - + ErasedProtobuf - + core::fmt::Debug - + Send - + Sync -{ - /// The height of the consensus state - fn height(&self) -> Height; - - /// The timestamp of the consensus state - fn timestamp(&self) -> Timestamp; - - /// Convert into a boxed trait object - fn into_box(self) -> Box - where - Self: Sized, - { - Box::new(self) - } -} - -// Implements `Clone` for `Box` -dyn_clone::clone_trait_object!(Header); - -// Implements `serde::Serialize` for all types that have Header as supertrait -#[cfg(feature = "serde")] -erased_serde::serialize_trait_object!(Header); - -impl PartialEq for dyn Header { - fn eq(&self, other: &Self) -> bool { - self.eq_header(other) - } -} - -mod sealed { - use super::*; - - pub trait ErasedPartialEqHeader { - fn eq_header(&self, other: &dyn Header) -> bool; - } - - impl ErasedPartialEqHeader for H - where - H: Header + PartialEq, - { - fn eq_header(&self, other: &dyn Header) -> bool { - other - .as_any() - .downcast_ref::() - .map_or(false, |h| self == h) - } - } -} diff --git a/crates/ibc/src/core/ics02_client/mod.rs b/crates/ibc/src/core/ics02_client/mod.rs index 54582eb59..658792962 100644 --- a/crates/ibc/src/core/ics02_client/mod.rs +++ b/crates/ibc/src/core/ics02_client/mod.rs @@ -6,6 +6,5 @@ pub mod consensus_state; pub mod error; pub mod events; pub mod handler; -pub mod header; pub mod height; pub mod msgs; diff --git a/crates/ibc/src/mock/context.rs b/crates/ibc/src/mock/context.rs index 5d484d5d5..dffea9ddf 100644 --- a/crates/ibc/src/mock/context.rs +++ b/crates/ibc/src/mock/context.rs @@ -30,7 +30,6 @@ use crate::core::ics02_client::client_state::ClientState; use crate::core::ics02_client::client_type::ClientType; use crate::core::ics02_client::consensus_state::ConsensusState; use crate::core::ics02_client::error::ClientError; -use crate::core::ics02_client::header::Header; use crate::core::ics03_connection::connection::ConnectionEnd; use crate::core::ics03_connection::error::ConnectionError; use crate::core::ics04_channel::channel::ChannelEnd; @@ -600,6 +599,11 @@ impl MockContext { pub fn ibc_store_share(&self) -> Arc> { self.ibc_store.clone() } + + pub fn query_latest_header(&self) -> Option { + let block_ref = self.host_block(&self.host_height().unwrap()); + block_ref.cloned() + } } type PortChannelIdMap = BTreeMap>; @@ -669,11 +673,6 @@ impl RelayerContext for MockContext { ValidationContext::client_state(self, client_id).ok() } - fn query_latest_header(&self) -> Option> { - let block_ref = self.host_block(&self.host_height().unwrap()); - block_ref.cloned().map(Header::into_box) - } - fn signer(&self) -> Signer { "0CDA3F47EF3C4906693B170EF650EB968C5F4B2C" .to_string() diff --git a/crates/ibc/src/mock/header.rs b/crates/ibc/src/mock/header.rs index 9bda4bec7..1054e83a8 100644 --- a/crates/ibc/src/mock/header.rs +++ b/crates/ibc/src/mock/header.rs @@ -6,7 +6,6 @@ use ibc_proto::ibc::mock::Header as RawMockHeader; use ibc_proto::protobuf::Protobuf; use crate::core::ics02_client::error::ClientError; -use crate::core::ics02_client::header::Header; use crate::core::timestamp::Timestamp; use crate::Height; @@ -89,16 +88,6 @@ impl MockHeader { } } -impl Header for MockHeader { - fn height(&self) -> Height { - self.height - } - - fn timestamp(&self) -> Timestamp { - self.timestamp - } -} - impl Protobuf for MockHeader {} impl TryFrom for MockHeader { diff --git a/crates/ibc/src/mock/host.rs b/crates/ibc/src/mock/host.rs index a7c609750..fec424aea 100644 --- a/crates/ibc/src/mock/host.rs +++ b/crates/ibc/src/mock/host.rs @@ -12,7 +12,6 @@ use crate::clients::ics07_tendermint::consensus_state::ConsensusState as TMConse use crate::clients::ics07_tendermint::header::TENDERMINT_HEADER_TYPE_URL; use crate::core::ics02_client::consensus_state::ConsensusState; use crate::core::ics02_client::error::ClientError; -use crate::core::ics02_client::header::Header; use crate::core::ics24_host::identifier::ChainId; use crate::core::timestamp::Timestamp; use crate::mock::consensus_state::MockConsensusState; @@ -181,13 +180,3 @@ impl From for Any { } } } - -impl Header for HostBlock { - fn height(&self) -> Height { - HostBlock::height(self) - } - - fn timestamp(&self) -> Timestamp { - HostBlock::timestamp(self) - } -} diff --git a/crates/ibc/src/mock/ics18_relayer/context.rs b/crates/ibc/src/mock/ics18_relayer/context.rs index 5b8322b17..797d68147 100644 --- a/crates/ibc/src/mock/ics18_relayer/context.rs +++ b/crates/ibc/src/mock/ics18_relayer/context.rs @@ -1,7 +1,6 @@ use crate::prelude::*; use crate::core::ics02_client::client_state::ClientState; -use crate::core::ics02_client::header::Header; use crate::core::ics24_host::identifier::ClientId; use crate::core::ContextError; @@ -21,9 +20,6 @@ pub trait RelayerContext { /// Wrapper over the `/abci_query?path=..` endpoint. fn query_client_full_state(&self, client_id: &ClientId) -> Option>; - /// Returns the most advanced header of this chain. - fn query_latest_header(&self) -> Option>; - /// Temporary solution. Similar to `CosmosSDKChain::key_and_signer()` but simpler. fn signer(&self) -> Signer; } @@ -31,7 +27,6 @@ pub trait RelayerContext { #[cfg(test)] mod tests { use crate::clients::ics07_tendermint::client_type as tm_client_type; - use crate::core::ics02_client::header::Header; use crate::core::ics02_client::msgs::update_client::MsgUpdateClient; use crate::core::ics02_client::msgs::ClientMsg; use crate::core::ics24_host::identifier::{ChainId, ClientId}; @@ -47,16 +42,12 @@ mod tests { use test_log::test; use tracing::debug; - fn downcast_header(h: &dyn Header) -> Option<&H> { - h.as_any().downcast_ref::() - } - /// Builds a `ClientMsg::UpdateClient` for a client with id `client_id` running on the `dest` /// context, assuming that the latest header on the source context is `src_header`. pub(crate) fn build_client_update_datagram( dest: &Ctx, client_id: &ClientId, - src_header: &dyn Header, + src_header: &HostBlock, ) -> Result where Ctx: RelayerContext, @@ -90,7 +81,7 @@ mod tests { // Client on destination chain can be updated. Ok(ClientMsg::UpdateClient(MsgUpdateClient { client_id: client_id.clone(), - header: src_header.clone_into(), + header: (*src_header).clone().into(), signer: dest.signer(), })) } @@ -141,7 +132,7 @@ mod tests { // - create the client update message with the latest header from A let a_latest_header = ctx_a.query_latest_header().unwrap(); let client_msg_b_res = - build_client_update_datagram(&ctx_b, &client_on_b_for_a, a_latest_header.as_ref()); + build_client_update_datagram(&ctx_b, &client_on_b_for_a, &a_latest_header); assert!( client_msg_b_res.is_ok(), @@ -173,18 +164,13 @@ mod tests { // Update client on chain A to latest height of B. // - create the client update message with the latest header from B // The test uses LightClientBlock that does not store the trusted height - let b_latest_header = ctx_b.query_latest_header().unwrap(); - let b_latest_header: &HostBlock = downcast_header(b_latest_header.as_ref()).unwrap(); - let mut b_latest_header = b_latest_header.clone(); + let mut b_latest_header = ctx_b.query_latest_header().unwrap(); let th = b_latest_header.height(); b_latest_header.set_trusted_height(th.decrement().unwrap()); - let client_msg_a_res = build_client_update_datagram( - &ctx_a, - &client_on_a_for_b, - b_latest_header.into_box().as_ref(), - ); + let client_msg_a_res = + build_client_update_datagram(&ctx_a, &client_on_a_for_b, &b_latest_header); assert!( client_msg_a_res.is_ok(),