2019-08-21 06:08:30 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2019-08-22 15:23:16 +00:00
|
|
|
"fmt"
|
2019-08-21 06:08:30 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2019-08-22 15:23:16 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2019-08-21 06:08:30 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/iputils"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
var discoveryWaitTime = 1 * time.Second
|
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
func createAddrAndPrivKey(t *testing.T) (net.IP, *ecdsa.PrivateKey) {
|
|
|
|
ip, err := iputils.ExternalIPv4()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not get ip: %v", err)
|
|
|
|
}
|
|
|
|
ipAddr := net.ParseIP(ip)
|
2019-08-24 16:07:03 +00:00
|
|
|
pkey, err := privKey(&Config{Encoding: "ssz"})
|
2019-08-21 06:08:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not get private key: %v", err)
|
|
|
|
}
|
|
|
|
return ipAddr, pkey
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateListener(t *testing.T) {
|
|
|
|
port := 1024
|
|
|
|
ipAddr, pkey := createAddrAndPrivKey(t)
|
2019-09-12 04:52:27 +00:00
|
|
|
listener := createListener(ipAddr, pkey, &Config{UDPPort: uint(port)})
|
2019-08-21 06:08:30 +00:00
|
|
|
defer listener.Close()
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
if !listener.Self().IP().Equal(ipAddr) {
|
|
|
|
t.Errorf("Ip address is not the expected type, wanted %s but got %s", ipAddr.String(), listener.Self().IP().String())
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
if port != int(listener.Self().UDP()) {
|
|
|
|
t.Errorf("In correct port number, wanted %d but got %d", port, listener.Self().UDP())
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
pubkey := listener.Self().Pubkey()
|
2019-08-21 06:08:30 +00:00
|
|
|
XisSame := pkey.PublicKey.X.Cmp(pubkey.X) == 0
|
|
|
|
YisSame := pkey.PublicKey.Y.Cmp(pubkey.Y) == 0
|
|
|
|
|
|
|
|
if !(XisSame && YisSame) {
|
|
|
|
t.Error("Pubkey is different from what was used to create the listener")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartDiscV5_DiscoverAllPeers(t *testing.T) {
|
|
|
|
port := 2000
|
|
|
|
ipAddr, pkey := createAddrAndPrivKey(t)
|
2019-09-12 04:52:27 +00:00
|
|
|
bootListener := createListener(ipAddr, pkey, &Config{UDPPort: uint(port)})
|
2019-08-21 06:08:30 +00:00
|
|
|
defer bootListener.Close()
|
|
|
|
|
|
|
|
bootNode := bootListener.Self()
|
|
|
|
cfg := &Config{
|
|
|
|
BootstrapNodeAddr: bootNode.String(),
|
2019-08-24 23:56:40 +00:00
|
|
|
Encoding: "ssz",
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
var listeners []*discover.UDPv5
|
2019-08-29 16:32:52 +00:00
|
|
|
for i := 1; i <= 5; i++ {
|
2019-09-06 19:20:20 +00:00
|
|
|
port = 3000 + i
|
2019-09-12 04:52:27 +00:00
|
|
|
cfg.UDPPort = uint(port)
|
2019-08-21 06:08:30 +00:00
|
|
|
ipAddr, pkey := createAddrAndPrivKey(t)
|
|
|
|
listener, err := startDiscoveryV5(ipAddr, pkey, cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not start discovery for node: %v", err)
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the nodes to have their local routing tables to be populated with the other nodes
|
2019-09-06 19:20:20 +00:00
|
|
|
time.Sleep(discoveryWaitTime)
|
2019-08-21 06:08:30 +00:00
|
|
|
|
|
|
|
lastListener := listeners[len(listeners)-1]
|
2019-09-06 19:20:20 +00:00
|
|
|
nodes := lastListener.Lookup(bootNode.ID())
|
|
|
|
if len(nodes) < 4 {
|
2019-08-21 06:08:30 +00:00
|
|
|
t.Errorf("The node's local table doesn't have the expected number of nodes. "+
|
2019-09-06 19:20:20 +00:00
|
|
|
"Expected more than or equal to %d but got %d", 4, len(nodes))
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close all ports
|
|
|
|
for _, listener := range listeners {
|
|
|
|
listener.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiAddrsConversion_InvalidIPAddr(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ipAddr := net.IPv6zero
|
2019-09-06 19:20:20 +00:00
|
|
|
_, pkey := createAddrAndPrivKey(t)
|
2019-09-12 04:52:27 +00:00
|
|
|
node, err := createLocalNode(pkey, ipAddr, 0, 0)
|
2019-08-21 06:08:30 +00:00
|
|
|
if err != nil {
|
2019-09-06 19:20:20 +00:00
|
|
|
t.Fatal(err)
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
_ = convertToMultiAddr([]*enode.Node{node.Node()})
|
2019-09-01 22:29:58 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "node doesn't have an ip4 address")
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiAddrConversion_OK(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ipAddr, pkey := createAddrAndPrivKey(t)
|
2019-09-06 19:20:20 +00:00
|
|
|
listener := createListener(ipAddr, pkey, &Config{})
|
2019-08-21 06:08:30 +00:00
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
_ = convertToMultiAddr([]*enode.Node{listener.Self()})
|
2019-08-21 06:08:30 +00:00
|
|
|
testutil.AssertLogsDoNotContain(t, hook, "Node doesn't have an ip4 address")
|
|
|
|
testutil.AssertLogsDoNotContain(t, hook, "Invalid port, the tcp port of the node is a reserved port")
|
|
|
|
testutil.AssertLogsDoNotContain(t, hook, "Could not get multiaddr")
|
|
|
|
}
|
2019-08-22 15:23:16 +00:00
|
|
|
|
|
|
|
func TestStaticPeering_PeersAreAdded(t *testing.T) {
|
2019-08-24 16:07:03 +00:00
|
|
|
cfg := &Config{Encoding: "ssz"}
|
2019-08-22 15:23:16 +00:00
|
|
|
port := 3000
|
|
|
|
var staticPeers []string
|
|
|
|
var hosts []host.Host
|
|
|
|
// setup other nodes
|
|
|
|
for i := 1; i <= 5; i++ {
|
|
|
|
h, _, ipaddr := createHost(t, port+i)
|
|
|
|
staticPeers = append(staticPeers, fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", ipaddr, port+i, h.ID()))
|
|
|
|
hosts = append(hosts, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
for _, h := range hosts {
|
|
|
|
_ = h.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-09-12 04:52:27 +00:00
|
|
|
cfg.TCPPort = 14001
|
2019-08-30 20:15:40 +00:00
|
|
|
cfg.UDPPort = 14000
|
2019-08-22 15:23:16 +00:00
|
|
|
cfg.StaticPeers = staticPeers
|
|
|
|
|
|
|
|
s, err := NewService(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Start()
|
|
|
|
s.dv5Listener = &mockListener{}
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
peers := s.host.Network().Peers()
|
|
|
|
if len(peers) != 5 {
|
|
|
|
t.Errorf("Not all peers added to peerstore, wanted %d but got %d", 5, len(peers))
|
|
|
|
}
|
|
|
|
}
|