2018-04-16 12:36:09 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
2018-08-17 06:25:55 +00:00
|
|
|
"net"
|
2018-04-16 12:36:09 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/les"
|
|
|
|
gethnode "github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
"github.com/status-im/status-go/discovery"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-06-27 07:55:25 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
2018-09-13 16:31:29 +00:00
|
|
|
"github.com/status-im/status-go/t/utils"
|
2018-04-16 12:36:09 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatusNodeStart(t *testing.T) {
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2018-04-16 12:36:09 +00:00
|
|
|
n := New()
|
|
|
|
|
|
|
|
// checks before node is started
|
|
|
|
require.Nil(t, n.GethNode())
|
|
|
|
require.Nil(t, n.Config())
|
|
|
|
require.Nil(t, n.RPCClient())
|
|
|
|
require.Equal(t, 0, n.PeerCount())
|
|
|
|
_, err = n.AccountManager()
|
|
|
|
require.EqualError(t, err, ErrNoGethNode.Error())
|
|
|
|
_, err = n.AccountKeyStore()
|
|
|
|
require.EqualError(t, err, ErrNoGethNode.Error())
|
|
|
|
|
|
|
|
// start node
|
2018-09-13 16:31:29 +00:00
|
|
|
require.NoError(t, n.Start(config))
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
// checks after node is started
|
|
|
|
require.True(t, n.IsRunning())
|
|
|
|
require.NotNil(t, n.GethNode())
|
|
|
|
require.NotNil(t, n.Config())
|
|
|
|
require.NotNil(t, n.RPCClient())
|
|
|
|
require.Equal(t, 0, n.PeerCount())
|
|
|
|
accountManager, err := n.AccountManager()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, accountManager)
|
|
|
|
keyStore, err := n.AccountKeyStore()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, keyStore)
|
|
|
|
// try to start already started node
|
2018-09-13 16:31:29 +00:00
|
|
|
require.EqualError(t, n.Start(config), ErrNodeRunning.Error())
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
// stop node
|
|
|
|
require.NoError(t, n.Stop())
|
|
|
|
// try to stop already stopped node
|
|
|
|
require.EqualError(t, n.Stop(), ErrNoRunningNode.Error())
|
|
|
|
|
|
|
|
// checks after node is stopped
|
|
|
|
require.Nil(t, n.GethNode())
|
|
|
|
require.Nil(t, n.RPCClient())
|
|
|
|
require.Equal(t, 0, n.PeerCount())
|
|
|
|
_, err = n.AccountManager()
|
|
|
|
require.EqualError(t, err, ErrNoGethNode.Error())
|
|
|
|
_, err = n.AccountKeyStore()
|
|
|
|
require.EqualError(t, err, ErrNoGethNode.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusNodeWithDataDir(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "status-node-test")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, os.RemoveAll(dir))
|
|
|
|
}()
|
|
|
|
|
|
|
|
// keystore directory
|
|
|
|
keyStoreDir := path.Join(dir, "keystore")
|
|
|
|
err = os.MkdirAll(keyStoreDir, os.ModePerm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
config := params.NodeConfig{
|
|
|
|
DataDir: dir,
|
|
|
|
KeyStoreDir: keyStoreDir,
|
|
|
|
}
|
|
|
|
n := New()
|
|
|
|
|
|
|
|
require.NoError(t, n.Start(&config))
|
|
|
|
require.NoError(t, n.Stop())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusNodeServiceGetters(t *testing.T) {
|
|
|
|
config := params.NodeConfig{
|
2018-09-13 16:31:29 +00:00
|
|
|
WhisperConfig: params.WhisperConfig{
|
2018-04-16 12:36:09 +00:00
|
|
|
Enabled: true,
|
|
|
|
},
|
2018-09-13 16:31:29 +00:00
|
|
|
LightEthConfig: params.LightEthConfig{
|
2018-04-16 12:36:09 +00:00
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
n := New()
|
|
|
|
|
|
|
|
var (
|
|
|
|
instance interface{}
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
services := []struct {
|
|
|
|
getter func() (interface{}, error)
|
|
|
|
typ reflect.Type
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
getter: func() (interface{}, error) {
|
|
|
|
return n.WhisperService()
|
|
|
|
},
|
|
|
|
typ: reflect.TypeOf(&whisper.Whisper{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
getter: func() (interface{}, error) {
|
|
|
|
return n.LightEthereumService()
|
|
|
|
},
|
|
|
|
typ: reflect.TypeOf(&les.LightEthereum{}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
t.Run(service.typ.String(), func(t *testing.T) {
|
|
|
|
// checks before node is started
|
|
|
|
instance, err = service.getter()
|
|
|
|
require.EqualError(t, err, ErrNoRunningNode.Error())
|
|
|
|
require.Nil(t, instance)
|
|
|
|
|
|
|
|
// start node
|
|
|
|
require.NoError(t, n.Start(&config))
|
|
|
|
|
|
|
|
// checks after node is started
|
|
|
|
instance, err = service.getter()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, instance)
|
|
|
|
require.Equal(t, service.typ, reflect.TypeOf(instance))
|
|
|
|
|
|
|
|
// stop node
|
|
|
|
require.NoError(t, n.Stop())
|
|
|
|
|
|
|
|
// checks after node is stopped
|
|
|
|
instance, err = service.getter()
|
|
|
|
require.EqualError(t, err, ErrNoRunningNode.Error())
|
|
|
|
require.Nil(t, instance)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusNodeAddPeer(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
peer, err := gethnode.New(&gethnode.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
ListenAddr: ":0",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, peer.Start())
|
2018-05-14 13:16:45 +00:00
|
|
|
defer func() { require.NoError(t, peer.Stop()) }()
|
2018-04-16 12:36:09 +00:00
|
|
|
peerURL := peer.Server().Self().String()
|
|
|
|
|
|
|
|
n := New()
|
|
|
|
|
|
|
|
// checks before node is started
|
|
|
|
require.EqualError(t, n.AddPeer(peerURL), ErrNoRunningNode.Error())
|
|
|
|
|
|
|
|
// start status node
|
|
|
|
config := params.NodeConfig{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
}
|
|
|
|
require.NoError(t, n.Start(&config))
|
2018-05-14 13:16:45 +00:00
|
|
|
defer func() { require.NoError(t, n.Stop()) }()
|
2018-04-16 12:36:09 +00:00
|
|
|
|
2018-06-27 07:55:25 +00:00
|
|
|
errCh := helpers.WaitForPeerAsync(n.Server(), peerURL, p2p.PeerEventTypeAdd, time.Second*5)
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
// checks after node is started
|
|
|
|
require.NoError(t, n.AddPeer(peerURL))
|
|
|
|
require.NoError(t, <-errCh)
|
|
|
|
require.Equal(t, 1, n.PeerCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusNodeReconnectStaticPeers(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
peer, err := gethnode.New(&gethnode.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
ListenAddr: ":0",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, peer.Start())
|
2018-05-14 13:16:45 +00:00
|
|
|
defer func() { require.NoError(t, peer.Stop()) }()
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
var errCh <-chan error
|
|
|
|
|
2018-05-14 13:16:45 +00:00
|
|
|
peerURL := peer.Server().Self().String()
|
|
|
|
n := New()
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
// checks before node is started
|
|
|
|
require.EqualError(t, n.ReconnectStaticPeers(), ErrNoRunningNode.Error())
|
|
|
|
|
|
|
|
// start status node
|
|
|
|
config := params.NodeConfig{
|
|
|
|
MaxPeers: math.MaxInt32,
|
2018-09-13 16:31:29 +00:00
|
|
|
ClusterConfig: params.ClusterConfig{
|
2018-04-16 12:36:09 +00:00
|
|
|
Enabled: true,
|
|
|
|
StaticNodes: []string{peerURL},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, n.Start(&config))
|
2018-05-14 13:16:45 +00:00
|
|
|
defer func() { require.NoError(t, n.Stop()) }()
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
// checks after node is started
|
2018-05-14 13:16:45 +00:00
|
|
|
// it may happen that the peer is already connected
|
|
|
|
// because it was already added to `StaticNodes`
|
|
|
|
connected, err := isPeerConnected(n, peerURL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if !connected {
|
2018-06-27 07:55:25 +00:00
|
|
|
errCh = helpers.WaitForPeerAsync(n.Server(), peerURL, p2p.PeerEventTypeAdd, time.Second*30)
|
2018-05-14 13:16:45 +00:00
|
|
|
require.NoError(t, <-errCh)
|
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
require.Equal(t, 1, n.PeerCount())
|
2018-05-29 11:24:23 +00:00
|
|
|
require.Equal(t, peer.Server().Self().ID.String(), n.GethNode().Server().PeersInfo()[0].ID)
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
// reconnect static peers
|
2018-06-27 07:55:25 +00:00
|
|
|
errDropCh := helpers.WaitForPeerAsync(n.Server(), peerURL, p2p.PeerEventTypeDrop, time.Second*30)
|
|
|
|
|
2018-05-29 11:24:23 +00:00
|
|
|
// it takes at least 30 seconds to bring back previously connected peer
|
2018-06-27 07:55:25 +00:00
|
|
|
errAddCh := helpers.WaitForPeerAsync(n.Server(), peerURL, p2p.PeerEventTypeAdd, time.Second*60)
|
2018-05-14 13:16:45 +00:00
|
|
|
require.NoError(t, n.ReconnectStaticPeers())
|
|
|
|
// first check if a peer gets disconnected
|
2018-05-29 11:24:23 +00:00
|
|
|
require.NoError(t, <-errDropCh)
|
|
|
|
require.NoError(t, <-errAddCh)
|
2018-04-16 12:36:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 13:16:45 +00:00
|
|
|
func isPeerConnected(node *StatusNode, peerURL string) (bool, error) {
|
|
|
|
if !node.IsRunning() {
|
|
|
|
return false, ErrNoRunningNode
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedPeer, err := discover.ParseNode(peerURL)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server := node.GethNode().Server()
|
|
|
|
|
|
|
|
for _, peer := range server.PeersInfo() {
|
|
|
|
if peer.ID == parsedPeer.ID.String() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
func TestStatusNodeRendezvousDiscovery(t *testing.T) {
|
|
|
|
config := params.NodeConfig{
|
|
|
|
Rendezvous: true,
|
|
|
|
NoDiscovery: true,
|
2018-09-13 16:31:29 +00:00
|
|
|
ClusterConfig: params.ClusterConfig{
|
2018-07-04 10:51:47 +00:00
|
|
|
Enabled: true,
|
|
|
|
// not necessarily with id, just valid multiaddr
|
|
|
|
RendezvousNodes: []string{"/ip4/127.0.0.1/tcp/34012", "/ip4/127.0.0.1/tcp/34011"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
n := New()
|
|
|
|
require.NoError(t, n.Start(&config))
|
|
|
|
require.NotNil(t, n.discovery)
|
|
|
|
require.True(t, n.discovery.Running())
|
|
|
|
require.IsType(t, &discovery.Rendezvous{}, n.discovery)
|
|
|
|
}
|
2018-08-17 06:25:55 +00:00
|
|
|
|
|
|
|
func TestStatusNodeDiscoverNode(t *testing.T) {
|
|
|
|
config := params.NodeConfig{
|
|
|
|
NoDiscovery: true,
|
|
|
|
ListenAddr: "127.0.0.1:0",
|
|
|
|
}
|
|
|
|
n := New()
|
|
|
|
require.NoError(t, n.Start(&config))
|
|
|
|
require.Equal(t, net.ParseIP("127.0.0.1").To4(), n.discoverNode().IP)
|
|
|
|
|
|
|
|
config = params.NodeConfig{
|
|
|
|
NoDiscovery: true,
|
|
|
|
AdvertiseAddr: "127.0.0.2",
|
|
|
|
ListenAddr: "127.0.0.1:0",
|
|
|
|
}
|
|
|
|
n = New()
|
|
|
|
require.NoError(t, n.Start(&config))
|
|
|
|
require.Equal(t, net.ParseIP("127.0.0.2"), n.discoverNode().IP)
|
|
|
|
}
|