Skip to content

Commit

Permalink
modules: Replaced uses of chrono with time-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
mzabaluev committed Dec 13, 2021
1 parent 2b7bf02 commit d101aaf
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl From<MsgTransfer> 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(),
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions modules/src/clients/ics07_tendermint/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,9 +58,12 @@ impl TryFrom<RawConsensusState> for ConsensusState {
type Error = Error;

fn try_from(raw: RawConsensusState) -> Result<Self, Self::Error> {
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)))?;
Expand All @@ -81,7 +85,10 @@ impl TryFrom<RawConsensusState> for ConsensusState {

impl From<ConsensusState> 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),
Expand Down
2 changes: 1 addition & 1 deletion modules/src/clients/ics07_tendermint/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions modules/src/core/ics02_client/client_consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,10 +53,7 @@ pub enum AnyConsensusState {
impl AnyConsensusState {
pub fn timestamp(&self) -> Timestamp {
match self {
Self::Tendermint(cs_state) => {
let date: DateTime<Utc> = 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(),
Expand Down
4 changes: 2 additions & 2 deletions modules/src/core/ics04_channel/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl TryFrom<Packet> for Vec<Tag> {
key: PKT_TIMEOUT_TIMESTAMP_ATTRIBUTE_KEY.parse().unwrap(),
value: p
.timeout_timestamp
.as_nanoseconds()
.nanoseconds()
.to_string()
.parse()
.unwrap(),
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion modules/src/core/ics04_channel/handler/send_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion modules/src/core/ics04_channel/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl From<Packet> 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(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions modules/src/mock/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl From<MockClientState> 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(),
}),
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ impl From<MockConsensusState> 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(),
}),
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/src/mock/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl From<MockHeader> for RawMockHeader {
fn from(value: MockHeader) -> Self {
RawMockHeader {
height: Some(value.height.into()),
timestamp: value.timestamp.as_nanoseconds(),
timestamp: value.timestamp.nanoseconds(),
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions modules/src/mock/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit d101aaf

Please sign in to comment.