Expand description
The iterative Kademlia lookup: converge on the k closest peers to a target key by repeatedly
querying the α closest un-queried peers we know, folding their returned closer contacts back
into the shortlist, until no closer peer can be found.
This is the heart of a Kademlia DHT. iterative_find drives it:
- Seed a shortlist with the closest contacts we already know (routing table + bootstrap).
- Each round, pick the
αclosest un-queried contacts and query them in parallel with the suppliedqueryclosure (afind_nodefor node lookup, or afind_providersfor provider lookup — the closure returns both closer contacts AND any providers it collected). - Merge every returned contact into the shortlist (dedup by
peer_id), keeping it sorted by XOR distance to the target and capped so it stays bounded. - Stop when a full round of the
kclosest have all been queried and none produced a strictly closer un-queried contact — the shortlist has converged. Return thekclosest and any providers collected.
The closure abstraction means node-lookup and provider-lookup share ONE convergence engine; only
what each query returns differs. Transport failures to individual peers are non-fatal — a peer
that errors is simply marked queried and the walk continues (the service’s query closure maps a
transport error to Err(())).
Each round’s α-sized batch is queried truly concurrently: every peer’s RPC is
tokio::spawned as its own task, so all α requests are in flight at once rather than
awaited one at a time. A round of α peers that each stall to the transport timeout therefore
costs about one rpc_timeout, not α × rpc_timeout.
Structs§
- Lookup
Result - The result of a completed iterative lookup.
- Query
Outcome - The outcome of querying one peer during a lookup: the closer contacts it knows, and any provider records it holds for the target (empty for a pure node lookup).
Functions§
- iterative_
find - Drive an iterative lookup toward
target, seeded withseeds, querying at mostalphapeers per round and converging on thekclosest.