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}
863
864impl Message<Arc<ts_control::StateUpdate>> for PeerTracker {
865    type Reply = ();
866
867    async fn handle(
868        &mut self,
869        msg: Arc<ts_control::StateUpdate>,
870        ctx: &mut Context<Self, Self::Reply>,
871    ) {
872        // Accumulate user profiles first — control sends them incrementally and a response may
873        // carry profiles with no peer delta (or peers that reference a profile from an earlier
874        // response), so this must happen before the no-peer-update early return below.
875        self.accumulate_user_profiles(&msg.user_profiles);
876
877        // Wall clock for everything below, sampled once so one response is evaluated at one
878        // instant. chrono is built without its `clock` feature in this workspace, so `local_now`
879        // derives it from `SystemTime` the same way the control runner / ssh-policy paths do.
880        let now = local_now();
881
882        // Record control's own clock BEFORE anything reads expiry — Go `onControlTime`, delivered
883        // to the expiry manager as its own event. From here on every expiry comparison is made
884        // against control's time, not this host's, so a node with a skewed clock neither expires
885        // peers early nor misses that they expired at all.
886        if let Some(control_time) = msg.control_time {
887            let delta = self.expiry.on_control_time(control_time, now);
888            if !delta.is_zero() {
889                tracing::debug!(
890                    delta_secs = delta.num_seconds(),
891                    "control's clock differs from ours; expiry is judged against control's time"
892                );
893            }
894        }
895
896        // Remember the self node so it can be folded into the next-expiry computation below, exactly as Go
897        // folds `nm.SelfNode` into `nextPeerExpiry`. Self is never a peer and is never flagged
898        // here; the runtime's own expiry decision stays with the control runner.
899        if let Some(self_node) = msg.node.as_ref() {
900            self.self_node = Some(self_node.clone());
901        }
902
903        // Apply the standalone online/last-seen delta maps (channels C/D, `MapResponse.OnlineChange`
904        // / `PeerSeenChange`). These arrive keyed by control node id and may ride a response that
905        // carries NO `peer_update` (a bare online flip is the common case), so they must be applied
906        // *before* the no-peer-update early return — otherwise online status freezes at the last
907        // full-node/patch value. Each entry only ever *sets* a value (never back to unknown).
908        // `now` (above) is also the wall clock for a `PeerSeenChange: true` (Go uses `clock.Now()`).
909        let liveness_changed =
910            self.apply_liveness_changes(&msg.online_change, &msg.peer_seen_change, now);
911
912        if msg.peer_update.is_none() && msg.peer_patches.is_empty() {
913            // No peer set or patch, so the peer expiries are unchanged — but the self node or the
914            // clock delta may have moved, so the timer still has to be re-aimed.
915            self.rearm_expiry_timer(now, ctx.actor_ref());
916
917            // No peer set or patch this response. If a liveness delta still mutated the netmap,
918            // publish the refreshed snapshot so watchers (and `GetStatus`) see the new online state.
919            if liveness_changed {
920                self.service_pending_requests();
921                self.peer_watch.send_replace(self.status_peers());
922                if let Err(e) = self
923                    .env
924                    .publish(Arc::new(PeerState {
925                        upserts: HashSet::default(),
926                        deletions: HashSet::default(),
927                        peers: Arc::new(self.peer_db.clone()),
928                    }))
929                    .await
930                {
931                    tracing::error!(error = %e, "publishing liveness-only peer state update");
932                }
933            }
934            return;
935        }
936
937        // Apply the whole-node peer set (if any) FIRST, then the field-level patches on top —
938        // mirroring Go's `controlclient` order (`Peers*` then `PeersChangedPatch`). A response may
939        // carry either, both, or (with a liveness-only delta) neither. Merge the upsert/deletion sets
940        // so the published `PeerState` reflects every node touched by both passes; a node both
941        // upserted by the set and patched stays in `upserts` (the patch removes it from `deletions`).
942        let (mut upserts, mut deletions) = msg
943            .peer_update
944            .as_ref()
945            .map(|u| self.apply_peer_update(u, now))
946            .unwrap_or_default();
947
948        if !msg.peer_patches.is_empty() {
949            let (patch_upserts, patch_deletions) = self.apply_peer_patches(&msg.peer_patches, now);
950            // A patch can evict a node the set just upserted (TKA rejection after key rotation), or
951            // re-admit/patch one not in the set — reconcile so each id lands in exactly one set.
952            for id in &patch_upserts {
953                deletions.remove(id);
954            }
955            for id in &patch_deletions {
956                upserts.remove(id);
957            }
958            upserts.extend(patch_upserts);
959            deletions.extend(patch_deletions);
960        }
961
962        tracing::debug!(
963            n_upsert = upserts.len(),
964            n_delete = deletions.len(),
965            peer_count = self.peer_db.peers().len(),
966            "new peer state"
967        );
968
969        // Aim the timer at the soonest expiry in the peer set this response just installed — Go
970        // `setControlClientStatusLocked`, which stops the old timer and starts a new one on every
971        // netmap. Peers already past their expiry were flagged on the way in, so what is left is
972        // strictly in the future.
973        self.rearm_expiry_timer(now, ctx.actor_ref());
974
975        self.service_pending_requests();
976
977        // Publish the latest peer snapshot to netmap watchers. `send_replace` keeps the receiver's
978        // value current even when there are no subscribers, so a late subscriber sees fresh state.
979        self.peer_watch.send_replace(self.status_peers());
980
981        if let Err(e) = self
982            .env
983            .publish(Arc::new(PeerState {
984                upserts,
985                deletions,
986                peers: Arc::new(self.peer_db.clone()),
987            }))
988            .await
989        {
990            tracing::error!(error = %e, "publishing peer state update");
991        }
992    }
993}
994
995impl Message<PeerDiscoKeyAdvertisement> for PeerTracker {
996    type Reply = ();
997
998    async fn handle(
999        &mut self,
1000        msg: PeerDiscoKeyAdvertisement,
1001        _ctx: &mut Context<Self, Self::Reply>,
1002    ) {
1003        if !self.learn_disco_key(msg.peer, msg.key) {
1004            return;
1005        }
1006
1007        // The key changed, so republish: the direct-path machinery resolves a peer's disco key out
1008        // of the published `PeerState` snapshot (`direct::DiscoPeerLookup`), which is the whole
1009        // point of learning it — it is what lets disco reach this peer without waiting for a
1010        // netmap update. Go does the equivalent by writing the key straight into the magicsock
1011        // endpoint and re-keying its peer map.
1012        self.peer_watch.send_replace(self.status_peers());
1013
1014        if let Err(e) = self
1015            .env
1016            .publish(Arc::new(PeerState {
1017                upserts: HashSet::from_iter([msg.peer]),
1018                deletions: HashSet::default(),
1019                peers: Arc::new(self.peer_db.clone()),
1020            }))
1021            .await
1022        {
1023            tracing::error!(error = %e, "publishing peer state after a TSMP disco-key advertisement");
1024        }
1025    }
1026}
1027
1028impl Message<DiscoKeyObserved> for PeerTracker {
1029    type Reply = ();
1030
1031    async fn handle(&mut self, msg: DiscoKeyObserved, _ctx: &mut Context<Self, Self::Reply>) {
1032        if !self.observe_disco_key(msg.peer, msg.key) {
1033            return;
1034        }
1035
1036        // The active key moved, so republish. This is the *same* channel a TSMP advertisement and a
1037        // netmap disco-key change use, and it is what makes the direct manager invalidate the
1038        // trusted path built under the old key: it diffs consecutive snapshots
1039        // (`direct::disco_key_rotations`) and calls `MagicSock::changed_active_disco` — this fork's
1040        // `endpoint.changedActiveDiscoLocked`, which Go likewise reaches from
1041        // `checkAndUpdateDiscoKey`. Keeping the switch and the invalidation on one path is why the
1042        // switch is done here rather than on the packet path that spotted it.
1043        self.peer_watch.send_replace(self.status_peers());
1044
1045        if let Err(e) = self
1046            .env
1047            .publish(Arc::new(PeerState {
1048                upserts: HashSet::from_iter([msg.peer]),
1049                deletions: HashSet::default(),
1050                peers: Arc::new(self.peer_db.clone()),
1051            }))
1052            .await
1053        {
1054            tracing::error!(error = %e, "publishing peer state after a disco active-key switch");
1055        }
1056    }
1057}
1058
1059/// Internal self-message: the armed expiry timer fired — the soonest key expiry the peer set knew
1060/// about has now passed, so expiry must be re-evaluated.
1061///
1062/// This is the whole point of the timer (Go `LocalBackend.nmExpiryTimer` →
1063/// `handleNetmapExpiry`): without it a peer whose key expires between two netmaps stays fully
1064/// configured — endpoints, DERP home, live node key — until control happens to send another
1065/// response, which on a steady map poll may be a long time.
1066///
1067/// Upstream `0640312e5` had to fix this path, because the timer there closed over the netmap
1068/// captured when it was armed and reinstalling that stale copy rolled back any delta that arrived
1069/// meanwhile; the fix re-reads live peer state before reinstalling. Here the pass reads the peer db
1070/// — the live state — directly, so there is no captured copy to roll anything back.
1071#[derive(Debug, Clone, Copy)]
1072pub(crate) struct ExpiryTimerFired;
1073
1074impl Message<ExpiryTimerFired> for PeerTracker {
1075    type Reply = ();
1076
1077    async fn handle(&mut self, _msg: ExpiryTimerFired, ctx: &mut Context<Self, Self::Reply>) {
1078        let now = local_now();
1079        let upserts = self.reevaluate_expiry(now);
1080
1081        // Re-aim at the next expiry after this one, whether or not anything was flagged: a timer
1082        // that fired early (clock skew, or the slack) must not be the last one armed.
1083        self.rearm_expiry_timer(now, ctx.actor_ref());
1084
1085        if upserts.is_empty() {
1086            return;
1087        }
1088
1089        // A newly expired peer lost its endpoints, its DERP home and its node key, so the
1090        // dataplane, route updater and source filter all have to see the new snapshot — the same
1091        // publish the netmap handler does after a peer set changes.
1092        self.service_pending_requests();
1093        self.peer_watch.send_replace(self.status_peers());
1094
1095        if let Err(e) = self
1096            .env
1097            .publish(Arc::new(PeerState {
1098                upserts,
1099                deletions: HashSet::default(),
1100                peers: Arc::new(self.peer_db.clone()),
1101            }))
1102            .await
1103        {
1104            tracing::error!(error = %e, "publishing peer state after a peer key expired");
1105        }
1106    }
1107}
1108
1109/// Internal self-message: the Tailnet-Lock enforcement-authority cell changed — the control runner
1110/// installed a freshly-synced [`Authority`](ts_tka::Authority) after a `/machine/tka/sync`, or
1111/// cleared it because the lock was disabled. Sent by the watch task
1112/// [`on_start`](kameo::Actor::on_start) spawns, so the peer db is re-filtered the moment enforcement
1113/// changes instead of at whatever later `Full` netmap happens to arrive.
1114#[derive(Debug, Clone, Copy)]
1115pub(crate) struct TkaAuthorityChanged;
1116
1117impl Message<TkaAuthorityChanged> for PeerTracker {
1118    type Reply = ();
1119
1120    async fn handle(&mut self, _msg: TkaAuthorityChanged, _ctx: &mut Context<Self, Self::Reply>) {
1121        let deletions = self.tka_reevaluate_peer_db();
1122        if deletions.is_empty() {
1123            // The common case: enforcement is inactive, or every admitted peer still verifies.
1124            return;
1125        }
1126
1127        // An evicted peer must lose its data path, not just its db row, so republish the snapshot
1128        // the `Arc<PeerState>` subscribers (route updater, source filter, dataplane) resolve
1129        // against — the same publish the netmap handler does after a peer set changes.
1130        self.peer_watch.send_replace(self.status_peers());
1131
1132        if let Err(e) = self
1133            .env
1134            .publish(Arc::new(PeerState {
1135                upserts: HashSet::default(),
1136                deletions,
1137                peers: Arc::new(self.peer_db.clone()),
1138            }))
1139            .await
1140        {
1141            tracing::error!(error = %e, "publishing peer state after a TKA authority change");
1142        }
1143    }
1144}
1145
1146/// Ask the peer tracker to re-broadcast its current peer snapshot on the bus, without any peer
1147/// change. Sent after a runtime preference change so the route updater and source filter (both
1148/// `Arc<PeerState>` subscribers) re-resolve against the new value immediately, rather than waiting
1149/// for the next netmap update: `Device::set_exit_node` (new exit-node selector) and
1150/// `Device::set_accept_routes` (new accept-routes flag) both send it.
1151#[derive(Debug, Clone, Copy)]
1152pub struct RepublishState;
1153
1154impl Message<RepublishState> for PeerTracker {
1155    type Reply = ();
1156
1157    async fn handle(&mut self, _msg: RepublishState, _ctx: &mut Context<Self, Self::Reply>) {
1158        // An empty upsert/deletion set: this is a re-broadcast of the unchanged peer set, not a
1159        // delta. Subscribers recompute their routes/filters against the current peers and the
1160        // (just-updated) runtime preferences (exit-node selector, accept-routes flag).
1161        if let Err(e) = self
1162            .env
1163            .publish(Arc::new(PeerState {
1164                upserts: HashSet::default(),
1165                deletions: HashSet::default(),
1166                peers: Arc::new(self.peer_db.clone()),
1167            }))
1168            .await
1169        {
1170            tracing::error!(error = %e, "re-publishing peer state after a runtime preference change");
1171        }
1172    }
1173}
1174
1175impl PeerTracker {
1176    /// Learn a peer's disco key from a TSMP disco-key advertisement, returning whether the
1177    /// advertisement was applied.
1178    ///
1179    /// Go [`magicsock.Conn.HandleDiscoKeyAdvertisement`], reduced to the state this fork keeps:
1180    /// Go stores the learned key on the magicsock endpoint and re-keys its peer map, whereas here
1181    /// the peer db's `disco_key` (and its disco index) *is* the live lookup every direct-path
1182    /// consumer reads. The key is recorded in the peer's [`EndpointDisco`] TSMP slot — never on top
1183    /// of control's — and the peer db then carries whichever of the two is active, so the next
1184    /// netmap cannot silently undo it ([`upsert_from_control`](Self::upsert_from_control)).
1185    ///
1186    /// The three refusals are Go's, in Go's order:
1187    ///
1188    /// 1. **A zero key is never learned.** Go checks it twice — `tstun` publishes only
1189    ///    `if !Key.IsZero()`, and `HandleDiscoKeyAdvertisement` rejects it again. The dataplane
1190    ///    already dropped it here too; this is the second check, kept because the cost of getting
1191    ///    it wrong is a peer bound to an unusable key.
1192    /// 2. **An unknown peer is ignored** (Go: "endpoint not found for node"). An advertisement
1193    ///    never creates a peer — only control does — so one that arrives before or after the
1194    ///    peer's netmap entry is a no-op, exactly like a `PeersChangedPatch` for an unknown node.
1195    /// 3. **An unchanged key is a no-op**, so a peer re-advertising the key we already hold costs
1196    ///    no upsert and no republish (Go counts this as
1197    ///    `magicsock_tsmp_disco_key_advertisement_unchanged` and returns). "Unchanged" is measured
1198    ///    against the **TSMP-learned** key (Go compares `epDisco.keyFromTSMP()`), NOT against the
1199    ///    effective one: an advertisement that merely restates what control already told us is new
1200    ///    information — it is the peer itself confirming the key — so it is recorded as the active
1201    ///    TSMP key and survives control later dropping or contradicting it.
1202    ///
1203    /// The tailnet-lock gate is deliberately *not* re-run: unlike a `PeersChangedPatch`, an
1204    /// advertisement cannot touch the node key or its TKA signature — only the disco key — so the
1205    /// peer-trust decision that admitted this node is unchanged by definition.
1206    ///
1207    /// [`magicsock.Conn.HandleDiscoKeyAdvertisement`]: https://github.com/tailscale/tailscale/blob/49e148c4a30b4f8098f69468fd27a7021d85ea02/wgengine/magicsock/magicsock.go
1208    fn learn_disco_key(&mut self, peer: PeerId, key: DiscoPublicKey) -> bool {
1209        if disco_key_is_zero(&key) {
1210            tracing::debug!(?peer, "TSMP-advertised disco key is the zero key; ignoring");
1211            return false;
1212        }
1213
1214        let Some((_id, existing)) = self.peer_db.get(&peer) else {
1215            tracing::debug!(
1216                ?peer,
1217                "TSMP disco-key advertisement for unknown peer; ignoring"
1218            );
1219            return false;
1220        };
1221
1222        let node_key = existing.node_key;
1223        if self
1224            .endpoint_disco
1225            .get(&node_key)
1226            .and_then(EndpointDisco::key_from_tsmp)
1227            == Some(key)
1228        {
1229            tracing::trace!(?peer, "TSMP-advertised disco key is unchanged");
1230            return false;
1231        }
1232
1233        let node = existing.clone();
1234        let disco = self.endpoint_disco.entry(node_key).or_default();
1235        disco.update_from_tsmp(Some(key));
1236        let disco = *disco;
1237        self.store_disco(&node, disco);
1238
1239        tracing::info!(
1240            ?peer,
1241            stable_id = ?node.stable_id,
1242            %key,
1243            "learned peer disco key from a TSMP advertisement"
1244        );
1245
1246        true
1247    }
1248
1249    /// Write a peer's resolved disco state onto the peer db.
1250    ///
1251    /// The node lands carrying the **effective** key ([`EndpointDisco::key`]), which is what the
1252    /// disco index — and so every *send* path — resolves against, and the peer's other known key
1253    /// (if any) is registered as its inactive ingress key so a frame arriving under it still
1254    /// attributes to this peer ([`PeerDb::peer_by_known_disco_key`]).
1255    ///
1256    /// Every disco-key writer goes through here — control, a TSMP advertisement, and an
1257    /// active-slot switch on receive — so the two cannot drift apart on which key is which.
1258    fn store_disco(&mut self, node: &Node, disco: EndpointDisco) -> PeerId {
1259        let effective = disco.key();
1260
1261        let id = if effective == node.disco_key {
1262            self.peer_db.upsert(node)
1263        } else {
1264            let mut node = node.clone();
1265            node.disco_key = effective;
1266            self.peer_db.upsert(&node)
1267        };
1268
1269        self.peer_db
1270            .set_inactive_disco_key(id, disco.inactive_key());
1271
1272        id
1273    }
1274
1275    /// Apply the sender key of an inbound disco frame to this peer's two-slot disco state — the
1276    /// `ts_runtime` half of Go [`endpoint.checkAndUpdateDiscoKey`].
1277    ///
1278    /// A peer mid-rotation keeps sending disco under the key it has not yet switched away from.
1279    /// Upstream accepts either of the two keys it knows for the peer and, when the one received is
1280    /// the currently-inactive one, makes it active: receiving under a key is proof of what the peer
1281    /// is using, and is stronger evidence than what control last said. Without this a rotation
1282    /// costs the peer its direct path until control catches up or the peer re-advertises.
1283    ///
1284    /// Returns whether the active key changed, so the caller can republish — which is how the
1285    /// direct manager learns to invalidate the trusted path built under the old key (Go's
1286    /// `changedActiveDiscoLocked`, reached here through the same snapshot diff every other
1287    /// disco-key transition uses).
1288    ///
1289    /// The refusals, all of which leave the peer db untouched:
1290    ///
1291    /// 1. **An unknown peer**, exactly as for a TSMP advertisement.
1292    /// 2. **A peer with no disco key material at all** (Go: `epDisco == nil` ⇒ `false`).
1293    /// 3. **A key belonging to neither slot.** This is the one that carries the security value:
1294    ///    a peer must not be able to move itself onto a key nobody told us about, so a third key
1295    ///    is refused even though the frame that carried it opened correctly.
1296    ///
1297    /// [`endpoint.checkAndUpdateDiscoKey`]: https://github.com/tailscale/tailscale/blob/9ea7cba44591e0cd840c6c94d23274dd222059bf/wgengine/magicsock/endpoint.go
1298    fn observe_disco_key(&mut self, peer: PeerId, key: DiscoPublicKey) -> bool {
1299        let Some((_id, existing)) = self.peer_db.get(&peer) else {
1300            tracing::debug!(?peer, "disco received for an unknown peer; ignoring");
1301            return false;
1302        };
1303
1304        let node = existing.clone();
1305        let Some(disco) = self.endpoint_disco.get_mut(&node.node_key) else {
1306            // Go's `epDisco == nil`: the peer has no key from either source, so there is nothing
1307            // this key could match and nothing to switch to.
1308            tracing::debug!(
1309                ?peer,
1310                "disco received for a peer with no known disco key; ignoring"
1311            );
1312            return false;
1313        };
1314
1315        let Some(changed) = disco.check_and_update(key) else {
1316            tracing::debug!(
1317                ?peer,
1318                %key,
1319                "refusing disco under a key that is neither of the peer's known disco keys"
1320            );
1321            return false;
1322        };
1323
1324        if !changed {
1325            return false;
1326        }
1327
1328        let disco = *disco;
1329        self.store_disco(&node, disco);
1330
1331        tracing::info!(
1332            ?peer,
1333            stable_id = ?node.stable_id,
1334            %key,
1335            "peer is sending disco under its other known key; making that key active"
1336        );
1337
1338        true
1339    }
1340
1341    /// Upsert a control-sourced [`Node`] into the peer db, resolving its disco key against anything
1342    /// this peer has told us over TSMP first.
1343    ///
1344    /// Every node built from control goes through here — `Full`, `Delta { upsert }`, and a
1345    /// `PeersChangedPatch` — so the three cannot diverge on which of the two keys wins. This is the
1346    /// disco half of Go [`endpoint.updateFromNode`]: control's key is written through
1347    /// [`EndpointDisco::update_from_control`] **only when it differs from what control last said**
1348    /// (Go's `if discoKey != n.DiscoKey()` guard, which compares `keyFromControl()`, never the
1349    /// effective key). So a netmap that merely restates the key control already sent leaves an
1350    /// active TSMP key alone — which is the entire point of the advertisement, whose motivating case
1351    /// is a peer whose key control has not caught up with. Control genuinely changing its mind is
1352    /// *recorded* in control's slot, but it does not take the active slot back from a TSMP-learned
1353    /// key: upstream switches back only when disco is received under control's key
1354    /// (`endpoint.checkAndUpdateDiscoKey`). See [`EndpointDisco::update_from_control`].
1355    ///
1356    /// The node lands in the db carrying the *effective* key ([`EndpointDisco::key`]), so the disco
1357    /// index and every send path resolve against the key we would actually send to; the other known
1358    /// key is registered for ingress attribution ([`store_disco`](Self::store_disco)).
1359    ///
1360    /// [`endpoint.updateFromNode`]: https://github.com/tailscale/tailscale/blob/49e148c4a30b4f8098f69468fd27a7021d85ea02/wgengine/magicsock/endpoint.go
1361    fn upsert_from_control(&mut self, node: &Node, now: chrono::DateTime<chrono::Utc>) -> PeerId {
1362        // The expiry pass, at the one site every peer install funnels through — Go
1363        // `flagExpiredPeers`, which runs over the whole netmap on the way in. A peer whose key
1364        // expiry has passed (judged against CONTROL's clock) is rewritten, never dropped: it keeps
1365        // its identity so `whois`, `status` and a peerAPI dial can all say *why* it is unreachable,
1366        // but it loses its endpoints, its home DERP and its usable node key. `None` is the ordinary
1367        // case — no transition — and costs no clone.
1368        let flagged = self.expiry.flag_expired_peer(node, now);
1369        // Log the transition, not the rewrite: control restates an expired peer unflagged on every
1370        // full netmap, so without this the line (and the reader's alarm) would repeat forever.
1371        if let Some(flagged) = flagged.as_ref().filter(|f| f.first_transition) {
1372            if flagged.peer.expired {
1373                tracing::info!(
1374                    stable_id = ?flagged.peer.stable_id,
1375                    "peer's node key has expired; clearing its endpoints and DERP home and \
1376                     breaking its node key"
1377                );
1378            } else {
1379                tracing::info!(
1380                    stable_id = ?flagged.peer.stable_id,
1381                    "peer's node-key expiry was extended; restoring its node key"
1382                );
1383            }
1384        }
1385        let node = flagged.as_ref().map_or(node, |flagged| &flagged.peer);
1386
1387        let node_key = node.node_key;
1388        let from_control = disco_key_from_control(node.disco_key);
1389
1390        let disco = self.endpoint_disco.entry(node_key).or_default();
1391        if disco.key_from_control() != from_control {
1392            disco.update_from_control(from_control);
1393        }
1394        let disco = *disco;
1395
1396        // No key material from either source: Go nils the endpoint's `disco` pointer, so a peer
1397        // that has never had a disco key costs us no entry either.
1398        if disco.is_empty() {
1399            self.endpoint_disco.remove(&node_key);
1400        }
1401
1402        self.store_disco(node, disco)
1403    }
1404
1405    /// The disco key control last gave us for `node_key` — Go `endpointDisco.keyFromControl()`.
1406    fn control_disco_key(&self, node_key: &NodePublicKey) -> Option<DiscoPublicKey> {
1407        self.endpoint_disco
1408            .get(node_key)
1409            .and_then(EndpointDisco::key_from_control)
1410    }
1411
1412    /// Drop [`EndpointDisco`] state for node keys the peer db no longer holds.
1413    ///
1414    /// Go gets this for free: the two keys live on the magicsock `endpoint`, which the peer map keys
1415    /// by node key and deletes when the peer leaves the netmap — and a peer that rotates its node
1416    /// key gets a brand-new endpoint, so a TSMP-learned key is not carried across a rotation. Here
1417    /// the state is a side table, so every control update prunes it to get the same lifetime.
1418    fn prune_endpoint_disco(&mut self) {
1419        if self.endpoint_disco.is_empty() {
1420            return;
1421        }
1422
1423        let peers = &self.peer_db;
1424        self.endpoint_disco
1425            .retain(|node_key, _| peers.has(node_key).is_some());
1426    }
1427
1428    /// Apply a single [`PeerUpdate`](ts_control::PeerUpdate) to the peer db, enforcing the
1429    /// Tailnet-Lock peer-trust chokepoint ([`tka_admits`](Self::tka_admits)) at every upsert site.
1430    ///
1431    /// This is the **single source of truth** for the peer-trust enforcement loop: the actor's
1432    /// netmap [`handle`](Message::handle) calls it, and so do the TKA enforcement tests, so the two
1433    /// real upsert sites (`Full` and `Delta { upsert }`) cannot diverge from what is tested.
1434    ///
1435    /// `now` is the local wall clock the expiry pass in
1436    /// [`upsert_from_control`](Self::upsert_from_control) judges against (after correction for
1437    /// control's clock); it is threaded in rather than read per peer so one netmap is evaluated at
1438    /// one instant.
1439    ///
1440    /// Returns `(upserts, deletions)` — the [`PeerId`]s touched — for downstream bookkeeping.
1441    fn apply_peer_update(
1442        &mut self,
1443        peer_update: &ts_control::PeerUpdate,
1444        now: chrono::DateTime<chrono::Utc>,
1445    ) -> (HashSet<PeerId>, HashSet<PeerId>) {
1446        let mut upserts = HashSet::default();
1447        let mut deletions = HashSet::default();
1448
1449        match peer_update {
1450            ts_control::PeerUpdate::Full(new_nodes) => {
1451                tracing::trace!("full peer update");
1452
1453                // Borrow the authority ONCE for the whole batch and verify each peer EXACTLY once
1454                // (Go runs `tkaFilterNetmapLocked` once over the assembled netmap; an earlier draft
1455                // verified every peer twice — once for `retained_ids`, once in the upsert loop —
1456                // doubling the ed25519 cost on the hot resync path). `tka_keep_verdicts` is that one
1457                // pass — per-peer signature verdict AND the cross-peer rotation filter — and is
1458                // shared verbatim with `tka_reevaluate_peer_db`, so the netmap path and the
1459                // authority-install path cannot drift apart on what "admitted" means.
1460                //
1461                // The result is a per-NODE keep vector (not a stable_id set), which drives both the
1462                // `retain` (evict revoked peers, keyed by stable_id) and the upsert loop. Judging
1463                // each node by its own verdict means a node whose signature fails is never admitted
1464                // on the strength of a different node that happens to share its stable_id.
1465                //
1466                // Revocation evicts: a peer re-included with a now-invalid/missing signature under an
1467                // active authority fails its verdict, so it is excluded from `retained_ids` and
1468                // `retain` drops the stale (previously-admitted) entry. With no authority the snapshot
1469                // is `None`, so every node passes — byte-for-byte the pre-TKA behavior (no regression).
1470                let authority = self.tka_authority_snapshot();
1471                let node_refs = new_nodes.iter().collect::<Vec<&Node>>();
1472                let keep = Self::tka_keep_verdicts(authority.as_deref(), &node_refs);
1473
1474                // `retained_ids` is the set of stable_ids that survive (drives `retain` to evict the
1475                // rest). It must agree with what the upsert loop below will leave in the db. Control
1476                // should never send two distinct nodes with the same `stable_id` in one `Full`, but if
1477                // it does, `peer_db.upsert` is last-writer-wins on `stable_id`, so the db ends holding
1478                // the LAST kept node for that id. Build `retained_ids` from kept nodes only — a
1479                // stable_id is retained iff at least one of its (possibly duplicate) nodes is kept, so
1480                // the upsert loop's last-kept node lands and `retain` never evicts a just-upserted id.
1481                let retained_ids = new_nodes
1482                    .iter()
1483                    .zip(keep.iter().copied())
1484                    .filter(|(_, k)| *k)
1485                    .map(|(node, _)| &node.stable_id)
1486                    .collect::<HashSet<_>>();
1487
1488                // Isolation diagnostic: an ACTIVE lock that authorized none of the offered peers
1489                // leaves this node with no peers — surface it loudly so a self-lockout (vs an attack)
1490                // is diagnosable. `authority.is_some()` means a real keyed lock (the empty-keyset
1491                // brick-guard admits-all, so it never reaches here with zero retained).
1492                if authority.is_some() && !new_nodes.is_empty() && retained_ids.is_empty() {
1493                    tracing::error!(
1494                        offered = new_nodes.len(),
1495                        "TKA: active lock authorized ZERO of the offered peers; node is isolated \
1496                         (verify the lock state, or disable tailnet lock to recover)"
1497                    );
1498                }
1499
1500                self.peer_db.retain(|id, peer| {
1501                    let retain = retained_ids.contains(&peer.stable_id);
1502
1503                    if !retain {
1504                        deletions.insert(id);
1505                    }
1506
1507                    retain
1508                });
1509
1510                for (node, k) in new_nodes.iter().zip(keep.iter().copied()) {
1511                    if !k {
1512                        continue; // fail-CLOSED: rejected by tailnet lock or rotation-obsolete (above)
1513                    }
1514                    let peer_id = self.upsert_from_control(node, now);
1515                    upserts.insert(peer_id);
1516                }
1517            }
1518
1519            ts_control::PeerUpdate::Delta { remove, upsert } => {
1520                tracing::trace!("delta peer update");
1521
1522                for peer in upsert {
1523                    if !self.tka_admits(peer) {
1524                        // fail-CLOSED: do not upsert a peer rejected by tailnet lock. If the peer is
1525                        // ALREADY in the db (a delta re-upserting an existing peer whose signature is
1526                        // now invalid — e.g. revoked between syncs), evict the stale entry rather than
1527                        // leaving an unverified peer admitted; Go re-filters the whole netmap each map
1528                        // response, so a now-unsigned peer would not survive there either.
1529                        if let Some((id, _)) = self.peer_db.remove(&peer.stable_id) {
1530                            tracing::warn!(
1531                                stable_id = ?peer.stable_id,
1532                                "TKA: delta re-upsert rejected; evicting now-unauthorized peer"
1533                            );
1534                            deletions.insert(id);
1535                        }
1536                        continue;
1537                    }
1538                    let id = self.upsert_from_control(peer, now);
1539
1540                    upserts.insert(id);
1541                }
1542
1543                for peer in remove {
1544                    let Some((id, _node)) = self.peer_db.remove(peer) else {
1545                        // A benign, expected race: the peer may already be gone (dropped in a prior
1546                        // `Full`, or fail-closed by TKA — whose now-"unknown" ids commonly reappear in
1547                        // a trailing `peers_removed`). Go treats an unknown removal as a no-op; log at
1548                        // debug, not error, to avoid false-alarm noise on a healthy node (matches the
1549                        // unknown-node handling in `apply_peer_patches`).
1550                        tracing::debug!(
1551                            control_node_id = peer,
1552                            "removed peer was unknown; ignoring"
1553                        );
1554                        continue;
1555                    };
1556
1557                    deletions.insert(id);
1558                }
1559            }
1560        }
1561
1562        self.prune_endpoint_disco();
1563
1564        (upserts, deletions)
1565    }
1566
1567    /// Re-run the Tailnet-Lock filter over the peers **already in the peer db**, evicting the ones
1568    /// the current authority does not admit. Returns the evicted [`PeerId`]s (empty when nothing
1569    /// changed, which is the overwhelmingly common case).
1570    ///
1571    /// # Why this exists (a Go-ordering gap, not an extra feature)
1572    /// Go filters the very netmap that announced the lock: `SetControlClientStatus`
1573    /// (`ipn/ipnlocal/local.go`, v1.100.0) calls `tkaSyncIfNeeded` and then, a few lines later,
1574    /// `tkaFilterNetmapLocked(st.NetMap)` — synchronously, on the same `st.NetMap`, in one pass. So
1575    /// the peers announced alongside `TKAEnabled` are checked by the authority that sync just built.
1576    ///
1577    /// Here the sync is a spawned task (`control_runner`'s `maybe_sync_tka`), so the ordering is
1578    /// inverted: the netmap that carried the `TkaStatus` reaches the peer db *before* the authority
1579    /// exists, and is admitted with enforcement inactive. Without this pass those peers stay
1580    /// admitted — unauthorized ones included — until control happens to send another `Full`, which on
1581    /// a steady map poll may be never. That is the whole initial peer set escaping a lock the node
1582    /// really did sync, so this runs the moment the authority is installed ([`TkaAuthorityChanged`])
1583    /// and brings the db back in line.
1584    ///
1585    /// No authority (nothing synced yet, or the lock was disabled) ⇒ no eviction: enforcement is
1586    /// inactive and every peer is admitted, exactly Go's `b.tka == nil` early return. A peer dropped
1587    /// while the lock was active is **not** resurrected by a later disable — the db no longer holds
1588    /// it and this fork keeps no shadow copy of filtered nodes (Go's `b.tka.filtered`); it returns on
1589    /// the next netmap that re-includes it. That is the safe direction: more restrictive, and
1590    /// connectivity-only.
1591    fn tka_reevaluate_peer_db(&mut self) -> HashSet<PeerId> {
1592        let Some(authority) = self.tka_authority_snapshot() else {
1593            return HashSet::default();
1594        };
1595
1596        // Verdicts first, under an immutable borrow of the db; the eviction below needs `&mut`.
1597        let evicted: HashSet<PeerId> = {
1598            let entries = self
1599                .peer_db
1600                .peers()
1601                .iter()
1602                // A peer this node already flagged expired is skipped: its node key is one WE
1603                // broke (`ExpiryManager::flag_expired_peer`), so re-verifying control's signature
1604                // against it would be checking our own mutation, and the peer would always be
1605                // evicted. It was admitted by the lock when it was installed, against the real key
1606                // control sent, and it has had no usable key since — so keeping the row costs no
1607                // trust and preserves the thing expiry flagging exists for: an expired peer is
1608                // FLAGGED, not dropped, so a caller can say why it is unreachable.
1609                .filter(|(_, node)| !node.expired)
1610                .map(|(id, node)| (*id, node))
1611                .collect::<Vec<(PeerId, &Node)>>();
1612            let nodes = entries
1613                .iter()
1614                .map(|(_, node)| *node)
1615                .collect::<Vec<&Node>>();
1616            let keep = Self::tka_keep_verdicts(Some(&authority), &nodes);
1617            entries
1618                .iter()
1619                .zip(keep)
1620                .filter_map(|((id, _), keep)| (!keep).then_some(*id))
1621                .collect()
1622        };
1623
1624        if evicted.is_empty() {
1625            return evicted;
1626        }
1627
1628        tracing::warn!(
1629            n_evicted = evicted.len(),
1630            peer_count = self.peer_db.peers().len(),
1631            "TKA: re-filtered the peer db against the newly installed lock authority; evicted \
1632             already-admitted peers"
1633        );
1634        self.peer_db.retain(|id, _| !evicted.contains(&id));
1635        self.prune_endpoint_disco();
1636        evicted
1637    }
1638
1639    /// Re-run the expiry pass over the peers **already in the peer db**, returning the [`PeerId`]s
1640    /// whose node changed (empty when nothing expired, the overwhelmingly common case).
1641    ///
1642    /// The timer's counterpart to the pass [`upsert_from_control`](Self::upsert_from_control) runs
1643    /// on the way in: that one catches a peer that was already expired when control handed it to
1644    /// us, this one catches a peer that expires while we sit on the same netmap.
1645    ///
1646    /// Only peers that actually transition are cloned and re-installed — the pass returns `None`
1647    /// for the rest — so a timer firing over a large peer set costs one walk and a handful of
1648    /// upserts. Re-installing goes through the ordinary upsert path so the node-key index follows
1649    /// the peer's now-broken key; the pass there is a no-op on an already-flagged peer.
1650    fn reevaluate_expiry(&mut self, now: chrono::DateTime<chrono::Utc>) -> HashSet<PeerId> {
1651        let flagged = self
1652            .peer_db
1653            .peers()
1654            .values()
1655            .filter_map(|peer| self.expiry.flag_expired_peer(peer, now))
1656            .collect::<Vec<ts_control::FlaggedPeer>>();
1657
1658        if flagged.is_empty() {
1659            return HashSet::default();
1660        }
1661
1662        tracing::info!(
1663            n = flagged.len(),
1664            "netmap expiry timer fired; peers whose node keys expired between netmaps"
1665        );
1666
1667        let mut upserts = HashSet::default();
1668        for flagged in &flagged {
1669            // `upsert_from_control` re-runs the pass, which is a no-op on the peer it just
1670            // rewrote — so the log line belongs here, where the transition is known.
1671            if flagged.first_transition && flagged.peer.expired {
1672                tracing::info!(
1673                    stable_id = ?flagged.peer.stable_id,
1674                    "peer's node key has expired; clearing its endpoints and DERP home and \
1675                     breaking its node key"
1676                );
1677            }
1678            upserts.insert(self.upsert_from_control(&flagged.peer, now));
1679        }
1680        self.prune_endpoint_disco();
1681
1682        upserts
1683    }
1684
1685    /// Stop the armed expiry timer and arm a new one for the soonest future key expiry across the
1686    /// peer db and the self node — Go `setControlClientStatusLocked`'s `nmExpiryTimer` block.
1687    ///
1688    /// No future expiry (every peer tagged or already flagged, and no self expiry) leaves no timer
1689    /// armed; the next netmap re-decides. The delay carries upstream's
1690    /// [`EXPIRY_TIMER_SLACK_SECS`](ts_control::EXPIRY_TIMER_SLACK_SECS) of slack so the key is
1691    /// unambiguously past its expiry by the time the pass runs.
1692    ///
1693    /// The old timer is **aborted**, which is this fork's version of Go's `numClientStatusCalls`
1694    /// generation check: a task that has already been dropped cannot deliver a stale wake-up. The
1695    /// spawned task holds only a `WeakActorRef`, so it can never keep the tracker's mailbox alive
1696    /// past shutdown.
1697    fn rearm_expiry_timer(&mut self, now: chrono::DateTime<chrono::Utc>, slf: &ActorRef<Self>) {
1698        if let Some(timer) = self.expiry_timer.take() {
1699            timer.abort();
1700        }
1701
1702        let Some(next) = self.expiry.next_peer_expiry(
1703            self.peer_db.peers().values(),
1704            self.self_node.as_ref(),
1705            now,
1706        ) else {
1707            return;
1708        };
1709
1710        let delay = (next - now) + chrono::TimeDelta::seconds(ts_control::EXPIRY_TIMER_SLACK_SECS);
1711        // `next` is never before `now` (the expiry manager floors it), so the conversion holds; a
1712        // negative delta would only mean "fire immediately", which is also the safe reading.
1713        let delay = delay.to_std().unwrap_or(std::time::Duration::ZERO);
1714
1715        tracing::debug!(
1716            delay_secs = delay.as_secs(),
1717            "arming the netmap expiry timer for the next node-key expiry"
1718        );
1719
1720        let notify = slf.downgrade();
1721        self.expiry_timer = Some(tokio::spawn(async move {
1722            tokio::time::sleep(delay).await;
1723            let Some(tracker) = notify.upgrade() else {
1724                return; // the peer tracker is gone; nothing left to re-evaluate
1725            };
1726            if let Err(e) = tracker.tell(ExpiryTimerFired).await {
1727                tracing::debug!(error = %e, "peer tracker stopped before the expiry timer fired");
1728            }
1729        }));
1730    }
1731
1732    /// Apply field-level peer patches (`MapResponse.PeersChangedPatch`), returning the upserted /
1733    /// deleted [`PeerId`]s.
1734    ///
1735    /// This is a SEPARATE channel from [`apply_peer_update`](Self::apply_peer_update): Go's
1736    /// `controlclient` applies the whole-node `Peers*` set first and then `PeersChangedPatch`, so a
1737    /// response that carries both has the peer set applied first (by the caller) and these patches
1738    /// applied second, on top of the freshly-synced nodes. A patch only mutates a peer already in the
1739    /// netmap; an unknown node id is ignored (the wire contract — a patch never creates a node).
1740    fn apply_peer_patches(
1741        &mut self,
1742        patches: &[ts_control::PeerChange],
1743        now: chrono::DateTime<chrono::Utc>,
1744    ) -> (HashSet<PeerId>, HashSet<PeerId>) {
1745        let mut upserts = HashSet::default();
1746        let mut deletions = HashSet::default();
1747
1748        tracing::trace!(n = patches.len(), "peer patch update");
1749
1750        for patch in patches {
1751            // Clone the current node, apply the present fields, and re-upsert through the same path
1752            // as a delta so indexes/routes stay consistent.
1753            let Some((_id, existing)) = self.peer_db.get(&patch.id) else {
1754                tracing::debug!(
1755                    control_node_id = patch.id,
1756                    "peer patch for unknown node; ignoring"
1757                );
1758                continue;
1759            };
1760
1761            let mut node = existing.clone();
1762            if let Some(endpoints) = &patch.underlay_addresses {
1763                node.underlay_addresses = endpoints.clone();
1764            }
1765            if let Some(derp) = patch.derp_region {
1766                node.derp_region = Some(derp);
1767            }
1768            if let Some(cap) = patch.cap {
1769                node.cap = cap;
1770            }
1771            if let Some(cap_map) = &patch.cap_map {
1772                node.cap_map = cap_map.clone();
1773            }
1774            // The db entry carries the EFFECTIVE disco key, which may have been learned over TSMP,
1775            // so restate what CONTROL last said before folding the patch in. Otherwise a patch that
1776            // says nothing about the disco key would hand a TSMP-learned key back as if control had
1777            // sent it, and `upsert_from_control` would write it into control's slot — losing the key
1778            // control actually gave us, on a patch that never mentioned the disco key at all.
1779            node.disco_key = self.control_disco_key(&node.node_key);
1780            if let Some(disco_key) = patch.disco_key {
1781                node.disco_key = Some(disco_key);
1782            }
1783            if let Some(expiry) = patch.node_key_expiry {
1784                node.node_key_expiry = Some(expiry);
1785            }
1786            // Online/last-seen liveness deltas (`PeerChange.Online`/`LastSeen`) — the dominant
1787            // channel by which peer online transitions arrive mid-session. A patch only ever *sets*
1788            // a value (never patches back to unknown), so apply when present.
1789            if let Some(online) = patch.online {
1790                node.online = Some(online);
1791            }
1792            if let Some(last_seen) = patch.last_seen {
1793                node.last_seen = Some(last_seen);
1794            }
1795            // Key rotation: a patch may swap the node key (and its TKA signature). Apply both
1796            // together so the trust gate below verifies the new signature against the new key, never
1797            // a mismatched pair.
1798            if let Some(node_key) = patch.node_key {
1799                node.node_key = node_key;
1800                // Control restated the key, so this node's own break of it (if the peer had
1801                // expired) no longer applies: clear the client-set flag and let the expiry pass in
1802                // `upsert_from_control` decide again against the (possibly also patched) expiry.
1803                // Go gets this for free — it patches a pristine node and re-runs `flagExpiredPeers`
1804                // over the result.
1805                node.expired = false;
1806            }
1807            if let Some(sig) = &patch.key_signature {
1808                node.key_signature = sig.clone();
1809            }
1810
1811            // Re-run the tailnet-lock gate on the patched node: a patch that rotates the key must
1812            // satisfy the active authority, exactly like a `Delta` upsert, or it would be a
1813            // trust-enforcement bypass. fail-CLOSED — if the patched node is no longer admitted,
1814            // evict it rather than keep the stale (now-unverified) entry.
1815            if !self.tka_admits(&node) {
1816                if let Some((id, _)) = self.peer_db.remove(&patch.id) {
1817                    tracing::warn!(
1818                        control_node_id = patch.id,
1819                        "peer patch rejected by tailnet lock; evicting peer"
1820                    );
1821                    deletions.insert(id);
1822                }
1823                continue;
1824            }
1825
1826            let id = self.upsert_from_control(&node, now);
1827            upserts.insert(id);
1828        }
1829
1830        self.prune_endpoint_disco();
1831
1832        (upserts, deletions)
1833    }
1834
1835    /// Apply the standalone online/last-seen delta maps (`MapResponse.OnlineChange` /
1836    /// `PeerSeenChange`, channels C/D) onto the retained netmap. Returns `true` if any node was
1837    /// actually mutated (so the caller knows whether to re-publish).
1838    ///
1839    /// Mirrors Go `controlclient/map.go:updatePeersStateFromResponse` (the two channels are
1840    /// semantically DISTINCT and must not be conflated):
1841    /// - `OnlineChange` (channel C) is the sole driver of a peer's `online` flag (`mut.Online = v`).
1842    /// - `PeerSeenChange` (channel D) is the sole driver of `last_seen`: `true ⇒ LastSeen = now`,
1843    ///   `false ⇒ LastSeen = nil` (cleared). It NEVER touches `online` — "not seen recently" is not
1844    ///   the same as "offline", which only `OnlineChange` asserts.
1845    ///
1846    /// Each entry is keyed by control node id and applies to a peer already in the netmap; an unknown
1847    /// node id is ignored (these maps never create a node). `now` is the wall-clock timestamp for a
1848    /// `PeerSeenChange: true` (Go uses `clock.Now()`); the caller passes it so this stays a pure
1849    /// function of its inputs. Returns `true` if any node was actually mutated.
1850    fn apply_liveness_changes(
1851        &mut self,
1852        online_change: &std::collections::BTreeMap<ts_control::NodeId, bool>,
1853        peer_seen_change: &std::collections::BTreeMap<ts_control::NodeId, bool>,
1854        now: chrono::DateTime<chrono::Utc>,
1855    ) -> bool {
1856        let mut changed = false;
1857
1858        // Channel C — direct online flips (the only writer of `online`).
1859        for (&node_id, &online) in online_change {
1860            if let Some((_pid, existing)) = self.peer_db.get(&node_id)
1861                && existing.online != Some(online)
1862            {
1863                let mut node = existing.clone();
1864                node.online = Some(online);
1865                self.peer_db.upsert(&node);
1866                changed = true;
1867            }
1868        }
1869
1870        // Channel D — peer-seen flips (the only writer of `last_seen`; never touches `online`).
1871        // `true` ⇒ last-seen is now; `false` ⇒ last-seen cleared (Go map.go:820-830).
1872        for (&node_id, &seen) in peer_seen_change {
1873            let new_last_seen = if seen { Some(now) } else { None };
1874            if let Some((_pid, existing)) = self.peer_db.get(&node_id)
1875                && existing.last_seen != new_last_seen
1876            {
1877                let mut node = existing.clone();
1878                node.last_seen = new_last_seen;
1879                self.peer_db.upsert(&node);
1880                changed = true;
1881            }
1882        }
1883
1884        changed
1885    }
1886
1887    /// Test-only constructor: build a [`PeerTracker`] with a chosen initial TKA authority without
1888    /// going through the actor `on_start` path. Returns the tracker plus the **`watch::Sender`** for
1889    /// its enforcement-authority cell, so a test can drive the exact enable/disable transitions the
1890    /// control runner drives at runtime (`tx.send_replace(Some(..))` ⇒ enforce, `tx.send_replace(None)`
1891    /// ⇒ clear). The initial `Some` exercises the fail-closed chokepoint
1892    /// ([`tka_admits`](Self::tka_admits)); `None` is the no-lock admit-all path. The returned sender
1893    /// must be kept alive for the tracker to read updated values.
1894    #[cfg(test)]
1895    fn for_test(
1896        env: Env,
1897        tka_authority: Option<ts_tka::Authority>,
1898    ) -> (Self, watch::Sender<Option<Arc<ts_tka::Authority>>>) {
1899        let (peer_watch, _) = watch::channel(Vec::new());
1900        let (tka_tx, tka_rx) = watch::channel(tka_authority.map(Arc::new));
1901        let tracker = Self {
1902            peer_db: PeerDb::default(),
1903            seen_state_update: false,
1904            pending_requests: Vec::new(),
1905            peer_watch,
1906            user_profiles: HashMap::new(),
1907            endpoint_disco: HashMap::new(),
1908            tka_authority: tka_rx,
1909            expiry: ExpiryManager::new(),
1910            self_node: None,
1911            expiry_timer: None,
1912            env,
1913        };
1914        (tracker, tka_tx)
1915    }
1916
1917    fn service_pending_requests(&mut self) {
1918        if self.seen_state_update {
1919            return;
1920        }
1921
1922        self.seen_state_update = true;
1923
1924        if !self.pending_requests.is_empty() {
1925            tracing::debug!(
1926                n_pending = self.pending_requests.len(),
1927                "state update received, servicing pending requests"
1928            );
1929        }
1930
1931        for req in core::mem::take(&mut self.pending_requests) {
1932            match req {
1933                Pending::PeerByName(PeerByName { name }, reply) => {
1934                    reply.send(self.peer_by_name_opt(&name).cloned());
1935                }
1936                Pending::TailnetIp(PeerByTailnetIp { ip }, reply) => {
1937                    reply.send(self.peer_by_tailnet_ip_opt(ip).cloned());
1938                }
1939                Pending::AcceptedRoute(PeerByAcceptedRoute { ip }, reply) => {
1940                    reply.send(
1941                        self.peer_db
1942                            .get_route(ip.into())
1943                            .map(|(_id, node)| node.clone())
1944                            .collect(),
1945                    );
1946                }
1947                Pending::Status(reply) => {
1948                    reply.send(self.status_peers_with_ids());
1949                }
1950                Pending::WhoIs(Whois { addr }, reply) => {
1951                    reply.send(self.whois_opt(addr));
1952                }
1953            }
1954        }
1955    }
1956}
1957
1958#[cfg(test)]
1959pub(crate) mod tka_tests {
1960    //! Tailnet-Lock (TKA) enforcement tests for the peer-trust chokepoint.
1961    //!
1962    //! These exercise [`PeerTracker::tka_admits`] and the `tka_admits ⇒ upsert` loop the netmap
1963    //! handler runs. The test [`ts_tka::Authority`] is built with [`ts_tka::Authority::from_state`]
1964    //! over a known Ed25519 trusted key, and the signed node-key signature CBOR is produced through
1965    //! `ts_tka`'s public `cbor` encoder + `aum_hash` (the exact same canonical bytes `ts_tka`'s own
1966    //! `direct_signature_verifies_end_to_end` test signs, with no new crypto vectors invented and no
1967    //! private `ts_tka` API used).
1968
1969    use ed25519_dalek::{Signer, SigningKey};
1970    use ts_control::{Node, StableNodeId, TailnetAddress};
1971    use ts_tka::{
1972        AumHash, Authority, Key, KeyKind, State,
1973        cbor::{self, Value},
1974    };
1975
1976    use super::*;
1977
1978    /// `SigKind::Direct` wire value (Go `SigKind`; `ts_tka::SigKind::Direct = 1`).
1979    const SIG_KIND_DIRECT: u64 = 1;
1980
1981    /// The 32-byte node key used across the signed-peer fixtures.
1982    const NODE_KEY_BYTES: [u8; 32] = [7u8; 32];
1983
1984    /// Build a real [`Env`] for the tracker. Only the bus/keys/shutdown plumbing matters here; the
1985    /// TKA gate reads neither, so the forwarding preferences are all benign defaults.
1986    pub(super) fn test_env() -> Env {
1987        let (_shutdown_tx, shutdown_rx) = watch::channel(false);
1988        Env::new(
1989            ts_keys::NodeState::generate(),
1990            shutdown_rx,
1991            crate::env::ForwarderConfig {
1992                accept_routes: false,
1993                accept_dns: true,
1994                exit_node: None,
1995                forward_routes: Vec::new(),
1996                forward_tcp_ports: Vec::new(),
1997                forward_udp_ports: Vec::new(),
1998                forward_all_ports: false,
1999                forward_exit_egress: false,
2000                block_incoming: false,
2001                exit_proxy: None,
2002                peerapi_port: None,
2003                taildrop_dir: None,
2004                enable_ipv6: false,
2005                wireguard_listen_port: None,
2006                network_monitor: false,
2007                persistent_keepalive_interval: None,
2008                ingress_active: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
2009            },
2010        )
2011    }
2012
2013    /// A minimal peer [`Node`] carrying `node_key` and the given `key_signature`.
2014    ///
2015    /// `pub(crate)` so the cold-start replay tests in `control_runner` build their peers the same way
2016    /// this module's TKA tests do — both run the same filter, and a second hand-rolled fixture could
2017    /// drift from it.
2018    pub(crate) fn peer_node(stable_id: &str, node_key: [u8; 32], key_signature: Vec<u8>) -> Node {
2019        Node {
2020            id: 1,
2021            stable_id: StableNodeId(stable_id.to_string()),
2022            hostname: stable_id.to_string(),
2023            user_id: 0,
2024            tailnet: Some("ts.net".to_string()),
2025            tags: Vec::new(),
2026            addresses: vec![
2027                "100.64.0.1/32".parse().unwrap(),
2028                "fd7a:115c:a1e0::1/128".parse().unwrap(),
2029            ],
2030            tailnet_address: TailnetAddress {
2031                ipv4: "100.64.0.1/32".parse().unwrap(),
2032                ipv6: "fd7a:115c:a1e0::1/128".parse().unwrap(),
2033            },
2034            node_key: node_key.into(),
2035            node_key_expiry: None,
2036            expired: false,
2037            online: None,
2038            last_seen: None,
2039            key_signature,
2040            machine_key: None,
2041            disco_key: None,
2042            accepted_routes: Vec::new(),
2043            underlay_addresses: Vec::new(),
2044            derp_region: None,
2045            cap: Default::default(),
2046            cap_map: Default::default(),
2047            peerapi_port: None,
2048            peerapi_dns_proxy: false,
2049            is_wireguard_only: false,
2050            exit_node_dns_resolvers: Vec::new(),
2051            peer_relay: false,
2052            ssh_host_keys: Vec::new(),
2053            service_vips: Default::default(),
2054            unsigned_peer_api_only: false,
2055        }
2056    }
2057
2058    /// Encode a `Direct` [`ts_tka::NodeKeySignature`] CBOR exactly as `ts_tka`'s private `to_cbor`
2059    /// does (int-map keys: 1=kind, 2=pubkey, 3=key_id, 4=signature; empty byte fields omitted),
2060    /// using only the crate's *public* `cbor` encoder. `signature` of `None` produces the
2061    /// signing-digest preimage (the `SigHash` form).
2062    fn direct_sig_cbor(node_key: &[u8], key_id: &[u8], signature: Option<&[u8]>) -> Vec<u8> {
2063        let mut pairs = alloc_pairs(node_key, key_id);
2064        if let Some(sig) = signature {
2065            pairs.push((4, Some(Value::Bytes(sig.to_vec()))));
2066        }
2067        cbor::int_map(pairs).to_vec()
2068    }
2069
2070    fn alloc_pairs(node_key: &[u8], key_id: &[u8]) -> Vec<(u64, Option<Value>)> {
2071        vec![
2072            (1, Some(Value::Uint(SIG_KIND_DIRECT))),
2073            (2, Some(Value::Bytes(node_key.to_vec()))),
2074            (3, Some(Value::Bytes(key_id.to_vec()))),
2075        ]
2076    }
2077
2078    /// Build a TKA [`Authority`] that trusts `signing.verifying_key()`, plus a valid `Direct`
2079    /// node-key signature CBOR authorizing [`NODE_KEY_BYTES`] under it.
2080    fn authority_and_valid_sig() -> (Authority, Vec<u8>) {
2081        // A fixed, known Ed25519 trusted key (mirrors ts_tka's own end-to-end test seed).
2082        let signing = SigningKey::from_bytes(&[42u8; 32]);
2083        let trusted_pub = signing.verifying_key().to_bytes().to_vec();
2084
2085        let authority = Authority::from_state(
2086            AumHash([0; 32]),
2087            State {
2088                keys: vec![Key {
2089                    kind: KeyKind::Ed25519,
2090                    votes: 1,
2091                    public: trusted_pub.clone(),
2092                }],
2093            },
2094        );
2095
2096        // SigHash preimage = canonical CBOR with the signature field omitted; sign its blake2s hash.
2097        let preimage = direct_sig_cbor(&NODE_KEY_BYTES, &trusted_pub, None);
2098        let sig_hash = ts_tka::aum_hash(&preimage).0;
2099        let signature = signing.sign(&sig_hash).to_bytes().to_vec();
2100
2101        let signed_cbor = direct_sig_cbor(&NODE_KEY_BYTES, &trusted_pub, Some(&signature));
2102        // Sanity: the authority accepts the signature we just built (same path the gate uses).
2103        assert!(
2104            authority
2105                .node_key_authorized(&NODE_KEY_BYTES, &signed_cbor)
2106                .is_ok()
2107        );
2108
2109        (authority, signed_cbor)
2110    }
2111
2112    #[tokio::test]
2113    async fn tka_inactive_upserts_all_peers() {
2114        // No authority ⇒ enforcement inactive ⇒ both a signed and an unsigned peer are admitted.
2115        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
2116
2117        let signed = peer_node("signed", [1u8; 32], vec![0xde, 0xad, 0xbe, 0xef]);
2118        let unsigned = peer_node("unsigned", [2u8; 32], vec![]);
2119
2120        assert!(tracker.tka_admits(&signed));
2121        assert!(tracker.tka_admits(&unsigned));
2122
2123        tracker.peer_db.upsert(&signed);
2124        tracker.peer_db.upsert(&unsigned);
2125        assert_eq!(tracker.peer_db.peers().len(), 2);
2126    }
2127
2128    #[tokio::test]
2129    async fn tka_active_rejects_unsigned_peer() {
2130        // Authority present + peer presents no signature ⇒ rejected (fail-closed), not in peer_db.
2131        let (authority, _sig) = authority_and_valid_sig();
2132        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2133
2134        let unsigned = peer_node("unsigned", NODE_KEY_BYTES, vec![]);
2135        assert!(!tracker.tka_admits(&unsigned));
2136
2137        // Mirror the handler's `if !tka_admits { continue }` loop.
2138        if tracker.tka_admits(&unsigned) {
2139            tracker.peer_db.upsert(&unsigned);
2140        }
2141        assert_eq!(tracker.peer_db.peers().len(), 0);
2142        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
2143    }
2144
2145    #[tokio::test]
2146    async fn tka_active_rejects_unsigned_peer_api_only_peer() {
2147        // `UnsignedPeerAPIOnly` buys NO admission exemption here: Go admits such a peer unsigned
2148        // under an active lock (peerAPI-only, no network access), this fork drops it like any other
2149        // unsigned peer. Pins the documented parity gap (`docs/PARITY_ROADMAP.md`, and the
2150        // `ts_control::Node::unsigned_peer_api_only` field docs) so implementing the carve-out has
2151        // to update the prose that promises no peerAPI access today.
2152        let (authority, _sig) = authority_and_valid_sig();
2153        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2154
2155        let mut peer_api_only = peer_node("peerapi-only", NODE_KEY_BYTES, vec![]);
2156        peer_api_only.unsigned_peer_api_only = true;
2157
2158        assert!(
2159            !tracker.tka_admits(&peer_api_only),
2160            "unsigned_peer_api_only must not exempt a peer from the tailnet-lock admission gate"
2161        );
2162
2163        // Mirror the handler's `if !tka_admits { continue }` loop: nothing reaches the peer db, so
2164        // the peer is not reachable for peerAPI either.
2165        if tracker.tka_admits(&peer_api_only) {
2166            tracker.peer_db.upsert(&peer_api_only);
2167        }
2168        assert_eq!(tracker.peer_db.peers().len(), 0);
2169        assert!(tracker.peer_db.get(&peer_api_only.node_key).is_none());
2170    }
2171
2172    #[tokio::test]
2173    async fn tka_empty_keyset_authority_admits_unsigned_peer_api_only_peer() {
2174        // The other side of `tka_active_rejects_unsigned_peer_api_only_peer`: "an authority is
2175        // present" is NOT on its own enough to drop an unsigned peer. The brick-guard fires first,
2176        // so an authority carrying no trusted keys enforces nothing and admits even the peer class
2177        // a keyed lock would reject. Pins the qualifier on the
2178        // `ts_control::Node::unsigned_peer_api_only` field docs — remove the guard and this fails.
2179        use ts_tka::{AumHash, Authority, State};
2180        let empty_auth = Authority::from_state(AumHash([0u8; 32]), State { keys: Vec::new() });
2181        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(empty_auth));
2182
2183        let mut peer_api_only = peer_node("peerapi-only", NODE_KEY_BYTES, vec![]);
2184        peer_api_only.unsigned_peer_api_only = true;
2185
2186        assert!(
2187            tracker.tka_admits(&peer_api_only),
2188            "an empty-keyset authority must not enforce, not even against an unsigned peer"
2189        );
2190
2191        tracker.apply_peer_update(
2192            &ts_control::PeerUpdate::Full(vec![peer_api_only.clone()]),
2193            local_now(),
2194        );
2195        assert!(
2196            tracker.peer_db.get(&peer_api_only.node_key).is_some(),
2197            "the peer reaches the peer db, so the gate's drop is keyset-conditional"
2198        );
2199    }
2200
2201    #[tokio::test]
2202    async fn tka_active_rejects_bad_signature() {
2203        // Authority present + a signature that fails to verify ⇒ rejected, not in peer_db.
2204        let (authority, mut sig) = authority_and_valid_sig();
2205        // Tamper the last byte (the trailing signature byte) so verification fails.
2206        let last = sig.len() - 1;
2207        sig[last] ^= 0xff;
2208
2209        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2210        let bad = peer_node("bad", NODE_KEY_BYTES, sig);
2211        assert!(!tracker.tka_admits(&bad));
2212
2213        if tracker.tka_admits(&bad) {
2214            tracker.peer_db.upsert(&bad);
2215        }
2216        assert_eq!(tracker.peer_db.peers().len(), 0);
2217    }
2218
2219    #[tokio::test]
2220    async fn tka_active_admits_authorized_peer() {
2221        // Authority present + correctly-signed node key ⇒ admitted and upserted.
2222        let (authority, sig) = authority_and_valid_sig();
2223        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2224
2225        let good = peer_node("good", NODE_KEY_BYTES, sig);
2226        assert!(tracker.tka_admits(&good));
2227
2228        if tracker.tka_admits(&good) {
2229            tracker.peer_db.upsert(&good);
2230        }
2231        assert_eq!(tracker.peer_db.peers().len(), 1);
2232        assert!(tracker.peer_db.get(&good.node_key).is_some());
2233    }
2234
2235    // ---------------------------------------------------------------------------------------------
2236    // Tests that drive REAL `PeerUpdate`s through the shared handler body
2237    // ([`PeerTracker::apply_peer_update`], the single source of truth the actor's netmap `handle`
2238    // also calls), so the two real upsert sites (`Full` and `Delta { upsert }`) are exercised via
2239    // the actual enforcement path — not by hand-mirroring `if !tka_admits { continue }`.
2240    // ---------------------------------------------------------------------------------------------
2241
2242    #[tokio::test]
2243    async fn tka_active_delta_upsert_rejects_unauthorized() {
2244        // Drive a real `Delta { upsert }` whose peer carries no signature. The Delta upsert site
2245        // must reject it under an active authority ⇒ not present in peer_db after the handler runs.
2246        let (authority, _sig) = authority_and_valid_sig();
2247        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2248
2249        let unsigned = peer_node("unsigned", NODE_KEY_BYTES, vec![]);
2250        let update = ts_control::PeerUpdate::Delta {
2251            upsert: vec![unsigned.clone()],
2252            remove: Vec::new(),
2253        };
2254
2255        tracker.apply_peer_update(&update, local_now());
2256
2257        assert_eq!(tracker.peer_db.peers().len(), 0);
2258        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
2259    }
2260
2261    #[tokio::test]
2262    async fn tka_active_delta_upsert_admits_authorized() {
2263        // Drive a real `Delta { upsert }` with a correctly-signed peer ⇒ present in peer_db.
2264        let (authority, sig) = authority_and_valid_sig();
2265        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2266
2267        let good = peer_node("good", NODE_KEY_BYTES, sig);
2268        let update = ts_control::PeerUpdate::Delta {
2269            upsert: vec![good.clone()],
2270            remove: Vec::new(),
2271        };
2272
2273        tracker.apply_peer_update(&update, local_now());
2274
2275        assert_eq!(tracker.peer_db.peers().len(), 1);
2276        assert!(tracker.peer_db.get(&good.node_key).is_some());
2277    }
2278
2279    #[tokio::test]
2280    async fn tka_active_full_admits_only_authorized_in_mixed_batch() {
2281        // Drive a real `Full` carrying a MIX of authorized + unauthorized peers. Only the
2282        // correctly-signed peer survives the Full upsert site; the unsigned and bad-sig peers are
2283        // dropped fail-closed.
2284        let (authority, sig) = authority_and_valid_sig();
2285        // A bad-sig variant of the same authorized signature (tamper the trailing byte).
2286        let mut bad_sig = sig.clone();
2287        let last = bad_sig.len() - 1;
2288        bad_sig[last] ^= 0xff;
2289
2290        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2291
2292        // Only the authorized peer carries NODE_KEY_BYTES (the key the authority signed); the
2293        // rejected peers use distinct node keys so the survivor is unambiguous.
2294        let good = peer_node("good", NODE_KEY_BYTES, sig);
2295        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2296        let bad = peer_node("bad", [9u8; 32], bad_sig);
2297
2298        let update =
2299            ts_control::PeerUpdate::Full(vec![good.clone(), unsigned.clone(), bad.clone()]);
2300
2301        tracker.apply_peer_update(&update, local_now());
2302
2303        assert_eq!(tracker.peer_db.peers().len(), 1);
2304        assert!(tracker.peer_db.get(&good.node_key).is_some());
2305        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
2306        assert!(tracker.peer_db.get(&bad.node_key).is_none());
2307    }
2308
2309    /// End-to-end through the REAL enforcement-authority transport (the `watch` cell the control
2310    /// runner writes), not a direct field poke: writing `Some(authority)` flips enforcement on so a
2311    /// mixed batch drops the unsigned/bad peers, and a subsequent `None` (lock disabled) clears
2312    /// enforcement so a peer DROPPED while enforced is re-admitted. Exercises the exact `borrow`-based
2313    /// read path `tka_admits` uses — a broken receiver wiring would pass every for_test-field test but
2314    /// fail here.
2315    #[tokio::test]
2316    async fn tka_authority_watch_enables_then_clears_enforcement() {
2317        let (authority, sig) = authority_and_valid_sig();
2318        let mut bad_sig = sig.clone();
2319        let last = bad_sig.len() - 1;
2320        bad_sig[last] ^= 0xff;
2321
2322        let (mut tracker, tka_tx) = PeerTracker::for_test(test_env(), None);
2323
2324        // 1) No authority yet ⇒ admit-all (Go b.tka == nil).
2325        let good = peer_node("good", NODE_KEY_BYTES, sig.clone());
2326        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2327        let bad = peer_node("bad", [9u8; 32], bad_sig);
2328        let batch = ts_control::PeerUpdate::Full(vec![good.clone(), unsigned.clone(), bad.clone()]);
2329        tracker.apply_peer_update(&batch, local_now());
2330        assert_eq!(tracker.peer_db.peers().len(), 3, "no lock ⇒ admit all");
2331
2332        // 2) Publish the verified authority over the watch cell (exactly what the control runner does
2333        //    on a successful sync) ⇒ enforcement ON. A re-applied Full now drops unsigned + bad.
2334        tka_tx.send_replace(Some(Arc::new(authority)));
2335        tracker.apply_peer_update(&batch, local_now());
2336        assert_eq!(
2337            tracker.peer_db.peers().len(),
2338            1,
2339            "lock active ⇒ only the signed peer survives"
2340        );
2341        assert!(tracker.peer_db.get(&good.node_key).is_some());
2342        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
2343        assert!(tracker.peer_db.get(&bad.node_key).is_none());
2344
2345        // 3) Lock disabled (None) ⇒ enforcement cleared ⇒ a peer that was DROPPED while enforced is
2346        //    re-admitted by a fresh netmap. Assert the specific previously-dropped key returns (not
2347        //    merely a count), so this proves the drop→clear→re-admit transition, not "admit-all-fresh".
2348        tka_tx.send_replace(None);
2349        tracker.apply_peer_update(&batch, local_now());
2350        assert_eq!(
2351            tracker.peer_db.peers().len(),
2352            3,
2353            "lock disabled ⇒ admit all again"
2354        );
2355        assert!(
2356            tracker.peer_db.get(&unsigned.node_key).is_some(),
2357            "the peer dropped under enforcement must come back once the lock is cleared"
2358        );
2359        assert!(tracker.peer_db.get(&bad.node_key).is_some());
2360    }
2361
2362    /// The ordering gap this closes. A peer admitted BEFORE the lock synced must be re-checked the
2363    /// moment the authority is installed — not left in the db until control happens to send another
2364    /// `Full`. Go never has this problem: `SetControlClientStatus` runs `tkaSyncIfNeeded` and then
2365    /// `tkaFilterNetmapLocked(st.NetMap)` on the SAME netmap in one pass, so the netmap that
2366    /// announced the lock is itself filtered. Here the sync is a spawned task, so the netmap lands
2367    /// first and `tka_reevaluate_peer_db` is what restores Go's ordering.
2368    ///
2369    /// Note this test applies NO second netmap: the eviction must come from the authority install
2370    /// alone, which is exactly what was missing before.
2371    #[tokio::test]
2372    async fn tka_authority_install_reevaluates_already_admitted_peers() {
2373        let (authority, sig) = authority_and_valid_sig();
2374        let mut bad_sig = sig.clone();
2375        let last = bad_sig.len() - 1;
2376        bad_sig[last] ^= 0xff;
2377
2378        let (mut tracker, tka_tx) = PeerTracker::for_test(test_env(), None);
2379
2380        // 1) A netmap arrives while nothing is synced ⇒ enforcement inactive ⇒ all three admitted.
2381        let good = peer_node("good", NODE_KEY_BYTES, sig);
2382        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2383        let bad = peer_node("bad", [9u8; 32], bad_sig);
2384        tracker.apply_peer_update(
2385            &ts_control::PeerUpdate::Full(vec![good.clone(), unsigned.clone(), bad.clone()]),
2386            local_now(),
2387        );
2388        assert_eq!(tracker.peer_db.peers().len(), 3, "no lock yet ⇒ admit all");
2389        let unsigned_id = tracker
2390            .peer_db
2391            .get(&unsigned.node_key)
2392            .expect("unsigned peer admitted while no lock is synced")
2393            .0;
2394        let bad_id = tracker
2395            .peer_db
2396            .get(&bad.node_key)
2397            .expect("bad-sig peer admitted while no lock is synced")
2398            .0;
2399
2400        // 2) The sync completes and the control runner installs the verified authority.
2401        tka_tx.send_replace(Some(Arc::new(authority)));
2402        let evicted = tracker.tka_reevaluate_peer_db();
2403
2404        assert_eq!(
2405            evicted,
2406            HashSet::from_iter([unsigned_id, bad_id]),
2407            "the unsigned and bad-signature peers are the ones reported evicted"
2408        );
2409        assert_eq!(tracker.peer_db.peers().len(), 1);
2410        assert!(
2411            tracker.peer_db.get(&good.node_key).is_some(),
2412            "the authorized peer stays admitted"
2413        );
2414        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
2415        assert!(tracker.peer_db.get(&bad.node_key).is_none());
2416
2417        // 3) Idempotent: a second pass over the now-clean db evicts nobody.
2418        assert!(tracker.tka_reevaluate_peer_db().is_empty());
2419    }
2420
2421    /// With no authority the re-evaluation evicts nobody — enforcement is inactive and every peer is
2422    /// admitted, exactly Go's `b.tka == nil` early return. Covers both "never synced" and "the lock
2423    /// was disabled after enforcing", the two ways the cell holds `None`.
2424    #[tokio::test]
2425    async fn tka_reevaluate_without_authority_evicts_nothing() {
2426        let (authority, sig) = authority_and_valid_sig();
2427        let (mut tracker, tka_tx) = PeerTracker::for_test(test_env(), None);
2428
2429        let good = peer_node("good", NODE_KEY_BYTES, sig);
2430        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2431        tracker.apply_peer_update(
2432            &ts_control::PeerUpdate::Full(vec![good.clone(), unsigned.clone()]),
2433            local_now(),
2434        );
2435
2436        // Never synced.
2437        assert!(tracker.tka_reevaluate_peer_db().is_empty());
2438        assert_eq!(tracker.peer_db.peers().len(), 2);
2439
2440        // Enforced, then disabled: the disable must not evict the peer the lock had authorized, and
2441        // must not start dropping the unsigned one either.
2442        tka_tx.send_replace(Some(Arc::new(authority)));
2443        assert_eq!(tracker.tka_reevaluate_peer_db().len(), 1);
2444        tka_tx.send_replace(None);
2445        assert!(tracker.tka_reevaluate_peer_db().is_empty());
2446        assert!(tracker.peer_db.get(&good.node_key).is_some());
2447    }
2448
2449    /// The re-evaluation runs the WHOLE Go `tkaFilterNetmapLocked` pass, not just the per-peer
2450    /// signature check: a peer presenting a node key that a newer rotation superseded is evicted too,
2451    /// even though its own `Direct` signature still verifies against the authority. Both peers are
2452    /// already in the db when the authority lands, so the cross-peer rotation filter has to run over
2453    /// the db contents — which is why `tka_keep_verdicts` is shared with the `Full` path rather than
2454    /// re-derived here.
2455    #[tokio::test]
2456    async fn tka_reevaluate_applies_the_cross_peer_rotation_filter() {
2457        use ed25519_dalek::SigningKey;
2458        use ts_tka::NodeKeySignature;
2459
2460        let trusted = SigningKey::from_bytes(&[42u8; 32]);
2461        let authority = Authority::from_state(
2462            AumHash([0; 32]),
2463            State {
2464                keys: vec![Key {
2465                    kind: KeyKind::Ed25519,
2466                    votes: 1,
2467                    public: trusted.verifying_key().to_bytes().to_vec(),
2468                }],
2469            },
2470        );
2471        // `stale` holds the pivot key with a valid Direct signature; `rotated` holds a key whose
2472        // rotation chain rotated the pivot key AWAY, which obsoletes `stale`.
2473        let pivot = SigningKey::from_bytes(&[9u8; 32]);
2474        let pivot_pub: [u8; 32] = pivot.verifying_key().to_bytes();
2475        let stale = peer_node(
2476            "stale",
2477            pivot_pub,
2478            NodeKeySignature::sign_direct(&pivot_pub, &trusted).serialize(),
2479        );
2480        let new_key = [4u8; 32];
2481        let rotated = peer_node(
2482            "rotated",
2483            new_key,
2484            NodeKeySignature::sign_rotation(&new_key, &trusted, &pivot).serialize(),
2485        );
2486
2487        // Both admitted while nothing is synced.
2488        let (mut tracker, tka_tx) = PeerTracker::for_test(test_env(), None);
2489        tracker.apply_peer_update(
2490            &ts_control::PeerUpdate::Full(vec![stale.clone(), rotated.clone()]),
2491            local_now(),
2492        );
2493        assert_eq!(tracker.peer_db.peers().len(), 2, "no lock yet ⇒ admit all");
2494
2495        tka_tx.send_replace(Some(Arc::new(authority)));
2496        let evicted = tracker.tka_reevaluate_peer_db();
2497
2498        assert_eq!(
2499            evicted.len(),
2500            1,
2501            "only the rotation-obsolete peer is evicted"
2502        );
2503        assert!(
2504            tracker.peer_db.get(&rotated.node_key).is_some(),
2505            "the freshly-rotated peer stays"
2506        );
2507        assert!(
2508            tracker.peer_db.get(&stale.node_key).is_none(),
2509            "the peer whose key a rotation superseded is evicted, though its own signature verifies"
2510        );
2511    }
2512
2513    /// A `StateUpdate` carrying nothing but a `Full` peer set — the netmap shape the live-actor test
2514    /// publishes on the bus.
2515    pub(crate) fn netmap_with_peers(peers: Vec<Node>) -> ts_control::StateUpdate {
2516        ts_control::StateUpdate {
2517            session_handle: None,
2518            seq: 0,
2519            keep_alive: false,
2520            derp: None,
2521            node: None,
2522            peer_update: Some(ts_control::PeerUpdate::Full(peers)),
2523            peer_patches: Vec::new(),
2524            user_profiles: Vec::new(),
2525            ping: None,
2526            packetfilter: None,
2527            cap_grants: None,
2528            pop_browser_url: None,
2529            dial_plan: None,
2530            dns_config: None,
2531            ssh_policy: None,
2532            tka: None,
2533            online_change: Default::default(),
2534            peer_seen_change: Default::default(),
2535            control_time: None,
2536        }
2537    }
2538
2539    /// Poll a live [`PeerTracker`] until it holds exactly `want` peers, bounded by a timeout so a
2540    /// broken wiring fails the test instead of hanging the suite.
2541    pub(crate) async fn await_peer_count(
2542        tracker: &ActorRef<PeerTracker>,
2543        want: usize,
2544    ) -> Vec<Node> {
2545        let settled = tokio::time::timeout(std::time::Duration::from_secs(10), async {
2546            loop {
2547                let peers = tracker.ask(AllPeers).await.expect("peer tracker is alive");
2548                if peers.len() == want {
2549                    return peers;
2550                }
2551                tokio::time::sleep(std::time::Duration::from_millis(5)).await;
2552            }
2553        })
2554        .await;
2555        settled.unwrap_or_else(|_| panic!("peer tracker never settled at {want} peer(s)"))
2556    }
2557
2558    /// End-to-end through the LIVE actor, which is the only thing that proves the wiring: the peer
2559    /// tracker watches its own enforcement cell, so the control runner's `send_replace` re-filters
2560    /// the peer db with no further netmap. If the watch task were never spawned (or the message not
2561    /// handled) the unsigned peer would stay admitted forever — a hole every `for_test` unit test
2562    /// above would still pass over, because they call the re-evaluation by hand.
2563    #[tokio::test]
2564    async fn tka_authority_change_refilters_through_the_live_actor() {
2565        use kameo::actor::Spawn as _;
2566
2567        let (authority, sig) = authority_and_valid_sig();
2568        let env = test_env();
2569        let (tka_tx, tka_rx) = watch::channel(None);
2570        let tracker = PeerTracker::spawn((env.clone(), tka_rx));
2571
2572        // Await one reply first: the actor's `on_start` (which registers it on the bus) has then
2573        // completed, so the netmap published below cannot race the subscription.
2574        assert!(
2575            tracker
2576                .ask(AllPeers)
2577                .await
2578                .expect("peer tracker started")
2579                .is_empty()
2580        );
2581
2582        let good = peer_node("good", NODE_KEY_BYTES, sig);
2583        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2584        env.publish(Arc::new(netmap_with_peers(vec![
2585            good.clone(),
2586            unsigned.clone(),
2587        ])))
2588        .await
2589        .expect("publish netmap");
2590
2591        // No lock synced ⇒ both peers land.
2592        await_peer_count(&tracker, 2).await;
2593
2594        // The control runner installs the verified authority. No netmap follows.
2595        tka_tx.send_replace(Some(Arc::new(authority)));
2596
2597        let peers = await_peer_count(&tracker, 1).await;
2598        assert_eq!(
2599            peers[0].stable_id, good.stable_id,
2600            "only the authorized peer survives the authority install"
2601        );
2602    }
2603
2604    /// Degenerate input: two DISTINCT nodes sharing one `stable_id` in a single `Full`, one with a
2605    /// valid signature and one unsigned, under an active lock. Each node is judged by its OWN verdict
2606    /// (the per-node `admits` vector), so the unsigned node is never admitted on the strength of its
2607    /// signed twin. The single-verify `Full` refactor keeps this per-node semantics (a stable_id-set
2608    /// alone would have admitted whichever node was upserted last). Malformed control input; asserted
2609    /// only to lock the verdict-per-node behavior against regression.
2610    #[tokio::test]
2611    async fn tka_full_duplicate_stable_id_judges_each_node_on_its_own_signature() {
2612        let (authority, sig) = authority_and_valid_sig();
2613        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2614
2615        // Both carry stable_id "dup"; the signed one authorizes NODE_KEY_BYTES, the other is unsigned
2616        // and uses a different node key. Order them unsigned-last so a last-writer-wins stable_id set
2617        // would (wrongly) leave the unsigned node's key in the db.
2618        let signed = peer_node("dup", NODE_KEY_BYTES, sig);
2619        let unsigned = peer_node("dup", [8u8; 32], vec![]);
2620        tracker.apply_peer_update(
2621            &ts_control::PeerUpdate::Full(vec![signed.clone(), unsigned.clone()]),
2622            local_now(),
2623        );
2624
2625        // The unsigned node's own verdict failed, so its key must NOT be present, regardless of the
2626        // shared stable_id. (The signed twin retained the stable_id; the db holds the signed key.)
2627        assert!(
2628            tracker.peer_db.get(&unsigned.node_key).is_none(),
2629            "a node whose own signature fails must not be admitted via a stable_id twin"
2630        );
2631        assert!(tracker.peer_db.get(&signed.node_key).is_some());
2632    }
2633
2634    /// Full-path consistency under two KEPT nodes sharing a `stable_id`: `peer_db.upsert` is
2635    /// last-writer-wins on `stable_id`, so the db ends holding exactly one node for that id (the last
2636    /// kept), and `retain` never evicts that just-upserted id (`retained_ids` contains the shared id
2637    /// because at least one of its nodes was kept). No lock here, so both nodes are "kept". This pins
2638    /// the published-state invariant the whole-surface audit flagged: `retain` and the upsert loop
2639    /// agree on the surviving stable_id. Malformed control input; asserted for robustness.
2640    #[tokio::test]
2641    async fn tka_full_duplicate_stable_id_both_kept_is_consistent() {
2642        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
2643        let first = peer_node("dup", [1u8; 32], vec![]);
2644        let last = peer_node("dup", [2u8; 32], vec![]);
2645        tracker.apply_peer_update(
2646            &ts_control::PeerUpdate::Full(vec![first.clone(), last.clone()]),
2647            local_now(),
2648        );
2649
2650        // Exactly one db entry for the shared stable_id, holding the LAST node (upsert is
2651        // last-writer-wins on stable_id); the first node's key was transparently superseded.
2652        assert_eq!(
2653            tracker.peer_db.peers().len(),
2654            1,
2655            "one entry for the shared stable_id"
2656        );
2657        assert!(
2658            tracker.peer_db.get(&last.node_key).is_some(),
2659            "the db holds the last-upserted node for the shared id"
2660        );
2661        assert!(
2662            tracker.peer_db.get(&first.node_key).is_none(),
2663            "the first node's key was superseded by the last at the shared id"
2664        );
2665    }
2666
2667    /// A peer admitted in one `Full`, then in a later `Full` presenting a key that a co-resident
2668    /// peer's rotation chain has rotated away, is EVICTED — the cross-peer rotation filter applies on
2669    /// every resync, not only at first admission. Exercises the rotation filter through two
2670    /// sequential `Full` updates with real signing.
2671    #[tokio::test]
2672    async fn tka_full_rotation_obsolete_evicts_on_resync() {
2673        use ed25519_dalek::SigningKey;
2674        use ts_tka::NodeKeySignature;
2675
2676        let trusted = SigningKey::from_bytes(&[42u8; 32]);
2677        let trusted_pub = trusted.verifying_key().to_bytes().to_vec();
2678        let authority = Authority::from_state(
2679            AumHash([0; 32]),
2680            State {
2681                keys: vec![Key {
2682                    kind: KeyKind::Ed25519,
2683                    votes: 1,
2684                    public: trusted_pub.clone(),
2685                }],
2686            },
2687        );
2688        let pivot = SigningKey::from_bytes(&[9u8; 32]);
2689        let pivot_pub: [u8; 32] = pivot.verifying_key().to_bytes();
2690
2691        // First Full: the soon-to-be-stale peer presents the pivot key with a valid Direct sig.
2692        let stale_sig = NodeKeySignature::sign_direct(&pivot_pub, &trusted).serialize();
2693        let stale_peer = peer_node("stale", pivot_pub, stale_sig);
2694        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2695        tracker.apply_peer_update(
2696            &ts_control::PeerUpdate::Full(vec![stale_peer.clone()]),
2697            local_now(),
2698        );
2699        assert!(
2700            tracker.peer_db.get(&stale_peer.node_key).is_some(),
2701            "the stale peer is admitted while no rotation has superseded it yet"
2702        );
2703
2704        // Second Full: a freshly-rotated peer (whose chain rotated AWAY the pivot key) joins, and the
2705        // stale peer is re-included. The rotation filter now obsoletes the pivot key ⇒ stale evicted.
2706        let new_key = [4u8; 32];
2707        let new_sig = NodeKeySignature::sign_rotation(&new_key, &trusted, &pivot).serialize();
2708        let new_peer = peer_node("rotated", new_key, new_sig);
2709        tracker.apply_peer_update(
2710            &ts_control::PeerUpdate::Full(vec![new_peer.clone(), stale_peer.clone()]),
2711            local_now(),
2712        );
2713        assert!(
2714            tracker.peer_db.get(&new_peer.node_key).is_some(),
2715            "the freshly-rotated peer is admitted"
2716        );
2717        assert!(
2718            tracker.peer_db.get(&stale_peer.node_key).is_none(),
2719            "the stale peer is EVICTED on the resync once a rotation supersedes its key"
2720        );
2721    }
2722
2723    /// The empty-trusted-key-state brick-guard: an authority with no keys must NOT drop the whole
2724    /// netmap (a `ts_tka` invariant violation / replayer edge). A verified chain always carries ≥1
2725    /// key, so this never weakens a genuine lock — it only prevents a black-hole. Uses ≥2 peers
2726    /// (one signed, one unsigned) to prove it admits **all**, not accidentally just one.
2727    #[tokio::test]
2728    async fn tka_empty_keyset_authority_admits_all() {
2729        use ts_tka::{AumHash, Authority, State};
2730        let empty_auth = Authority::from_state(AumHash([0u8; 32]), State { keys: Vec::new() });
2731        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(empty_auth));
2732        let signed = peer_node("signed", [7u8; 32], vec![0xde, 0xad]);
2733        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2734        tracker.apply_peer_update(
2735            &ts_control::PeerUpdate::Full(vec![signed.clone(), unsigned.clone()]),
2736            local_now(),
2737        );
2738        assert_eq!(
2739            tracker.peer_db.peers().len(),
2740            2,
2741            "an empty-keyset authority must admit ALL peers (brick-guard), not enforce"
2742        );
2743    }
2744
2745    /// Signature-replay / `NodeKeyMismatch`: a structurally-valid signature that authorizes
2746    /// `NODE_KEY_BYTES` must NOT admit a DIFFERENT node key carrying that same signature blob. This is
2747    /// the highest-value bypass — if the sig↔node-key binding in `verify_signature` were dropped, this
2748    /// is the only test that would catch it (the other "bad" peers only flip a byte ⇒ `BadSignature`).
2749    #[tokio::test]
2750    async fn tka_active_rejects_valid_sig_for_wrong_node_key() {
2751        let (authority, sig) = authority_and_valid_sig();
2752        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2753
2754        // The signature authorizes NODE_KEY_BYTES; attach it to an imposter with a different key.
2755        let imposter = peer_node("imposter", [0x55u8; 32], sig);
2756        assert!(
2757            !tracker.tka_admits(&imposter),
2758            "a signature bound to one node key must not authorize a different node key"
2759        );
2760        tracker.apply_peer_update(
2761            &ts_control::PeerUpdate::Full(vec![imposter.clone()]),
2762            local_now(),
2763        );
2764        assert!(tracker.peer_db.get(&imposter.node_key).is_none());
2765    }
2766
2767    /// `UntrustedKey`: a signature produced by a well-formed Ed25519 key that is NOT in the
2768    /// authority's trusted-key state must be rejected — distinct from a tampered-byte `BadSignature`.
2769    #[tokio::test]
2770    async fn tka_active_rejects_sig_from_untrusted_key() {
2771        use ed25519_dalek::{Signer, SigningKey};
2772        let (authority, _sig) = authority_and_valid_sig();
2773        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2774
2775        // Sign a valid CBOR with a DIFFERENT key (not the one the authority trusts). The key_id in
2776        // the signature names this untrusted key, so `get_key` misses ⇒ UntrustedKey.
2777        let rogue = SigningKey::from_bytes(&[99u8; 32]);
2778        let rogue_pub = rogue.verifying_key().to_bytes().to_vec();
2779        let preimage = direct_sig_cbor(&NODE_KEY_BYTES, &rogue_pub, None);
2780        let sig_hash = ts_tka::aum_hash(&preimage).0;
2781        let signature = rogue.sign(&sig_hash).to_bytes().to_vec();
2782        let rogue_cbor = direct_sig_cbor(&NODE_KEY_BYTES, &rogue_pub, Some(&signature));
2783
2784        let peer = peer_node("rogue-signed", NODE_KEY_BYTES, rogue_cbor);
2785        assert!(
2786            !tracker.tka_admits(&peer),
2787            "a signature from a key outside the trusted set must be rejected"
2788        );
2789        // Drive the real upsert path too (match the sibling replay test's depth): an untrusted-key
2790        // signature must keep the peer out of the db, not merely fail the verdict in isolation.
2791        tracker.apply_peer_update(
2792            &ts_control::PeerUpdate::Full(vec![peer.clone()]),
2793            local_now(),
2794        );
2795        assert!(tracker.peer_db.get(&peer.node_key).is_none());
2796    }
2797
2798    /// Bus-enable analogue for `Delta`: enforcement engaged via the watch cell must also gate a
2799    /// `Delta { upsert }` (not only `Full`). Closes the "authority arrived over the transport AND the
2800    /// next update is a Delta" combination.
2801    #[tokio::test]
2802    async fn tka_watch_enable_enforces_delta_upsert() {
2803        let (authority, sig) = authority_and_valid_sig();
2804        let (mut tracker, tka_tx) = PeerTracker::for_test(test_env(), None);
2805        tka_tx.send_replace(Some(Arc::new(authority)));
2806
2807        let good = peer_node("good", NODE_KEY_BYTES, sig);
2808        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
2809        tracker.apply_peer_update(
2810            &ts_control::PeerUpdate::Delta {
2811                remove: vec![],
2812                upsert: vec![good.clone(), unsigned.clone()],
2813            },
2814            local_now(),
2815        );
2816        assert!(tracker.peer_db.get(&good.node_key).is_some());
2817        assert!(
2818            tracker.peer_db.get(&unsigned.node_key).is_none(),
2819            "delta upsert under an active lock must drop the unsigned peer"
2820        );
2821    }
2822
2823    /// A `Delta` re-upsert of an ALREADY-ADMITTED peer whose signature is now invalid must EVICT the
2824    /// stale entry (revocation-via-delta), not leave it admitted. Go re-filters the whole netmap each
2825    /// response, so a now-unsigned peer would not survive there either.
2826    #[tokio::test]
2827    async fn tka_delta_reupsert_with_invalid_sig_evicts_existing() {
2828        let (authority, sig) = authority_and_valid_sig();
2829        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2830
2831        // Admit the signed peer.
2832        let good = peer_node("good", NODE_KEY_BYTES, sig.clone());
2833        tracker.apply_peer_update(
2834            &ts_control::PeerUpdate::Full(vec![good.clone()]),
2835            local_now(),
2836        );
2837        assert!(tracker.peer_db.get(&good.node_key).is_some());
2838
2839        // Re-upsert the SAME stable_id (now with no signature) via a delta ⇒ evicted, not retained.
2840        let revoked = peer_node("good", NODE_KEY_BYTES, vec![]);
2841        tracker.apply_peer_update(
2842            &ts_control::PeerUpdate::Delta {
2843                remove: vec![],
2844                upsert: vec![revoked],
2845            },
2846            local_now(),
2847        );
2848        assert!(
2849            tracker.peer_db.get(&good.node_key).is_none(),
2850            "a delta re-upsert that fails the lock must evict the previously-admitted peer"
2851        );
2852    }
2853
2854    #[tokio::test]
2855    async fn tka_full_resync_revocation_behavior() {
2856        // Revocation-on-resync: admit a peer, then re-include the SAME stable_id in a `Full` with a
2857        // now-invalid signature. Per the Logic review finding, the pre-fix `retain` kept the stale
2858        // (previously-admitted) entry because membership was decided purely by stable_id.
2859        //
2860        // FIXED (not merely documented): the `Full` `retain` now keys on `tka_admits`-passing
2861        // stable_ids, so a peer whose re-included signature no longer verifies under the active
2862        // authority is EVICTED. This test asserts eviction. The inactive (authority=None) path is
2863        // provably unchanged — `tka_admits` always returns `true` there, so the retained set equals
2864        // the set of re-included stable_ids exactly (see `tka_inactive_full_resync_keeps_*`).
2865        let (authority, sig) = authority_and_valid_sig();
2866        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
2867
2868        // 1) Admit the peer with a valid signature via a real `Full`.
2869        let good = peer_node("revoked", NODE_KEY_BYTES, sig.clone());
2870        tracker.apply_peer_update(
2871            &ts_control::PeerUpdate::Full(vec![good.clone()]),
2872            local_now(),
2873        );
2874        assert_eq!(tracker.peer_db.peers().len(), 1);
2875        assert!(tracker.peer_db.get(&good.node_key).is_some());
2876
2877        // 2) Re-sync the SAME stable_id, but with a now-invalid signature (tamper trailing byte).
2878        let mut bad_sig = sig;
2879        let last = bad_sig.len() - 1;
2880        bad_sig[last] ^= 0xff;
2881        let revoked = peer_node("revoked", NODE_KEY_BYTES, bad_sig);
2882        tracker.apply_peer_update(
2883            &ts_control::PeerUpdate::Full(vec![revoked.clone()]),
2884            local_now(),
2885        );
2886
2887        // Eviction: the stale entry is dropped because its re-included signature fails the gate.
2888        assert_eq!(tracker.peer_db.peers().len(), 0);
2889        assert!(tracker.peer_db.get(&revoked.node_key).is_none());
2890    }
2891
2892    #[tokio::test]
2893    async fn tka_inactive_full_resync_keeps_reincluded_peer() {
2894        // Guard the inactive (authority=None) path against the revocation fix: with no authority,
2895        // a peer re-included in a `Full` survives regardless of its signature bytes — byte-for-byte
2896        // pre-TKA behavior, proving the `Full` `retain` change does not regress the always-taken
2897        // branch this wave.
2898        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
2899
2900        let peer = peer_node("p", NODE_KEY_BYTES, vec![0xde, 0xad]);
2901        tracker.apply_peer_update(
2902            &ts_control::PeerUpdate::Full(vec![peer.clone()]),
2903            local_now(),
2904        );
2905        assert_eq!(tracker.peer_db.peers().len(), 1);
2906
2907        // Re-sync the same stable_id with garbage signature bytes; inactive enforcement keeps it.
2908        let resynced = peer_node("p", NODE_KEY_BYTES, vec![0x00]);
2909        tracker.apply_peer_update(
2910            &ts_control::PeerUpdate::Full(vec![resynced.clone()]),
2911            local_now(),
2912        );
2913        assert_eq!(tracker.peer_db.peers().len(), 1);
2914        assert!(tracker.peer_db.get(&resynced.node_key).is_some());
2915    }
2916
2917    /// A `Patch` for a peer already in the netmap merges only the fields it carries — here new UDP
2918    /// endpoints and a new home DERP — leaving the rest of the node intact. This is the fix for
2919    /// dropped `peers_changed_patch`: without it the netmap keeps stale endpoints and the peer can
2920    /// never re-handshake after it moves.
2921    #[tokio::test]
2922    async fn patch_merges_endpoints_and_derp_into_existing_peer() {
2923        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
2924
2925        // Seed a peer (id == 1, per `peer_node`) with no endpoints / no DERP.
2926        let peer = peer_node("mover", [1u8; 32], vec![]);
2927        tracker.apply_peer_update(
2928            &ts_control::PeerUpdate::Full(vec![peer.clone()]),
2929            local_now(),
2930        );
2931        let (_pid, before) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
2932        assert!(before.underlay_addresses.is_empty());
2933        assert!(before.derp_region.is_none());
2934
2935        // Patch in fresh reachability (the idle-peer-reconnect case).
2936        let new_ep: std::net::SocketAddr = "203.0.113.7:41641".parse().unwrap();
2937        let patch = ts_control::PeerChange {
2938            id: 1,
2939            derp_region: Some(ts_derp::RegionId(core::num::NonZeroU32::new(5).unwrap())),
2940            cap: None,
2941            cap_map: None,
2942            underlay_addresses: Some(vec![new_ep]),
2943            node_key: None,
2944            key_signature: None,
2945            disco_key: None,
2946            node_key_expiry: None,
2947            online: None,
2948            last_seen: None,
2949        };
2950        let (upserts, deletions) =
2951            tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
2952
2953        assert_eq!(upserts.len(), 1);
2954        assert_eq!(deletions.len(), 0);
2955        // Same peer, now carrying the patched endpoint + DERP; node key untouched.
2956        assert_eq!(tracker.peer_db.peers().len(), 1);
2957        let (_pid, after) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
2958        assert_eq!(after.underlay_addresses, vec![new_ep]);
2959        assert_eq!(
2960            after.derp_region,
2961            Some(ts_derp::RegionId(core::num::NonZeroU32::new(5).unwrap()))
2962        );
2963        assert_eq!(after.node_key, peer.node_key);
2964    }
2965
2966    /// Regression for `tsr-5u0`: when a whole-node set (`Delta`/`Full`) and a patch co-occur in one
2967    /// response, the patch is applied *on top of* the node the set just upserted — mirroring the
2968    /// handler's apply-order (peer set first, then `peer_patches`). Before the fix the patch shared
2969    /// the single `peer_update` slot and the co-occurring set silently dropped it, so a peer brought
2970    /// in by the delta kept stale (empty) reachability.
2971    #[tokio::test]
2972    async fn patch_applies_on_top_of_co_occurring_delta() {
2973        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
2974
2975        // The whole-node delta upserts a brand-new peer (id == 1) with no reachability.
2976        let peer = peer_node("mover", [1u8; 32], vec![]);
2977        let (set_upserts, _) = tracker.apply_peer_update(
2978            &ts_control::PeerUpdate::Delta {
2979                upsert: vec![peer.clone()],
2980                remove: vec![],
2981            },
2982            local_now(),
2983        );
2984        assert_eq!(set_upserts.len(), 1, "delta upserts the new peer");
2985
2986        // The patch from the SAME response then sets that peer's endpoints + DERP. This is exactly
2987        // the consumer order the handler runs (apply_peer_update then apply_peer_patches).
2988        let new_ep: std::net::SocketAddr = "203.0.113.7:41641".parse().unwrap();
2989        let patch = ts_control::PeerChange {
2990            id: 1,
2991            derp_region: Some(ts_derp::RegionId(core::num::NonZeroU32::new(7).unwrap())),
2992            cap: None,
2993            cap_map: None,
2994            underlay_addresses: Some(vec![new_ep]),
2995            node_key: None,
2996            key_signature: None,
2997            disco_key: None,
2998            node_key_expiry: None,
2999            online: None,
3000            last_seen: None,
3001        };
3002        let (patch_upserts, patch_deletions) =
3003            tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3004
3005        assert_eq!(
3006            patch_upserts.len(),
3007            1,
3008            "patch re-upserts the just-added peer"
3009        );
3010        assert_eq!(patch_deletions.len(), 0);
3011        // The peer added by the delta now carries the patched reachability — the patch was NOT lost.
3012        let (_pid, after) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3013        assert_eq!(after.underlay_addresses, vec![new_ep]);
3014        assert_eq!(
3015            after.derp_region,
3016            Some(ts_derp::RegionId(core::num::NonZeroU32::new(7).unwrap()))
3017        );
3018    }
3019
3020    /// A `Patch` whose node id is not in the current netmap is ignored (the wire contract: a patch
3021    /// never creates a node). No upsert, no deletion, peer set unchanged.
3022    #[tokio::test]
3023    async fn patch_for_unknown_node_is_ignored() {
3024        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3025        let known = peer_node("known", [1u8; 32], vec![]); // id == 1
3026        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![known]), local_now());
3027
3028        let patch = ts_control::PeerChange {
3029            id: 999, // not in the netmap
3030            derp_region: None,
3031            cap: None,
3032            cap_map: None,
3033            underlay_addresses: Some(vec!["198.51.100.9:1".parse().unwrap()]),
3034            node_key: None,
3035            key_signature: None,
3036            disco_key: None,
3037            node_key_expiry: None,
3038            online: None,
3039            last_seen: None,
3040        };
3041        let (upserts, deletions) =
3042            tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3043
3044        assert_eq!(upserts.len(), 0);
3045        assert_eq!(deletions.len(), 0);
3046        assert_eq!(tracker.peer_db.peers().len(), 1);
3047        assert!(tracker.peer_db.get(&(999 as ts_control::NodeId)).is_none());
3048    }
3049
3050    /// An expiry-only `Patch` updates `node_key_expiry` on the matching peer (Go
3051    /// `PeerChange.KeyExpiry`), rather than being silently dropped until the next full resync.
3052    #[tokio::test]
3053    async fn patch_updates_node_key_expiry() {
3054        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3055        let peer = peer_node("expiring", [1u8; 32], vec![]); // id == 1, node_key_expiry: None
3056        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer]), local_now());
3057
3058        let expiry = "2027-01-01T00:00:00Z"
3059            .parse::<chrono::DateTime<chrono::Utc>>()
3060            .unwrap();
3061        let patch = ts_control::PeerChange {
3062            id: 1,
3063            derp_region: None,
3064            cap: None,
3065            cap_map: None,
3066            underlay_addresses: None,
3067            node_key: None,
3068            key_signature: None,
3069            disco_key: None,
3070            node_key_expiry: Some(expiry),
3071            online: None,
3072            last_seen: None,
3073        };
3074        tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3075
3076        let (_pid, after) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3077        assert_eq!(after.node_key_expiry, Some(expiry));
3078    }
3079
3080    /// Channel B: a `PeerChange.online` patch flips a peer's online state without a full node.
3081    #[tokio::test]
3082    async fn patch_updates_online() {
3083        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3084        let peer = peer_node("p", [1u8; 32], vec![]); // id == 1, online: None
3085        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer]), local_now());
3086        assert_eq!(
3087            tracker
3088                .peer_db
3089                .get(&(1 as ts_control::NodeId))
3090                .unwrap()
3091                .1
3092                .online,
3093            None
3094        );
3095
3096        let mut patch = ts_control::PeerChange {
3097            id: 1,
3098            derp_region: None,
3099            cap: None,
3100            cap_map: None,
3101            underlay_addresses: None,
3102            node_key: None,
3103            key_signature: None,
3104            disco_key: None,
3105            node_key_expiry: None,
3106            online: Some(true),
3107            last_seen: None,
3108        };
3109        tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3110        assert_eq!(
3111            tracker
3112                .peer_db
3113                .get(&(1 as ts_control::NodeId))
3114                .unwrap()
3115                .1
3116                .online,
3117            Some(true),
3118            "PeerChange.online=Some(true) marks the peer online"
3119        );
3120
3121        // A subsequent patch flips it offline.
3122        patch.online = Some(false);
3123        tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3124        assert_eq!(
3125            tracker
3126                .peer_db
3127                .get(&(1 as ts_control::NodeId))
3128                .unwrap()
3129                .1
3130                .online,
3131            Some(false)
3132        );
3133    }
3134
3135    /// Channel C/D (Go `map.go:updatePeersStateFromResponse`): `online_change` is the sole driver of
3136    /// `online`; `peer_seen_change` is the sole driver of `last_seen` (true ⇒ now, false ⇒ cleared)
3137    /// and must NEVER touch `online`. Both apply to a peer already in the netmap and ignore unknown
3138    /// ids. This pins the fix for the prior bug where channel D wrote `online=false` (conflating
3139    /// "not seen recently" with "offline" — distinct signals in Go).
3140    #[tokio::test]
3141    async fn liveness_change_maps_apply_online() {
3142        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3143        let peer = peer_node("p", [1u8; 32], vec![]); // id == 1
3144        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer]), local_now());
3145        // A fixed timestamp (chrono is built without its `clock` feature, so no `Utc::now()`).
3146        let now = chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap();
3147
3148        // Channel C: online_change sets online=true.
3149        let mut online_change = std::collections::BTreeMap::new();
3150        online_change.insert(1 as ts_control::NodeId, true);
3151        online_change.insert(999 as ts_control::NodeId, true); // unknown id — ignored
3152        let changed = tracker.apply_liveness_changes(&online_change, &Default::default(), now);
3153        assert!(changed);
3154        assert_eq!(
3155            tracker
3156                .peer_db
3157                .get(&(1 as ts_control::NodeId))
3158                .unwrap()
3159                .1
3160                .online,
3161            Some(true)
3162        );
3163
3164        // Channel D: peer_seen_change=true sets last_seen=now and leaves online UNTOUCHED.
3165        let mut seen_true = std::collections::BTreeMap::new();
3166        seen_true.insert(1 as ts_control::NodeId, true);
3167        let changed = tracker.apply_liveness_changes(&Default::default(), &seen_true, now);
3168        assert!(changed);
3169        {
3170            let (_id, node) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3171            assert_eq!(
3172                node.last_seen,
3173                Some(now),
3174                "peer_seen_change=true sets last_seen=now"
3175            );
3176            assert_eq!(
3177                node.online,
3178                Some(true),
3179                "channel D must NOT touch online (still true from channel C)"
3180            );
3181        }
3182
3183        // Channel D: peer_seen_change=false clears last_seen, still leaving online untouched.
3184        let mut seen_false = std::collections::BTreeMap::new();
3185        seen_false.insert(1 as ts_control::NodeId, false);
3186        let changed = tracker.apply_liveness_changes(&Default::default(), &seen_false, now);
3187        assert!(changed);
3188        {
3189            let (_id, node) = tracker.peer_db.get(&(1 as ts_control::NodeId)).unwrap();
3190            assert_eq!(
3191                node.last_seen, None,
3192                "peer_seen_change=false clears last_seen"
3193            );
3194            assert_eq!(node.online, Some(true), "channel D must NOT mark offline");
3195        }
3196        assert_eq!(
3197            tracker.peer_db.peers().len(),
3198            1,
3199            "the node is retained, not removed"
3200        );
3201
3202        // No-op when nothing matches / changes.
3203        assert!(!tracker.apply_liveness_changes(&Default::default(), &Default::default(), now));
3204    }
3205
3206    /// Security: a `Patch` that rotates the node key must re-satisfy the tailnet-lock authority,
3207    /// exactly like a `Delta` upsert. A key-rotation patch whose new signature does NOT verify
3208    /// evicts the peer (fail-closed) rather than leaving a now-unverified entry — closing what would
3209    /// otherwise be a trust-enforcement bypass via the patch path.
3210    #[tokio::test]
3211    async fn patch_key_rotation_failing_tka_evicts_peer() {
3212        let (authority, sig) = authority_and_valid_sig();
3213        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
3214
3215        // Admit a correctly-signed peer (id == 1).
3216        let good = peer_node("rotator", NODE_KEY_BYTES, sig.clone());
3217        tracker.apply_peer_update(
3218            &ts_control::PeerUpdate::Full(vec![good.clone()]),
3219            local_now(),
3220        );
3221        assert_eq!(tracker.peer_db.peers().len(), 1);
3222
3223        // Patch a new node key whose signature is garbage under the active authority.
3224        let patch = ts_control::PeerChange {
3225            id: 1,
3226            derp_region: None,
3227            cap: None,
3228            cap_map: None,
3229            underlay_addresses: None,
3230            node_key: Some([0x33u8; 32].into()),
3231            key_signature: Some(vec![0x00, 0x01, 0x02]),
3232            disco_key: None,
3233            node_key_expiry: None,
3234            online: None,
3235            last_seen: None,
3236        };
3237        let (upserts, deletions) =
3238            tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3239
3240        assert_eq!(upserts.len(), 0);
3241        assert_eq!(deletions.len(), 1);
3242        assert_eq!(tracker.peer_db.peers().len(), 0);
3243    }
3244
3245    /// A node's `user_id` joins against the accumulated UserProfiles table to resolve the owning
3246    /// user's profile in `WhoIs.user_profile`. With no matching profile, it is `None` (the
3247    /// pre-existing behavior); once a profile arrives, the same node resolves to it. This
3248    /// proves the accumulate-then-join path the netmap handler builds.
3249    fn profile(id: ts_control::UserId, login: &str) -> ts_control::UserProfile {
3250        ts_control::UserProfile {
3251            id,
3252            login_name: login.to_string(),
3253            display_name: None,
3254            groups: Vec::new(),
3255        }
3256    }
3257
3258    #[tokio::test]
3259    async fn whois_resolves_user_from_accumulated_profiles() {
3260        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3261
3262        // A peer owned by user id 42 at 100.64.0.1 (the peer_node fixture's address).
3263        let mut peer = peer_node("p", NODE_KEY_BYTES, Vec::new());
3264        peer.user_id = 42;
3265        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer]), local_now());
3266        let addr = "100.64.0.1:0".parse().unwrap();
3267
3268        // No profile yet: the node resolves but its owner is unknown.
3269        let who = tracker.whois_opt(addr).expect("peer is known");
3270        assert_eq!(who.user_profile, None);
3271        assert_eq!(who.user(), None);
3272
3273        // Profile for a DIFFERENT user must not match.
3274        tracker
3275            .user_profiles
3276            .insert(7, profile(7, "someone-else@example.com"));
3277        assert_eq!(tracker.whois_opt(addr).unwrap().user(), None);
3278
3279        // The owning user's profile arrives (as the netmap handler would accumulate it): now the
3280        // login resolves.
3281        tracker
3282            .user_profiles
3283            .insert(42, profile(42, "alice@example.com"));
3284        assert_eq!(
3285            tracker.whois_opt(addr).unwrap().user(),
3286            Some("alice@example.com".to_string())
3287        );
3288    }
3289
3290    /// The whole carry, end to end: a real `MapResponse` body — the JSON control writes on the map
3291    /// poll — decoded by the production wire types and the production `From` impls, accumulated by
3292    /// the production profile merge, and read back out of `whois`.
3293    ///
3294    /// `Groups` is why `WhoIs` carries the profile rather than one display label: it is the only
3295    /// attribute of an owning user that a node cannot re-derive from anything else in the netmap,
3296    /// so an embedder authorising an inbound connection on group membership has no other source
3297    /// for it. Every hop here is production code; the only thing the test assembles is the
3298    /// `StateUpdate` struct itself (`ts_control`'s frame decode is not public, and its own tests
3299    /// pin the body-to-`StateUpdate` half).
3300    fn state_update_from_body(body: &str) -> ts_control::StateUpdate {
3301        let wire: ts_control_serde::MapResponse<'_> =
3302            serde_json::from_str(body).expect("a real MapResponse body decodes");
3303        let peers = wire
3304            .peers
3305            .as_ref()
3306            .expect("the fixture carries a full peer set")
3307            .iter()
3308            .map(ts_control::Node::from)
3309            .collect();
3310        ts_control::StateUpdate {
3311            user_profiles: wire
3312                .user_profiles
3313                .iter()
3314                .map(ts_control::UserProfile::from)
3315                .collect(),
3316            ..netmap_with_peers(peers)
3317        }
3318    }
3319
3320    /// The body control sends for a tailnet with one peer owned by user 42, whose profile carries
3321    /// `Groups`. `groups` is spliced in so the present and absent cases share one fixture.
3322    fn netmap_body_with_profile_groups(groups: &str) -> String {
3323        format!(
3324            r#"{{
3325                "MapSessionHandle": "sess-1",
3326                "Seq": 9,
3327                "Peers": [{{
3328                    "ID": 2,
3329                    "StableID": "peer-2",
3330                    "Name": "peer.example.ts.net.",
3331                    "Addresses": ["100.64.0.1/32", "fd7a:115c:a1e0::1/128"],
3332                    "User": 42
3333                }}],
3334                "UserProfiles": [{{
3335                    "ID": 42,
3336                    "LoginName": "alice@example.com",
3337                    "DisplayName": "Alice Smith"{groups}
3338                }}]
3339            }}"#
3340        )
3341    }
3342
3343    #[tokio::test]
3344    async fn whois_carries_user_groups_from_a_real_map_response() {
3345        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3346        let update = state_update_from_body(&netmap_body_with_profile_groups(
3347            r#", "Groups": ["engineering@example.com", "group:eng"]"#,
3348        ));
3349
3350        tracker.accumulate_user_profiles(&update.user_profiles);
3351        tracker.apply_peer_update(
3352            update.peer_update.as_ref().expect("a full peer set"),
3353            local_now(),
3354        );
3355
3356        let who = tracker
3357            .whois_opt("100.64.0.1:0".parse().unwrap())
3358            .expect("the peer owns that address");
3359
3360        let profile = who.user_profile.as_ref().expect("user 42's profile");
3361        assert_eq!(profile.id, 42);
3362        assert_eq!(profile.login_name, "alice@example.com");
3363        assert_eq!(profile.display_name.as_deref(), Some("Alice Smith"));
3364        assert_eq!(
3365            who.user_groups(),
3366            ["engineering@example.com", "group:eng"],
3367            "the groups control reported reach the embedder in the order control sent them"
3368        );
3369        // The flattened label the pre-widening `WhoIs.user` field carried is unchanged.
3370        assert_eq!(who.user(), Some("alice@example.com".to_string()));
3371    }
3372
3373    /// The absent case, which is what every control server that does not send the field looks
3374    /// like: no `Groups` key at all. That must yield a profile with an EMPTY group list — never a
3375    /// missing profile, and never a failed decode that would drop the owner identity entirely.
3376    #[tokio::test]
3377    async fn a_map_response_without_groups_yields_an_empty_group_list() {
3378        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3379        let update = state_update_from_body(&netmap_body_with_profile_groups(""));
3380
3381        tracker.accumulate_user_profiles(&update.user_profiles);
3382        tracker.apply_peer_update(
3383            update.peer_update.as_ref().expect("a full peer set"),
3384            local_now(),
3385        );
3386
3387        let who = tracker
3388            .whois_opt("100.64.0.1:0".parse().unwrap())
3389            .expect("the peer owns that address");
3390
3391        assert!(
3392            who.user_profile.is_some(),
3393            "an omitted Groups must not cost us the profile"
3394        );
3395        assert_eq!(who.user(), Some("alice@example.com".to_string()));
3396        assert!(who.user_groups().is_empty());
3397    }
3398
3399    /// Control sends profiles incrementally, so a later response restating user 42 replaces the
3400    /// held copy wholesale — including a group list that SHRANK. A membership control has revoked
3401    /// must stop being reported, or an embedder authorising on it keeps honouring it forever.
3402    #[tokio::test]
3403    async fn a_restated_profile_replaces_the_held_group_list() {
3404        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3405        let first = state_update_from_body(&netmap_body_with_profile_groups(
3406            r#", "Groups": ["group:eng", "group:oncall"]"#,
3407        ));
3408        tracker.accumulate_user_profiles(&first.user_profiles);
3409        tracker.apply_peer_update(
3410            first.peer_update.as_ref().expect("a full peer set"),
3411            local_now(),
3412        );
3413
3414        let second = state_update_from_body(&netmap_body_with_profile_groups(
3415            r#", "Groups": ["group:eng"]"#,
3416        ));
3417        tracker.accumulate_user_profiles(&second.user_profiles);
3418
3419        let who = tracker
3420            .whois_opt("100.64.0.1:0".parse().unwrap())
3421            .expect("the peer owns that address");
3422        assert_eq!(who.user_groups(), ["group:eng"]);
3423    }
3424
3425    /// `UserProfile::best_label` prefers the login name, falling back to display name, else `None`.
3426    #[test]
3427    fn user_profile_best_label_prefers_login() {
3428        assert_eq!(
3429            profile(1, "alice@example.com").best_label(),
3430            Some("alice@example.com".to_string())
3431        );
3432        let display_only = ts_control::UserProfile {
3433            id: 2,
3434            login_name: String::new(),
3435            display_name: Some("Bob".to_string()),
3436            groups: Vec::new(),
3437        };
3438        assert_eq!(display_only.best_label(), Some("Bob".to_string()));
3439        let empty = ts_control::UserProfile {
3440            id: 3,
3441            login_name: String::new(),
3442            display_name: None,
3443            groups: Vec::new(),
3444        };
3445        assert_eq!(empty.best_label(), None);
3446    }
3447
3448    // ----- tsr-jo1: RotationTracker (Go ipnlocal.rotationTracker.obsoleteKeys) -----
3449
3450    /// A `RotationDetails` for a `Direct`-rooted chain with the given prior keys + wrapping key.
3451    fn rot_details(
3452        prev: &[&[u8]],
3453        wrapping: &[u8],
3454        kind: ts_tka::SigKind,
3455    ) -> ts_tka::RotationDetails {
3456        ts_tka::RotationDetails {
3457            prev_node_keys: prev.iter().map(|p| p.to_vec()).collect(),
3458            initial_sig_kind: kind,
3459            initial_wrapping_pubkey: wrapping.to_vec(),
3460        }
3461    }
3462
3463    /// Rule 1: every prior node key named by any rotation chain is obsolete, regardless of the
3464    /// chain's root kind (Go's ungated `obsolete.AddSlice(d.PrevNodeKeys)`).
3465    #[test]
3466    fn rotation_tracker_prev_keys_always_obsolete() {
3467        let mut t = RotationTracker::default();
3468        // A Direct-rooted chain that rotated away OLD1, and a Credential-rooted one that rotated OLD2.
3469        t.add(
3470            b"newA".to_vec(),
3471            &rot_details(&[b"OLD1"], b"wrapA", ts_tka::SigKind::Direct),
3472        );
3473        t.add(
3474            b"newB".to_vec(),
3475            &rot_details(&[b"OLD2"], b"wrapB", ts_tka::SigKind::Credential),
3476        );
3477        let obsolete = t.obsolete_keys();
3478        assert!(
3479            obsolete.contains(b"OLD1".as_slice()),
3480            "Direct chain's prior key obsolete"
3481        );
3482        assert!(
3483            obsolete.contains(b"OLD2".as_slice()),
3484            "Credential chain's prior key obsolete too (rule 1 is ungated)"
3485        );
3486        // The current keys themselves are not obsolete (only one peer per wrapping key here).
3487        assert!(!obsolete.contains(b"newA".as_slice()));
3488        assert!(!obsolete.contains(b"newB".as_slice()));
3489    }
3490
3491    /// Rule 2: among `Direct`-rooted chains sharing a wrapping key, only the longest survives; the
3492    /// shorter (older) clone's key is obsolete.
3493    #[test]
3494    fn rotation_tracker_unequal_chain_keeps_longest() {
3495        let mut t = RotationTracker::default();
3496        // Same wrapping key; "long" has 2 prior keys, "short" has 1 ⇒ "short" is the older clone.
3497        t.add(
3498            b"long".to_vec(),
3499            &rot_details(&[b"p1", b"p2"], b"wrap", ts_tka::SigKind::Direct),
3500        );
3501        t.add(
3502            b"short".to_vec(),
3503            &rot_details(&[b"q1"], b"wrap", ts_tka::SigKind::Direct),
3504        );
3505        let obsolete = t.obsolete_keys();
3506        assert!(
3507            obsolete.contains(b"short".as_slice()),
3508            "the shorter-chain clone is obsolete"
3509        );
3510        assert!(
3511            !obsolete.contains(b"long".as_slice()),
3512            "the longest-chain peer survives"
3513        );
3514    }
3515
3516    /// Rule 2 tie: two `Direct`-rooted chains sharing a wrapping key with EQUAL chain length cannot
3517    /// be disambiguated ⇒ BOTH are dropped (Go's safety branch).
3518    #[test]
3519    fn rotation_tracker_equal_chain_drops_both() {
3520        let mut t = RotationTracker::default();
3521        t.add(
3522            b"cloneA".to_vec(),
3523            &rot_details(&[b"p1"], b"wrap", ts_tka::SigKind::Direct),
3524        );
3525        t.add(
3526            b"cloneB".to_vec(),
3527            &rot_details(&[b"p2"], b"wrap", ts_tka::SigKind::Direct),
3528        );
3529        let obsolete = t.obsolete_keys();
3530        assert!(
3531            obsolete.contains(b"cloneA".as_slice()),
3532            "tied clone A dropped"
3533        );
3534        assert!(
3535            obsolete.contains(b"cloneB".as_slice()),
3536            "tied clone B dropped"
3537        );
3538    }
3539
3540    /// `Credential`-rooted chains sharing a wrapping key are EXEMPT from rule 2 (reusable-authkey
3541    /// carve-out): both are kept even with equal chain length.
3542    #[test]
3543    fn rotation_tracker_credential_root_clones_both_kept() {
3544        let mut t = RotationTracker::default();
3545        t.add(
3546            b"credA".to_vec(),
3547            &rot_details(&[b"p1"], b"wrap", ts_tka::SigKind::Credential),
3548        );
3549        t.add(
3550            b"credB".to_vec(),
3551            &rot_details(&[b"p2"], b"wrap", ts_tka::SigKind::Credential),
3552        );
3553        let obsolete = t.obsolete_keys();
3554        assert!(
3555            !obsolete.contains(b"credA".as_slice()),
3556            "credential-rooted clone A kept"
3557        );
3558        assert!(
3559            !obsolete.contains(b"credB".as_slice()),
3560            "credential-rooted clone B kept"
3561        );
3562    }
3563
3564    /// A peer that another chain already rotated away does not also act as a surviving clone: it is
3565    /// removed from its wrapping-key group before the longest-survivor pick (Go's `DeleteFunc`).
3566    #[test]
3567    fn rotation_tracker_already_obsolete_peer_not_a_survivor() {
3568        let mut t = RotationTracker::default();
3569        // "victim" is rotated away by "rotator" (different wrapping key), AND shares wrapping key
3570        // "w" with "other". Because "victim" is already obsolete, only "other" is in play for "w" and
3571        // survives (no spurious tie-drop of "other").
3572        t.add(
3573            b"rotator".to_vec(),
3574            &rot_details(&[b"victim"], b"wRot", ts_tka::SigKind::Direct),
3575        );
3576        t.add(
3577            b"victim".to_vec(),
3578            &rot_details(&[b"x"], b"w", ts_tka::SigKind::Direct),
3579        );
3580        t.add(
3581            b"other".to_vec(),
3582            &rot_details(&[b"y"], b"w", ts_tka::SigKind::Direct),
3583        );
3584        let obsolete = t.obsolete_keys();
3585        assert!(
3586            obsolete.contains(b"victim".as_slice()),
3587            "victim rotated away by rotator"
3588        );
3589        assert!(
3590            !obsolete.contains(b"other".as_slice()),
3591            "other survives — victim was removed from the group before the tie check"
3592        );
3593    }
3594
3595    /// Empty tracker (no rotation-signed peers) ⇒ no obsolete keys (the non-rotation netmap path).
3596    #[test]
3597    fn rotation_tracker_empty_is_noop() {
3598        let t = RotationTracker::default();
3599        assert!(t.obsolete_keys().is_empty());
3600    }
3601
3602    /// End-to-end through the real `Full` path: a peer presenting a freshly-rotated key (a Rotation
3603    /// chain) is admitted, while a second peer still presenting the rotated-AWAY pivot key — even with
3604    /// that key's own still-valid Direct signature — is DROPPED by the cross-peer rotation filter.
3605    /// This is the gap closed here: Go `tkaFilterNetmapLocked` drops the stale clone; we used to admit
3606    /// it. Uses real `ts_tka` signing (`sign_direct` + `sign_rotation`) so the whole
3607    /// verify → details → filter pipeline runs.
3608    ///
3609    /// Construction: the trusted key signs an inner `Direct` over the PIVOT keypair's public key; the
3610    /// pivot key then signs an outer `Rotation` authorizing `new_key`. That chain's `prev_node_keys`
3611    /// names the pivot pubkey — so a peer presenting the pivot pubkey as its node key is the
3612    /// rotated-away key the filter must drop.
3613    #[tokio::test]
3614    async fn tka_full_drops_rotated_away_key_e2e() {
3615        use ed25519_dalek::SigningKey;
3616        use ts_tka::NodeKeySignature;
3617
3618        let trusted = SigningKey::from_bytes(&[42u8; 32]);
3619        let trusted_pub = trusted.verifying_key().to_bytes().to_vec();
3620        let authority = Authority::from_state(
3621            AumHash([0; 32]),
3622            State {
3623                keys: vec![Key {
3624                    kind: KeyKind::Ed25519,
3625                    votes: 1,
3626                    public: trusted_pub.clone(),
3627                }],
3628            },
3629        );
3630
3631        // The rotation pivot: a keypair whose public key the inner Direct authorizes and whose
3632        // private key signs the outer rotation wrap. This pivot pubkey IS the key being rotated away.
3633        let pivot = SigningKey::from_bytes(&[9u8; 32]);
3634        let pivot_pub: [u8; 32] = pivot.verifying_key().to_bytes();
3635
3636        let new_key = [4u8; 32]; // the freshly-rotated node key
3637
3638        // Fresh peer: a Rotation chain authorizing `new_key`, inner Direct over the pivot signed by
3639        // trusted, outer wrap signed by the pivot. Its prev_node_keys names `pivot_pub`.
3640        let new_sig = NodeKeySignature::sign_rotation(&new_key, &trusted, &pivot).serialize();
3641        let new_peer = peer_node("rotated", new_key, new_sig);
3642
3643        // Stale peer: still presents the pivot pubkey (the rotated-away key) with its own valid
3644        // Direct signature — valid in isolation, but obsoleted by the fresh peer's rotation chain.
3645        let stale_sig = NodeKeySignature::sign_direct(&pivot_pub, &trusted).serialize();
3646        let stale_peer = peer_node("stale", pivot_pub, stale_sig);
3647
3648        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), Some(authority));
3649        tracker.apply_peer_update(
3650            &ts_control::PeerUpdate::Full(vec![new_peer.clone(), stale_peer.clone()]),
3651            local_now(),
3652        );
3653
3654        assert!(
3655            tracker.peer_db.get(&new_peer.node_key).is_some(),
3656            "the freshly-rotated peer is admitted"
3657        );
3658        assert!(
3659            tracker.peer_db.get(&stale_peer.node_key).is_none(),
3660            "the peer presenting the rotated-away key is dropped (Go tkaFilterNetmapLocked)"
3661        );
3662    }
3663}
3664
3665#[cfg(test)]
3666mod tsmp_disco_key_tests {
3667    //! Receive side of the TSMP disco-key advertisement, at the point the key is *learned*.
3668    //!
3669    //! These exercise [`PeerTracker::learn_disco_key`] — the fork's stand-in for Go
3670    //! `magicsock.Conn.HandleDiscoKeyAdvertisement` — which is the single place an advertisement
3671    //! reaches peer state. The wire decode and the "consumed, not delivered" drop are covered in
3672    //! `ts_packet::tsmp` and `ts_dataplane` respectively.
3673
3674    use ts_keys::DiscoPublicKey;
3675
3676    use super::{
3677        tka_tests::{peer_node, test_env},
3678        *,
3679    };
3680
3681    /// The key a peer advertises, and a second one for the re-advertise case.
3682    const ADVERTISED: [u8; 32] = [0xa5u8; 32];
3683    const READVERTISED: [u8; 32] = [0x5au8; 32];
3684    /// The (staler) key control has for that same peer, and the one control eventually catches up
3685    /// to.
3686    const FROM_CONTROL: [u8; 32] = [0xc0u8; 32];
3687    const CONTROL_CAUGHT_UP: [u8; 32] = [0x0cu8; 32];
3688
3689    /// The node key of the single peer these tests use.
3690    const PEER_NODE_KEY: [u8; 32] = [1u8; 32];
3691
3692    /// A tracker holding one peer with no disco key yet, plus that peer's [`PeerId`].
3693    fn tracker_with_peer() -> (PeerTracker, PeerId) {
3694        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3695        let node = peer_node("peer", PEER_NODE_KEY, Vec::new());
3696        let id = tracker.peer_db.upsert(&node);
3697        (tracker, id)
3698    }
3699
3700    /// The peer as CONTROL describes it: the same node, carrying whatever disco key the netmap says
3701    /// it has (`None` for a peer control has no disco key for at all).
3702    fn node_from_control(disco_key: Option<[u8; 32]>) -> Node {
3703        let mut node = peer_node("peer", PEER_NODE_KEY, Vec::new());
3704        node.disco_key = disco_key.map(DiscoPublicKey::from);
3705        node
3706    }
3707
3708    /// A netmap `Full` carrying just this peer, as control currently describes it.
3709    fn control_full(disco_key: Option<[u8; 32]>) -> ts_control::PeerUpdate {
3710        ts_control::PeerUpdate::Full(vec![node_from_control(disco_key)])
3711    }
3712
3713    /// A tracker whose single peer arrived through the netmap carrying `disco_key`, exactly as the
3714    /// actor's handler applies it. Returns the peer's [`PeerId`] too.
3715    fn tracker_with_control_peer(disco_key: Option<[u8; 32]>) -> (PeerTracker, PeerId) {
3716        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
3717        let node = node_from_control(disco_key);
3718        tracker.apply_peer_update(&control_full(disco_key), local_now());
3719        let id = tracker
3720            .peer_db
3721            .has(&node.node_key)
3722            .expect("control delivered it");
3723        (tracker, id)
3724    }
3725
3726    /// The disco key the peer db currently holds for `peer` — the effective key every direct-path
3727    /// consumer resolves against.
3728    fn effective_key(tracker: &PeerTracker, peer: PeerId) -> Option<DiscoPublicKey> {
3729        tracker
3730            .peer_db
3731            .get(&peer)
3732            .expect("peer still present")
3733            .1
3734            .disco_key
3735    }
3736
3737    /// The happy path: an advertised key is applied to the peer AND lands in the disco index, which
3738    /// is what the direct-path machinery (`direct::DiscoPeerLookup`) reads. Re-advertising the same
3739    /// key is a no-op; advertising a different one replaces it, retracting the old index entry.
3740    #[tokio::test]
3741    async fn advertisement_learns_the_peers_disco_key() {
3742        let (mut tracker, peer) = tracker_with_peer();
3743        let key = DiscoPublicKey::from(ADVERTISED);
3744
3745        assert!(
3746            tracker.learn_disco_key(peer, key),
3747            "a first advertisement changes the peer db"
3748        );
3749        assert_eq!(
3750            tracker
3751                .peer_db
3752                .get(&peer)
3753                .expect("peer still present")
3754                .1
3755                .disco_key,
3756            Some(key),
3757            "the advertised disco key is learned"
3758        );
3759        assert_eq!(
3760            tracker.peer_db.has(&key),
3761            Some(peer),
3762            "and is reachable through the disco index the direct path resolves against"
3763        );
3764
3765        assert!(
3766            !tracker.learn_disco_key(peer, key),
3767            "re-advertising the same key is a no-op (Go counts it 'unchanged' and returns)"
3768        );
3769
3770        let rotated = DiscoPublicKey::from(READVERTISED);
3771        assert!(tracker.learn_disco_key(peer, rotated));
3772        assert_eq!(
3773            tracker
3774                .peer_db
3775                .get(&peer)
3776                .expect("peer still present")
3777                .1
3778                .disco_key,
3779            Some(rotated),
3780            "a later advertisement replaces the key without a netmap update"
3781        );
3782        assert_eq!(tracker.peer_db.has(&rotated), Some(peer));
3783        assert_eq!(
3784            tracker.peer_db.has(&key),
3785            None,
3786            "the superseded key no longer resolves to the peer"
3787        );
3788    }
3789
3790    /// The refusals, each of which must leave the peer db untouched: the zero key is never learned,
3791    /// and an advertisement never creates a peer.
3792    #[tokio::test]
3793    async fn refused_advertisements_change_nothing() {
3794        let (mut tracker, peer) = tracker_with_peer();
3795
3796        assert!(
3797            !tracker.learn_disco_key(peer, DiscoPublicKey::from([0u8; 32])),
3798            "the zero key is never learned"
3799        );
3800        assert_eq!(
3801            tracker
3802                .peer_db
3803                .get(&peer)
3804                .expect("peer still present")
3805                .1
3806                .disco_key,
3807            None,
3808            "a zero-key advertisement must not bind the peer to an unusable key"
3809        );
3810
3811        // An advertisement for a peer control has never told us about. Go logs "endpoint not found
3812        // for node" and returns; it must not conjure a peer into existence.
3813        let unknown = PeerId(4242);
3814        assert_eq!(tracker.peer_db.get(&unknown), None, "precondition");
3815        assert!(
3816            !tracker.learn_disco_key(unknown, DiscoPublicKey::from(ADVERTISED)),
3817            "an advertisement for an unknown peer is ignored"
3818        );
3819        assert_eq!(
3820            tracker.peer_db.peers().len(),
3821            1,
3822            "an advertisement never creates a peer — only control does"
3823        );
3824        assert_eq!(
3825            tracker.peer_db.has(&DiscoPublicKey::from(ADVERTISED)),
3826            None,
3827            "and never indexes a key against a peer that does not exist"
3828        );
3829    }
3830
3831    /// The feature's motivating case, end to end: the peer told us a key control has not caught up
3832    /// with, and then control polls again with the SAME stale key it had before. The advertisement
3833    /// must survive.
3834    ///
3835    /// Go keeps the two keys apart on the endpoint (`endpointDisco.controlKey` /
3836    /// `tsmpKey`), and `updateFromNode` only rewrites the control side when control's key actually
3837    /// changed — so a netmap restating the old key never touches the active TSMP key. With a single
3838    /// field the next map poll silently reverted the peer to control's stale key, which is precisely
3839    /// the state the advertisement exists to escape.
3840    #[tokio::test]
3841    async fn netmap_restating_controls_stale_key_keeps_the_tsmp_key() {
3842        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
3843        let advertised = DiscoPublicKey::from(ADVERTISED);
3844        assert_eq!(
3845            effective_key(&tracker, peer),
3846            Some(DiscoPublicKey::from(FROM_CONTROL)),
3847            "precondition: the peer starts on the key control gave us"
3848        );
3849
3850        assert!(tracker.learn_disco_key(peer, advertised));
3851        assert_eq!(effective_key(&tracker, peer), Some(advertised));
3852
3853        // Control polls again, still behind: a `Full` resync, then a `Delta` re-upsert, both
3854        // carrying the key control already sent.
3855        tracker.apply_peer_update(&control_full(Some(FROM_CONTROL)), local_now());
3856        assert_eq!(
3857            effective_key(&tracker, peer),
3858            Some(advertised),
3859            "a Full restating control's stale key must not undo the TSMP-learned key"
3860        );
3861        tracker.apply_peer_update(
3862            &ts_control::PeerUpdate::Delta {
3863                upsert: vec![node_from_control(Some(FROM_CONTROL))],
3864                remove: vec![],
3865            },
3866            local_now(),
3867        );
3868        assert_eq!(
3869            effective_key(&tracker, peer),
3870            Some(advertised),
3871            "and neither must a Delta re-upsert of the same node"
3872        );
3873        assert_eq!(
3874            tracker.peer_db.has(&advertised),
3875            Some(peer),
3876            "the direct path still resolves the peer by the key it advertised"
3877        );
3878        assert_eq!(
3879            tracker.peer_db.has(&DiscoPublicKey::from(FROM_CONTROL)),
3880            None,
3881            "and control's superseded key does not resolve to it"
3882        );
3883
3884        // Control finally changes its mind. The new key is recorded in control's slot, but the key
3885        // the peer itself told us stays active — upstream returns to control's key only when disco
3886        // is received under it (`endpoint.checkAndUpdateDiscoKey`).
3887        tracker.apply_peer_update(&control_full(Some(CONTROL_CAUGHT_UP)), local_now());
3888        assert_eq!(
3889            effective_key(&tracker, peer),
3890            Some(advertised),
3891            "a control-side key change must not preempt an active TSMP-learned key"
3892        );
3893        assert_eq!(
3894            tracker.control_disco_key(&PEER_NODE_KEY.into()),
3895            Some(DiscoPublicKey::from(CONTROL_CAUGHT_UP)),
3896            "but control's new key IS recorded in control's slot"
3897        );
3898    }
3899
3900    /// An advertisement that merely restates the key control already gave us is still *new*
3901    /// information — it is the peer itself confirming the key — so Go records it as the TSMP key and
3902    /// makes it active. Its "unchanged" early return compares `epDisco.keyFromTSMP()`, the
3903    /// TSMP-learned key specifically, never the effective one.
3904    ///
3905    /// The observable consequence, asserted here: once the peer has confirmed the key, control
3906    /// dropping it (a netmap node with no disco key) leaves the confirmed key in place instead of
3907    /// blinding the direct path.
3908    #[tokio::test]
3909    async fn advertisement_restating_controls_key_is_recorded_as_the_tsmp_key() {
3910        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
3911        let key = DiscoPublicKey::from(FROM_CONTROL);
3912
3913        assert!(
3914            tracker.learn_disco_key(peer, key),
3915            "an advertisement of the key control already sent is recorded, not dropped"
3916        );
3917        assert_eq!(
3918            tracker
3919                .endpoint_disco
3920                .get(&PEER_NODE_KEY.into())
3921                .and_then(EndpointDisco::key_from_tsmp),
3922            Some(key),
3923            "it lands in the TSMP slot (Go epDisco.tsmpKey), not only in control's"
3924        );
3925        assert!(
3926            !tracker.learn_disco_key(peer, key),
3927            "re-advertising it now IS unchanged, and is refused"
3928        );
3929
3930        // Control drops the peer's disco key. The key the peer itself confirmed stays active.
3931        tracker.apply_peer_update(&control_full(None), local_now());
3932        assert_eq!(
3933            effective_key(&tracker, peer),
3934            Some(key),
3935            "a control key going away hands the active slot to the TSMP-learned key"
3936        );
3937        assert_eq!(tracker.peer_db.has(&key), Some(peer));
3938    }
3939
3940    /// A `PeersChangedPatch` is a control write like any other: one that says nothing about the
3941    /// disco key must leave an active TSMP key alone, and one that carries a new key is control
3942    /// catching up, so it wins.
3943    ///
3944    /// The patch path is the subtle one — it starts from the db node, which carries the *effective*
3945    /// key, so without re-deriving what control last said it would hand the TSMP key back as if
3946    /// control had sent it.
3947    #[tokio::test]
3948    async fn patch_without_a_disco_key_leaves_the_tsmp_key_active() {
3949        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
3950        let advertised = DiscoPublicKey::from(ADVERTISED);
3951        assert!(tracker.learn_disco_key(peer, advertised));
3952
3953        // A reachability-only patch (the idle-peer-reconnect case) for the same node.
3954        let endpoint: std::net::SocketAddr = "203.0.113.9:41641".parse().unwrap();
3955        let mut patch = ts_control::PeerChange {
3956            id: 1,
3957            derp_region: None,
3958            cap: None,
3959            cap_map: None,
3960            underlay_addresses: Some(vec![endpoint]),
3961            node_key: None,
3962            key_signature: None,
3963            disco_key: None,
3964            node_key_expiry: None,
3965            online: None,
3966            last_seen: None,
3967        };
3968        tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3969        assert_eq!(
3970            effective_key(&tracker, peer),
3971            Some(advertised),
3972            "a patch that never mentions the disco key must not revert it to control's"
3973        );
3974        assert_eq!(
3975            tracker
3976                .peer_db
3977                .get(&peer)
3978                .expect("peer still present")
3979                .1
3980                .underlay_addresses,
3981            vec![endpoint],
3982            "and the patch it DID carry still applied"
3983        );
3984
3985        // Now control changes the key through the patch channel. Same rule as the netmap path: the
3986        // key lands in control's slot, and the active TSMP key is left alone.
3987        patch.disco_key = Some(DiscoPublicKey::from(CONTROL_CAUGHT_UP));
3988        tracker.apply_peer_patches(std::slice::from_ref(&patch), local_now());
3989        assert_eq!(
3990            effective_key(&tracker, peer),
3991            Some(advertised),
3992            "a patch carrying a new disco key does not preempt the active TSMP-learned key either"
3993        );
3994        assert_eq!(
3995            tracker.control_disco_key(&PEER_NODE_KEY.into()),
3996            Some(DiscoPublicKey::from(CONTROL_CAUGHT_UP)),
3997            "the patched key is still recorded as what control now says"
3998        );
3999    }
4000
4001    /// The rule this whole pair of slots exists to express: once the peer has told us its key over
4002    /// TSMP, control changing its mind is *recorded* but does not take the active slot back — Go
4003    /// `endpoint.updateDiscoKey`'s `epDisco.tsmpActive = old.tsmpActive || key.IsZero()`.
4004    ///
4005    /// Control is the slower source; a key the peer sent us itself is the better evidence. Upstream
4006    /// hands the slot back only when disco is actually *received* under control's key
4007    /// (`endpoint.checkAndUpdateDiscoKey`). Here the peer re-advertising is the path back, and it is
4008    /// asserted at the end so the sticky rule cannot be read as "the TSMP key is now permanent".
4009    #[tokio::test]
4010    async fn a_control_key_change_does_not_preempt_an_active_tsmp_key() {
4011        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4012        let advertised = DiscoPublicKey::from(ADVERTISED);
4013        let caught_up = DiscoPublicKey::from(CONTROL_CAUGHT_UP);
4014        assert!(tracker.learn_disco_key(peer, advertised));
4015
4016        tracker.apply_peer_update(&control_full(Some(CONTROL_CAUGHT_UP)), local_now());
4017        assert_eq!(
4018            effective_key(&tracker, peer),
4019            Some(advertised),
4020            "the TSMP-learned key stays active across a control-side change"
4021        );
4022        assert_eq!(
4023            tracker.peer_db.has(&advertised),
4024            Some(peer),
4025            "so the direct path still resolves the peer by the key it advertised"
4026        );
4027        assert_eq!(
4028            tracker.peer_db.has(&caught_up),
4029            None,
4030            "and control's new key is not what we send to"
4031        );
4032        assert_eq!(
4033            tracker.control_disco_key(&PEER_NODE_KEY.into()),
4034            Some(caught_up),
4035            "control's new key is recorded all the same — it is not discarded, just not active"
4036        );
4037
4038        // Control changing its mind a second time, and then dropping the key entirely, changes
4039        // nothing about which key is active.
4040        tracker.apply_peer_update(&control_full(Some(FROM_CONTROL)), local_now());
4041        tracker.apply_peer_update(&control_full(None), local_now());
4042        assert_eq!(
4043            effective_key(&tracker, peer),
4044            Some(advertised),
4045            "neither a second control change nor control dropping the key moves the active slot"
4046        );
4047
4048        // The peer itself is what moves it: it advertises the key control had been trying to give
4049        // us, and that advertisement is what we act on.
4050        assert!(tracker.learn_disco_key(peer, caught_up));
4051        assert_eq!(
4052            effective_key(&tracker, peer),
4053            Some(caught_up),
4054            "a peer re-advertising moves the active key, because the peer is the evidence"
4055        );
4056    }
4057
4058    /// The sticky flag must not strand a peer that never had a TSMP key: control sending nothing
4059    /// leaves no key material at all, and the key control sends next must become the active one.
4060    ///
4061    /// This is the case Go covers by nil-ing the endpoint's `disco` pointer when both keys are
4062    /// zero; here [`PeerTracker::upsert_from_control`] drops the entry, so the "no control key means
4063    /// the TSMP slot is active" flag cannot survive to shadow a later control key with nothing.
4064    #[tokio::test]
4065    async fn a_first_control_key_is_active_even_after_control_sent_none() {
4066        let (mut tracker, peer) = tracker_with_control_peer(None);
4067        assert_eq!(effective_key(&tracker, peer), None, "precondition");
4068        assert!(
4069            tracker.endpoint_disco.is_empty(),
4070            "a peer with no key material from either source costs no entry"
4071        );
4072
4073        tracker.apply_peer_update(&control_full(Some(FROM_CONTROL)), local_now());
4074        assert_eq!(
4075            effective_key(&tracker, peer),
4076            Some(DiscoPublicKey::from(FROM_CONTROL)),
4077            "control's first key is active — there is no TSMP key for it to defer to"
4078        );
4079        assert_eq!(
4080            tracker.peer_db.has(&DiscoPublicKey::from(FROM_CONTROL)),
4081            Some(peer)
4082        );
4083    }
4084
4085    /// The other half of `tsmpActive = old.tsmpActive || key.IsZero()`, in the one state where the
4086    /// left operand is false *and* a TSMP key exists: after disco was received under control's key,
4087    /// which is upstream's only route back to control holding the active slot
4088    /// (`endpoint.checkAndUpdateDiscoKey`).
4089    ///
4090    /// Two things follow, and neither is obvious from the sticky rule alone. Control's later changes
4091    /// **do** land, because what is sticky is the flag, not the TSMP key — so this is not "the TSMP
4092    /// key wins forever", and a peer that genuinely rotated is not stranded. And control *dropping*
4093    /// its key does not leave the peer with no disco key at all: the `key.IsZero()` operand hands the
4094    /// slot to the TSMP key still sitting in the other slot, which is why Go only nils the endpoint's
4095    /// `disco` pointer when **both** keys are zero.
4096    #[tokio::test]
4097    async fn control_regains_the_slot_by_being_received_under_and_then_keeps_it() {
4098        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4099        let from_control = DiscoPublicKey::from(FROM_CONTROL);
4100        let advertised = DiscoPublicKey::from(ADVERTISED);
4101        let caught_up = DiscoPublicKey::from(CONTROL_CAUGHT_UP);
4102
4103        // Get into the state: the peer advertises, then sends disco under control's key anyway, so
4104        // control's key is active again with the TSMP key demoted but retained.
4105        assert!(tracker.learn_disco_key(peer, advertised));
4106        assert!(tracker.observe_disco_key(peer, from_control));
4107        assert_eq!(
4108            effective_key(&tracker, peer),
4109            Some(from_control),
4110            "precondition: control holds the active slot because we received under its key"
4111        );
4112        assert_eq!(
4113            ingress_match(&tracker, advertised),
4114            Some((peer, peer_db::DiscoKeyMatch::Inactive)),
4115            "precondition: the TSMP key is demoted, not discarded"
4116        );
4117
4118        // Control changes its key. With the TSMP key demoted the sticky operand is false, so this
4119        // one does take the active slot — the flag is what is sticky, not the TSMP key.
4120        tracker.apply_peer_update(&control_full(Some(CONTROL_CAUGHT_UP)), local_now());
4121        assert_eq!(
4122            effective_key(&tracker, peer),
4123            Some(caught_up),
4124            "a demoted TSMP key does not block control's next key from becoming active"
4125        );
4126        assert_eq!(
4127            ingress_match(&tracker, advertised),
4128            Some((peer, peer_db::DiscoKeyMatch::Inactive)),
4129            "and the TSMP key is still the peer's other known key for ingress"
4130        );
4131        assert_eq!(
4132            ingress_match(&tracker, from_control),
4133            None,
4134            "control's superseded key is not a third slot"
4135        );
4136
4137        // Control drops its key entirely. `key.IsZero()` is the operand that carries the peer here:
4138        // the retained TSMP key becomes active rather than the peer losing disco altogether.
4139        tracker.apply_peer_update(&control_full(None), local_now());
4140        assert_eq!(
4141            effective_key(&tracker, peer),
4142            Some(advertised),
4143            "control dropping its key falls back to the TSMP key, not to no key"
4144        );
4145        assert_eq!(
4146            ingress_match(&tracker, advertised),
4147            Some((peer, peer_db::DiscoKeyMatch::Active))
4148        );
4149        assert_eq!(
4150            tracker.control_disco_key(&PEER_NODE_KEY.into()),
4151            None,
4152            "control's slot is cleared, so there is no second key to accept"
4153        );
4154        assert_eq!(
4155            ingress_match(&tracker, caught_up),
4156            None,
4157            "the key control withdrew stops resolving on ingress"
4158        );
4159    }
4160
4161    /// The TSMP-learned key lives exactly as long as Go's endpoint does: it is dropped when the peer
4162    /// leaves the netmap, and it is not carried across a node-key rotation (Go builds the rotated
4163    /// peer a brand-new endpoint, with a brand-new `endpointDisco`).
4164    #[tokio::test]
4165    async fn tsmp_key_does_not_outlive_the_peer_or_its_node_key() {
4166        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4167        assert!(tracker.learn_disco_key(peer, DiscoPublicKey::from(ADVERTISED)));
4168
4169        // The peer leaves the netmap, then comes back on control's key.
4170        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![]), local_now());
4171        assert!(tracker.peer_db.peers().is_empty());
4172        assert!(
4173            tracker.endpoint_disco.is_empty(),
4174            "the departed peer's disco state goes with it"
4175        );
4176        tracker.apply_peer_update(&control_full(Some(FROM_CONTROL)), local_now());
4177        let readded = node_from_control(Some(FROM_CONTROL));
4178        let peer = tracker.peer_db.has(&readded.node_key).expect("re-added");
4179        assert_eq!(
4180            effective_key(&tracker, peer),
4181            Some(DiscoPublicKey::from(FROM_CONTROL)),
4182            "a peer that left and rejoined starts from control's key again"
4183        );
4184
4185        // Learn a key again, then rotate the node key underneath it.
4186        assert!(tracker.learn_disco_key(peer, DiscoPublicKey::from(READVERTISED)));
4187        let mut rotated = node_from_control(Some(FROM_CONTROL));
4188        rotated.node_key = [2u8; 32].into();
4189        tracker.apply_peer_update(
4190            &ts_control::PeerUpdate::Full(vec![rotated.clone()]),
4191            local_now(),
4192        );
4193        let peer = tracker
4194            .peer_db
4195            .has(&rotated.node_key)
4196            .expect("rotated peer");
4197        assert_eq!(
4198            effective_key(&tracker, peer),
4199            Some(DiscoPublicKey::from(FROM_CONTROL)),
4200            "a key learned under the old node key is not carried onto the new one"
4201        );
4202        assert_eq!(
4203            tracker.endpoint_disco.len(),
4204            1,
4205            "and the old node key's state is pruned"
4206        );
4207    }
4208
4209    /// How the peer db resolves `key` for an inbound disco frame: the peer it belongs to and which
4210    /// of that peer's two slots it matched.
4211    fn ingress_match(
4212        tracker: &PeerTracker,
4213        key: DiscoPublicKey,
4214    ) -> Option<(PeerId, peer_db::DiscoKeyMatch)> {
4215        tracker
4216            .peer_db
4217            .peer_by_known_disco_key(&key)
4218            .map(|(id, _node, matched)| (id, matched))
4219    }
4220
4221    /// The bead's case, end to end: the peer advertised K2 over TSMP so we send to K2, but it is
4222    /// still sending disco under the K1 control gave us. That frame must resolve to the peer, and
4223    /// receiving under K1 must make K1 the key we send to — because it is demonstrably what the
4224    /// peer uses.
4225    ///
4226    /// Go: every inbound disco comparison goes through `endpoint.checkAndUpdateDiscoKey`, which
4227    /// accepts either slot and compare-and-swaps `tsmpActive` when the key seen is the inactive one.
4228    #[tokio::test]
4229    async fn disco_under_the_inactive_key_is_accepted_and_makes_that_key_active() {
4230        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4231        let from_control = DiscoPublicKey::from(FROM_CONTROL);
4232        let advertised = DiscoPublicKey::from(ADVERTISED);
4233
4234        assert!(tracker.learn_disco_key(peer, advertised));
4235        assert_eq!(
4236            effective_key(&tracker, peer),
4237            Some(advertised),
4238            "precondition: we are sending to the TSMP-learned key"
4239        );
4240        assert_eq!(
4241            ingress_match(&tracker, from_control),
4242            Some((peer, peer_db::DiscoKeyMatch::Inactive)),
4243            "control's key is still the peer's other known key, and still resolves on ingress"
4244        );
4245
4246        // Disco arrives under control's key: accepted, and it becomes the active one.
4247        assert!(
4248            tracker.observe_disco_key(peer, from_control),
4249            "receiving under the inactive key switches the active key"
4250        );
4251        assert_eq!(
4252            effective_key(&tracker, peer),
4253            Some(from_control),
4254            "we now send to the key the peer is demonstrably using"
4255        );
4256        assert_eq!(
4257            tracker.peer_db.has(&from_control),
4258            Some(peer),
4259            "and it is the key the send-side disco index carries"
4260        );
4261        assert_eq!(
4262            ingress_match(&tracker, from_control),
4263            Some((peer, peer_db::DiscoKeyMatch::Active))
4264        );
4265        assert_eq!(
4266            ingress_match(&tracker, advertised),
4267            Some((peer, peer_db::DiscoKeyMatch::Inactive)),
4268            "the TSMP key is retained in the other slot, so ingress under it still resolves"
4269        );
4270
4271        assert!(
4272            !tracker.observe_disco_key(peer, from_control),
4273            "a second frame under the now-active key changes nothing (and forces no republish)"
4274        );
4275
4276        // And it switches back: the peer resumes sending under the key it advertised.
4277        assert!(tracker.observe_disco_key(peer, advertised));
4278        assert_eq!(effective_key(&tracker, peer), Some(advertised));
4279        assert_eq!(
4280            ingress_match(&tracker, from_control),
4281            Some((peer, peer_db::DiscoKeyMatch::Inactive))
4282        );
4283    }
4284
4285    /// The refusal that is the whole security value of the check: a key belonging to NEITHER slot
4286    /// is rejected, leaving the peer on the key it was on. Plus the two other refusals Go has —
4287    /// an unknown peer, and a peer with no disco key material at all (`epDisco == nil`).
4288    #[tokio::test]
4289    async fn disco_under_a_key_in_neither_slot_is_refused() {
4290        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4291        let from_control = DiscoPublicKey::from(FROM_CONTROL);
4292        let advertised = DiscoPublicKey::from(ADVERTISED);
4293        let third = DiscoPublicKey::from(READVERTISED);
4294
4295        assert!(tracker.learn_disco_key(peer, advertised));
4296
4297        assert!(
4298            !tracker.observe_disco_key(peer, third),
4299            "a third key is refused: a peer must not move itself onto a key nobody told us about"
4300        );
4301        assert_eq!(
4302            effective_key(&tracker, peer),
4303            Some(advertised),
4304            "and the peer stays on the key it was on"
4305        );
4306        assert_eq!(
4307            ingress_match(&tracker, third),
4308            None,
4309            "the refused key never becomes resolvable"
4310        );
4311        assert_eq!(
4312            ingress_match(&tracker, from_control),
4313            Some((peer, peer_db::DiscoKeyMatch::Inactive)),
4314            "the two real slots are untouched"
4315        );
4316
4317        // An unknown peer: like a TSMP advertisement, this never creates one.
4318        assert!(!tracker.observe_disco_key(PeerId(4242), from_control));
4319        assert_eq!(tracker.peer_db.peers().len(), 1);
4320
4321        // A peer with no disco key from either source — Go returns false on `epDisco == nil`.
4322        let (mut bare, bare_peer) = tracker_with_control_peer(None);
4323        assert_eq!(effective_key(&bare, bare_peer), None, "precondition");
4324        assert!(
4325            !bare.observe_disco_key(bare_peer, from_control),
4326            "a peer with no known disco key has no slot for this key to match"
4327        );
4328        assert_eq!(effective_key(&bare, bare_peer), None);
4329    }
4330
4331    /// A peer that has only ever had one key registers no inactive key at all, so the second index
4332    /// stays empty and an inbound frame under any other key is refused.
4333    #[tokio::test]
4334    async fn a_single_key_peer_has_no_second_slot() {
4335        let (tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4336        let from_control = DiscoPublicKey::from(FROM_CONTROL);
4337
4338        assert_eq!(
4339            ingress_match(&tracker, from_control),
4340            Some((peer, peer_db::DiscoKeyMatch::Active))
4341        );
4342        assert_eq!(
4343            ingress_match(&tracker, DiscoPublicKey::from(ADVERTISED)),
4344            None,
4345            "no second key was ever learned, so nothing else resolves to this peer"
4346        );
4347    }
4348
4349    /// An advertisement that merely restates control's key must not leave the peer with the same
4350    /// key in both slots pretending to be two — `inactive_key` reports `None` when the inactive
4351    /// slot holds the active key, so ingress sees exactly one key.
4352    #[tokio::test]
4353    async fn the_same_key_in_both_slots_is_one_key() {
4354        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4355        let from_control = DiscoPublicKey::from(FROM_CONTROL);
4356
4357        assert!(tracker.learn_disco_key(peer, from_control));
4358        assert_eq!(
4359            tracker
4360                .endpoint_disco
4361                .get(&PEER_NODE_KEY.into())
4362                .and_then(EndpointDisco::inactive_key),
4363            None,
4364            "both slots hold the same key, so there is no second key"
4365        );
4366        assert_eq!(
4367            ingress_match(&tracker, from_control),
4368            Some((peer, peer_db::DiscoKeyMatch::Active))
4369        );
4370        assert!(
4371            !tracker.observe_disco_key(peer, from_control),
4372            "and receiving under it is a no-op, not a switch"
4373        );
4374    }
4375
4376    /// A peer that leaves the netmap takes BOTH its keys with it: the inactive-key index must not
4377    /// keep attributing frames to a peer that is gone.
4378    #[tokio::test]
4379    async fn a_departed_peer_stops_resolving_under_either_key() {
4380        let (mut tracker, peer) = tracker_with_control_peer(Some(FROM_CONTROL));
4381        let advertised = DiscoPublicKey::from(ADVERTISED);
4382        assert!(tracker.learn_disco_key(peer, advertised));
4383        assert!(ingress_match(&tracker, DiscoPublicKey::from(FROM_CONTROL)).is_some());
4384
4385        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![]), local_now());
4386        assert_eq!(ingress_match(&tracker, advertised), None);
4387        assert_eq!(
4388            ingress_match(&tracker, DiscoPublicKey::from(FROM_CONTROL)),
4389            None,
4390            "the inactive key is retracted with the peer, not left dangling"
4391        );
4392    }
4393}
4394
4395#[cfg(test)]
4396mod expiry_tests {
4397    //! Node-key expiry enforcement at the peer tracker — the port of Go's `expiryManager`
4398    //! (`ipn/ipnlocal/expiry.go`) wired into this fork's netmap.
4399    //!
4400    //! [`ts_control::ExpiryManager`] carries its own unit tests for the decision itself. These
4401    //! cover the wiring: that the pass runs at the install site, that the state it leaves is
4402    //! observable through [`StatusNode`], and that the timer catches a peer that expires with **no
4403    //! netmap in between** — the case the timer exists for.
4404
4405    use chrono::TimeDelta;
4406    use kameo::actor::Spawn as _;
4407
4408    use super::{
4409        tka_tests::{await_peer_count, netmap_with_peers, peer_node, test_env},
4410        *,
4411    };
4412
4413    /// A peer with a chosen key expiry, endpoints and a DERP home — the three things the expiry
4414    /// pass strips.
4415    fn expiring_peer(
4416        stable_id: &str,
4417        key: u8,
4418        expiry: Option<chrono::DateTime<chrono::Utc>>,
4419    ) -> Node {
4420        let mut node = peer_node(stable_id, [key; 32], Vec::new());
4421        node.node_key_expiry = expiry;
4422        node.underlay_addresses = vec!["192.0.2.9:41641".parse().unwrap()];
4423        node.derp_region = Some(ts_derp::RegionId(core::num::NonZeroU32::new(3).unwrap()));
4424        node.peerapi_port = Some(8080);
4425        node
4426    }
4427
4428    /// The fail-closed direction here is to **flag, not drop**: Go deliberately keeps an expired
4429    /// peer in the netmap so callers can give a clear error, and removing it would lose that. The
4430    /// peer stays addressable by stable id while losing everything that could carry traffic.
4431    #[tokio::test]
4432    async fn an_expired_peer_is_flagged_and_kept_not_dropped() {
4433        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
4434        let now = local_now();
4435        let peer = expiring_peer("eXpIrEd", 7, Some(now - TimeDelta::hours(1)));
4436        let pristine_key = peer.node_key;
4437
4438        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer.clone()]), now);
4439
4440        let (_id, stored) = tracker
4441            .peer_db
4442            .get(&peer.stable_id)
4443            .expect("the expired peer is KEPT in the netmap, not dropped");
4444        assert!(stored.expired);
4445        assert!(
4446            stored.underlay_addresses.is_empty(),
4447            "endpoints are cleared"
4448        );
4449        assert_eq!(stored.derp_region, None, "the DERP home is cleared");
4450        assert_eq!(
4451            stored.node_key,
4452            ts_keys::node_public_with_bad_old_prefix(pristine_key),
4453            "the node key is broken, so nothing can handshake with the peer"
4454        );
4455        assert_eq!(
4456            stored.peerapi_addr(),
4457            None,
4458            "a peerAPI dial to the expired peer is refused"
4459        );
4460
4461        let status = tracker.status_peers();
4462        assert_eq!(status.len(), 1);
4463        assert!(status[0].expired, "the state is observable to a watcher");
4464    }
4465
4466    /// The negative case: a tagged node carries no key expiry at all (Go's zero `KeyExpiry`) and is
4467    /// never flagged, however far the clock is pushed.
4468    #[tokio::test]
4469    async fn a_peer_with_no_expiry_is_never_flagged() {
4470        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
4471        let now = local_now();
4472        let tagged = expiring_peer("tAgGeD", 8, None);
4473
4474        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![tagged.clone()]), now);
4475        assert!(
4476            tracker
4477                .reevaluate_expiry(now + TimeDelta::days(3650))
4478                .is_empty(),
4479            "a node with no expiry never expires, ten years on"
4480        );
4481
4482        let (_id, stored) = tracker
4483            .peer_db
4484            .get(&tagged.stable_id)
4485            .expect("still a peer");
4486        assert!(!stored.expired);
4487        assert_eq!(stored.node_key, tagged.node_key, "its key is left alone");
4488        assert_eq!(stored.underlay_addresses, tagged.underlay_addresses);
4489        assert_eq!(stored.derp_region, tagged.derp_region);
4490    }
4491
4492    /// The case the timer exists for: a peer that is perfectly live when it is installed, and whose
4493    /// key expiry then passes with **no netmap in between**. `reevaluate_expiry` is what the fired
4494    /// timer runs.
4495    #[tokio::test]
4496    async fn a_peer_that_expires_between_netmaps_is_flagged_by_the_timer_pass() {
4497        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
4498        let now = local_now();
4499        let peer = expiring_peer("lIvE", 9, Some(now + TimeDelta::hours(1)));
4500
4501        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer.clone()]), now);
4502        let (_id, stored) = tracker.peer_db.get(&peer.stable_id).expect("installed");
4503        assert!(!stored.expired, "not expired when control handed it to us");
4504
4505        // No netmap arrives. The clock crosses the peer's expiry.
4506        let upserts = tracker.reevaluate_expiry(now + TimeDelta::hours(2));
4507
4508        assert_eq!(upserts.len(), 1, "the peer is re-installed, flagged");
4509        let (_id, stored) = tracker
4510            .peer_db
4511            .get(&peer.stable_id)
4512            .expect("still kept, just flagged");
4513        assert!(stored.expired);
4514        assert!(stored.underlay_addresses.is_empty());
4515        assert_eq!(stored.derp_region, None);
4516    }
4517
4518    /// An already-expired peer must be skipped rather than re-flagged, or the log and the
4519    /// invalidation it triggers repeat on every pass — and the broken key would be re-broken.
4520    #[tokio::test]
4521    async fn an_already_flagged_peer_is_not_reflagged() {
4522        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
4523        let now = local_now();
4524        let peer = expiring_peer("lIvE", 9, Some(now + TimeDelta::hours(1)));
4525        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer.clone()]), now);
4526
4527        let later = now + TimeDelta::hours(2);
4528        assert_eq!(tracker.reevaluate_expiry(later).len(), 1);
4529        let after_first = tracker
4530            .peer_db
4531            .get(&peer.stable_id)
4532            .expect("kept")
4533            .1
4534            .clone();
4535
4536        assert!(
4537            tracker.reevaluate_expiry(later).is_empty(),
4538            "the second pass reports no transition, so nothing is re-published"
4539        );
4540        assert_eq!(
4541            tracker.peer_db.get(&peer.stable_id).expect("kept").1,
4542            &after_first,
4543            "and nothing is mutated a second time"
4544        );
4545    }
4546
4547    /// End to end through the LIVE actor, which is the only thing that proves the wiring: a peer
4548    /// that is live when the netmap installs it, and whose key then expires while the map poll sits
4549    /// idle, is flagged by the timer alone. If the timer were never armed — or its firing never
4550    /// re-ran the pass — the peer would keep its endpoints, its DERP home and a usable node key
4551    /// until control happened to send another netmap.
4552    #[tokio::test]
4553    async fn a_key_expiring_with_no_netmap_in_between_is_caught_by_the_live_timer() {
4554        let env = test_env();
4555        let (_tka_tx, tka_rx) = watch::channel(None);
4556        let live = PeerTracker::spawn((env.clone(), tka_rx));
4557        assert!(live.ask(AllPeers).await.expect("started").is_empty());
4558
4559        // Expires shortly, but strictly in the future: the install-time pass must NOT flag it. The
4560        // window has to outlast actor start + publish + one ask on a loaded box, hence seconds
4561        // rather than milliseconds; the test does not wait it out, it only waits for the wall clock
4562        // to cross it (below).
4563        let expiry = local_now() + TimeDelta::seconds(2);
4564        let peer = expiring_peer("sHoRtLiVeD", 4, Some(expiry));
4565        env.publish(Arc::new(netmap_with_peers(vec![peer.clone()])))
4566            .await
4567            .expect("publish netmap");
4568
4569        let installed = await_peer_count(&live, 1).await;
4570        assert!(
4571            local_now() < expiry,
4572            "the install has to finish inside the window, or this test is not testing the timer"
4573        );
4574        assert!(
4575            !installed[0].expired,
4576            "still live when control handed it to us"
4577        );
4578
4579        // Let the key really expire on the wall clock the pass reads...
4580        while local_now() <= expiry {
4581            tokio::time::sleep(std::time::Duration::from_millis(25)).await;
4582        }
4583        // ...then jump the runtime's timer wheel past the armed delay (the peer's expiry plus Go's
4584        // slack) so the timer fires now instead of ten seconds from now. No netmap in between.
4585        tokio::time::pause();
4586        tokio::time::advance(std::time::Duration::from_secs(
4587            ts_control::EXPIRY_TIMER_SLACK_SECS as u64 + 5,
4588        ))
4589        .await;
4590        tokio::time::resume();
4591
4592        let flagged = tokio::time::timeout(std::time::Duration::from_secs(10), async {
4593            loop {
4594                let peers = live.ask(AllPeers).await.expect("peer tracker is alive");
4595                if peers.first().is_some_and(|p| p.expired) {
4596                    return peers;
4597                }
4598                tokio::time::sleep(std::time::Duration::from_millis(5)).await;
4599            }
4600        })
4601        .await
4602        .expect("the expiry timer flagged the peer with no netmap in between");
4603
4604        assert_eq!(flagged.len(), 1, "flagged, not dropped");
4605        assert!(flagged[0].underlay_addresses.is_empty());
4606        assert_eq!(flagged[0].derp_region, None);
4607        assert_eq!(
4608            flagged[0].node_key,
4609            ts_keys::node_public_with_bad_old_prefix(peer.node_key)
4610        );
4611    }
4612
4613    /// The recovery path, through the channel that actually carries it: control extends an expired
4614    /// peer's key with a `PeerChange` that restates only `KeyExpiry`. The peer has to come back
4615    /// with its direct candidates and its home DERP, not merely with a usable node key — flagging
4616    /// cleared all three, the patch restates none of them, and a peer that is un-expired but has
4617    /// neither an endpoint nor a DERP home is unroutable until the next full netmap.
4618    #[tokio::test]
4619    async fn an_expiry_only_patch_restores_the_peers_routes() {
4620        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
4621        let now = local_now();
4622        let peer = expiring_peer("eXtEnDeD", 6, Some(now - TimeDelta::hours(1)));
4623
4624        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer.clone()]), now);
4625        assert!(
4626            tracker
4627                .peer_db
4628                .get(&peer.stable_id)
4629                .expect("kept")
4630                .1
4631                .expired,
4632            "flagged on the way in"
4633        );
4634
4635        // Exactly the shape of a `PeerChange` that only extends the key's life.
4636        let patch = ts_control::PeerChange {
4637            id: peer.id,
4638            derp_region: None,
4639            cap: None,
4640            cap_map: None,
4641            underlay_addresses: None,
4642            node_key: None,
4643            key_signature: None,
4644            disco_key: None,
4645            node_key_expiry: Some(now + TimeDelta::days(30)),
4646            online: None,
4647            last_seen: None,
4648        };
4649        tracker.apply_peer_patches(std::slice::from_ref(&patch), now);
4650
4651        let (_id, stored) = tracker.peer_db.get(&peer.stable_id).expect("still a peer");
4652        assert!(!stored.expired, "the extension un-expires the peer");
4653        assert_eq!(stored.node_key, peer.node_key, "its real node key is back");
4654        assert_eq!(
4655            stored.underlay_addresses, peer.underlay_addresses,
4656            "and its direct-path candidates"
4657        );
4658        assert_eq!(
4659            stored.derp_region, peer.derp_region,
4660            "and its home DERP route"
4661        );
4662        assert_eq!(
4663            stored.peerapi_addr(),
4664            peer.peerapi_addr(),
4665            "so a peerAPI dial to it is answerable again"
4666        );
4667    }
4668
4669    /// The lookup a caller holding an older [`Node`] snapshot refreshes it through, end to end
4670    /// through the live actor: `tailscale::Device::send_file` re-reads the peer by stable id so it
4671    /// refuses an expired peer with the reason instead of dialing a broken one and reporting a
4672    /// timeout. Also pins that the query answers *immediately* before the first netmap — queueing
4673    /// it (as [`PeerByName`] does) would park a send behind a netmap that may never arrive.
4674    #[tokio::test]
4675    async fn peer_by_stable_id_answers_with_the_current_flagged_record() {
4676        let env = test_env();
4677        let (_tka_tx, tka_rx) = watch::channel(None);
4678        let live = PeerTracker::spawn((env.clone(), tka_rx));
4679
4680        let peer = expiring_peer("eXpIrEd", 7, Some(local_now() - TimeDelta::hours(1)));
4681
4682        // Before any netmap: an immediate `None`, not a queued reply.
4683        let unknown = tokio::time::timeout(
4684            std::time::Duration::from_secs(5),
4685            live.ask(PeerByStableId {
4686                stable_id: peer.stable_id.clone(),
4687            }),
4688        )
4689        .await
4690        .expect("the query answers without waiting for a netmap")
4691        .expect("peer tracker is alive");
4692        assert_eq!(unknown, None);
4693
4694        env.publish(Arc::new(netmap_with_peers(vec![peer.clone()])))
4695            .await
4696            .expect("publish netmap");
4697        await_peer_count(&live, 1).await;
4698
4699        let current = live
4700            .ask(PeerByStableId {
4701                stable_id: peer.stable_id.clone(),
4702            })
4703            .await
4704            .expect("peer tracker is alive")
4705            .expect("the expired peer is kept, so it is still resolvable by stable id");
4706        assert!(
4707            current.expired,
4708            "the record carries the flag, not the stale state"
4709        );
4710        assert_eq!(
4711            current.peerapi_addr(),
4712            None,
4713            "so a peerAPI dial resolved from it is refused"
4714        );
4715    }
4716
4717    /// `MapResponse.ControlTime` is the reason the comparison is exact rather than approximate: a
4718    /// node whose own clock is hours behind control's must still see a peer as expired.
4719    #[tokio::test]
4720    async fn a_control_time_delta_decides_expiry_against_controls_clock() {
4721        let (mut tracker, _tka_tx) = PeerTracker::for_test(test_env(), None);
4722        let now = local_now();
4723        // Local time says this expires in an hour.
4724        let peer = expiring_peer("sKeWeD", 5, Some(now + TimeDelta::hours(1)));
4725
4726        // Control's clock is two hours ahead of ours, so by control's reckoning it went an hour ago.
4727        tracker
4728            .expiry
4729            .on_control_time(now + TimeDelta::hours(2), now);
4730        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer.clone()]), now);
4731
4732        let (_id, stored) = tracker.peer_db.get(&peer.stable_id).expect("kept");
4733        assert!(
4734            stored.expired,
4735            "expiry is judged against control's clock, not this host's"
4736        );
4737    }
4738}