Skip to content

Commit

Permalink
Remove ibc::Timestamp::now to support no_std (#1426)
Browse files Browse the repository at this point in the history
  • Loading branch information
soareschen authored Oct 6, 2021
1 parent 5a4e57f commit 82dd5b6
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions modules/src/ics04_channel/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
12 changes: 3 additions & 9 deletions modules/src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion relayer/src/foreign_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -524,7 +525,7 @@ impl<DstChain: ChainHandle, SrcChain: ChainHandle> ForeignClient<DstChain, SrcCh
.timestamp();

let refresh_window = client_state.refresh_period();
let elapsed = Timestamp::now().duration_since(&last_update_time);
let elapsed = Timestamp::from_datetime(Utc::now()).duration_since(&last_update_time);

if client_state.is_frozen() || client_state.expired(elapsed.unwrap_or_default()) {
return Err(ForeignClientError::expired_or_frozen(
Expand Down
4 changes: 3 additions & 1 deletion relayer/src/link/relay_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use core::fmt;
use std::thread;
use std::time::Instant;

use chrono::Utc;
use ibc::timestamp::Timestamp;
use itertools::Itertools;
use prost_types::Any;
use tracing::{debug, error, info, trace};
Expand Down Expand Up @@ -1178,7 +1180,7 @@ impl<ChainA: ChainHandle, ChainB: ChainHandle> RelayPath<ChainA, ChainB> {
.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)
Expand Down
4 changes: 3 additions & 1 deletion relayer/src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,7 +102,8 @@ pub fn build_and_send_transfer_messages<Chain: ChainHandle>(
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 {
Expand Down

0 comments on commit 82dd5b6

Please sign in to comment.