Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findnode(self) should return multiple peers #968

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,30 +262,26 @@ func (dht *IpfsDHT) handleFindPeer(ctx context.Context, from peer.ID, pmes *pb.M

// if looking for self... special case where we send it on CloserPeers.
targetPid := peer.ID(pmes.GetKey())
if targetPid == dht.self {
closest = []peer.ID{dht.self}
} else {
closest = dht.betterPeersToQuery(pmes, from, dht.bucketSize)

// Never tell a peer about itself.
if targetPid != from {
// Add the target peer to the set of closest peers if
// not already present in our routing table.
//
// Later, when we lookup known addresses for all peers
// in this set, we'll prune this peer if we don't
// _actually_ know where it is.
found := false
for _, p := range closest {
if targetPid == p {
found = true
break
}
}
if !found {
closest = append(closest, targetPid)
closest = dht.betterPeersToQuery(pmes, from, dht.bucketSize)
Copy link
Member

Choose a reason for hiding this comment

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

dht.betterPeersToQuery errors if the list contains self.
https://github.com/libp2p/go-libp2p-kad-dht/blob/master/dht.go#L765-L769

		if clp == dht.self {
			logger.Error("BUG betterPeersToQuery: attempted to return self! this shouldn't happen...")
			return nil
		}
		// Dont send a peer back themselves
		if clp == from {
			continue
		}

It also handles the case that we don't inform the peer about itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This snippet handles the output of routingTable.NearestPeers, which never contains itself.

The peer adds it own peer id, only if its own peer id is the target of the FIND_NODE request at

// if looking for self... special case where we send it on CloserPeers.
targetPid := peer.ID(pmes.GetKey())
closest = dht.betterPeersToQuery(pmes, from, dht.bucketSize)
// Never tell a peer about itself.
if targetPid != from {
// Add the target peer to the set of closest peers if
// not already present in our routing table.
//
// Later, when we lookup known addresses for all peers
// in this set, we'll prune this peer if we don't
// _actually_ know where it is.
found := false
for _, p := range closest {
if targetPid == p {
found = true
break
}
}
if !found {
closest = append(closest, targetPid)
}
}


// Never tell a peer about itself.
if targetPid != from {
sukunrt marked this conversation as resolved.
Show resolved Hide resolved
// Add the target peer to the set of closest peers if
// not already present in our routing table.
//
// Later, when we lookup known addresses for all peers
// in this set, we'll prune this peer if we don't
// _actually_ know where it is.
found := false
for _, p := range closest {
if targetPid == p {
found = true
break
}
}
if !found {
closest = append(closest, targetPid)
}
Comment on lines +282 to +284
Copy link
Member

Choose a reason for hiding this comment

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

Why do we do this? If the peerID isn't found, should we return it in findPeer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dht.betterPeersToQuery never includes itself (nor from) in the results.

Currently if A sends FIND_NODE(B) to B, B will reply [B], and we now want it to return [B, C, D, ...]. In this case targetPid is our own peer id, it wasn't returned by dht.betterPeersToQuery, so we need to add it at this step.

We will be able to get rid of this when the response to FIND_NODE(self) will be not include self.

Copy link
Member

Choose a reason for hiding this comment

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

But if the target node doesnt exist we will include it in the output, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Peers without addresses are excluded from the response

closestinfos := pstore.PeerInfos(dht.peerstore, closest)
// possibly an over-allocation but this array is temporary anyways.
withAddresses := make([]peer.AddrInfo, 0, len(closestinfos))
for _, pi := range closestinfos {
if len(pi.Addrs) > 0 {
withAddresses = append(withAddresses, pi)
}
}
resp.CloserPeers = pb.PeerInfosToPBPeers(dht.host.Network(), withAddresses)

}

if closest == nil {
Expand Down
Loading