Skip to content

Commit

Permalink
Remove Header trait (#617) from IBC crate. (#730)
Browse files Browse the repository at this point in the history
* Remove Header trait (#617) from IBC crate.

* Remove query_latest_header() definition from MockContext and related code cleanup.
  • Loading branch information
DaviRain-Su authored Jun 26, 2023
1 parent 6ccddf4 commit e2c3d13
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Remove Header trait ([#617](https://github.com/cosmos/ibc-rs/issues/617))
14 changes: 4 additions & 10 deletions crates/ibc/src/clients/ics07_tendermint/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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<RawHeader> for Header {}

impl TryFrom<RawHeader> for Header {
Expand Down
77 changes: 0 additions & 77 deletions crates/ibc/src/core/ics02_client/header.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/ibc/src/core/ics02_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
11 changes: 5 additions & 6 deletions crates/ibc/src/mock/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -600,6 +599,11 @@ impl MockContext {
pub fn ibc_store_share(&self) -> Arc<Mutex<MockIbcStore>> {
self.ibc_store.clone()
}

pub fn query_latest_header(&self) -> Option<HostBlock> {
let block_ref = self.host_block(&self.host_height().unwrap());
block_ref.cloned()
}
}

type PortChannelIdMap<V> = BTreeMap<PortId, BTreeMap<ChannelId, V>>;
Expand Down Expand Up @@ -669,11 +673,6 @@ impl RelayerContext for MockContext {
ValidationContext::client_state(self, client_id).ok()
}

fn query_latest_header(&self) -> Option<Box<dyn Header>> {
let block_ref = self.host_block(&self.host_height().unwrap());
block_ref.cloned().map(Header::into_box)
}

fn signer(&self) -> Signer {
"0CDA3F47EF3C4906693B170EF650EB968C5F4B2C"
.to_string()
Expand Down
11 changes: 0 additions & 11 deletions crates/ibc/src/mock/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -89,16 +88,6 @@ impl MockHeader {
}
}

impl Header for MockHeader {
fn height(&self) -> Height {
self.height
}

fn timestamp(&self) -> Timestamp {
self.timestamp
}
}

impl Protobuf<Any> for MockHeader {}

impl TryFrom<Any> for MockHeader {
Expand Down
11 changes: 0 additions & 11 deletions crates/ibc/src/mock/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -181,13 +180,3 @@ impl From<HostBlock> for Any {
}
}
}

impl Header for HostBlock {
fn height(&self) -> Height {
HostBlock::height(self)
}

fn timestamp(&self) -> Timestamp {
HostBlock::timestamp(self)
}
}
26 changes: 6 additions & 20 deletions crates/ibc/src/mock/ics18_relayer/context.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,17 +20,13 @@ pub trait RelayerContext {
/// Wrapper over the `/abci_query?path=..` endpoint.
fn query_client_full_state(&self, client_id: &ClientId) -> Option<Box<dyn ClientState>>;

/// Returns the most advanced header of this chain.
fn query_latest_header(&self) -> Option<Box<dyn Header>>;

/// Temporary solution. Similar to `CosmosSDKChain::key_and_signer()` but simpler.
fn signer(&self) -> Signer;
}

#[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};
Expand All @@ -47,16 +42,12 @@ mod tests {
use test_log::test;
use tracing::debug;

fn downcast_header<H: Header>(h: &dyn Header) -> Option<&H> {
h.as_any().downcast_ref::<H>()
}

/// 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<Ctx>(
dest: &Ctx,
client_id: &ClientId,
src_header: &dyn Header,
src_header: &HostBlock,
) -> Result<ClientMsg, RelayerError>
where
Ctx: RelayerContext,
Expand Down Expand Up @@ -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(),
}))
}
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit e2c3d13

Please sign in to comment.