I have a question about betterPeersToQuery(): There is a comment in the code for both the go and js versions of the DHT that says:
betterPeersToQuery returns nearestPeersToQuery, but if and only if closer than self.
However it seems that this is not strictly true. The code filters the local node out of the list of closer peers, but doesn’t check that the peers are actually closer than the local node.
In practice, it may not matter much, because
- a node that is close to the target key will know about all the peers in that neighbourhood, so it will return close peers to the key (even if they’re not strictly closer than itself)
- a node that is far from the target key will know about a lot of peers in between itself and the key, so probably the list of closer peers will all be closer than the local peer.
However it might make a difference if
- the node is new, and doesn’t have much information about the network yet
- a malicious or malfunctioning remote peer returns a list of far away nodes