feat(kad-dht): add RoutingTableDiagnostics for routing health inspection#1332
Open
bhuvan-somisetty wants to merge 1 commit into
Open
Conversation
When a KadDHT node misbehaves — slow lookups, unreachable keys, failed
bootstraps — operators previously had no visibility into why. This commit
adds a first-class diagnostic surface that answers the core questions:
• Which k-buckets are under-populated or empty?
• Where are the keyspace coverage gaps?
• How fresh are my known peers? (fresh / aging / stale / very stale)
• What is the overall routing-table health as a single 0–100 score?
Changes:
libp2p/kad_dht/diagnostics.py
New RoutingTableDiagnostics class (read-only analyser).
Produces a RoutingTableReport with BucketStat list, CoverageGap list,
FreshnessDistribution, composite health score, and human-readable summary.
libp2p/kad_dht/routing_table.py
Add RoutingTable.get_diagnostics() convenience factory.
libp2p/kad_dht/kad_dht.py
Add KadDHT.get_diagnostics() convenience factory.
libp2p/kad_dht/__init__.py
Export all new public types.
tests/core/kad_dht/test_routing_table_diagnostics.py
27 unit tests; fully offline (mock host, no network required).
examples/kademlia/routing_table_diagnostics.py
Two-node demo that prints a full report after bootstrapping.
Usage:
report = dht.get_diagnostics().analyse()
print(report.summary())
print(f"Health score: {report.health_score}/100")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this solve?
When a KadDHT node starts misbehaving - slow lookups, failed bootstraps, keys that can never be found operators are flying blind. The routing table is a black box. The only way to debug it today is to sprinkle
printstatements through the Kademlia internals or stare at raw bucket lists.This PR adds a first-class diagnostic surface so you can answer the real questions in seconds, not hours:
What was added
libp2p/kad_dht/diagnostics.py- core engineA new
RoutingTableDiagnosticsclass that analyses a live routing table and produces aRoutingTableReport. The analyser is read-only - it never touches the routing table state.Sample output:
Health score breakdown (0–100, composite):
The weighting reflects Kademlia reality: a full bucket closest to the local node is worth far more than a full bucket at the far end of the keyspace.
Convenience entry points
Public types exported from
libp2p.kad_dhtTests
tests/core/kad_dht/test_routing_table_diagnostics.py- 27 unit tests, fully offline (mock host, no network required). Covers:BucketStatproperties (is_full,is_empty,health)FreshnessDistributionratios and totalsRoutingTableDiagnosticswith empty tables, single-peer tables, and multi-bucket tablesRoutingTableReport.summary()outputExample
examples/kademlia/routing_table_diagnostics.py- two-node runnable demo:Non-goals / out of scope
Checklist
libp2p/kad_dht/diagnostics.pywith full docstringsRoutingTable.get_diagnostics()convenience factoryKadDHT.get_diagnostics()convenience factorylibp2p/kad_dht/__init__.pyexamples/kademlia/