Skip to content

feat(service-disco): add discoverer - #2306

Merged
gmelodie merged 92 commits into
masterfrom
feat/service-disco/discoverer-tests
Apr 21, 2026
Merged

gmelodie merged 92 commits into
masterfrom
feat/service-disco/discoverer-tests

Conversation

@gmelodie

@gmelodie gmelodie commented Apr 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This PR introduces the Discoverer component and completes the service discovery protocol by implementing the previously stubbed REGISTER and GET_ADS handlers.

  • lookup enables a node to query the network for providers of a given service ID by iterating its per-service routing table and issuing GET_ADS requests.
  • Server-side handlers (handleRegister, handleGetAds) complete the round-trip:
    • Registrars can now accept registrations with rate-limiting and signed wait tickets
    • Registrars can respond to advertisement queries from their cache

Changes

libp2p/protocols/service_discovery/discoverer.nim (new)

  • sendGetAds

    • Dials a peer
    • Sends a GET_ADS message
    • Decodes returned advertisements (filters mismatched service IDs)
    • Collects closer peers
  • lookup

    • Iterates the per-service routing table bucket by bucket
    • Queries up to kLookup peers per bucket via sendGetAds
    • Seeds the routing table with returned closer peers
    • Accumulates up to fLookup valid advertisements

libp2p/protocols/service_discovery/registrar.nim

  • handleRegister

    • Validates the advertisement
    • Computes rate-limiting wait time
    • Adjusts wait using a valid retry ticket (if provided)
    • Responds:
      • Confirmed → calls acceptAdvertisement
      • Wait → returns a signed Ticket
      • Rejected → fallback on signing failure
  • handleGetAds

    • Looks up registrar cache for the requested service ID
    • Returns up to fReturn advertisements
    • Attaches closer peers from the routing table

libp2p/protocols/service_discovery.nim

  • Added registrar and discoverer imports
  • Exported discoverer (exposes lookup)
  • Replaced "Unimplemented" stubs:
    • MessageType.register → await disco.handleRegister(conn, msg)
    • MessageType.getAds → await disco.handleGetAds(conn, msg)

tests/libp2p/service_discovery/test_discoverer.nim (new)

Covers:

  • Routing table creation on first call
  • Empty table returns ok([])
  • Idempotent repeated calls
  • Independent tables per service ID
  • Result count capped (see risk note)

tests/libp2p/service_discovery/utils.nim

  • Exported routing_table_manager
  • Added helpers:
    • populateRoutingTable
    • populateAdvTable
    • populateSearchTable

Affected Areas

  • Peer Management / Discovery
    Implements client-side provider lookup over per-service Kademlia routing tables.

  • Protocol Logic
    REGISTER and GET_ADS are now fully handled server-side (previously dropped).

  • Gossipsub

  • Transports

  • Build / Tooling

  • Other

(No direct changes in these areas.)


Compatibility & Downstream Validation

  • No changes to existing KadDHT encode/decode or APIs
  • Message types register (16) and getAds (17) already existed; only dispatch was missing
  • Mixed-version behavior:
    • Older nodes silently drop messages
    • Advertisers retry on timeout → graceful degradation

No validation required for standard KadDHT consumers (Nimbus, Waku, Codex).

ServiceDiscovery users should verify that stop() correctly drains in-flight advertising tasks.


Impact on Library Users

  • New API

    • lookup*(disco: ServiceDiscovery, serviceId: ServiceId): Future[Result[seq[Advertisement], string]]
  • Behavior changes

    • REGISTER is now processed (returns Confirmed, Wait, or Rejected)
    • GET_ADS is now processed (returns cached advertisements)

Risk Assessment

  • Retry ticket tInit not bounded to wall time
    A manipulated past tInit could produce a large negative adjustment.
    Mitigated by retry-window validation in processRetryTicket.
    Risk: Low

  • Deterministic advertisement ordering
    handleGetAds returns ads in insertion order (no randomization).
    May bias results under load.
    Risk: Low (can be improved later)

  • Ticket signing fallback to Rejected
    If signing fails, advertiser backs off for full expiry instead of retrying sooner.
    Safe but may introduce delays.
    Risk: Low

  • Test uses kLookup instead of fLookup
    Result cap assertion may be weaker than intended if values differ.
    Risk: Low


References

gmelodie and others added 30 commits April 15, 2026 15:44
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@gmelodie
gmelodie enabled auto-merge April 20, 2026 19:26
let searchTable = disco.rtManager.getTable(serviceId).valueOr:
return err("service table not found for service id: " & $serviceId)

var found = newSeqOfCap[Advertisement](disco.discoConfig.fLookup)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

disco.discoConfig.fLookup
why is this not
disco.config.fLookup

@gmelodie gmelodie Apr 20, 2026 •

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.

ServiceDiscovery is of type KadDHT which already has a config field

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

still, should be doable. KadDHT will have type param C that represents config. then ServiceDiscovery extends KadDHT with config type DiscoveryConfig that extends KadConfig.

but talking out from top of my head 1) not sure how this will work in nim 2) not sure how these would stack if there is really senergy or not

@gmelodie
gmelodie force-pushed the feat/service-disco/discoverer-tests branch 2 times, most recently from 853da6f to 9e47fe8 Compare April 21, 2026 00:36
@gmelodie
gmelodie force-pushed the feat/service-disco/discoverer-tests branch from 9e47fe8 to d1e3824 Compare April 21, 2026 00:36

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

This PR completes the Service Discovery protocol round-trip by adding a client-side Discoverer (lookup) and implementing server-side handling for REGISTER and GET_ADS messages, plus associated maintenance loops and tests.

Changes:

  • Added discoverer.nim implementing lookup + GET_ADS querying/peer seeding.
  • Implemented handleRegister and handleGetAds in the registrar and wired both into the ServiceDiscovery message dispatch.
  • Added/updated tests and test utilities for discoverer behavior and registrar validation.

Reviewed changes

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

Show a summary per file
File Description
libp2p/protocols/service_discovery/discoverer.nim New discoverer component implementing GET_ADS dialing/decoding and lookup.
libp2p/protocols/service_discovery/registrar.nim Implements REGISTER and GET_ADS handlers (rate limiting, tickets, cached ad responses).
libp2p/protocols/service_discovery/types.nim Adds background loop fields to ServiceDiscovery and introduces ad-seq encoding helper.
libp2p/protocols/service_discovery.nim Wires registrar/discoverer into imports/exports, message dispatch, and start/stop maintenance loops.
libp2p/protocols/kademlia/types.nim Adds toPeerIds helper used by discoverer peer selection.
tests/libp2p/service_discovery/test_discoverer.nim New tests for discoverer lookup/table initialization behavior.
tests/libp2p/service_discovery/utils.nim Adds helper to populate per-service search tables for tests.
tests/libp2p/service_discovery/test_registrar.nim Updates registrar validation tests for new validateRegisterMessage signature/service check.

gmelodie and others added 5 commits April 21, 2026 10:57
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@gmelodie
gmelodie force-pushed the feat/service-disco/discoverer-tests branch from db902bb to 5d0148b Compare April 21, 2026 14:07
gmelodie and others added 8 commits April 21, 2026 11:07
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
auto-merge was automatically disabled April 21, 2026 17:51

Merge commits are not allowed on this repository

@gmelodie
gmelodie merged commit 9e4e99a into master Apr 21, 2026
35 of 36 checks passed
@gmelodie
gmelodie deleted the feat/service-disco/discoverer-tests branch April 21, 2026 17:54
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