Unverified Commit b33ccf39 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #581 from libp2p/fix/dedup-addresses

fix: dedup addresses
parents bfa23ff8 8634c92c
...@@ -15,6 +15,8 @@ import ( ...@@ -15,6 +15,8 @@ import (
dht "github.com/libp2p/go-libp2p-kad-dht" dht "github.com/libp2p/go-libp2p-kad-dht"
helper "github.com/libp2p/go-libp2p-routing-helpers" helper "github.com/libp2p/go-libp2p-routing-helpers"
ma "github.com/multiformats/go-multiaddr"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
) )
...@@ -151,9 +153,22 @@ func (dht *DHT) FindPeer(ctx context.Context, pid peer.ID) (peer.AddrInfo, error ...@@ -151,9 +153,22 @@ func (dht *DHT) FindPeer(ctx context.Context, pid peer.ID) (peer.AddrInfo, error
wg.Wait() wg.Wait()
// combine addresses
deduped := make(map[string]ma.Multiaddr, len(wanInfo.Addrs)+len(lanInfo.Addrs))
for _, addr := range wanInfo.Addrs {
deduped[string(addr.Bytes())] = addr
}
for _, addr := range lanInfo.Addrs {
deduped[string(addr.Bytes())] = addr
}
addrs := make([]ma.Multiaddr, 0, len(deduped))
for _, addr := range deduped {
addrs = append(addrs, addr)
}
return peer.AddrInfo{ return peer.AddrInfo{
ID: pid, ID: pid,
Addrs: append(wanInfo.Addrs, lanInfo.Addrs...), Addrs: addrs,
}, multierror.Append(wanErr, lanErr).ErrorOrNil() }, multierror.Append(wanErr, lanErr).ErrorOrNil()
} }
......
...@@ -356,14 +356,14 @@ func TestFindPeer(t *testing.T) { ...@@ -356,14 +356,14 @@ func TestFindPeer(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(p.Addrs) == 0 { if len(p.Addrs) != 1 {
t.Fatal("expeced find peer to find addresses.") t.Fatalf("expeced find peer to find 1 address, found %d", len(p.Addrs))
} }
p, err = d.FindPeer(ctx, wan.PeerID()) p, err = d.FindPeer(ctx, wan.PeerID())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(p.Addrs) == 0 { if len(p.Addrs) != 1 {
t.Fatal("expeced find peer to find addresses.") t.Fatalf("expeced find peer to find addresses, found %d", len(p.Addrs))
} }
} }
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