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.

@SuchitraSwain
Copy link
Author

@sumanjeet0012 I will address the comments by today

@sumanjeet0012
Copy link
Contributor

@SuchitraSwain @yashksaini-coder The PR also contains some commits from flood publishing PR,
Please rebase it on latest main branch.

- Added tests to ensure fallback to connected peers when routing table has insufficient peers.
- Improved flood publish tests for message propagation in various network topologies.
- Cleaned up temporary file handling tests for better readability.
yashksaini-coder and others added 17 commits October 9, 2025 00:47
…le modules

- Updated logging statements in `peer_routing.py` to improve readability by splitting long lines.
- Refactored debug logs in `messages.py` to use parameterized logging for better performance and consistency.
- Enhanced logging in `yamux.py` to use formatted strings for clarity and consistency.
- Improved logging in `connection.py` and `listener.py` for QUIC transport to ensure consistent formatting.
- Standardized logging in `security.py` for TLS handling to use parameterized strings.
- Updated logging in `transport_registry.py` to maintain consistency in log formatting.
- Refined logging in `websocket` transport files to ensure uniformity in message formatting.
- Adjusted assertions in test files to use formatted strings for better readability and consistency.
… WebsocketListener, and YamuxStream classes; adjust formatting in test_gossipsub.py for clarity
…ve data handling and certificate loading security in TLSTransport
…ICListener and remove redundant import in WebsocketTransport
…CConnection stream initialization for clarity; add static method _multiaddr_from_socket in WebsocketListener class
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.

6 participants