From ce33cd53dd87703fb161c513694d5e45381c2ab4 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 28 Jun 2023 16:31:34 +0200 Subject: [PATCH 1/5] build: upgrade to tenderdash v0.13.0-dev.1 --- abci/Cargo.toml | 4 ++-- proto/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/abci/Cargo.toml b/abci/Cargo.toml index f970ae2..dcdd5e8 100644 --- a/abci/Cargo.toml +++ b/abci/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tenderdash-abci" -version = "0.12.0" +version = "0.13.0-dev.1" edition = "2021" license = "Apache-2.0" readme = "README.md" @@ -29,7 +29,7 @@ name = "echo_socket" required-features = ["server"] [dependencies] -tenderdash-proto = { version = "0.12.0", path = "../proto" } +tenderdash-proto = { version = "0.13.0-dev.1", path = "../proto" } bytes = { version = "1.0" } prost = { version = "0.11" } tracing = { version = "0.1", default-features = false } diff --git a/proto/Cargo.toml b/proto/Cargo.toml index eeb9179..7a8a5c0 100644 --- a/proto/Cargo.toml +++ b/proto/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tenderdash-proto" -version = "0.12.0" +version = "0.13.0-dev.1" edition = "2021" license = "Apache-2.0" repository = "https://github.com/dashpay/rs-tenderdash-abci/tree/main/proto" From 46690628578a94f1c25671d1064dd71a5b29ffac Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 28 Jun 2023 16:42:45 +0200 Subject: [PATCH 2/5] chore: formatting improvement to restart GHA --- abci/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/abci/Cargo.toml b/abci/Cargo.toml index dcdd5e8..ddf5bdf 100644 --- a/abci/Cargo.toml +++ b/abci/Cargo.toml @@ -37,7 +37,7 @@ tracing-subscriber = { version = "0.3", optional = true, default-features = fals "ansi", "env-filter", ] } -thiserror = "1.0.39" +thiserror = { version = "1.0.39" } url = { version = "2.3.1" } semver = { version = "1.0.17" } lhash = { version = "1.0.1", features = ["sha256"], optional = true } From 19ef2e1a290b0a4f66649f39acfb78c649aadd7e Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Jun 2023 16:51:02 +0200 Subject: [PATCH 3/5] feat(proto): Timestamp::from_milis() --- proto/src/serializers/timestamp.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/proto/src/serializers/timestamp.rs b/proto/src/serializers/timestamp.rs index 84f1d37..50f2dd9 100644 --- a/proto/src/serializers/timestamp.rs +++ b/proto/src/serializers/timestamp.rs @@ -41,6 +41,28 @@ impl ToMilis for Timestamp { } } +pub trait FromMilis { + /// Create protobuf timestamp from miliseconds since epoch + /// + /// Note there is a resolution difference, as timestamp uses nanoseconds + fn from_milis(millis: i64) -> Self; +} + +impl FromMilis for Timestamp { + /// Create protobuf timestamp from miliseconds since epoch + /// + /// Note there is a resolution difference, as timestamp uses nanoseconds + fn from_milis(millis: i64) -> Self { + let dt = + chrono::NaiveDateTime::from_timestamp_millis(millis).expect("cannot parse timestamp"); + + Self { + nanos: dt.timestamp_subsec_nanos() as i32, + seconds: dt.timestamp(), + } + } +} + /// Deserialize string into Timestamp pub fn deserialize<'de, D>(deserializer: D) -> Result where From 566c427dd7433d6f6c239f2069d199a50ba36c1a Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Tue, 27 Jun 2023 09:31:08 +0200 Subject: [PATCH 4/5] chore(proto): panic when timestamp millis out of range + tests --- proto/src/serializers/timestamp.rs | 65 +++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/proto/src/serializers/timestamp.rs b/proto/src/serializers/timestamp.rs index 50f2dd9..fc0a5f3 100644 --- a/proto/src/serializers/timestamp.rs +++ b/proto/src/serializers/timestamp.rs @@ -1,6 +1,6 @@ //! Serialize/deserialize Timestamp type from and into string: -use core::fmt; +use core::fmt::{self, Debug}; use serde::{de::Error as _, ser::Error, Deserialize, Deserializer, Serialize, Serializer}; use time::{ @@ -29,6 +29,16 @@ impl From for Timestamp { pub trait ToMilis { /// Convert protobuf timestamp into miliseconds since epoch + + /// Note there is a resolution difference, as timestamp uses nanoseconds + /// + /// # Arguments + /// + /// * millis - time since epoch, in miliseconds + /// + /// # Panics + /// + /// Panics when timestamp doesn't fit `u64` type fn to_milis(&self) -> u64; } @@ -37,7 +47,9 @@ impl ToMilis for Timestamp { fn to_milis(&self) -> u64 { chrono::NaiveDateTime::from_timestamp_opt(self.seconds, self.nanos as u32) .unwrap() - .timestamp_millis() as u64 + .timestamp_millis() + .try_into() + .expect("timestamp value out of u64 range") } } @@ -45,16 +57,28 @@ pub trait FromMilis { /// Create protobuf timestamp from miliseconds since epoch /// /// Note there is a resolution difference, as timestamp uses nanoseconds - fn from_milis(millis: i64) -> Self; + /// + /// # Arguments + /// + /// * millis - time since epoch, in miliseconds; must fit `i64` type + fn from_milis(millis: u64) -> Self; } impl FromMilis for Timestamp { /// Create protobuf timestamp from miliseconds since epoch /// /// Note there is a resolution difference, as timestamp uses nanoseconds - fn from_milis(millis: i64) -> Self { - let dt = - chrono::NaiveDateTime::from_timestamp_millis(millis).expect("cannot parse timestamp"); + /// + /// # Panics + /// + /// Panics when `millis` don't fit `i64` type + fn from_milis(millis: u64) -> Self { + let dt = chrono::NaiveDateTime::from_timestamp_millis( + millis + .try_into() + .expect("milliseconds timestamp out of i64 range"), + ) + .expect("cannot parse timestamp"); Self { nanos: dt.timestamp_subsec_nanos() as i32, @@ -229,4 +253,33 @@ mod test { assert_eq!(json, serde_json::to_string(&rfc).unwrap()); } } + + #[test] + fn timestamp_from_to() { + let time_ms = 1687848809533; + + let from = Timestamp::from_milis(time_ms); + let to = from.to_milis(); + + assert_eq!(to, time_ms); + } + + #[test] + #[should_panic] + fn timestamp_millis_out_of_range() { + let time_ms = u64::MAX - 1; + + let from = Timestamp::from_milis(time_ms); + } + + #[test] + #[should_panic] + fn timestamp_negative() { + let ts = Timestamp { + nanos: 1000, + seconds: -12, + }; + + let to = ts.to_milis(); + } } From ea478a4037935944d7cdb561f0a7dd75097d9384 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Thu, 20 Jul 2023 16:57:21 +0200 Subject: [PATCH 5/5] chore: unify cargo.toml dependency code style --- abci/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/abci/Cargo.toml b/abci/Cargo.toml index 7720a9c..b6f5390 100644 --- a/abci/Cargo.toml +++ b/abci/Cargo.toml @@ -56,9 +56,9 @@ tokio = { version = "1.28", features = [ futures = { version = "0.3.28", optional = true } [dev-dependencies] -anyhow = "1.0.69" -bincode = "2.0.0-rc.2" -blake2 = "0.10.6" +anyhow = { version = "1.0.69" } +bincode = { version = "2.0.0-rc.2" } +blake2 = { version = "0.10.6" } bollard = { version = "0.14.0" } futures = { version = "0.3.26" } tokio = { version = "1", features = ["macros", "signal", "time", "io-std"] }