Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roll back time-rs dependency and remove methods that require version 0.3.5 #1048

Merged
merged 2 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .changelog/unreleased/improvements/1030-new-time-api.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
- Remove dependencies on the `chrono` crate.
([#1030](https://github.com/informalsystems/tendermint-rs/issues/1030))
- `[tendermint]` Improve `tendermint::Time`
([#1030](https://github.com/informalsystems/tendermint-rs/issues/1030)):
([#1036](https://github.com/informalsystems/tendermint-rs/issues/1036),
revised by [#1048](https://github.com/informalsystems/tendermint-rs/pull/1048)):
* Restrict the validity range of `Time` to dates with years in the range
1-9999, to match the specification of protobuf message `Timestamp`.
Add an `ErrorDetail` variant `DateOutOfRange` to report when this
restriction is not met.
* Added a conversion to, and a fallible conversion from,
`OffsetDateTime` of the `time` crate.
* Added `Time` methods `checked_add` and `checked_sub`.
2 changes: 1 addition & 1 deletion light-client/src/predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ mod tests {
assert!(result_ok.is_ok());

// 2. ensure it fails if header is from a future time
let now = now.checked_sub(one_second * 15).unwrap();
let now = (now - one_second * 15).unwrap();
let result_err = vp.is_header_from_past(header.time, one_second, now);

match result_err {
Expand Down
4 changes: 2 additions & 2 deletions pbt-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ description = """
default = ["time"]

[dependencies]
time = { version = "0.3.5", default-features = false, optional = true }
time = { version = "0.3", default-features = false, optional = true }
proptest = { version = "0.10.1", default-features = false, features = ["std"] }

[dev-dependencies]
time = { version = "0.3.5", features = ["macros"] }
time = { version = "0.3", features = ["macros"] }
45 changes: 2 additions & 43 deletions tendermint/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,6 @@ impl Time {
pub fn to_rfc3339(&self) -> String {
timestamp::to_rfc3339_nanos(self.0.assume_utc())
}

/// Computes `self + duration`, returning `None` if an overflow occurred.
pub fn checked_add(self, duration: Duration) -> Option<Self> {
let duration = duration.try_into().ok()?;
let t = self.0.checked_add(duration)?;
Self::from_utc(t.assume_utc()).ok()
}

/// Computes `self - duration`, returning `None` if an overflow occurred.
pub fn checked_sub(self, duration: Duration) -> Option<Self> {
let duration = duration.try_into().ok()?;
let t = self.0.checked_sub(duration)?;
Self::from_utc(t.assume_utc()).ok()
}
}

impl fmt::Display for Time {
Expand Down Expand Up @@ -306,6 +292,7 @@ mod tests {
}
}

#[allow(dead_code)]
fn duration_from_nanos(whole_nanos: u128) -> Duration {
let secs: u64 = (whole_nanos / 1_000_000_000).try_into().unwrap();
let nanos = (whole_nanos % 1_000_000_000) as u32;
Expand Down Expand Up @@ -368,33 +355,5 @@ mod tests {
}
}

proptest! {
#[test]
fn checked_add_regular((dt, d) in args_for_regular_add()) {
let t: Time = dt.try_into().unwrap();
let t = t.checked_add(d).unwrap();
let res: OffsetDateTime = t.into();
assert_eq!(res, dt + d);
}

#[test]
fn checked_sub_regular((dt, d) in args_for_regular_sub()) {
let t: Time = dt.try_into().unwrap();
let t = t.checked_sub(d).unwrap();
let res: OffsetDateTime = t.into();
assert_eq!(res, dt - d);
}

#[test]
fn checked_add_overflow((dt, d) in args_for_overflowed_add()) {
let t: Time = dt.try_into().unwrap();
assert_eq!(t.checked_add(d), None);
}

#[test]
fn checked_sub_overflow((dt, d) in args_for_overflowed_sub()) {
let t: Time = dt.try_into().unwrap();
assert_eq!(t.checked_sub(d), None);
}
}
// TODO: add tests for additive ops using the strategies above
}