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"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
gethnode "github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
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)
|
2021-12-21 15:44:37 +00:00
|
|
|
n := New(nil)
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
}
|
2021-12-21 15:44:37 +00:00
|
|
|
n := New(nil)
|
2018-04-16 12:36:09 +00:00
|
|
|
|
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 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())
|
2021-07-09 13:19:33 +00:00
|
|
|
defer func() { require.NoError(t, peer.Close()) }()
|
2019-10-04 15:21:24 +00:00
|
|
|
peerURL := peer.Server().Self().URLv4()
|
2018-04-16 12:36:09 +00:00
|
|
|
|
2021-12-21 15:44:37 +00:00
|
|
|
n := New(nil)
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-12-21 15:44:37 +00:00
|
|
|
n := New(nil)
|
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",
|
|
|
|
}
|
2021-12-21 15:44:37 +00:00
|
|
|
n := New(nil)
|
2019-01-25 18:17:08 +00:00
|
|
|
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",
|
|
|
|
}
|
2021-12-21 15:44:37 +00:00
|
|
|
n := New(nil)
|
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",
|
|
|
|
}
|
2021-12-21 15:44:37 +00:00
|
|
|
n = New(nil)
|
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
|
|
|
}
|