From 8a63d30904b4383f0ffb731580ff98bf32b42058 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Mon, 3 Jan 2022 01:41:26 -0800 Subject: [PATCH] remove dependency on the go-libp2p-peerstore/addr package (#82) --- p2p/discovery/backoff/backoffcache.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/p2p/discovery/backoff/backoffcache.go b/p2p/discovery/backoff/backoffcache.go index 1ec71b8598..bb427605c4 100644 --- a/p2p/discovery/backoff/backoffcache.go +++ b/p2p/discovery/backoff/backoffcache.go @@ -8,7 +8,8 @@ import ( "github.com/libp2p/go-libp2p-core/discovery" "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p-peerstore/addr" + + ma "github.com/multiformats/go-multiaddr" ) // BackoffDiscovery is an implementation of discovery that caches peer data and attenuates repeated queries @@ -277,7 +278,20 @@ func findPeerReceiver(ctx context.Context, pch, evtCh chan peer.AddrInfo, rcvPee } func mergeAddrInfos(prevAi, newAi peer.AddrInfo) *peer.AddrInfo { - combinedAddrs := addr.UniqueSource(addr.Slice(prevAi.Addrs), addr.Slice(newAi.Addrs)).Addrs() + seen := make(map[string]struct{}, len(prevAi.Addrs)) + combinedAddrs := make([]ma.Multiaddr, 0, len(prevAi.Addrs)) + addAddrs := func(addrs []ma.Multiaddr) { + for _, addr := range addrs { + if _, ok := seen[addr.String()]; ok { + continue + } + seen[addr.String()] = struct{}{} + combinedAddrs = append(combinedAddrs, addr) + } + } + addAddrs(prevAi.Addrs) + addAddrs(newAi.Addrs) + if len(combinedAddrs) > len(prevAi.Addrs) { combinedAi := &peer.AddrInfo{ID: prevAi.ID, Addrs: combinedAddrs} return combinedAi