Skip to content

Commit

Permalink
Simplify time maths in MetaAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jun 16, 2021
1 parent 0c50bc2 commit 239fb70
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
37 changes: 29 additions & 8 deletions zebra-chain/src/serialization/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,41 @@ impl DateTime32 {
.expect("unexpected out of range chrono::DateTime")
}

/// If `earlier` is less than or equal to `self`, returns the number of
/// seconds elapsed between `earlier` and `self`.
///
/// Otherwise, returns `None`.
/// Returns the number of seconds elapsed between `earlier` and this time,
/// or `None` if `earlier` is later than this time.
pub fn checked_duration_since(&self, earlier: DateTime32) -> Option<u32> {
self.timestamp.checked_sub(earlier.timestamp)
}

/// Returns the number of seconds elapsed since this time.
///
/// If this time is in the future, returns `None`.
pub fn elapsed(&self) -> Option<u32> {
/// Returns the number of seconds elapsed between `earlier` and this time,
/// or zero if `earlier` is later than this time.
pub fn saturating_duration_since(&self, earlier: DateTime32) -> u32 {
self.timestamp.saturating_sub(earlier.timestamp)
}

/// Returns the number of seconds elapsed since this time,
/// or if this time is in the future, returns `None`.
pub fn checked_elapsed(&self) -> Option<u32> {
DateTime32::now().checked_duration_since(*self)
}

/// Returns the number of seconds elapsed since this time,
/// or if this time is in the future, returns zero.
pub fn saturating_elapsed(&self) -> u32 {
DateTime32::now().saturating_duration_since(*self)
}

pub

/// The earliest possible `DateTime32` value.
pub const MIN: DateTime32 = DateTime32 {
timestamp: u32::MIN,
};

/// The latest possible `DateTime32` value.
pub const MAX: DateTime32 = DateTime32 {
timestamp: u32::MAX,
};
}

impl fmt::Debug for DateTime32 {
Expand Down
51 changes: 23 additions & 28 deletions zebra-network/src/meta_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,22 +410,14 @@ impl MetaAddr {
/// disconnected from it. Otherwise, we could potentially be connected to it.
pub fn was_recently_live(&self) -> bool {
if let Some(last_response) = self.last_response {
if let Some(elapsed) = last_response.elapsed() {
elapsed
<= constants::LIVE_PEER_DURATION
.as_secs()
.try_into()
.expect("unexpectedly large constant")
} else {
info!(last_response = ?self.last_response,
now = ?DateTime32::now(),
"unexpected future response time: assuming peer is live. Hint: has the system clock changed?"
);
// future times should be considered live
true
}
// Recent times and future times are considered live
last_response.saturating_elapsed()
<= constants::LIVE_PEER_DURATION
.as_secs()
.try_into()
.expect("unexpectedly large constant")
} else {
// if there is no response, it can't possibly be live
// If there has never been any response, it can't possibly be live
false
}
}
Expand All @@ -435,25 +427,28 @@ impl MetaAddr {
/// Returns `true` if this peer was recently attempted, or has a connection
/// attempt in progress.
pub fn was_recently_attempted(&self) -> bool {
self.last_attempt
// `now` should always be later than `last_attempt`, except for tests
// so we can call future attempts "not recent" to simplify the code
.map(|last_attempt| Instant::now().checked_duration_since(last_attempt))
.flatten()
.map(|since_last_attempt| since_last_attempt <= constants::LIVE_PEER_DURATION)
.unwrap_or(false)
if let Some(last_attempt) = self.last_attempt {
// Recent times and future times are considered live.
// Instants are monotonic, so `now` should always be later than `last_attempt`,
// except for synthetic data in tests.
Instant::now().saturating_duration_since(last_attempt) <= constants::LIVE_PEER_DURATION
} else {
// If there has never been any attempt, it can't possibly be live
false
}
}

/// Have we recently had a failed connection to this peer?
///
/// Returns `true` if this peer has recently failed.
pub fn was_recently_failed(&self) -> bool {
self.last_failure
// `now` should always be later than `last_failure`, except for tests
.map(|last_failure| Instant::now().checked_duration_since(last_failure))
.flatten()
.map(|since_last_failure| since_last_failure <= constants::LIVE_PEER_DURATION)
.unwrap_or(false)
if let Some(last_failure) = self.last_failure {
// Recent times and future times are considered live
Instant::now().saturating_duration_since(last_failure) <= constants::LIVE_PEER_DURATION
} else {
// If there has never been any failure, it can't possibly be recent
false
}
}

/// Is this address ready for a new outbound connection attempt?
Expand Down

0 comments on commit 239fb70

Please sign in to comment.