From 046649855fbc552ae36e4c8e1536c1ba3bab5eb3 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 19 Dec 2021 17:04:41 +0400 Subject: [PATCH] remove the addr package --- p2p/host/peerstore/addr/addrsrcs.go | 70 --------------------- p2p/host/peerstore/addr/addrsrcs_test.go | 78 ------------------------ 2 files changed, 148 deletions(-) delete mode 100644 p2p/host/peerstore/addr/addrsrcs.go delete mode 100644 p2p/host/peerstore/addr/addrsrcs_test.go diff --git a/p2p/host/peerstore/addr/addrsrcs.go b/p2p/host/peerstore/addr/addrsrcs.go deleted file mode 100644 index 9be78d5ccc..0000000000 --- a/p2p/host/peerstore/addr/addrsrcs.go +++ /dev/null @@ -1,70 +0,0 @@ -// Package addr provides utility functions to handle peer addresses. -package addr - -import ( - ma "github.com/multiformats/go-multiaddr" -) - -// AddrSource is a source of addresses. It allows clients to retrieve -// a set of addresses at a last possible moment in time. It is used -// to query a set of addresses that may change over time, as a result -// of the network changing interfaces or mappings. -type Source interface { - Addrs() []ma.Multiaddr -} - -// CombineSources returns a new AddrSource which is the -// concatenation of all input AddrSources: -// -// combined := CombinedSources(a, b) -// combined.Addrs() // append(a.Addrs(), b.Addrs()...) -// -func CombineSources(srcs ...Source) Source { - return combinedAS(srcs) -} - -type combinedAS []Source - -func (cas combinedAS) Addrs() []ma.Multiaddr { - var addrs []ma.Multiaddr - for _, s := range cas { - addrs = append(addrs, s.Addrs()...) - } - return addrs -} - -// UniqueSource returns a new AddrSource which omits duplicate -// addresses from the inputs: -// -// unique := UniqueSource(a, b) -// unique.Addrs() // append(a.Addrs(), b.Addrs()...) -// // but only adds each addr once. -// -func UniqueSource(srcs ...Source) Source { - return uniqueAS(srcs) -} - -type uniqueAS []Source - -func (uas uniqueAS) Addrs() []ma.Multiaddr { - seen := make(map[string]struct{}) - var addrs []ma.Multiaddr - for _, s := range uas { - for _, a := range s.Addrs() { - s := a.String() - if _, found := seen[s]; !found { - addrs = append(addrs, a) - seen[s] = struct{}{} - } - } - } - return addrs -} - -// Slice is a simple slice of addresses that implements -// the AddrSource interface. -type Slice []ma.Multiaddr - -func (as Slice) Addrs() []ma.Multiaddr { - return as -} diff --git a/p2p/host/peerstore/addr/addrsrcs_test.go b/p2p/host/peerstore/addr/addrsrcs_test.go deleted file mode 100644 index 06bea07bce..0000000000 --- a/p2p/host/peerstore/addr/addrsrcs_test.go +++ /dev/null @@ -1,78 +0,0 @@ -package addr - -import ( - "fmt" - "testing" - - ma "github.com/multiformats/go-multiaddr" -) - -func newAddrOrFatal(t *testing.T, s string) ma.Multiaddr { - a, err := ma.NewMultiaddr(s) - if err != nil { - t.Fatal("error parsing multiaddr", err) - } - return a -} - -func newAddrs(t *testing.T, n int) []ma.Multiaddr { - addrs := make([]ma.Multiaddr, n) - for i := 0; i < n; i++ { - s := fmt.Sprintf("/ip4/1.2.3.4/tcp/%d", i) - addrs[i] = newAddrOrFatal(t, s) - } - return addrs -} - -func addrSetsSame(a, b []ma.Multiaddr) bool { - if len(a) != len(b) { - return false - } - for i, aa := range a { - bb := b[i] - if !aa.Equal(bb) { - return false - } - } - return true -} - -func addrSourcesSame(a, b Source) bool { - return addrSetsSame(a.Addrs(), b.Addrs()) -} - -func TestAddrCombine(t *testing.T) { - addrs := newAddrs(t, 30) - a := Slice(addrs[0:10]) - b := Slice(addrs[10:20]) - c := Slice(addrs[20:30]) - d := CombineSources(a, b, c) - if !addrSetsSame(addrs, d.Addrs()) { - t.Error("addrs differ") - } - if !addrSourcesSame(Slice(addrs), d) { - t.Error("addrs differ") - } -} - -func TestAddrUnique(t *testing.T) { - - addrs := newAddrs(t, 40) - a := Slice(addrs[0:20]) - b := Slice(addrs[10:30]) - c := Slice(addrs[20:40]) - d := CombineSources(a, b, c) - e := UniqueSource(a, b, c) - if addrSetsSame(addrs, d.Addrs()) { - t.Error("addrs same") - } - if addrSourcesSame(Slice(addrs), d) { - t.Error("addrs same") - } - if !addrSetsSame(addrs, e.Addrs()) { - t.Error("addrs differ", addrs, "\n\n", e.Addrs(), "\n\n") - } - if !addrSourcesSame(Slice(addrs), e) { - t.Error("addrs differ", addrs, "\n\n", e.Addrs(), "\n\n") - } -}