Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update go-libp2p-asn-util #128

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/ipfs/go-log v1.0.5
github.com/libp2p/go-cidranger v1.1.0
github.com/libp2p/go-libp2p v0.30.0
github.com/libp2p/go-libp2p-asn-util v0.3.0
github.com/libp2p/go-libp2p-asn-util v0.4.1
github.com/minio/sha256-simd v1.0.1
github.com/multiformats/go-multiaddr v0.11.0
github.com/multiformats/go-multihash v0.2.3
Expand Down Expand Up @@ -39,7 +39,7 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 // indirect
golang.org/x/sys v0.15.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38y
github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic=
github.com/libp2p/go-libp2p v0.30.0 h1:9EZwFtJPFBcs/yJTnP90TpN1hgrT/EsFfM+OZuwV87U=
github.com/libp2p/go-libp2p v0.30.0/go.mod h1:nr2g5V7lfftwgiJ78/HrID+pwvayLyqKCEirT2Y3Byg=
github.com/libp2p/go-libp2p-asn-util v0.3.0 h1:gMDcMyYiZKkocGXDQ5nsUQyquC9+H+iLEQHwOCZ7s8s=
github.com/libp2p/go-libp2p-asn-util v0.3.0/go.mod h1:B1mcOrKUE35Xq/ASTmQ4tN3LNzVVaMNmq2NACuqyB9w=
github.com/libp2p/go-libp2p-asn-util v0.4.1 h1:xqL7++IKD9TBFMgnLPZR6/6iYhawHKHl950SO9L6n94=
github.com/libp2p/go-libp2p-asn-util v0.4.1/go.mod h1:d/NI6XZ9qxw67b4e+NgpQexCIiFYJjErASrYW4PFDN8=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
Expand Down Expand Up @@ -109,8 +109,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ=
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8=
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 h1:qCEDpW1G+vcj3Y7Fy52pEM1AWm3abj8WimGYejI3SC4=
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
33 changes: 9 additions & 24 deletions peerdiversity/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"fmt"
"net"
"sort"
"strconv"
"sync"

"github.com/libp2p/go-libp2p/core/peer"
Expand All @@ -19,10 +20,6 @@

var dfLog = logging.Logger("diversityFilter")

type asnStore interface {
AsnForIPv6(ip net.IP) (string, error)
}

// PeerIPGroupKey is a unique key that represents ONE of the IP Groups the peer belongs to.
// A peer has one PeerIPGroupKey per address. Thus, a peer can belong to MULTIPLE Groups if it has
// multiple addresses.
Expand Down Expand Up @@ -87,8 +84,6 @@
cplFnc func(peer.ID) int

cplPeerGroups map[int]map[peer.ID][]PeerIPGroupKey

asnStore asnStore
}

// NewFilter creates a Filter for Peer Diversity.
Expand Down Expand Up @@ -117,7 +112,6 @@
logKey: logKey,
cplFnc: cplFnc,
cplPeerGroups: make(map[int]map[peer.ID][]PeerIPGroupKey),
asnStore: asnutil.Store,
}, nil
}

Expand Down Expand Up @@ -167,12 +161,7 @@
}

// reject the peer if we can't determine a grouping for one of it's address.
key, err := f.ipGroupKey(ip)
if err != nil {
dfLog.Errorw("failed to find Group Key", "appKey", f.logKey, "ip", ip.String(), "peer", p,
"err", err)
return false
}
key := f.ipGroupKey(ip)
if len(key) == 0 {
dfLog.Errorw("group key is empty", "appKey", f.logKey, "ip", ip.String(), "peer", p)
return false
Expand Down Expand Up @@ -211,34 +200,30 @@
}

// returns the PeerIPGroupKey to which the given IP belongs.
func (f *Filter) ipGroupKey(ip net.IP) (PeerIPGroupKey, error) {
func (f *Filter) ipGroupKey(ip net.IP) PeerIPGroupKey {
switch bz := ip.To4(); bz {
case nil:
// TODO Clean up the ASN codebase
// ipv6 Address -> get ASN
s, err := f.asnStore.AsnForIPv6(ip)
if err != nil {
return "", fmt.Errorf("failed to fetch ASN for IPv6 addr %s: %w", ip.String(), err)
}
s := asnutil.AsnForIPv6(ip)

// if no ASN found then fallback on using the /32 prefix
if len(s) == 0 {
if s == 0 {
dfLog.Debugw("ASN not known", "appKey", f.logKey, "ip", ip)
s = fmt.Sprintf("unknown ASN: %s", net.CIDRMask(32, 128).String())
return PeerIPGroupKey(fmt.Sprintf("unknown ASN: %s", net.CIDRMask(32, 128).String()))

Check warning on line 212 in peerdiversity/filter.go

View check run for this annotation

Codecov / codecov/patch

peerdiversity/filter.go#L212

Added line #L212 was not covered by tests
}

return PeerIPGroupKey(s), nil
return PeerIPGroupKey(strconv.FormatUint(uint64(s), 10))
default:
// If it belongs to a legacy Class 8, we return the /8 prefix as the key
rs, _ := f.legacyCidrs.ContainingNetworks(ip)
if len(rs) != 0 {
key := ip.Mask(net.IPv4Mask(255, 0, 0, 0)).String()
return PeerIPGroupKey(key), nil
return PeerIPGroupKey(key)
}

// otherwise -> /16 prefix
key := ip.Mask(net.IPv4Mask(255, 255, 0, 0)).String()
return PeerIPGroupKey(key), nil
return PeerIPGroupKey(key)
}
}

Expand Down
22 changes: 6 additions & 16 deletions peerdiversity/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package peerdiversity

import (
"net"
"strconv"
"sync"
"testing"

asnutil "github.com/libp2p/go-libp2p-asn-util"
"github.com/libp2p/go-libp2p/core/peer"

ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -223,38 +225,26 @@ func TestDiversityFilter(t *testing.T) {
}
}

type mockAsnStore struct {
reply string
}

func (m *mockAsnStore) AsnForIPv6(net.IP) (string, error) {
return m.reply, nil
}

func TestIPGroupKey(t *testing.T) {
f, err := NewFilter(newMockPeerGroupFilter(), "test", func(p peer.ID) int { return 1 })
f.asnStore = &mockAsnStore{"test"}
require.NoError(t, err)

// case 1 legacy /8
ip := net.ParseIP("17.111.0.1")
require.NotNil(t, ip.To4())
g, err := f.ipGroupKey(ip)
require.NoError(t, err)
g := f.ipGroupKey(ip)
require.Equal(t, "17.0.0.0", string(g))

// case2 ip4 /16
ip = net.ParseIP("192.168.1.1")
require.NotNil(t, ip.To4())
g, err = f.ipGroupKey(ip)
require.NoError(t, err)
g = f.ipGroupKey(ip)
require.Equal(t, "192.168.0.0", string(g))

// case3 ipv6
ip = net.ParseIP("2a03:2880:f003:c07:face:b00c::2")
g, err = f.ipGroupKey(ip)
require.NoError(t, err)
require.Equal(t, "test", string(g))
g = f.ipGroupKey(ip)
require.Equal(t, strconv.FormatUint(uint64(asnutil.AsnForIPv6(ip)), 10), string(g))
}

func TestGetDiversityStats(t *testing.T) {
Expand Down