Skip to content

feat(service-disco): routing table manager - #2274

Merged
richard-ramos merged 10 commits into
masterfrom
feat/service-disco/service-routing-tables
Apr 15, 2026
Merged

richard-ramos merged 10 commits into
masterfrom
feat/service-disco/service-routing-tables

Conversation

@gmelodie

@gmelodie gmelodie commented Apr 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds ServiceRoutingTableManager, enabling per-service Kademlia routing tables seeded from the main DHT, with service status tracking and metrics.


Changes

  • service_routing_tables.nim

    • ServiceStatus: Interest, Provided, Both

    • ServiceRoutingTableManager:

      • tables: per-service RoutingTable
      • serviceStatus: tracks participation
    • new() — init manager

    • addService(...)

      • Creates + seeds table from main routing table
      • Handles status transitions
      • Updates metrics
    • removeService(...)

      • Removes or downgrades status
      • Updates metrics
    • getTable(...) → Opt[RoutingTable]

    • insertPeer(...) — inserts peer, increments metric

    • hasService(...)

    • refreshAllTables(...) — refresh via kad.refreshTable

    • count(), serviceIds(), clear()

  • Metrics:

    • Table count, total peers, insertions

Affected Areas

  • Peer Discovery

    • Adds per-service routing tables
  • Protocol Logic

    • Extends Kademlia with service-level segmentation

Compatibility & Downstream Validation

  • Fully additive

  • No changes to existing APIs or wire formats

  • Nimbus / Waku / Codex: no impact


Impact on Library Users

  • New:

    • ServiceStatus, ServiceRoutingTableManager
  • New APIs:

    • addService, removeService, getTable, insertPeer, refreshAllTables

Risk Assessment

  • Extra memory (per-service tables)
    Risk: low

  • Sequential refresh may be slow
    Risk: low

  • Silent no-op on missing service in insertPeer
    Risk: low


References

@gmelodie gmelodie self-assigned this Apr 14, 2026
@gmelodie
gmelodie force-pushed the feat/service-disco/service-routing-tables branch 2 times, most recently from 83e1db0 to 0b74e2c Compare April 14, 2026 14:40
@gmelodie
gmelodie changed the base branch from master to feat/service-disco/iptree-signatures April 14, 2026 15:06
@gmelodie
gmelodie marked this pull request as ready for review April 14, 2026 15:09
@gmelodie
gmelodie requested a review from a team as a code owner April 14, 2026 15:09
@gmelodie
gmelodie requested review from richard-ramos and vladopajic and removed request for a team April 14, 2026 15:09
@codecov-commenter

codecov-commenter commented Apr 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.60000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.41%. Comparing base (b11c330) to head (423c403).

Files with missing lines Patch % Lines
...tocols/service_discovery/routing_table_manager.nim 97.50% 2 Missing ⚠️
libp2p/protocols/kademlia/find.nim 96.42% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2274      +/-   ##
==========================================
+ Coverage   72.31%   72.41%   +0.09%     
==========================================
  Files         163      164       +1     
  Lines       20577    20689     +112     
  Branches       18       20       +2     
==========================================
+ Hits        14881    14982     +101     
- Misses       5696     5707      +11     
Files with missing lines Coverage Δ
libp2p/protocols/kademlia.nim 77.77% <100.00%> (+0.66%) ⬆️
libp2p/protocols/kademlia/routing_table.nim 95.41% <100.00%> (+0.36%) ⬆️
libp2p/protocols/service_discovery/types.nim 17.77% <ø> (ø)
libp2p/utils/tablekey.nim 100.00% <ø> (ø)
libp2p/protocols/kademlia/find.nim 67.80% <96.42%> (+0.86%) ⬆️
...tocols/service_discovery/routing_table_manager.nim 97.53% <97.50%> (ø)

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds per-service Kademlia routing tables for the service-discovery protocol via a new ServiceRoutingTableManager, along with service-specific metrics and unit tests. This also refactors Kademlia’s bucket refresh logic into a reusable refreshTable() proc so non-main routing tables can be refreshed.

Changes:

  • Introduces ServiceRoutingTableManager + ServiceStatus to manage per-service RoutingTables seeded from the main table.
  • Adds service_discovery_metrics.nim metrics for service-discovery messaging, registrar cache, and service routing tables.
  • Refactors KadDHT.bootstrap() to delegate to a new KadDHT.refreshTable(rtable, ...) and adds unit tests for both routing tables and the manager.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
libp2p/protocols/service_discovery/routing_table_manager.nim New manager for per-service routing tables + refresh integration + metrics updates.
libp2p/protocols/service_discovery/service_discovery_metrics.nim New service-discovery metrics declarations (includes routing table gauges/counters).
libp2p/protocols/service_discovery/types.nim Adds ServiceId = Key alias for service routing table identification.
libp2p/protocols/kademlia.nim Adds refreshTable(kad, rtable, ...) and updates bootstrap() to use it.
tests/libp2p/service_discovery/test_routing_table_manager.nim New unit tests for service routing table manager + refreshAllTables behavior.
tests/libp2p/kademlia/test_routing_table.nim New unit tests for routing table insertion/eviction/selection helpers.

Comment on lines +104 to +106

discard table.insert(peerKey)
cd_service_table_insertions.inc()

Copilot AI Apr 14, 2026

Copy link

Choose a reason for hiding this comment

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

table.insert(peerKey) updates the global Kademlia routing-table gauges (kad_routing_table_peers, etc.) as a side effect. Inserting into per-service routing tables will therefore corrupt the Kademlia routing-table metrics. This likely needs a metrics-isolation mechanism (separate metrics for service tables, or a way to disable Kademlia metric updates for these inserts).

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think this is ok. service tables inserts/deletes/whatever are still kademlia inserts/deletes/whatevers

Comment thread libp2p/protocols/kademlia.nim Resolved
Comment on lines +60 to +63
for bucket in mainRoutingTable.buckets:
for peer in bucket.peers:
discard serviceTable.insert(peer.nodeId)

Copilot AI Apr 14, 2026

Copy link

Choose a reason for hiding this comment

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

serviceTable.insert(...) calls into kademlia/routing_table.insert, which updates the global kad_routing_table_* gauges/counters. Seeding service routing tables from the main table will therefore overwrite the Kademlia routing table metrics and make them incorrect operationally. Consider adding a “no-metrics” insert path for service tables or moving routing-table metrics updates out of the core RoutingTable.insert API.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think this is ok. service tables inserts/deletes/whatever are still kademlia inserts/deletes/whatevers

@gmelodie
gmelodie force-pushed the feat/service-disco/service-routing-tables branch from 2051b62 to c6a8deb Compare April 14, 2026 17:42
@gmelodie
gmelodie force-pushed the feat/service-disco/iptree-signatures branch from 89138c9 to a495405 Compare April 14, 2026 18:21
@github-project-automation github-project-automation Bot moved this from new to In Progress in nim-libp2p Apr 14, 2026
@gmelodie
gmelodie force-pushed the feat/service-disco/iptree-signatures branch from a495405 to b4d439b Compare April 14, 2026 19:33
@gmelodie
gmelodie force-pushed the feat/service-disco/iptree-signatures branch from f8c40a6 to a9fa626 Compare April 15, 2026 14:35
Base automatically changed from feat/service-disco/iptree-signatures to master April 15, 2026 15:39
@gmelodie
gmelodie requested a review from a team April 15, 2026 15:39
@gmelodie
gmelodie enabled auto-merge April 15, 2026 16:44
@gmelodie
gmelodie force-pushed the feat/service-disco/service-routing-tables branch from be64b83 to 423c403 Compare April 15, 2026 18:47
@gmelodie
gmelodie added this pull request to the merge queue Apr 15, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 15, 2026
@richard-ramos
richard-ramos added this pull request to the merge queue Apr 15, 2026
Merged via the queue into master with commit 5670faa Apr 15, 2026
38 of 40 checks passed
@richard-ramos
richard-ramos deleted the feat/service-disco/service-routing-tables branch April 15, 2026 21:22
@github-project-automation github-project-automation Bot moved this from In Progress to done in nim-libp2p Apr 15, 2026
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.

5 participants