GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — id(discovery): bound future announcement timestamps symmetrically (Co… · developing-today/code@d8d58d9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pkgs/id/src
    • discovery.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pkgs/id/src/discovery.rs‎

Copy file name to clipboardExpand all lines: pkgs/id/src/discovery.rs
+52-3Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,13 @@ impl PeerDiscovery {
439439
/// [`STALE_THRESHOLD`] relative to the local wall clock is
440440
/// refused: it neither inserts a peer nor refreshes one, so
441441
/// replaying a captured announcement cannot keep a departed peer
442-
/// live. (A stamp in the future counts as fresh; the ordering
443-
/// check still governs whether it may replace stored state.)
442+
/// live. The bound is symmetric: a stamp more than
443+
/// [`STALE_THRESHOLD`] in the future is also refused, so a forged
444+
/// far-future stamp (e.g. `u64::MAX`) can never enter the table
445+
/// and poison the ordering watermark against every later genuine
446+
/// announcement. Stamps within the bound — past or future —
447+
/// count as fresh; the ordering check then governs whether they
448+
/// may replace stored state.
444449
/// - **Ordering.** An announcement older than the stored one for
445450
/// the same node is dropped: out-of-order delivery never
446451
/// regresses the table. Equal timestamps are a tie, broken
@@ -452,7 +457,9 @@ impl PeerDiscovery {
452457
let now_secs = SystemTime::now()
453458
.duration_since(UNIX_EPOCH)
454459
.map_or(0, |d| d.as_secs());
455-
if now_secs.saturating_sub(announcement.timestamp_secs) > STALE_THRESHOLD.as_secs() {
460+
if now_secs.saturating_sub(announcement.timestamp_secs) > STALE_THRESHOLD.as_secs()
461+
|| announcement.timestamp_secs.saturating_sub(now_secs) > STALE_THRESHOLD.as_secs()
462+
{
456463
return;
457464
}
458465
let node_id = announcement.node_id;
@@ -646,6 +653,48 @@ mod tests {
646653
assert_eq!(peers[0].announcement.blob_count, 3);
647654
}
648655

656+
#[test]
657+
fn test_far_future_announcement_is_refused_and_cannot_poison_ordering() {
658+
let discovery = PeerDiscovery::new();
659+
// A forged stamp at the end of time never enters the table.
660+
discovery.update(make_announcement_at(1, Some("forged"), 99, u64::MAX));
661+
assert_eq!(discovery.count(), 0);
662+
assert!(discovery.peers().is_empty());
663+
664+
// Nor does a far-future stamp overwrite a live peer's stored
665+
// state, and the peer's genuine announcements keep updating it
666+
// afterwards (no ordering-watermark lockout).
667+
let now = now_secs();
668+
discovery.update(make_announcement_at(2, Some("live"), 3, now));
669+
discovery.update(make_announcement_at(
670+
2,
671+
Some("forged"),
672+
99,
673+
now + STALE_THRESHOLD.as_secs() + 1,
674+
));
675+
discovery.update(make_announcement_at(2, Some("live-2"), 4, now));
676+
let peers = discovery.peers();
677+
assert_eq!(peers.len(), 1);
678+
assert_eq!(peers[0].announcement.name, Some("live-2".to_owned()));
679+
assert_eq!(peers[0].announcement.blob_count, 4);
680+
assert_eq!(peers[0].announcement.timestamp_secs, now);
681+
}
682+
683+
#[test]
684+
fn test_near_future_announcement_within_skew_bound_is_accepted() {
685+
let discovery = PeerDiscovery::new();
686+
let now = now_secs();
687+
discovery.update(make_announcement_at(
688+
1,
689+
Some("skewed"),
690+
5,
691+
now + STALE_THRESHOLD.as_secs(),
692+
));
693+
let peers = discovery.peers();
694+
assert_eq!(peers.len(), 1);
695+
assert_eq!(peers[0].announcement.name, Some("skewed".to_owned()));
696+
}
697+
649698
#[test]
650699
fn test_announcement_serialization_roundtrip() {
651700
let announcement = make_announcement(1, Some("test-peer"), 42);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.