From 82dd5b6c922bf453483aeb545a977d51779d1fcc Mon Sep 17 00:00:00 2001 From: Soares Chen Date: Wed, 6 Oct 2021 09:45:49 +0200 Subject: [PATCH] Remove `ibc::Timestamp::now` to support no_std (#1426) --- Cargo.lock | 1 + modules/src/ics04_channel/packet.rs | 4 ++-- modules/src/timestamp.rs | 12 +++--------- relayer/Cargo.toml | 1 + relayer/src/foreign_client.rs | 3 ++- relayer/src/link/relay_path.rs | 4 +++- relayer/src/transfer.rs | 4 +++- 7 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 302aaa9d57..48bd040837 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1331,6 +1331,7 @@ dependencies = [ "bech32", "bitcoin", "bytes", + "chrono", "crossbeam-channel 0.5.1", "dirs-next", "env_logger", diff --git a/modules/src/ics04_channel/packet.rs b/modules/src/ics04_channel/packet.rs index 471b00ee8d..4f3a55a240 100644 --- a/modules/src/ics04_channel/packet.rs +++ b/modules/src/ics04_channel/packet.rs @@ -114,10 +114,10 @@ pub struct Packet { } impl Packet { - pub fn timed_out(&self, dst_chain_height: Height) -> bool { + pub fn timed_out(&self, now: &Timestamp, dst_chain_height: Height) -> bool { (self.timeout_height != Height::zero() && self.timeout_height < dst_chain_height) || (self.timeout_timestamp != Timestamp::none() - && Timestamp::now().check_expiry(&self.timeout_timestamp) == Expired) + && now.check_expiry(&self.timeout_timestamp) == Expired) } } diff --git a/modules/src/timestamp.rs b/modules/src/timestamp.rs index 44e8e63742..fe08476b08 100644 --- a/modules/src/timestamp.rs +++ b/modules/src/timestamp.rs @@ -59,13 +59,6 @@ impl Timestamp { } } - /// Returns a `Timestamp` representation of the current time. - pub fn now() -> Timestamp { - Timestamp { - time: Some(Utc::now()), - } - } - /// Returns a `Timestamp` representation of a timestamp not being set. pub fn none() -> Self { Timestamp { time: None } @@ -193,6 +186,7 @@ impl Default for Timestamp { #[cfg(test)] mod tests { + use chrono::Utc; use core::convert::TryInto; use core::time::Duration; use std::thread::sleep; @@ -257,9 +251,9 @@ mod tests { fn subtract_compare() { let sleep_duration = Duration::from_micros(100); - let start = Timestamp::now(); + let start = Timestamp::from_datetime(Utc::now()); sleep(sleep_duration); - let end = Timestamp::now(); + let end = Timestamp::from_datetime(Utc::now()); let res = end.duration_since(&start); assert!(res.is_some()); diff --git a/relayer/Cargo.toml b/relayer/Cargo.toml index e633df2adb..49eb60f5ad 100644 --- a/relayer/Cargo.toml +++ b/relayer/Cargo.toml @@ -26,6 +26,7 @@ ibc = { version = "0.7.3", path = "../modules" } ibc-proto = { version = "0.11.0", path = "../proto" } ibc-telemetry = { version = "0.7.3", path = "../telemetry", optional = true } +chrono = { version = "0.4.19", default-features = false, features = ["clock"] } subtle-encoding = "0.5" async-trait = "0.1.50" humantime-serde = "1.0.0" diff --git a/relayer/src/foreign_client.rs b/relayer/src/foreign_client.rs index e79f86611a..a18de701dd 100644 --- a/relayer/src/foreign_client.rs +++ b/relayer/src/foreign_client.rs @@ -2,6 +2,7 @@ use core::{fmt, time::Duration}; use std::thread; use std::time::Instant; +use chrono::Utc; use itertools::Itertools; use prost_types::Any; use tracing::{debug, error, info, trace, warn}; @@ -524,7 +525,7 @@ impl ForeignClient RelayPath { .state_matches(&ChannelState::Closed) { Ok(self.build_timeout_on_close_packet(&event.packet, dst_chain_height)?) - } else if packet.timed_out(dst_chain_height) { + } else if packet.timed_out(&Timestamp::from_datetime(Utc::now()), dst_chain_height) { Ok(self.build_timeout_packet(&event.packet, dst_chain_height)?) } else { Ok(None) diff --git a/relayer/src/transfer.rs b/relayer/src/transfer.rs index d3d626446e..c3bda4a4d1 100644 --- a/relayer/src/transfer.rs +++ b/relayer/src/transfer.rs @@ -2,6 +2,7 @@ use core::fmt::{Display, Formatter}; use core::str::FromStr; use core::time::Duration; +use chrono::Utc; use flex_error::{define_error, DetailOnly}; use ibc::application::ics20_fungible_token_transfer::msgs::transfer::MsgTransfer; use ibc::events::IbcEvent; @@ -101,7 +102,8 @@ pub fn build_and_send_transfer_messages( let timeout_timestamp = if opts.timeout_seconds == Duration::from_secs(0) { Timestamp::none() } else { - (Timestamp::now() + opts.timeout_seconds).map_err(PacketError::timestamp_overflow)? + (Timestamp::from_datetime(Utc::now()) + opts.timeout_seconds) + .map_err(PacketError::timestamp_overflow)? }; let timeout_height = if opts.timeout_height_offset == 0 {