From ccdcc703275f8195a95e156b8108388737653a36 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 19 Dec 2021 16:59:48 +0400 Subject: [PATCH] remove dependency on the go-libp2p-peerstore/addr package --- backoffcache.go | 24 ++++++++++++++++++------ go.mod | 1 - 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/backoffcache.go b/backoffcache.go index 1ec71b8..b6da6dd 100644 --- a/backoffcache.go +++ b/backoffcache.go @@ -8,7 +8,6 @@ import ( "github.com/libp2p/go-libp2p-core/discovery" "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p-peerstore/addr" ) // BackoffDiscovery is an implementation of discovery that caches peer data and attenuates repeated queries @@ -276,13 +275,26 @@ func findPeerReceiver(ctx context.Context, pch, evtCh chan peer.AddrInfo, rcvPee } } +// mergeAddrInfos merges to AddrInfos. +// It returns nil if the merge doesn't add any new addresses. func mergeAddrInfos(prevAi, newAi peer.AddrInfo) *peer.AddrInfo { - combinedAddrs := addr.UniqueSource(addr.Slice(prevAi.Addrs), addr.Slice(newAi.Addrs)).Addrs() - if len(combinedAddrs) > len(prevAi.Addrs) { - combinedAi := &peer.AddrInfo{ID: prevAi.ID, Addrs: combinedAddrs} - return combinedAi + addrs := prevAi.Addrs +addrLoop: + for _, addr := range newAi.Addrs { + for _, a := range prevAi.Addrs { + if a.Equal(addr) { + continue addrLoop + } + } + addrs = append(addrs, addr) + } + if len(addrs) <= len(prevAi.Addrs) { + return nil + } + return &peer.AddrInfo{ + ID: prevAi.ID, + Addrs: addrs, } - return nil } func checkUpdates(orig, update map[peer.ID]peer.AddrInfo) bool { diff --git a/go.mod b/go.mod index 2827cdf..b27e814 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/ipfs/go-log/v2 v2.4.0 github.com/libp2p/go-libp2p-blankhost v0.2.0 github.com/libp2p/go-libp2p-core v0.11.0 - github.com/libp2p/go-libp2p-peerstore v0.4.0 github.com/libp2p/go-libp2p-swarm v0.8.0 github.com/multiformats/go-multihash v0.0.15 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c