From 1defac61f784ead13f26c5759cf47fd544ae29f0 Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Sun, 20 Oct 2024 13:54:44 +0900 Subject: [PATCH] remove tendermint crate from `lcp-types` Signed-off-by: Jun Kimura --- Cargo.lock | 2 +- enclave/Cargo.lock | 2 +- modules/mock-lc/src/client.rs | 7 +- modules/mock-lc/src/errors.rs | 6 +- modules/tendermint-lc/src/client.rs | 32 ++++- modules/tendermint-lc/src/errors.rs | 9 +- modules/types/Cargo.toml | 5 +- modules/types/src/errors.rs | 10 +- modules/types/src/time.rs | 179 +++++++++++++++++++++------- 9 files changed, 190 insertions(+), 62 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c321f8d1..7af4387e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2729,7 +2729,7 @@ dependencies = [ "serde_json", "serde_with", "sgx_types", - "tendermint 0.29.0", + "time", ] [[package]] diff --git a/enclave/Cargo.lock b/enclave/Cargo.lock index 2616bebb..bd0d9834 100644 --- a/enclave/Cargo.lock +++ b/enclave/Cargo.lock @@ -867,7 +867,7 @@ dependencies = [ "serde_json", "serde_with", "sgx_types", - "tendermint", + "time", ] [[package]] diff --git a/modules/mock-lc/src/client.rs b/modules/mock-lc/src/client.rs index 5404944f..37f85a7d 100644 --- a/modules/mock-lc/src/client.rs +++ b/modules/mock-lc/src/client.rs @@ -50,7 +50,10 @@ impl LightClient for MockLightClient { let client_state: ClientState = any_client_state.clone().try_into()?; let consensus_state: ConsensusState = any_consensus_state.try_into()?; let height = client_state.latest_height().into(); - let timestamp: Time = consensus_state.timestamp().into(); + let timestamp: Time = consensus_state + .timestamp() + .try_into() + .map_err(Error::time)?; Ok(CreateClientResult { height, message: UpdateStateProxyMessage { @@ -128,7 +131,7 @@ impl MockLightClient { } let height = header.height().into(); - let header_timestamp = header.timestamp().into(); + let header_timestamp = header.timestamp().try_into().map_err(Error::time)?; let latest_height = client_state.latest_height(); diff --git a/modules/mock-lc/src/errors.rs b/modules/mock-lc/src/errors.rs index a486a351..414ef771 100644 --- a/modules/mock-lc/src/errors.rs +++ b/modules/mock-lc/src/errors.rs @@ -30,7 +30,11 @@ define_error! { Commitment [light_client::commitments::Error] - |_| { "Commitment error" } + |_| { "Commitment error" }, + + Time + [light_client::types::TimeError] + |_| { "Time error" }, } } diff --git a/modules/tendermint-lc/src/client.rs b/modules/tendermint-lc/src/client.rs index c3ad65d7..585c3d80 100644 --- a/modules/tendermint-lc/src/client.rs +++ b/modules/tendermint-lc/src/client.rs @@ -70,7 +70,13 @@ impl LightClient for TendermintLightClient { let canonical_client_state = canonicalize_state(&client_state); let height = client_state.latest_height().into(); - let timestamp: Time = consensus_state.timestamp.into(); + let timestamp = Time::try_from( + consensus_state + .timestamp() + .into_datetime() + .ok_or_else(Error::invalid_timestamp)?, + ) + .map_err(Error::time)?; let state_id = gen_state_id(canonical_client_state, consensus_state)?; Ok(CreateClientResult { @@ -241,7 +247,7 @@ impl TendermintLightClient { } let height = header.height().into(); - let header_timestamp: Time = header.timestamp().into(); + let header_timestamp: Time = header.timestamp().try_into().map_err(Error::time)?; let trusted_consensus_state: ConsensusState = ctx .consensus_state(&client_id, &header.trusted_height.into()) @@ -282,7 +288,10 @@ impl TendermintLightClient { .clone(), ); - let trusted_state_timestamp: Time = trusted_consensus_state.timestamp().into(); + let trusted_state_timestamp: Time = trusted_consensus_state + .timestamp() + .try_into() + .map_err(Error::time)?; let lc_opts = client_state.as_light_client_options().unwrap(); let prev_state_id = @@ -365,13 +374,21 @@ impl TendermintLightClient { context: TrustingPeriodContext::new( lc_opts.trusting_period, lc_opts.clock_drift, - misbehaviour.header1().timestamp().into(), + misbehaviour + .header1() + .timestamp() + .try_into() + .map_err(Error::time)?, trusted_consensus_state_timestamps[0], ) .aggregate(TrustingPeriodContext::new( lc_opts.trusting_period, lc_opts.clock_drift, - misbehaviour.header2().timestamp().into(), + misbehaviour + .header2() + .timestamp() + .try_into() + .map_err(Error::time)?, trusted_consensus_state_timestamps[1], )) .map_err(|e| { @@ -405,7 +422,10 @@ impl TendermintLightClient { }) })? .try_into()?; - let timestamp = consensus_state.timestamp().into(); + let timestamp = consensus_state + .timestamp() + .try_into() + .map_err(Error::time)?; let prev_state_id = gen_state_id(canonicalize_state(client_state), consensus_state)?; prev_states.push(PrevState { height, diff --git a/modules/tendermint-lc/src/errors.rs b/modules/tendermint-lc/src/errors.rs index 1f4d4848..6f14115c 100644 --- a/modules/tendermint-lc/src/errors.rs +++ b/modules/tendermint-lc/src/errors.rs @@ -30,7 +30,14 @@ define_error! { Commitment [light_client::commitments::Error] - |_| { "Commitment error" } + |_| { "Commitment error" }, + + InvalidTimestamp + |_| { "Invalid timestamp" }, + + Time + [light_client::types::TimeError] + |_| { "Time error" }, } } diff --git a/modules/types/Cargo.toml b/modules/types/Cargo.toml index dfdc2201..86f03382 100644 --- a/modules/types/Cargo.toml +++ b/modules/types/Cargo.toml @@ -8,12 +8,12 @@ sgx_types = { rev = "v1.1.6", git = "https://github.com/apache/incubator-teaclav prost = { version = "0.11", default-features = false } ibc = { version = "0.29.0", default-features = false, features = ["serde"], optional = true } lcp-proto = { path = "../../proto", default-features = false } -tendermint = { version = "0.29", default-features = false } serde = { version = "1.0.184", default-features = false, features = ["alloc", "derive"] } serde_with = { version = "2.0.1", default-features = false, features = ["alloc", "macros"] } serde_json = { version = "1.0", default-features = false, features = ["alloc"] } flex-error = { version = "0.4.4", default-features = false } hex = { version = "0.4", default-features = false, features = ["alloc"] } +time = { version = "0.3", default-features = false, features = ["macros", "parsing"] } [dev-dependencies] proptest = "1.2.0" @@ -22,7 +22,8 @@ ibc = { version = "0.29.0", default-features = false, features = ["serde"] } [features] default = ["std"] std = [ - "flex-error/std" + "flex-error/std", + "time/std", ] ibc = [ "dep:ibc" diff --git a/modules/types/src/errors.rs b/modules/types/src/errors.rs index 74cba744..a0157e98 100644 --- a/modules/types/src/errors.rs +++ b/modules/types/src/errors.rs @@ -97,11 +97,15 @@ impl From for TypeError { define_error! { #[derive(Debug, Clone, PartialEq, Eq)] TimeError { - Tendermint - [tendermint::Error] + InvalidDate |_| { - "tendermint error" + "invalid date" }, + DurationOutOfRange + |_| { format_args!("duration value out of range") }, + ComponentRange + [TraceError] + |e| { format_args!("{}", e) }, TryFromIntError [TraceError] |_| {"TryFromIntError"} diff --git a/modules/types/src/time.rs b/modules/types/src/time.rs index fc3cb5bb..ed2b76cb 100644 --- a/modules/types/src/time.rs +++ b/modules/types/src/time.rs @@ -1,67 +1,114 @@ use crate::errors::TimeError; use crate::prelude::*; use core::fmt::Display; -use core::ops::Deref; use core::ops::{Add, Sub}; use core::time::Duration; +use lcp_proto::google::protobuf::Timestamp; +use lcp_proto::protobuf::Protobuf; use serde::{Deserialize, Serialize}; -use tendermint::Time as TmTime; +use time::macros::{datetime, offset}; +use time::{OffsetDateTime, PrimitiveDateTime}; -// NOTE: This value is limited by "tendermint/time" crate -// i.e. 9999-12-31T23:59:59.999999999Z +/// The maximum Unix timestamp in nanoseconds that can be represented in `Time`. +/// i.e., 9999-12-31T23:59:59.999999999Z pub const MAX_UNIX_TIMESTAMP_NANOS: u128 = 253_402_300_799_999_999_999; +/// A newtype wrapper over `PrimitiveDateTime` which serves as the foundational +/// basis for capturing timestamps. It is used directly to keep track of host +/// timestamps. #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] -#[serde(transparent)] -pub struct Time(TmTime); +#[serde(try_from = "Timestamp", into = "Timestamp")] +pub struct Time(PrimitiveDateTime); + +impl Protobuf for Time {} + +impl TryFrom for Time { + type Error = TimeError; + + fn try_from(value: Timestamp) -> Result { + let nanos = value.nanos.try_into()?; + Self::from_unix_timestamp(value.seconds, nanos) + } +} + +impl From