js-libp2p
js-libp2p copied to clipboard
peer-routing getClosestPeers returns more than K nodes when using kad dht
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 can you open a PR to fix this? :pray: :pray:
@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
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.