Skip to content

Commit

Permalink
Roll back time-rs dependency and remove methods that require version …
Browse files Browse the repository at this point in the history
…0.3.5 (#1048)

* Relax dependency on time 0.3

Do not require time 0.3.5, because ibc-rs has another dependency that
pins time at =0.3.2.

This means the recently added methods Time::checked_add and
Time::checked_sub have to be removed. Fortunately, they haven't made it
into a 0.23.x release yet.

* Updated changelog for #1048
  • Loading branch information
mzabaluev committed Dec 8, 2021
1 parent 49d3783 commit dbdb71c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 48 deletions.
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
}

0 comments on commit dbdb71c

Please sign in to comment.