Skip to content

fix(kad-dht): prefer connected peers to reduce unnecessary dials#3418

Open
paschal533 wants to merge 4 commits intolibp2p:mainfrom
paschal533:fix/kad-dht-prefer-connected-peers
Open

fix(kad-dht): prefer connected peers to reduce unnecessary dials#3418
paschal533 wants to merge 4 commits intolibp2p:mainfrom
paschal533:fix/kad-dht-prefer-connected-peers

Conversation

@paschal533
Copy link
Contributor

@paschal533 paschal533 commented Mar 23, 2026

Summary

Reduces unnecessary new dials in the Amino DHT by preferring already-connected peers at three levels:

Tier 1 Query manager: connected-peers-first path distribution (query/manager.ts)

Before distributing the K closest routing-table peers into D disjoint query paths, sort them so already-connected peers come first. With a round-robin bucket assignment, each path gets a connected peer as its starting point, reducing the cold-dial penalty at the beginning of every DHT query.

Tier 2 Content routing: connected-peers-first ADD_PROVIDER fan-out (content-routing/index.ts)

After collecting the K closest FINAL_PEER events during provide(), sort them by connection status before enqueuing the parallel ADD_PROVIDER sends. The first ALPHA queue slots go to already-connected peers, so provider records reach the network with fewer new dials.

Tier 3 Reprovider: Kademlia key order sweep (reprovider.ts)

Port go-libp2p's SweepingProvider optimisation. processRecords now collects all CIDs that need reproviding, computes their Kademlia keys, sorts them lexicographically, and queues reprovides in that order. XOR-adjacent CIDs share the same K closest peers, so connections opened for one CID are reused for the next, reducing the total number of new dials across a reprovide run.

Test plan

  • query.spec.ts verifies each disjoint path gets at least one connected starting peer when connected peers are available
  • content-routing.spec.ts verifies connectionManager.getConnections is called during provide() for the connected-first sort
  • reprovider.spec.ts inserts 5 CIDs in reverse Kademlia key order, then verifies contentRouting.provide is called in ascending Kademlia key order
  • All 152 existing tests continue to pass
  • Lint and TypeScript build clean

@paschal533 paschal533 marked this pull request as ready for review March 23, 2026 12:44
@paschal533 paschal533 requested a review from a team as a code owner March 23, 2026 12:44
@paschal533 paschal533 marked this pull request as draft March 23, 2026 13:00
@paschal533 paschal533 marked this pull request as draft March 23, 2026 13:00
@paschal533 paschal533 force-pushed the fix/kad-dht-prefer-connected-peers branch from faa87be to 395c833 Compare March 23, 2026 13:08
Sort CIDs by their Kademlia key before queueing reprovides so that
XOR-adjacent CIDs are processed consecutively. Nearby CIDs in the
keyspace share the same K closest peers, so connections opened for one
CID are likely to be reused for the next, reducing the number of new
dials per reprovide run (Amino DHT SweepingProvider optimisation).
@paschal533 paschal533 force-pushed the fix/kad-dht-prefer-connected-peers branch from 395c833 to 968206e Compare March 23, 2026 13:11
@paschal533 paschal533 marked this pull request as ready for review March 23, 2026 14:10
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