Skip to content

feat(service-disco): add advertiser - #2285

Merged
gmelodie merged 64 commits into
masterfrom
feat/service-disco/advertiser
Apr 20, 2026
Merged

gmelodie merged 64 commits into
masterfrom
feat/service-disco/advertiser

Conversation

@gmelodie

Copy link
Copy Markdown
Collaborator

Summary

This PR adds the advertiser subsystem to ServiceDiscovery, building on the registrar and per-service routing tables introduced in the parent branch.

It wires up the Advertiser type (already declared in types.nim) into the ServiceDiscovery object and implements the full outbound advertisement lifecycle:

  • Encoding the local peer record
  • Sending REGISTER messages
  • Handling Confirmed / Wait / Rejected responses with appropriate back-off
  • Feeding closer peers back into the service routing table

addProvidedService and removeProvidedService are added as the primary API for managing which services a node actively advertises.


Changes

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

  • sendRegister

    • Dials a registrar peer, sends a REGISTER message, and returns:
      (RegistrationStatus, Opt[Ticket], seq[PeerId])
  • startAdvertising

    • Long-running loop per registrar peer
    • Builds the signed advertisement from peerInfo (if not externally supplied)
    • Handles response states:
      • Confirmed → sleep advertExpiry
      • Wait → sleep min(advertExpiry, tWaitFor)
      • Rejected → stop
    • Feeds closer peers into the service routing table on each reply
  • addProvidedService

    • Registers the service as Provided in rtManager (seeded from main routing table)
    • Shuffles peers in each bucket
    • Spawns up to kRegister startAdvertising tasks per non-empty bucket
  • removeProvidedService

    • Cancels and removes all pending tasks for the service
    • Calls rtManager.removeService
    • Updates metrics
  • clear

    • Bulk-cancels all running advertise tasks

libp2p/protocols/service_discovery/types.nim

  • Adds:
    • advertiser*: Advertiser field to ServiceDiscovery

libp2p/protocols/service_discovery.nim

  • Initializes Advertiser via Advertiser.new() in the ServiceDiscovery constructor

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

  • 12 tests across three suites:
    • addProvidedService (5 async)
    • removeProvidedService (3 async)
    • startAdvertising / stopAdvertising (4 sync)

tests/libp2p/service_discovery/utils.nim

  • Adds test helpers:
    • populateRoutingTable(disco, n)
    • populateAdvTable(disco, serviceId)
  • Extends imports for:
    • routing_table_manager
    • kademlia/routing_table
    • Kademlia test utilities

Affected Areas

  • Gossipsub
  • Transports
  • Peer Management / Discovery
    • Adds outbound advertisement scheduling loop
    • Adds service lifecycle management (addProvidedService / removeProvidedService)
  • Protocol Logic
    • Implements REGISTER send path
    • Implements ticket-based retry protocol (client side)
  • Build / Tooling
  • Other

Compatibility & Downstream Validation

All changes are additive relative to the registrar branch.

  • No existing types, fields, or proc signatures are modified
  • The new advertiser field is initialized in ServiceDiscovery.new
  • Consumers using the constructor are unaffected

Downstream projects (Nimbus, Waku, Codex) do not require validation unless they construct ServiceDiscovery via object literals instead of new


Impact on Library Users

  • ServiceDiscovery.advertiser: Advertiser

    • New public field (additive)
  • addProvidedService(disco, service, add)

    • New async proc (additive)
  • removeProvidedService(disco, service)

    • New async proc (additive)
  • sendRegister(disco, peerId, serviceId, ad, ticket)

    • New public async proc (additive)
  • startAdvertising(disco, serviceId, registrar, bucketIdx, ticket, add)

    • New public async proc (overloads existing sync stub with distinct signature)
  • Advertiser.clear()

    • New public proc (additive)

Risk Assessment

  • startAdvertising name collision

    • Existing sync stub and new async proc coexist as overloads
    • Nim resolves correctly, but naming may cause confusion
    • Low risk; should be renamed or removed in a follow-up
  • Task not started when upgrading Interest → Both

    • If service already exists as Interest, upgrading to Provided returns early
    • No advertiser tasks are spawned in this case
    • Medium risk; may surprise callers expecting automatic advertising
  • No task completion tracking

    • advertiser.running is not cleaned up when tasks finish or error
    • Leads to over-reporting of active tasks over time
    • Low functional impact; affects metrics accuracy

References

gmelodie and others added 4 commits April 15, 2026 16:47
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 force-pushed the feat/service-disco/registrar branch from 09bf42d to dccfe2a Compare April 15, 2026 20:01
@gmelodie
gmelodie force-pushed the feat/service-disco/advertiser branch from cf60e59 to 44876d1 Compare April 17, 2026 14:17
@gmelodie
gmelodie requested a review from vladopajic April 17, 2026 14:18
@github-project-automation github-project-automation Bot moved this from new to In Progress in nim-libp2p Apr 17, 2026
Base automatically changed from feat/service-disco/registrar to master April 17, 2026 21:03
@gmelodie
gmelodie requested a review from a team April 17, 2026 21:03
@gmelodie
gmelodie enabled auto-merge April 17, 2026 22:44
@gmelodie
gmelodie requested a review from richard-ramos April 20, 2026 12:36
@gmelodie
gmelodie added this pull request to the merge queue Apr 20, 2026
Merged via the queue into master with commit 0341cd0 Apr 20, 2026
24 checks passed
@gmelodie
gmelodie deleted the feat/service-disco/advertiser branch April 20, 2026 13:48
@github-project-automation github-project-automation Bot moved this from In Progress to done in nim-libp2p Apr 20, 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.

4 participants