Skip to content

Commit

Permalink
Refactor sanitize to use the new DateTime32/Duration32 methods
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jun 18, 2021
1 parent 068aaa8 commit 03c5fb0
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions zebra-network/src/meta_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,16 +487,19 @@ impl MetaAddr {
}

// Sanitize time
let interval = crate::constants::TIMESTAMP_TRUNCATION_SECONDS;
let ts = self.last_seen()?.timestamp();
// This can't underflow, because `0 <= rem_euclid < ts`
let last_seen = ts - ts.rem_euclid(interval);
let last_seen = DateTime32::from(last_seen);
let last_seen = self.last_seen()?;
let remainder = last_seen
.timestamp()
.rem_euclid(crate::constants::TIMESTAMP_TRUNCATION_SECONDS);
let last_seen = last_seen
.checked_sub(remainder.into())
.expect("unexpected underflow: rem_euclid is strictly less than timestamp");

Some(MetaAddr {
addr: self.addr,
// deserialization also sanitizes services to known flags
services: self.services & PeerServices::all(),
// TODO: split untrusted and direct services
// sanitize untrusted services to NODE_NETWORK only? (#2234)
services: self.services,
// only put the last seen time in the untrusted field,
// this matches deserialization, and avoids leaking internal state
untrusted_last_seen: Some(last_seen),
Expand Down

0 comments on commit 03c5fb0

Please sign in to comment.