2019-08-13 21:12:00 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2019-08-21 06:08:30 +00:00
|
|
|
"context"
|
2019-08-22 15:23:16 +00:00
|
|
|
"crypto/ecdsa"
|
2019-08-21 06:08:30 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-08-13 21:12:00 +00:00
|
|
|
"testing"
|
2019-08-21 06:08:30 +00:00
|
|
|
"time"
|
2019-08-13 21:12:00 +00:00
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2019-08-28 10:59:34 +00:00
|
|
|
libp2p "github.com/libp2p/go-libp2p"
|
2019-08-21 06:08:30 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2019-08-23 16:53:38 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-08-28 10:59:34 +00:00
|
|
|
multiaddr "github.com/multiformats/go-multiaddr"
|
2019-08-13 21:12:00 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
type mockListener struct{}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) Self() *enode.Node {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) Close() {
|
2019-08-21 06:08:30 +00:00
|
|
|
//no-op
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) Lookup(enode.ID) []*enode.Node {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) ReadRandomNodes([]*enode.Node) int {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) Resolve(*enode.Node) *enode.Node {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) LookupRandom() []*enode.Node {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) Ping(*enode.Node) error {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) RequestENR(*enode.Node) (*enode.Node, error) {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func createPeer(t *testing.T, cfg *Config, port int) (Listener, host.Host) {
|
2019-08-22 15:23:16 +00:00
|
|
|
h, pkey, ipAddr := createHost(t, port)
|
|
|
|
cfg.UDPPort = uint(port)
|
2019-09-12 04:52:27 +00:00
|
|
|
cfg.TCPPort = uint(port)
|
2019-08-22 15:23:16 +00:00
|
|
|
listener, err := startDiscoveryV5(ipAddr, pkey, cfg)
|
2019-08-21 06:08:30 +00:00
|
|
|
if err != nil {
|
2019-08-22 15:23:16 +00:00
|
|
|
t.Errorf("Could not start discovery for node: %v", err)
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2019-08-22 15:23:16 +00:00
|
|
|
return listener, h
|
|
|
|
}
|
|
|
|
|
|
|
|
func createHost(t *testing.T, port int) (host.Host, *ecdsa.PrivateKey, net.IP) {
|
|
|
|
ipAddr, pkey := createAddrAndPrivKey(t)
|
|
|
|
ipAddr = net.ParseIP("127.0.0.1")
|
2019-08-21 06:08:30 +00:00
|
|
|
listen, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr, port))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to p2p listen: %v", err)
|
|
|
|
}
|
|
|
|
h, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen)}...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-22 15:23:16 +00:00
|
|
|
return h, pkey, ipAddr
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 21:12:00 +00:00
|
|
|
func TestService_Stop_SetsStartedToFalse(t *testing.T) {
|
2019-08-30 20:15:40 +00:00
|
|
|
s, _ := NewService(&Config{})
|
2019-08-13 21:12:00 +00:00
|
|
|
s.started = true
|
2019-08-21 06:08:30 +00:00
|
|
|
s.dv5Listener = &mockListener{}
|
2019-08-13 21:12:00 +00:00
|
|
|
_ = s.Stop()
|
2019-08-21 06:08:30 +00:00
|
|
|
|
2019-08-13 21:12:00 +00:00
|
|
|
if s.started != false {
|
|
|
|
t.Error("Expected Service.started to be false, got true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 10:59:34 +00:00
|
|
|
func TestService_Stop_DontPanicIfDv5ListenerIsNotInited(t *testing.T) {
|
2019-08-30 20:15:40 +00:00
|
|
|
s, _ := NewService(&Config{})
|
2019-08-28 10:59:34 +00:00
|
|
|
_ = s.Stop()
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:12:00 +00:00
|
|
|
func TestService_Start_OnlyStartsOnce(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
cfg := &Config{
|
2019-09-12 04:52:27 +00:00
|
|
|
TCPPort: 2000,
|
2019-08-24 23:56:40 +00:00
|
|
|
UDPPort: 2000,
|
2019-08-24 16:07:03 +00:00
|
|
|
Encoding: "ssz",
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
s, _ := NewService(cfg)
|
|
|
|
s.dv5Listener = &mockListener{}
|
2019-08-13 21:12:00 +00:00
|
|
|
defer s.Stop()
|
|
|
|
s.Start()
|
|
|
|
if s.started != true {
|
|
|
|
t.Error("Expected service to be started")
|
|
|
|
}
|
|
|
|
s.Start()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Attempted to start p2p service when it was already started")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_Status_NotRunning(t *testing.T) {
|
|
|
|
s := &Service{started: false}
|
2019-08-21 06:08:30 +00:00
|
|
|
s.dv5Listener = &mockListener{}
|
2019-08-13 21:12:00 +00:00
|
|
|
if s.Status().Error() != "not running" {
|
|
|
|
t.Errorf("Status returned wrong error, got %v", s.Status())
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 06:08:30 +00:00
|
|
|
|
|
|
|
func TestListenForNewNodes(t *testing.T) {
|
|
|
|
// setup bootnode
|
2019-09-06 19:20:20 +00:00
|
|
|
cfg := &Config{}
|
2019-08-21 06:08:30 +00:00
|
|
|
port := 2000
|
2019-09-12 04:52:27 +00:00
|
|
|
cfg.UDPPort = uint(port)
|
2019-08-21 06:08:30 +00:00
|
|
|
_, pkey := createAddrAndPrivKey(t)
|
|
|
|
ipAddr := net.ParseIP("127.0.0.1")
|
2019-09-06 19:20:20 +00:00
|
|
|
bootListener := createListener(ipAddr, pkey, cfg)
|
2019-08-21 06:08:30 +00:00
|
|
|
defer bootListener.Close()
|
|
|
|
|
|
|
|
bootNode := bootListener.Self()
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
cfg = &Config{
|
2019-09-23 17:24:16 +00:00
|
|
|
BootstrapNodeAddr: []string{bootNode.String()},
|
|
|
|
Discv5BootStrapAddr: []string{bootNode.String()},
|
|
|
|
Encoding: "ssz",
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
var listeners []*discover.UDPv5
|
2019-08-22 15:23:16 +00:00
|
|
|
var hosts []host.Host
|
2019-08-21 06:08:30 +00:00
|
|
|
// setup other nodes
|
|
|
|
for i := 1; i <= 5; i++ {
|
2019-08-22 15:23:16 +00:00
|
|
|
listener, h := createPeer(t, cfg, port+i)
|
2019-09-06 19:20:20 +00:00
|
|
|
listeners = append(listeners, listener.(*discover.UDPv5))
|
2019-08-22 15:23:16 +00:00
|
|
|
hosts = append(hosts, h)
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 15:23:16 +00:00
|
|
|
// close peers upon exit of test
|
|
|
|
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-21 06:08:30 +00:00
|
|
|
|
|
|
|
s, err := NewService(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Start()
|
|
|
|
defer s.Stop()
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
time.Sleep(4 * time.Second)
|
2019-08-21 06:08:30 +00:00
|
|
|
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))
|
|
|
|
}
|
2019-09-01 22:29:58 +00:00
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
// close down all peers
|
|
|
|
for _, listener := range listeners {
|
|
|
|
listener.Close()
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 16:53:38 +00:00
|
|
|
|
|
|
|
func TestPeer_Disconnect(t *testing.T) {
|
|
|
|
h1, _, _ := createHost(t, 5000)
|
|
|
|
defer h1.Close()
|
|
|
|
|
|
|
|
s := &Service{
|
|
|
|
host: h1,
|
|
|
|
}
|
|
|
|
|
|
|
|
h2, _, ipaddr := createHost(t, 5001)
|
|
|
|
defer h2.Close()
|
|
|
|
|
|
|
|
h2Addr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", ipaddr, 5001, h2.ID()))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addrInfo, err := peer.AddrInfoFromP2pAddr(h2Addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := s.host.Connect(context.Background(), *addrInfo); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(s.host.Network().Peers()) != 1 {
|
|
|
|
t.Fatalf("Number of peers is %d when it was supposed to be %d", len(s.host.Network().Peers()), 1)
|
|
|
|
}
|
|
|
|
if len(s.host.Network().Conns()) != 1 {
|
|
|
|
t.Fatalf("Number of connections is %d when it was supposed to be %d", len(s.host.Network().Conns()), 1)
|
|
|
|
}
|
|
|
|
if err := s.Disconnect(h2.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(s.host.Network().Conns()) != 0 {
|
|
|
|
t.Fatalf("Number of connections is %d when it was supposed to be %d", len(s.host.Network().Conns()), 0)
|
|
|
|
}
|
|
|
|
}
|