Skip to content

Commit

Permalink
Use host_timestamp() instead of now
Browse files Browse the repository at this point in the history
  • Loading branch information
hu55a1n1 committed Jan 17, 2022
1 parent 81ade41 commit 82d84e9
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 77 deletions.
11 changes: 6 additions & 5 deletions modules/src/clients/ics07_tendermint/client_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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;
Expand Down Expand Up @@ -50,7 +49,6 @@ 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 @@ -113,9 +111,12 @@ impl ClientDef for TendermintClient {

let options = client_state.as_light_client_options()?;

let verdict = self
.verifier
.verify(untrusted_state, trusted_state, &options, now);
let verdict = self.verifier.verify(
untrusted_state,
trusted_state,
&options,
ctx.host_timestamp().into_tm_time().unwrap(),
);

match verdict {
Verdict::Success => {}
Expand Down
21 changes: 4 additions & 17 deletions modules/src/core/ics02_client/client_def.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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 @@ -30,7 +29,6 @@ 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 @@ -197,7 +195,6 @@ 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 @@ -211,13 +208,8 @@ 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(
now,
ctx,
client_id,
client_state,
header,
)?;
let (new_state, new_consensus) =
client.check_header_and_update_state(ctx, client_id, client_state, header)?;

Ok((
AnyClientState::Tendermint(new_state),
Expand All @@ -233,13 +225,8 @@ 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(
now,
ctx,
client_id,
client_state,
header,
)?;
let (new_state, new_consensus) =
client.check_header_and_update_state(ctx, client_id, client_state, header)?;

Ok((
AnyClientState::Mock(new_state),
Expand Down
12 changes: 3 additions & 9 deletions modules/src/core/ics02_client/handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! 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 @@ -19,17 +17,13 @@ pub enum ClientResult {
}

/// General entry point for processing any message related to ICS2 (client functions) protocols.
pub fn dispatch<Ctx>(
now: Time,
ctx: &Ctx,
msg: ClientMsg,
) -> Result<HandlerOutput<ClientResult>, Error>
pub fn dispatch<Ctx>(ctx: &Ctx, msg: ClientMsg) -> Result<HandlerOutput<ClientResult>, Error>
where
Ctx: ClientReader,
{
match msg {
ClientMsg::CreateClient(msg) => create_client::process(now, ctx, msg),
ClientMsg::UpdateClient(msg) => update_client::process(now, ctx, msg),
ClientMsg::CreateClient(msg) => create_client::process(ctx, msg),
ClientMsg::UpdateClient(msg) => update_client::process(ctx, msg),
ClientMsg::UpgradeClient(msg) => upgrade_client::process(ctx, msg),
_ => {
unimplemented!()
Expand Down
10 changes: 3 additions & 7 deletions modules/src/core/ics02_client/handler/create_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! 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 @@ -31,7 +29,6 @@ pub struct Result {
}

pub fn process(
now: Time,
ctx: &dyn ClientReader,
msg: MsgCreateAnyClient,
) -> HandlerResult<ClientResult, Error> {
Expand Down Expand Up @@ -71,7 +68,6 @@ 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 @@ -108,7 +104,7 @@ mod tests {
)
.unwrap();

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

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

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

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

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

match output {
Ok(HandlerOutput {
Expand Down
28 changes: 11 additions & 17 deletions modules/src/core/ics02_client/handler/update_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! 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 @@ -32,7 +30,6 @@ pub struct Result {
}

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

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

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

if client_state.expired(duration) {
Expand All @@ -85,7 +80,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(now, ctx, client_id.clone(), client_state, header)
.check_header_and_update_state(ctx, client_id.clone(), client_state, header)
.map_err(|e| Error::header_verification_failure(e.to_string()))?;

let result = ClientResult::Update(Result {
Expand All @@ -108,7 +103,6 @@ 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 @@ -148,7 +142,7 @@ mod tests {
signer,
};

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

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

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

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

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

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

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

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

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

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

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

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

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

match output {
Ok(_) => {
Expand Down
7 changes: 3 additions & 4 deletions modules/src/core/ics02_client/handler/upgrade_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ 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 @@ -110,7 +109,7 @@ mod tests {
signer,
};

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

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

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

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

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

match output {
Err(Error(ErrorDetail::LowUpgradeHeight(e), _)) => {
Expand Down
17 changes: 5 additions & 12 deletions modules/src/core/ics26_routing/handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::prelude::*;

use prost_types::Any;
use tendermint::Time;

use crate::applications::ics20_fungible_token_transfer::relay_application_logic::send_transfer::send_transfer as ics20_msg_dispatcher;
use crate::core::ics02_client::handler::dispatch as ics2_msg_dispatcher;
Expand All @@ -18,7 +17,7 @@ use crate::{events::IbcEvent, handler::HandlerOutput};
/// Mimics the DeliverTx ABCI interface, but a slightly lower level. No need for authentication
/// info or signature checks here.
/// Returns a vector of all events that got generated as a byproduct of processing `messages`.
pub fn deliver<Ctx>(now: Time, ctx: &mut Ctx, messages: Vec<Any>) -> Result<Vec<IbcEvent>, Error>
pub fn deliver<Ctx>(ctx: &mut Ctx, messages: Vec<Any>) -> Result<Vec<IbcEvent>, Error>
where
Ctx: Ics26Context,
{
Expand All @@ -33,7 +32,7 @@ where
let envelope = decode(any_msg)?;

// Process the envelope, and accumulate any events that were generated.
let mut output = dispatch(now, &mut ctx_interim, envelope)?;
let mut output = dispatch(&mut ctx_interim, envelope)?;
// TODO: output.log and output.result are discarded
res.append(&mut output.events);
}
Expand All @@ -51,17 +50,13 @@ pub fn decode(message: Any) -> Result<Ics26Envelope, Error> {
/// Top-level ICS dispatch function. Routes incoming IBC messages to their corresponding module.
/// Returns a handler output with empty result of type `HandlerOutput<()>` which contains the log
/// and events produced after processing the input `msg`.
pub fn dispatch<Ctx>(
now: Time,
ctx: &mut Ctx,
msg: Ics26Envelope,
) -> Result<HandlerOutput<()>, Error>
pub fn dispatch<Ctx>(ctx: &mut Ctx, msg: Ics26Envelope) -> Result<HandlerOutput<()>, Error>
where
Ctx: Ics26Context,
{
let output = match msg {
Ics2Msg(msg) => {
let handler_output = ics2_msg_dispatcher(now, ctx, msg).map_err(Error::ics02_client)?;
let handler_output = ics2_msg_dispatcher(ctx, msg).map_err(Error::ics02_client)?;
// Apply the result to the context (host chain store).
ctx.store_client_result(handler_output.result)
.map_err(Error::ics02_client)?;
Expand Down Expand Up @@ -134,7 +129,6 @@ where
mod tests {
use crate::prelude::*;

use tendermint::Time;
use test_log::test;

use crate::core::ics02_client::client_consensus::AnyConsensusState;
Expand Down Expand Up @@ -274,7 +268,6 @@ mod tests {

// First, create a client..
let res = dispatch(
Time::now(),
&mut ctx,
Ics26Envelope::Ics2Msg(ClientMsg::CreateClient(create_client_msg.clone())),
);
Expand Down Expand Up @@ -475,7 +468,7 @@ mod tests {
.collect();

for test in tests {
let res = dispatch(Time::now(), &mut ctx, test.msg.clone());
let res = dispatch(&mut ctx, test.msg.clone());

assert_eq!(
test.want_pass,
Expand Down
2 changes: 0 additions & 2 deletions modules/src/mock/client_def.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ibc_proto::ibc::core::commitment::v1::MerkleProof;
use tendermint::Time;

use crate::core::ics02_client::client_consensus::AnyConsensusState;
use crate::core::ics02_client::client_def::ClientDef;
Expand Down Expand Up @@ -32,7 +31,6 @@ impl ClientDef for MockClient {

fn check_header_and_update_state(
&self,
_now: Time,
_ctx: &dyn ClientReader,
_client_id: ClientId,
client_state: Self::ClientState,
Expand Down
Loading

0 comments on commit 82d84e9

Please sign in to comment.