Skip to main content

ts_runtime/peer_tracker/
mod.rs

1//! Peer delta update tracking.
2
3use std::{
4    collections::{HashMap, HashSet},
5    net::IpAddr,
6    sync::Arc,
7};
8
9use kameo::{
10    actor::ActorRef,
11    message::{Context, Message},
12    reply::ReplySender,
13};
14use tokio::sync::watch;
15use ts_control::{ExpiryManager, Node, UserId, UserProfile};
16use ts_keys::{DiscoPublicKey, NodePublicKey};
17use ts_transport::PeerId;
18
19use crate::{
20    Error, dataplane::PeerDiscoKeyAdvertisement, direct::DiscoKeyObserved, env::Env,
21    status::StatusNode,
22};
23
24mod peer_db;
25
26pub use peer_db::{DiscoKeyMatch, PeerDb};
27
28/// Whether `key` is the all-zero disco key, which Go spells `key.DiscoPublic.IsZero()` and treats
29/// everywhere as "this peer has no disco key" rather than as a usable key.
30fn disco_key_is_zero(key: &DiscoPublicKey) -> bool {
31    key.to_bytes() == [0u8; DiscoPublicKey::KEY_LEN_BYTES]
32}
33
34/// Normalize a disco key as it arrives from control: the all-zero key means "absent", exactly as
35/// Go's `IsZero()` checks in `endpoint.updateDiscoKey` read it.
36fn disco_key_from_control(key: Option<DiscoPublicKey>) -> Option<DiscoPublicKey> {
37    key.filter(|k| !disco_key_is_zero(k))
38}
39
40/// The local wall clock as a UTC timestamp.
41///
42/// chrono is built without its `clock` feature in this workspace, so derive it from `SystemTime`
43/// the same way the control runner and the ssh-policy paths do. A clock before the Unix epoch
44/// (unrepresentable) falls back to the epoch itself, which the expiry pass then refuses as being
45/// before its hardcoded epoch — fail-safe: flag nothing rather than expire everything.
46fn local_now() -> chrono::DateTime<chrono::Utc> {
47    std::time::SystemTime::now()
48        .duration_since(std::time::UNIX_EPOCH)
49        .ok()
50        .and_then(|d| chrono::DateTime::from_timestamp(d.as_secs() as i64, d.subsec_nanos()))
51        .unwrap_or_default()
52}
53
54/// The two disco keys a peer can present, and which of them is currently active — Go
55/// [`magicsock.endpointDisco`] (`wgengine/magicsock/endpoint.go`).
56///
57/// A peer's disco key reaches us from two independent sources: **control**, in a netmap node or a
58/// `PeersChangedPatch`, and the **peer itself**, in a TSMP disco-key advertisement carried inside
59/// the WireGuard tunnel. Go keeps both side by side on the endpoint, and so do we, because control
60/// is the slower of the two: an advertisement exists precisely to cover the window where control has
61/// not caught up with the peer's current key, so collapsing the two into one field would let the
62/// next map poll overwrite a freshly-learned key with control's stale one — losing the feature's own
63/// motivating case.
64///
65/// Only one key is active for sending at a time ([`key`](Self::key)). That active key is what the
66/// peer db carries in [`Node::disco_key`], which is this fork's live lookup for every direct-path
67/// consumer (`direct::DiscoPeerLookup` resolves against it, and `PeerDb`'s disco index is built from
68/// it) — the stand-in for Go's per-endpoint `disco` pointer.
69///
70/// [`magicsock.endpointDisco`]: https://github.com/tailscale/tailscale/blob/49e148c4a30b4f8098f69468fd27a7021d85ea02/wgengine/magicsock/endpoint.go
71#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
72struct EndpointDisco {
73    /// The key learned from control (Go `endpointDisco.controlKey`).
74    control: Option<DiscoPublicKey>,
75    /// The key learned from a TSMP advertisement (Go `endpointDisco.tsmpKey`).
76    tsmp: Option<DiscoPublicKey>,
77    /// Whether [`tsmp`](Self::tsmp) is the active key (Go `endpointDisco.tsmpActive`).
78    tsmp_active: bool,
79}
80
81impl EndpointDisco {
82    /// The key currently regarded as active — Go `endpointDisco.key()`.
83    fn key(&self) -> Option<DiscoPublicKey> {
84        if self.tsmp_active {
85            self.tsmp
86        } else {
87            self.control
88        }
89    }
90
91    /// The control-learned key, active or not — Go `endpointDisco.keyFromControl()`.
92    fn key_from_control(&self) -> Option<DiscoPublicKey> {
93        self.control
94    }
95
96    /// The TSMP-learned key, active or not — Go `endpointDisco.keyFromTSMP()`.
97    fn key_from_tsmp(&self) -> Option<DiscoPublicKey> {
98        self.tsmp
99    }
100
101    /// Replace the control-learned key, leaving any TSMP-learned key in place — Go
102    /// [`endpoint.updateDiscoKey`].
103    ///
104    /// Control's key is always recorded in control's own slot, but it takes the *active* slot only
105    /// if no TSMP-learned key already holds it: Go `epDisco.tsmpActive = old.tsmpActive ||
106    /// key.IsZero()`. A key the peer told us itself is better evidence than a control server that
107    /// is, by construction, the slower of the two sources — so control changing its mind no longer
108    /// preempts an active TSMP key. Upstream returns to control's key when disco is actually
109    /// *received* under it (`endpoint.checkAndUpdateDiscoKey`), not when control asserts it.
110    ///
111    /// An absent (Go: zero) control key still hands the slot to the TSMP key, if there is one. When
112    /// there is neither key, the caller drops the whole entry ([`is_empty`](Self::is_empty)) — which
113    /// is what stops an active TSMP slot with no TSMP key in it outliving this call, exactly as Go
114    /// nils the endpoint's `disco` pointer in the same case.
115    ///
116    /// [`endpoint.updateDiscoKey`]: https://github.com/tailscale/tailscale/blob/9ea7cba44591e0cd840c6c94d23274dd222059bf/wgengine/magicsock/endpoint.go
117    fn update_from_control(&mut self, key: Option<DiscoPublicKey>) {
118        self.control = key;
119        self.tsmp_active = self.tsmp_active || key.is_none();
120    }
121
122    /// Replace the TSMP-learned key, leaving the control-learned key in place — Go
123    /// `endpoint.updateTSMPDiscoKey`.
124    fn update_from_tsmp(&mut self, key: Option<DiscoPublicKey>) {
125        self.tsmp = key;
126        self.tsmp_active = key.is_some();
127    }
128
129    /// The peer's other known key: the slot that is not active, when it holds a key that differs
130    /// from the active one.
131    ///
132    /// This is what makes ingress under the peer's *other* key resolvable
133    /// ([`PeerDb::set_inactive_disco_key`]). `None` when the inactive slot is empty or holds the
134    /// same key as the active one — there is no second key to accept in either case.
135    fn inactive_key(&self) -> Option<DiscoPublicKey> {
136        let inactive = if self.tsmp_active {
137            self.control
138        } else {
139            self.tsmp
140        };
141
142        inactive.filter(|k| Some(*k) != self.key())
143    }
144
145    /// Accept `key` as this peer's, switching the active slot to it when it is the currently
146    /// *inactive* one — Go [`endpoint.checkAndUpdateDiscoKey`].
147    ///
148    /// Called with the sender key of a disco frame we have opened, which proves the sender holds
149    /// that key's private half. Receiving under a key is therefore demonstrative: it is what the
150    /// peer is actually using, so upstream makes it the key we send to as well.
151    ///
152    /// Returns `None` when `key` belongs to **neither** slot — the refusal that is the whole
153    /// security value of the check, and the reason this is not simply "trust whatever key opened".
154    /// Otherwise `Some(changed)`, where `changed` reports whether the active key moved (and so
155    /// whether the direct path built under the old one has to be invalidated).
156    ///
157    /// [`endpoint.checkAndUpdateDiscoKey`]: https://github.com/tailscale/tailscale/blob/9ea7cba44591e0cd840c6c94d23274dd222059bf/wgengine/magicsock/endpoint.go
158    fn check_and_update(&mut self, key: DiscoPublicKey) -> Option<bool> {
159        if self.key() == Some(key) {
160            return Some(false);
161        }
162
163        // Not the active key. Go's compare-and-swap on `tsmpActive`: whichever slot holds it
164        // becomes the active one. Control's slot is tried first only for determinism — the two
165        // holding the same key is already handled by the equality check above.
166        if self.control == Some(key) {
167            self.tsmp_active = false;
168            return Some(true);
169        }
170        if self.tsmp == Some(key) {
171            self.tsmp_active = true;
172            return Some(true);
173        }
174
175        None
176    }
177
178    /// No key material from either source — Go nils out the endpoint's `disco` pointer here.
179    fn is_empty(&self) -> bool {
180        self.control.is_none() && self.tsmp.is_none()
181    }
182}
183
184/// Actor that tracks peer delta updates and emits new states.
185pub struct PeerTracker {
186    peer_db: PeerDb,
187    seen_state_update: bool,
188    pending_requests: Vec<Pending>,
189    /// Latest peer snapshot, published on every netmap update so embedders can watch for peer
190    /// changes ([`WatchNetmap`]).
191    peer_watch: watch::Sender<Vec<StatusNode>>,
192    /// Accumulated netmap user profiles (`MapResponse.UserProfiles`), keyed by user id, joined
193    /// against a node's [`Node::user_id`](ts_control::Node::user_id) to resolve the owning user's
194    /// login/display name for a [`WhoIs`](crate::status::WhoIs). Control sends these incrementally
195    /// (only new/changed profiles per response), so this map **accumulates** across updates rather
196    /// than being replaced — a peer upserted in one response may reference a profile delivered in an
197    /// earlier one.
198    user_profiles: HashMap<UserId, UserProfile>,
199    /// Per-peer disco-key provenance ([`EndpointDisco`]), keyed by the peer's node key.
200    ///
201    /// Go keeps this on the magicsock `endpoint`, which the peer map keys by node key; here the peer
202    /// db stores control's [`Node`] verbatim, so the second key (and which of the two is active)
203    /// lives beside it. Keying by node key reproduces Go's lifetime exactly: the state is dropped
204    /// when the peer leaves the netmap, and a peer that ROTATES its node key gets a fresh entry —
205    /// Go builds it a new endpoint, so a key learned over TSMP under the old node key is never
206    /// carried onto the new one. [`prune_endpoint_disco`](PeerTracker::prune_endpoint_disco) does
207    /// the dropping.
208    endpoint_disco: HashMap<NodePublicKey, EndpointDisco>,
209    /// Tailnet-Lock (TKA) authority enforced at the peer-trust chokepoint, matching Go
210    /// `tkaFilterNetmapLocked`. Read on demand from a [`watch`] cell the control runner owns: when it
211    /// holds `Some` (a verified lock has been synced from control), enforcement is **active** — every
212    /// upserted peer must present a `key_signature` this authority authorizes, or it is dropped
213    /// (fail-closed), exactly as Go drops peers with a missing or failing signature. When it holds
214    /// `None` (no lock, or the lock was disabled) enforcement is **inactive** and every peer is
215    /// upserted, identical to pre-TKA behavior and to Go's `b.tka == nil` early return.
216    ///
217    /// A `watch::Receiver` (not the bus) is the transport on purpose: the authority is a single
218    /// security-critical state cell, and `watch` is last-write-wins, never-dropped, and ordered by
219    /// the control runner's own writes — so a disable (`None`) can never be reordered behind or
220    /// silently dropped before a stale `Some` (which a best-effort broadcast bus could do, leaving a
221    /// defunct lock enforcing forever). The control runner is the sole writer; we only ever read.
222    ///
223    /// The authority always passes through `VerifiedAumChain::verify` before the control runner
224    /// publishes it, so enforcement only engages on a chain we have cryptographically verified.
225    /// Connectivity now depends on `ts_tka` verifying genuinely-good signatures correctly (see
226    /// SECURITY.md). Self is structurally never filtered here (the self node never enters `peer_db` —
227    /// it is routed to the control runner's `self_node` cell), so a node cannot lock itself out of
228    /// its own netmap.
229    tka_authority: watch::Receiver<Option<Arc<ts_tka::Authority>>>,
230    /// Node-key expiry enforcement — Go `ipnlocal.expiryManager` (`ipn/ipnlocal/expiry.go`).
231    ///
232    /// Holds the local-to-control clock delta (fed from `MapResponse.ControlTime`) and the set of
233    /// peers already flagged, and is the thing that actually rewrites an expired peer. It lives
234    /// here because the peer db is this fork's netmap: every site that installs a peer goes through
235    /// [`upsert_from_control`](PeerTracker::upsert_from_control), so putting the pass there is what
236    /// makes "no peer is ever installed unflagged" true by construction rather than by review.
237    expiry: ExpiryManager,
238    /// The most recent self node control sent, kept only so it can be folded into
239    /// [`ExpiryManager::next_peer_expiry`] exactly as Go folds in `nm.SelfNode` — this node's own
240    /// key expiry must arm the timer too. Never entered into the peer db (self is not a peer) and
241    /// never flagged here: the self-expiry *decision* is the control runner's (`expiry_action`).
242    self_node: Option<Node>,
243    /// The armed expiry timer — Go `LocalBackend.nmExpiryTimer`.
244    ///
245    /// Sleeps until the soonest future key expiry across the peers and the self node, then sends
246    /// [`ExpiryTimerFired`] so expiry is re-evaluated **when it happens** rather than whenever the
247    /// next netmap arrives. Re-armed (and the old one aborted) after every netmap and after every
248    /// firing. Aborting rather than letting a stale timer run is this fork's equivalent of Go's
249    /// `numClientStatusCalls` generation check.
250    expiry_timer: Option<tokio::task::JoinHandle<()>>,
251    env: Env,
252}
253
254impl PeerTracker {
255    fn peer_by_name_opt(&self, name: &str) -> Option<&Node> {
256        // Canonicalization (case + trailing dot) is handled inside the name index lookup.
257        self.peer_db.get(&name).map(|(_id, node)| node)
258    }
259
260    fn peer_by_tailnet_ip_opt(&self, ip: IpAddr) -> Option<&Node> {
261        self.peer_db.get(&ip).map(|(_id, node)| node)
262    }
263
264    /// Build the peer entries for a [`Status`](crate::Status) snapshot from the current peer db.
265    ///
266    /// Connectivity fields (`cur_addr`/`relay`) are left at their `from_node` defaults (`None`) here:
267    /// this is the live-watch/hot path and must stay magicsock-free and synchronous. The explicit
268    /// [`GetStatus`] snapshot enriches them ([`status_peers_with_ids`](Self::status_peers_with_ids)).
269    fn status_peers(&self) -> Vec<StatusNode> {
270        self.peer_db
271            .peers()
272            .values()
273            .map(StatusNode::from_node)
274            .collect()
275    }
276
277    /// Like [`status_peers`](Self::status_peers) but pairs each entry with its [`PeerId`], so the
278    /// caller can join per-peer connectivity (the direct manager's `best_addrs`, keyed by `PeerId`)
279    /// onto the `StatusNode` before returning it. Order is unspecified (a `HashMap` walk).
280    fn status_peers_with_ids(&self) -> Vec<(PeerId, StatusNode)> {
281        self.peer_db
282            .peers()
283            .iter()
284            .map(|(id, node)| (*id, StatusNode::from_node(node)))
285            .collect()
286    }
287
288    fn whois_opt(&self, addr: std::net::SocketAddr) -> Option<crate::status::WhoIs> {
289        let ip = crate::status::whois_addr(addr);
290        let node = self.peer_by_tailnet_ip_opt(ip).cloned()?;
291        // Join the node's owning user id against the accumulated UserProfiles table. `None` when
292        // control sent no profile for that user (e.g. tagged nodes with no human owner, or a
293        // profile not yet delivered). The whole profile is handed over, not a flattened label:
294        // `WhoIs` is what an embedder authorises on, and `UserProfile::groups` is the only owner
295        // attribute it cannot re-derive from the netmap itself.
296        let user_profile = self.resolve_user_profile(node.user_id);
297        Some(crate::status::WhoIs::from_node_with_profile(
298            node,
299            user_profile,
300        ))
301    }
302
303    /// Merge a response's `MapResponse.UserProfiles` into the accumulated table, keyed by user id.
304    ///
305    /// Control sends profiles incrementally — only new or changed ones per response — so this
306    /// **accumulates**: a profile already held for a user id that this response does not restate
307    /// stays, and one it does restate is replaced wholesale (control's newer copy wins, including
308    /// a group list that shrank).
309    fn accumulate_user_profiles(&mut self, profiles: &[UserProfile]) {
310        for profile in profiles {
311            self.user_profiles.insert(profile.id, profile.clone());
312        }
313    }
314
315    /// Resolve a user id to its profile from the accumulated profile table.
316    fn resolve_user_profile(&self, user_id: UserId) -> Option<UserProfile> {
317        self.user_profiles.get(&user_id).cloned()
318    }
319
320    /// Whether `node` may be admitted to the peer db under Tailnet Lock, matching Go
321    /// `tkaFilterNetmapLocked`'s per-peer verdict (drop unsigned / failed-signature peers).
322    ///
323    /// This consults the live [`tka_authority`](Self::tka_authority) cell on each call (one `borrow`,
324    /// held only for the duration of the verdict). For a `Full` resync — which checks every peer —
325    /// prefer [`tka_authority_snapshot`](Self::tka_authority_snapshot) +
326    /// [`tka_snapshot_admits`](Self::tka_snapshot_admits) to borrow once and verify each peer a single
327    /// time; this method is the convenience wrapper for the single-peer (`Delta`/patch) sites.
328    ///
329    /// Fail-closed and gated:
330    /// - No authority ⇒ no lock synced ⇒ always admit (Go's `b.tka == nil` early return; identical to
331    ///   pre-TKA behavior).
332    /// - **Empty trusted-key state** ⇒ always admit (logged at `error!` — see
333    ///   [`tka_snapshot_admits`](Self::tka_snapshot_admits) for the full rationale).
334    /// - Authority present + peer carries a `key_signature` the authority authorizes for the peer's
335    ///   node key ⇒ admit.
336    /// - Authority present + signature missing or unauthorized/invalid ⇒ **drop** (Go drops peers
337    ///   with a missing signature or failed `NodeKeyAuthorized` under tailnet lock).
338    fn tka_admits(&self, node: &Node) -> bool {
339        // Single-peer sites (`Delta`/patch) only need the admit bool; the rotation details are used
340        // exclusively by the cross-peer `Full` filter (rotation obsolescence is whole-netmap).
341        Self::tka_snapshot_admits(self.tka_authority.borrow().as_deref(), node).admitted
342    }
343
344    /// Borrow the current TKA authority once (cloning the cheap `Arc`) for a batch verdict. Returns
345    /// `None` when no lock is synced (admit-all). Used by the `Full` path so a netmap of N peers
346    /// reads the cell once and runs at most one signature verify per peer (not two).
347    fn tka_authority_snapshot(&self) -> Option<Arc<ts_tka::Authority>> {
348        self.tka_authority.borrow().clone()
349    }
350
351    /// The per-peer Tailnet-Lock verdict against an already-borrowed `authority` snapshot. Factored
352    /// out so both the single-peer [`tka_admits`](Self::tka_admits) and the `Full` batch path share
353    /// one verdict implementation (no divergence) while the batch path verifies each peer exactly
354    /// once.
355    ///
356    /// Returns whether the peer is admitted AND, for an admitted peer signed by a rotation chain, the
357    /// [`RotationDetails`](ts_tka::RotationDetails) of that chain — so the `Full` path can run the
358    /// cross-peer rotation filter (Go's `rotationTracker`) without a second verify per peer. A peer
359    /// that is dropped, unsigned, or signed by a non-rotation chain carries `rotation == None`.
360    ///
361    /// Never logs key/signature bytes — only the `stable_id` and the `TkaError` Display (static
362    /// descriptors). One documented parity gap remains vs Go (in PARITY_ROADMAP): no
363    /// `UnsignedPeerAPIOnly` *admission* exemption — Go admits such a peer unsigned under an active
364    /// lock, we drop it (stricter, the safe direction). [`Node::unsigned_peer_api_only`] is now
365    /// carried, and the routes half of upstream's treatment is enforced at decode
366    /// (`ts_control::Node`'s `From` impl clamps such a peer's accepted routes to its own addresses,
367    /// unconditionally, whether or not a lock is active); only the admission carve-out is deferred.
368    fn tka_snapshot_admits(authority: Option<&ts_tka::Authority>, node: &Node) -> TkaVerdict {
369        let Some(auth) = authority else {
370            return TkaVerdict::admit();
371        };
372
373        // Brick-guard: an authority with no trusted keys would drop every peer. A verified chain is
374        // structurally guaranteed ≥1 key (genesis rejects an empty key set, and the last key cannot
375        // be removed), so reaching here means a `ts_tka` invariant was violated — admit rather than
376        // black-hole the whole netmap, and log at `error!` because it signals a real bug, not an
377        // expected runtime input. This is OUR fail-safe, not a Go behavior. NOTE: it only catches the
378        // empty-keyset shape; a non-empty authority that authorizes none of the offered peers still
379        // (correctly) drops them — that is what a lock that revoked everyone means. The
380        // "authorized-zero-peers" isolation case is surfaced separately by the caller.
381        if auth.state().keys.is_empty() {
382            tracing::error!(
383                "TKA: authority has an empty trusted-key set (verified chains never do — likely a \
384                 ts_tka bug); not enforcing (admitting all) to avoid isolating the node"
385            );
386            return TkaVerdict::admit();
387        }
388
389        if node.key_signature.is_empty() {
390            tracing::warn!(
391                stable_id = ?node.stable_id,
392                "TKA: dropping unsigned peer under tailnet lock"
393            );
394            return TkaVerdict::drop();
395        }
396
397        match auth.node_key_authorized_with_details(&node.node_key.to_bytes(), &node.key_signature)
398        {
399            Ok(rotation) => {
400                tracing::debug!(stable_id = ?node.stable_id, "TKA: peer node-key authorized");
401                TkaVerdict {
402                    admitted: true,
403                    rotation,
404                }
405            }
406            Err(e) => {
407                tracing::warn!(
408                    stable_id = ?node.stable_id,
409                    error = %e,
410                    "TKA: dropping peer with unauthorized node key"
411                );
412                TkaVerdict::drop()
413            }
414        }
415    }
416
417    /// The **keep** verdict for a whole batch of peers under `authority` — one complete Go
418    /// `tkaFilterNetmapLocked` pass (`ipn/ipnlocal/tailnet-lock.go`, v1.100.0), in Go's order:
419    ///
420    /// 1. the per-peer signature verdict ([`tka_snapshot_admits`](Self::tka_snapshot_admits)), then
421    /// 2. the cross-peer rotation filter (Go `rotationTracker`): a peer presenting a node key that a
422    ///    newer rotation has superseded — or a tied clone of one — is dropped even though its own
423    ///    signature verifies. That is whole-batch by nature (one peer's chain obsoletes another's
424    ///    key), which is why it lives here and not in the per-peer verdict.
425    ///
426    /// Factored out because two call sites must agree exactly on what "admitted" means: the `Full`
427    /// netmap upsert in [`apply_peer_update`](Self::apply_peer_update), and
428    /// [`tka_reevaluate_peer_db`](Self::tka_reevaluate_peer_db), which re-runs the same pass over the
429    /// peers already in the db when a freshly-synced authority is installed. A divergence between
430    /// them would be a peer admitted by one path and dropped by the other.
431    ///
432    /// `authority` is borrowed once and each peer verified exactly once (the ed25519 verify is the
433    /// expensive part). Returns one `bool` per input node, in input order; `None` authority ⇒ all
434    /// `true` (no lock synced ⇒ admit all, Go's `b.tka == nil` early return).
435    ///
436    /// `pub(crate)` for a third caller with the same requirement: the cold-start replay of a cached
437    /// netmap ([`control_runner::load_cached_netmap`](crate::control_runner::load_cached_netmap)),
438    /// which must apply the same pass to the cached peers that the netmap they were cached from
439    /// already went through — Go replays its cached map through `setNetMapLocked`, so it runs this
440    /// very filter.
441    pub(crate) fn tka_keep_verdicts(
442        authority: Option<&ts_tka::Authority>,
443        nodes: &[&Node],
444    ) -> Vec<bool> {
445        let verdicts = nodes
446            .iter()
447            .map(|node| Self::tka_snapshot_admits(authority, node))
448            .collect::<Vec<_>>();
449
450        let mut rotation = RotationTracker::default();
451        for (node, verdict) in nodes.iter().zip(&verdicts) {
452            if verdict.admitted
453                && let Some(details) = &verdict.rotation
454            {
455                rotation.add(node.node_key.to_bytes().to_vec(), details);
456            }
457        }
458        let obsolete = rotation.obsolete_keys();
459
460        nodes
461            .iter()
462            .zip(&verdicts)
463            .map(|(node, v)| {
464                // `contains` takes `&[u8]` (HashSet<Vec<u8>> borrows as a slice) — no alloc.
465                v.admitted && !obsolete.contains(&node.node_key.to_bytes()[..])
466            })
467            .collect()
468    }
469}
470
471/// The outcome of a per-peer Tailnet-Lock check: whether the peer is admitted, plus (for an admitted
472/// peer signed by a rotation chain) the chain's [`RotationDetails`](ts_tka::RotationDetails) so the
473/// `Full` path can run the cross-peer rotation filter from the SAME verify pass (no second verify).
474struct TkaVerdict {
475    admitted: bool,
476    rotation: Option<ts_tka::RotationDetails>,
477}
478
479impl TkaVerdict {
480    /// Admitted, no rotation details (no lock / brick-guard / non-rotation signature).
481    fn admit() -> Self {
482        Self {
483            admitted: true,
484            rotation: None,
485        }
486    }
487    /// Dropped.
488    fn drop() -> Self {
489        Self {
490            admitted: false,
491            rotation: None,
492        }
493    }
494}
495
496/// Cross-peer rotation-obsolescence tracker, mirroring Go `ipnlocal.rotationTracker`. Fed the
497/// [`RotationDetails`](ts_tka::RotationDetails) of every admitted, rotation-signed peer in a `Full`
498/// netmap; [`obsolete_keys`](Self::obsolete_keys) then returns the node keys to drop on top of the
499/// per-peer verdict. Two rules (Go `tkaFilterNetmapLocked` + `rotationTracker.obsoleteKeys`):
500///
501/// 1. Every prior node key named in any rotation chain is obsolete (a newer chain rotated it away).
502/// 2. Among `Direct`-rooted chains sharing one wrapping pubkey (a clone signal), only the
503///    longest-chain peer survives; if the two longest are tied, ALL in that group are dropped (we
504///    cannot tell which is the latest, so reject for safety). `Credential`-rooted chains are exempt
505///    from rule 2 — several nodes can legitimately join under one reusable auth key (same wrapping
506///    pubkey), so sharing it is not a clone signal there. (Rule 1 still applies to them.)
507///
508/// Node keys are tracked as raw `Vec<u8>` (the verified 32-byte node-public bytes).
509#[derive(Default)]
510struct RotationTracker {
511    obsolete: HashSet<Vec<u8>>,
512    by_wrapping_key: HashMap<Vec<u8>, Vec<SigRotation>>,
513}
514
515/// One admitted peer's rotation entry within a wrapping-key group.
516struct SigRotation {
517    node_key: Vec<u8>,
518    num_prev_keys: usize,
519}
520
521impl RotationTracker {
522    /// Record an admitted peer `node_key` and its rotation `details` (Go `addRotationDetails`).
523    fn add(&mut self, node_key: Vec<u8>, details: &ts_tka::RotationDetails) {
524        // Rule 1: every prior key is obsolete — applied for ALL chains (incl. credential-rooted),
525        // matching Go's ungated `obsolete.AddSlice(d.PrevNodeKeys)`.
526        self.obsolete.extend(details.prev_node_keys.iter().cloned());
527        // Rule 2 (clone-uniqueness) is gated to Direct-rooted chains only.
528        if details.initial_sig_kind != ts_tka::SigKind::Direct {
529            return;
530        }
531        self.by_wrapping_key
532            .entry(details.initial_wrapping_pubkey.clone())
533            .or_default()
534            .push(SigRotation {
535                node_key,
536                num_prev_keys: details.prev_node_keys.len(),
537            });
538    }
539
540    /// Compute the full obsolete node-key set (Go `rotationTracker.obsoleteKeys`). Processes each
541    /// wrapping-key group, mutating the shared `obsolete` set as it goes (so a key obsoleted by one
542    /// group is seen as obsolete by later groups via the `retain` below — Go's
543    /// `slices.DeleteFunc(... Contains)`). Group iteration order (a `HashMap` drain) is
544    /// nondeterministic, but the result is order-INDEPENDENT: this only ever *inserts* into
545    /// `obsolete` (never removes), and rule 1 already obsoleted every prior key before this loop, so
546    /// the final set is a union that does not depend on which group runs first (as in Go).
547    fn obsolete_keys(mut self) -> HashSet<Vec<u8>> {
548        // Drain only the group map so the loop can mutate `self.obsolete` without aliasing it; the
549        // shared `obsolete` set itself is NOT drained, preserving the cross-group visibility above.
550        let groups: Vec<Vec<SigRotation>> = self.by_wrapping_key.drain().map(|(_k, v)| v).collect();
551        for mut group in groups {
552            // Drop entries already obsoleted (rotated away) by another chain.
553            group.retain(|rd| !self.obsolete.contains(&rd.node_key));
554            if group.is_empty() {
555                continue;
556            }
557            // Longest chain (most prior keys) is the newest ⇒ the survivor; sort decreasing.
558            // `sort_by_key` is stable (like Go's `SortStableFunc`); `Reverse` gives descending order.
559            group.sort_by_key(|rd| core::cmp::Reverse(rd.num_prev_keys));
560            if group.len() >= 2 && group[0].num_prev_keys == group[1].num_prev_keys {
561                // Tie for longest ⇒ cannot disambiguate the latest ⇒ drop the WHOLE group.
562                tracing::warn!(
563                    "TKA: multiple peers share a wrapping key with equal rotation depth; dropping all (cannot determine the latest)"
564                );
565                for rd in &group {
566                    self.obsolete.insert(rd.node_key.clone());
567                }
568            } else {
569                // Only the longest-chain peer survives; the rest are obsolete.
570                for rd in &group[1..] {
571                    self.obsolete.insert(rd.node_key.clone());
572                }
573            }
574        }
575        self.obsolete
576    }
577}
578
579impl kameo::Actor for PeerTracker {
580    /// `(env, tka_authority)`: the bus/keys env, plus the read end of the control runner's TKA
581    /// enforcement-authority cell (Go `tkaFilterNetmapLocked`). The control runner is the sole
582    /// writer; it publishes the verified `Authority` after a successful `/machine/tka/sync` and
583    /// `None` when the lock is disabled. A `watch` cell (not a bus message) so the latest value is
584    /// always readable on demand, never dropped, and never reordered (see the control runner's
585    /// `tka_authority` cell).
586    type Args = (Env, watch::Receiver<Option<Arc<ts_tka::Authority>>>);
587    type Error = Error;
588
589    async fn on_start(
590        (env, tka_authority): Self::Args,
591        slf: ActorRef<Self>,
592    ) -> Result<Self, Self::Error> {
593        env.subscribe::<Arc<ts_control::StateUpdate>>(&slf).await?;
594        env.subscribe::<PeerDiscoKeyAdvertisement>(&slf).await?;
595        env.subscribe::<DiscoKeyObserved>(&slf).await?;
596
597        // Re-filter the peer db whenever the enforcement authority changes. Go gets this for free:
598        // `SetControlClientStatus` runs `tkaSyncIfNeeded` and `tkaFilterNetmapLocked` back to back
599        // over one netmap. Here the sync is asynchronous, so the peers admitted before the authority
600        // arrived need a second pass — see `tka_reevaluate_peer_db`. `changed()` resolves on every
601        // write to the cell (enable, re-sync, disable); the task ends when the control runner drops
602        // the sender (shutdown) or the tracker itself is gone.
603        //
604        // A **weak** ref on purpose: the runtime holds only a `WeakActorRef` to the peer tracker, so
605        // a strong one parked in this task would keep the actor's mailbox alive past shutdown.
606        let mut authority_changes = tka_authority.clone();
607        let notify = slf.downgrade();
608        tokio::spawn(async move {
609            while authority_changes.changed().await.is_ok() {
610                let Some(tracker) = notify.upgrade() else {
611                    break; // the peer tracker is gone; nothing left to re-filter
612                };
613                if tracker.tell(TkaAuthorityChanged).await.is_err() {
614                    break; // the peer tracker stopped
615                }
616            }
617        });
618
619        let (peer_watch, _) = watch::channel(Vec::new());
620
621        Ok(Self {
622            peer_db: PeerDb::default(),
623            pending_requests: Default::default(),
624            seen_state_update: false,
625            peer_watch,
626            user_profiles: HashMap::new(),
627            endpoint_disco: HashMap::new(),
628            // The cell starts `None` (no lock synced ⇒ enforcement inactive, admit all, matching
629            // Go's `b.tka == nil`); the control runner flips it to `Some` on the first sync.
630            tka_authority,
631            expiry: ExpiryManager::new(),
632            self_node: None,
633            expiry_timer: None,
634            env,
635        })
636    }
637}
638
639enum Pending {
640    PeerByName(PeerByName, ReplySender<Option<Node>>),
641    AcceptedRoute(PeerByAcceptedRoute, ReplySender<Vec<Node>>),
642    TailnetIp(PeerByTailnetIp, ReplySender<Option<Node>>),
643    Status(ReplySender<Vec<(PeerId, StatusNode)>>),
644    WhoIs(Whois, ReplySender<Option<crate::status::WhoIs>>),
645}
646
647// For messages with arguments, a struct is generated with the args as fields. They aren't
648// documented, and we can't apply attributes directly to the fields. Hence, wrap in a module where
649// docs are turned off everywhere.
650#[allow(missing_docs)]
651mod msg_impl {
652    use std::net::IpAddr;
653
654    use kameo::prelude::DelegatedReply;
655
656    use super::*;
657
658    #[kameo::messages]
659    impl PeerTracker {
660        /// Lookup a peer by name.
661        ///
662        /// Waits until we've received at least one peer update from control.
663        #[message(ctx)]
664        pub async fn peer_by_name(
665            &mut self,
666            ctx: &mut Context<Self, DelegatedReply<Option<Node>>>,
667            name: String,
668        ) -> DelegatedReply<Option<Node>> {
669            let (deleg, sender) = ctx.reply_sender();
670            let Some(sender) = sender else { return deleg };
671
672            if !self.seen_state_update {
673                tracing::debug!(query = name, "no peer state seen yet, queueing request");
674
675                self.pending_requests
676                    .push(Pending::PeerByName(PeerByName { name }, sender));
677
678                return deleg;
679            }
680
681            sender.send(self.peer_by_name_opt(&name).cloned());
682
683            deleg
684        }
685
686        /// Lookup all peers that accept packets addressed to the given IP.
687        ///
688        /// This includes the peer's tailnet address and any subnet routes it provides. Only
689        /// the peers with the most specific subnet route match that covers `ip` will be
690        /// returned.
691        ///
692        /// E.g., suppose:
693        ///
694        /// - We're querying for `10.1.2.3`
695        /// - `PeerA` and `PeerB` have accepted routes for `10.1.2.0/24`
696        /// - `PeerC` has an accepted route for `10.1.0.0/16`
697        ///
698        /// Only `PeerA` and `PeerB` will be returned, since they have the most specific
699        /// prefix match.
700        #[message(ctx)]
701        pub fn peer_by_accepted_route(
702            &mut self,
703            ctx: &mut Context<Self, DelegatedReply<Vec<Node>>>,
704            ip: IpAddr,
705        ) -> DelegatedReply<Vec<Node>> {
706            let (deleg, sender) = ctx.reply_sender();
707            let Some(sender) = sender else { return deleg };
708
709            if !self.seen_state_update {
710                tracing::debug!(query = %ip, "no peer state seen yet, queueing request");
711
712                self.pending_requests
713                    .push(Pending::AcceptedRoute(PeerByAcceptedRoute { ip }, sender));
714
715                return deleg;
716            }
717
718            sender.send(
719                self.peer_db
720                    .get_route(ip.into())
721                    .map(|(_id, node)| node.clone())
722                    .collect(),
723            );
724
725            deleg
726        }
727
728        /// Lookup the peer that has the given tailnet IP address.
729        #[message(ctx)]
730        pub fn peer_by_tailnet_ip(
731            &mut self,
732            ctx: &mut Context<Self, DelegatedReply<Option<Node>>>,
733            ip: IpAddr,
734        ) -> DelegatedReply<Option<Node>> {
735            let (deleg, sender) = ctx.reply_sender();
736            let Some(sender) = sender else { return deleg };
737
738            if !self.seen_state_update {
739                tracing::debug!(query = %ip, "no peer state seen yet, queueing request");
740
741                self.pending_requests
742                    .push(Pending::TailnetIp(PeerByTailnetIp { ip }, sender));
743
744                return deleg;
745            }
746
747            sender.send(self.peer_by_tailnet_ip_opt(ip).cloned());
748
749            deleg
750        }
751
752        /// Build the peer entries of a [`Status`](crate::Status) snapshot, each paired with its
753        /// [`PeerId`] so [`Runtime::status`](crate::Runtime::status) can join per-peer connectivity
754        /// (`cur_addr`/`relay`) from the direct manager before returning. The self node is *not*
755        /// included here (it lives in the control runner); `Runtime::status` combines both and drops
756        /// the ids.
757        ///
758        /// Waits until we've received at least one peer update from control.
759        #[message(ctx)]
760        pub fn get_status(
761            &mut self,
762            ctx: &mut Context<Self, DelegatedReply<Vec<(PeerId, StatusNode)>>>,
763        ) -> DelegatedReply<Vec<(PeerId, StatusNode)>> {
764            let (deleg, sender) = ctx.reply_sender();
765            let Some(sender) = sender else { return deleg };
766
767            if !self.seen_state_update {
768                tracing::debug!("no peer state seen yet, queueing status request");
769                self.pending_requests.push(Pending::Status(sender));
770                return deleg;
771            }
772
773            sender.send(self.status_peers_with_ids());
774
775            deleg
776        }
777
778        /// Return every known peer's full domain [`Node`] (not the lossy [`StatusNode`]).
779        ///
780        /// Used by [`Runtime::file_targets`](crate::Runtime::file_targets), which needs the full node
781        /// (peerAPI address, owning user id, cap map) to compute Taildrop send targets. The self node
782        /// is not included (it lives in the control runner). Returns empty before the first netmap —
783        /// the natural "not connected yet" analog (an immediate answer, no queueing needed: callers
784        /// that need a populated list await `Running` first).
785        #[message]
786        pub fn all_peers(&self) -> Vec<Node> {
787            self.peer_db.peers().values().cloned().collect()
788        }
789
790        /// Look up a peer by its control-assigned stable node id ([`Node::stable_id`]).
791        ///
792        /// The lookup a caller holding an older [`Node`] snapshot uses to refresh it before acting
793        /// on it — notably the Taildrop send path (`tailscale::Device::send_file`), which must not
794        /// dial a peer this node has since flagged expired. `None` means the db holds no peer with
795        /// that id: either it has left the tailnet, or no netmap has arrived yet.
796        ///
797        /// Answers immediately in both cases; unlike [`PeerByName`] it does **not** queue until the
798        /// first peer update. A caller that already holds a snapshot has one to fall back on, and
799        /// blocking a send behind a netmap that may never come would be worse than answering from
800        /// what is known.
801        #[message]
802        pub fn peer_by_stable_id(&self, stable_id: ts_control::StableNodeId) -> Option<Node> {
803            self.peer_db.get(&stable_id).map(|(_id, node)| node.clone())
804        }
805
806        /// Resolve which node owns a tailnet source address.
807        ///
808        /// Maps the source IP of `addr` to the owning node via the tailnet-IP index, returning a
809        /// [`WhoIs`](crate::WhoIs). The port is ignored (a tailnet IP uniquely identifies a node).
810        ///
811        /// The resulting [`WhoIs`](crate::WhoIs) carries no user/login or capability data: this
812        /// fork's domain [`Node`] does not retain those wire fields. See the
813        /// [`status`](crate::status) module docs for the gap.
814        ///
815        /// Waits until we've received at least one peer update from control.
816        #[message(ctx)]
817        pub fn whois(
818            &mut self,
819            ctx: &mut Context<Self, DelegatedReply<Option<crate::status::WhoIs>>>,
820            addr: std::net::SocketAddr,
821        ) -> DelegatedReply<Option<crate::status::WhoIs>> {
822            let (deleg, sender) = ctx.reply_sender();
823            let Some(sender) = sender else { return deleg };
824
825            if !self.seen_state_update {
826                tracing::debug!(query = %addr, "no peer state seen yet, queueing whois request");
827                self.pending_requests
828                    .push(Pending::WhoIs(Whois { addr }, sender));
829                return deleg;
830            }
831
832            sender.send(self.whois_opt(addr));
833
834            deleg
835        }
836
837        /// Subscribe to netmap peer-change events.
838        ///
839        /// Returns a [`watch::Receiver`] whose value is the current set of peer
840        /// [`StatusNode`]s, updated on every netmap state update from control. Embedders can await
841        /// changes via [`watch::Receiver::changed`] to react to peers joining, leaving, or changing.
842        ///
843        /// The receiver's initial value is the peer set at subscription time (empty before the
844        /// first netmap update). This is a peer-only view; combine with the self node from
845        /// [`Runtime::status`](crate::Runtime::status) when a full snapshot is needed.
846        #[message(derive(Clone))]
847        pub fn watch_netmap(&self) -> watch::Receiver<Vec<StatusNode>> {
848            self.peer_watch.subscribe()
849        }
850    }
851}
852
853pub use msg_impl::*;
854
855#[derive(Debug, Clone)]
856pub(crate) struct PeerState {
857    #[allow(unused)]
858    pub deletions: HashSet<PeerId>,
859    #[allow(unused)]
860    pub upserts: HashSet<PeerId>,
861    pub peers: Arc<PeerDb>,
862    /// Control's `silent-disco` node attribute, read off the **self** node
863    /// ([`ts_control::Node::silent_disco`]) and carried with the peer snapshot so
864    /// [`crate::direct::DirectManager`] can push it onto the magicsock.
865    ///
866    /// It rides this message rather than a channel of its own because that is the shape it has
867    /// upstream: Go hands `debugFlagsLocked().heartbeatDisabled` to `endpoint.updateFromNode` while
868    /// applying the netmap, so the flag and the peer set land on the endpoints together. A snapshot
869    /// published for some other reason (a disco-key switch, an expiry, a TKA eviction) carries the
870    /// attribute's current value too, which is harmless — the push is idempotent.
871    pub silent_disco: bool,
872}
873
874impl Message<Arc<ts_control::StateUpdate>> for PeerTracker {
875    type Reply = ();
876
877    async fn handle(
878        &mut self,
879        msg: Arc<ts_control::StateUpdate>,
880        ctx: &mut Context<Self, Self::Reply>,
881    ) {
882        // Accumulate user profiles first — control sends them incrementally and a response may
883        // carry profiles with no peer delta (or peers that reference a profile from an earlier
884        // response), so this must happen before the no-peer-update early return below.
885        self.accumulate_user_profiles(&msg.user_profiles);
886
887        // Wall clock for everything below, sampled once so one response is evaluated at one
888        // instant. chrono is built without its `clock` feature in this workspace, so `local_now`
889        // derives it from `SystemTime` the same way the control runner / ssh-policy paths do.
890        let now = local_now();
891
892        // Record control's own clock BEFORE anything reads expiry — Go `onControlTime`, delivered
893        // to the expiry manager as its own event. From here on every expiry comparison is made
894        // against control's time, not this host's, so a node with a skewed clock neither expires
895        // peers early nor misses that they expired at all.
896        if let Some(control_time) = msg.control_time {
897            let delta = self.expiry.on_control_time(control_time, now);
898            if !delta.is_zero() {
899                tracing::debug!(
900                    delta_secs = delta.num_seconds(),
901                    "control's clock differs from ours; expiry is judged against control's time"
902                );
903            }
904        }
905
906        // Remember the self node so it can be folded into the next-expiry computation below, exactly as Go
907        // folds `nm.SelfNode` into `nextPeerExpiry`. Self is never a peer and is never flagged
908        // here; the runtime's own expiry decision stays with the control runner.
909        if let Some(self_node) = msg.node.as_ref() {
910            self.self_node = Some(self_node.clone());
911        }
912
913        // Apply the standalone online/last-seen delta maps (channels C/D, `MapResponse.OnlineChange`
914        // / `PeerSeenChange`). These arrive keyed by control node id and may ride a response that
915        // carries NO `peer_update` (a bare online flip is the common case), so they must be applied
916        // *before* the no-peer-update early return — otherwise online status freezes at the last
917        // full-node/patch value. Each entry only ever *sets* a value (never back to unknown).
918        // `now` (above) is also the wall clock for a `PeerSeenChange: true` (Go uses `clock.Now()`).
919        let liveness_changed =
920            self.apply_liveness_changes(&msg.online_change, &msg.peer_seen_change, now);
921
922        if msg.peer_update.is_none() && msg.peer_patches.is_empty() {
923            // No peer set or patch, so the peer expiries are unchanged — but the self node or the
924            // clock delta may have moved, so the timer still has to be re-aimed.
925            self.rearm_expiry_timer(now, ctx.actor_ref());
926
927            // A response carrying the self node is a full netmap even without `Peers`: control
928            // omits the field when the peer list is empty (`omitempty`), so a node that may see no
929            // peers never gets a peer set at all. Its (empty) peer state is known now; don't keep
930            // peer queries waiting for a peer set that will never come.
931            if msg.node.is_some() && !self.seen_state_update {
932                self.service_pending_requests();
933                self.peer_watch.send_replace(self.status_peers());
934            }
935
936            // No peer set or patch this response. If a liveness delta still mutated the netmap,
937            // publish the refreshed snapshot so watchers (and `GetStatus`) see the new online state.
938            if liveness_changed {
939                self.service_pending_requests();
940                self.peer_watch.send_replace(self.status_peers());
941                if let Err(e) = self
942                    .env
943                    .publish(Arc::new(PeerState {
944                        upserts: HashSet::default(),
945                        deletions: HashSet::default(),
946                        peers: Arc::new(self.peer_db.clone()),
947                        silent_disco: self.silent_disco(),
948                    }))
949                    .await
950                {
951                    tracing::error!(error = %e, "publishing liveness-only peer state update");
952                }
953            }
954            return;
955        }
956
957        // Apply the whole-node peer set (if any) FIRST, then the field-level patches on top —
958        // mirroring Go's `controlclient` order (`Peers*` then `PeersChangedPatch`). A response may
959        // carry either, both, or (with a liveness-only delta) neither. Merge the upsert/deletion sets
960        // so the published `PeerState` reflects every node touched by both passes; a node both
961        // upserted by the set and patched stays in `upserts` (the patch removes it from `deletions`).
962        let (mut upserts, mut deletions) = msg
963            .peer_update
964            .as_ref()
965            .map(|u| self.apply_peer_update(u, now))
966            .unwrap_or_default();
967
968        if !msg.peer_patches.is_empty() {
969            // `apply_peer_patch_set`, not `apply_peer_patches`: control can switch this node off
970            // the incremental path with the `disable-delta-updates` node attribute, in which case
971            // the same patches are applied as a full netmap update instead of as mutations.
972            let (patch_upserts, patch_deletions) =
973                self.apply_peer_patch_set(&msg.peer_patches, now);
974            // A patch can evict a node the set just upserted (TKA rejection after key rotation), or
975            // re-admit/patch one not in the set — reconcile so each id lands in exactly one set.
976            for id in &patch_upserts {
977                deletions.remove(id);
978            }
979            for id in &patch_deletions {
980                upserts.remove(id);
981            }
982            upserts.extend(patch_upserts);
983            deletions.extend(patch_deletions);
984        }
985
986        tracing::debug!(
987            n_upsert = upserts.len(),
988            n_delete = deletions.len(),
989            peer_count = self.peer_db.peers().len(),
990            "new peer state"
991        );
992
993        // Aim the timer at the soonest expiry in the peer set this response just installed — Go
994        // `setControlClientStatusLocked`, which stops the old timer and starts a new one on every
995        // netmap. Peers already past their expiry were flagged on the way in, so what is left is
996        // strictly in the future.
997        self.rearm_expiry_timer(now, ctx.actor_ref());
998
999        self.service_pending_requests();
1000
1001        // Publish the latest peer snapshot to netmap watchers. `send_replace` keeps the receiver's
1002        // value current even when there are no subscribers, so a late subscriber sees fresh state.
1003        self.peer_watch.send_replace(self.status_peers());
1004
1005        if let Err(e) = self
1006            .env
1007            .publish(Arc::new(PeerState {
1008                upserts,
1009                deletions,
1010                peers: Arc::new(self.peer_db.clone()),
1011                silent_disco: self.silent_disco(),
1012            }))
1013            .await
1014        {
1015            tracing::error!(error = %e, "publishing peer state update");
1016        }
1017    }
1018}
1019
1020impl Message<PeerDiscoKeyAdvertisement> for PeerTracker {
1021    type Reply = ();
1022
1023    async fn handle(
1024        &mut self,
1025        msg: PeerDiscoKeyAdvertisement,
1026        _ctx: &mut Context<Self, Self::Reply>,
1027    ) {
1028        if !self.learn_disco_key(msg.peer, msg.key) {
1029            return;
1030        }
1031
1032        // The key changed, so republish: the direct-path machinery resolves a peer's disco key out
1033        // of the published `PeerState` snapshot (`direct::DiscoPeerLookup`), which is the whole
1034        // point of learning it — it is what lets disco reach this peer without waiting for a
1035        // netmap update. Go does the equivalent by writing the key straight into the magicsock
1036        // endpoint and re-keying its peer map.
1037        self.peer_watch.send_replace(self.status_peers());
1038
1039        if let Err(e) = self
1040            .env
1041            .publish(Arc::new(PeerState {
1042                upserts: HashSet::from_iter([msg.peer]),
1043                deletions: HashSet::default(),
1044                peers: Arc::new(self.peer_db.clone()),
1045                silent_disco: self.silent_disco(),
1046            }))
1047            .await
1048        {
1049            tracing::error!(error = %e, "publishing peer state after a TSMP disco-key advertisement");
1050        }
1051    }
1052}
1053
1054impl Message<DiscoKeyObserved> for PeerTracker {
1055    type Reply = ();
1056
1057    async fn handle(&mut self, msg: DiscoKeyObserved, _ctx: &mut Context<Self, Self::Reply>) {
1058        if !self.observe_disco_key(msg.peer, msg.key) {
1059            return;
1060        }
1061
1062        // The active key moved, so republish. This is the *same* channel a TSMP advertisement and a
1063        // netmap disco-key change use, and it is what makes the direct manager invalidate the
1064        // trusted path built under the old key: it diffs consecutive snapshots
1065        // (`direct::disco_key_rotations`) and calls `MagicSock::changed_active_disco` — this fork's
1066        // `endpoint.changedActiveDiscoLocked`, which Go likewise reaches from
1067        // `checkAndUpdateDiscoKey`. Keeping the switch and the invalidation on one path is why the
1068        // switch is done here rather than on the packet path that spotted it.
1069        self.peer_watch.send_replace(self.status_peers());
1070
1071        if let Err(e) = self
1072            .env
1073            .publish(Arc::new(PeerState {
1074                upserts: HashSet::from_iter([msg.peer]),
1075                deletions: HashSet::default(),
1076                peers: Arc::new(self.peer_db.clone()),
1077                silent_disco: self.silent_disco(),
1078            }))
1079            .await
1080        {
1081            tracing::error!(error = %e, "publishing peer state after a disco active-key switch");
1082        }
1083    }
1084}
1085
1086/// Internal self-message: the armed expiry timer fired — the soonest key expiry the peer set knew
1087/// about has now passed, so expiry must be re-evaluated.
1088///
1089/// This is the whole point of the timer (Go `LocalBackend.nmExpiryTimer` →
1090/// `handleNetmapExpiry`): without it a peer whose key expires between two netmaps stays fully
1091/// configured — endpoints, DERP home, live node key — until control happens to send another
1092/// response, which on a steady map poll may be a long time.
1093///
1094/// Upstream `0640312e5` had to fix this path, because the timer there closed over the netmap
1095/// captured when it was armed and reinstalling that stale copy rolled back any delta that arrived
1096/// meanwhile; the fix re-reads live peer state before reinstalling. Here the pass reads the peer db
1097/// — the live state — directly, so there is no captured copy to roll anything back.
1098#[derive(Debug, Clone, Copy)]
1099pub(crate) struct ExpiryTimerFired;
1100
1101impl Message<ExpiryTimerFired> for PeerTracker {
1102    type Reply = ();
1103
1104    async fn handle(&mut self, _msg: ExpiryTimerFired, ctx: &mut Context<Self, Self::Reply>) {
1105        let now = local_now();
1106        let upserts = self.reevaluate_expiry(now);
1107
1108        // Re-aim at the next expiry after this one, whether or not anything was flagged: a timer
1109        // that fired early (clock skew, or the slack) must not be the last one armed.
1110        self.rearm_expiry_timer(now, ctx.actor_ref());
1111
1112        if upserts.is_empty() {
1113            return;
1114        }
1115
1116        // A newly expired peer lost its endpoints, its DERP home and its node key, so the
1117        // dataplane, route updater and source filter all have to see the new snapshot — the same
1118        // publish the netmap handler does after a peer set changes.
1119        self.service_pending_requests();
1120        self.peer_watch.send_replace(self.status_peers());
1121
1122        if let Err(e) = self
1123            .env
1124            .publish(Arc::new(PeerState {
1125                upserts,
1126                deletions: HashSet::default(),
1127                peers: Arc::new(self.peer_db.clone()),
1128                silent_disco: self.silent_disco(),
1129            }))
1130            .await
1131        {
1132            tracing::error!(error = %e, "publishing peer state after a peer key expired");
1133        }
1134    }
1135}
1136
1137/// Internal self-message: the Tailnet-Lock enforcement-authority cell changed — the control runner
1138/// installed a freshly-synced [`Authority`](ts_tka::Authority) after a `/machine/tka/sync`, or
1139/// cleared it because the lock was disabled. Sent by the watch task
1140/// [`on_start`](kameo::Actor::on_start) spawns, so the peer db is re-filtered the moment enforcement
1141/// changes instead of at whatever later `Full` netmap happens to arrive.
1142#[derive(Debug, Clone, Copy)]
1143pub(crate) struct TkaAuthorityChanged;
1144
1145impl Message<TkaAuthorityChanged> for PeerTracker {
1146    type Reply = ();
1147
1148    async fn handle(&mut self, _msg: TkaAuthorityChanged, _ctx: &mut Context<Self, Self::Reply>) {
1149        let deletions = self.tka_reevaluate_peer_db();
1150        if deletions.is_empty() {
1151            // The common case: enforcement is inactive, or every admitted peer still verifies.
1152            return;
1153        }
1154
1155        // An evicted peer must lose its data path, not just its db row, so republish the snapshot
1156        // the `Arc<PeerState>` subscribers (route updater, source filter, dataplane) resolve
1157        // against — the same publish the netmap handler does after a peer set changes.
1158        self.peer_watch.send_replace(self.status_peers());
1159
1160        if let Err(e) = self
1161            .env
1162            .publish(Arc::new(PeerState {
1163                upserts: HashSet::default(),
1164                deletions,
1165                peers: Arc::new(self.peer_db.clone()),
1166                silent_disco: self.silent_disco(),
1167            }))
1168            .await
1169        {
1170            tracing::error!(error = %e, "publishing peer state after a TKA authority change");
1171        }
1172    }
1173}
1174
1175/// Ask the peer tracker to re-broadcast its current peer snapshot on the bus, without any peer
1176/// change. Sent after a runtime preference change so the route updater and source filter (both
1177/// `Arc<PeerState>` subscribers) re-resolve against the new value immediately, rather than waiting
1178/// for the next netmap update: `Device::set_exit_node` (new exit-node selector) and
1179/// `Device::set_accept_routes` (new accept-routes flag) both send it.
1180#[derive(Debug, Clone, Copy)]
1181pub struct RepublishState;
1182
1183impl Message<RepublishState> for PeerTracker {
1184    type Reply = ();
1185
1186    async fn handle(&mut self, _msg: RepublishState, _ctx: &mut Context<Self, Self::Reply>) {
1187        // An empty upsert/deletion set: this is a re-broadcast of the unchanged peer set, not a
1188        // delta. Subscribers recompute their routes/filters against the current peers and the
1189        // (just-updated) runtime preferences (exit-node selector, accept-routes flag).
1190        if let Err(e) = self
1191            .env
1192            .publish(Arc::new(PeerState {
1193                upserts: HashSet::default(),
1194                deletions: HashSet::default(),
1195                peers: Arc::new(self.peer_db.clone()),
1196                silent_disco: self.silent_disco(),
1197            }))
1198            .await
1199        {
1200            tracing::error!(error = %e, "re-publishing peer state after a runtime preference change");
1201        }
1202    }
1203}
1204
1205impl PeerTracker {
1206    /// Learn a peer's disco key from a TSMP disco-key advertisement, returning whether the
1207    /// advertisement was applied.
1208    ///
1209    /// Go [`magicsock.Conn.HandleDiscoKeyAdvertisement`], reduced to the state this fork keeps:
1210    /// Go stores the learned key on the magicsock endpoint and re-keys its peer map, whereas here
1211    /// the peer db's `disco_key` (and its disco index) *is* the live lookup every direct-path
1212    /// consumer reads. The key is recorded in the peer's [`EndpointDisco`] TSMP slot — never on top
1213    /// of control's — and the peer db then carries whichever of the two is active, so the next
1214    /// netmap cannot silently undo it ([`upsert_from_control`](Self::upsert_from_control)).
1215    ///
1216    /// The three refusals are Go's, in Go's order:
1217    ///
1218    /// 1. **A zero key is never learned.** Go checks it twice — `tstun` publishes only
1219    ///    `if !Key.IsZero()`, and `HandleDiscoKeyAdvertisement` rejects it again. The dataplane
1220    ///    already dropped it here too; this is the second check, kept because the cost of getting
1221    ///    it wrong is a peer bound to an unusable key.
1222    /// 2. **An unknown peer is ignored** (Go: "endpoint not found for node"). An advertisement
1223    ///    never creates a peer — only control does — so one that arrives before or after the
1224    ///    peer's netmap entry is a no-op, exactly like a `PeersChangedPatch` for an unknown node.
1225    /// 3. **An unchanged key is a no-op**, so a peer re-advertising the key we already hold costs
1226    ///    no upsert and no republish (Go counts this as
1227    ///    `magicsock_tsmp_disco_key_advertisement_unchanged` and returns). "Unchanged" is measured
1228    ///    against the **TSMP-learned** key (Go compares `epDisco.keyFromTSMP()`), NOT against the
1229    ///    effective one: an advertisement that merely restates what control already told us is new
1230    ///    information — it is the peer itself confirming the key — so it is recorded as the active
1231    ///    TSMP key and survives control later dropping or contradicting it.
1232    ///
1233    /// The tailnet-lock gate is deliberately *not* re-run: unlike a `PeersChangedPatch`, an
1234    /// advertisement cannot touch the node key or its TKA signature — only the disco key — so the
1235    /// peer-trust decision that admitted this node is unchanged by definition.
1236    ///
1237    /// [`magicsock.Conn.HandleDiscoKeyAdvertisement`]: https://github.com/tailscale/tailscale/blob/49e148c4a30b4f8098f69468fd27a7021d85ea02/wgengine/magicsock/magicsock.go
1238    fn learn_disco_key(&mut self, peer: PeerId, key: DiscoPublicKey) -> bool {
1239        if disco_key_is_zero(&key) {
1240            tracing::debug!(?peer, "TSMP-advertised disco key is the zero key; ignoring");
1241            return false;
1242        }
1243
1244        let Some((_id, existing)) = self.peer_db.get(&peer) else {
1245            tracing::debug!(
1246                ?peer,
1247                "TSMP disco-key advertisement for unknown peer; ignoring"
1248            );
1249            return false;
1250        };
1251
1252        let node_key = existing.node_key;
1253        if self
1254            .endpoint_disco
1255            .get(&node_key)
1256            .and_then(EndpointDisco::key_from_tsmp)
1257            == Some(key)
1258        {
1259            tracing::trace!(?peer, "TSMP-advertised disco key is unchanged");
1260            return false;
1261        }
1262
1263        let node = existing.clone();
1264        let disco = self.endpoint_disco.entry(node_key).or_default();
1265        disco.update_from_tsmp(Some(key));
1266        let disco = *disco;
1267        self.store_disco(&node, disco);
1268
1269        tracing::info!(
1270            ?peer,
1271            stable_id = ?node.stable_id,
1272            %key,
1273            "learned peer disco key from a TSMP advertisement"
1274        );
1275
1276        true
1277    }
1278
1279    /// Write a peer's resolved disco state onto the peer db.
1280    ///
1281    /// The node lands carrying the **effective** key ([`EndpointDisco::key`]), which is what the
1282    /// disco index — and so every *send* path — resolves against, and the peer's other known key
1283    /// (if any) is registered as its inactive ingress key so a frame arriving under it still
1284    /// attributes to this peer ([`PeerDb::peer_by_known_disco_key`]).
1285    ///
1286    /// Every disco-key writer goes through here — control, a TSMP advertisement, and an
1287    /// active-slot switch on receive — so the two cannot drift apart on which key is which.
1288    fn store_disco(&mut self, node: &Node, disco: EndpointDisco) -> PeerId {
1289        let effective = disco.key();
1290
1291        let id = if effective == node.disco_key {
1292            self.peer_db.upsert(node)
1293        } else {
1294            let mut node = node.clone();
1295            node.disco_key = effective;
1296            self.peer_db.upsert(&node)
1297        };
1298
1299        self.peer_db
1300            .set_inactive_disco_key(id, disco.inactive_key());
1301
1302        id
1303    }
1304
1305    /// Apply the sender key of an inbound disco frame to this peer's two-slot disco state — the
1306    /// `ts_runtime` half of Go [`endpoint.checkAndUpdateDiscoKey`].
1307    ///
1308    /// A peer mid-rotation keeps sending disco under the key it has not yet switched away from.
1309    /// Upstream accepts either of the two keys it knows for the peer and, when the one received is
1310    /// the currently-inactive one, makes it active: receiving under a key is proof of what the peer
1311    /// is using, and is stronger evidence than what control last said. Without this a rotation
1312    /// costs the peer its direct path until control catches up or the peer re-advertises.
1313    ///
1314    /// Returns whether the active key changed, so the caller can republish — which is how the
1315    /// direct manager learns to invalidate the trusted path built under the old key (Go's
1316    /// `changedActiveDiscoLocked`, reached here through the same snapshot diff every other
1317    /// disco-key transition uses).
1318    ///
1319    /// The refusals, all of which leave the peer db untouched:
1320    ///
1321    /// 1. **An unknown peer**, exactly as for a TSMP advertisement.
1322    /// 2. **A peer with no disco key material at all** (Go: `epDisco == nil` ⇒ `false`).
1323    /// 3. **A key belonging to neither slot.** This is the one that carries the security value:
1324    ///    a peer must not be able to move itself onto a key nobody told us about, so a third key
1325    ///    is refused even though the frame that carried it opened correctly.
1326    ///
1327    /// [`endpoint.checkAndUpdateDiscoKey`]: https://github.com/tailscale/tailscale/blob/9ea7cba44591e0cd840c6c94d23274dd222059bf/wgengine/magicsock/endpoint.go
1328    fn observe_disco_key(&mut self, peer: PeerId, key: DiscoPublicKey) -> bool {
1329        let Some((_id, existing)) = self.peer_db.get(&peer) else {
1330            tracing::debug!(?peer, "disco received for an unknown peer; ignoring");
1331            return false;
1332        };
1333
1334        let node = existing.clone();
1335        let Some(disco) = self.endpoint_disco.get_mut(&node.node_key) else {
1336            // Go's `epDisco == nil`: the peer has no key from either source, so there is nothing
1337            // this key could match and nothing to switch to.
1338            tracing::debug!(
1339                ?peer,
1340                "disco received for a peer with no known disco key; ignoring"
1341            );
1342            return false;
1343        };
1344
1345        let Some(changed) = disco.check_and_update(key) else {
1346            tracing::debug!(
1347                ?peer,
1348                %key,
1349                "refusing disco under a key that is neither of the peer's known disco keys"
1350            );
1351            return false;
1352        };
1353
1354        if !changed {
1355            return false;
1356        }
1357
1358        let disco = *disco;
1359        self.store_disco(&node, disco);
1360
1361        tracing::info!(
1362            ?peer,
1363            stable_id = ?node.stable_id,
1364            %key,
1365            "peer is sending disco under its other known key; making that key active"
1366        );
1367
1368        true
1369    }
1370
1371    /// Upsert a control-sourced [`Node`] into the peer db, resolving its disco key against anything
1372    /// this peer has told us over TSMP first.
1373    ///
1374    /// Every node built from control goes through here — `Full`, `Delta { upsert }`, and a
1375    /// `PeersChangedPatch` — so the three cannot diverge on which of the two keys wins. This is the
1376    /// disco half of Go [`endpoint.updateFromNode`]: control's key is written through
1377    /// [`EndpointDisco::update_from_control`] **only when it differs from what control last said**
1378    /// (Go's `if discoKey != n.DiscoKey()` guard, which compares `keyFromControl()`, never the
1379    /// effective key). So a netmap that merely restates the key control already sent leaves an
1380    /// active TSMP key alone — which is the entire point of the advertisement, whose motivating case
1381    /// is a peer whose key control has not caught up with. Control genuinely changing its mind is
1382    /// *recorded* in control's slot, but it does not take the active slot back from a TSMP-learned
1383    /// key: upstream switches back only when disco is received under control's key
1384    /// (`endpoint.checkAndUpdateDiscoKey`). See [`EndpointDisco::update_from_control`].
1385    ///
1386    /// The node lands in the db carrying the *effective* key ([`EndpointDisco::key`]), so the disco
1387    /// index and every send path resolve against the key we would actually send to; the other known
1388    /// key is registered for ingress attribution ([`store_disco`](Self::store_disco)).
1389    ///
1390    /// [`endpoint.updateFromNode`]: https://github.com/tailscale/tailscale/blob/49e148c4a30b4f8098f69468fd27a7021d85ea02/wgengine/magicsock/endpoint.go
1391    fn upsert_from_control(&mut self, node: &Node, now: chrono::DateTime<chrono::Utc>) -> PeerId {
1392        // The expiry pass, at the one site every peer install funnels through — Go
1393        // `flagExpiredPeers`, which runs over the whole netmap on the way in. A peer whose key
1394        // expiry has passed (judged against CONTROL's clock) is rewritten, never dropped: it keeps
1395        // its identity so `whois`, `status` and a peerAPI dial can all say *why* it is unreachable,
1396        // but it loses its endpoints, its home DERP and its usable node key. `None` is the ordinary
1397        // case — no transition — and costs no clone.
1398        let flagged = self.expiry.flag_expired_peer(node, now);
1399        // Log the transition, not the rewrite: control restates an expired peer unflagged on every
1400        // full netmap, so without this the line (and the reader's alarm) would repeat forever.
1401        if let Some(flagged) = flagged.as_ref().filter(|f| f.first_transition) {
1402            if flagged.peer.expired {
1403                tracing::info!(
1404                    stable_id = ?flagged.peer.stable_id,
1405                    "peer's node key has expired; clearing its endpoints and DERP home and \
1406                     breaking its node key"
1407                );
1408            } else {
1409                tracing::info!(
1410                    stable_id = ?flagged.peer.stable_id,
1411                    "peer's node-key expiry was extended; restoring its node key"
1412                );
1413            }
1414        }
1415        let node = flagged.as_ref().map_or(node, |flagged| &flagged.peer);
1416
1417        let node_key = node.node_key;
1418        let from_control = disco_key_from_control(node.disco_key);
1419
1420        let disco = self.endpoint_disco.entry(node_key).or_default();
1421        if disco.key_from_control() != from_control {
1422            disco.update_from_control(from_control);
1423        }
1424        let disco = *disco;
1425
1426        // No key material from either source: Go nils the endpoint's `disco` pointer, so a peer
1427        // that has never had a disco key costs us no entry either.
1428        if disco.is_empty() {
1429            self.endpoint_disco.remove(&node_key);
1430        }
1431
1432        self.store_disco(node, disco)
1433    }
1434
1435    /// The disco key control last gave us for `node_key` — Go `endpointDisco.keyFromControl()`.
1436    fn control_disco_key(&self, node_key: &NodePublicKey) -> Option<DiscoPublicKey> {
1437        self.endpoint_disco
1438            .get(node_key)
1439            .and_then(EndpointDisco::key_from_control)
1440    }
1441
1442    /// Drop [`EndpointDisco`] state for node keys the peer db no longer holds.
1443    ///
1444    /// Go gets this for free: the two keys live on the magicsock `endpoint`, which the peer map keys
1445    /// by node key and deletes when the peer leaves the netmap — and a peer that rotates its node
1446    /// key gets a brand-new endpoint, so a TSMP-learned key is not carried across a rotation. Here
1447    /// the state is a side table, so every control update prunes it to get the same lifetime.
1448    fn prune_endpoint_disco(&mut self) {
1449        if self.endpoint_disco.is_empty() {
1450            return;
1451        }
1452
1453        let peers = &self.peer_db;
1454        self.endpoint_disco
1455            .retain(|node_key, _| peers.has(node_key).is_some());
1456    }
1457
1458    /// Apply a single [`PeerUpdate`](ts_control::PeerUpdate) to the peer db, enforcing the
1459    /// Tailnet-Lock peer-trust chokepoint ([`tka_admits`](Self::tka_admits)) at every upsert site.
1460    ///
1461    /// This is the **single source of truth** for the peer-trust enforcement loop: the actor's
1462    /// netmap [`handle`](Message::handle) calls it, and so do the TKA enforcement tests, so the two
1463    /// real upsert sites (`Full` and `Delta { upsert }`) cannot diverge from what is tested.
1464    ///
1465    /// `now` is the local wall clock the expiry pass in
1466    /// [`upsert_from_control`](Self::upsert_from_control) judges against (after correction for
1467    /// control's clock); it is threaded in rather than read per peer so one netmap is evaluated at
1468    /// one instant.
1469    ///
1470    /// Returns `(upserts, deletions)` — the [`PeerId`]s touched — for downstream bookkeeping.
1471    fn apply_peer_update(
1472        &mut self,
1473        peer_update: &ts_control::PeerUpdate,
1474        now: chrono::DateTime<chrono::Utc>,
1475    ) -> (HashSet<PeerId>, HashSet<PeerId>) {
1476        let mut upserts = HashSet::default();
1477        let mut deletions = HashSet::default();
1478
1479        match peer_update {
1480            ts_control::PeerUpdate::Full(new_nodes) => {
1481                tracing::trace!("full peer update");
1482
1483                // Borrow the authority ONCE for the whole batch and verify each peer EXACTLY once
1484                // (Go runs `tkaFilterNetmapLocked` once over the assembled netmap; an earlier draft
1485                // verified every peer twice — once for `retained_ids`, once in the upsert loop —
1486                // doubling the ed25519 cost on the hot resync path). `tka_keep_verdicts` is that one
1487                // pass — per-peer signature verdict AND the cross-peer rotation filter — and is
1488                // shared verbatim with `tka_reevaluate_peer_db`, so the netmap path and the
1489                // authority-install path cannot drift apart on what "admitted" means.
1490                //
1491                // The result is a per-NODE keep vector (not a stable_id set), which drives both the
1492                // `retain` (evict revoked peers, keyed by stable_id) and the upsert loop. Judging
1493                // each node by its own verdict means a node whose signature fails is never admitted
1494                // on the strength of a different node that happens to share its stable_id.
1495                //
1496                // Revocation evicts: a peer re-included with a now-invalid/missing signature under an
1497                // active authority fails its verdict, so it is excluded from `retained_ids` and
1498                // `retain` drops the stale (previously-admitted) entry. With no authority the snapshot
1499                // is `None`, so every node passes — byte-for-byte the pre-TKA behavior (no regression).
1500                let authority = self.tka_authority_snapshot();
1501                let node_refs = new_nodes.iter().collect::<Vec<&Node>>();
1502                let keep = Self::tka_keep_verdicts(authority.as_deref(), &node_refs);
1503
1504                // `retained_ids` is the set of stable_ids that survive (drives `retain` to evict the
1505                // rest). It must agree with what the upsert loop below will leave in the db. Control
1506                // should never send two distinct nodes with the same `stable_id` in one `Full`, but if
1507                // it does, `peer_db.upsert` is last-writer-wins on `stable_id`, so the db ends holding
1508                // the LAST kept node for that id. Build `retained_ids` from kept nodes only — a
1509                // stable_id is retained iff at least one of its (possibly duplicate) nodes is kept, so
1510                // the upsert loop's last-kept node lands and `retain` never evicts a just-upserted id.
1511                let retained_ids = new_nodes
1512                    .iter()
1513                    .zip(keep.iter().copied())
1514                    .filter(|(_, k)| *k)
1515                    .map(|(node, _)| &node.stable_id)
1516                    .collect::<HashSet<_>>();
1517
1518                // Isolation diagnostic: an ACTIVE lock that authorized none of the offered peers
1519                // leaves this node with no peers — surface it loudly so a self-lockout (vs an attack)
1520                // is diagnosable. `authority.is_some()` means a real keyed lock (the empty-keyset
1521                // brick-guard admits-all, so it never reaches here with zero retained).
1522                if authority.is_some() && !new_nodes.is_empty() && retained_ids.is_empty() {
1523                    tracing::error!(
1524                        offered = new_nodes.len(),
1525                        "TKA: active lock authorized ZERO of the offered peers; node is isolated \
1526                         (verify the lock state, or disable tailnet lock to recover)"
1527                    );
1528                }
1529
1530                self.peer_db.retain(|id, peer| {
1531                    let retain = retained_ids.contains(&peer.stable_id);
1532
1533                    if !retain {
1534                        deletions.insert(id);
1535                    }
1536
1537                    retain
1538                });
1539
1540                for (node, k) in new_nodes.iter().zip(keep.iter().copied()) {
1541                    if !k {
1542                        continue; // fail-CLOSED: rejected by tailnet lock or rotation-obsolete (above)
1543                    }
1544                    let peer_id = self.upsert_from_control(node, now);
1545                    upserts.insert(peer_id);
1546                }
1547            }
1548
1549            ts_control::PeerUpdate::Delta { remove, upsert } => {
1550                tracing::trace!("delta peer update");
1551
1552                for peer in upsert {
1553                    if !self.tka_admits(peer) {
1554                        // fail-CLOSED: do not upsert a peer rejected by tailnet lock. If the peer is
1555                        // ALREADY in the db (a delta re-upserting an existing peer whose signature is
1556                        // now invalid — e.g. revoked between syncs), evict the stale entry rather than
1557                        // leaving an unverified peer admitted; Go re-filters the whole netmap each map
1558                        // response, so a now-unsigned peer would not survive there either.
1559                        if let Some((id, _)) = self.peer_db.remove(&peer.stable_id) {
1560                            tracing::warn!(
1561                                stable_id = ?peer.stable_id,
1562                                "TKA: delta re-upsert rejected; evicting now-unauthorized peer"
1563                            );
1564                            deletions.insert(id);
1565                        }
1566                        continue;
1567                    }
1568                    let id = self.upsert_from_control(peer, now);
1569
1570                    upserts.insert(id);
1571                }
1572
1573                for peer in remove {
1574                    let Some((id, _node)) = self.peer_db.remove(peer) else {
1575                        // A benign, expected race: the peer may already be gone (dropped in a prior
1576                        // `Full`, or fail-closed by TKA — whose now-"unknown" ids commonly reappear in
1577                        // a trailing `peers_removed`). Go treats an unknown removal as a no-op; log at
1578                        // debug, not error, to avoid false-alarm noise on a healthy node (matches the
1579                        // unknown-node handling in `apply_peer_patches`).
1580                        tracing::debug!(
1581                            control_node_id = peer,
1582                            "removed peer was unknown; ignoring"
1583                        );
1584                        continue;
1585                    };
1586
1587                    deletions.insert(id);
1588                }
1589            }
1590        }
1591
1592        self.prune_endpoint_disco();
1593
1594        (upserts, deletions)
1595    }
1596
1597    /// Re-run the Tailnet-Lock filter over the peers **already in the peer db**, evicting the ones
1598    /// the current authority does not admit. Returns the evicted [`PeerId`]s (empty when nothing
1599    /// changed, which is the overwhelmingly common case).
1600    ///
1601    /// # Why this exists (a Go-ordering gap, not an extra feature)
1602    /// Go filters the very netmap that announced the lock: `SetControlClientStatus`
1603    /// (`ipn/ipnlocal/local.go`, v1.100.0) calls `tkaSyncIfNeeded` and then, a few lines later,
1604    /// `tkaFilterNetmapLocked(st.NetMap)` — synchronously, on the same `st.NetMap`, in one pass. So
1605    /// the peers announced alongside `TKAEnabled` are checked by the authority that sync just built.
1606    ///
1607    /// Here the sync is a spawned task (`control_runner`'s `maybe_sync_tka`), so the ordering is
1608    /// inverted: the netmap that carried the `TkaStatus` reaches the peer db *before* the authority
1609    /// exists, and is admitted with enforcement inactive. Without this pass those peers stay
1610    /// admitted — unauthorized ones included — until control happens to send another `Full`, which on
1611    /// a steady map poll may be never. That is the whole initial peer set escaping a lock the node
1612    /// really did sync, so this runs the moment the authority is installed ([`TkaAuthorityChanged`])
1613    /// and brings the db back in line.
1614    ///
1615    /// No authority (nothing synced yet, or the lock was disabled) ⇒ no eviction: enforcement is
1616    /// inactive and every peer is admitted, exactly Go's `b.tka == nil` early return. A peer dropped
1617    /// while the lock was active is **not** resurrected by a later disable — the db no longer holds
1618    /// it and this fork keeps no shadow copy of filtered nodes (Go's `b.tka.filtered`); it returns on
1619    /// the next netmap that re-includes it. That is the safe direction: more restrictive, and
1620    /// connectivity-only.
1621    fn tka_reevaluate_peer_db(&mut self) -> HashSet<PeerId> {
1622        let Some(authority) = self.tka_authority_snapshot() else {
1623            return HashSet::default();
1624        };
1625
1626        // Verdicts first, under an immutable borrow of the db; the eviction below needs `&mut`.
1627        let evicted: HashSet<PeerId> = {
1628            let entries = self
1629                .peer_db
1630                .peers()
1631                .iter()
1632                // A peer this node already flagged expired is skipped: its node key is one WE
1633                // broke (`ExpiryManager::flag_expired_peer`), so re-verifying control's signature
1634                // against it would be checking our own mutation, and the peer would always be
1635                // evicted. It was admitted by the lock when it was installed, against the real key
1636                // control sent, and it has had no usable key since — so keeping the row costs no
1637                // trust and preserves the thing expiry flagging exists for: an expired peer is
1638                // FLAGGED, not dropped, so a caller can say why it is unreachable.
1639                .filter(|(_, node)| !node.expired)
1640                .map(|(id, node)| (*id, node))
1641                .collect::<Vec<(PeerId, &Node)>>();
1642            let nodes = entries
1643                .iter()
1644                .map(|(_, node)| *node)
1645                .collect::<Vec<&Node>>();
1646            let keep = Self::tka_keep_verdicts(Some(&authority), &nodes);
1647            entries
1648                .iter()
1649                .zip(keep)
1650                .filter_map(|((id, _), keep)| (!keep).then_some(*id))
1651                .collect()
1652        };
1653
1654        if evicted.is_empty() {
1655            return evicted;
1656        }
1657
1658        tracing::warn!(
1659            n_evicted = evicted.len(),
1660            peer_count = self.peer_db.peers().len(),
1661            "TKA: re-filtered the peer db against the newly installed lock authority; evicted \
1662             already-admitted peers"
1663        );
1664        self.peer_db.retain(|id, _| !evicted.contains(&id));
1665        self.prune_endpoint_disco();
1666        evicted
1667    }
1668
1669    /// Re-run the expiry pass over the peers **already in the peer db**, returning the [`PeerId`]s
1670    /// whose node changed (empty when nothing expired, the overwhelmingly common case).
1671    ///
1672    /// The timer's counterpart to the pass [`upsert_from_control`](Self::upsert_from_control) runs
1673    /// on the way in: that one catches a peer that was already expired when control handed it to
1674    /// us, this one catches a peer that expires while we sit on the same netmap.
1675    ///
1676    /// Only peers that actually transition are cloned and re-installed — the pass returns `None`
1677    /// for the rest — so a timer firing over a large peer set costs one walk and a handful of
1678    /// upserts. Re-installing goes through the ordinary upsert path so the node-key index follows
1679    /// the peer's now-broken key; the pass there is a no-op on an already-flagged peer.
1680    fn reevaluate_expiry(&mut self, now: chrono::DateTime<chrono::Utc>) -> HashSet<PeerId> {
1681        let flagged = self
1682            .peer_db
1683            .peers()
1684            .values()
1685            .filter_map(|peer| self.expiry.flag_expired_peer(peer, now))
1686            .collect::<Vec<ts_control::FlaggedPeer>>();
1687
1688        if flagged.is_empty() {
1689            return HashSet::default();
1690        }
1691
1692        tracing::info!(
1693            n = flagged.len(),
1694            "netmap expiry timer fired; peers whose node keys expired between netmaps"
1695        );
1696
1697        let mut upserts = HashSet::default();
1698        for flagged in &flagged {
1699            // `upsert_from_control` re-runs the pass, which is a no-op on the peer it just
1700            // rewrote — so the log line belongs here, where the transition is known.
1701            if flagged.first_transition && flagged.peer.expired {
1702                tracing::info!(
1703                    stable_id = ?flagged.peer.stable_id,
1704                    "peer's node key has expired; clearing its endpoints and DERP home and \
1705                     breaking its node key"
1706                );
1707            }
1708            upserts.insert(self.upsert_from_control(&flagged.peer, now));
1709        }
1710        self.prune_endpoint_disco();
1711
1712        upserts
1713    }
1714
1715    /// Whether control has asked this node to stop heartbeating its peers — the `silent-disco`
1716    /// attribute on the self node, published with every [`PeerState`] snapshot.
1717    ///
1718    /// Read off the self node this tracker last saw, which is the one the current response
1719    /// refreshed, so an attribute control grants takes effect on the netmap that granted it (the
1720    /// timing `delta_updates_disabled` already has here). No self node yet ⇒ `false` ⇒ the existing
1721    /// heartbeat cadence, which is the safe default: the cost of heartbeating a node control wanted
1722    /// quiet is a ping every 2s, while the cost of going quiet on a guess is a direct path falling
1723    /// back to DERP.
1724    fn silent_disco(&self) -> bool {
1725        self.self_node.as_ref().is_some_and(Node::silent_disco)
1726    }
1727
1728    /// Stop the armed expiry timer and arm a new one for the soonest future key expiry across the
1729    /// peer db and the self node — Go `setControlClientStatusLocked`'s `nmExpiryTimer` block.
1730    ///
1731    /// No future expiry (every peer tagged or already flagged, and no self expiry) leaves no timer
1732    /// armed; the next netmap re-decides. The delay carries upstream's
1733    /// [`EXPIRY_TIMER_SLACK_SECS`](ts_control::EXPIRY_TIMER_SLACK_SECS) of slack so the key is
1734    /// unambiguously past its expiry by the time the pass runs.
1735    ///
1736    /// The old timer is **aborted**, which is this fork's version of Go's `numClientStatusCalls`
1737    /// generation check: a task that has already been dropped cannot deliver a stale wake-up. The
1738    /// spawned task holds only a `WeakActorRef`, so it can never keep the tracker's mailbox alive
1739    /// past shutdown.
1740    fn rearm_expiry_timer(&mut self, now: chrono::DateTime<chrono::Utc>, slf: &ActorRef<Self>) {
1741        if let Some(timer) = self.expiry_timer.take() {
1742            timer.abort();
1743        }
1744
1745        let Some(next) = self.expiry.next_peer_expiry(
1746            self.peer_db.peers().values(),
1747            self.self_node.as_ref(),
1748            now,
1749        ) else {
1750            return;
1751        };
1752
1753        let delay = (next - now) + chrono::TimeDelta::seconds(ts_control::EXPIRY_TIMER_SLACK_SECS);
1754        // `next` is never before `now` (the expiry manager floors it), so the conversion holds; a
1755        // negative delta would only mean "fire immediately", which is also the safe reading.
1756        let delay = delay.to_std().unwrap_or(std::time::Duration::ZERO);
1757
1758        tracing::debug!(
1759            delay_secs = delay.as_secs(),
1760            "arming the netmap expiry timer for the next node-key expiry"
1761        );
1762
1763        let notify = slf.downgrade();
1764        self.expiry_timer = Some(tokio::spawn(async move {
1765            tokio::time::sleep(delay).await;
1766            let Some(tracker) = notify.upgrade() else {
1767                return; // the peer tracker is gone; nothing left to re-evaluate
1768            };
1769            if let Err(e) = tracker.tell(ExpiryTimerFired).await {
1770                tracing::debug!(error = %e, "peer tracker stopped before the expiry timer fired");
1771            }
1772        }));
1773    }
1774
1775    /// Apply the response's `MapResponse.PeersChangedPatch` set, choosing the incremental path or
1776    /// the fall-back-to-full one according to control's `disable-delta-updates` node attribute.
1777    ///
1778    /// This is the port of the first statement of Go `control/controlclient/map.go`'s
1779    /// `tryHandleIncrementally` — `if ms.controlKnobs != nil &&
1780    /// ms.controlKnobs.DisableDeltaUpdates.Load() { return false }` — and of what returning `false`
1781    /// there means: the map session does not reject the response and does not drop the mutations it
1782    /// carries, it declines the incremental arm so the full netmap rebuild handles the *same*
1783    /// response. So a patch-only response under the attribute is still applied, as a full update.
1784    ///
1785    /// The attribute is read off the **self** node, which the netmap handler refreshes from this
1786    /// very response before it gets here, so an attribute control granted on this response takes
1787    /// effect on the response that granted it — the same timing the netmap cache's attribute read
1788    /// has. No self node yet (nothing has carried one) reads as absent ⇒ the incremental path.
1789    fn apply_peer_patch_set(
1790        &mut self,
1791        patches: &[ts_control::PeerChange],
1792        now: chrono::DateTime<chrono::Utc>,
1793    ) -> (HashSet<PeerId>, HashSet<PeerId>) {
1794        if self
1795            .self_node
1796            .as_ref()
1797            .is_some_and(Node::delta_updates_disabled)
1798        {
1799            tracing::debug!(
1800                n = patches.len(),
1801                "control set disable-delta-updates; applying this response's peer patches as a \
1802                 full netmap update"
1803            );
1804            return self.rebuild_netmap_with_patches(patches, now);
1805        }
1806
1807        self.apply_peer_patches(patches, now)
1808    }
1809
1810    /// The fall-back-to-full arm of [`apply_peer_patch_set`](Self::apply_peer_patch_set): fold the
1811    /// patches in, then re-install the **whole** retained netmap rather than the patched nodes
1812    /// alone.
1813    ///
1814    /// Go reaches the same place by a different route because its map session keeps its own peer
1815    /// store: `HandleNonKeepAliveMapResponse` absorbs `PeersChangedPatch` into that store
1816    /// (`updateStateFromResponse`) *before* it decides how to hand the result downstream, then —
1817    /// when `tryHandleIncrementally` declines — rebuilds the netmap from the store (`ms.netmap()`,
1818    /// which re-runs `flagExpiredPeers` over every peer) and installs it whole with
1819    /// `UpdateFullNetmap`. Here the peer db *is* that store, so step one is the ordinary patch fold
1820    /// and step two is re-installing every peer through the same control-sourced upsert path, which
1821    /// is what re-runs the expiry pass over the whole netmap and puts every peer in the published
1822    /// upsert set. Upstream names the cost itself — "lots of garbage & work downstream" — and it is
1823    /// the point of the escape hatch, not a side effect of it.
1824    ///
1825    /// Nothing here evicts a peer the incremental path would have kept: the only trust gate is the
1826    /// per-patched-node one the fold already runs. Re-verifying the *whole* db against tailnet lock
1827    /// (Go's full arm re-runs `tkaFilterNetmapLocked`) is deliberately NOT done, because the nodes
1828    /// in the db are this node's own copies rather than control's pristine ones — a peer this node
1829    /// flagged expired carries a node key we broke ourselves, so its signature can no longer verify
1830    /// and re-filtering would evict it. That is the same carve-out, for the same reason, that
1831    /// [`tka_reevaluate_peer_db`](Self::tka_reevaluate_peer_db) already makes.
1832    fn rebuild_netmap_with_patches(
1833        &mut self,
1834        patches: &[ts_control::PeerChange],
1835        now: chrono::DateTime<chrono::Utc>,
1836    ) -> (HashSet<PeerId>, HashSet<PeerId>) {
1837        let (mut upserts, deletions) = self.apply_peer_patches(patches, now);
1838
1839        // Re-install the netmap the fold above just produced, whole. Cloning first keeps the db
1840        // borrow off the upsert loop; it is the "garbage" half of upstream's own description.
1841        let netmap: Vec<Node> = self.peer_db.peers().values().cloned().collect();
1842        for mut node in netmap {
1843            // The db carries the EFFECTIVE disco key, which may have been learned over TSMP, so
1844            // restate what CONTROL last said before re-installing. Without this, re-installing a
1845            // peer whose active key came from a TSMP advertisement would hand that key back as if
1846            // control had sent it, and `upsert_from_control` would write it into control's slot —
1847            // losing the key control actually gave us. Same reasoning as the patch fold's own
1848            // restatement, applied to every peer because every peer is re-installed here.
1849            node.disco_key = self.control_disco_key(&node.node_key);
1850            upserts.insert(self.upsert_from_control(&node, now));
1851        }
1852
1853        (upserts, deletions)
1854    }
1855
1856    /// Apply field-level peer patches (`MapResponse.PeersChangedPatch`), returning the upserted /
1857    /// deleted [`PeerId`]s. The incremental arm of
1858    /// [`apply_peer_patch_set`](Self::apply_peer_patch_set), and the fold both arms share.
1859    ///
1860    /// This is a SEPARATE channel from [`apply_peer_update`](Self::apply_peer_update): Go's
1861    /// `controlclient` applies the whole-node `Peers*` set first and then `PeersChangedPatch`, so a
1862    /// response that carries both has the peer set applied first (by the caller) and these patches
1863    /// applied second, on top of the freshly-synced nodes. A patch only mutates a peer already in the
1864    /// netmap; an unknown node id is ignored (the wire contract — a patch never creates a node).
1865    fn apply_peer_patches(
1866        &mut self,
1867        patches: &[ts_control::PeerChange],
1868        now: chrono::DateTime<chrono::Utc>,
1869    ) -> (HashSet<PeerId>, HashSet<PeerId>) {
1870        let mut upserts = HashSet::default();
1871        let mut deletions = HashSet::default();
1872
1873        tracing::trace!(n = patches.len(), "peer patch update");
1874
1875        for patch in patches {
1876            // Clone the current node, apply the present fields, and re-upsert through the same path
1877            // as a delta so indexes/routes stay consistent.
1878            let Some((_id, existing)) = self.peer_db.get(&patch.id) else {
1879                tracing::debug!(
1880                    control_node_id = patch.id,
1881                    "peer patch for unknown node; ignoring"
1882                );
1883                continue;
1884            };
1885
1886            let mut node = existing.clone();
1887            if let Some(endpoints) = &patch.underlay_addresses {
1888                node.underlay_addresses = endpoints.clone();
1889            }
1890            if let Some(derp) = patch.derp_region {
1891                node.derp_region = Some(derp);
1892            }
1893            if let Some(cap) = patch.cap {
1894                node.cap = cap;
1895            }
1896            if let Some(cap_map) = &patch.cap_map {
1897                node.cap_map = cap_map.clone();
1898            }
1899            // The db entry carries the EFFECTIVE disco key, which may have been learned over TSMP,
1900            // so restate what CONTROL last said before folding the patch in. Otherwise a patch that
1901            // says nothing about the disco key would hand a TSMP-learned key back as if control had
1902            // sent it, and `upsert_from_control` would write it into control's slot — losing the key
1903            // control actually gave us, on a patch that never mentioned the disco key at all.
1904            node.disco_key = self.control_disco_key(&node.node_key);
1905            if let Some(disco_key) = patch.disco_key {
1906                node.disco_key = Some(disco_key);
1907            }
1908            if let Some(expiry) = patch.node_key_expiry {
1909                node.node_key_expiry = Some(expiry);
1910            }
1911            // Online/last-seen liveness deltas (`PeerChange.Online`/`LastSeen`) — the dominant
1912            // channel by which peer online transitions arrive mid-session. A patch only ever *sets*
1913            // a value (never patches back to unknown), so apply when present.
1914            if let Some(online) = patch.online {
1915                node.online = Some(online);
1916            }
1917            if let Some(last_seen) = patch.last_seen {
1918                node.last_seen = Some(last_seen);
1919            }
1920            // Key rotation: a patch may swap the node key (and its TKA signature). Apply both
1921            // together so the trust gate below verifies the new signature against the new key, never
1922            // a mismatched pair.
1923            if let Some(node_key) = patch.node_key {
1924                node.node_key = node_key;
1925                // Control restated the key, so this node's own break of it (if the peer had
1926                // expired) no longer applies: clear the client-set flag and let the expiry pass in
1927                // `upsert_from_control` decide again against the (possibly also patched) expiry.
1928                // Go gets this for free — it patches a pristine node and re-runs `flagExpiredPeers`
1929                // over the result.
1930                node.expired = false;
1931            }
1932            if let Some(sig) = &patch.key_signature {
1933                node.key_signature = sig.clone();
1934            }
1935
1936            // Re-run the tailnet-lock gate on the patched node: a patch that rotates the key must
1937            // satisfy the active authority, exactly like a `Delta` upsert, or it would be a
1938            // trust-enforcement bypass. fail-CLOSED — if the patched node is no longer admitted,
1939            // evict it rather than keep the stale (now-unverified) entry.
1940            if !self.tka_admits(&node) {
1941                if let Some((id, _)) = self.peer_db.remove(&patch.id) {
1942                    tracing::warn!(
1943                        control_node_id = patch.id,
1944                        "peer patch rejected by tailnet lock; evicting peer"
1945                    );
1946                    deletions.insert(id);
1947                }
1948                continue;
1949            }
1950
1951            let id = self.upsert_from_control(&node, now);
1952            upserts.insert(id);
1953        }
1954
1955        self.prune_endpoint_disco();
1956
1957        (upserts, deletions)
1958    }
1959
1960    /// Apply the standalone online/last-seen delta maps (`MapResponse.OnlineChange` /
1961    /// `PeerSeenChange`, channels C/D) onto the retained netmap. Returns `true` if any node was
1962    /// actually mutated (so the caller knows whether to re-publish).
1963    ///
1964    /// Mirrors Go `controlclient/map.go:updatePeersStateFromResponse` (the two channels are
1965    /// semantically DISTINCT and must not be conflated):
1966    /// - `OnlineChange` (channel C) is the sole driver of a peer's `online` flag (`mut.Online = v`).
1967    /// - `PeerSeenChange` (channel D) is the sole driver of `last_seen`: `true ⇒ LastSeen = now`,
1968    ///   `false ⇒ LastSeen = nil` (cleared). It NEVER touches `online` — "not seen recently" is not
1969    ///   the same as "offline", which only `OnlineChange` asserts.
1970    ///
1971    /// Each entry is keyed by control node id and applies to a peer already in the netmap; an unknown
1972    /// node id is ignored (these maps never create a node). `now` is the wall-clock timestamp for a
1973    /// `PeerSeenChange: true` (Go uses `clock.Now()`); the caller passes it so this stays a pure
1974    /// function of its inputs. Returns `true` if any node was actually mutated.
1975    fn apply_liveness_changes(
1976        &mut self,
1977        online_change: &std::collections::BTreeMap<ts_control::NodeId, bool>,
1978        peer_seen_change: &std::collections::BTreeMap<ts_control::NodeId, bool>,
1979        now: chrono::DateTime<chrono::Utc>,
1980    ) -> bool {
1981        let mut changed = false;
1982
1983        // Channel C — direct online flips (the only writer of `online`).
1984        for (&node_id, &online) in online_change {
1985            if let Some((_pid, existing)) = self.peer_db.get(&node_id)
1986                && existing.online != Some(online)
1987            {
1988                let mut node = existing.clone();
1989                node.online = Some(online);
1990                self.peer_db.upsert(&node);
1991                changed = true;
1992            }
1993        }
1994
1995        // Channel D — peer-seen flips (the only writer of `last_seen`; never touches `online`).
1996        // `true` ⇒ last-seen is now; `false` ⇒ last-seen cleared (Go map.go:820-830).
1997        for (&node_id, &seen) in peer_seen_change {
1998            let new_last_seen = if seen { Some(now) } else { None };
1999            if let Some((_pid, existing)) = self.peer_db.get(&node_id)
2000                && existing.last_seen != new_last_seen
2001            {
2002                let mut node = existing.clone();
2003                node.last_seen = new_last_seen;
2004                self.peer_db.upsert(&node);
2005                changed = true;
2006            }
2007        }
2008
2009        changed
2010    }
2011
2012    /// Test-only constructor: build a [`PeerTracker`] with a chosen initial TKA authority without
2013    /// going through the actor `on_start` path. Returns the tracker plus the **`watch::Sender`** for
2014    /// its enforcement-authority cell, so a test can drive the exact enable/disable transitions the
2015    /// control runner drives at runtime (`tx.send_replace(Some(..))` ⇒ enforce, `tx.send_replace(None)`
2016    /// ⇒ clear). The initial `Some` exercises the fail-closed chokepoint
2017    /// ([`tka_admits`](Self::tka_admits)); `None` is the no-lock admit-all path. The returned sender
2018    /// must be kept alive for the tracker to read updated values.
2019    #[cfg(test)]
2020    fn for_test(
2021        env: Env,
2022        tka_authority: Option<ts_tka::Authority>,
2023    ) -> (Self, watch::Sender<Option<Arc<ts_tka::Authority>>>) {
2024        let (peer_watch, _) = watch::channel(Vec::new());
2025        let (tka_tx, tka_rx) = watch::channel(tka_authority.map(Arc::new));
2026        let tracker = Self {
2027            peer_db: PeerDb::default(),
2028            seen_state_update: false,
2029            pending_requests: Vec::new(),
2030            peer_watch,
2031            user_profiles: HashMap::new(),
2032            endpoint_disco: HashMap::new(),
2033            tka_authority: tka_rx,
2034            expiry: ExpiryManager::new(),
2035            self_node: None,
2036            expiry_timer: None,
2037            env,
2038        };
2039        (tracker, tka_tx)
2040    }
2041
2042    fn service_pending_requests(&mut self) {
2043        if self.seen_state_update {
2044            return;
2045        }
2046
2047        self.seen_state_update = true;
2048
2049        if !self.pending_requests.is_empty() {
2050            tracing::debug!(
2051                n_pending = self.pending_requests.len(),
2052                "state update received, servicing pending requests"
2053            );
2054        }
2055
2056        for req in core::mem::take(&mut self.pending_requests) {
2057            match req {
2058                Pending::PeerByName(PeerByName { name }, reply) => {
2059                    reply.send(self.peer_by_name_opt(&name).cloned());
2060                }
2061                Pending::TailnetIp(PeerByTailnetIp { ip }, reply) => {
2062                    reply.send(self.peer_by_tailnet_ip_opt(ip).cloned());
2063                }
2064                Pending::AcceptedRoute(PeerByAcceptedRoute { ip }, reply) => {
2065                    reply.send(
2066                        self.peer_db
2067                            .get_route(ip.into())
2068                            .map(|(_id, node)| node.clone())
2069                            .collect(),
2070                    );
2071                }
2072                Pending::Status(reply) => {
2073                    reply.send(self.status_peers_with_ids());
2074                }
2075                Pending::WhoIs(Whois { addr }, reply) => {
2076                    reply.send(self.whois_opt(addr));
2077                }
2078            }
2079        }
2080    }
2081}
2082
2083#[cfg(test)]
2084pub(crate) mod tka_tests {
2085    //! Tailnet-Lock (TKA) enforcement tests for the peer-trust chokepoint.
2086    //!
2087    //! These exercise [`PeerTracker::tka_admits`] and the `tka_admits ⇒ upsert` loop the netmap
2088    //! handler runs. The test [`ts_tka::Authority`] is built with [`ts_tka::Authority::from_state`]
2089    //! over a known Ed25519 trusted key, and the signed node-key signature CBOR is produced through
2090    //! `ts_tka`'s public `cbor` encoder + `aum_hash` (the exact same canonical bytes `ts_tka`'s own
2091    //! `direct_signature_verifies_end_to_end` test signs, with no new crypto vectors invented and no
2092    //! private `ts_tka` API used).
2093
2094    use ed25519_dalek::{Signer, SigningKey};
2095    use ts_control::{Node, StableNodeId, TailnetAddress};
2096    use ts_tka::{
2097        AumHash, Authority, Key, KeyKind, State,
2098        cbor::{self, Value},
2099    };
2100
2101    use super::*;
2102
2103    /// `SigKind::Direct` wire value (Go `SigKind`; `ts_tka::SigKind::Direct = 1`).
2104    const SIG_KIND_DIRECT: u64 = 1;
2105
2106    /// The 32-byte node key used across the signed-peer fixtures.
2107    const NODE_KEY_BYTES: [u8; 32] = [7u8; 32];
2108
2109    /// Build a real [`Env`] for the tracker. Only the bus/keys/shutdown plumbing matters here; the
2110    /// TKA gate reads neither, so the forwarding preferences are all benign defaults.
2111    pub(super) fn test_env() -> Env {
2112        let (_shutdown_tx, shutdown_rx) = watch::channel(false);
2113        Env::new(
2114            ts_keys::NodeState::generate(),
2115            shutdown_rx,
2116            crate::env::ForwarderConfig {
2117                accept_routes: false,
2118                accept_dns: true,
2119                exit_node: None,
2120                forward_routes: Vec::new(),
2121                forward_tcp_ports: Vec::new(),
2122                forward_udp_ports: Vec::new(),
2123                forward_all_ports: false,
2124                forward_exit_egress: false,
2125                block_incoming: false,
2126                exit_proxy: None,
2127                exit_node_dns: None,
2128                peerapi_port: None,
2129                taildrop_dir: None,
2130                enable_ipv6: false,
2131                wireguard_listen_port: None,
2132                network_monitor: false,
2133                persistent_keepalive_interval: None,
2134                ingress_active: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
2135            },
2136        )
2137    }
2138
2139    /// A minimal peer [`Node`] carrying `node_key` and the given `key_signature`.
2140    ///
2141    /// `pub(crate)` so the cold-start replay tests in `control_runner` build their peers the same way
2142    /// this module's TKA tests do — both run the same filter, and a second hand-rolled fixture could
2143    /// drift from it.
2144    pub(crate) fn peer_node(stable_id: &str, node_key: [u8; 32], key_signature: Vec<u8>) -> Node {
2145        Node {
2146            id: 1,
2147            stable_id: StableNodeId(stable_id.to_string()),
2148            hostname: stable_id.to_string(),
2149            user_id: 0,
2150            tailnet: Some("ts.net".to_string()),
2151            tags: Vec::new(),
2152            addresses: vec![
2153                "100.64.0.1/32".parse().unwrap(),
2154                "fd7a:115c:a1e0::1/128".parse().unwrap(),
2155            ],
2156            tailnet_address: TailnetAddress {
2157                ipv4: "100.64.0.1/32".parse().unwrap(),
2158                ipv6: "fd7a:115c:a1e0::1/128".parse().unwrap(),
2159            },
2160            node_key: node_key.into(),
2161            node_key_expiry: None,
2162            expired: false,
2163            online: None,
2164            last_seen: None,
2165            key_signature,
2166            machine_key: None,
2167            disco_key: None,
2168            accepted_routes: Vec::new(),
2169            underlay_addresses: Vec::new(),
2170            derp_region: None,
2171            cap: Default::default(),
2172            cap_map: Default::default(),
2173            peerapi_port: None,
2174            peerapi_dns_proxy: false,
2175            is_wireguard_only: false,
2176            exit_node_dns_resolvers: Vec::new(),
2177            peer_relay: false,
2178            ssh_host_keys: Vec::new(),
2179            service_vips: Default::default(),
2180            unsigned_peer_api_only: false,
2181        }
2182    }
2183
2184    /// Encode a `Direct` [`ts_tka::NodeKeySignature`] CBOR exactly as `ts_tka`'s private `to_cbor`
2185    /// does (int-map keys: 1=kind, 2=pubkey, 3=key_id, 4=signature; empty byte fields omitted),
2186    /// using only the crate's *public* `cbor` encoder. `signature` of `None` produces the
2187    /// signing-digest preimage (the `SigHash` form).
2188    fn direct_sig_cbor(node_key: &[u8], key_id: &[u8], signature: Option<&[u8]>) -> Vec<u8> {
2189        let mut pairs = alloc_pairs(node_key, key_id);
2190        if let Some(sig) = signature {
2191            pairs.push((4, Some(Value::Bytes(sig.to_vec()))));
2192        }
2193        cbor::int_map(pairs).to_vec()
2194    }
2195
2196    fn alloc_pairs(node_key: &[u8], key_id: &[u8]) -> Vec<(u64, Option<Value>)> {
2197        vec![
2198            (1, Some(Value::Uint(SIG_KIND_DIRECT))),
2199            (2, Some(Value::Bytes(node_key.to_vec()))),
2200            (3, Some(Value::Bytes(key_id.to_vec()))),
2201        ]
2202    }
2203
2204    /// Build a TKA [`Authority`] that trusts `signing.verifying_key()`, plus a valid `Direct`
2205    /// node-key signature CBOR authorizing [`NODE_KEY_BYTES`] under it.
2206    fn authority_and_valid_sig() -> (Authority, Vec<u8>) {
2207        // A fixed, known Ed25519 trusted key (mirrors ts_tka's own end-to-end test seed).
2208        let signing = SigningKey::from_bytes(&[42u8; 32]);
2209        let trusted_pub = signing.verifying_key().to_bytes().to_vec();
2210
2211        let authority = Authority::from_state(
2212            AumHash([0; 32]),
2213            State {
2214                keys: vec![Key {
2215                    kind: KeyKind::Ed25519,
2216                    votes: 1,
2217                    public: trusted_pub.clone(),
2218                }],
2219            },
2220        );
2221
2222        // SigHash preimage = canonical CBOR with the signature field omitted; sign its blake2s hash.
2223        let preimage = direct_sig_cbor(&NODE_KEY_BYTES, &trusted_pub, None);
2224        let sig_hash = ts_tka::aum_hash(&preimage).0;
2225        let signature = signing.sign(&sig_hash).to_bytes().to_vec();
2226
2227        let signed_cbor = direct_sig_cbor(&NODE_KEY_BYTES, &trusted_pub, Some(&signature));
2228        // Sanity: the authority accepts the signature we just built (same path the gate uses).
2229        assert!(
2230            authority
2231                .node_key_authorized(&NODE_KEY_BYTES, &signed_cbor)
2232                .is_ok()
2233        );
2234
2235        (authority, signed_cbor)
2236    }
2237
2238    #[tokio::test]
2239    async fn tka_inactive_upserts_all_peers() {
2240        // No authority ⇒ enforcement inactive ⇒ both a signed and an unsigned peer are admitted.
2241        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
2242
2243        let signed = peer_node("signed", [1u8; 32], vec![0xde, 0xad, 0xbe, 0xef]);
2244        let unsigned = peer_node("unsigned", [2u8; 32], vec![]);
2245
2246        assert!(tracker.tka_admits(&signed));
2247        assert!(tracker.tka_admits(&unsigned));
2248
2249        tracker.peer_db.upsert(&signed);
2250        tracker.peer_db.upsert(&unsigned);
2251        assert_eq!(tracker.peer_db.peers().len(), 2);
2252    }
2253
2254    #[tokio::test]
2255    async fn tka_active_rejects_unsigned_peer() {
2256        // Authority present + peer presents no signature ⇒ rejected (fail-closed), not in peer_db.
2257        let (authority, _sig) = authority_and_valid_sig();
2258        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2259
2260        let unsigned = peer_node("unsigned", NODE_KEY_BYTES, vec![]);
2261        assert!(!tracker.tka_admits(&unsigned));
2262
2263        // Mirror the handler's `if !tka_admits { continue }` loop.
2264        if tracker.tka_admits(&unsigned) {
2265            tracker.peer_db.upsert(&unsigned);
2266        }
2267        assert_eq!(tracker.peer_db.peers().len(), 0);
2268        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
2269    }
2270
2271    #[tokio::test]
2272    async fn tka_active_rejects_unsigned_peer_api_only_peer() {
2273        // `UnsignedPeerAPIOnly` buys NO admission exemption here: Go admits such a peer unsigned
2274        // under an active lock (peerAPI-only, no network access), this fork drops it like any other
2275        // unsigned peer. Pins the documented parity gap (`docs/PARITY_ROADMAP.md`, and the
2276        // `ts_control::Node::unsigned_peer_api_only` field docs) so implementing the carve-out has
2277        // to update the prose that promises no peerAPI access today.
2278        let (authority, _sig) = authority_and_valid_sig();
2279        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2280
2281        let mut peer_api_only = peer_node("peerapi-only", NODE_KEY_BYTES, vec![]);
2282        peer_api_only.unsigned_peer_api_only = true;
2283
2284        assert!(
2285            !tracker.tka_admits(&peer_api_only),
2286            "unsigned_peer_api_only must not exempt a peer from the tailnet-lock admission gate"
2287        );
2288
2289        // Mirror the handler's `if !tka_admits { continue }` loop: nothing reaches the peer db, so
2290        // the peer is not reachable for peerAPI either.
2291        if tracker.tka_admits(&peer_api_only) {
2292            tracker.peer_db.upsert(&peer_api_only);
2293        }
2294        assert_eq!(tracker.peer_db.peers().len(), 0);
2295        assert!(tracker.peer_db.get(&peer_api_only.node_key).is_none());
2296    }
2297
2298    #[tokio::test]
2299    async fn tka_empty_keyset_authority_admits_unsigned_peer_api_only_peer() {
2300        // The other side of `tka_active_rejects_unsigned_peer_api_only_peer`: "an authority is
2301        // present" is NOT on its own enough to drop an unsigned peer. The brick-guard fires first,
2302        // so an authority carrying no trusted keys enforces nothing and admits even the peer class
2303        // a keyed lock would reject. Pins the qualifier on the
2304        // `ts_control::Node::unsigned_peer_api_only` field docs — remove the guard and this fails.
2305        use ts_tka::{AumHash, Authority, State};
2306        let empty_auth = Authority::from_state(AumHash([0u8; 32]), State { keys: Vec::new() });
2307        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(empty_auth));
2308
2309        let mut peer_api_only = peer_node("peerapi-only", NODE_KEY_BYTES, vec![]);
2310        peer_api_only.unsigned_peer_api_only = true;
2311
2312        assert!(
2313            tracker.tka_admits(&peer_api_only),
2314            "an empty-keyset authority must not enforce, not even against an unsigned peer"
2315        );
2316
2317        tracker.apply_peer_update(
2318            &ts_control::PeerUpdate::Full(vec![peer_api_only.clone()]),
2319            local_now(),
2320        );
2321        assert!(
2322            tracker.peer_db.get(&peer_api_only.node_key).is_some(),
2323            "the peer reaches the peer db, so the gate's drop is keyset-conditional"
2324        );
2325    }
2326
2327    #[tokio::test]
2328    async fn tka_active_rejects_bad_signature() {
2329        // Authority present + a signature that fails to verify ⇒ rejected, not in peer_db.
2330        let (authority, mut sig) = authority_and_valid_sig();
2331        // Tamper the last byte (the trailing signature byte) so verification fails.
2332        let last = sig.len() - 1;
2333        sig[last] ^= 0xff;
2334
2335        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2336        let bad = peer_node("bad", NODE_KEY_BYTES, sig);
2337        assert!(!tracker.tka_admits(&bad));
2338
2339        if tracker.tka_admits(&bad) {
2340            tracker.peer_db.upsert(&bad);
2341        }
2342        assert_eq!(tracker.peer_db.peers().len(), 0);
2343    }
2344
2345    #[tokio::test]
2346    async fn tka_active_admits_authorized_peer() {
2347        // Authority present + correctly-signed node key ⇒ admitted and upserted.
2348        let (authority, sig) = authority_and_valid_sig();
2349        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2350
2351        let good = peer_node("good", NODE_KEY_BYTES, sig);
2352        assert!(tracker.tka_admits(&good));
2353
2354        if tracker.tka_admits(&good) {
2355            tracker.peer_db.upsert(&good);
2356        }
2357        assert_eq!(tracker.peer_db.peers().len(), 1);
2358        assert!(tracker.peer_db.get(&good.node_key).is_some());
2359    }
2360
2361    // ---------------------------------------------------------------------------------------------
2362    // Tests that drive REAL `PeerUpdate`s through the shared handler body
2363    // ([`PeerTracker::apply_peer_update`], the single source of truth the actor's netmap `handle`
2364    // also calls), so the two real upsert sites (`Full` and `Delta { upsert }`) are exercised via
2365    // the actual enforcement path — not by hand-mirroring `if !tka_admits { continue }`.
2366    // ---------------------------------------------------------------------------------------------
2367
2368    #[tokio::test]
2369    async fn tka_active_delta_upsert_rejects_unauthorized() {
2370        // Drive a real `Delta { upsert }` whose peer carries no signature. The Delta upsert site
2371        // must reject it under an active authority ⇒ not present in peer_db after the handler runs.
2372        let (authority, _sig) = authority_and_valid_sig();
2373        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2374
2375        let unsigned = peer_node("unsigned", NODE_KEY_BYTES, vec![]);
2376        let update = ts_control::PeerUpdate::Delta {
2377            upsert: vec![unsigned.clone()],
2378            remove: Vec::new(),
2379        };
2380
2381        tracker.apply_peer_update(&update, local_now());
2382
2383        assert_eq!(tracker.peer_db.peers().len(), 0);
2384        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
2385    }
2386
2387    #[tokio::test]
2388    async fn tka_active_delta_upsert_admits_authorized() {
2389        // Drive a real `Delta { upsert }` with a correctly-signed peer ⇒ present in peer_db.
2390        let (authority, sig) = authority_and_valid_sig();
2391        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2392
2393        let good = peer_node("good", NODE_KEY_BYTES, sig);
2394        let update = ts_control::PeerUpdate::Delta {
2395            upsert: vec![good.clone()],
2396            remove: Vec::new(),
2397        };
2398
2399        tracker.apply_peer_update(&update, local_now());
2400
2401        assert_eq!(tracker.peer_db.peers().len(), 1);
2402        assert!(tracker.peer_db.get(&good.node_key).is_some());
2403    }
2404
2405    #[tokio::test]
2406    async fn tka_active_full_admits_only_authorized_in_mixed_batch() {
2407        // Drive a real `Full` carrying a MIX of authorized + unauthorized peers. Only the
2408        // correctly-signed peer survives the Full upsert site; the unsigned and bad-sig peers are
2409        // dropped fail-closed.
2410        let (authority, sig) = authority_and_valid_sig();
2411        // A bad-sig variant of the same authorized signature (tamper the trailing byte).
2412        let mut bad_sig = sig.clone();
2413        let last = bad_sig.len() - 1;
2414        bad_sig[last] ^= 0xff;
2415
2416        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2417
2418        // Only the authorized peer carries NODE_KEY_BYTES (the key the authority signed); the
2419        // rejected peers use distinct node keys so the survivor is unambiguous.
2420        let good = peer_node("good", NODE_KEY_BYTES, sig);
2421        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2422        let bad = peer_node("bad", [9u8; 32], bad_sig);
2423
2424        let update =
2425            ts_control::PeerUpdate::Full(vec![good.clone(), unsigned.clone(), bad.clone()]);
2426
2427        tracker.apply_peer_update(&update, local_now());
2428
2429        assert_eq!(tracker.peer_db.peers().len(), 1);
2430        assert!(tracker.peer_db.get(&good.node_key).is_some());
2431        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
2432        assert!(tracker.peer_db.get(&bad.node_key).is_none());
2433    }
2434
2435    /// End-to-end through the REAL enforcement-authority transport (the `watch` cell the control
2436    /// runner writes), not a direct field poke: writing `Some(authority)` flips enforcement on so a
2437    /// mixed batch drops the unsigned/bad peers, and a subsequent `None` (lock disabled) clears
2438    /// enforcement so a peer DROPPED while enforced is re-admitted. Exercises the exact `borrow`-based
2439    /// read path `tka_admits` uses — a broken receiver wiring would pass every for_test-field test but
2440    /// fail here.
2441    #[tokio::test]
2442    async fn tka_authority_watch_enables_then_clears_enforcement() {
2443        let (authority, sig) = authority_and_valid_sig();
2444        let mut bad_sig = sig.clone();
2445        let last = bad_sig.len() - 1;
2446        bad_sig[last] ^= 0xff;
2447
2448        let (mut tracker, tka_tx) = PeerTracker::for_test(test_env(), None);
2449
2450        // 1) No authority yet ⇒ admit-all (Go b.tka == nil).
2451        let good = peer_node("good", NODE_KEY_BYTES, sig.clone());
2452        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2453        let bad = peer_node("bad", [9u8; 32], bad_sig);
2454        let batch = ts_control::PeerUpdate::Full(vec![good.clone(), unsigned.clone(), bad.clone()]);
2455        tracker.apply_peer_update(&batch, local_now());
2456        assert_eq!(tracker.peer_db.peers().len(), 3, "no lock ⇒ admit all");
2457
2458        // 2) Publish the verified authority over the watch cell (exactly what the control runner does
2459        //    on a successful sync) ⇒ enforcement ON. A re-applied Full now drops unsigned + bad.
2460        tka_tx.send_replace(Some(Arc::new(authority)));
2461        tracker.apply_peer_update(&batch, local_now());
2462        assert_eq!(
2463            tracker.peer_db.peers().len(),
2464            1,
2465            "lock active ⇒ only the signed peer survives"
2466        );
2467        assert!(tracker.peer_db.get(&good.node_key).is_some());
2468        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
2469        assert!(tracker.peer_db.get(&bad.node_key).is_none());
2470
2471        // 3) Lock disabled (None) ⇒ enforcement cleared ⇒ a peer that was DROPPED while enforced is
2472        //    re-admitted by a fresh netmap. Assert the specific previously-dropped key returns (not
2473        //    merely a count), so this proves the drop→clear→re-admit transition, not "admit-all-fresh".
2474        tka_tx.send_replace(None);
2475        tracker.apply_peer_update(&batch, local_now());
2476        assert_eq!(
2477            tracker.peer_db.peers().len(),
2478            3,
2479            "lock disabled ⇒ admit all again"
2480        );
2481        assert!(
2482            tracker.peer_db.get(&unsigned.node_key).is_some(),
2483            "the peer dropped under enforcement must come back once the lock is cleared"
2484        );
2485        assert!(tracker.peer_db.get(&bad.node_key).is_some());
2486    }
2487
2488    /// The ordering gap this closes. A peer admitted BEFORE the lock synced must be re-checked the
2489    /// moment the authority is installed — not left in the db until control happens to send another
2490    /// `Full`. Go never has this problem: `SetControlClientStatus` runs `tkaSyncIfNeeded` and then
2491    /// `tkaFilterNetmapLocked(st.NetMap)` on the SAME netmap in one pass, so the netmap that
2492    /// announced the lock is itself filtered. Here the sync is a spawned task, so the netmap lands
2493    /// first and `tka_reevaluate_peer_db` is what restores Go's ordering.
2494    ///
2495    /// Note this test applies NO second netmap: the eviction must come from the authority install
2496    /// alone, which is exactly what was missing before.
2497    #[tokio::test]
2498    async fn tka_authority_install_reevaluates_already_admitted_peers() {
2499        let (authority, sig) = authority_and_valid_sig();
2500        let mut bad_sig = sig.clone();
2501        let last = bad_sig.len() - 1;
2502        bad_sig[last] ^= 0xff;
2503
2504        let (mut tracker, tka_tx) = PeerTracker::for_test(test_env(), None);
2505
2506        // 1) A netmap arrives while nothing is synced ⇒ enforcement inactive ⇒ all three admitted.
2507        let good = peer_node("good", NODE_KEY_BYTES, sig);
2508        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2509        let bad = peer_node("bad", [9u8; 32], bad_sig);
2510        tracker.apply_peer_update(
2511            &ts_control::PeerUpdate::Full(vec![good.clone(), unsigned.clone(), bad.clone()]),
2512            local_now(),
2513        );
2514        assert_eq!(tracker.peer_db.peers().len(), 3, "no lock yet ⇒ admit all");
2515        let unsigned_id = tracker
2516            .peer_db
2517            .get(&unsigned.node_key)
2518            .expect("unsigned peer admitted while no lock is synced")
2519            .0;
2520        let bad_id = tracker
2521            .peer_db
2522            .get(&bad.node_key)
2523            .expect("bad-sig peer admitted while no lock is synced")
2524            .0;
2525
2526        // 2) The sync completes and the control runner installs the verified authority.
2527        tka_tx.send_replace(Some(Arc::new(authority)));
2528        let evicted = tracker.tka_reevaluate_peer_db();
2529
2530        assert_eq!(
2531            evicted,
2532            HashSet::from_iter([unsigned_id, bad_id]),
2533            "the unsigned and bad-signature peers are the ones reported evicted"
2534        );
2535        assert_eq!(tracker.peer_db.peers().len(), 1);
2536        assert!(
2537            tracker.peer_db.get(&good.node_key).is_some(),
2538            "the authorized peer stays admitted"
2539        );
2540        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
2541        assert!(tracker.peer_db.get(&bad.node_key).is_none());
2542
2543        // 3) Idempotent: a second pass over the now-clean db evicts nobody.
2544        assert!(tracker.tka_reevaluate_peer_db().is_empty());
2545    }
2546
2547    /// With no authority the re-evaluation evicts nobody — enforcement is inactive and every peer is
2548    /// admitted, exactly Go's `b.tka == nil` early return. Covers both "never synced" and "the lock
2549    /// was disabled after enforcing", the two ways the cell holds `None`.
2550    #[tokio::test]
2551    async fn tka_reevaluate_without_authority_evicts_nothing() {
2552        let (authority, sig) = authority_and_valid_sig();
2553        let (mut tracker, tka_tx) = PeerTracker::for_test(test_env(), None);
2554
2555        let good = peer_node("good", NODE_KEY_BYTES, sig);
2556        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2557        tracker.apply_peer_update(
2558            &ts_control::PeerUpdate::Full(vec![good.clone(), unsigned.clone()]),
2559            local_now(),
2560        );
2561
2562        // Never synced.
2563        assert!(tracker.tka_reevaluate_peer_db().is_empty());
2564        assert_eq!(tracker.peer_db.peers().len(), 2);
2565
2566        // Enforced, then disabled: the disable must not evict the peer the lock had authorized, and
2567        // must not start dropping the unsigned one either.
2568        tka_tx.send_replace(Some(Arc::new(authority)));
2569        assert_eq!(tracker.tka_reevaluate_peer_db().len(), 1);
2570        tka_tx.send_replace(None);
2571        assert!(tracker.tka_reevaluate_peer_db().is_empty());
2572        assert!(tracker.peer_db.get(&good.node_key).is_some());
2573    }
2574
2575    /// The re-evaluation runs the WHOLE Go `tkaFilterNetmapLocked` pass, not just the per-peer
2576    /// signature check: a peer presenting a node key that a newer rotation superseded is evicted too,
2577    /// even though its own `Direct` signature still verifies against the authority. Both peers are
2578    /// already in the db when the authority lands, so the cross-peer rotation filter has to run over
2579    /// the db contents — which is why `tka_keep_verdicts` is shared with the `Full` path rather than
2580    /// re-derived here.
2581    #[tokio::test]
2582    async fn tka_reevaluate_applies_the_cross_peer_rotation_filter() {
2583        use ed25519_dalek::SigningKey;
2584        use ts_tka::NodeKeySignature;
2585
2586        let trusted = SigningKey::from_bytes(&[42u8; 32]);
2587        let authority = Authority::from_state(
2588            AumHash([0; 32]),
2589            State {
2590                keys: vec![Key {
2591                    kind: KeyKind::Ed25519,
2592                    votes: 1,
2593                    public: trusted.verifying_key().to_bytes().to_vec(),
2594                }],
2595            },
2596        );
2597        // `stale` holds the pivot key with a valid Direct signature; `rotated` holds a key whose
2598        // rotation chain rotated the pivot key AWAY, which obsoletes `stale`.
2599        let pivot = SigningKey::from_bytes(&[9u8; 32]);
2600        let pivot_pub: [u8; 32] = pivot.verifying_key().to_bytes();
2601        let stale = peer_node(
2602            "stale",
2603            pivot_pub,
2604            NodeKeySignature::sign_direct(&pivot_pub, &trusted).serialize(),
2605        );
2606        let new_key = [4u8; 32];
2607        let rotated = peer_node(
2608            "rotated",
2609            new_key,
2610            NodeKeySignature::sign_rotation(&new_key, &trusted, &pivot).serialize(),
2611        );
2612
2613        // Both admitted while nothing is synced.
2614        let (mut tracker, tka_tx) = PeerTracker::for_test(test_env(), None);
2615        tracker.apply_peer_update(
2616            &ts_control::PeerUpdate::Full(vec![stale.clone(), rotated.clone()]),
2617            local_now(),
2618        );
2619        assert_eq!(tracker.peer_db.peers().len(), 2, "no lock yet ⇒ admit all");
2620
2621        tka_tx.send_replace(Some(Arc::new(authority)));
2622        let evicted = tracker.tka_reevaluate_peer_db();
2623
2624        assert_eq!(
2625            evicted.len(),
2626            1,
2627            "only the rotation-obsolete peer is evicted"
2628        );
2629        assert!(
2630            tracker.peer_db.get(&rotated.node_key).is_some(),
2631            "the freshly-rotated peer stays"
2632        );
2633        assert!(
2634            tracker.peer_db.get(&stale.node_key).is_none(),
2635            "the peer whose key a rotation superseded is evicted, though its own signature verifies"
2636        );
2637    }
2638
2639    /// A `StateUpdate` carrying nothing but a `Full` peer set — the netmap shape the live-actor test
2640    /// publishes on the bus.
2641    pub(crate) fn netmap_with_peers(peers: Vec<Node>) -> ts_control::StateUpdate {
2642        ts_control::StateUpdate {
2643            session_handle: None,
2644            seq: 0,
2645            keep_alive: false,
2646            derp: None,
2647            node: None,
2648            peer_update: Some(ts_control::PeerUpdate::Full(peers)),
2649            peer_patches: Vec::new(),
2650            user_profiles: Vec::new(),
2651            ping: None,
2652            packetfilter: None,
2653            cap_grants: None,
2654            pop_browser_url: None,
2655            dial_plan: None,
2656            dns_config: None,
2657            ssh_policy: None,
2658            tka: None,
2659            online_change: Default::default(),
2660            peer_seen_change: Default::default(),
2661            control_time: None,
2662        }
2663    }
2664
2665    /// Poll a live [`PeerTracker`] until it holds exactly `want` peers, bounded by a timeout so a
2666    /// broken wiring fails the test instead of hanging the suite.
2667    pub(crate) async fn await_peer_count(
2668        tracker: &ActorRef<PeerTracker>,
2669        want: usize,
2670    ) -> Vec<Node> {
2671        let settled = tokio::time::timeout(std::time::Duration::from_secs(10), async {
2672            loop {
2673                let peers = tracker.ask(AllPeers).await.expect("peer tracker is alive");
2674                if peers.len() == want {
2675                    return peers;
2676                }
2677                tokio::time::sleep(std::time::Duration::from_millis(5)).await;
2678            }
2679        })
2680        .await;
2681        settled.unwrap_or_else(|_| panic!("peer tracker never settled at {want} peer(s)"))
2682    }
2683
2684    /// Control omits `Peers` when a node may see no peers (`omitempty`), so a netmap carrying the
2685    /// self node and no peer set is that node's complete initial state. Peer queries made before it
2686    /// (status, lookups) must be answered — empty — instead of waiting forever for a peer set.
2687    #[tokio::test]
2688    async fn self_only_netmap_answers_pending_status() {
2689        use kameo::actor::Spawn as _;
2690
2691        let env = test_env();
2692        let (_tka_tx, tka_rx) = watch::channel(None);
2693        let tracker = PeerTracker::spawn((env.clone(), tka_rx));
2694        let pending = tokio::spawn({
2695            let tracker = tracker.clone();
2696            async move { tracker.ask(GetStatus).await }
2697        });
2698        // The status query is parked until control's first netmap.
2699        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
2700        assert!(!pending.is_finished());
2701
2702        let mut update = netmap_with_peers(vec![]);
2703        update.peer_update = None;
2704        update.node = Some(peer_node("self", [9u8; 32], vec![]));
2705        env.publish(Arc::new(update)).await.expect("publish netmap");
2706
2707        let status = tokio::time::timeout(std::time::Duration::from_secs(5), pending)
2708            .await
2709            .expect("status query still waiting after a self-only netmap")
2710            .unwrap()
2711            .expect("peer tracker is alive");
2712        assert!(status.is_empty());
2713    }
2714
2715    /// End-to-end through the LIVE actor, which is the only thing that proves the wiring: the peer
2716    /// tracker watches its own enforcement cell, so the control runner's `send_replace` re-filters
2717    /// the peer db with no further netmap. If the watch task were never spawned (or the message not
2718    /// handled) the unsigned peer would stay admitted forever — a hole every `for_test` unit test
2719    /// above would still pass over, because they call the re-evaluation by hand.
2720    #[tokio::test]
2721    async fn tka_authority_change_refilters_through_the_live_actor() {
2722        use kameo::actor::Spawn as _;
2723
2724        let (authority, sig) = authority_and_valid_sig();
2725        let env = test_env();
2726        let (tka_tx, tka_rx) = watch::channel(None);
2727        let tracker = PeerTracker::spawn((env.clone(), tka_rx));
2728
2729        // Await one reply first: the actor's `on_start` (which registers it on the bus) has then
2730        // completed, so the netmap published below cannot race the subscription.
2731        assert!(
2732            tracker
2733                .ask(AllPeers)
2734                .await
2735                .expect("peer tracker started")
2736                .is_empty()
2737        );
2738
2739        let good = peer_node("good", NODE_KEY_BYTES, sig);
2740        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2741        env.publish(Arc::new(netmap_with_peers(vec![
2742            good.clone(),
2743            unsigned.clone(),
2744        ])))
2745        .await
2746        .expect("publish netmap");
2747
2748        // No lock synced ⇒ both peers land.
2749        await_peer_count(&tracker, 2).await;
2750
2751        // The control runner installs the verified authority. No netmap follows.
2752        tka_tx.send_replace(Some(Arc::new(authority)));
2753
2754        let peers = await_peer_count(&tracker, 1).await;
2755        assert_eq!(
2756            peers[0].stable_id, good.stable_id,
2757            "only the authorized peer survives the authority install"
2758        );
2759    }
2760
2761    /// Degenerate input: two DISTINCT nodes sharing one `stable_id` in a single `Full`, one with a
2762    /// valid signature and one unsigned, under an active lock. Each node is judged by its OWN verdict
2763    /// (the per-node `admits` vector), so the unsigned node is never admitted on the strength of its
2764    /// signed twin. The single-verify `Full` refactor keeps this per-node semantics (a stable_id-set
2765    /// alone would have admitted whichever node was upserted last). Malformed control input; asserted
2766    /// only to lock the verdict-per-node behavior against regression.
2767    #[tokio::test]
2768    async fn tka_full_duplicate_stable_id_judges_each_node_on_its_own_signature() {
2769        let (authority, sig) = authority_and_valid_sig();
2770        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2771
2772        // Both carry stable_id "dup"; the signed one authorizes NODE_KEY_BYTES, the other is unsigned
2773        // and uses a different node key. Order them unsigned-last so a last-writer-wins stable_id set
2774        // would (wrongly) leave the unsigned node's key in the db.
2775        let signed = peer_node("dup", NODE_KEY_BYTES, sig);
2776        let unsigned = peer_node("dup", [8u8; 32], vec![]);
2777        tracker.apply_peer_update(
2778            &ts_control::PeerUpdate::Full(vec![signed.clone(), unsigned.clone()]),
2779            local_now(),
2780        );
2781
2782        // The unsigned node's own verdict failed, so its key must NOT be present, regardless of the
2783        // shared stable_id. (The signed twin retained the stable_id; the db holds the signed key.)
2784        assert!(
2785            tracker.peer_db.get(&unsigned.node_key).is_none(),
2786            "a node whose own signature fails must not be admitted via a stable_id twin"
2787        );
2788        assert!(tracker.peer_db.get(&signed.node_key).is_some());
2789    }
2790
2791    /// Full-path consistency under two KEPT nodes sharing a `stable_id`: `peer_db.upsert` is
2792    /// last-writer-wins on `stable_id`, so the db ends holding exactly one node for that id (the last
2793    /// kept), and `retain` never evicts that just-upserted id (`retained_ids` contains the shared id
2794    /// because at least one of its nodes was kept). No lock here, so both nodes are "kept". This pins
2795    /// the published-state invariant the whole-surface audit flagged: `retain` and the upsert loop
2796    /// agree on the surviving stable_id. Malformed control input; asserted for robustness.
2797    #[tokio::test]
2798    async fn tka_full_duplicate_stable_id_both_kept_is_consistent() {
2799        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
2800        let first = peer_node("dup", [1u8; 32], vec![]);
2801        let last = peer_node("dup", [2u8; 32], vec![]);
2802        tracker.apply_peer_update(
2803            &ts_control::PeerUpdate::Full(vec![first.clone(), last.clone()]),
2804            local_now(),
2805        );
2806
2807        // Exactly one db entry for the shared stable_id, holding the LAST node (upsert is
2808        // last-writer-wins on stable_id); the first node's key was transparently superseded.
2809        assert_eq!(
2810            tracker.peer_db.peers().len(),
2811            1,
2812            "one entry for the shared stable_id"
2813        );
2814        assert!(
2815            tracker.peer_db.get(&last.node_key).is_some(),
2816            "the db holds the last-upserted node for the shared id"
2817        );
2818        assert!(
2819            tracker.peer_db.get(&first.node_key).is_none(),
2820            "the first node's key was superseded by the last at the shared id"
2821        );
2822    }
2823
2824    /// A peer admitted in one `Full`, then in a later `Full` presenting a key that a co-resident
2825    /// peer's rotation chain has rotated away, is EVICTED — the cross-peer rotation filter applies on
2826    /// every resync, not only at first admission. Exercises the rotation filter through two
2827    /// sequential `Full` updates with real signing.
2828    #[tokio::test]
2829    async fn tka_full_rotation_obsolete_evicts_on_resync() {
2830        use ed25519_dalek::SigningKey;
2831        use ts_tka::NodeKeySignature;
2832
2833        let trusted = SigningKey::from_bytes(&[42u8; 32]);
2834        let trusted_pub = trusted.verifying_key().to_bytes().to_vec();
2835        let authority = Authority::from_state(
2836            AumHash([0; 32]),
2837            State {
2838                keys: vec![Key {
2839                    kind: KeyKind::Ed25519,
2840                    votes: 1,
2841                    public: trusted_pub.clone(),
2842                }],
2843            },
2844        );
2845        let pivot = SigningKey::from_bytes(&[9u8; 32]);
2846        let pivot_pub: [u8; 32] = pivot.verifying_key().to_bytes();
2847
2848        // First Full: the soon-to-be-stale peer presents the pivot key with a valid Direct sig.
2849        let stale_sig = NodeKeySignature::sign_direct(&pivot_pub, &trusted).serialize();
2850        let stale_peer = peer_node("stale", pivot_pub, stale_sig);
2851        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2852        tracker.apply_peer_update(
2853            &ts_control::PeerUpdate::Full(vec![stale_peer.clone()]),
2854            local_now(),
2855        );
2856        assert!(
2857            tracker.peer_db.get(&stale_peer.node_key).is_some(),
2858            "the stale peer is admitted while no rotation has superseded it yet"
2859        );
2860
2861        // Second Full: a freshly-rotated peer (whose chain rotated AWAY the pivot key) joins, and the
2862        // stale peer is re-included. The rotation filter now obsoletes the pivot key ⇒ stale evicted.
2863        let new_key = [4u8; 32];
2864        let new_sig = NodeKeySignature::sign_rotation(&new_key, &trusted, &pivot).serialize();
2865        let new_peer = peer_node("rotated", new_key, new_sig);
2866        tracker.apply_peer_update(
2867            &ts_control::PeerUpdate::Full(vec![new_peer.clone(), stale_peer.clone()]),
2868            local_now(),
2869        );
2870        assert!(
2871            tracker.peer_db.get(&new_peer.node_key).is_some(),
2872            "the freshly-rotated peer is admitted"
2873        );
2874        assert!(
2875            tracker.peer_db.get(&stale_peer.node_key).is_none(),
2876            "the stale peer is EVICTED on the resync once a rotation supersedes its key"
2877        );
2878    }
2879
2880    /// The empty-trusted-key-state brick-guard: an authority with no keys must NOT drop the whole
2881    /// netmap (a `ts_tka` invariant violation / replayer edge). A verified chain always carries ≥1
2882    /// key, so this never weakens a genuine lock — it only prevents a black-hole. Uses ≥2 peers
2883    /// (one signed, one unsigned) to prove it admits **all**, not accidentally just one.
2884    #[tokio::test]
2885    async fn tka_empty_keyset_authority_admits_all() {
2886        use ts_tka::{AumHash, Authority, State};
2887        let empty_auth = Authority::from_state(AumHash([0u8; 32]), State { keys: Vec::new() });
2888        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(empty_auth));
2889        let signed = peer_node("signed", [7u8; 32], vec![0xde, 0xad]);
2890        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2891        tracker.apply_peer_update(
2892            &ts_control::PeerUpdate::Full(vec![signed.clone(), unsigned.clone()]),
2893            local_now(),
2894        );
2895        assert_eq!(
2896            tracker.peer_db.peers().len(),
2897            2,
2898            "an empty-keyset authority must admit ALL peers (brick-guard), not enforce"
2899        );
2900    }
2901
2902    /// Signature-replay / `NodeKeyMismatch`: a structurally-valid signature that authorizes
2903    /// `NODE_KEY_BYTES` must NOT admit a DIFFERENT node key carrying that same signature blob. This is
2904    /// the highest-value bypass — if the sig↔node-key binding in `verify_signature` were dropped, this
2905    /// is the only test that would catch it (the other "bad" peers only flip a byte ⇒ `BadSignature`).
2906    #[tokio::test]
2907    async fn tka_active_rejects_valid_sig_for_wrong_node_key() {
2908        let (authority, sig) = authority_and_valid_sig();
2909        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2910
2911        // The signature authorizes NODE_KEY_BYTES; attach it to an imposter with a different key.
2912        let imposter = peer_node("imposter", [0x55u8; 32], sig);
2913        assert!(
2914            !tracker.tka_admits(&imposter),
2915            "a signature bound to one node key must not authorize a different node key"
2916        );
2917        tracker.apply_peer_update(
2918            &ts_control::PeerUpdate::Full(vec![imposter.clone()]),
2919            local_now(),
2920        );
2921        assert!(tracker.peer_db.get(&imposter.node_key).is_none());
2922    }
2923
2924    /// `UntrustedKey`: a signature produced by a well-formed Ed25519 key that is NOT in the
2925    /// authority's trusted-key state must be rejected — distinct from a tampered-byte `BadSignature`.
2926    #[tokio::test]
2927    async fn tka_active_rejects_sig_from_untrusted_key() {
2928        use ed25519_dalek::{Signer, SigningKey};
2929        let (authority, _sig) = authority_and_valid_sig();
2930        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2931
2932        // Sign a valid CBOR with a DIFFERENT key (not the one the authority trusts). The key_id in
2933        // the signature names this untrusted key, so `get_key` misses ⇒ UntrustedKey.
2934        let rogue = SigningKey::from_bytes(&[99u8; 32]);
2935        let rogue_pub = rogue.verifying_key().to_bytes().to_vec();
2936        let preimage = direct_sig_cbor(&NODE_KEY_BYTES, &rogue_pub, None);
2937        let sig_hash = ts_tka::aum_hash(&preimage).0;
2938        let signature = rogue.sign(&sig_hash).to_bytes().to_vec();
2939        let rogue_cbor = direct_sig_cbor(&NODE_KEY_BYTES, &rogue_pub, Some(&signature));
2940
2941        let peer = peer_node("rogue-signed", NODE_KEY_BYTES, rogue_cbor);
2942        assert!(
2943            !tracker.tka_admits(&peer),
2944            "a signature from a key outside the trusted set must be rejected"
2945        );
2946        // Drive the real upsert path too (match the sibling replay test's depth): an untrusted-key
2947        // signature must keep the peer out of the db, not merely fail the verdict in isolation.
2948        tracker.apply_peer_update(
2949            &ts_control::PeerUpdate::Full(vec![peer.clone()]),
2950            local_now(),
2951        );
2952        assert!(tracker.peer_db.get(&peer.node_key).is_none());
2953    }
2954
2955    /// Bus-enable analogue for `Delta`: enforcement engaged via the watch cell must also gate a
2956    /// `Delta { upsert }` (not only `Full`). Closes the "authority arrived over the transport AND the
2957    /// next update is a Delta" combination.
2958    #[tokio::test]
2959    async fn tka_watch_enable_enforces_delta_upsert() {
2960        let (authority, sig) = authority_and_valid_sig();
2961        let (mut tracker, tka_tx) = PeerTracker::for_test(test_env(), None);
2962        tka_tx.send_replace(Some(Arc::new(authority)));
2963
2964        let good = peer_node("good", NODE_KEY_BYTES, sig);
2965        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2966        tracker.apply_peer_update(
2967            &ts_control::PeerUpdate::Delta {
2968                remove: vec![],
2969                upsert: vec![good.clone(), unsigned.clone()],
2970            },
2971            local_now(),
2972        );
2973        assert!(tracker.peer_db.get(&good.node_key).is_some());
2974        assert!(
2975            tracker.peer_db.get(&unsigned.node_key).is_none(),
2976            "delta upsert under an active lock must drop the unsigned peer"
2977        );
2978    }
2979
2980    /// A `Delta` re-upsert of an ALREADY-ADMITTED peer whose signature is now invalid must EVICT the
2981    /// stale entry (revocation-via-delta), not leave it admitted. Go re-filters the whole netmap each
2982    /// response, so a now-unsigned peer would not survive there either.
2983    #[tokio::test]
2984    async fn tka_delta_reupsert_with_invalid_sig_evicts_existing() {
2985        let (authority, sig) = authority_and_valid_sig();
2986        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2987
2988        // Admit the signed peer.
2989        let good = peer_node("good", NODE_KEY_BYTES, sig.clone());
2990        tracker.apply_peer_update(
2991            &ts_control::PeerUpdate::Full(vec![good.clone()]),
2992            local_now(),
2993        );
2994        assert!(tracker.peer_db.get(&good.node_key).is_some());
2995
2996        // Re-upsert the SAME stable_id (now with no signature) via a delta ⇒ evicted, not retained.
2997        let revoked = peer_node("good", NODE_KEY_BYTES, vec![]);
2998        tracker.apply_peer_update(
2999            &ts_control::PeerUpdate::Delta {
3000                remove: vec![],
3001                upsert: vec![revoked],
3002            },
3003            local_now(),
3004        );
3005        assert!(
3006            tracker.peer_db.get(&good.node_key).is_none(),
3007            "a delta re-upsert that fails the lock must evict the previously-admitted peer"
3008        );
3009    }
3010
3011    #[tokio::test]
3012    async fn tka_full_resync_revocation_behavior() {
3013        // Revocation-on-resync: admit a peer, then re-include the SAME stable_id in a `Full` with a
3014        // now-invalid signature. Per the Logic review finding, the pre-fix `retain` kept the stale
3015        // (previously-admitted) entry because membership was decided purely by stable_id.
3016        //
3017        // FIXED (not merely documented): the `Full` `retain` now keys on `tka_admits`-passing
3018        // stable_ids, so a peer whose re-included signature no longer verifies under the active
3019        // authority is EVICTED. This test asserts eviction. The inactive (authority=None) path is
3020        // provably unchanged — `tka_admits` always returns `true` there, so the retained set equals
3021        // the set of re-included stable_ids exactly (see `tka_inactive_full_resync_keeps_*`).
3022        let (authority, sig) = authority_and_valid_sig();
3023        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
3024
3025        // 1) Admit the peer with a valid signature via a real `Full`.
3026        let good = peer_node("revoked", NODE_KEY_BYTES, sig.clone());
3027        tracker.apply_peer_update(
3028            &ts_control::PeerUpdate::Full(vec![good.clone()]),
3029            local_now(),
3030        );
3031        assert_eq!(tracker.peer_db.peers().len(), 1);
3032        assert!(tracker.peer_db.get(&good.node_key).is_some());
3033
3034        // 2) Re-sync the SAME stable_id, but with a now-invalid signature (tamper trailing byte).
3035        let mut bad_sig = sig;
3036        let last = bad_sig.len() - 1;
3037        bad_sig[last] ^= 0xff;
3038        let revoked = peer_node("revoked", NODE_KEY_BYTES, bad_sig);
3039        tracker.apply_peer_update(
3040            &ts_control::PeerUpdate::Full(vec![revoked.clone()]),
3041            local_now(),
3042        );
3043
3044        // Eviction: the stale entry is dropped because its re-included signature fails the gate.
3045        assert_eq!(tracker.peer_db.peers().len(), 0);
3046        assert!(tracker.peer_db.get(&revoked.node_key).is_none());
3047    }
3048
3049    #[tokio::test]
3050    async fn tka_inactive_full_resync_keeps_reincluded_peer() {
3051        // Guard the inactive (authority=None) path against the revocation fix: with no authority,
3052        // a peer re-included in a `Full` survives regardless of its signature bytes — byte-for-byte
3053        // pre-TKA behavior, proving the `Full` `retain` change does not regress the always-taken
3054        // branch this wave.
3055        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3056
3057        let peer = peer_node("p", NODE_KEY_BYTES, vec![0xde, 0xad]);
3058        tracker.apply_peer_update(
3059            &ts_control::PeerUpdate::Full(vec![peer.clone()]),
3060            local_now(),
3061        );
3062        assert_eq!(tracker.peer_db.peers().len(), 1);
3063
3064        // Re-sync the same stable_id with garbage signature bytes; inactive enforcement keeps it.
3065        let resynced = peer_node("p", NODE_KEY_BYTES, vec![0x00]);
3066        tracker.apply_peer_update(
3067            &ts_control::PeerUpdate::Full(vec![resynced.clone()]),
3068            local_now(),
3069        );
3070        assert_eq!(tracker.peer_db.peers().len(), 1);
3071        assert!(tracker.peer_db.get(&resynced.node_key).is_some());
3072    }
3073
3074    /// A `Patch` for a peer already in the netmap merges only the fields it carries — here new UDP
3075    /// endpoints and a new home DERP — leaving the rest of the node intact. This is the fix for
3076    /// dropped `peers_changed_patch`: without it the netmap keeps stale endpoints and the peer can
3077    /// never re-handshake after it moves.
3078    #[tokio::test]
3079    async fn patch_merges_endpoints_and_derp_into_existing_peer() {
3080        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3081
3082        // Seed a peer (id == 1, per `peer_node`) with no endpoints / no DERP.
3083        let peer = peer_node("mover", [1u8; 32], vec![]);
3084        tracker.apply_peer_update(
3085            &ts_control::PeerUpdate::Full(vec![peer.clone()]),
3086            local_now(),
3087        );
3088        let (_pid, before) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3089        assert!(before.underlay_addresses.is_empty());
3090        assert!(before.derp_region.is_none());
3091
3092        // Patch in fresh reachability (the idle-peer-reconnect case).
3093        let new_ep: std::net::SocketAddr = "203.0.113.7:41641".parse().unwrap();
3094        let patch = ts_control::PeerChange {
3095            id: 1,
3096            derp_region: Some(ts_derp::RegionId(core::num::NonZeroU32::new(5).unwrap())),
3097            cap: None,
3098            cap_map: None,
3099            underlay_addresses: Some(vec![new_ep]),
3100            node_key: None,
3101            key_signature: None,
3102            disco_key: None,
3103            node_key_expiry: None,
3104            online: None,
3105            last_seen: None,
3106        };
3107        let (upserts, deletions) =
3108            tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3109
3110        assert_eq!(upserts.len(), 1);
3111        assert_eq!(deletions.len(), 0);
3112        // Same peer, now carrying the patched endpoint + DERP; node key untouched.
3113        assert_eq!(tracker.peer_db.peers().len(), 1);
3114        let (_pid, after) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3115        assert_eq!(after.underlay_addresses, vec![new_ep]);
3116        assert_eq!(
3117            after.derp_region,
3118            Some(ts_derp::RegionId(core::num::NonZeroU32::new(5).unwrap()))
3119        );
3120        assert_eq!(after.node_key, peer.node_key);
3121    }
3122
3123    /// Regression for `tsr-5u0`: when a whole-node set (`Delta`/`Full`) and a patch co-occur in one
3124    /// response, the patch is applied *on top of* the node the set just upserted — mirroring the
3125    /// handler's apply-order (peer set first, then `peer_patches`). Before the fix the patch shared
3126    /// the single `peer_update` slot and the co-occurring set silently dropped it, so a peer brought
3127    /// in by the delta kept stale (empty) reachability.
3128    #[tokio::test]
3129    async fn patch_applies_on_top_of_co_occurring_delta() {
3130        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3131
3132        // The whole-node delta upserts a brand-new peer (id == 1) with no reachability.
3133        let peer = peer_node("mover", [1u8; 32], vec![]);
3134        let (set_upserts, _) = tracker.apply_peer_update(
3135            &ts_control::PeerUpdate::Delta {
3136                upsert: vec![peer.clone()],
3137                remove: vec![],
3138            },
3139            local_now(),
3140        );
3141        assert_eq!(set_upserts.len(), 1, "delta upserts the new peer");
3142
3143        // The patch from the SAME response then sets that peer's endpoints + DERP. This is exactly
3144        // the consumer order the handler runs (apply_peer_update then apply_peer_patches).
3145        let new_ep: std::net::SocketAddr = "203.0.113.7:41641".parse().unwrap();
3146        let patch = ts_control::PeerChange {
3147            id: 1,
3148            derp_region: Some(ts_derp::RegionId(core::num::NonZeroU32::new(7).unwrap())),
3149            cap: None,
3150            cap_map: None,
3151            underlay_addresses: Some(vec![new_ep]),
3152            node_key: None,
3153            key_signature: None,
3154            disco_key: None,
3155            node_key_expiry: None,
3156            online: None,
3157            last_seen: None,
3158        };
3159        let (patch_upserts, patch_deletions) =
3160            tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3161
3162        assert_eq!(
3163            patch_upserts.len(),
3164            1,
3165            "patch re-upserts the just-added peer"
3166        );
3167        assert_eq!(patch_deletions.len(), 0);
3168        // The peer added by the delta now carries the patched reachability — the patch was NOT lost.
3169        let (_pid, after) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3170        assert_eq!(after.underlay_addresses, vec![new_ep]);
3171        assert_eq!(
3172            after.derp_region,
3173            Some(ts_derp::RegionId(core::num::NonZeroU32::new(7).unwrap()))
3174        );
3175    }
3176
3177    /// The node attribute by which control switches this node off the incremental netmap path
3178    /// (Go `tailcfg/nodecap`'s `DisableDeltaUpdates`).
3179    const DISABLE_DELTA_UPDATES: &str = "disable-delta-updates";
3180
3181    /// A second peer, distinct from `peer_node`'s single node in every indexed field: control node
3182    /// id, stable id, node key and tailnet addresses. Used as the peer NO patch names, so a test can
3183    /// tell "only the patched node was installed" from "the whole netmap was installed".
3184    fn other_peer_node(stable_id: &str) -> Node {
3185        let mut node = peer_node(stable_id, [2u8; 32], vec![]);
3186        node.id = 2;
3187        node.addresses = vec![
3188            "100.64.0.2/32".parse().unwrap(),
3189            "fd7a:115c:a1e0::2/128".parse().unwrap(),
3190        ];
3191        node.tailnet_address = TailnetAddress {
3192            ipv4: "100.64.0.2/32".parse().unwrap(),
3193            ipv6: "fd7a:115c:a1e0::2/128".parse().unwrap(),
3194        };
3195        node
3196    }
3197
3198    /// A self node carrying `attrs` in its capability map — the channel control uses to set node
3199    /// attributes, and the one `Node::delta_updates_disabled` reads.
3200    fn self_node_with(attrs: &[&str]) -> Node {
3201        let mut node = peer_node("self", [9u8; 32], vec![]);
3202        for attr in attrs {
3203            node.cap_map.insert((*attr).to_string(), vec![]);
3204        }
3205        node
3206    }
3207
3208    /// A reachability patch (new UDP endpoint) for the peer with control node id `id`.
3209    fn endpoint_patch(
3210        id: ts_control::NodeId,
3211        endpoint: std::net::SocketAddr,
3212    ) -> ts_control::PeerChange {
3213        ts_control::PeerChange {
3214            id,
3215            derp_region: None,
3216            cap: None,
3217            cap_map: None,
3218            underlay_addresses: Some(vec![endpoint]),
3219            node_key: None,
3220            key_signature: None,
3221            disco_key: None,
3222            node_key_expiry: None,
3223            online: None,
3224            last_seen: None,
3225        }
3226    }
3227
3228    /// The positive assertion the escape hatch is measured against: with `disable-delta-updates`
3229    /// ABSENT the patch path is exactly what it was — the patched peer is re-installed and reported,
3230    /// and a peer no patch names is left alone. This is the default and the overwhelmingly common
3231    /// case, so it is the one that must not move.
3232    #[tokio::test]
3233    async fn a_patch_without_the_attribute_installs_only_the_patched_peer() {
3234        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3235        tracker.apply_peer_update(
3236            &ts_control::PeerUpdate::Full(vec![
3237                peer_node("a", [1u8; 32], vec![]),
3238                other_peer_node("b"),
3239            ]),
3240            local_now(),
3241        );
3242        // A self node with no attributes at all: control has said nothing about delta updates.
3243        tracker.self_node = Some(self_node_with(&[]));
3244
3245        let new_ep: std::net::SocketAddr = "203.0.113.7:41641".parse().unwrap();
3246        let patch = endpoint_patch(1, new_ep);
3247        let (upserts, deletions) =
3248            tracker.apply_peer_patch_set(std::slice::from_ref(&patch), local_now());
3249
3250        assert!(deletions.is_empty());
3251        assert_eq!(
3252            upserts.len(),
3253            1,
3254            "the incremental path installs the patch's mutation, not the whole netmap"
3255        );
3256        let (patched_id, patched) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3257        assert_eq!(patched.underlay_addresses, vec![new_ep]);
3258        assert!(upserts.contains(&patched_id));
3259        let (unpatched_id, unpatched) = tracker.peer_db.get(&(2 as ts_control::NodeId)).unwrap();
3260        assert!(
3261            unpatched.underlay_addresses.is_empty(),
3262            "a peer no patch names keeps its own reachability"
3263        );
3264        assert!(
3265            !upserts.contains(&unpatched_id),
3266            "and is not reported as installed by the delta path"
3267        );
3268    }
3269
3270    /// Control's escape hatch: with `disable-delta-updates` set, a response carrying ONLY
3271    /// `PeersChangedPatch` is still applied — as a full netmap update. The attribute declines the
3272    /// incremental arm, exactly as Go's `tryHandleIncrementally` returning `false` does; it neither
3273    /// rejects the response nor drops the patches, so asserting the patch is *ignored* here would
3274    /// pin the opposite of upstream's behaviour.
3275    #[tokio::test]
3276    async fn disable_delta_updates_applies_a_patch_only_response_as_a_full_update() {
3277        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3278        tracker.apply_peer_update(
3279            &ts_control::PeerUpdate::Full(vec![
3280                peer_node("a", [1u8; 32], vec![]),
3281                other_peer_node("b"),
3282            ]),
3283            local_now(),
3284        );
3285        tracker.self_node = Some(self_node_with(&[DISABLE_DELTA_UPDATES]));
3286
3287        let new_ep: std::net::SocketAddr = "203.0.113.7:41641".parse().unwrap();
3288        let patch = endpoint_patch(1, new_ep);
3289        let (upserts, deletions) =
3290            tracker.apply_peer_patch_set(std::slice::from_ref(&patch), local_now());
3291
3292        assert!(deletions.is_empty(), "the fall-back never drops a peer");
3293        // Applied, not dropped: the patched field is in the netmap.
3294        let (patched_id, patched) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3295        assert_eq!(
3296            patched.underlay_addresses,
3297            vec![new_ep],
3298            "the patch is still applied under the attribute"
3299        );
3300        // And applied as a FULL update: every retained peer is re-installed and reported, not just
3301        // the one the patch named.
3302        assert_eq!(tracker.peer_db.peers().len(), 2, "no peer is evicted");
3303        let (unpatched_id, unpatched) = tracker.peer_db.get(&(2 as ts_control::NodeId)).unwrap();
3304        assert_eq!(
3305            upserts,
3306            HashSet::from_iter([patched_id, unpatched_id]),
3307            "the full arm installs the whole netmap, not the patch's mutation"
3308        );
3309        assert!(
3310            unpatched.underlay_addresses.is_empty(),
3311            "re-installing a peer no patch named does not invent state for it"
3312        );
3313    }
3314
3315    /// Under the attribute the `Peers*`-then-patch order the module documents is unchanged: the
3316    /// whole-node set is applied first (by the caller) and the patch lands on top of the node that
3317    /// set just upserted, rather than the patch being overwritten by the netmap re-install.
3318    #[tokio::test]
3319    async fn disable_delta_updates_keeps_the_peer_set_then_patch_ordering() {
3320        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3321        tracker.self_node = Some(self_node_with(&[DISABLE_DELTA_UPDATES]));
3322
3323        // The whole-node delta from this response brings in a peer with no reachability...
3324        tracker.apply_peer_update(
3325            &ts_control::PeerUpdate::Delta {
3326                upsert: vec![peer_node("mover", [1u8; 32], vec![])],
3327                remove: vec![],
3328            },
3329            local_now(),
3330        );
3331
3332        // ...and the patch from the SAME response then sets its endpoints, second.
3333        let new_ep: std::net::SocketAddr = "203.0.113.7:41641".parse().unwrap();
3334        let patch = endpoint_patch(1, new_ep);
3335        let (upserts, _deletions) =
3336            tracker.apply_peer_patch_set(std::slice::from_ref(&patch), local_now());
3337
3338        assert_eq!(upserts.len(), 1, "one peer in the netmap, so one installed");
3339        let (_pid, after) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3340        assert_eq!(
3341            after.underlay_addresses,
3342            vec![new_ep],
3343            "the patch is applied on top of the peer set, not lost to the re-install"
3344        );
3345    }
3346
3347    /// End-to-end through the LIVE actor, which is the only thing that proves the read is wired to
3348    /// the right node and the right response: control grants `disable-delta-updates` on the self
3349    /// node of the very response that carries the patches, and the patches must still land. A
3350    /// self-node attribute read from the wrong place (or one response late) would silently leave
3351    /// this node on the incremental path control just asked it to leave.
3352    #[tokio::test]
3353    async fn disable_delta_updates_takes_effect_on_the_response_that_grants_it() {
3354        use kameo::actor::Spawn as _;
3355
3356        let env = test_env();
3357        let (_tka_tx, tka_rx) = watch::channel(None);
3358        let tracker = PeerTracker::spawn((env.clone(), tka_rx));
3359
3360        // Await one reply first so `on_start` (which subscribes the actor to the bus) has run.
3361        assert!(
3362            tracker
3363                .ask(AllPeers)
3364                .await
3365                .expect("peer tracker started")
3366                .is_empty()
3367        );
3368
3369        env.publish(Arc::new(netmap_with_peers(vec![
3370            peer_node("a", [1u8; 32], vec![]),
3371            other_peer_node("b"),
3372        ])))
3373        .await
3374        .expect("publish netmap");
3375        await_peer_count(&tracker, 2).await;
3376
3377        // One response: the self node granting the attribute, and nothing but `PeersChangedPatch`.
3378        let new_ep: std::net::SocketAddr = "203.0.113.7:41641".parse().unwrap();
3379        env.publish(Arc::new(ts_control::StateUpdate {
3380            node: Some(self_node_with(&[DISABLE_DELTA_UPDATES])),
3381            peer_update: None,
3382            peer_patches: vec![endpoint_patch(1, new_ep)],
3383            ..netmap_with_peers(Vec::new())
3384        }))
3385        .await
3386        .expect("publish the patch-only response");
3387
3388        let settled = tokio::time::timeout(std::time::Duration::from_secs(10), async {
3389            loop {
3390                let peers = tracker.ask(AllPeers).await.expect("peer tracker is alive");
3391                if peers.iter().any(|p| p.underlay_addresses == vec![new_ep]) {
3392                    return peers;
3393                }
3394                tokio::time::sleep(std::time::Duration::from_millis(5)).await;
3395            }
3396        })
3397        .await;
3398        let peers = settled.expect("the patch-only response is applied under the attribute");
3399        assert_eq!(peers.len(), 2, "the fall-back to full evicts nobody");
3400    }
3401
3402    /// The node attribute by which control tells this node to stop heartbeating its peers (Go
3403    /// `tailcfg/nodecap`'s `SilentDisco`).
3404    const SILENT_DISCO: &str = "silent-disco";
3405
3406    /// A stand-in for the `Arc<PeerState>` subscribers (`DirectManager` among them) that records the
3407    /// `silent_disco` flag of every snapshot the tracker publishes, so a test can read what the
3408    /// direct manager would have been handed without standing up the socket, the dataplane and the
3409    /// DERP mesh behind it.
3410    struct SilentDiscoTap {
3411        seen: Arc<std::sync::Mutex<Vec<bool>>>,
3412    }
3413    impl kameo::Actor for SilentDiscoTap {
3414        type Args = Arc<std::sync::Mutex<Vec<bool>>>;
3415        type Error = Error;
3416        async fn on_start(seen: Self::Args, _s: ActorRef<Self>) -> Result<Self, Self::Error> {
3417            Ok(Self { seen })
3418        }
3419    }
3420    impl Message<Arc<PeerState>> for SilentDiscoTap {
3421        type Reply = ();
3422        async fn handle(&mut self, m: Arc<PeerState>, _c: &mut Context<Self, Self::Reply>) {
3423            self.seen.lock().expect("tap mutex").push(m.silent_disco);
3424        }
3425    }
3426
3427    /// End to end through the live tracker and the bus: control's `silent-disco` attribute on the
3428    /// **self** node rides the published peer snapshot, which is how it reaches
3429    /// `DirectManager` and, from there, the magicsock. Granting and withdrawing both land, and the
3430    /// attribute is read off the self node of the very response that carried it — the same timing
3431    /// `disable-delta-updates` has.
3432    #[tokio::test]
3433    async fn silent_disco_rides_the_published_peer_snapshot() {
3434        use kameo::actor::Spawn as _;
3435
3436        let env = test_env();
3437        let (_tka_tx, tka_rx) = watch::channel(None);
3438        let tracker = PeerTracker::spawn((env.clone(), tka_rx));
3439        let seen: Arc<std::sync::Mutex<Vec<bool>>> = Default::default();
3440        let tap = SilentDiscoTap::spawn(seen.clone());
3441        env.subscribe::<Arc<PeerState>>(&tap)
3442            .await
3443            .expect("subscribe the tap");
3444
3445        // Await one reply so the tracker's `on_start` (which subscribes it to the bus) has run.
3446        assert!(
3447            tracker
3448                .ask(AllPeers)
3449                .await
3450                .expect("peer tracker started")
3451                .is_empty()
3452        );
3453
3454        // A netmap whose self node carries the attribute.
3455        env.publish(Arc::new(ts_control::StateUpdate {
3456            node: Some(self_node_with(&[SILENT_DISCO])),
3457            ..netmap_with_peers(vec![peer_node("a", [1u8; 32], vec![])])
3458        }))
3459        .await
3460        .expect("publish the granting netmap");
3461        await_peer_count(&tracker, 1).await;
3462
3463        let granted = await_tap(&seen, 1).await;
3464        assert_eq!(
3465            granted.last(),
3466            Some(&true),
3467            "the attribute on the self node must reach the peer snapshot the direct manager reads"
3468        );
3469
3470        // Control withdraws it on the next netmap: the flag has to come back down, because
3471        // magicsock's setter is live and would otherwise leave the node silent forever.
3472        env.publish(Arc::new(ts_control::StateUpdate {
3473            node: Some(self_node_with(&[])),
3474            ..netmap_with_peers(vec![peer_node("a", [1u8; 32], vec![])])
3475        }))
3476        .await
3477        .expect("publish the withdrawing netmap");
3478
3479        let withdrawn = await_tap(&seen, granted.len() + 1).await;
3480        assert_eq!(
3481            withdrawn.last(),
3482            Some(&false),
3483            "withdrawing the attribute must publish the node back onto the heartbeat cadence"
3484        );
3485    }
3486
3487    /// Wait for the tap to have recorded at least `want` snapshots, returning them.
3488    async fn await_tap(seen: &Arc<std::sync::Mutex<Vec<bool>>>, want: usize) -> Vec<bool> {
3489        for _ in 0..1000 {
3490            {
3491                let got = seen.lock().expect("tap mutex");
3492                if got.len() >= want {
3493                    return got.clone();
3494                }
3495            }
3496            tokio::time::sleep(std::time::Duration::from_millis(5)).await;
3497        }
3498        panic!("the tap never saw {want} published peer snapshot(s)");
3499    }
3500
3501    /// A `Patch` whose node id is not in the current netmap is ignored (the wire contract: a patch
3502    /// never creates a node). No upsert, no deletion, peer set unchanged.
3503    #[tokio::test]
3504    async fn patch_for_unknown_node_is_ignored() {
3505        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3506        let known = peer_node("known", [1u8; 32], vec![]); // id == 1
3507        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![known]), local_now());
3508
3509        let patch = ts_control::PeerChange {
3510            id: 999, // not in the netmap
3511            derp_region: None,
3512            cap: None,
3513            cap_map: None,
3514            underlay_addresses: Some(vec!["198.51.100.9:1".parse().unwrap()]),
3515            node_key: None,
3516            key_signature: None,
3517            disco_key: None,
3518            node_key_expiry: None,
3519            online: None,
3520            last_seen: None,
3521        };
3522        let (upserts, deletions) =
3523            tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3524
3525        assert_eq!(upserts.len(), 0);
3526        assert_eq!(deletions.len(), 0);
3527        assert_eq!(tracker.peer_db.peers().len(), 1);
3528        assert!(tracker.peer_db.get(&(999 as ts_control::NodeId)).is_none());
3529    }
3530
3531    /// An expiry-only `Patch` updates `node_key_expiry` on the matching peer (Go
3532    /// `PeerChange.KeyExpiry`), rather than being silently dropped until the next full resync.
3533    #[tokio::test]
3534    async fn patch_updates_node_key_expiry() {
3535        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3536        let peer = peer_node("expiring", [1u8; 32], vec![]); // id == 1, node_key_expiry: None
3537        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer]), local_now());
3538
3539        let expiry = "2027-01-01T00:00:00Z"
3540            .parse::<chrono::DateTime<chrono::Utc>>()
3541            .unwrap();
3542        let patch = ts_control::PeerChange {
3543            id: 1,
3544            derp_region: None,
3545            cap: None,
3546            cap_map: None,
3547            underlay_addresses: None,
3548            node_key: None,
3549            key_signature: None,
3550            disco_key: None,
3551            node_key_expiry: Some(expiry),
3552            online: None,
3553            last_seen: None,
3554        };
3555        tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3556
3557        let (_pid, after) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3558        assert_eq!(after.node_key_expiry, Some(expiry));
3559    }
3560
3561    /// Channel B: a `PeerChange.online` patch flips a peer's online state without a full node.
3562    #[tokio::test]
3563    async fn patch_updates_online() {
3564        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3565        let peer = peer_node("p", [1u8; 32], vec![]); // id == 1, online: None
3566        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer]), local_now());
3567        assert_eq!(
3568            tracker
3569                .peer_db
3570                .get(&(1 as ts_control::NodeId))
3571                .unwrap()
3572                .1
3573                .online,
3574            None
3575        );
3576
3577        let mut patch = ts_control::PeerChange {
3578            id: 1,
3579            derp_region: None,
3580            cap: None,
3581            cap_map: None,
3582            underlay_addresses: None,
3583            node_key: None,
3584            key_signature: None,
3585            disco_key: None,
3586            node_key_expiry: None,
3587            online: Some(true),
3588            last_seen: None,
3589        };
3590        tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3591        assert_eq!(
3592            tracker
3593                .peer_db
3594                .get(&(1 as ts_control::NodeId))
3595                .unwrap()
3596                .1
3597                .online,
3598            Some(true),
3599            "PeerChange.online=Some(true) marks the peer online"
3600        );
3601
3602        // A subsequent patch flips it offline.
3603        patch.online = Some(false);
3604        tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3605        assert_eq!(
3606            tracker
3607                .peer_db
3608                .get(&(1 as ts_control::NodeId))
3609                .unwrap()
3610                .1
3611                .online,
3612            Some(false)
3613        );
3614    }
3615
3616    /// Channel C/D (Go `map.go:updatePeersStateFromResponse`): `online_change` is the sole driver of
3617    /// `online`; `peer_seen_change` is the sole driver of `last_seen` (true ⇒ now, false ⇒ cleared)
3618    /// and must NEVER touch `online`. Both apply to a peer already in the netmap and ignore unknown
3619    /// ids. This pins the fix for the prior bug where channel D wrote `online=false` (conflating
3620    /// "not seen recently" with "offline" — distinct signals in Go).
3621    #[tokio::test]
3622    async fn liveness_change_maps_apply_online() {
3623        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3624        let peer = peer_node("p", [1u8; 32], vec![]); // id == 1
3625        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer]), local_now());
3626        // A fixed timestamp (chrono is built without its `clock` feature, so no `Utc::now()`).
3627        let now = chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap();
3628
3629        // Channel C: online_change sets online=true.
3630        let mut online_change = std::collections::BTreeMap::new();
3631        online_change.insert(1 as ts_control::NodeId, true);
3632        online_change.insert(999 as ts_control::NodeId, true); // unknown id — ignored
3633        let changed = tracker.apply_liveness_changes(&online_change, &Default::default(), now);
3634        assert!(changed);
3635        assert_eq!(
3636            tracker
3637                .peer_db
3638                .get(&(1 as ts_control::NodeId))
3639                .unwrap()
3640                .1
3641                .online,
3642            Some(true)
3643        );
3644
3645        // Channel D: peer_seen_change=true sets last_seen=now and leaves online UNTOUCHED.
3646        let mut seen_true = std::collections::BTreeMap::new();
3647        seen_true.insert(1 as ts_control::NodeId, true);
3648        let changed = tracker.apply_liveness_changes(&Default::default(), &seen_true, now);
3649        assert!(changed);
3650        {
3651            let (_id, node) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3652            assert_eq!(
3653                node.last_seen,
3654                Some(now),
3655                "peer_seen_change=true sets last_seen=now"
3656            );
3657            assert_eq!(
3658                node.online,
3659                Some(true),
3660                "channel D must NOT touch online (still true from channel C)"
3661            );
3662        }
3663
3664        // Channel D: peer_seen_change=false clears last_seen, still leaving online untouched.
3665        let mut seen_false = std::collections::BTreeMap::new();
3666        seen_false.insert(1 as ts_control::NodeId, false);
3667        let changed = tracker.apply_liveness_changes(&Default::default(), &seen_false, now);
3668        assert!(changed);
3669        {
3670            let (_id, node) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3671            assert_eq!(
3672                node.last_seen, None,
3673                "peer_seen_change=false clears last_seen"
3674            );
3675            assert_eq!(node.online, Some(true), "channel D must NOT mark offline");
3676        }
3677        assert_eq!(
3678            tracker.peer_db.peers().len(),
3679            1,
3680            "the node is retained, not removed"
3681        );
3682
3683        // No-op when nothing matches / changes.
3684        assert!(!tracker.apply_liveness_changes(&Default::default(), &Default::default(), now));
3685    }
3686
3687    /// Security: a `Patch` that rotates the node key must re-satisfy the tailnet-lock authority,
3688    /// exactly like a `Delta` upsert. A key-rotation patch whose new signature does NOT verify
3689    /// evicts the peer (fail-closed) rather than leaving a now-unverified entry — closing what would
3690    /// otherwise be a trust-enforcement bypass via the patch path.
3691    #[tokio::test]
3692    async fn patch_key_rotation_failing_tka_evicts_peer() {
3693        let (authority, sig) = authority_and_valid_sig();
3694        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
3695
3696        // Admit a correctly-signed peer (id == 1).
3697        let good = peer_node("rotator", NODE_KEY_BYTES, sig.clone());
3698        tracker.apply_peer_update(
3699            &ts_control::PeerUpdate::Full(vec![good.clone()]),
3700            local_now(),
3701        );
3702        assert_eq!(tracker.peer_db.peers().len(), 1);
3703
3704        // Patch a new node key whose signature is garbage under the active authority.
3705        let patch = ts_control::PeerChange {
3706            id: 1,
3707            derp_region: None,
3708            cap: None,
3709            cap_map: None,
3710            underlay_addresses: None,
3711            node_key: Some([0x33u8; 32].into()),
3712            key_signature: Some(vec![0x00, 0x01, 0x02]),
3713            disco_key: None,
3714            node_key_expiry: None,
3715            online: None,
3716            last_seen: None,
3717        };
3718        let (upserts, deletions) =
3719            tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3720
3721        assert_eq!(upserts.len(), 0);
3722        assert_eq!(deletions.len(), 1);
3723        assert_eq!(tracker.peer_db.peers().len(), 0);
3724    }
3725
3726    /// A node's `user_id` joins against the accumulated UserProfiles table to resolve the owning
3727    /// user's profile in `WhoIs.user_profile`. With no matching profile, it is `None` (the
3728    /// pre-existing behavior); once a profile arrives, the same node resolves to it. This
3729    /// proves the accumulate-then-join path the netmap handler builds.
3730    fn profile(id: ts_control::UserId, login: &str) -> ts_control::UserProfile {
3731        ts_control::UserProfile {
3732            id,
3733            login_name: login.to_string(),
3734            display_name: None,
3735            groups: Vec::new(),
3736        }
3737    }
3738
3739    #[tokio::test]
3740    async fn whois_resolves_user_from_accumulated_profiles() {
3741        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3742
3743        // A peer owned by user id 42 at 100.64.0.1 (the peer_node fixture's address).
3744        let mut peer = peer_node("p", NODE_KEY_BYTES, Vec::new());
3745        peer.user_id = 42;
3746        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer]), local_now());
3747        let addr = "100.64.0.1:0".parse().unwrap();
3748
3749        // No profile yet: the node resolves but its owner is unknown.
3750        let who = tracker.whois_opt(addr).expect("peer is known");
3751        assert_eq!(who.user_profile, None);
3752        assert_eq!(who.user(), None);
3753
3754        // Profile for a DIFFERENT user must not match.
3755        tracker
3756            .user_profiles
3757            .insert(7, profile(7, "someone-else@example.com"));
3758        assert_eq!(tracker.whois_opt(addr).unwrap().user(), None);
3759
3760        // The owning user's profile arrives (as the netmap handler would accumulate it): now the
3761        // login resolves.
3762        tracker
3763            .user_profiles
3764            .insert(42, profile(42, "alice@example.com"));
3765        assert_eq!(
3766            tracker.whois_opt(addr).unwrap().user(),
3767            Some("alice@example.com".to_string())
3768        );
3769    }
3770
3771    /// The whole carry, end to end: a real `MapResponse` body — the JSON control writes on the map
3772    /// poll — decoded by the production wire types and the production `From` impls, accumulated by
3773    /// the production profile merge, and read back out of `whois`.
3774    ///
3775    /// `Groups` is why `WhoIs` carries the profile rather than one display label: it is the only
3776    /// attribute of an owning user that a node cannot re-derive from anything else in the netmap,
3777    /// so an embedder authorising an inbound connection on group membership has no other source
3778    /// for it. Every hop here is production code; the only thing the test assembles is the
3779    /// `StateUpdate` struct itself (`ts_control`'s frame decode is not public, and its own tests
3780    /// pin the body-to-`StateUpdate` half).
3781    fn state_update_from_body(body: &str) -> ts_control::StateUpdate {
3782        let wire: ts_control_serde::MapResponse<'_> =
3783            serde_json::from_str(body).expect("a real MapResponse body decodes");
3784        let peers = wire
3785            .peers
3786            .as_ref()
3787            .expect("the fixture carries a full peer set")
3788            .iter()
3789            .map(ts_control::Node::from)
3790            .collect();
3791        ts_control::StateUpdate {
3792            user_profiles: wire
3793                .user_profiles
3794                .iter()
3795                .map(ts_control::UserProfile::from)
3796                .collect(),
3797            ..netmap_with_peers(peers)
3798        }
3799    }
3800
3801    /// The body control sends for a tailnet with one peer owned by user 42, whose profile carries
3802    /// `Groups`. `groups` is spliced in so the present and absent cases share one fixture.
3803    fn netmap_body_with_profile_groups(groups: &str) -> String {
3804        format!(
3805            r#"{{
3806                "MapSessionHandle": "sess-1",
3807                "Seq": 9,
3808                "Peers": [{{
3809                    "ID": 2,
3810                    "StableID": "peer-2",
3811                    "Name": "peer.example.ts.net.",
3812                    "Addresses": ["100.64.0.1/32", "fd7a:115c:a1e0::1/128"],
3813                    "User": 42
3814                }}],
3815                "UserProfiles": [{{
3816                    "ID": 42,
3817                    "LoginName": "alice@example.com",
3818                    "DisplayName": "Alice Smith"{groups}
3819                }}]
3820            }}"#
3821        )
3822    }
3823
3824    #[tokio::test]
3825    async fn whois_carries_user_groups_from_a_real_map_response() {
3826        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3827        let update = state_update_from_body(&netmap_body_with_profile_groups(
3828            r#", "Groups": ["engineering@example.com", "group:eng"]"#,
3829        ));
3830
3831        tracker.accumulate_user_profiles(&update.user_profiles);
3832        tracker.apply_peer_update(
3833            update.peer_update.as_ref().expect("a full peer set"),
3834            local_now(),
3835        );
3836
3837        let who = tracker
3838            .whois_opt("100.64.0.1:0".parse().unwrap())
3839            .expect("the peer owns that address");
3840
3841        let profile = who.user_profile.as_ref().expect("user 42's profile");
3842        assert_eq!(profile.id, 42);
3843        assert_eq!(profile.login_name, "alice@example.com");
3844        assert_eq!(profile.display_name.as_deref(), Some("Alice Smith"));
3845        assert_eq!(
3846            who.user_groups(),
3847            ["engineering@example.com", "group:eng"],
3848            "the groups control reported reach the embedder in the order control sent them"
3849        );
3850        // The flattened label the pre-widening `WhoIs.user` field carried is unchanged.
3851        assert_eq!(who.user(), Some("alice@example.com".to_string()));
3852    }
3853
3854    /// The absent case, which is what every control server that does not send the field looks
3855    /// like: no `Groups` key at all. That must yield a profile with an EMPTY group list — never a
3856    /// missing profile, and never a failed decode that would drop the owner identity entirely.
3857    #[tokio::test]
3858    async fn a_map_response_without_groups_yields_an_empty_group_list() {
3859        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3860        let update = state_update_from_body(&netmap_body_with_profile_groups(""));
3861
3862        tracker.accumulate_user_profiles(&update.user_profiles);
3863        tracker.apply_peer_update(
3864            update.peer_update.as_ref().expect("a full peer set"),
3865            local_now(),
3866        );
3867
3868        let who = tracker
3869            .whois_opt("100.64.0.1:0".parse().unwrap())
3870            .expect("the peer owns that address");
3871
3872        assert!(
3873            who.user_profile.is_some(),
3874            "an omitted Groups must not cost us the profile"
3875        );
3876        assert_eq!(who.user(), Some("alice@example.com".to_string()));
3877        assert!(who.user_groups().is_empty());
3878    }
3879
3880    /// Control sends profiles incrementally, so a later response restating user 42 replaces the
3881    /// held copy wholesale — including a group list that SHRANK. A membership control has revoked
3882    /// must stop being reported, or an embedder authorising on it keeps honouring it forever.
3883    #[tokio::test]
3884    async fn a_restated_profile_replaces_the_held_group_list() {
3885        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3886        let first = state_update_from_body(&netmap_body_with_profile_groups(
3887            r#", "Groups": ["group:eng", "group:oncall"]"#,
3888        ));
3889        tracker.accumulate_user_profiles(&first.user_profiles);
3890        tracker.apply_peer_update(
3891            first.peer_update.as_ref().expect("a full peer set"),
3892            local_now(),
3893        );
3894
3895        let second = state_update_from_body(&netmap_body_with_profile_groups(
3896            r#", "Groups": ["group:eng"]"#,
3897        ));
3898        tracker.accumulate_user_profiles(&second.user_profiles);
3899
3900        let who = tracker
3901            .whois_opt("100.64.0.1:0".parse().unwrap())
3902            .expect("the peer owns that address");
3903        assert_eq!(who.user_groups(), ["group:eng"]);
3904    }
3905
3906    /// `UserProfile::best_label` prefers the login name, falling back to display name, else `None`.
3907    #[test]
3908    fn user_profile_best_label_prefers_login() {
3909        assert_eq!(
3910            profile(1, "alice@example.com").best_label(),
3911            Some("alice@example.com".to_string())
3912        );
3913        let display_only = ts_control::UserProfile {
3914            id: 2,
3915            login_name: String::new(),
3916            display_name: Some("Bob".to_string()),
3917            groups: Vec::new(),
3918        };
3919        assert_eq!(display_only.best_label(), Some("Bob".to_string()));
3920        let empty = ts_control::UserProfile {
3921            id: 3,
3922            login_name: String::new(),
3923            display_name: None,
3924            groups: Vec::new(),
3925        };
3926        assert_eq!(empty.best_label(), None);
3927    }
3928
3929    // ----- tsr-jo1: RotationTracker (Go ipnlocal.rotationTracker.obsoleteKeys) -----
3930
3931    /// A `RotationDetails` for a `Direct`-rooted chain with the given prior keys + wrapping key.
3932    fn rot_details(
3933        prev: &[&[u8]],
3934        wrapping: &[u8],
3935        kind: ts_tka::SigKind,
3936    ) -> ts_tka::RotationDetails {
3937        ts_tka::RotationDetails {
3938            prev_node_keys: prev.iter().map(|p| p.to_vec()).collect(),
3939            initial_sig_kind: kind,
3940            initial_wrapping_pubkey: wrapping.to_vec(),
3941        }
3942    }
3943
3944    /// Rule 1: every prior node key named by any rotation chain is obsolete, regardless of the
3945    /// chain's root kind (Go's ungated `obsolete.AddSlice(d.PrevNodeKeys)`).
3946    #[test]
3947    fn rotation_tracker_prev_keys_always_obsolete() {
3948        let mut t = RotationTracker::default();
3949        // A Direct-rooted chain that rotated away OLD1, and a Credential-rooted one that rotated OLD2.
3950        t.add(
3951            b"newA".to_vec(),
3952            &rot_details(&[b"OLD1"], b"wrapA", ts_tka::SigKind::Direct),
3953        );
3954        t.add(
3955            b"newB".to_vec(),
3956            &rot_details(&[b"OLD2"], b"wrapB", ts_tka::SigKind::Credential),
3957        );
3958        let obsolete = t.obsolete_keys();
3959        assert!(
3960            obsolete.contains(b"OLD1".as_slice()),
3961            "Direct chain's prior key obsolete"
3962        );
3963        assert!(
3964            obsolete.contains(b"OLD2".as_slice()),
3965            "Credential chain's prior key obsolete too (rule 1 is ungated)"
3966        );
3967        // The current keys themselves are not obsolete (only one peer per wrapping key here).
3968        assert!(!obsolete.contains(b"newA".as_slice()));
3969        assert!(!obsolete.contains(b"newB".as_slice()));
3970    }
3971
3972    /// Rule 2: among `Direct`-rooted chains sharing a wrapping key, only the longest survives; the
3973    /// shorter (older) clone's key is obsolete.
3974    #[test]
3975    fn rotation_tracker_unequal_chain_keeps_longest() {
3976        let mut t = RotationTracker::default();
3977        // Same wrapping key; "long" has 2 prior keys, "short" has 1 ⇒ "short" is the older clone.
3978        t.add(
3979            b"long".to_vec(),
3980            &rot_details(&[b"p1", b"p2"], b"wrap", ts_tka::SigKind::Direct),
3981        );
3982        t.add(
3983            b"short".to_vec(),
3984            &rot_details(&[b"q1"], b"wrap", ts_tka::SigKind::Direct),
3985        );
3986        let obsolete = t.obsolete_keys();
3987        assert!(
3988            obsolete.contains(b"short".as_slice()),
3989            "the shorter-chain clone is obsolete"
3990        );
3991        assert!(
3992            !obsolete.contains(b"long".as_slice()),
3993            "the longest-chain peer survives"
3994        );
3995    }
3996
3997    /// Rule 2 tie: two `Direct`-rooted chains sharing a wrapping key with EQUAL chain length cannot
3998    /// be disambiguated ⇒ BOTH are dropped (Go's safety branch).
3999    #[test]
4000    fn rotation_tracker_equal_chain_drops_both() {
4001        let mut t = RotationTracker::default();
4002        t.add(
4003            b"cloneA".to_vec(),
4004            &rot_details(&[b"p1"], b"wrap", ts_tka::SigKind::Direct),
4005        );
4006        t.add(
4007            b"cloneB".to_vec(),
4008            &rot_details(&[b"p2"], b"wrap", ts_tka::SigKind::Direct),
4009        );
4010        let obsolete = t.obsolete_keys();
4011        assert!(
4012            obsolete.contains(b"cloneA".as_slice()),
4013            "tied clone A dropped"
4014        );
4015        assert!(
4016            obsolete.contains(b"cloneB".as_slice()),
4017            "tied clone B dropped"
4018        );
4019    }
4020
4021    /// `Credential`-rooted chains sharing a wrapping key are EXEMPT from rule 2 (reusable-authkey
4022    /// carve-out): both are kept even with equal chain length.
4023    #[test]
4024    fn rotation_tracker_credential_root_clones_both_kept() {
4025        let mut t = RotationTracker::default();
4026        t.add(
4027            b"credA".to_vec(),
4028            &rot_details(&[b"p1"], b"wrap", ts_tka::SigKind::Credential),
4029        );
4030        t.add(
4031            b"credB".to_vec(),
4032            &rot_details(&[b"p2"], b"wrap", ts_tka::SigKind::Credential),
4033        );
4034        let obsolete = t.obsolete_keys();
4035        assert!(
4036            !obsolete.contains(b"credA".as_slice()),
4037            "credential-rooted clone A kept"
4038        );
4039        assert!(
4040            !obsolete.contains(b"credB".as_slice()),
4041            "credential-rooted clone B kept"
4042        );
4043    }
4044
4045    /// A peer that another chain already rotated away does not also act as a surviving clone: it is
4046    /// removed from its wrapping-key group before the longest-survivor pick (Go's `DeleteFunc`).
4047    #[test]
4048    fn rotation_tracker_already_obsolete_peer_not_a_survivor() {
4049        let mut t = RotationTracker::default();
4050        // "victim" is rotated away by "rotator" (different wrapping key), AND shares wrapping key
4051        // "w" with "other". Because "victim" is already obsolete, only "other" is in play for "w" and
4052        // survives (no spurious tie-drop of "other").
4053        t.add(
4054            b"rotator".to_vec(),
4055            &rot_details(&[b"victim"], b"wRot", ts_tka::SigKind::Direct),
4056        );
4057        t.add(
4058            b"victim".to_vec(),
4059            &rot_details(&[b"x"], b"w", ts_tka::SigKind::Direct),
4060        );
4061        t.add(
4062            b"other".to_vec(),
4063            &rot_details(&[b"y"], b"w", ts_tka::SigKind::Direct),
4064        );
4065        let obsolete = t.obsolete_keys();
4066        assert!(
4067            obsolete.contains(b"victim".as_slice()),
4068            "victim rotated away by rotator"
4069        );
4070        assert!(
4071            !obsolete.contains(b"other".as_slice()),
4072            "other survives — victim was removed from the group before the tie check"
4073        );
4074    }
4075
4076    /// Empty tracker (no rotation-signed peers) ⇒ no obsolete keys (the non-rotation netmap path).
4077    #[test]
4078    fn rotation_tracker_empty_is_noop() {
4079        let t = RotationTracker::default();
4080        assert!(t.obsolete_keys().is_empty());
4081    }
4082
4083    /// End-to-end through the real `Full` path: a peer presenting a freshly-rotated key (a Rotation
4084    /// chain) is admitted, while a second peer still presenting the rotated-AWAY pivot key — even with
4085    /// that key's own still-valid Direct signature — is DROPPED by the cross-peer rotation filter.
4086    /// This is the gap closed here: Go `tkaFilterNetmapLocked` drops the stale clone; we used to admit
4087    /// it. Uses real `ts_tka` signing (`sign_direct` + `sign_rotation`) so the whole
4088    /// verify → details → filter pipeline runs.
4089    ///
4090    /// Construction: the trusted key signs an inner `Direct` over the PIVOT keypair's public key; the
4091    /// pivot key then signs an outer `Rotation` authorizing `new_key`. That chain's `prev_node_keys`
4092    /// names the pivot pubkey — so a peer presenting the pivot pubkey as its node key is the
4093    /// rotated-away key the filter must drop.
4094    #[tokio::test]
4095    async fn tka_full_drops_rotated_away_key_e2e() {
4096        use ed25519_dalek::SigningKey;
4097        use ts_tka::NodeKeySignature;
4098
4099        let trusted = SigningKey::from_bytes(&[42u8; 32]);
4100        let trusted_pub = trusted.verifying_key().to_bytes().to_vec();
4101        let authority = Authority::from_state(
4102            AumHash([0; 32]),
4103            State {
4104                keys: vec![Key {
4105                    kind: KeyKind::Ed25519,
4106                    votes: 1,
4107                    public: trusted_pub.clone(),
4108                }],
4109            },
4110        );
4111
4112        // The rotation pivot: a keypair whose public key the inner Direct authorizes and whose
4113        // private key signs the outer rotation wrap. This pivot pubkey IS the key being rotated away.
4114        let pivot = SigningKey::from_bytes(&[9u8; 32]);
4115        let pivot_pub: [u8; 32] = pivot.verifying_key().to_bytes();
4116
4117        let new_key = [4u8; 32]; // the freshly-rotated node key
4118
4119        // Fresh peer: a Rotation chain authorizing `new_key`, inner Direct over the pivot signed by
4120        // trusted, outer wrap signed by the pivot. Its prev_node_keys names `pivot_pub`.
4121        let new_sig = NodeKeySignature::sign_rotation(&new_key, &trusted, &pivot).serialize();
4122        let new_peer = peer_node("rotated", new_key, new_sig);
4123
4124        // Stale peer: still presents the pivot pubkey (the rotated-away key) with its own valid
4125        // Direct signature — valid in isolation, but obsoleted by the fresh peer's rotation chain.
4126        let stale_sig = NodeKeySignature::sign_direct(&pivot_pub, &trusted).serialize();
4127        let stale_peer = peer_node("stale", pivot_pub, stale_sig);
4128
4129        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
4130        tracker.apply_peer_update(
4131            &ts_control::PeerUpdate::Full(vec![new_peer.clone(), stale_peer.clone()]),
4132            local_now(),
4133        );
4134
4135        assert!(
4136            tracker.peer_db.get(&new_peer.node_key).is_some(),
4137            "the freshly-rotated peer is admitted"
4138        );
4139        assert!(
4140            tracker.peer_db.get(&stale_peer.node_key).is_none(),
4141            "the peer presenting the rotated-away key is dropped (Go tkaFilterNetmapLocked)"
4142        );
4143    }
4144}
4145
4146#[cfg(test)]
4147mod tsmp_disco_key_tests {
4148    //! Receive side of the TSMP disco-key advertisement, at the point the key is *learned*.
4149    //!
4150    //! These exercise [`PeerTracker::learn_disco_key`] — the fork's stand-in for Go
4151    //! `magicsock.Conn.HandleDiscoKeyAdvertisement` — which is the single place an advertisement
4152    //! reaches peer state. The wire decode and the "consumed, not delivered" drop are covered in
4153    //! `ts_packet::tsmp` and `ts_dataplane` respectively.
4154
4155    use ts_keys::DiscoPublicKey;
4156
4157    use super::{
4158        tka_tests::{peer_node, test_env},
4159        *,
4160    };
4161
4162    /// The key a peer advertises, and a second one for the re-advertise case.
4163    const ADVERTISED: [u8; 32] = [0xa5u8; 32];
4164    const READVERTISED: [u8; 32] = [0x5au8; 32];
4165    /// The (staler) key control has for that same peer, and the one control eventually catches up
4166    /// to.
4167    const FROM_CONTROL: [u8; 32] = [0xc0u8; 32];
4168    const CONTROL_CAUGHT_UP: [u8; 32] = [0x0cu8; 32];
4169
4170    /// The node key of the single peer these tests use.
4171    const PEER_NODE_KEY: [u8; 32] = [1u8; 32];
4172
4173    /// A tracker holding one peer with no disco key yet, plus that peer's [`PeerId`].
4174    fn tracker_with_peer() -> (PeerTracker, PeerId) {
4175        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
4176        let node = peer_node("peer", PEER_NODE_KEY, Vec::new());
4177        let id = tracker.peer_db.upsert(&node);
4178        (tracker, id)
4179    }
4180
4181    /// The peer as CONTROL describes it: the same node, carrying whatever disco key the netmap says
4182    /// it has (`None` for a peer control has no disco key for at all).
4183    fn node_from_control(disco_key: Option<[u8; 32]>) -> Node {
4184        let mut node = peer_node("peer", PEER_NODE_KEY, Vec::new());
4185        node.disco_key = disco_key.map(DiscoPublicKey::from);
4186        node
4187    }
4188
4189    /// A netmap `Full` carrying just this peer, as control currently describes it.
4190    fn control_full(disco_key: Option<[u8; 32]>) -> ts_control::PeerUpdate {
4191        ts_control::PeerUpdate::Full(vec![node_from_control(disco_key)])
4192    }
4193
4194    /// A tracker whose single peer arrived through the netmap carrying `disco_key`, exactly as the
4195    /// actor's handler applies it. Returns the peer's [`PeerId`] too.
4196    fn tracker_with_control_peer(disco_key: Option<[u8; 32]>) -> (PeerTracker, PeerId) {
4197        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
4198        let node = node_from_control(disco_key);
4199        tracker.apply_peer_update(&control_full(disco_key), local_now());
4200        let id = tracker
4201            .peer_db
4202            .has(&node.node_key)
4203            .expect("control delivered it");
4204        (tracker, id)
4205    }
4206
4207    /// The disco key the peer db currently holds for `peer` — the effective key every direct-path
4208    /// consumer resolves against.
4209    fn effective_key(tracker: &PeerTracker, peer: PeerId) -> Option<DiscoPublicKey> {
4210        tracker
4211            .peer_db
4212            .get(&peer)
4213            .expect("peer still present")
4214            .1
4215            .disco_key
4216    }
4217
4218    /// The happy path: an advertised key is applied to the peer AND lands in the disco index, which
4219    /// is what the direct-path machinery (`direct::DiscoPeerLookup`) reads. Re-advertising the same
4220    /// key is a no-op; advertising a different one replaces it, retracting the old index entry.
4221    #[tokio::test]
4222    async fn advertisement_learns_the_peers_disco_key() {
4223        let (mut tracker, peer) = tracker_with_peer();
4224        let key = DiscoPublicKey::from(ADVERTISED);
4225
4226        assert!(
4227            tracker.learn_disco_key(peer, key),
4228            "a first advertisement changes the peer db"
4229        );
4230        assert_eq!(
4231            tracker
4232                .peer_db
4233                .get(&peer)
4234                .expect("peer still present")
4235                .1
4236                .disco_key,
4237            Some(key),
4238            "the advertised disco key is learned"
4239        );
4240        assert_eq!(
4241            tracker.peer_db.has(&key),
4242            Some(peer),
4243            "and is reachable through the disco index the direct path resolves against"
4244        );
4245
4246        assert!(
4247            !tracker.learn_disco_key(peer, key),
4248            "re-advertising the same key is a no-op (Go counts it 'unchanged' and returns)"
4249        );
4250
4251        let rotated = DiscoPublicKey::from(READVERTISED);
4252        assert!(tracker.learn_disco_key(peer, rotated));
4253        assert_eq!(
4254            tracker
4255                .peer_db
4256                .get(&peer)
4257                .expect("peer still present")
4258                .1
4259                .disco_key,
4260            Some(rotated),
4261            "a later advertisement replaces the key without a netmap update"
4262        );
4263        assert_eq!(tracker.peer_db.has(&rotated), Some(peer));
4264        assert_eq!(
4265            tracker.peer_db.has(&key),
4266            None,
4267            "the superseded key no longer resolves to the peer"
4268        );
4269    }
4270
4271    /// The refusals, each of which must leave the peer db untouched: the zero key is never learned,
4272    /// and an advertisement never creates a peer.
4273    #[tokio::test]
4274    async fn refused_advertisements_change_nothing() {
4275        let (mut tracker, peer) = tracker_with_peer();
4276
4277        assert!(
4278            !tracker.learn_disco_key(peer, DiscoPublicKey::from([0u8; 32])),
4279            "the zero key is never learned"
4280        );
4281        assert_eq!(
4282            tracker
4283                .peer_db
4284                .get(&peer)
4285                .expect("peer still present")
4286                .1
4287                .disco_key,
4288            None,
4289            "a zero-key advertisement must not bind the peer to an unusable key"
4290        );
4291
4292        // An advertisement for a peer control has never told us about. Go logs "endpoint not found
4293        // for node" and returns; it must not conjure a peer into existence.
4294        let unknown = PeerId(4242);
4295        assert_eq!(tracker.peer_db.get(&unknown), None, "precondition");
4296        assert!(
4297            !tracker.learn_disco_key(unknown, DiscoPublicKey::from(ADVERTISED)),
4298            "an advertisement for an unknown peer is ignored"
4299        );
4300        assert_eq!(
4301            tracker.peer_db.peers().len(),
4302            1,
4303            "an advertisement never creates a peer — only control does"
4304        );
4305        assert_eq!(
4306            tracker.peer_db.has(&DiscoPublicKey::from(ADVERTISED)),
4307            None,
4308            "and never indexes a key against a peer that does not exist"
4309        );
4310    }
4311
4312    /// The feature's motivating case, end to end: the peer told us a key control has not caught up
4313    /// with, and then control polls again with the SAME stale key it had before. The advertisement
4314    /// must survive.
4315    ///
4316    /// Go keeps the two keys apart on the endpoint (`endpointDisco.controlKey` /
4317    /// `tsmpKey`), and `updateFromNode` only rewrites the control side when control's key actually
4318    /// changed — so a netmap restating the old key never touches the active TSMP key. With a single
4319    /// field the next map poll silently reverted the peer to control's stale key, which is precisely
4320    /// the state the advertisement exists to escape.
4321    #[tokio::test]
4322    async fn netmap_restating_controls_stale_key_keeps_the_tsmp_key() {
4323        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4324        let advertised = DiscoPublicKey::from(ADVERTISED);
4325        assert_eq!(
4326            effective_key(&tracker, peer),
4327            Some(DiscoPublicKey::from(FROM_CONTROL)),
4328            "precondition: the peer starts on the key control gave us"
4329        );
4330
4331        assert!(tracker.learn_disco_key(peer, advertised));
4332        assert_eq!(effective_key(&tracker, peer), Some(advertised));
4333
4334        // Control polls again, still behind: a `Full` resync, then a `Delta` re-upsert, both
4335        // carrying the key control already sent.
4336        tracker.apply_peer_update(&control_full(Some(FROM_CONTROL)), local_now());
4337        assert_eq!(
4338            effective_key(&tracker, peer),
4339            Some(advertised),
4340            "a Full restating control's stale key must not undo the TSMP-learned key"
4341        );
4342        tracker.apply_peer_update(
4343            &ts_control::PeerUpdate::Delta {
4344                upsert: vec![node_from_control(Some(FROM_CONTROL))],
4345                remove: vec![],
4346            },
4347            local_now(),
4348        );
4349        assert_eq!(
4350            effective_key(&tracker, peer),
4351            Some(advertised),
4352            "and neither must a Delta re-upsert of the same node"
4353        );
4354        assert_eq!(
4355            tracker.peer_db.has(&advertised),
4356            Some(peer),
4357            "the direct path still resolves the peer by the key it advertised"
4358        );
4359        assert_eq!(
4360            tracker.peer_db.has(&DiscoPublicKey::from(FROM_CONTROL)),
4361            None,
4362            "and control's superseded key does not resolve to it"
4363        );
4364
4365        // Control finally changes its mind. The new key is recorded in control's slot, but the key
4366        // the peer itself told us stays active — upstream returns to control's key only when disco
4367        // is received under it (`endpoint.checkAndUpdateDiscoKey`).
4368        tracker.apply_peer_update(&control_full(Some(CONTROL_CAUGHT_UP)), local_now());
4369        assert_eq!(
4370            effective_key(&tracker, peer),
4371            Some(advertised),
4372            "a control-side key change must not preempt an active TSMP-learned key"
4373        );
4374        assert_eq!(
4375            tracker.control_disco_key(&PEER_NODE_KEY.into()),
4376            Some(DiscoPublicKey::from(CONTROL_CAUGHT_UP)),
4377            "but control's new key IS recorded in control's slot"
4378        );
4379    }
4380
4381    /// An advertisement that merely restates the key control already gave us is still *new*
4382    /// information — it is the peer itself confirming the key — so Go records it as the TSMP key and
4383    /// makes it active. Its "unchanged" early return compares `epDisco.keyFromTSMP()`, the
4384    /// TSMP-learned key specifically, never the effective one.
4385    ///
4386    /// The observable consequence, asserted here: once the peer has confirmed the key, control
4387    /// dropping it (a netmap node with no disco key) leaves the confirmed key in place instead of
4388    /// blinding the direct path.
4389    #[tokio::test]
4390    async fn advertisement_restating_controls_key_is_recorded_as_the_tsmp_key() {
4391        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4392        let key = DiscoPublicKey::from(FROM_CONTROL);
4393
4394        assert!(
4395            tracker.learn_disco_key(peer, key),
4396            "an advertisement of the key control already sent is recorded, not dropped"
4397        );
4398        assert_eq!(
4399            tracker
4400                .endpoint_disco
4401                .get(&PEER_NODE_KEY.into())
4402                .and_then(EndpointDisco::key_from_tsmp),
4403            Some(key),
4404            "it lands in the TSMP slot (Go epDisco.tsmpKey), not only in control's"
4405        );
4406        assert!(
4407            !tracker.learn_disco_key(peer, key),
4408            "re-advertising it now IS unchanged, and is refused"
4409        );
4410
4411        // Control drops the peer's disco key. The key the peer itself confirmed stays active.
4412        tracker.apply_peer_update(&control_full(None), local_now());
4413        assert_eq!(
4414            effective_key(&tracker, peer),
4415            Some(key),
4416            "a control key going away hands the active slot to the TSMP-learned key"
4417        );
4418        assert_eq!(tracker.peer_db.has(&key), Some(peer));
4419    }
4420
4421    /// A `PeersChangedPatch` is a control write like any other: one that says nothing about the
4422    /// disco key must leave an active TSMP key alone, and one that carries a new key is control
4423    /// catching up, so it wins.
4424    ///
4425    /// The patch path is the subtle one — it starts from the db node, which carries the *effective*
4426    /// key, so without re-deriving what control last said it would hand the TSMP key back as if
4427    /// control had sent it.
4428    #[tokio::test]
4429    async fn patch_without_a_disco_key_leaves_the_tsmp_key_active() {
4430        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4431        let advertised = DiscoPublicKey::from(ADVERTISED);
4432        assert!(tracker.learn_disco_key(peer, advertised));
4433
4434        // A reachability-only patch (the idle-peer-reconnect case) for the same node.
4435        let endpoint: std::net::SocketAddr = "203.0.113.9:41641".parse().unwrap();
4436        let mut patch = ts_control::PeerChange {
4437            id: 1,
4438            derp_region: None,
4439            cap: None,
4440            cap_map: None,
4441            underlay_addresses: Some(vec![endpoint]),
4442            node_key: None,
4443            key_signature: None,
4444            disco_key: None,
4445            node_key_expiry: None,
4446            online: None,
4447            last_seen: None,
4448        };
4449        tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
4450        assert_eq!(
4451            effective_key(&tracker, peer),
4452            Some(advertised),
4453            "a patch that never mentions the disco key must not revert it to control's"
4454        );
4455        assert_eq!(
4456            tracker
4457                .peer_db
4458                .get(&peer)
4459                .expect("peer still present")
4460                .1
4461                .underlay_addresses,
4462            vec![endpoint],
4463            "and the patch it DID carry still applied"
4464        );
4465
4466        // Now control changes the key through the patch channel. Same rule as the netmap path: the
4467        // key lands in control's slot, and the active TSMP key is left alone.
4468        patch.disco_key = Some(DiscoPublicKey::from(CONTROL_CAUGHT_UP));
4469        tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
4470        assert_eq!(
4471            effective_key(&tracker, peer),
4472            Some(advertised),
4473            "a patch carrying a new disco key does not preempt the active TSMP-learned key either"
4474        );
4475        assert_eq!(
4476            tracker.control_disco_key(&PEER_NODE_KEY.into()),
4477            Some(DiscoPublicKey::from(CONTROL_CAUGHT_UP)),
4478            "the patched key is still recorded as what control now says"
4479        );
4480    }
4481
4482    /// The rule this whole pair of slots exists to express: once the peer has told us its key over
4483    /// TSMP, control changing its mind is *recorded* but does not take the active slot back — Go
4484    /// `endpoint.updateDiscoKey`'s `epDisco.tsmpActive = old.tsmpActive || key.IsZero()`.
4485    ///
4486    /// Control is the slower source; a key the peer sent us itself is the better evidence. Upstream
4487    /// hands the slot back only when disco is actually *received* under control's key
4488    /// (`endpoint.checkAndUpdateDiscoKey`). Here the peer re-advertising is the path back, and it is
4489    /// asserted at the end so the sticky rule cannot be read as "the TSMP key is now permanent".
4490    #[tokio::test]
4491    async fn a_control_key_change_does_not_preempt_an_active_tsmp_key() {
4492        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4493        let advertised = DiscoPublicKey::from(ADVERTISED);
4494        let caught_up = DiscoPublicKey::from(CONTROL_CAUGHT_UP);
4495        assert!(tracker.learn_disco_key(peer, advertised));
4496
4497        tracker.apply_peer_update(&control_full(Some(CONTROL_CAUGHT_UP)), local_now());
4498        assert_eq!(
4499            effective_key(&tracker, peer),
4500            Some(advertised),
4501            "the TSMP-learned key stays active across a control-side change"
4502        );
4503        assert_eq!(
4504            tracker.peer_db.has(&advertised),
4505            Some(peer),
4506            "so the direct path still resolves the peer by the key it advertised"
4507        );
4508        assert_eq!(
4509            tracker.peer_db.has(&caught_up),
4510            None,
4511            "and control's new key is not what we send to"
4512        );
4513        assert_eq!(
4514            tracker.control_disco_key(&PEER_NODE_KEY.into()),
4515            Some(caught_up),
4516            "control's new key is recorded all the same — it is not discarded, just not active"
4517        );
4518
4519        // Control changing its mind a second time, and then dropping the key entirely, changes
4520        // nothing about which key is active.
4521        tracker.apply_peer_update(&control_full(Some(FROM_CONTROL)), local_now());
4522        tracker.apply_peer_update(&control_full(None), local_now());
4523        assert_eq!(
4524            effective_key(&tracker, peer),
4525            Some(advertised),
4526            "neither a second control change nor control dropping the key moves the active slot"
4527        );
4528
4529        // The peer itself is what moves it: it advertises the key control had been trying to give
4530        // us, and that advertisement is what we act on.
4531        assert!(tracker.learn_disco_key(peer, caught_up));
4532        assert_eq!(
4533            effective_key(&tracker, peer),
4534            Some(caught_up),
4535            "a peer re-advertising moves the active key, because the peer is the evidence"
4536        );
4537    }
4538
4539    /// The sticky flag must not strand a peer that never had a TSMP key: control sending nothing
4540    /// leaves no key material at all, and the key control sends next must become the active one.
4541    ///
4542    /// This is the case Go covers by nil-ing the endpoint's `disco` pointer when both keys are
4543    /// zero; here [`PeerTracker::upsert_from_control`] drops the entry, so the "no control key means
4544    /// the TSMP slot is active" flag cannot survive to shadow a later control key with nothing.
4545    #[tokio::test]
4546    async fn a_first_control_key_is_active_even_after_control_sent_none() {
4547        let (mut tracker, peer) = tracker_with_control_peer(None);
4548        assert_eq!(effective_key(&tracker, peer), None, "precondition");
4549        assert!(
4550            tracker.endpoint_disco.is_empty(),
4551            "a peer with no key material from either source costs no entry"
4552        );
4553
4554        tracker.apply_peer_update(&control_full(Some(FROM_CONTROL)), local_now());
4555        assert_eq!(
4556            effective_key(&tracker, peer),
4557            Some(DiscoPublicKey::from(FROM_CONTROL)),
4558            "control's first key is active — there is no TSMP key for it to defer to"
4559        );
4560        assert_eq!(
4561            tracker.peer_db.has(&DiscoPublicKey::from(FROM_CONTROL)),
4562            Some(peer)
4563        );
4564    }
4565
4566    /// The other half of `tsmpActive = old.tsmpActive || key.IsZero()`, in the one state where the
4567    /// left operand is false *and* a TSMP key exists: after disco was received under control's key,
4568    /// which is upstream's only route back to control holding the active slot
4569    /// (`endpoint.checkAndUpdateDiscoKey`).
4570    ///
4571    /// Two things follow, and neither is obvious from the sticky rule alone. Control's later changes
4572    /// **do** land, because what is sticky is the flag, not the TSMP key — so this is not "the TSMP
4573    /// key wins forever", and a peer that genuinely rotated is not stranded. And control *dropping*
4574    /// its key does not leave the peer with no disco key at all: the `key.IsZero()` operand hands the
4575    /// slot to the TSMP key still sitting in the other slot, which is why Go only nils the endpoint's
4576    /// `disco` pointer when **both** keys are zero.
4577    #[tokio::test]
4578    async fn control_regains_the_slot_by_being_received_under_and_then_keeps_it() {
4579        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4580        let from_control = DiscoPublicKey::from(FROM_CONTROL);
4581        let advertised = DiscoPublicKey::from(ADVERTISED);
4582        let caught_up = DiscoPublicKey::from(CONTROL_CAUGHT_UP);
4583
4584        // Get into the state: the peer advertises, then sends disco under control's key anyway, so
4585        // control's key is active again with the TSMP key demoted but retained.
4586        assert!(tracker.learn_disco_key(peer, advertised));
4587        assert!(tracker.observe_disco_key(peer, from_control));
4588        assert_eq!(
4589            effective_key(&tracker, peer),
4590            Some(from_control),
4591            "precondition: control holds the active slot because we received under its key"
4592        );
4593        assert_eq!(
4594            ingress_match(&tracker, advertised),
4595            Some((peer, peer_db::DiscoKeyMatch::Inactive)),
4596            "precondition: the TSMP key is demoted, not discarded"
4597        );
4598
4599        // Control changes its key. With the TSMP key demoted the sticky operand is false, so this
4600        // one does take the active slot — the flag is what is sticky, not the TSMP key.
4601        tracker.apply_peer_update(&control_full(Some(CONTROL_CAUGHT_UP)), local_now());
4602        assert_eq!(
4603            effective_key(&tracker, peer),
4604            Some(caught_up),
4605            "a demoted TSMP key does not block control's next key from becoming active"
4606        );
4607        assert_eq!(
4608            ingress_match(&tracker, advertised),
4609            Some((peer, peer_db::DiscoKeyMatch::Inactive)),
4610            "and the TSMP key is still the peer's other known key for ingress"
4611        );
4612        assert_eq!(
4613            ingress_match(&tracker, from_control),
4614            None,
4615            "control's superseded key is not a third slot"
4616        );
4617
4618        // Control drops its key entirely. `key.IsZero()` is the operand that carries the peer here:
4619        // the retained TSMP key becomes active rather than the peer losing disco altogether.
4620        tracker.apply_peer_update(&control_full(None), local_now());
4621        assert_eq!(
4622            effective_key(&tracker, peer),
4623            Some(advertised),
4624            "control dropping its key falls back to the TSMP key, not to no key"
4625        );
4626        assert_eq!(
4627            ingress_match(&tracker, advertised),
4628            Some((peer, peer_db::DiscoKeyMatch::Active))
4629        );
4630        assert_eq!(
4631            tracker.control_disco_key(&PEER_NODE_KEY.into()),
4632            None,
4633            "control's slot is cleared, so there is no second key to accept"
4634        );
4635        assert_eq!(
4636            ingress_match(&tracker, caught_up),
4637            None,
4638            "the key control withdrew stops resolving on ingress"
4639        );
4640    }
4641
4642    /// The TSMP-learned key lives exactly as long as Go's endpoint does: it is dropped when the peer
4643    /// leaves the netmap, and it is not carried across a node-key rotation (Go builds the rotated
4644    /// peer a brand-new endpoint, with a brand-new `endpointDisco`).
4645    #[tokio::test]
4646    async fn tsmp_key_does_not_outlive_the_peer_or_its_node_key() {
4647        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4648        assert!(tracker.learn_disco_key(peer, DiscoPublicKey::from(ADVERTISED)));
4649
4650        // The peer leaves the netmap, then comes back on control's key.
4651        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![]), local_now());
4652        assert!(tracker.peer_db.peers().is_empty());
4653        assert!(
4654            tracker.endpoint_disco.is_empty(),
4655            "the departed peer's disco state goes with it"
4656        );
4657        tracker.apply_peer_update(&control_full(Some(FROM_CONTROL)), local_now());
4658        let readded = node_from_control(Some(FROM_CONTROL));
4659        let peer = tracker.peer_db.has(&readded.node_key).expect("re-added");
4660        assert_eq!(
4661            effective_key(&tracker, peer),
4662            Some(DiscoPublicKey::from(FROM_CONTROL)),
4663            "a peer that left and rejoined starts from control's key again"
4664        );
4665
4666        // Learn a key again, then rotate the node key underneath it.
4667        assert!(tracker.learn_disco_key(peer, DiscoPublicKey::from(READVERTISED)));
4668        let mut rotated = node_from_control(Some(FROM_CONTROL));
4669        rotated.node_key = [2u8; 32].into();
4670        tracker.apply_peer_update(
4671            &ts_control::PeerUpdate::Full(vec![rotated.clone()]),
4672            local_now(),
4673        );
4674        let peer = tracker
4675            .peer_db
4676            .has(&rotated.node_key)
4677            .expect("rotated peer");
4678        assert_eq!(
4679            effective_key(&tracker, peer),
4680            Some(DiscoPublicKey::from(FROM_CONTROL)),
4681            "a key learned under the old node key is not carried onto the new one"
4682        );
4683        assert_eq!(
4684            tracker.endpoint_disco.len(),
4685            1,
4686            "and the old node key's state is pruned"
4687        );
4688    }
4689
4690    /// How the peer db resolves `key` for an inbound disco frame: the peer it belongs to and which
4691    /// of that peer's two slots it matched.
4692    fn ingress_match(
4693        tracker: &PeerTracker,
4694        key: DiscoPublicKey,
4695    ) -> Option<(PeerId, peer_db::DiscoKeyMatch)> {
4696        tracker
4697            .peer_db
4698            .peer_by_known_disco_key(&key)
4699            .map(|(id, _node, matched)| (id, matched))
4700    }
4701
4702    /// The bead's case, end to end: the peer advertised K2 over TSMP so we send to K2, but it is
4703    /// still sending disco under the K1 control gave us. That frame must resolve to the peer, and
4704    /// receiving under K1 must make K1 the key we send to — because it is demonstrably what the
4705    /// peer uses.
4706    ///
4707    /// Go: every inbound disco comparison goes through `endpoint.checkAndUpdateDiscoKey`, which
4708    /// accepts either slot and compare-and-swaps `tsmpActive` when the key seen is the inactive one.
4709    #[tokio::test]
4710    async fn disco_under_the_inactive_key_is_accepted_and_makes_that_key_active() {
4711        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4712        let from_control = DiscoPublicKey::from(FROM_CONTROL);
4713        let advertised = DiscoPublicKey::from(ADVERTISED);
4714
4715        assert!(tracker.learn_disco_key(peer, advertised));
4716        assert_eq!(
4717            effective_key(&tracker, peer),
4718            Some(advertised),
4719            "precondition: we are sending to the TSMP-learned key"
4720        );
4721        assert_eq!(
4722            ingress_match(&tracker, from_control),
4723            Some((peer, peer_db::DiscoKeyMatch::Inactive)),
4724            "control's key is still the peer's other known key, and still resolves on ingress"
4725        );
4726
4727        // Disco arrives under control's key: accepted, and it becomes the active one.
4728        assert!(
4729            tracker.observe_disco_key(peer, from_control),
4730            "receiving under the inactive key switches the active key"
4731        );
4732        assert_eq!(
4733            effective_key(&tracker, peer),
4734            Some(from_control),
4735            "we now send to the key the peer is demonstrably using"
4736        );
4737        assert_eq!(
4738            tracker.peer_db.has(&from_control),
4739            Some(peer),
4740            "and it is the key the send-side disco index carries"
4741        );
4742        assert_eq!(
4743            ingress_match(&tracker, from_control),
4744            Some((peer, peer_db::DiscoKeyMatch::Active))
4745        );
4746        assert_eq!(
4747            ingress_match(&tracker, advertised),
4748            Some((peer, peer_db::DiscoKeyMatch::Inactive)),
4749            "the TSMP key is retained in the other slot, so ingress under it still resolves"
4750        );
4751
4752        assert!(
4753            !tracker.observe_disco_key(peer, from_control),
4754            "a second frame under the now-active key changes nothing (and forces no republish)"
4755        );
4756
4757        // And it switches back: the peer resumes sending under the key it advertised.
4758        assert!(tracker.observe_disco_key(peer, advertised));
4759        assert_eq!(effective_key(&tracker, peer), Some(advertised));
4760        assert_eq!(
4761            ingress_match(&tracker, from_control),
4762            Some((peer, peer_db::DiscoKeyMatch::Inactive))
4763        );
4764    }
4765
4766    /// The refusal that is the whole security value of the check: a key belonging to NEITHER slot
4767    /// is rejected, leaving the peer on the key it was on. Plus the two other refusals Go has —
4768    /// an unknown peer, and a peer with no disco key material at all (`epDisco == nil`).
4769    #[tokio::test]
4770    async fn disco_under_a_key_in_neither_slot_is_refused() {
4771        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4772        let from_control = DiscoPublicKey::from(FROM_CONTROL);
4773        let advertised = DiscoPublicKey::from(ADVERTISED);
4774        let third = DiscoPublicKey::from(READVERTISED);
4775
4776        assert!(tracker.learn_disco_key(peer, advertised));
4777
4778        assert!(
4779            !tracker.observe_disco_key(peer, third),
4780            "a third key is refused: a peer must not move itself onto a key nobody told us about"
4781        );
4782        assert_eq!(
4783            effective_key(&tracker, peer),
4784            Some(advertised),
4785            "and the peer stays on the key it was on"
4786        );
4787        assert_eq!(
4788            ingress_match(&tracker, third),
4789            None,
4790            "the refused key never becomes resolvable"
4791        );
4792        assert_eq!(
4793            ingress_match(&tracker, from_control),
4794            Some((peer, peer_db::DiscoKeyMatch::Inactive)),
4795            "the two real slots are untouched"
4796        );
4797
4798        // An unknown peer: like a TSMP advertisement, this never creates one.
4799        assert!(!tracker.observe_disco_key(PeerId(4242), from_control));
4800        assert_eq!(tracker.peer_db.peers().len(), 1);
4801
4802        // A peer with no disco key from either source — Go returns false on `epDisco == nil`.
4803        let (mut bare, bare_peer) = tracker_with_control_peer(None);
4804        assert_eq!(effective_key(&bare, bare_peer), None, "precondition");
4805        assert!(
4806            !bare.observe_disco_key(bare_peer, from_control),
4807            "a peer with no known disco key has no slot for this key to match"
4808        );
4809        assert_eq!(effective_key(&bare, bare_peer), None);
4810    }
4811
4812    /// A peer that has only ever had one key registers no inactive key at all, so the second index
4813    /// stays empty and an inbound frame under any other key is refused.
4814    #[tokio::test]
4815    async fn a_single_key_peer_has_no_second_slot() {
4816        let (tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4817        let from_control = DiscoPublicKey::from(FROM_CONTROL);
4818
4819        assert_eq!(
4820            ingress_match(&tracker, from_control),
4821            Some((peer, peer_db::DiscoKeyMatch::Active))
4822        );
4823        assert_eq!(
4824            ingress_match(&tracker, DiscoPublicKey::from(ADVERTISED)),
4825            None,
4826            "no second key was ever learned, so nothing else resolves to this peer"
4827        );
4828    }
4829
4830    /// An advertisement that merely restates control's key must not leave the peer with the same
4831    /// key in both slots pretending to be two — `inactive_key` reports `None` when the inactive
4832    /// slot holds the active key, so ingress sees exactly one key.
4833    #[tokio::test]
4834    async fn the_same_key_in_both_slots_is_one_key() {
4835        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4836        let from_control = DiscoPublicKey::from(FROM_CONTROL);
4837
4838        assert!(tracker.learn_disco_key(peer, from_control));
4839        assert_eq!(
4840            tracker
4841                .endpoint_disco
4842                .get(&PEER_NODE_KEY.into())
4843                .and_then(EndpointDisco::inactive_key),
4844            None,
4845            "both slots hold the same key, so there is no second key"
4846        );
4847        assert_eq!(
4848            ingress_match(&tracker, from_control),
4849            Some((peer, peer_db::DiscoKeyMatch::Active))
4850        );
4851        assert!(
4852            !tracker.observe_disco_key(peer, from_control),
4853            "and receiving under it is a no-op, not a switch"
4854        );
4855    }
4856
4857    /// A peer that leaves the netmap takes BOTH its keys with it: the inactive-key index must not
4858    /// keep attributing frames to a peer that is gone.
4859    #[tokio::test]
4860    async fn a_departed_peer_stops_resolving_under_either_key() {
4861        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4862        let advertised = DiscoPublicKey::from(ADVERTISED);
4863        assert!(tracker.learn_disco_key(peer, advertised));
4864        assert!(ingress_match(&tracker, DiscoPublicKey::from(FROM_CONTROL)).is_some());
4865
4866        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![]), local_now());
4867        assert_eq!(ingress_match(&tracker, advertised), None);
4868        assert_eq!(
4869            ingress_match(&tracker, DiscoPublicKey::from(FROM_CONTROL)),
4870            None,
4871            "the inactive key is retracted with the peer, not left dangling"
4872        );
4873    }
4874}
4875
4876#[cfg(test)]
4877mod index_eviction_tests {
4878    //! A departing peer must not evict the index rows of the peer that took its place.
4879    //!
4880    //! Control reassigns a churning (typically ephemeral) peer's tailnet IP and MagicDNS name to a
4881    //! newer node, and the newer node's upsert can reach us before the old node's removal — either
4882    //! in an earlier `MapResponse`, or reordered inside one batch. Here it is not even a race:
4883    //! [`PeerTracker::apply_peer_update`] applies a delta's upserts first and its removals second,
4884    //! so the intra-batch ordering is the one this tree ALWAYS uses. These drive real
4885    //! [`ts_control::PeerUpdate`]s through that function and assert the lookups an embedder
4886    //! actually depends on — `peer_by_tailnet_ip` (whois, peerAPI source checks) and
4887    //! `peer_by_name` — still answer with the live peer.
4888
4889    use super::{
4890        tka_tests::{peer_node, test_env},
4891        *,
4892    };
4893
4894    /// A peer holding a specific control node id, tailnet address pair and hostname.
4895    fn peer_at(stable_id: &str, control_id: i64, key: u8, host: u8) -> Node {
4896        let mut node = peer_node(stable_id, [key; 32], Vec::new());
4897        node.id = control_id;
4898        node.hostname = stable_id.to_string();
4899        node.tailnet = Some("ts.net".to_string());
4900
4901        let ipv4: ipnet::Ipv4Net = format!("100.64.0.{host}/32").parse().unwrap();
4902        let ipv6: ipnet::Ipv6Net = format!("fd7a:115c:a1e0::{host}/128").parse().unwrap();
4903        node.addresses = vec![ipv4.into(), ipv6.into()];
4904        node.tailnet_address = ts_control::TailnetAddress { ipv4, ipv6 };
4905        node.disco_key = Some([key; 32].into());
4906        node
4907    }
4908
4909    fn tailnet_ipv4(host: u8) -> IpAddr {
4910        format!("100.64.0.{host}").parse().unwrap()
4911    }
4912
4913    /// The successor's upsert and the departing peer's removal in ONE delta — the ordering
4914    /// `apply_peer_update` always applies (upserts, then removals).
4915    #[tokio::test]
4916    async fn a_delta_that_replaces_a_peer_keeps_the_successor_addressable() {
4917        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
4918        let now = local_now();
4919
4920        let departing = peer_at("departing", 1, 1, 9);
4921        tracker.apply_peer_update(
4922            &ts_control::PeerUpdate::Delta {
4923                upsert: vec![departing.clone()],
4924                remove: Vec::new(),
4925            },
4926            now,
4927        );
4928        assert_eq!(
4929            tracker
4930                .peer_by_tailnet_ip_opt(tailnet_ipv4(9))
4931                .map(|n| &n.stable_id),
4932            Some(&departing.stable_id)
4933        );
4934
4935        // Control hands the address and the MagicDNS name to a new node and retires the old one in
4936        // the same batch.
4937        let mut successor = peer_at("successor", 2, 2, 9);
4938        successor.hostname = departing.hostname.clone();
4939        successor.disco_key = departing.disco_key;
4940
4941        tracker.apply_peer_update(
4942            &ts_control::PeerUpdate::Delta {
4943                upsert: vec![successor.clone()],
4944                remove: vec![departing.id],
4945            },
4946            now,
4947        );
4948
4949        assert_eq!(tracker.peer_db.peers().len(), 1, "the old peer is gone");
4950        assert_eq!(
4951            tracker
4952                .peer_by_tailnet_ip_opt(tailnet_ipv4(9))
4953                .map(|n| &n.stable_id),
4954            Some(&successor.stable_id),
4955            "whois and every peerAPI source check resolve through this index"
4956        );
4957        assert_eq!(
4958            tracker
4959                .peer_by_name_opt("departing.ts.net")
4960                .map(|n| &n.stable_id),
4961            Some(&successor.stable_id),
4962            "the MagicDNS name follows the address to its new owner"
4963        );
4964        assert_eq!(
4965            tracker
4966                .peer_db
4967                .get(&successor.disco_key.unwrap())
4968                .map(|(_id, n)| &n.stable_id),
4969            Some(&successor.stable_id),
4970            "and so does the disco key, which is the successor's direct path"
4971        );
4972    }
4973
4974    /// The same replacement split across TWO deltas: the successor arrives in one `MapResponse`,
4975    /// the departing peer's removal trails in a later one.
4976    #[tokio::test]
4977    async fn a_removal_trailing_a_later_upsert_keeps_the_successor_addressable() {
4978        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
4979        let now = local_now();
4980
4981        let departing = peer_at("departing", 1, 1, 9);
4982        let successor = peer_at("successor", 2, 2, 9);
4983
4984        tracker.apply_peer_update(
4985            &ts_control::PeerUpdate::Delta {
4986                upsert: vec![departing.clone()],
4987                remove: Vec::new(),
4988            },
4989            now,
4990        );
4991        tracker.apply_peer_update(
4992            &ts_control::PeerUpdate::Delta {
4993                upsert: vec![successor.clone()],
4994                remove: Vec::new(),
4995            },
4996            now,
4997        );
4998        tracker.apply_peer_update(
4999            &ts_control::PeerUpdate::Delta {
5000                upsert: Vec::new(),
5001                remove: vec![departing.id],
5002            },
5003            now,
5004        );
5005
5006        assert_eq!(tracker.peer_db.peers().len(), 1);
5007        assert_eq!(
5008            tracker
5009                .peer_by_tailnet_ip_opt(tailnet_ipv4(9))
5010                .map(|n| &n.stable_id),
5011            Some(&successor.stable_id),
5012            "a removal that arrives late must not evict the address's live owner"
5013        );
5014        assert!(
5015            tracker
5016                .whois_opt("100.64.0.9:80".parse().unwrap())
5017                .is_some(),
5018            "so whois still identifies the peer that is present and handshaking"
5019        );
5020    }
5021
5022    /// The positive case, through the same path: a peer removed while it still owns its rows is
5023    /// really gone from every index, so the guard cannot pass by never evicting anything.
5024    #[tokio::test]
5025    async fn a_removal_with_no_successor_clears_the_indexes() {
5026        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
5027        let now = local_now();
5028
5029        let peer = peer_at("solo", 1, 1, 9);
5030        tracker.apply_peer_update(
5031            &ts_control::PeerUpdate::Delta {
5032                upsert: vec![peer.clone()],
5033                remove: Vec::new(),
5034            },
5035            now,
5036        );
5037        tracker.apply_peer_update(
5038            &ts_control::PeerUpdate::Delta {
5039                upsert: Vec::new(),
5040                remove: vec![peer.id],
5041            },
5042            now,
5043        );
5044
5045        assert!(tracker.peer_db.peers().is_empty());
5046        assert!(tracker.peer_by_tailnet_ip_opt(tailnet_ipv4(9)).is_none());
5047        assert!(tracker.peer_by_name_opt("solo.ts.net").is_none());
5048        assert!(tracker.peer_db.get(&peer.node_key).is_none());
5049        assert!(tracker.peer_db.get(&peer.stable_id).is_none());
5050        assert!(
5051            tracker
5052                .whois_opt("100.64.0.9:80".parse().unwrap())
5053                .is_none(),
5054            "a departed peer must not stay attributable by its old address"
5055        );
5056    }
5057}
5058
5059#[cfg(test)]
5060mod expiry_tests {
5061    //! Node-key expiry enforcement at the peer tracker — the port of Go's `expiryManager`
5062    //! (`ipn/ipnlocal/expiry.go`) wired into this fork's netmap.
5063    //!
5064    //! [`ts_control::ExpiryManager`] carries its own unit tests for the decision itself. These
5065    //! cover the wiring: that the pass runs at the install site, that the state it leaves is
5066    //! observable through [`StatusNode`], and that the timer catches a peer that expires with **no
5067    //! netmap in between** — the case the timer exists for.
5068
5069    use chrono::TimeDelta;
5070    use kameo::actor::Spawn as _;
5071
5072    use super::{
5073        tka_tests::{await_peer_count, netmap_with_peers, peer_node, test_env},
5074        *,
5075    };
5076
5077    /// A peer with a chosen key expiry, endpoints and a DERP home — the three things the expiry
5078    /// pass strips.
5079    fn expiring_peer(
5080        stable_id: &str,
5081        key: u8,
5082        expiry: Option<chrono::DateTime<chrono::Utc>>,
5083    ) -> Node {
5084        let mut node = peer_node(stable_id, [key; 32], Vec::new());
5085        node.node_key_expiry = expiry;
5086        node.underlay_addresses = vec!["192.0.2.9:41641".parse().unwrap()];
5087        node.derp_region = Some(ts_derp::RegionId(core::num::NonZeroU32::new(3).unwrap()));
5088        node.peerapi_port = Some(8080);
5089        node
5090    }
5091
5092    /// The fail-closed direction here is to **flag, not drop**: Go deliberately keeps an expired
5093    /// peer in the netmap so callers can give a clear error, and removing it would lose that. The
5094    /// peer stays addressable by stable id while losing everything that could carry traffic.
5095    #[tokio::test]
5096    async fn an_expired_peer_is_flagged_and_kept_not_dropped() {
5097        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
5098        let now = local_now();
5099        let peer = expiring_peer("eXpIrEd", 7, Some(now - TimeDelta::hours(1)));
5100        let pristine_key = peer.node_key;
5101
5102        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer.clone()]), now);
5103
5104        let (_id, stored) = tracker
5105            .peer_db
5106            .get(&peer.stable_id)
5107            .expect("the expired peer is KEPT in the netmap, not dropped");
5108        assert!(stored.expired);
5109        assert!(
5110            stored.underlay_addresses.is_empty(),
5111            "endpoints are cleared"
5112        );
5113        assert_eq!(stored.derp_region, None, "the DERP home is cleared");
5114        assert_eq!(
5115            stored.node_key,
5116            ts_keys::node_public_with_bad_old_prefix(pristine_key),
5117            "the node key is broken, so nothing can handshake with the peer"
5118        );
5119        assert_eq!(
5120            stored.peerapi_addr(),
5121            None,
5122            "a peerAPI dial to the expired peer is refused"
5123        );
5124
5125        let status = tracker.status_peers();
5126        assert_eq!(status.len(), 1);
5127        assert!(status[0].expired, "the state is observable to a watcher");
5128    }
5129
5130    /// The negative case: a tagged node carries no key expiry at all (Go's zero `KeyExpiry`) and is
5131    /// never flagged, however far the clock is pushed.
5132    #[tokio::test]
5133    async fn a_peer_with_no_expiry_is_never_flagged() {
5134        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
5135        let now = local_now();
5136        let tagged = expiring_peer("tAgGeD", 8, None);
5137
5138        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![tagged.clone()]), now);
5139        assert!(
5140            tracker
5141                .reevaluate_expiry(now + TimeDelta::days(3650))
5142                .is_empty(),
5143            "a node with no expiry never expires, ten years on"
5144        );
5145
5146        let (_id, stored) = tracker
5147            .peer_db
5148            .get(&tagged.stable_id)
5149            .expect("still a peer");
5150        assert!(!stored.expired);
5151        assert_eq!(stored.node_key, tagged.node_key, "its key is left alone");
5152        assert_eq!(stored.underlay_addresses, tagged.underlay_addresses);
5153        assert_eq!(stored.derp_region, tagged.derp_region);
5154    }
5155
5156    /// The case the timer exists for: a peer that is perfectly live when it is installed, and whose
5157    /// key expiry then passes with **no netmap in between**. `reevaluate_expiry` is what the fired
5158    /// timer runs.
5159    #[tokio::test]
5160    async fn a_peer_that_expires_between_netmaps_is_flagged_by_the_timer_pass() {
5161        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
5162        let now = local_now();
5163        let peer = expiring_peer("lIvE", 9, Some(now + TimeDelta::hours(1)));
5164
5165        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer.clone()]), now);
5166        let (_id, stored) = tracker.peer_db.get(&peer.stable_id).expect("installed");
5167        assert!(!stored.expired, "not expired when control handed it to us");
5168
5169        // No netmap arrives. The clock crosses the peer's expiry.
5170        let upserts = tracker.reevaluate_expiry(now + TimeDelta::hours(2));
5171
5172        assert_eq!(upserts.len(), 1, "the peer is re-installed, flagged");
5173        let (_id, stored) = tracker
5174            .peer_db
5175            .get(&peer.stable_id)
5176            .expect("still kept, just flagged");
5177        assert!(stored.expired);
5178        assert!(stored.underlay_addresses.is_empty());
5179        assert_eq!(stored.derp_region, None);
5180    }
5181
5182    /// An already-expired peer must be skipped rather than re-flagged, or the log and the
5183    /// invalidation it triggers repeat on every pass — and the broken key would be re-broken.
5184    #[tokio::test]
5185    async fn an_already_flagged_peer_is_not_reflagged() {
5186        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
5187        let now = local_now();
5188        let peer = expiring_peer("lIvE", 9, Some(now + TimeDelta::hours(1)));
5189        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer.clone()]), now);
5190
5191        let later = now + TimeDelta::hours(2);
5192        assert_eq!(tracker.reevaluate_expiry(later).len(), 1);
5193        let after_first = tracker
5194            .peer_db
5195            .get(&peer.stable_id)
5196            .expect("kept")
5197            .1
5198            .clone();
5199
5200        assert!(
5201            tracker.reevaluate_expiry(later).is_empty(),
5202            "the second pass reports no transition, so nothing is re-published"
5203        );
5204        assert_eq!(
5205            tracker.peer_db.get(&peer.stable_id).expect("kept").1,
5206            &after_first,
5207            "and nothing is mutated a second time"
5208        );
5209    }
5210
5211    /// End to end through the LIVE actor, which is the only thing that proves the wiring: a peer
5212    /// that is live when the netmap installs it, and whose key then expires while the map poll sits
5213    /// idle, is flagged by the timer alone. If the timer were never armed — or its firing never
5214    /// re-ran the pass — the peer would keep its endpoints, its DERP home and a usable node key
5215    /// until control happened to send another netmap.
5216    #[tokio::test]
5217    async fn a_key_expiring_with_no_netmap_in_between_is_caught_by_the_live_timer() {
5218        let env = test_env();
5219        let (_tka_tx, tka_rx) = watch::channel(None);
5220        let live = PeerTracker::spawn((env.clone(), tka_rx));
5221        assert!(live.ask(AllPeers).await.expect("started").is_empty());
5222
5223        // Expires shortly, but strictly in the future: the install-time pass must NOT flag it. The
5224        // window has to outlast actor start + publish + one ask on a loaded box, hence seconds
5225        // rather than milliseconds; the test does not wait it out, it only waits for the wall clock
5226        // to cross it (below).
5227        let expiry = local_now() + TimeDelta::seconds(2);
5228        let peer = expiring_peer("sHoRtLiVeD", 4, Some(expiry));
5229        env.publish(Arc::new(netmap_with_peers(vec![peer.clone()])))
5230            .await
5231            .expect("publish netmap");
5232
5233        let installed = await_peer_count(&live, 1).await;
5234        assert!(
5235            local_now() < expiry,
5236            "the install has to finish inside the window, or this test is not testing the timer"
5237        );
5238        assert!(
5239            !installed[0].expired,
5240            "still live when control handed it to us"
5241        );
5242
5243        // Let the key really expire on the wall clock the pass reads...
5244        while local_now() <= expiry {
5245            tokio::time::sleep(std::time::Duration::from_millis(25)).await;
5246        }
5247        // ...then jump the runtime's timer wheel past the armed delay (the peer's expiry plus Go's
5248        // slack) so the timer fires now instead of ten seconds from now. No netmap in between.
5249        tokio::time::pause();
5250        tokio::time::advance(std::time::Duration::from_secs(
5251            ts_control::EXPIRY_TIMER_SLACK_SECS as u64 + 5,
5252        ))
5253        .await;
5254        tokio::time::resume();
5255
5256        let flagged = tokio::time::timeout(std::time::Duration::from_secs(10), async {
5257            loop {
5258                let peers = live.ask(AllPeers).await.expect("peer tracker is alive");
5259                if peers.first().is_some_and(|p| p.expired) {
5260                    return peers;
5261                }
5262                tokio::time::sleep(std::time::Duration::from_millis(5)).await;
5263            }
5264        })
5265        .await
5266        .expect("the expiry timer flagged the peer with no netmap in between");
5267
5268        assert_eq!(flagged.len(), 1, "flagged, not dropped");
5269        assert!(flagged[0].underlay_addresses.is_empty());
5270        assert_eq!(flagged[0].derp_region, None);
5271        assert_eq!(
5272            flagged[0].node_key,
5273            ts_keys::node_public_with_bad_old_prefix(peer.node_key)
5274        );
5275    }
5276
5277    /// The recovery path, through the channel that actually carries it: control extends an expired
5278    /// peer's key with a `PeerChange` that restates only `KeyExpiry`. The peer has to come back
5279    /// with its direct candidates and its home DERP, not merely with a usable node key — flagging
5280    /// cleared all three, the patch restates none of them, and a peer that is un-expired but has
5281    /// neither an endpoint nor a DERP home is unroutable until the next full netmap.
5282    #[tokio::test]
5283    async fn an_expiry_only_patch_restores_the_peers_routes() {
5284        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
5285        let now = local_now();
5286        let peer = expiring_peer("eXtEnDeD", 6, Some(now - TimeDelta::hours(1)));
5287
5288        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer.clone()]), now);
5289        assert!(
5290            tracker
5291                .peer_db
5292                .get(&peer.stable_id)
5293                .expect("kept")
5294                .1
5295                .expired,
5296            "flagged on the way in"
5297        );
5298
5299        // Exactly the shape of a `PeerChange` that only extends the key's life.
5300        let patch = ts_control::PeerChange {
5301            id: peer.id,
5302            derp_region: None,
5303            cap: None,
5304            cap_map: None,
5305            underlay_addresses: None,
5306            node_key: None,
5307            key_signature: None,
5308            disco_key: None,
5309            node_key_expiry: Some(now + TimeDelta::days(30)),
5310            online: None,
5311            last_seen: None,
5312        };
5313        tracker.apply_peer_patches(std::slice::from_ref(&patch), now);
5314
5315        let (_id, stored) = tracker.peer_db.get(&peer.stable_id).expect("still a peer");
5316        assert!(!stored.expired, "the extension un-expires the peer");
5317        assert_eq!(stored.node_key, peer.node_key, "its real node key is back");
5318        assert_eq!(
5319            stored.underlay_addresses, peer.underlay_addresses,
5320            "and its direct-path candidates"
5321        );
5322        assert_eq!(
5323            stored.derp_region, peer.derp_region,
5324            "and its home DERP route"
5325        );
5326        assert_eq!(
5327            stored.peerapi_addr(),
5328            peer.peerapi_addr(),
5329            "so a peerAPI dial to it is answerable again"
5330        );
5331    }
5332
5333    /// The lookup a caller holding an older [`Node`] snapshot refreshes it through, end to end
5334    /// through the live actor: `tailscale::Device::send_file` re-reads the peer by stable id so it
5335    /// refuses an expired peer with the reason instead of dialing a broken one and reporting a
5336    /// timeout. Also pins that the query answers *immediately* before the first netmap — queueing
5337    /// it (as [`PeerByName`] does) would park a send behind a netmap that may never arrive.
5338    #[tokio::test]
5339    async fn peer_by_stable_id_answers_with_the_current_flagged_record() {
5340        let env = test_env();
5341        let (_tka_tx, tka_rx) = watch::channel(None);
5342        let live = PeerTracker::spawn((env.clone(), tka_rx));
5343
5344        let peer = expiring_peer("eXpIrEd", 7, Some(local_now() - TimeDelta::hours(1)));
5345
5346        // Before any netmap: an immediate `None`, not a queued reply.
5347        let unknown = tokio::time::timeout(
5348            std::time::Duration::from_secs(5),
5349            live.ask(PeerByStableId {
5350                stable_id: peer.stable_id.clone(),
5351            }),
5352        )
5353        .await
5354        .expect("the query answers without waiting for a netmap")
5355        .expect("peer tracker is alive");
5356        assert_eq!(unknown, None);
5357
5358        env.publish(Arc::new(netmap_with_peers(vec![peer.clone()])))
5359            .await
5360            .expect("publish netmap");
5361        await_peer_count(&live, 1).await;
5362
5363        let current = live
5364            .ask(PeerByStableId {
5365                stable_id: peer.stable_id.clone(),
5366            })
5367            .await
5368            .expect("peer tracker is alive")
5369            .expect("the expired peer is kept, so it is still resolvable by stable id");
5370        assert!(
5371            current.expired,
5372            "the record carries the flag, not the stale state"
5373        );
5374        assert_eq!(
5375            current.peerapi_addr(),
5376            None,
5377            "so a peerAPI dial resolved from it is refused"
5378        );
5379    }
5380
5381    /// `MapResponse.ControlTime` is the reason the comparison is exact rather than approximate: a
5382    /// node whose own clock is hours behind control's must still see a peer as expired.
5383    #[tokio::test]
5384    async fn a_control_time_delta_decides_expiry_against_controls_clock() {
5385        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
5386        let now = local_now();
5387        // Local time says this expires in an hour.
5388        let peer = expiring_peer("sKeWeD", 5, Some(now + TimeDelta::hours(1)));
5389
5390        // Control's clock is two hours ahead of ours, so by control's reckoning it went an hour ago.
5391        tracker
5392            .expiry
5393            .on_control_time(now + TimeDelta::hours(2), now);
5394        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer.clone()]), now);
5395
5396        let (_id, stored) = tracker.peer_db.get(&peer.stable_id).expect("kept");
5397        assert!(
5398            stored.expired,
5399            "expiry is judged against control's clock, not this host's"
5400        );
5401    }
5402}