From 8634c92c98cf6d5e03ed57d278b8a1118528a86c Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 9 Apr 2020 18:40:31 -0700 Subject: [PATCH] fix: dedup addresses --- dual/dual.go | 17 ++++++++++++++++- dual/dual_test.go | 8 ++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/dual/dual.go b/dual/dual.go index f60e6a92a..4df46c914 100644 --- a/dual/dual.go +++ b/dual/dual.go @@ -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" ) @@ -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() } diff --git a/dual/dual_test.go b/dual/dual_test.go index 87349f3c8..dbd4aab12 100644 --- a/dual/dual_test.go +++ b/dual/dual_test.go @@ -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)) } }