Unverified Commit f05ebb20 authored by Adin Schmahmann's avatar Adin Schmahmann Committed by GitHub

Merge pull request #711 from libp2p/fix/findpeer-relay

findpeer should work even on peers that are not part of DHT queries
parents 03d4b62d 03734098
...@@ -1177,6 +1177,62 @@ func TestFindPeer(t *testing.T) { ...@@ -1177,6 +1177,62 @@ func TestFindPeer(t *testing.T) {
} }
} }
func TestFindPeerWithQueryFilter(t *testing.T) {
// t.Skip("skipping test to debug another")
if testing.Short() {
t.SkipNow()
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
filteredPeer := bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport))
dhts := setupDHTS(t, ctx, 4, QueryFilter(func(_ *IpfsDHT, ai peer.AddrInfo) bool {
return ai.ID != filteredPeer.ID()
}))
defer func() {
for i := 0; i < 4; i++ {
dhts[i].Close()
dhts[i].host.Close()
}
}()
connect(t, ctx, dhts[0], dhts[1])
connect(t, ctx, dhts[1], dhts[2])
connect(t, ctx, dhts[1], dhts[3])
err := filteredPeer.Connect(ctx, peer.AddrInfo{
ID: dhts[2].host.ID(),
Addrs: dhts[2].host.Addrs(),
})
if err != nil {
t.Fatal(err)
}
for i, maxLoops := 0, 5; i < maxLoops; i++ {
if len(dhts[2].host.Network().ConnsToPeer(filteredPeer.ID())) > 0 {
break
}
if i == maxLoops {
t.Fatal("failed to connect to the filtered peer")
}
}
ctxT, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
p, err := dhts[0].FindPeer(ctxT, filteredPeer.ID())
if err != nil {
t.Fatal(err)
}
if p.ID == "" {
t.Fatal("Failed to find peer.")
}
if p.ID != filteredPeer.ID() {
t.Fatal("Didnt find expected peer.")
}
}
func TestConnectCollision(t *testing.T) { func TestConnectCollision(t *testing.T) {
if testing.Short() { if testing.Short() {
t.SkipNow() t.SkipNow()
......
This diff is collapsed.
...@@ -432,7 +432,11 @@ func (q *query) queryPeer(ctx context.Context, ch chan<- *queryUpdate, p peer.ID ...@@ -432,7 +432,11 @@ func (q *query) queryPeer(ctx context.Context, ch chan<- *queryUpdate, p peer.ID
next.Addrs = append(next.Addrs, curInfo.Addrs...) next.Addrs = append(next.Addrs, curInfo.Addrs...)
// add their addresses to the dialer's peerstore // add their addresses to the dialer's peerstore
if q.dht.queryPeerFilter(q.dht, *next) { //
// add the next peer to the query if matches the query target even if it would otherwise fail the query filter
// TODO: this behavior is really specific to how FindPeer works and not GetClosestPeers or any other function
isTarget := string(next.ID) == q.key
if isTarget || q.dht.queryPeerFilter(q.dht, *next) {
q.dht.maybeAddAddrs(next.ID, next.Addrs, pstore.TempAddrTTL) q.dht.maybeAddAddrs(next.ID, next.Addrs, pstore.TempAddrTTL)
saw = append(saw, next.ID) saw = append(saw, next.ID)
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment