2020-06-14 14:47:58 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-30 18:42:22 +00:00
|
|
|
"github.com/kevinms/leakybucket-go"
|
2020-06-14 14:47:58 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers"
|
2020-10-07 13:08:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers/scorers"
|
2020-07-14 16:51:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-06-14 14:47:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPeer_AtMaxLimit(t *testing.T) {
|
|
|
|
// create host and remote peer
|
|
|
|
ipAddr, pkey := createAddrAndPrivKey(t)
|
|
|
|
ipAddr2, pkey2 := createAddrAndPrivKey(t)
|
|
|
|
|
|
|
|
listen, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr, 2000))
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err, "Failed to p2p listen")
|
2020-07-30 18:42:22 +00:00
|
|
|
s := &Service{
|
|
|
|
ipLimiter: leakybucket.NewCollector(ipLimit, ipBurst, false),
|
|
|
|
}
|
2020-07-19 03:45:08 +00:00
|
|
|
s.peers = peers.NewStatus(context.Background(), &peers.StatusConfig{
|
2020-07-20 15:12:59 +00:00
|
|
|
PeerLimit: 0,
|
2020-10-07 13:08:51 +00:00
|
|
|
ScorerParams: &scorers.Config{
|
|
|
|
BadResponsesScorerConfig: &scorers.BadResponsesScorerConfig{
|
2020-07-29 09:26:46 +00:00
|
|
|
Threshold: 3,
|
|
|
|
},
|
2020-07-20 15:12:59 +00:00
|
|
|
},
|
2020-07-19 03:45:08 +00:00
|
|
|
})
|
2020-06-14 14:47:58 +00:00
|
|
|
s.cfg = &Config{MaxPeers: 0}
|
|
|
|
s.addrFilter, err = configureFilter(&Config{})
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
h1, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
s.host = h1
|
|
|
|
defer func() {
|
|
|
|
err := h1.Close()
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// create alternate host
|
|
|
|
listen, err = multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr2, 3000))
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err, "Failed to p2p listen")
|
2020-06-14 14:47:58 +00:00
|
|
|
h2, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey2), libp2p.ListenAddrs(listen)}...)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
defer func() {
|
|
|
|
err := h2.Close()
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
}()
|
|
|
|
multiAddress, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", ipAddr, 2000, h1.ID()))
|
2020-10-01 18:53:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
addrInfo, err := peer.AddrInfoFromP2pAddr(multiAddress)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
err = h2.Connect(context.Background(), *addrInfo)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NotNil(t, err, "Wanted connection to fail with max peer")
|
2020-06-14 14:47:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 18:42:22 +00:00
|
|
|
func TestService_InterceptBannedIP(t *testing.T) {
|
|
|
|
s := &Service{
|
|
|
|
ipLimiter: leakybucket.NewCollector(ipLimit, ipBurst, false),
|
2020-11-13 12:58:13 +00:00
|
|
|
peers: peers.NewStatus(context.Background(), &peers.StatusConfig{
|
|
|
|
ScorerParams: &scorers.Config{},
|
|
|
|
}),
|
2020-07-30 18:42:22 +00:00
|
|
|
}
|
|
|
|
var err error
|
|
|
|
s.addrFilter, err = configureFilter(&Config{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
ip := "212.67.10.122"
|
|
|
|
multiAddress, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for i := 0; i < ipBurst; i++ {
|
|
|
|
valid := s.validateDial(multiAddress)
|
|
|
|
if !valid {
|
|
|
|
t.Errorf("Expected multiaddress with ip %s to not be rejected", ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
valid := s.validateDial(multiAddress)
|
|
|
|
if valid {
|
|
|
|
t.Errorf("Expected multiaddress with ip %s to be rejected as it exceeds the burst limit", ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 14:47:58 +00:00
|
|
|
func TestPeer_BelowMaxLimit(t *testing.T) {
|
|
|
|
// create host and remote peer
|
|
|
|
ipAddr, pkey := createAddrAndPrivKey(t)
|
|
|
|
ipAddr2, pkey2 := createAddrAndPrivKey(t)
|
|
|
|
|
|
|
|
listen, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr, 2000))
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err, "Failed to p2p listen")
|
2020-07-30 18:42:22 +00:00
|
|
|
s := &Service{
|
|
|
|
ipLimiter: leakybucket.NewCollector(ipLimit, ipBurst, false),
|
|
|
|
}
|
2020-07-19 03:45:08 +00:00
|
|
|
s.peers = peers.NewStatus(context.Background(), &peers.StatusConfig{
|
2020-07-20 15:12:59 +00:00
|
|
|
PeerLimit: 1,
|
2020-10-07 13:08:51 +00:00
|
|
|
ScorerParams: &scorers.Config{
|
|
|
|
BadResponsesScorerConfig: &scorers.BadResponsesScorerConfig{
|
2020-07-29 09:26:46 +00:00
|
|
|
Threshold: 3,
|
|
|
|
},
|
2020-07-20 15:12:59 +00:00
|
|
|
},
|
2020-07-19 03:45:08 +00:00
|
|
|
})
|
2020-06-14 14:47:58 +00:00
|
|
|
s.cfg = &Config{MaxPeers: 1}
|
|
|
|
s.addrFilter, err = configureFilter(&Config{})
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
h1, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
s.host = h1
|
|
|
|
defer func() {
|
|
|
|
err := h1.Close()
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// create alternate host
|
|
|
|
listen, err = multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr2, 3000))
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err, "Failed to p2p listen")
|
2020-06-14 14:47:58 +00:00
|
|
|
h2, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey2), libp2p.ListenAddrs(listen)}...)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
defer func() {
|
|
|
|
err := h2.Close()
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
}()
|
|
|
|
multiAddress, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", ipAddr, 2000, h1.ID()))
|
2020-10-01 18:53:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
addrInfo, err := peer.AddrInfoFromP2pAddr(multiAddress)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
err = h2.Connect(context.Background(), *addrInfo)
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.NoError(t, err, "Wanted connection to succeed")
|
2020-06-14 14:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerAllowList(t *testing.T) {
|
|
|
|
// create host with allow list
|
|
|
|
ipAddr, pkey := createAddrAndPrivKey(t)
|
|
|
|
ipAddr2, pkey2 := createAddrAndPrivKey(t)
|
|
|
|
|
|
|
|
// use unattainable subnet, which will lead to
|
|
|
|
// peer rejecting all peers, except for those
|
|
|
|
// from that subnet.
|
|
|
|
cidr := "202.35.89.12/16"
|
|
|
|
|
|
|
|
listen, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr, 2000))
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err, "Failed to p2p listen")
|
2020-07-30 18:42:22 +00:00
|
|
|
s := &Service{
|
|
|
|
ipLimiter: leakybucket.NewCollector(ipLimit, ipBurst, false),
|
2020-11-13 12:58:13 +00:00
|
|
|
peers: peers.NewStatus(context.Background(), &peers.StatusConfig{
|
|
|
|
ScorerParams: &scorers.Config{},
|
|
|
|
}),
|
2020-07-30 18:42:22 +00:00
|
|
|
}
|
2020-06-14 14:47:58 +00:00
|
|
|
s.addrFilter, err = configureFilter(&Config{AllowListCIDR: cidr})
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
h1, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
s.host = h1
|
|
|
|
defer func() {
|
|
|
|
err := h1.Close()
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// create alternate host
|
|
|
|
listen, err = multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr2, 3000))
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err, "Failed to p2p listen")
|
2020-06-14 14:47:58 +00:00
|
|
|
h2, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey2), libp2p.ListenAddrs(listen)}...)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
defer func() {
|
|
|
|
err := h2.Close()
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
}()
|
|
|
|
multiAddress, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", ipAddr2, 3000, h2.ID()))
|
2020-10-01 18:53:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
addrInfo, err := peer.AddrInfoFromP2pAddr(multiAddress)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
err = h1.Connect(context.Background(), *addrInfo)
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.NotNil(t, err, "Wanted connection to fail with allow list")
|
|
|
|
assert.ErrorContains(t, "no good addresses", err)
|
2020-06-14 14:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerDenyList(t *testing.T) {
|
|
|
|
// create host with deny list
|
|
|
|
ipAddr, pkey := createAddrAndPrivKey(t)
|
|
|
|
ipAddr2, pkey2 := createAddrAndPrivKey(t)
|
|
|
|
|
|
|
|
mask := ipAddr2.DefaultMask()
|
|
|
|
ones, _ := mask.Size()
|
|
|
|
maskedIP := ipAddr2.Mask(mask)
|
|
|
|
cidr := maskedIP.String() + fmt.Sprintf("/%d", ones)
|
|
|
|
|
|
|
|
listen, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr, 2000))
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err, "Failed to p2p listen")
|
2020-07-30 18:42:22 +00:00
|
|
|
s := &Service{
|
|
|
|
ipLimiter: leakybucket.NewCollector(ipLimit, ipBurst, false),
|
2020-11-13 12:58:13 +00:00
|
|
|
peers: peers.NewStatus(context.Background(), &peers.StatusConfig{
|
|
|
|
ScorerParams: &scorers.Config{},
|
|
|
|
}),
|
2020-07-30 18:42:22 +00:00
|
|
|
}
|
2020-06-14 14:47:58 +00:00
|
|
|
s.addrFilter, err = configureFilter(&Config{DenyListCIDR: []string{cidr}})
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
h1, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
s.host = h1
|
|
|
|
defer func() {
|
|
|
|
err := h1.Close()
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// create alternate host
|
|
|
|
listen, err = multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr2, 3000))
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err, "Failed to p2p listen")
|
2020-06-14 14:47:58 +00:00
|
|
|
h2, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey2), libp2p.ListenAddrs(listen)}...)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
defer func() {
|
|
|
|
err := h2.Close()
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
}()
|
|
|
|
multiAddress, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", ipAddr2, 3000, h2.ID()))
|
2020-10-01 18:53:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
addrInfo, err := peer.AddrInfoFromP2pAddr(multiAddress)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
err = h1.Connect(context.Background(), *addrInfo)
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.NotNil(t, err, "Wanted connection to fail with deny list")
|
|
|
|
assert.ErrorContains(t, "no good addresses", err)
|
2020-06-14 14:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_InterceptAddrDial_Allow(t *testing.T) {
|
2020-07-30 18:42:22 +00:00
|
|
|
s := &Service{
|
|
|
|
ipLimiter: leakybucket.NewCollector(ipLimit, ipBurst, false),
|
2020-11-13 12:58:13 +00:00
|
|
|
peers: peers.NewStatus(context.Background(), &peers.StatusConfig{
|
|
|
|
ScorerParams: &scorers.Config{},
|
|
|
|
}),
|
2020-07-30 18:42:22 +00:00
|
|
|
}
|
2020-06-14 14:47:58 +00:00
|
|
|
var err error
|
|
|
|
cidr := "212.67.89.112/16"
|
|
|
|
s.addrFilter, err = configureFilter(&Config{AllowListCIDR: cidr})
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
ip := "212.67.10.122"
|
|
|
|
multiAddress, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 14:47:58 +00:00
|
|
|
valid := s.InterceptAddrDial("", multiAddress)
|
|
|
|
if !valid {
|
|
|
|
t.Errorf("Expected multiaddress with ip %s to not be rejected with an allow cidr mask of %s", ip, cidr)
|
|
|
|
}
|
|
|
|
}
|