From d101aaf84b4e02b958342e3bd47d82c954c4be3e Mon Sep 17 00:00:00 2001 From: Mikhail Zabaluev Date: Mon, 13 Dec 2021 18:19:10 +0200 Subject: [PATCH] modules: Replaced uses of chrono with time-rs --- .../msgs/transfer.rs | 2 +- .../ics07_tendermint/consensus_state.rs | 11 +- .../src/clients/ics07_tendermint/header.rs | 2 +- .../src/core/ics02_client/client_consensus.rs | 7 +- modules/src/core/ics04_channel/events.rs | 4 +- .../core/ics04_channel/handler/send_packet.rs | 2 +- modules/src/core/ics04_channel/packet.rs | 2 +- modules/src/mock/client_state.rs | 4 +- modules/src/mock/header.rs | 2 +- modules/src/mock/host.rs | 7 +- modules/src/timestamp.rs | 168 +++++++----------- 11 files changed, 87 insertions(+), 124 deletions(-) diff --git a/modules/src/applications/ics20_fungible_token_transfer/msgs/transfer.rs b/modules/src/applications/ics20_fungible_token_transfer/msgs/transfer.rs index 2522376f95..88be8fb54b 100644 --- a/modules/src/applications/ics20_fungible_token_transfer/msgs/transfer.rs +++ b/modules/src/applications/ics20_fungible_token_transfer/msgs/transfer.rs @@ -92,7 +92,7 @@ impl From for RawMsgTransfer { sender: domain_msg.sender.to_string(), receiver: domain_msg.receiver.to_string(), timeout_height: Some(domain_msg.timeout_height.into()), - timeout_timestamp: domain_msg.timeout_timestamp.as_nanoseconds(), + timeout_timestamp: domain_msg.timeout_timestamp.nanoseconds(), } } } diff --git a/modules/src/clients/ics07_tendermint/consensus_state.rs b/modules/src/clients/ics07_tendermint/consensus_state.rs index 3f0f173672..141df337a6 100644 --- a/modules/src/clients/ics07_tendermint/consensus_state.rs +++ b/modules/src/clients/ics07_tendermint/consensus_state.rs @@ -4,6 +4,7 @@ use core::convert::Infallible; use serde::Serialize; use tendermint::{hash::Algorithm, time::Time, Hash}; +use tendermint_proto::google::protobuf as tpb; use tendermint_proto::Protobuf; use ibc_proto::ibc::lightclients::tendermint::v1::ConsensusState as RawConsensusState; @@ -57,9 +58,12 @@ impl TryFrom for ConsensusState { type Error = Error; fn try_from(raw: RawConsensusState) -> Result { - let proto_timestamp = raw + let prost_types::Timestamp { seconds, nanos } = raw .timestamp .ok_or_else(|| Error::invalid_raw_consensus_state("missing timestamp".into()))?; + // FIXME: shunts like this are necessary due to + // https://github.com/informalsystems/tendermint-rs/issues/1053 + let proto_timestamp = tpb::Timestamp { seconds, nanos }; let timestamp = proto_timestamp .try_into() .map_err(|e| Error::invalid_raw_consensus_state(format!("invalid timestamp: {}", e)))?; @@ -81,7 +85,10 @@ impl TryFrom for ConsensusState { impl From for RawConsensusState { fn from(value: ConsensusState) -> Self { - let timestamp: prost_types::Timestamp = value.timestamp.into(); + // FIXME: shunts like this are necessary due to + // https://github.com/informalsystems/tendermint-rs/issues/1053 + let tpb::Timestamp { seconds, nanos } = value.timestamp.into(); + let timestamp = prost_types::Timestamp { seconds, nanos }; RawConsensusState { timestamp: Some(timestamp), diff --git a/modules/src/clients/ics07_tendermint/header.rs b/modules/src/clients/ics07_tendermint/header.rs index 03bc5c9090..8fe32105a5 100644 --- a/modules/src/clients/ics07_tendermint/header.rs +++ b/modules/src/clients/ics07_tendermint/header.rs @@ -77,7 +77,7 @@ impl crate::core::ics02_client::header::Header for Header { } fn timestamp(&self) -> Timestamp { - Timestamp::from_datetime(DateTime::from(self.signed_header.header.time)) + self.signed_header.header.time.into() } fn wrap_any(self) -> AnyHeader { diff --git a/modules/src/core/ics02_client/client_consensus.rs b/modules/src/core/ics02_client/client_consensus.rs index 54c7192db6..de1e54fb80 100644 --- a/modules/src/core/ics02_client/client_consensus.rs +++ b/modules/src/core/ics02_client/client_consensus.rs @@ -3,8 +3,6 @@ use crate::prelude::*; use core::convert::Infallible; use core::marker::{Send, Sync}; -use chrono::{DateTime, Utc}; - use ibc_proto::ibc::core::client::v1::ConsensusStateWithHeight; use prost_types::Any; use serde::Serialize; @@ -55,10 +53,7 @@ pub enum AnyConsensusState { impl AnyConsensusState { pub fn timestamp(&self) -> Timestamp { match self { - Self::Tendermint(cs_state) => { - let date: DateTime = cs_state.timestamp.into(); - Timestamp::from_datetime(date) - } + Self::Tendermint(cs_state) => cs_state.timestamp.into(), #[cfg(any(test, feature = "mocks"))] Self::Mock(mock_state) => mock_state.timestamp(), diff --git a/modules/src/core/ics04_channel/events.rs b/modules/src/core/ics04_channel/events.rs index 972794c8a8..11efb275b6 100644 --- a/modules/src/core/ics04_channel/events.rs +++ b/modules/src/core/ics04_channel/events.rs @@ -294,7 +294,7 @@ impl TryFrom for Vec { key: PKT_TIMEOUT_TIMESTAMP_ATTRIBUTE_KEY.parse().unwrap(), value: p .timeout_timestamp - .as_nanoseconds() + .nanoseconds() .to_string() .parse() .unwrap(), @@ -960,7 +960,7 @@ mod tests { destination_channel: "b_test_channel".parse().unwrap(), data: "test_data".as_bytes().to_vec(), timeout_height: Height::new(1, 10), - timeout_timestamp: Timestamp::from_datetime(chrono::offset::Utc::now()), + timeout_timestamp: Timestamp::now(), }; let mut abci_events = vec![]; let send_packet = SendPacket { diff --git a/modules/src/core/ics04_channel/handler/send_packet.rs b/modules/src/core/ics04_channel/handler/send_packet.rs index dde7d60ede..1131fa6c8b 100644 --- a/modules/src/core/ics04_channel/handler/send_packet.rs +++ b/modules/src/core/ics04_channel/handler/send_packet.rs @@ -145,7 +145,7 @@ mod tests { let timestamp = Timestamp::now().add(Duration::from_secs(10)); //CD:TODO remove unwrap - let mut packet: Packet = get_dummy_raw_packet(1, timestamp.unwrap().as_nanoseconds()) + let mut packet: Packet = get_dummy_raw_packet(1, timestamp.unwrap().nanoseconds()) .try_into() .unwrap(); packet.sequence = 1.into(); diff --git a/modules/src/core/ics04_channel/packet.rs b/modules/src/core/ics04_channel/packet.rs index 12ec013549..6b4cdd1093 100644 --- a/modules/src/core/ics04_channel/packet.rs +++ b/modules/src/core/ics04_channel/packet.rs @@ -238,7 +238,7 @@ impl From for RawPacket { destination_channel: packet.destination_channel.to_string(), data: packet.data, timeout_height: Some(packet.timeout_height.into()), - timeout_timestamp: packet.timeout_timestamp.as_nanoseconds(), + timeout_timestamp: packet.timeout_timestamp.nanoseconds(), } } } diff --git a/modules/src/mock/client_state.rs b/modules/src/mock/client_state.rs index a218255942..d0779a0b77 100644 --- a/modules/src/mock/client_state.rs +++ b/modules/src/mock/client_state.rs @@ -85,7 +85,7 @@ impl From for RawMockClientState { RawMockClientState { header: Some(ibc_proto::ibc::mock::Header { height: Some(value.header.height().into()), - timestamp: value.header.timestamp.as_nanoseconds(), + timestamp: value.header.timestamp.nanoseconds(), }), } } @@ -158,7 +158,7 @@ impl From for RawMockConsensusState { RawMockConsensusState { header: Some(ibc_proto::ibc::mock::Header { height: Some(value.header.height().into()), - timestamp: value.header.timestamp.as_nanoseconds(), + timestamp: value.header.timestamp.nanoseconds(), }), } } diff --git a/modules/src/mock/header.rs b/modules/src/mock/header.rs index 1678a62407..381975ae48 100644 --- a/modules/src/mock/header.rs +++ b/modules/src/mock/header.rs @@ -37,7 +37,7 @@ impl From for RawMockHeader { fn from(value: MockHeader) -> Self { RawMockHeader { height: Some(value.height.into()), - timestamp: value.timestamp.as_nanoseconds(), + timestamp: value.timestamp.nanoseconds(), } } } diff --git a/modules/src/mock/host.rs b/modules/src/mock/host.rs index ec65ad7171..71cdd6d39b 100644 --- a/modules/src/mock/host.rs +++ b/modules/src/mock/host.rs @@ -3,6 +3,7 @@ use tendermint::time::Time; use tendermint_testgen::light_block::TmLightBlock; use tendermint_testgen::{Generator, LightBlock as TestgenLightBlock}; +use time::OffsetDateTime; use crate::clients::ics07_tendermint::consensus_state::ConsensusState as TMConsensusState; use crate::clients::ics07_tendermint::header::Header as TMHeader; @@ -63,10 +64,8 @@ impl HostBlock { // same timestamp as two block can be generated per second. let ten_millis = core::time::Duration::from_millis(1000); std::thread::sleep(ten_millis); - let time = Time(chrono::Utc::now()) - .duration_since(Time::unix_epoch()) - .unwrap() - .as_secs(); + let time: Time = OffsetDateTime::now_utc().try_into().unwrap(); + let time = time.duration_since(Time::unix_epoch()).unwrap().as_secs(); TestgenLightBlock::new_default_with_time_and_chain_id(chain_id.to_string(), time, height) .generate() diff --git a/modules/src/timestamp.rs b/modules/src/timestamp.rs index 500ea37377..267e7bf824 100644 --- a/modules/src/timestamp.rs +++ b/modules/src/timestamp.rs @@ -1,28 +1,40 @@ use crate::prelude::*; use core::fmt::Display; +use core::hash::{Hash, Hasher}; use core::num::ParseIntError; use core::ops::{Add, Sub}; use core::str::FromStr; use core::time::Duration; -use chrono::{offset::Utc, DateTime, TimeZone}; use flex_error::{define_error, TraceError}; use serde_derive::{Deserialize, Serialize}; use tendermint::Time; +use time::OffsetDateTime; pub const ZERO_DURATION: Duration = Duration::from_secs(0); -/// A newtype wrapper over `Option>` to keep track of +const NANOS_PER_SEC: u64 = 1_000_000_000; + +/// A newtype wrapper over `Option