Skip to content

Conversation

SuchitraSwain
Copy link

issue: #905

### What was wrong?

  • After bootstrap, peers were added to the peerstore but not yet in the routing table.
  • DHT queries called find_local_closest_peers(), which returned an empty list.
  • This blocked random walks, provider lookups, and peer discovery.
  • As a result, nodes remained isolated and couldn’t progress beyond initial bootstrap peers.

### How was it fixed?
A fallback mechanism was implemented in the DHT query logic:

  • Configurable threshold: Uses connected peers from the peerstore when the routing table has fewer than MIN_PEERS_THRESHOLD = 5 entries.
  • Distance-based sorting: Connected peers are sorted by XOR distance to the target key.
  • Integration points: Added across peer_routing.py and kad_dht.py for FIND_NODE, GET_VALUE, PUT_VALUE, and provider lookups.
  • Graceful handling: Falls back only when necessary; no impact when routing table is sufficiently populated.
  • Tech requirements / highlights
  • No breaking changes to existing DHT APIs.
  • Threshold is configurable for different network environments.
  • Fully covered with 3 new unit tests ensuring fallback correctness.
  • All existing peer routing and DHT tests updated and passing (28/28 total).

✅ Benefits

  • Eliminates bootstrap deadlock.
  • Ensures peer discovery works immediately after startup.
  • Improves resilience and connectivity in sparse networks.
  • Maintains Kademlia’s routing efficiency by preferring routing table peers when available.

To-Do

  • Clean up commit history
  • Add or update documentation related to these changes
  • Add entry to the release notes

Cute Animal Picture

put a cute animal picture link inside the parentheses

@sumanjeet0012
Copy link
Contributor

@SuchitraSwain Could you please rebase your branch on the latest main branch?
Also, whenever a PR is ready for review, kindly tag the concerned person on that PR.

@seetadev
Copy link
Contributor

@SuchitraSwain : HI Suchitra, wish if you could resolve the merge conflicts.

Re-ran the CI/Cd pipeline. Please arrive at a good conclusion on the test results.

CCing @sumanjeet0012

@SuchitraSwain
Copy link
Author

@seetadev please Re-ran the CI/Cd pipeline.

Copy link
Contributor

@sumanjeet0012 sumanjeet0012 left a comment

Choose a reason for hiding this comment

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

@SuchitraSwain There are a couple of issues in this PR:

1.	The branch contains some previous commits from another branch. Please rebase it on the latest main branch.

2.	The main objective of this PR is to ensure that, in case of fewer peers in the routing table, the connected peers are used for the random walk, But here this logic is used in multiple places like find node which is not required.

3.	I have also added comments regarding some minor issues in this PR.

)

# Fallback to connected peers if routing table has insufficient peers
MIN_PEERS_THRESHOLD = 5 # Configurable minimum
Copy link
Contributor

Choose a reason for hiding this comment

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

Constants should be placed at top.

connected_peers = self.host.get_connected_peers()
if connected_peers:
# Sort connected peers by distance to target and use as response
from .utils import sort_peer_ids_by_distance
Copy link
Contributor

Choose a reason for hiding this comment

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

import statements should be placed at top.

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