js-libp2p icon indicating copy to clipboard operation
js-libp2p copied to clipboard

peer-routing getClosestPeers returns more than K nodes when using kad dht

Open zeroxbt opened this issue 3 years ago • 1 comments

The getClosestPeers function in peer-routing.js returns more than K nodes when using the kad dht module.

The reason for this is that the getClosestPeers function in the kad-dht module yields not only FINAL_PEER events, but also DIALING_PEER, PEER_RESPONSE and SENDING_QUERYevents. In order to be compatible, either getClosestPeers from kad-dht module should return only FINAL_PEER events, or the getClosestPeers function in peer-routing.js should filter sources by FINAL_PEER events.

zeroxbt avatar Mar 17 '22 20:03 zeroxbt

@zeroxbt can you open a PR to fix this? :pray: :pray:

wemeetagain avatar May 24 '22 15:05 wemeetagain

@wemeetagain I made a PR for this, and also found a bug that slows down kad dht find closest peers quite a bit. Please take a look when you have some time :)

https://github.com/libp2p/js-libp2p-kad-dht/pull/371

zeroxbt avatar Sep 11 '22 18:09 zeroxbt

If you're only interested in the closest peers, you should disregard all events that are not FINAL_PEER. The others are useful for tracing and diagnostics, and for giving the user some feedback their query has not stalled but should not be treated as actual results.

achingbrain avatar Sep 14 '22 16:09 achingbrain