Skip to content

Commit

Permalink
Merge pull request #581 from libp2p/fix/dedup-addresses
Browse files Browse the repository at this point in the history
fix: dedup addresses
  • Loading branch information
Stebalien authored Apr 10, 2020
2 parents bfa23ff + 8634c92 commit b33ccf3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
17 changes: 16 additions & 1 deletion dual/dual.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
dht "github.com/libp2p/go-libp2p-kad-dht"
helper "github.com/libp2p/go-libp2p-routing-helpers"

ma "github.com/multiformats/go-multiaddr"

"github.com/hashicorp/go-multierror"
)

Expand Down Expand Up @@ -151,9 +153,22 @@ func (dht *DHT) FindPeer(ctx context.Context, pid peer.ID) (peer.AddrInfo, error

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{
ID: pid,
Addrs: append(wanInfo.Addrs, lanInfo.Addrs...),
Addrs: addrs,
}, multierror.Append(wanErr, lanErr).ErrorOrNil()
}

Expand Down
8 changes: 4 additions & 4 deletions dual/dual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,14 @@ func TestFindPeer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(p.Addrs) == 0 {
t.Fatal("expeced find peer to find addresses.")
if len(p.Addrs) != 1 {
t.Fatalf("expeced find peer to find 1 address, found %d", len(p.Addrs))
}
p, err = d.FindPeer(ctx, wan.PeerID())
if err != nil {
t.Fatal(err)
}
if len(p.Addrs) == 0 {
t.Fatal("expeced find peer to find addresses.")
if len(p.Addrs) != 1 {
t.Fatalf("expeced find peer to find addresses, found %d", len(p.Addrs))
}
}

0 comments on commit b33ccf3

Please sign in to comment.