Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide ibc::Timestamp::now() behind clock feature flag #1747

Merged
merged 3 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/unreleased/breaking-changes/1612-ibc-clock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Hide `ibc::Timestamp::now()` behind `clock` feature flag (#1612)[https://github.com/informalsystems/ibc-rs/pull/1612]
9 changes: 7 additions & 2 deletions modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ description = """
all-features = true

[features]
default = ["flex-error/std", "flex-error/eyre_tracer"]
default = ["std"]
std = ["flex-error/std", "flex-error/eyre_tracer", "clock"]
clock = ["tendermint/clock", "time/std"]

# This feature grants access to development-time mocking libraries, such as `MockContext` or `MockHeader`.
# Depends on the `testgen` suite for generating Tendermint light blocks.
mocks = ["tendermint-testgen"]
mocks = ["tendermint-testgen", "clock"]

[dependencies]
# Proto definitions for all IBC-related interfaces, e.g., connections or channels.
Expand All @@ -44,9 +46,11 @@ derive_more = { version = "0.99.0", default-features = false, features = ["from"

[dependencies.tendermint]
version = "=0.23.5"
default-features = false

[dependencies.tendermint-proto]
version = "=0.23.5"
default-features = false

[dependencies.tendermint-light-client-verifier]
version = "=0.23.5"
Expand All @@ -55,6 +59,7 @@ default-features = false
[dependencies.tendermint-testgen]
version = "=0.23.5"
optional = true
default-features = false

[dev-dependencies]
env_logger = "0.9.0"
Expand Down
12 changes: 5 additions & 7 deletions modules/src/clients/ics07_tendermint/client_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use core::convert::TryInto;

use ibc_proto::ibc::core::commitment::v1::MerkleProof as RawMerkleProof;
use prost::Message;
use tendermint::Time;
use tendermint_light_client_verifier::types::{TrustedBlockState, UntrustedBlockState};
use tendermint_light_client_verifier::{ProdVerifier, Verdict, Verifier};
use tendermint_proto::Protobuf;
use time::OffsetDateTime;

use crate::clients::ics07_tendermint::client_state::ClientState;
use crate::clients::ics07_tendermint::consensus_state::ConsensusState;
Expand Down Expand Up @@ -50,6 +50,7 @@ impl ClientDef for TendermintClient {

fn check_header_and_update_state(
&self,
now: Time,
ctx: &dyn ClientReader,
client_id: ClientId,
client_state: Self::ClientState,
Expand Down Expand Up @@ -112,12 +113,9 @@ impl ClientDef for TendermintClient {

let options = client_state.as_light_client_options()?;

let verdict = self.verifier.verify(
untrusted_state,
trusted_state,
&options,
OffsetDateTime::now_utc().try_into().unwrap(),
);
let verdict = self
.verifier
.verify(untrusted_state, trusted_state, &options, now);

match verdict {
Verdict::Success => {}
Expand Down
21 changes: 17 additions & 4 deletions modules/src/core/ics02_client/client_def.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ibc_proto::ibc::core::commitment::v1::MerkleProof;
use tendermint::Time;

use crate::clients::ics07_tendermint::client_def::TendermintClient;
use crate::core::ics02_client::client_consensus::{AnyConsensusState, ConsensusState};
Expand Down Expand Up @@ -29,6 +30,7 @@ pub trait ClientDef: Clone {

fn check_header_and_update_state(
&self,
now: Time,
ctx: &dyn ClientReader,
client_id: ClientId,
client_state: Self::ClientState,
Expand Down Expand Up @@ -195,6 +197,7 @@ impl ClientDef for AnyClient {
/// Validates an incoming `header` against the latest consensus state of this client.
fn check_header_and_update_state(
&self,
now: Time,
ctx: &dyn ClientReader,
client_id: ClientId,
client_state: AnyClientState,
Expand All @@ -208,8 +211,13 @@ impl ClientDef for AnyClient {
)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Tendermint))?;

let (new_state, new_consensus) =
client.check_header_and_update_state(ctx, client_id, client_state, header)?;
let (new_state, new_consensus) = client.check_header_and_update_state(
now,
ctx,
client_id,
client_state,
header,
)?;

Ok((
AnyClientState::Tendermint(new_state),
Expand All @@ -225,8 +233,13 @@ impl ClientDef for AnyClient {
)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Mock))?;

let (new_state, new_consensus) =
client.check_header_and_update_state(ctx, client_id, client_state, header)?;
let (new_state, new_consensus) = client.check_header_and_update_state(
now,
ctx,
client_id,
client_state,
header,
)?;

Ok((
AnyClientState::Mock(new_state),
Expand Down
12 changes: 9 additions & 3 deletions modules/src/core/ics02_client/handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! This module implements the processing logic for ICS2 (client abstractions and functions) msgs.

use tendermint::Time;

use crate::core::ics02_client::context::ClientReader;
use crate::core::ics02_client::error::Error;
use crate::core::ics02_client::msgs::ClientMsg;
Expand All @@ -17,13 +19,17 @@ pub enum ClientResult {
}

/// General entry point for processing any message related to ICS2 (client functions) protocols.
pub fn dispatch<Ctx>(ctx: &Ctx, msg: ClientMsg) -> Result<HandlerOutput<ClientResult>, Error>
pub fn dispatch<Ctx>(
now: Time,
ctx: &Ctx,
msg: ClientMsg,
) -> Result<HandlerOutput<ClientResult>, Error>
where
Ctx: ClientReader,
{
match msg {
ClientMsg::CreateClient(msg) => create_client::process(ctx, msg),
ClientMsg::UpdateClient(msg) => update_client::process(ctx, msg),
ClientMsg::CreateClient(msg) => create_client::process(now, ctx, msg),
ClientMsg::UpdateClient(msg) => update_client::process(now, ctx, msg),
ClientMsg::UpgradeClient(msg) => upgrade_client::process(ctx, msg),
_ => {
unimplemented!()
Expand Down
13 changes: 9 additions & 4 deletions modules/src/core/ics02_client/handler/create_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! Protocol logic specific to processing ICS2 messages of type `MsgCreateAnyClient`.

use tendermint::Time;

use crate::prelude::*;

use crate::core::ics02_client::client_consensus::AnyConsensusState;
Expand Down Expand Up @@ -27,6 +30,7 @@ pub struct Result {
}

pub fn process(
now: Time,
ctx: &dyn ClientReader,
msg: MsgCreateAnyClient,
) -> HandlerResult<ClientResult, Error> {
Expand All @@ -48,7 +52,7 @@ pub fn process(
client_type: msg.client_state().client_type(),
client_state: msg.client_state(),
consensus_state: msg.consensus_state(),
processed_time: Timestamp::now(),
processed_time: now.into(),
processed_height: ctx.host_height(),
});

Expand All @@ -66,6 +70,7 @@ mod tests {
use crate::prelude::*;

use core::time::Duration;
use tendermint::Time;
use test_log::test;

use crate::clients::ics07_tendermint::client_state::{
Expand Down Expand Up @@ -102,7 +107,7 @@ mod tests {
)
.unwrap();

let output = dispatch(&ctx, ClientMsg::CreateClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::CreateClient(msg.clone()));

match output {
Ok(HandlerOutput {
Expand Down Expand Up @@ -193,7 +198,7 @@ mod tests {
let expected_client_id = ClientId::new(ClientType::Mock, 0).unwrap();

for msg in create_client_msgs {
let output = dispatch(&ctx, ClientMsg::CreateClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::CreateClient(msg.clone()));

match output {
Ok(HandlerOutput {
Expand Down Expand Up @@ -255,7 +260,7 @@ mod tests {
)
.unwrap();

let output = dispatch(&ctx, ClientMsg::CreateClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::CreateClient(msg.clone()));

match output {
Ok(HandlerOutput {
Expand Down
24 changes: 14 additions & 10 deletions modules/src/core/ics02_client/handler/update_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Protocol logic specific to processing ICS2 messages of type `MsgUpdateAnyClient`.

use core::convert::From;
use tendermint::Time;
use tracing::debug;

use crate::core::ics02_client::client_consensus::AnyConsensusState;
Expand Down Expand Up @@ -27,6 +29,7 @@ pub struct Result {
}

pub fn process(
now: Time,
ctx: &dyn ClientReader,
msg: MsgUpdateAnyClient,
) -> HandlerResult<ClientResult, Error> {
Expand Down Expand Up @@ -59,12 +62,12 @@ pub fn process(

debug!("latest consensus state: {:?}", latest_consensus_state);

let duration = Timestamp::now()
let duration = Timestamp::from(now)
.duration_since(&latest_consensus_state.timestamp())
.ok_or_else(|| {
Error::invalid_consensus_state_timestamp(
latest_consensus_state.timestamp(),
header.timestamp(),
Timestamp::from(now),
)
})?;

Expand All @@ -79,7 +82,7 @@ pub fn process(
// This function will return the new client_state (its latest_height changed) and a
// consensus_state obtained from header. These will be later persisted by the keeper.
let (new_client_state, new_consensus_state) = client_def
.check_header_and_update_state(ctx, client_id.clone(), client_state, header)
.check_header_and_update_state(now, ctx, client_id.clone(), client_state, header)
.map_err(|e| Error::header_verification_failure(e.to_string()))?;

let result = ClientResult::Update(Result {
Expand All @@ -100,6 +103,7 @@ pub fn process(
#[cfg(test)]
mod tests {
use core::str::FromStr;
use tendermint::Time;
use test_log::test;

use crate::core::ics02_client::client_consensus::AnyConsensusState;
Expand Down Expand Up @@ -139,7 +143,7 @@ mod tests {
signer,
};

let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::UpdateClient(msg.clone()));

match output {
Ok(HandlerOutput {
Expand Down Expand Up @@ -186,7 +190,7 @@ mod tests {
signer,
};

let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::UpdateClient(msg.clone()));

match output {
Err(Error(ErrorDetail::ClientNotFound(e), _)) => {
Expand Down Expand Up @@ -222,7 +226,7 @@ mod tests {
signer: signer.clone(),
};

let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::UpdateClient(msg.clone()));

match output {
Ok(HandlerOutput {
Expand Down Expand Up @@ -289,7 +293,7 @@ mod tests {
signer,
};

let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::UpdateClient(msg.clone()));

match output {
Ok(HandlerOutput {
Expand Down Expand Up @@ -366,7 +370,7 @@ mod tests {
signer,
};

let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::UpdateClient(msg.clone()));

match output {
Ok(HandlerOutput {
Expand Down Expand Up @@ -446,7 +450,7 @@ mod tests {
signer,
};

let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::UpdateClient(msg.clone()));

match output {
Ok(HandlerOutput {
Expand Down Expand Up @@ -520,7 +524,7 @@ mod tests {
signer,
};

let output = dispatch(&ctx, ClientMsg::UpdateClient(msg));
let output = dispatch(Time::now(), &ctx, ClientMsg::UpdateClient(msg));

match output {
Ok(_) => {
Expand Down
7 changes: 4 additions & 3 deletions modules/src/core/ics02_client/handler/upgrade_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ mod tests {
use crate::prelude::*;

use core::str::FromStr;
use tendermint::Time;

use crate::core::ics02_client::error::{Error, ErrorDetail};
use crate::core::ics02_client::handler::dispatch;
Expand Down Expand Up @@ -117,7 +118,7 @@ mod tests {
signer,
};

let output = dispatch(&ctx, ClientMsg::UpgradeClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::UpgradeClient(msg.clone()));

match output {
Ok(HandlerOutput {
Expand Down Expand Up @@ -168,7 +169,7 @@ mod tests {
signer,
};

let output = dispatch(&ctx, ClientMsg::UpgradeClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::UpgradeClient(msg.clone()));

match output {
Err(Error(ErrorDetail::ClientNotFound(e), _)) => {
Expand Down Expand Up @@ -202,7 +203,7 @@ mod tests {
signer,
};

let output = dispatch(&ctx, ClientMsg::UpgradeClient(msg.clone()));
let output = dispatch(Time::now(), &ctx, ClientMsg::UpgradeClient(msg.clone()));

match output {
Err(Error(ErrorDetail::LowUpgradeHeight(e), _)) => {
Expand Down
Loading