dig-pex 0.2.0

Peer Exchange (PEX) for the DIG Node peer network — delta-based first-hand known-peer exchange over already-authenticated links (node<->node dig-nat mux, relay->node RLY-008 WebSocket), in the spirit of BitTorrent PEX: periodic added/dropped deltas, hard per-message caps, receiver-enforced minimum interval anti-flood floor, and no third-party re-flooding. Ships a transport-agnostic sans-IO PexEngine embedded by dig-node and dig-relay.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! The **peer entry** — the unit of exchange (SPEC §3). It is the L7 `PeerRecord` shape extended
//! with a `flags` list, carrying a `peer_id`, candidate `addresses`, the `network_id`, a `last_seen`
//! Unix-seconds timestamp, a `via` provenance, and per-peer capability `flags`.
//!
//! ## Byte-compatibility
//!
//! [`Address`] (`{ host, port, kind }`) is byte-compatible with the L7 `dig.getPeers` addresses and
//! the dig-nat / dig-gossip / dig-dht `Contact` address shape — same JSON field names and the same
//! `kind` tokens (`direct` | `mapped` | `reflexive` | `relay`). It is mirrored here (rather than
//! importing those crates) to keep the PEX dependency surface minimal; the wire form MUST stay
//! identical so a returned entry drops straight into a dial target.
//!
//! ## Tolerant decode, strict validation
//!
//! Inbound entries decode **tolerantly**: an unrecognized `kind` or `via` token deserializes to the
//! [`AddressKind::Unknown`] / [`Provenance::Unknown`] catch-all rather than failing the whole
//! message, and every field has a default. This is what makes a malformed entry *skipped, not fatal*
//! (SPEC §3.3, §7.3): [`PeerEntry::validate`] decides keep-or-skip; a broken token never aborts the
//! sibling entries around it.

use serde::{Deserialize, Serialize};

use crate::caps::{PEX_MAX_ADDRESSES, PEX_MAX_ENTRY_AGE, PEX_MAX_FLAGS, PEX_MAX_FLAG_LEN};
use crate::error::EntrySkip;
use crate::payment::{PaymentClaim, PaymentClaimError, SignatureVerifier};

/// How a candidate address was learned — the L7 `dig.getPeers` `addresses[].kind` tokens (SPEC §3.1).
/// The lowercase serde spelling is the frozen wire form.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AddressKind {
    /// A directly reachable address (publicly routable or port-forwarded).
    Direct,
    /// A UPnP / NAT-PMP / PCP-mapped external address.
    Mapped,
    /// A STUN-discovered public reflexive address.
    Reflexive,
    /// Reachable through the relay (no direct candidate).
    Relay,
    /// An unrecognized token — the catch-all so an unknown `kind` skips the entry (SPEC §3.3)
    /// instead of aborting the message. Never emitted by a conformant sender.
    #[serde(other)]
    #[default]
    Unknown,
}

impl AddressKind {
    /// The frozen lowercase wire token (or `"unknown"` for the catch-all).
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            AddressKind::Direct => "direct",
            AddressKind::Mapped => "mapped",
            AddressKind::Reflexive => "reflexive",
            AddressKind::Relay => "relay",
            AddressKind::Unknown => "unknown",
        }
    }

    /// Whether this is one of the four registered tokens (i.e. not the `Unknown` catch-all).
    #[must_use]
    pub fn is_registered(self) -> bool {
        !matches!(self, AddressKind::Unknown)
    }
}

/// The advertiser's **provenance** for an entry (SPEC §3.1, §8.1) — how it knows the peer first-hand.
/// There is deliberately no `"pex"` token: an entry known only via PEX has no legitimate `via` to
/// claim, which is what stops re-gossip amplification (SPEC §8.1).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Provenance {
    /// Learned from a direct mTLS-verified connection to the peer.
    Direct,
    /// Learned from a relayed mTLS-verified connection to the peer.
    Relay,
    /// Learned from this participant's own introducer / relay registration surface.
    Introducer,
    /// An unrecognized token — the catch-all so an unknown `via` skips the entry (SPEC §3.3).
    /// Never emitted by a conformant sender.
    #[serde(other)]
    #[default]
    Unknown,
}

impl Provenance {
    /// The frozen lowercase wire token (or `"unknown"` for the catch-all).
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Provenance::Direct => "direct",
            Provenance::Relay => "relay",
            Provenance::Introducer => "introducer",
            Provenance::Unknown => "unknown",
        }
    }

    /// Whether this is one of the three registered provenance tokens.
    #[must_use]
    pub fn is_registered(self) -> bool {
        !matches!(self, Provenance::Unknown)
    }
}

/// One candidate address for a peer: `{ host, port, kind }` (SPEC §3.1). Byte-compatible with the L7
/// `dig.getPeers` / DHT `Contact` address shape.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Address {
    /// IPv4/IPv6 literal or hostname. MUST be non-empty (SPEC §3.3).
    #[serde(default)]
    pub host: String,
    /// P2P port, 1–65535. A `port` of 0 skips the entry (SPEC §3.3).
    #[serde(default)]
    pub port: u16,
    /// How this address was learned.
    #[serde(default)]
    pub kind: AddressKind,
}

impl Address {
    /// A directly-dialable candidate (public / port-forwarded / discovered).
    #[must_use]
    pub fn direct(host: impl Into<String>, port: u16) -> Self {
        Address {
            host: host.into(),
            port,
            kind: AddressKind::Direct,
        }
    }

    /// A candidate of a specific [`AddressKind`].
    #[must_use]
    pub fn new(host: impl Into<String>, port: u16, kind: AddressKind) -> Self {
        Address {
            host: host.into(),
            port,
            kind,
        }
    }

    /// Whether this address is well-formed for a receiver (non-empty host, non-zero port, a
    /// registered `kind`) — SPEC §3.3.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        !self.host.is_empty() && self.port != 0 && self.kind.is_registered()
    }
}

/// The unit of exchange — a peer entry (SPEC §3). Constructed via [`PeerEntry::new`] + the builder
/// methods for outgoing advertisements; decoded tolerantly for inbound validation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PeerEntry {
    /// The advertised peer's mTLS identity, `<64hex>`.
    #[serde(default)]
    pub peer_id: String,
    /// Candidate addresses, most-direct-first. MAY be empty (reachable only via shared
    /// infrastructure, e.g. relay rendezvous by `peer_id`; see the `relay-only` flag).
    #[serde(default)]
    pub addresses: Vec<Address>,
    /// The network the peer belongs to. MUST equal the link's network.
    #[serde(default)]
    pub network_id: String,
    /// Unix seconds when the advertiser last had first-hand evidence of the peer (SPEC §8.2).
    #[serde(default)]
    pub last_seen: u64,
    /// The advertiser's provenance for this entry (SPEC §8.1).
    #[serde(default)]
    pub via: Provenance,
    /// Per-peer capability flags (SPEC §3.2). Optional; defaults to empty.
    #[serde(default)]
    pub flags: Vec<String>,
    /// The peer's self-signed payment address (SPEC §3.4). Optional and omitted entirely when absent,
    /// so a record stays readable by peers predating the field.
    ///
    /// Read it with [`verified_payment_address`](Self::verified_payment_address) — the claim's own
    /// fields are private precisely so an unchecked payee cannot be obtained from it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub payment: Option<PaymentClaim>,
}

impl PeerEntry {
    /// A new entry for `peer_id` on `network_id`, last seen at `last_seen` (Unix seconds), with
    /// provenance `via`. Add addresses/flags with the builder methods.
    #[must_use]
    pub fn new(
        peer_id: impl Into<String>,
        network_id: impl Into<String>,
        last_seen: u64,
        via: Provenance,
    ) -> Self {
        PeerEntry {
            peer_id: peer_id.into(),
            addresses: Vec::new(),
            network_id: network_id.into(),
            last_seen,
            via,
            flags: Vec::new(),
            payment: None,
        }
    }

    /// Builder: append a candidate address.
    #[must_use]
    pub fn with_address(mut self, addr: Address) -> Self {
        self.addresses.push(addr);
        self
    }

    /// Builder: append a capability flag token.
    #[must_use]
    pub fn with_flag(mut self, flag: impl Into<String>) -> Self {
        self.flags.push(flag.into());
        self
    }

    /// Builder: attach the peer's self-signed payment claim (SPEC §3.4).
    #[must_use]
    pub fn with_payment(mut self, claim: PaymentClaim) -> Self {
        self.payment = Some(claim);
        self
    }

    /// The peer's payment address, **only** when the attached claim proves this peer designated it on
    /// this entry's network (SPEC §3.4.3); otherwise the reason there is no payee.
    ///
    /// This is deliberately a different question from whether the entry is usable at all: an entry
    /// whose claim fails here is still a perfectly good dial hint (see
    /// [`validate`](Self::validate)), because letting a corrupted payee cost a peer its reachability
    /// would hand a relaying attacker a way to partition it. Reachability and payability are two
    /// verdicts on one record, and they are answered by two different methods.
    ///
    /// `verifier` supplies the signature primitive for the peer's key type (SPEC §3.4.3); the
    /// `SHA-256(SPKI) == peer_id` binding is checked by this crate regardless of what the verifier
    /// does.
    pub fn verified_payment_address(
        &self,
        verifier: &impl SignatureVerifier,
    ) -> Result<&str, PaymentClaimError> {
        self.payment
            .as_ref()
            .ok_or(PaymentClaimError::NotPresent)?
            .verify(&self.peer_id, &self.network_id, verifier)
    }

    /// Validate this entry against the receiver's link context (SPEC §3.3). Returns the reason a
    /// conformant receiver skips it, or `Ok(())` to keep it. Skipping is silent — no strike.
    pub fn validate(&self, ctx: &ValidateCtx<'_>) -> Result<(), EntrySkip> {
        if !is_hex64(&self.peer_id) {
            return Err(EntrySkip::BadPeerId);
        }
        if self.peer_id == ctx.receiver_peer_id || self.peer_id == ctx.sender_peer_id {
            return Err(EntrySkip::SelfOrPartner);
        }
        if self.addresses.len() > PEX_MAX_ADDRESSES {
            return Err(EntrySkip::TooManyAddresses);
        }
        if self.addresses.iter().any(|a| !a.is_valid()) {
            return Err(EntrySkip::BadAddress);
        }
        if self.flags.len() > PEX_MAX_FLAGS || self.flags.iter().any(|f| f.len() > PEX_MAX_FLAG_LEN)
        {
            return Err(EntrySkip::TooManyFlags);
        }
        if self.network_id != ctx.network_id {
            return Err(EntrySkip::NetworkMismatch);
        }
        if !self.via.is_registered() {
            return Err(EntrySkip::BadVia);
        }
        // Size only — an unverifiable claim is NOT a reason to drop the entry (SPEC §3.4.3); this
        // bounds what a hostile sender can make a receiver hold, nothing more.
        if self.payment.as_ref().is_some_and(|p| !p.within_caps()) {
            return Err(EntrySkip::OversizePayment);
        }
        // A `last_seen` in the future is clamped by the caller (see `clamped`); only an entry too far
        // in the PAST is skipped.
        if self.last_seen < ctx.now_secs && ctx.now_secs - self.last_seen > PEX_MAX_ENTRY_AGE {
            return Err(EntrySkip::TooOld);
        }
        Ok(())
    }

    /// A copy with a future `last_seen` clamped to `now_secs` (SPEC §3.3 — a receiver SHOULD clamp a
    /// `last_seen` in the future to its own clock).
    #[must_use]
    pub fn clamped(&self, now_secs: u64) -> PeerEntry {
        let mut e = self.clone();
        if e.last_seen > now_secs {
            e.last_seen = now_secs;
        }
        e
    }

    /// The per-link **advertised-content fingerprint** — a stable string over the addresses and flags
    /// **excluding `last_seen`** (SPEC §9.1), so heartbeat churn (a fresher `last_seen` alone) never
    /// re-advertises an unchanged peer. Two entries with the same fingerprint are "the same
    /// advertisement" for delta purposes.
    ///
    /// This allocates (a `Vec<String>` per call plus the final formatted `String`) and is intended
    /// for display/debugging/tests. The delta hot path uses the allocation-free
    /// [`fingerprint_hash`](Self::fingerprint_hash) instead (#179 MED optimization).
    #[must_use]
    pub fn fingerprint(&self) -> String {
        let mut addrs: Vec<String> = self
            .addresses
            .iter()
            .map(|a| format!("{}|{}|{}", a.host, a.port, a.kind.as_str()))
            .collect();
        addrs.sort();
        let mut flags = self.flags.clone();
        flags.sort();
        let payment = self
            .payment
            .as_ref()
            .map(|p| p.wire_parts().join("|"))
            .unwrap_or_default();
        format!("{}#{}#{}", addrs.join(","), flags.join(","), payment)
    }

    /// The allocation-free equivalent of [`fingerprint`](Self::fingerprint): a 64-bit hash over the
    /// same canonical content (addresses + flags, sorted, **excluding `last_seen`**) with the same
    /// equality semantics — two entries with equal `fingerprint()` strings MUST have equal
    /// `fingerprint_hash()` values (and vice versa for practical purposes; a hash collision is
    /// possible but not a correctness concern for this delta-suppression use). Used as the `told`-map
    /// value (SPEC §9.1) so the per-tick, per-link delta comparison is a `Copy`, allocation-free `u64`
    /// equality check instead of building + sorting + formatting a `String` per advertisable entry
    /// per link per tick (#179 MED optimization).
    ///
    /// Sorts addresses/flags by **reference** (`Vec<&Address>` / `Vec<&str>`, no cloning) before
    /// feeding a stable field-separated byte stream to the hasher, so the result is independent of
    /// input order while never allocating an intermediate `String`.
    #[must_use]
    pub fn fingerprint_hash(&self) -> u64 {
        use std::hash::{Hash, Hasher};

        let mut addrs: Vec<&Address> = self.addresses.iter().collect();
        addrs.sort_by(|a, b| {
            (a.host.as_str(), a.port, a.kind.as_str()).cmp(&(
                b.host.as_str(),
                b.port,
                b.kind.as_str(),
            ))
        });
        let mut flags: Vec<&str> = self.flags.iter().map(String::as_str).collect();
        flags.sort_unstable();

        // A fixed-seed hasher (not HashMap's randomized default) so the result is reproducible within
        // and across engine instances in the same process run — only ever compared in-memory, never
        // persisted or sent over the wire, so cross-process/version stability is not required.
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        addrs.len().hash(&mut hasher);
        for a in &addrs {
            a.host.hash(&mut hasher);
            a.port.hash(&mut hasher);
            a.kind.as_str().hash(&mut hasher);
        }
        flags.len().hash(&mut hasher);
        for f in &flags {
            f.hash(&mut hasher);
        }
        // A replaced or re-signed payment claim is a content change, so it must re-advertise.
        match &self.payment {
            Some(p) => p.wire_parts().hash(&mut hasher),
            None => 0u8.hash(&mut hasher),
        }
        hasher.finish()
    }
}

/// The receiver-side context an inbound entry is validated against (SPEC §3.3).
#[derive(Debug, Clone, Copy)]
pub struct ValidateCtx<'a> {
    /// The receiver's own `peer_id` — an entry advertising it is skipped (SPEC §5.4).
    pub receiver_peer_id: &'a str,
    /// The link partner's (sender's) `peer_id` — an entry advertising it is skipped (SPEC §5.4).
    pub sender_peer_id: &'a str,
    /// The link's network — an entry on a different `network_id` is skipped.
    pub network_id: &'a str,
    /// The receiver's current time in Unix seconds — for the freshness check.
    pub now_secs: u64,
}

/// Whether `s` is exactly 64 lowercase hexadecimal characters (`<64hex>`, SPEC §2).
#[must_use]
pub fn is_hex64(s: &str) -> bool {
    s.len() == 64
        && s.bytes()
            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn hex(b: u8) -> String {
        format!("{b:02x}").repeat(32)
    }

    fn ctx<'a>(recv: &'a str, send: &'a str, net: &'a str, now: u64) -> ValidateCtx<'a> {
        ValidateCtx {
            receiver_peer_id: recv,
            sender_peer_id: send,
            network_id: net,
            now_secs: now,
        }
    }

    #[test]
    fn hex64_recognizer() {
        assert!(is_hex64(&"a".repeat(64)));
        assert!(is_hex64(&hex(0xab)));
        assert!(!is_hex64(&"a".repeat(63)));
        assert!(!is_hex64(&"A".repeat(64))); // uppercase not allowed
        assert!(!is_hex64(&"g".repeat(64))); // non-hex
    }

    #[test]
    fn kind_and_via_tokens_are_frozen_lowercase() {
        assert_eq!(
            serde_json::to_string(&AddressKind::Direct).unwrap(),
            "\"direct\""
        );
        assert_eq!(
            serde_json::to_string(&AddressKind::Relay).unwrap(),
            "\"relay\""
        );
        assert_eq!(
            serde_json::to_string(&Provenance::Introducer).unwrap(),
            "\"introducer\""
        );
    }

    #[test]
    fn unknown_kind_and_via_decode_to_catch_all() {
        let a: Address = serde_json::from_str(r#"{"host":"h","port":1,"kind":"quantum"}"#).unwrap();
        assert_eq!(a.kind, AddressKind::Unknown);
        let e: PeerEntry = serde_json::from_str(
            r#"{"peer_id":"x","addresses":[],"network_id":"n","last_seen":1,"via":"teleport"}"#,
        )
        .unwrap();
        assert_eq!(e.via, Provenance::Unknown);
    }

    #[test]
    fn valid_entry_passes() {
        let e = PeerEntry::new(hex(0x07), "mainnet", 1000, Provenance::Direct)
            .with_address(Address::direct("203.0.113.7", 9444))
            .with_flag("storage");
        assert!(e
            .validate(&ctx(&hex(0x01), &hex(0x02), "mainnet", 1000))
            .is_ok());
    }

    #[test]
    fn skip_reasons_match_spec() {
        let recv = hex(0x01);
        let send = hex(0x02);
        // bad peer_id
        let e = PeerEntry::new("nothex", "mainnet", 10, Provenance::Direct);
        assert_eq!(
            e.validate(&ctx(&recv, &send, "mainnet", 10)),
            Err(EntrySkip::BadPeerId)
        );
        // self / partner
        let e = PeerEntry::new(recv.clone(), "mainnet", 10, Provenance::Direct);
        assert_eq!(
            e.validate(&ctx(&recv, &send, "mainnet", 10)),
            Err(EntrySkip::SelfOrPartner)
        );
        let e = PeerEntry::new(send.clone(), "mainnet", 10, Provenance::Direct);
        assert_eq!(
            e.validate(&ctx(&recv, &send, "mainnet", 10)),
            Err(EntrySkip::SelfOrPartner)
        );
        // bad address (port 0)
        let e = PeerEntry::new(hex(0x07), "mainnet", 10, Provenance::Direct)
            .with_address(Address::new("h", 0, AddressKind::Direct));
        assert_eq!(
            e.validate(&ctx(&recv, &send, "mainnet", 10)),
            Err(EntrySkip::BadAddress)
        );
        // unknown kind
        let e = PeerEntry::new(hex(0x07), "mainnet", 10, Provenance::Direct)
            .with_address(Address::new("h", 1, AddressKind::Unknown));
        assert_eq!(
            e.validate(&ctx(&recv, &send, "mainnet", 10)),
            Err(EntrySkip::BadAddress)
        );
        // network mismatch
        let e = PeerEntry::new(hex(0x07), "testnet", 10, Provenance::Direct);
        assert_eq!(
            e.validate(&ctx(&recv, &send, "mainnet", 10)),
            Err(EntrySkip::NetworkMismatch)
        );
        // bad via
        let e = PeerEntry::new(hex(0x07), "mainnet", 10, Provenance::Unknown);
        assert_eq!(
            e.validate(&ctx(&recv, &send, "mainnet", 10)),
            Err(EntrySkip::BadVia)
        );
        // too old
        let e = PeerEntry::new(hex(0x07), "mainnet", 10, Provenance::Direct);
        assert_eq!(
            e.validate(&ctx(&recv, &send, "mainnet", 2000)),
            Err(EntrySkip::TooOld)
        );
    }

    #[test]
    fn too_many_addresses_and_flags() {
        let recv = hex(0x01);
        let send = hex(0x02);
        let mut e = PeerEntry::new(hex(0x07), "mainnet", 10, Provenance::Direct);
        for i in 0..9 {
            e = e.with_address(Address::direct("h", 1000 + i));
        }
        assert_eq!(
            e.validate(&ctx(&recv, &send, "mainnet", 10)),
            Err(EntrySkip::TooManyAddresses)
        );

        let mut e = PeerEntry::new(hex(0x07), "mainnet", 10, Provenance::Direct);
        for i in 0..9 {
            e = e.with_flag(format!("f{i}"));
        }
        assert_eq!(
            e.validate(&ctx(&recv, &send, "mainnet", 10)),
            Err(EntrySkip::TooManyFlags)
        );
    }

    #[test]
    fn future_last_seen_is_clamped_not_skipped() {
        let recv = hex(0x01);
        let send = hex(0x02);
        let e = PeerEntry::new(hex(0x07), "mainnet", 5000, Provenance::Direct);
        // now=1000, last_seen=5000 (future) — valid, and clamped to now.
        assert!(e.validate(&ctx(&recv, &send, "mainnet", 1000)).is_ok());
        assert_eq!(e.clamped(1000).last_seen, 1000);
        assert_eq!(e.clamped(6000).last_seen, 5000);
    }

    #[test]
    fn fingerprint_ignores_last_seen_but_tracks_addresses_and_flags() {
        let a = PeerEntry::new(hex(0x07), "mainnet", 100, Provenance::Direct)
            .with_address(Address::direct("h", 1))
            .with_flag("storage");
        let a2 = PeerEntry::new(hex(0x07), "mainnet", 999, Provenance::Direct)
            .with_address(Address::direct("h", 1))
            .with_flag("storage");
        assert_eq!(
            a.fingerprint(),
            a2.fingerprint(),
            "last_seen must not affect fingerprint"
        );
        let b = a2.clone().with_flag("holepunch");
        assert_ne!(
            a.fingerprint(),
            b.fingerprint(),
            "a flag change must change fingerprint"
        );
    }

    /// MEDIUM finding (#179): `fingerprint_hash()` is the allocation-free hot-path equality check
    /// used in the delta loop (`told` stores this hash, not the `String` fingerprint). It MUST agree
    /// with `fingerprint()`'s equality semantics: same addresses+flags (order-independent) and
    /// `last_seen`-independence hash equal; a real content change hashes different.
    #[test]
    fn fingerprint_hash_matches_fingerprint_equality_semantics() {
        let a = PeerEntry::new(hex(0x07), "mainnet", 100, Provenance::Direct)
            .with_address(Address::direct("h", 1))
            .with_flag("storage");
        let a2 = PeerEntry::new(hex(0x07), "mainnet", 999, Provenance::Direct)
            .with_address(Address::direct("h", 1))
            .with_flag("storage");
        assert_eq!(
            a.fingerprint_hash(),
            a2.fingerprint_hash(),
            "last_seen must not affect fingerprint_hash"
        );
        assert_eq!(
            a.fingerprint() == a2.fingerprint(),
            a.fingerprint_hash() == a2.fingerprint_hash(),
            "fingerprint_hash must agree with fingerprint on equality"
        );

        let b = a2.clone().with_flag("holepunch");
        assert_ne!(
            a.fingerprint_hash(),
            b.fingerprint_hash(),
            "a flag change must change fingerprint_hash"
        );
        assert_eq!(
            a.fingerprint() == b.fingerprint(),
            a.fingerprint_hash() == b.fingerprint_hash(),
            "fingerprint_hash must agree with fingerprint on inequality"
        );

        // Multiple addresses/flags supplied in a different insertion order must still hash equal
        // (the canonical form sorts both before hashing, same as `fingerprint()`).
        let c = PeerEntry::new(hex(0x08), "mainnet", 1, Provenance::Direct)
            .with_address(Address::direct("h1", 1))
            .with_address(Address::direct("h2", 2))
            .with_flag("storage")
            .with_flag("holepunch");
        let d = PeerEntry::new(hex(0x08), "mainnet", 2, Provenance::Direct)
            .with_address(Address::direct("h2", 2))
            .with_address(Address::direct("h1", 1))
            .with_flag("holepunch")
            .with_flag("storage");
        assert_eq!(
            c.fingerprint_hash(),
            d.fingerprint_hash(),
            "address/flag insertion order must not affect fingerprint_hash"
        );
        assert_eq!(c.fingerprint(), d.fingerprint());
    }

    #[test]
    fn entry_round_trips_through_json() {
        let e = PeerEntry::new(hex(0x07), "mainnet", 1_719_763_200, Provenance::Direct)
            .with_address(Address::direct("203.0.113.7", 9444))
            .with_flag("storage")
            .with_flag("holepunch");
        let json = serde_json::to_string(&e).unwrap();
        assert!(json.contains("\"peer_id\":"));
        assert!(json.contains("\"via\":\"direct\""));
        assert!(json.contains("\"kind\":\"direct\""));
        let back: PeerEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(e, back);
    }
}