Skip to content

Commit

Permalink
imp: allow ConsensusState::timestamp() return error
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani committed Sep 24, 2024
1 parent 14abdcc commit bd27973
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ibc-clients/ics07-tendermint/src/client_state/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub fn consensus_state_status<CS: ConsensusState>(
// consensus state is in the future, then we don't consider the client
// to be expired.
if let Some(elapsed_since_latest_consensus_state) =
host_timestamp.duration_since(&consensus_state.timestamp())
host_timestamp.duration_since(&consensus_state.timestamp()?)
{
// Note: The equality is considered as expired to stay consistent with
// the check in tendermint-rs, where a header at `trusted_header_time +
Expand Down
8 changes: 3 additions & 5 deletions ibc-clients/ics07-tendermint/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ibc_client_tendermint_types::proto::v1::ConsensusState as RawTmConsensusState;
use ibc_client_tendermint_types::ConsensusState as ConsensusStateType;
use ibc_core_client::context::consensus_state::ConsensusState as ConsensusStateTrait;
use ibc_core_client::types::error::ClientError;
use ibc_core_commitment_types::commitment::CommitmentRoot;
use ibc_core_host::types::error::DecodingError;
use ibc_primitives::prelude::*;
Expand Down Expand Up @@ -91,10 +92,7 @@ impl ConsensusStateTrait for ConsensusState {
&self.0.root
}

fn timestamp(&self) -> Timestamp {
self.0
.timestamp
.into_timestamp()
.expect("UNIX Timestamp can't be negative")
fn timestamp(&self) -> Result<Timestamp, ClientError> {
self.0.timestamp.into_timestamp().map_err(Into::into)
}
}
1 change: 1 addition & 0 deletions ibc-clients/ics07-tendermint/types/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl ConsensusState {
}
}

/// Returns the timestamp of the consensus state as a `tendermint::Time`.
pub fn timestamp(&self) -> Time {
self.timestamp
}
Expand Down
3 changes: 2 additions & 1 deletion ibc-core/ics02-client/context/src/consensus_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Defines the trait to be implemented by all concrete consensus state types
use ibc_core_client_types::error::ClientError;
use ibc_core_commitment_types::commitment::CommitmentRoot;
use ibc_primitives::prelude::*;
use ibc_primitives::proto::Any;
Expand All @@ -16,5 +17,5 @@ pub trait ConsensusState: Send + Sync + Convertible<Any> {
fn root(&self) -> &CommitmentRoot;

/// The timestamp of the consensus state
fn timestamp(&self) -> Timestamp;
fn timestamp(&self) -> Result<Timestamp, ClientError>;
}
2 changes: 1 addition & 1 deletion ibc-core/ics04-channel/src/handler/send_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn send_packet_validate(
);
let consensus_state_of_b_on_a =
client_val_ctx_a.consensus_state(&client_cons_state_path_on_a)?;
let latest_timestamp = consensus_state_of_b_on_a.timestamp();
let latest_timestamp = consensus_state_of_b_on_a.timestamp()?;
let packet_timestamp = packet.timeout_timestamp_on_b;
if packet_timestamp.has_expired(&latest_timestamp) {
return Err(ChannelError::ExpiredPacketTimestamp);
Expand Down
2 changes: 1 addition & 1 deletion ibc-core/ics04-channel/src/handler/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ where
);
let consensus_state_of_b_on_a =
client_val_ctx_a.consensus_state(&client_cons_state_path_on_a)?;
let timestamp_of_b = consensus_state_of_b_on_a.timestamp();
let timestamp_of_b = consensus_state_of_b_on_a.timestamp()?;

if !msg.packet.timed_out(&timestamp_of_b, msg.proof_height_on_b) {
return Err(ChannelError::InsufficientPacketTimeout {
Expand Down
3 changes: 2 additions & 1 deletion ibc-derive/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn consensus_state_derive_impl(ast: DeriveInput, imports: &Imports) -> Token
let CommitmentRoot = imports.commitment_root();
let ConsensusState = imports.consensus_state();
let Timestamp = imports.timestamp();
let ClientError = imports.client_error();

quote! {
impl #ConsensusState for #enum_name {
Expand All @@ -33,7 +34,7 @@ pub fn consensus_state_derive_impl(ast: DeriveInput, imports: &Imports) -> Token
}
}

fn timestamp(&self) -> #Timestamp {
fn timestamp(&self) -> core::result::Result<#Timestamp, #ClientError> {
match self {
#(#timestamp_impl),*
}
Expand Down
5 changes: 3 additions & 2 deletions ibc-testkit/src/testapp/ibc/clients/mock/consensus_state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ibc::core::client::context::consensus_state::ConsensusState;
use ibc::core::client::types::error::ClientError;
use ibc::core::commitment_types::commitment::CommitmentRoot;
use ibc::core::host::types::error::DecodingError;
use ibc::core::primitives::prelude::*;
Expand Down Expand Up @@ -91,7 +92,7 @@ impl ConsensusState for MockConsensusState {
&self.root
}

fn timestamp(&self) -> Timestamp {
self.header.timestamp
fn timestamp(&self) -> Result<Timestamp, ClientError> {
Ok(self.header.timestamp)
}
}
7 changes: 6 additions & 1 deletion ibc-testkit/src/testapp/ibc/core/core_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ where
fn host_timestamp(&self) -> Result<Timestamp, HostError> {
let host_height = self.host_height()?;
let host_cons_state = self.host_consensus_state(&host_height)?;
Ok(host_cons_state.timestamp())
let timestamp = host_cons_state
.timestamp()
.map_err(|e| HostError::InvalidState {
description: e.to_string(),

Check warning on line 54 in ibc-testkit/src/testapp/ibc/core/core_ctx.rs

View check run for this annotation

Codecov / codecov/patch

ibc-testkit/src/testapp/ibc/core/core_ctx.rs#L54

Added line #L54 was not covered by tests
})?;
Ok(timestamp)
}

fn client_counter(&self) -> Result<u64, HostError> {
Expand Down

0 comments on commit bd27973

Please sign in to comment.