Skip to content

Quorum: add a from-scratch Kademlia DHT lab (XOR-metric routing)#668

Merged
github-actions[bot] merged 1 commit into
mainfrom
claude/hopeful-keller-ujyp1l
Jul 23, 2026
Merged

Quorum: add a from-scratch Kademlia DHT lab (XOR-metric routing)#668
github-actions[bot] merged 1 commit into
mainfrom
claude/hopeful-keller-ujyp1l

Conversation

@kkd16

@kkd16 kkd16 commented Jul 23, 2026

Copy link
Copy Markdown
Owner

What

Adds Kademlia — the XOR-metric distributed hash table — as a new lab in the Quorum distributed-systems simulator (projects/quorum-distsys-k7r2/). It's the counterpoint to the existing Chord lab and the DHT that actually runs the internet (BitTorrent Mainline, IPFS, Ethereum discv5, Storj). Everything is implemented from scratch on the project's existing deterministic kernel; all changes live in the single project folder.

Chord vs. Kademlia is a genuinely instructive pair:

  • Chord routes clockwise around a ring with a recursive lookup.
  • Kademlia routes down a binary tree by XOR distance with an iterative, α-parallel lookup the initiator drives itself.

Implementation (Maymounkov & Mazières, 2002)

  • XOR metric (xor.ts) — distance(a,b)=a⊕b; bucketIndex = most-significant differing bit (the law 2^i ≤ a⊕b < 2^(i+1) is asserted in a test).
  • k-buckets (routing.ts) — one bucket per bit; the real prefer-old-live-contacts rule: a full bucket pings its least-recently-seen head and evicts it only if it fails to answer (the anti-churn / anti-eclipse bias). Pure data + functions so the kernel can snapshot it.
  • Iterative α-parallel lookup (kademlia.ts) — seed a shortlist from own buckets, keep α=3 FIND_NODE probes in flight to the closest unqueried contacts, fold replies back, stop when the frontier closes on the true k-closest. Each hop moves ≥1 bit closer ⇒ O(log N) rounds.
  • FIND_VALUE / STORE — a real PUT/GET key-value store; a node holding the key short-circuits the search. Contact-learning from every message, bucket-refresh + republish maintenance, crash/restart (volatile routing rebuilt by a self-lookup, durable stored pairs). A dropped lookup reply never evicts a live contact — only a failed liveness ping does, which is what makes the lookup loss-tolerant.
  • Invariants (invariants.ts) — k-bucket well-formedness (always-on safety) plus two eventual gauges; the headline iterative lookup finds the true k-closest re-runs the α-parallel search offline over the frozen tables from every node to every target and demands the exact XOR-nearest k.
  • Binary-trie UI (ui/KademliaTree.tsx, labs/KademliaLab.tsx) — the canonical paper Figure-1: live ids as leaves of a compressed binary trie, and for the selected node its k-buckets drawn as the shaded subtrees hanging off its root-to-leaf path ("bucket i · known/total"), contacts gold-ringed, the lookup target dropped in along its bit-path. A k slider (shrink k to force real multi-hop lookups), a bucket inspector with XOR distances, a stored-pairs view, a key-placement table, and deep links.

Verification

  • 13 new self-tests, suite 165 → 178/178: XOR laws, k-bucket LRU/eviction (both outcomes), closest() ordering, 8-node convergence, live lookups exact at k=4, k=2 partial tables forcing genuine multi-hop yet staying exact, offline-replay matching truth from every node, PUT→GET retrievable from every node, absent-key not-found, heal-after-two-crashes, restart-rejoin, tables converging under 15% message loss, determinism.
  • Drove the built #/kademlia route in headless Chromium: 8-node network converges, all three invariants green (26/26 required buckets populated, 64/64 (node, target) lookups exact), the trie renders the buckets-as-subtrees correctly, no page errors.
  • Full gate green (node scripts/verify-project.mjs quorum-distsys-k7r2 — scope + conformance + lint + build).

Scope

Single project, single folder — every changed file is under projects/quorum-distsys-k7r2/ (Golden Rule). Journal updated with the design, a session-log entry, and a six-item Kademlia backlog for future sessions.

🤖 Generated with Claude Code

https://claude.ai/code/session_019XuHZAjE3TuggDG5k91gL6


Generated by Claude Code

The counterpoint to Chord and the DHT that actually runs the internet
(BitTorrent Mainline, IPFS, Ethereum discv5, Storj). Built the full
Maymounkov-Mazieres (2002) design on the shared kernel:

- XOR metric + k-buckets with the prefer-old-live-contacts LRU rule
  (a full bucket pings its LRU head and evicts only on a failed ping).
- Iterative, alpha-parallel lookup driven by the initiator (contrast
  Chord's recursive forward): seed a shortlist, keep alpha probes in
  flight to the closest unqueried contacts, fold replies back, stop
  when the frontier closes on the true k-closest. O(log N) rounds.
- FIND_VALUE/STORE for a real PUT/GET key-value store; contact
  learning from every message; bucket-refresh + republish maintenance;
  crash/restart (volatile routing rebuilt by a self-lookup, durable
  stored pairs). A dropped lookup reply never evicts a live contact.
- Invariants: k-bucket well-formedness (safety) plus two eventual
  gauges; the headline re-runs the alpha-parallel search offline over
  the frozen tables from every node to every target and demands the
  exact XOR-nearest k.
- Bespoke binary-trie visualiser (the canonical Figure-1): the
  selected node's k-buckets drawn as the shaded subtrees hanging off
  its root-to-leaf path, with a k slider, bucket inspector, stored
  pairs and key-placement panels.
- 13 self-tests (suite 165 -> 178/178), incl. k=2 multi-hop lookups,
  PUT/GET retrieval from every node, heal-after-two-crashes, and
  convergence under 15% message loss. Verified live in headless
  Chromium; full verify-project gate green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019XuHZAjE3TuggDG5k91gL6

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Scope + lint + build passed.

@github-actions
github-actions Bot merged commit ec88554 into main Jul 23, 2026
2 checks passed
@github-actions
github-actions Bot deleted the claude/hopeful-keller-ujyp1l branch July 23, 2026 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants