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

fix: Replace use of Duration for timestamps with an actual timestamp, e.g. chrono::DateTime #4762

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/hyperledger/iroha/tree/iroha2-lts"
documentation = "https://hyperledger.github.io/iroha-2-docs"
homepage = "https://iroha.tech"

chrono = "0.4.38"
license = "Apache-2.0"
keywords = ["blockchain", "crypto", "iroha", "ledger"]
categories = ["cryptography::cryptocurrencies"]
Expand Down
106 changes: 50 additions & 56 deletions data_model/src/events/time.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Time event and filter
use core::{ops::Range, time::Duration};

use chrono::{DateTime, Utc, TimeZone};
use derive_more::Constructor;
use getset::Getters;
use iroha_data_model_derive::model;
Expand Down Expand Up @@ -99,7 +99,7 @@ mod model {
)]
pub struct Schedule {
/// The first execution time
pub start: Duration,
pub start_ms: u64,
/// If some, the period between cyclic executions
pub period: Option<Duration>,
}
Expand All @@ -125,7 +125,7 @@ mod model {
#[ffi_type]
pub struct TimeInterval {
/// The start of a time interval
pub since: Duration,
pub since_ms: u64,
/// The length of a time interval
pub length: Duration,
}
Expand All @@ -145,7 +145,7 @@ impl EventFilter for TimeEventFilter {
ExecutionTime::PreCommit => 1,
ExecutionTime::Schedule(schedule) => {
// Prevent matching in the future it will be handled by the next block
if schedule.start > event.interval.since {
if schedule.start_ms > event.interval.since_ms {
return 0;
}

Expand Down Expand Up @@ -175,13 +175,13 @@ impl EventFilter for TimeEventFilter {
//
// Schedule start is after current block (c1).
// In this case event won't match and it will be handled in the next block.
let since = if Range::from(prev).contains(&schedule.start) {
schedule.start
let since_ms = if Range::from(prev).contains(&schedule.start_ms) {
schedule.start_ms
} else {
prev.since + prev.length
prev.since_ms + prev.length
};
let estimation = event.interval.since + event.interval.length;
let length = estimation - since;
let estimation = event.interval.since_ms + event.interval.length;
let length = estimation - since_ms;

TimeInterval { since, length }
});
Expand All @@ -203,15 +203,15 @@ impl EventFilter for TimeEventFilter {
#[cfg(feature = "transparent_api")]
fn count_matches_in_interval(schedule: &Schedule, interval: &TimeInterval) -> u32 {
schedule.period.map_or_else(
|| u32::from(Range::from(*interval).contains(&schedule.start)),
|| u32::from(Range::from(*interval).contains(&schedule.start_ms)),
|period| {
#[allow(clippy::integer_division)]
let k = interval.since.saturating_sub(schedule.start).as_millis() / period.as_millis();
let start = schedule.start + multiply_duration_by_u128(period, k);
let k = interval.since_ms.saturating_sub(schedule.start_ms).as_millis() / period.as_millis();
let start_ms = schedule.start_ms + multiply_duration_by_u128(period, k);
let range = Range::from(*interval);
(0..)
.map(|i| start + period * i)
.skip_while(|time| *time < interval.since)
.map(|i| start_ms + period * i)
.skip_while(|time| *time < interval.since_ms)
.take_while(|time| range.contains(time))
.count()
.try_into()
Expand Down Expand Up @@ -245,41 +245,35 @@ fn multiply_duration_by_u128(duration: Duration, n: u128) -> Duration {
}

impl Schedule {
/// Create new [`Schedule`] starting at `start` and without period
/// Create new [`Schedule`] starting at `start_ms` and without period
#[must_use]
#[inline]
pub const fn starting_at(start: Duration) -> Self {
pub const fn starting_at(start_ms: Duration) -> Self {
Self {
start,
start_ms,
period: None,
}
}

/// Add `period` to `self`
#[must_use]
#[inline]
pub const fn with_period(mut self, period: Duration) -> Self {
self.period = Some(period);
self
/// Getter for `start_ms` returning `chrono::DateTime`
pub fn start(&self) -> DateTime<Utc> {
Utc.timestamp_millis(self.start_ms.as_millis() as i64)
}
}

impl TimeInterval {
/// Getter for `since`
pub fn since(&self) -> &Duration {
&self.since
/// Getter for `since_ms` returning `chrono::DateTime`
pub fn since(&self) -> DateTime<Utc> {
Utc.timestamp_millis(self.since_ms.as_millis() as i64)
}

/// Getter for `length`
pub fn length(&self) -> &Duration {
&self.length
/// Getter for `since_ms`
pub fn since(&self) -> &Duration {
&self.since_ms
}
}

impl From<TimeInterval> for Range<Duration> {
#[inline]
fn from(interval: TimeInterval) -> Self {
interval.since..interval.since + interval.length
interval.since..interval.since_ms + interval.length
}
}

Expand Down Expand Up @@ -308,7 +302,7 @@ mod tests {
// p i1 i2

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP - 5));
let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(10);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 0);
Expand All @@ -321,7 +315,7 @@ mod tests {
// p, i1 i2

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP));
let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(10);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 1);
Expand All @@ -333,7 +327,7 @@ mod tests {
// i1 p i2

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP + 5));
let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(10);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 1);
Expand All @@ -346,7 +340,7 @@ mod tests {
// i1 i2, p

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP + 10));
let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(10);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 0);
Expand All @@ -359,7 +353,7 @@ mod tests {

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP + 5))
.with_period(Duration::from_secs(30));
let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(10);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 1);
Expand All @@ -372,7 +366,7 @@ mod tests {

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP))
.with_period(Duration::from_secs(10));
let since = Duration::from_secs(TIMESTAMP + 35);
let since_ms = Duration::from_secs(TIMESTAMP + 35);
let length = Duration::from_secs(4);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 0);
Expand All @@ -385,7 +379,7 @@ mod tests {

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP))
.with_period(Duration::from_secs(6));
let since = Duration::from_secs(TIMESTAMP - 10);
let since_ms = Duration::from_secs(TIMESTAMP - 10);
let length = Duration::from_secs(4);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 0);
Expand All @@ -398,7 +392,7 @@ mod tests {

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP))
.with_period(Duration::from_secs(6));
let since = Duration::from_secs(TIMESTAMP - 10);
let since_ms = Duration::from_secs(TIMESTAMP - 10);
let length = Duration::from_secs(30);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 4);
Expand All @@ -411,7 +405,7 @@ mod tests {

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP))
.with_period(Duration::from_millis(600));
let since = Duration::from_secs(TIMESTAMP + 3) + Duration::from_millis(500);
let since_ms = Duration::from_secs(TIMESTAMP + 3) + Duration::from_millis(500);
let length = Duration::from_secs(2);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 4);
Expand All @@ -425,7 +419,7 @@ mod tests {

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP - 10))
.with_period(Duration::from_secs(10));
let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(5);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 1);
Expand All @@ -439,7 +433,7 @@ mod tests {

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP))
.with_period(Duration::from_secs(5));
let since = Duration::from_secs(TIMESTAMP - 10);
let since_ms = Duration::from_secs(TIMESTAMP - 10);
let length = Duration::from_secs(15);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 1);
Expand All @@ -453,7 +447,7 @@ mod tests {

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP - 10))
.with_period(Duration::from_secs(15));
let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(5);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 0);
Expand All @@ -467,7 +461,7 @@ mod tests {

let schedule = Schedule::starting_at(Duration::from_secs(TIMESTAMP))
.with_period(Duration::from_secs(1));
let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(7);
let interval = TimeInterval { since, length };
assert_eq!(count_matches_in_interval(&schedule, &interval), 7);
Expand All @@ -488,11 +482,11 @@ mod tests {
.with_period(Duration::from_secs(10));
let filter = TimeEventFilter(ExecutionTime::Schedule(schedule));

let since = Duration::from_secs(TIMESTAMP + 5);
let since_ms = Duration::from_secs(TIMESTAMP + 5);
let length = Duration::from_secs(10);
let prev_interval = TimeInterval { since, length };

let since = Duration::from_secs(TIMESTAMP + 25);
let since_ms = Duration::from_secs(TIMESTAMP + 25);
let length = Duration::from_secs(10);
let interval = TimeInterval { since, length };

Expand All @@ -514,11 +508,11 @@ mod tests {
.with_period(Duration::from_secs(10));
let filter = TimeEventFilter(ExecutionTime::Schedule(schedule));

let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(10);
let prev_interval = TimeInterval { since, length };

let since = Duration::from_secs(TIMESTAMP + 20);
let since_ms = Duration::from_secs(TIMESTAMP + 20);
let length = Duration::from_secs(10);
let interval = TimeInterval { since, length };

Expand All @@ -540,11 +534,11 @@ mod tests {
.with_period(Duration::from_secs(10));
let filter = TimeEventFilter(ExecutionTime::Schedule(schedule));

let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(10);
let prev_interval = TimeInterval { since, length };

let since = Duration::from_secs(TIMESTAMP + 20);
let since_ms = Duration::from_secs(TIMESTAMP + 20);
let length = Duration::from_secs(10);
let interval = TimeInterval { since, length };

Expand All @@ -566,11 +560,11 @@ mod tests {
.with_period(Duration::from_secs(10));
let filter = TimeEventFilter(ExecutionTime::Schedule(schedule));

let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(10);
let prev_interval = TimeInterval { since, length };

let since = Duration::from_secs(TIMESTAMP + 20);
let since_ms = Duration::from_secs(TIMESTAMP + 20);
let length = Duration::from_secs(10);
let interval = TimeInterval { since, length };

Expand All @@ -592,11 +586,11 @@ mod tests {
.with_period(Duration::from_secs(10));
let filter = TimeEventFilter(ExecutionTime::Schedule(schedule));

let since = Duration::from_secs(TIMESTAMP);
let since_ms = Duration::from_secs(TIMESTAMP);
let length = Duration::from_secs(10);
let prev_interval = TimeInterval { since, length };

let since = Duration::from_secs(TIMESTAMP + 20);
let since_ms = Duration::from_secs(TIMESTAMP + 20);
let length = Duration::from_secs(10);
let interval = TimeInterval { since, length };

Expand Down