Skip to content

Commit

Permalink
fix(comms/peer_manager): fix possible panic in offline calc (#4877)
Browse files Browse the repository at this point in the history
Description
---
- fixes possible panic in `Peer::offline_since` 
- offline_at has second resolution

Motivation and Context
---
Possible panic when converting chrono Duration to std Duration
second resolution is good enough for offline_since

How Has This Been Tested?
---
Updated unit test for this case
  • Loading branch information
sdbondi authored Nov 3, 2022
1 parent a580103 commit c0d1f58
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion comms/core/src/peer_manager/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Peer {
pub fn offline_since(&self) -> Option<Duration> {
self.offline_at
.map(|offline_at| Utc::now().naive_utc() - offline_at)
.map(|since| Duration::from_millis(u64::try_from(since.num_milliseconds()).unwrap()))
.map(|since| Duration::from_secs(u64::try_from(since.num_seconds()).unwrap_or(0)))
}

pub(super) fn set_id(&mut self, id: PeerId) {
Expand Down Expand Up @@ -436,6 +436,8 @@ mod test {
assert!(peer.offline_since().is_none());
peer.set_offline(true);
assert!(peer.offline_since().is_some());
peer.offline_at = Some(Utc::now().naive_utc() + chrono::Duration::seconds(10));
assert_eq!(peer.offline_since().unwrap(), Duration::from_secs(0));
}

#[test]
Expand Down

0 comments on commit c0d1f58

Please sign in to comment.