Skip to main content

dig_dht/
service.rs

1//! [`DhtService`] — the public handle that ties the routing table, provider store, transport, and
2//! iterative lookup into the four operations a DIG Node needs:
3//!
4//! - [`bootstrap`](DhtService::bootstrap) — seed the routing table from known peers (the dig-gossip
5//!   pool / relay introducer) + populate it with a self-lookup.
6//! - [`find_providers`](DhtService::find_providers) — "who holds this content?" → the provider
7//!   records (the node then fetches over the L7 peer RPC).
8//! - [`announce_provider`](DhtService::announce_provider) — "I hold this content" → PUT a provider
9//!   record at the `k` nodes closest to the content key (and locally), and remember to republish it.
10//! - [`find_node`](DhtService::find_node) — the `k` peers closest to a `peer_id` (routing primitive).
11//!
12//! Plus maintenance ([`republish`](DhtService::republish), [`refresh_buckets`](DhtService::refresh_buckets),
13//! [`gc`](DhtService::gc)) and the **serving side** ([`handle_request`](DhtService::handle_request))
14//! that answers inbound DHT RPCs from other nodes.
15//!
16//! ## Serving vs. querying
17//!
18//! A node is both a client and a server of the DHT. [`handle_request`](DhtService::handle_request)
19//! is the server: given an inbound [`DhtRequest`], it reads/writes the local routing table +
20//! provider store and returns the [`DhtResponse`]. The `find_*` / `announce_*` methods are the
21//! client: they run iterative lookups over the [`DhtTransport`]. A dig-node wires `handle_request`
22//! to inbound DHT streams and gives the service a transport that dials outbound.
23
24use std::sync::Arc;
25
26use tokio::sync::Mutex;
27
28use dig_nat::PeerId;
29
30use crate::clock::now_secs;
31use crate::config::DhtConfig;
32use crate::content::ContentId;
33use crate::error::DhtError;
34use crate::key::Key;
35use crate::lookup::{iterative_find, QueryOutcome};
36use crate::provider_store::{ProviderStore, PutOutcome};
37use crate::record::{CandidateAddr, ProviderRecord};
38use crate::routing::{Contact, InsertOutcome, RoutingTable};
39use crate::transport::DhtTransport;
40use crate::wire::{DhtRequest, DhtResponse};
41
42/// A peer to bootstrap the routing table from — its `peer_id` and at least one candidate address.
43/// These come from the node's existing discovery (the dig-gossip peer pool / the relay introducer);
44/// the DHT crate takes them as input and never hard-depends on a live relay itself.
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct BootstrapPeer {
47    /// The bootstrap peer's identity.
48    pub peer_id: PeerId,
49    /// Candidate addresses to reach it.
50    pub addresses: Vec<CandidateAddr>,
51}
52
53impl BootstrapPeer {
54    /// A bootstrap peer with a single direct address.
55    pub fn direct(peer_id: PeerId, host: impl Into<String>, port: u16) -> Self {
56        BootstrapPeer {
57            peer_id,
58            addresses: vec![CandidateAddr::direct(host, port)],
59        }
60    }
61
62    fn to_contact(&self) -> Contact {
63        Contact::new(&self.peer_id, self.addresses.clone())
64    }
65}
66
67/// The DHT service for one node. Cloneable-by-`Arc` internally; wrap in `Arc` to share between the
68/// serving task (inbound RPC) and querying callers.
69pub struct DhtService {
70    local_id: PeerId,
71    /// This node's own candidate addresses — put into provider records it announces so finders can
72    /// reach it.
73    local_addresses: Vec<CandidateAddr>,
74    config: DhtConfig,
75    routing: Arc<Mutex<RoutingTable>>,
76    providers: Arc<Mutex<ProviderStore>>,
77    transport: Arc<dyn DhtTransport>,
78}
79
80impl DhtService {
81    /// Create a service for the node identified by `local_id`, advertising `local_addresses` in the
82    /// provider records it announces, driving RPC over `transport`.
83    pub fn new(
84        local_id: PeerId,
85        local_addresses: Vec<CandidateAddr>,
86        config: DhtConfig,
87        transport: Arc<dyn DhtTransport>,
88    ) -> Self {
89        let routing = RoutingTable::new(&local_id, config.k);
90        let providers = ProviderStore::with_limits(config.provider_store_limits);
91        DhtService {
92            local_id,
93            local_addresses,
94            config,
95            routing: Arc::new(Mutex::new(routing)),
96            providers: Arc::new(Mutex::new(providers)),
97            transport,
98        }
99    }
100
101    /// This node's id.
102    pub fn local_id(&self) -> &PeerId {
103        &self.local_id
104    }
105
106    /// This node's own [`Contact`] (its id + advertised addresses) — the authenticated caller
107    /// identity supplied to the transport as the RPC `from`.
108    fn local_contact(&self) -> Contact {
109        Contact::new(&self.local_id, self.local_addresses.clone())
110    }
111
112    // ---- Bootstrap ---------------------------------------------------------------------------
113
114    /// Seed the routing table from `peers` and populate it by looking up this node's own id (the
115    /// canonical Kademlia bootstrap: a self-lookup fills the buckets around us). Returns the number
116    /// of distinct peers now known.
117    ///
118    /// Safe to call repeatedly (on reconnect / when new bootstrap peers arrive) — it merges, never
119    /// resets.
120    pub async fn bootstrap(&self, peers: &[BootstrapPeer]) -> Result<usize, DhtError> {
121        {
122            let mut rt = self.routing.lock().await;
123            for p in peers {
124                let _ = rt.insert(p.to_contact());
125            }
126        }
127        // Self-lookup: find the nodes closest to us to fill our buckets.
128        let self_key = Key::from_peer_id(&self.local_id);
129        let seeds: Vec<Contact> = peers.iter().map(|p| p.to_contact()).collect();
130        let result = self.run_lookup(self_key, seeds, false).await;
131        self.absorb_contacts(&result.closest).await;
132        Ok(self.routing.lock().await.len())
133    }
134
135    /// Add a single live peer to the routing table as it connects (e.g. a `dig-gossip`
136    /// `PoolEvent::PeerAdded`), WITHOUT the network round-trip [`bootstrap`](Self::bootstrap) does.
137    ///
138    /// This is the LIVE seam the one-shot pre-connect bootstrap cannot cover: in a freshly-formed
139    /// network the pool is empty when `bootstrap` runs, so routing stays empty and `find_providers`
140    /// finds nobody. Feeding each connected peer here populates routing as the pool fills, which is
141    /// what makes cross-node discovery work (#1574). Idempotent — re-adding a known peer merges its
142    /// address(es) via the routing table's insert policy; adding this node's own id is a no-op.
143    pub async fn add_peer(&self, peer_id: &PeerId, addresses: Vec<CandidateAddr>) {
144        let contact = Contact::new(peer_id, addresses);
145        let _ = self.routing.lock().await.insert(contact);
146    }
147
148    /// Remove a peer from the routing table as it leaves (a `dig-gossip` `PoolEvent::PeerRemoved`),
149    /// keeping routing accurate so lookups don't seed from a dead contact. Returns whether it was
150    /// present. `peer_id_hex` is the 64-char hex id (as carried on `Contact::provider_peer_id` /
151    /// [`PeerId::to_hex`]).
152    pub async fn remove_peer(&self, peer_id_hex: &str) -> bool {
153        self.routing.lock().await.remove(peer_id_hex)
154    }
155
156    // ---- Client operations -------------------------------------------------------------------
157
158    /// Find the `k` peers closest to `peer_id` (the routing primitive). Runs an iterative
159    /// `find_node` lookup and returns the converged closest contacts.
160    pub async fn find_node(&self, peer_id: &PeerId) -> Result<Vec<Contact>, DhtError> {
161        let target = Key::from_peer_id(peer_id);
162        let seeds = self.seed_contacts(&target).await;
163        if seeds.is_empty() {
164            return Err(DhtError::NoPeers);
165        }
166        let result = self.run_lookup(target, seeds, false).await;
167        self.absorb_contacts(&result.closest).await;
168        Ok(result.closest)
169    }
170
171    /// Find the providers of `content` — the peers holding it. Runs an iterative `find_providers`
172    /// lookup toward the content key, returning every live provider record collected (deduped by
173    /// provider). The node then connects to those providers over dig-nat and fetches via the L7 peer
174    /// RPC.
175    ///
176    /// Returns an empty vec (not an error) when the content simply has no known providers; returns
177    /// [`DhtError::NoPeers`] only when there is no one to ask (empty routing table + no bootstrap).
178    pub async fn find_providers(
179        &self,
180        content: &ContentId,
181    ) -> Result<Vec<ProviderRecord>, DhtError> {
182        let target = content.to_key();
183
184        // Local short-circuit: if we already hold providers for this key, include them.
185        let now = now_secs();
186        let mut local = self.providers.lock().await.get(&target.to_hex(), now);
187
188        let seeds = self.seed_contacts(&target).await;
189        if seeds.is_empty() {
190            // No peers to ask — return whatever we hold locally (possibly empty).
191            return Ok(local);
192        }
193        let result = self.run_lookup(target, seeds, true).await;
194        self.absorb_contacts(&result.closest).await;
195
196        // Merge local + discovered, dedup by provider, drop expired. Discovered records come
197        // straight off the wire from other peers' responses, bypassing `ProviderRecord::new`'s
198        // address cap — capped here before handing them back to our caller (SPEC §5.5, §14).
199        // Records for a key we did not query were already discarded at the wire boundary in
200        // `run_lookup`'s query closure (SPEC §6.7), so every record here is for `target`.
201        let mut discovered = result.providers;
202        for r in &mut discovered {
203            crate::record::sort_and_cap_addresses(&mut r.addresses);
204        }
205        local.extend(discovered);
206        let now = now_secs();
207        let mut seen = std::collections::HashSet::new();
208        local.retain(|r| !r.is_expired(now) && seen.insert(r.provider_peer_id.clone()));
209        Ok(local)
210    }
211
212    /// Announce that THIS node holds `content`: build a provider record (this node's `peer_id` +
213    /// addresses, expiring at `now + provider_ttl`), store it locally, remember to republish it, and
214    /// PUT it at the `k` nodes closest to the content key. Returns how many peers accepted the PUT.
215    ///
216    /// Called when the node's inventory gains content (a new capsule/root/resource it now serves).
217    pub async fn announce_provider(&self, content: &ContentId) -> Result<usize, DhtError> {
218        let target = content.to_key();
219        let record = self.build_local_record(&target);
220
221        // Store locally + remember for republish.
222        {
223            let mut ps = self.providers.lock().await;
224            ps.put(record.clone());
225            ps.mark_announced(target.to_hex());
226        }
227
228        // PUT at the k closest peers we can find.
229        let seeds = self.seed_contacts(&target).await;
230        if seeds.is_empty() {
231            // No peers yet — the local record stands; republish will re-attempt once bootstrapped.
232            return Ok(0);
233        }
234        let result = self.run_lookup(target, seeds, false).await;
235        self.absorb_contacts(&result.closest).await;
236        Ok(self.put_record_at(&result.closest, &record).await)
237    }
238
239    /// Stop announcing `content` (the node no longer holds it). The record ages out of the DHT via
240    /// TTL; we just stop republishing it. Returns whether it was being announced.
241    ///
242    /// This is the **passive** withdraw: it leaves this node's own local provider record in place
243    /// (it only expires with TTL) and merely stops re-publishing it, so a `find_providers` on this
244    /// node may still return self until the local record's TTL elapses. For an **immediate**
245    /// own-retract — the local-state half of the #1423 evict+retract step — use
246    /// [`retract_own_provider`](Self::retract_own_provider).
247    pub async fn withdraw_provider(&self, content: &ContentId) -> bool {
248        let key = content.to_key().to_hex();
249        self.providers.lock().await.unmark_announced(&key)
250    }
251
252    // ---- Real-time holdings API (#1394 / #1423) ----------------------------------------------
253
254    /// Ingest a provider record for a THIRD-PARTY holder that the caller has ALREADY verified was
255    /// signed by `record.provider_peer_id` — the inbound-**add** half of the real-time holdings map
256    /// (SPEC §6.5). Returns the store admission outcome.
257    ///
258    /// This is the authenticated push path a node's announce receiver calls after verifying a
259    /// signed `HoldingsAnnounce` (dig-gossip opcode 222): the holder's signature has replaced mTLS
260    /// attribution as the proof of who provides the content, so — unlike the serving-side
261    /// `add_provider` (§6.4) — this method **bypasses the mTLS self-announce identity check** (the
262    /// caller, not the DHT, established authenticity). dig-dht itself stays crypto-free (SPEC §15):
263    /// it NEVER verifies a signature; passing an unverified record here is a caller bug that
264    /// poisons the local provider set.
265    ///
266    /// Every other admission guard still applies exactly as for `add_provider`: the address list is
267    /// capped ([`MAX_ADDRESSES_PER_RECORD`](crate::MAX_ADDRESSES_PER_RECORD)), `expires_at` is
268    /// clamped to `min(record.expires_at, now + provider_ttl)` (§6.2), and the per-key / global
269    /// admission caps (§6.3) are enforced — an over-capacity ingest returns
270    /// [`PutOutcome::RejectedOverCapacity`] and stores nothing. On acceptance the holder is folded
271    /// into the routing table so this node can reach it.
272    pub async fn ingest_verified_provider(&self, record: ProviderRecord) -> PutOutcome {
273        self.admit_verified_record(record).await
274    }
275
276    /// Remove exactly the local provider record for `(content_key, provider_peer_id)` — the
277    /// inbound-**retract** half of the real-time holdings map (SPEC §6.6). Returns whether a record
278    /// was removed.
279    ///
280    /// `content_key` and `provider_peer_id` are the 64-hex forms as they appear on a
281    /// [`ProviderRecord`] (`content` → `content.to_key().to_hex()`; the holder's `peer_id` hex).
282    /// The caller MUST have verified the retract was signed by that same `provider_peer_id`
283    /// (authenticated retract): a retract signed by one holder removes ONLY that holder's record and
284    /// can never evict another provider of the same key (censorship-resistance, §6.6). dig-dht does
285    /// not verify the signature (SPEC §15) — that is the caller's responsibility.
286    pub async fn remove_provider_record(&self, content_key: &str, provider_peer_id: &str) -> bool {
287        self.providers
288            .lock()
289            .await
290            .remove(content_key, provider_peer_id)
291    }
292
293    /// Actively retract THIS node's own provider record for `content`: remove the local record AND
294    /// stop republishing it, so `find_providers` on this node stops returning self as a holder
295    /// immediately (SPEC §6.6). Returns whether this node was providing the content (a local record
296    /// existed or the key was being announced).
297    ///
298    /// This is the local-state half of the #1423 atomic **evict + retract** step (on an LRU cache
299    /// eviction the node no longer serves the content). Unlike the passive
300    /// [`withdraw_provider`](Self::withdraw_provider) (which leaves the local record to expire via
301    /// TTL), this deletes it now. The copies previously PUT at the `k` closest peers are NOT deleted
302    /// by this call — they age out via TTL, or are removed sooner when dig-node floods the signed
303    /// retract announce and each recipient calls
304    /// [`remove_provider_record`](Self::remove_provider_record).
305    pub async fn retract_own_provider(&self, content: &ContentId) -> bool {
306        let key = content.to_key().to_hex();
307        let self_id = self.local_id.to_hex();
308        let mut ps = self.providers.lock().await;
309        let removed_record = ps.remove(&key, &self_id);
310        let was_announced = ps.unmark_announced(&key);
311        removed_record || was_announced
312    }
313
314    /// The `peer_id`s of the peers that hold `content` — a thin, address-free convenience over
315    /// [`find_providers`](Self::find_providers) for callers that only need "which peers hold X"
316    /// (e.g. an RPC holder-set query) and do not dial the holders themselves.
317    ///
318    /// `find_providers` remains the PRIMARY API: it returns full [`ProviderRecord`]s with candidate
319    /// addresses, which dig-download needs to actually connect and fetch. This method runs the same
320    /// distributed iterative lookup and simply projects each record to its holder `peer_id`
321    /// (records with a malformed peer id are skipped; the set is already deduped by provider).
322    pub async fn holders_of(&self, content: &ContentId) -> Result<Vec<PeerId>, DhtError> {
323        let records = self.find_providers(content).await?;
324        Ok(records
325            .iter()
326            .filter_map(|r| r.provider_peer_id())
327            .collect())
328    }
329
330    // ---- Maintenance -------------------------------------------------------------------------
331
332    /// Republish every content key this node still announces — re-runs the announce PUT so provider
333    /// records never expire while the node is online. Call on the [`DhtConfig::republish_interval`].
334    /// Returns the number of content keys republished.
335    pub async fn republish(&self) -> usize {
336        let keys = self.providers.lock().await.local_announcements();
337        let count = keys.len();
338        for hex in keys {
339            let Some(bytes) = hex64_to_bytes(&hex) else {
340                continue;
341            };
342            let target = Key::from_bytes(bytes);
343            let record = self.build_local_record(&target);
344            self.providers.lock().await.put(record.clone());
345            let seeds = self.seed_contacts(&target).await;
346            if !seeds.is_empty() {
347                let result = self.run_lookup(target, seeds, false).await;
348                self.absorb_contacts(&result.closest).await;
349                self.put_record_at(&result.closest, &record).await;
350            }
351        }
352        count
353    }
354
355    /// Refresh populated buckets by looking up a random key in each — keeps the routing table fresh
356    /// as peers churn. Call on the [`DhtConfig::refresh_interval`]. Returns the number of buckets
357    /// refreshed.
358    pub async fn refresh_buckets(&self) -> usize {
359        let indices = self.routing.lock().await.non_empty_bucket_indices();
360        let count = indices.len();
361        for idx in indices {
362            let target = self.random_key_in_bucket(idx);
363            let seeds = self.seed_contacts(&target).await;
364            if !seeds.is_empty() {
365                let result = self.run_lookup(target, seeds, false).await;
366                self.absorb_contacts(&result.closest).await;
367            }
368        }
369        count
370    }
371
372    /// Drop expired provider records. Call periodically (piggy-backs on republish/refresh). Returns
373    /// the number of records removed.
374    pub async fn gc(&self) -> usize {
375        self.providers.lock().await.gc(now_secs())
376    }
377
378    /// Ping a peer for liveness; on failure, evict it from the routing table. Used by the
379    /// ping-and-replace maintenance when a bucket is full. Returns whether the peer is alive.
380    pub async fn ping(&self, peer: &Contact) -> bool {
381        let nonce = rand::random::<u64>();
382        let from = self.local_contact();
383        match self
384            .transport
385            .rpc(&from, peer, &DhtRequest::Ping { nonce })
386            .await
387        {
388            Ok(DhtResponse::Pong { nonce: got }) if got == nonce => true,
389            _ => {
390                self.routing.lock().await.remove(&peer.peer_id);
391                false
392            }
393        }
394    }
395
396    // ---- Serving side (inbound RPC) ----------------------------------------------------------
397
398    /// Answer an inbound DHT request from another node, without a known caller identity. Prefer
399    /// [`handle_request_from`](Self::handle_request_from) on an authenticated transport (it lets the
400    /// responder learn the caller and populate its routing table bidirectionally, the way Kademlia
401    /// tables fill).
402    pub async fn handle_request(&self, request: DhtRequest) -> DhtResponse {
403        self.handle_request_from(None, request).await
404    }
405
406    /// Answer an inbound DHT request, folding the **authenticated caller** into the routing table.
407    ///
408    /// This is the server half — a dig-node wires it to inbound DHT streams, passing the caller's
409    /// mTLS-verified [`Contact`] as `caller`. Learning the caller from every inbound RPC is how a
410    /// Kademlia node discovers peers *without* an explicit announce: a node that talks to you becomes
411    /// a candidate in your table. The caller MUST come from the authenticated transport (the mTLS
412    /// `peer_id`), never from the request body — identity is not self-asserted.
413    ///
414    /// It reads/writes only local state (routing table + provider store) and never makes outbound
415    /// RPCs, so it cannot recurse or block on the network.
416    pub async fn handle_request_from(
417        &self,
418        caller: Option<Contact>,
419        request: DhtRequest,
420    ) -> DhtResponse {
421        // The authenticated caller's peer_id (if any), kept for the AddProvider self-announce check
422        // below — taken BEFORE the caller Contact is (conditionally) moved into the routing table.
423        let caller_peer_id = caller.as_ref().map(|c| c.peer_id.clone());
424
425        // Learn the (authenticated) caller — every inbound RPC is evidence the caller is alive.
426        // Cap its address list at the boundary (SPEC §5.5, §14): a `Contact` decoded off the wire
427        // bypasses `Contact::new`'s cap entirely (its fields are public), so an uncapped caller
428        // address list would otherwise be folded straight into our routing table and later re-served
429        // to every peer that queries us.
430        if let Some(mut c) = caller {
431            if c.peer_id != self.local_id.to_hex() {
432                crate::record::sort_and_cap_addresses(&mut c.addresses);
433                let _ = self.routing.lock().await.insert(c);
434            }
435        }
436        match request {
437            DhtRequest::Ping { nonce } => DhtResponse::Pong { nonce },
438            DhtRequest::FindNode { target } => {
439                let Some(key) = parse_key(&target) else {
440                    return DhtResponse::Error {
441                        code: 2,
442                        message: "bad target key".into(),
443                    };
444                };
445                let nodes = self.routing.lock().await.closest(&key);
446                DhtResponse::Nodes { nodes }
447            }
448            DhtRequest::FindProviders { content_key } => {
449                let Some(key) = parse_key(&content_key) else {
450                    return DhtResponse::Error {
451                        code: 2,
452                        message: "bad content key".into(),
453                    };
454                };
455                let now = now_secs();
456                let providers = self.providers.lock().await.get(&key.to_hex(), now);
457                let closer = self.routing.lock().await.closest(&key);
458                DhtResponse::Providers { providers, closer }
459            }
460            DhtRequest::AddProvider { record } => {
461                // Self-announce check (SPEC §6.4, §14): when the caller identity is known (an
462                // authenticated transport), the record's provider_peer_id MUST be the caller itself.
463                // ProviderRecord carries no signature, so without this check any authenticated caller
464                // could announce an arbitrary THIRD-PARTY peer_id as a provider of arbitrary content
465                // at attacker-chosen addresses — provider-set poisoning. A caller we cannot identify
466                // (`handle_request`, no transport-supplied identity) cannot be checked and is let
467                // through unchanged — that path already deviates from the mTLS-authenticated model.
468                if let Some(caller_id) = &caller_peer_id {
469                    if *caller_id != record.provider_peer_id {
470                        return DhtResponse::Error {
471                            code: 4,
472                            message:
473                                "add_provider: provider_peer_id must match the authenticated caller"
474                                    .into(),
475                        };
476                    }
477                }
478
479                // Address-cap, TTL-clamp, admission-control, and (on acceptance) fold into routing —
480                // the shared verified-record admission pipeline (SPEC §6.3, §14).
481                match self.admit_verified_record(record).await {
482                    PutOutcome::Accepted => DhtResponse::AddProviderOk,
483                    PutOutcome::RejectedOverCapacity => DhtResponse::Error {
484                        code: 3,
485                        message: "provider store over capacity".into(),
486                    },
487                }
488            }
489        }
490    }
491
492    // ---- Internals ---------------------------------------------------------------------------
493
494    /// Admit a provider record whose provider attribution is ALREADY established — either the
495    /// serving-side mTLS self-announce check passed (`handle_request_from`'s `AddProvider` arm) or
496    /// the caller pre-verified the holder signature ([`ingest_verified_provider`]). This is the one
497    /// admission pipeline both paths share (SPEC §6.3, §14), in order:
498    ///
499    /// 1. **Cap the address list** at [`MAX_ADDRESSES_PER_RECORD`](crate::MAX_ADDRESSES_PER_RECORD)
500    ///    — a record decoded off the wire bypasses `ProviderRecord::new`'s cap (its fields are
501    ///    public), so an attacker could otherwise pack thousands of addresses into one record.
502    /// 2. **Clamp `expires_at`** to `now + provider_ttl` — an inbound record is never trusted to
503    ///    self-report its expiry; without this a record naming `u64::MAX` would never GC.
504    /// 3. **Admission-control** via [`ProviderStore::put`], enforcing the per-key + global caps so a
505    ///    flood cannot grow the store without bound.
506    /// 4. On [`PutOutcome::Accepted`], **fold the holder into the routing table** (its addresses let
507    ///    us reach it). A rejected record folds nothing.
508    ///
509    /// [`ingest_verified_provider`]: Self::ingest_verified_provider
510    async fn admit_verified_record(&self, mut record: ProviderRecord) -> PutOutcome {
511        crate::record::sort_and_cap_addresses(&mut record.addresses);
512
513        let now = now_secs();
514        let clamp_ceiling = now.saturating_add(self.config.provider_ttl_secs());
515        record.expires_at = record.expires_at.min(clamp_ceiling);
516
517        // `put_at` with the SAME instant the clamp used, so admission cannot reclaim a slot it
518        // considers expired while the clamp considered it live (or vice versa).
519        let outcome = self.providers.lock().await.put_at(record.clone(), now);
520        if outcome == PutOutcome::Accepted {
521            if let Some(pid) = record.provider_peer_id() {
522                let contact = Contact::new(&pid, record.addresses.clone());
523                let _ = self.routing.lock().await.insert(contact);
524            }
525        }
526        outcome
527    }
528
529    /// Build a provider record for content key `target` naming THIS node, expiring at
530    /// `now + provider_ttl`.
531    fn build_local_record(&self, target: &Key) -> ProviderRecord {
532        let expires_at = now_secs().saturating_add(self.config.provider_ttl_secs());
533        ProviderRecord::new(
534            target,
535            &self.local_id,
536            self.local_addresses.clone(),
537            expires_at,
538        )
539    }
540
541    /// The seed set for a lookup toward `target`: the closest contacts we currently know.
542    async fn seed_contacts(&self, target: &Key) -> Vec<Contact> {
543        self.routing.lock().await.closest(target)
544    }
545
546    /// Run an iterative lookup toward `target` from `seeds`, querying peers over the transport. Each
547    /// peer is asked `find_providers` (which also returns closer contacts), so ONE query kind serves
548    /// both node- and provider-lookups; `stop_on_providers` controls early exit.
549    async fn run_lookup(
550        &self,
551        target: Key,
552        seeds: Vec<Contact>,
553        stop_on_providers: bool,
554    ) -> crate::lookup::LookupResult {
555        let transport = self.transport.clone();
556        let content_key = target.to_hex();
557        let from = self.local_contact();
558        let query = move |contact: Contact| {
559            let transport = transport.clone();
560            let content_key = content_key.clone();
561            let from = from.clone();
562            async move {
563                let req = DhtRequest::FindProviders {
564                    content_key: content_key.clone(),
565                };
566                match transport.rpc(&from, &contact, &req).await {
567                    Ok(DhtResponse::Providers {
568                        mut providers,
569                        closer,
570                    }) => {
571                        // Answer-to-question binding (SPEC §6.7, §14): keep only records for the
572                        // key we actually asked about. A responder is free to say ANYTHING here —
573                        // `ProviderRecord` carries no signature and the peer is not the record's
574                        // subject — so without this equality check any peer on the lookup path
575                        // could stamp arbitrary provider peer_ids and address hints onto records
576                        // for keys the finder never queried, and the finder would return them to
577                        // its caller as dial targets (dial fan-out / wasted-dial DoS, and a
578                        // spirit-defeat of the #1490 amplification bound).
579                        //
580                        // Filtering HERE, at the wire boundary, rather than at the final merge is
581                        // load-bearing: the lookup's `stop_on_providers` early exit fires as soon
582                        // as any provider is collected, so a mismatched record counted as "found"
583                        // would end the walk before it reached a real holder — discovery
584                        // censorship. Nothing downstream of this point sees an off-key record.
585                        providers.retain(|r| r.content_key == content_key);
586                        Ok(QueryOutcome { closer, providers })
587                    }
588                    Ok(DhtResponse::Nodes { nodes }) => Ok(QueryOutcome {
589                        closer: nodes,
590                        providers: vec![],
591                    }),
592                    _ => Err(()),
593                }
594            }
595        };
596        iterative_find(
597            target,
598            seeds,
599            self.config.k,
600            self.config.alpha,
601            stop_on_providers,
602            query,
603        )
604        .await
605    }
606
607    /// Fold discovered contacts back into the routing table (skipping ourselves). Applies the LRS
608    /// insert policy; a full bucket's [`InsertOutcome::Full`] is left for the ping-and-replace
609    /// maintenance (we do not ping inline to keep lookups fast).
610    ///
611    /// `contacts` come straight off the wire (a peer's `find_node`/`find_providers` response) and
612    /// so bypass [`Contact::new`]'s address cap (its fields are public) — this is another
613    /// untrusted-input boundary (SPEC §5.5, §14), capped here before insertion.
614    async fn absorb_contacts(&self, contacts: &[Contact]) {
615        let mut rt = self.routing.lock().await;
616        for c in contacts {
617            let mut c = c.clone();
618            crate::record::sort_and_cap_addresses(&mut c.addresses);
619            match rt.insert(c) {
620                InsertOutcome::Inserted => {}
621                InsertOutcome::Full { .. } => {
622                    // Bucket full — leave for ping-and-replace; do not block the lookup on a ping.
623                }
624            }
625        }
626    }
627
628    /// PUT `record` at each of `peers` via `add_provider`, counting acceptances. A peer that errors
629    /// is skipped (best-effort replication — the record survives at the peers that accepted + locally).
630    async fn put_record_at(&self, peers: &[Contact], record: &ProviderRecord) -> usize {
631        let req = DhtRequest::AddProvider {
632            record: record.clone(),
633        };
634        let from = self.local_contact();
635        let mut accepted = 0;
636        for p in peers {
637            if p.peer_id == self.local_id.to_hex() {
638                continue; // already stored locally
639            }
640            if let Ok(DhtResponse::AddProviderOk) = self.transport.rpc(&from, p, &req).await {
641                accepted += 1;
642            }
643        }
644        accepted
645    }
646
647    /// A random key whose distance from this node falls in bucket `idx` (so a refresh lookup targets
648    /// that bucket's region). Sets the bit at position `255 - idx` and randomizes the lower bits.
649    fn random_key_in_bucket(&self, idx: usize) -> Key {
650        let local = *self.local_id.as_bytes();
651        let mut distance = [0u8; 32];
652        let bit = 255 - idx; // MSB-set position for this bucket
653        let byte = bit / 8;
654        let bit_in_byte = 7 - (bit % 8);
655        distance[byte] = 1 << bit_in_byte;
656        // Randomize lower-significant bits so successive refreshes vary the target.
657        for b in distance.iter_mut().skip(byte + 1) {
658            *b = rand::random::<u8>();
659        }
660        let mut target = [0u8; 32];
661        for i in 0..32 {
662            target[i] = local[i] ^ distance[i];
663        }
664        Key::from_bytes(target)
665    }
666
667    /// The contacts currently in this node's routing table closest to `target` (diagnostic /
668    /// introspection — the peers this node knows without any network round-trip).
669    pub async fn known_closest(&self, target: &Key) -> Vec<Contact> {
670        self.routing.lock().await.closest(target)
671    }
672
673    /// The number of peers currently in this node's routing table (diagnostic / metrics).
674    pub async fn routing_len(&self) -> usize {
675        self.routing.lock().await.len()
676    }
677}
678
679/// Current wall-clock Unix seconds (saturating to 0 before the epoch), for provider TTLs.
680/// Parse a 64-hex string into a [`Key`] (used on the serving side for wire targets).
681fn parse_key(hex: &str) -> Option<Key> {
682    hex64_to_bytes(hex).map(Key::from_bytes)
683}
684
685/// Decode a 64-char hex string to 32 bytes.
686fn hex64_to_bytes(hex: &str) -> Option<[u8; 32]> {
687    if hex.len() != 64 {
688        return None;
689    }
690    let mut out = [0u8; 32];
691    let bytes = hex.as_bytes();
692    for (i, chunk) in bytes.chunks(2).enumerate() {
693        let hi = (chunk[0] as char).to_digit(16)?;
694        let lo = (chunk[1] as char).to_digit(16)?;
695        out[i] = ((hi << 4) | lo) as u8;
696    }
697    Some(out)
698}
699
700#[cfg(test)]
701mod tests {
702    use super::*;
703
704    fn key_hex_round_trips() {
705        // sanity for the local hex helper
706    }
707
708    #[test]
709    fn hex64_round_trip() {
710        let bytes = [0xABu8; 32];
711        let hex = Key::from_bytes(bytes).to_hex();
712        assert_eq!(hex64_to_bytes(&hex).unwrap(), bytes);
713        assert!(hex64_to_bytes("short").is_none());
714        assert!(hex64_to_bytes(&"zz".repeat(32)).is_none());
715        key_hex_round_trips();
716    }
717
718    #[test]
719    fn parse_key_rejects_bad_hex() {
720        assert!(parse_key("nothex").is_none());
721        assert!(parse_key(&"00".repeat(32)).is_some());
722    }
723}