2018-04-16 12:36:09 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2019-03-21 12:02:16 +00:00
|
|
|
"fmt"
|
2018-04-16 12:36:09 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
2018-08-17 06:25:55 +00:00
|
|
|
"net"
|
2019-03-21 12:02:16 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
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"
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2019-12-09 10:36:14 +00:00
|
|
|
"github.com/status-im/status-go/whisper/v6"
|
2018-04-16 12:36:09 +00:00
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatusNodeStart(t *testing.T) {
|
2018-09-21 14:09:31 +00:00
|
|
|
config, err := utils.MakeTestNodeConfigWithDataDir("", "", params.StatusChainNetworkID)
|
2018-09-13 16:31:29 +00:00
|
|
|
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())
|
|
|
|
// start node
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, n.Start(config, nil))
|
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)
|
|
|
|
// try to start already started node
|
2019-08-20 15:38:40 +00:00
|
|
|
require.EqualError(t, n.Start(config, nil), 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())
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, n.Start(&config, nil))
|
2018-04-16 12:36:09 +00:00
|
|
|
require.NoError(t, n.Stop())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusNodeServiceGetters(t *testing.T) {
|
|
|
|
config := params.NodeConfig{
|
2020-01-08 11:12:23 +00:00
|
|
|
EnableNTPSync: true,
|
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
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, n.Start(&config, nil))
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
// 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",
|
|
|
|
},
|
2019-02-01 17:02:52 +00:00
|
|
|
NoUSB: true,
|
2018-04-16 12:36:09 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, peer.Start())
|
2018-05-14 13:16:45 +00:00
|
|
|
defer func() { require.NoError(t, peer.Stop()) }()
|
2019-10-04 15:21:24 +00:00
|
|
|
peerURL := peer.Server().Self().URLv4()
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
n := New()
|
|
|
|
|
|
|
|
// checks before node is started
|
|
|
|
require.EqualError(t, n.AddPeer(peerURL), ErrNoRunningNode.Error())
|
|
|
|
|
|
|
|
// start status node
|
|
|
|
config := params.NodeConfig{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, n.Start(&config, nil))
|
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) {
|
2020-10-08 11:03:32 +00:00
|
|
|
// Skipping as flaky
|
|
|
|
t.Skip()
|
2018-04-16 12:36:09 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
peer, err := gethnode.New(&gethnode.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
ListenAddr: ":0",
|
|
|
|
},
|
2019-02-01 17:02:52 +00:00
|
|
|
NoUSB: true,
|
2018-04-16 12:36:09 +00:00
|
|
|
})
|
|
|
|
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
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
peerURL := peer.Server().Self().URLv4()
|
2018-05-14 13:16:45 +00:00
|
|
|
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},
|
|
|
|
},
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, n.Start(&config, nil))
|
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-11-14 07:03:58 +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
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
parsedPeer, err := enode.ParseV4(peerURL)
|
2018-05-14 13:16:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server := node.GethNode().Server()
|
|
|
|
|
|
|
|
for _, peer := range server.PeersInfo() {
|
2018-11-14 07:03:58 +00:00
|
|
|
if peer.ID == parsedPeer.ID().String() {
|
2018-05-14 13:16:45 +00:00
|
|
|
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"},
|
|
|
|
},
|
2019-01-25 18:17:08 +00:00
|
|
|
// use custom address to test the all possibilities
|
|
|
|
AdvertiseAddr: "127.0.0.1",
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
n := New()
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, n.Start(&config, nil))
|
2018-07-04 10:51:47 +00:00
|
|
|
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
|
|
|
|
2019-01-25 18:17:08 +00:00
|
|
|
func TestStatusNodeStartDiscoveryManual(t *testing.T) {
|
|
|
|
config := params.NodeConfig{
|
|
|
|
Rendezvous: true,
|
|
|
|
NoDiscovery: true,
|
|
|
|
ClusterConfig: params.ClusterConfig{
|
|
|
|
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"},
|
|
|
|
},
|
|
|
|
// use custom address to test the all possibilities
|
|
|
|
AdvertiseAddr: "127.0.0.1",
|
|
|
|
}
|
|
|
|
n := New()
|
|
|
|
require.NoError(t, n.StartWithOptions(&config, StartOptions{}))
|
|
|
|
require.Nil(t, n.discovery)
|
|
|
|
// start discovery manually
|
|
|
|
require.NoError(t, n.StartDiscovery())
|
|
|
|
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()
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, n.Start(&config, nil))
|
2018-11-14 07:03:58 +00:00
|
|
|
node, err := n.discoverNode()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, net.ParseIP("127.0.0.1").To4(), node.IP())
|
2018-08-17 06:25:55 +00:00
|
|
|
|
|
|
|
config = params.NodeConfig{
|
|
|
|
NoDiscovery: true,
|
|
|
|
AdvertiseAddr: "127.0.0.2",
|
|
|
|
ListenAddr: "127.0.0.1:0",
|
|
|
|
}
|
|
|
|
n = New()
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, n.Start(&config, nil))
|
2018-11-14 07:03:58 +00:00
|
|
|
node, err = n.discoverNode()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, net.ParseIP("127.0.0.2").To4(), node.IP())
|
2018-08-17 06:25:55 +00:00
|
|
|
}
|
2019-03-21 12:02:16 +00:00
|
|
|
|
|
|
|
func TestChaosModeCheckRPCClientsUpstreamURL(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, `{
|
|
|
|
"id": 1,
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"result": 1
|
|
|
|
}`)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
config := params.NodeConfig{
|
|
|
|
NoDiscovery: true,
|
|
|
|
ListenAddr: "127.0.0.1:0",
|
|
|
|
UpstreamConfig: params.UpstreamRPCConfig{
|
|
|
|
Enabled: true,
|
|
|
|
// put "infura.io" substring to simulate blocking an actual infura.io URLs
|
|
|
|
URL: ts.URL + "?actualURL=infura.io",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
n := New()
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, n.Start(&config, nil))
|
2019-03-21 12:02:16 +00:00
|
|
|
defer func() { require.NoError(t, n.Stop()) }()
|
|
|
|
require.NotNil(t, n.RPCClient())
|
|
|
|
|
|
|
|
client := n.RPCClient()
|
|
|
|
require.NotNil(t, client)
|
|
|
|
|
|
|
|
err := client.Call(nil, "net_version")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// act
|
|
|
|
err = n.ChaosModeCheckRPCClientsUpstreamURL(true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// assert
|
|
|
|
err = client.Call(nil, "net_version")
|
2020-02-13 11:48:04 +00:00
|
|
|
require.Error(t, err)
|
2019-03-21 12:02:16 +00:00
|
|
|
|
|
|
|
// act
|
|
|
|
err = n.ChaosModeCheckRPCClientsUpstreamURL(false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// assert
|
|
|
|
err = client.Call(nil, "net_version")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|