2018-04-16 14:36:09 +02:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
2018-08-17 08:25:55 +02:00
|
|
|
"net"
|
2018-04-16 14:36:09 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-10-28 21:54:17 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2018-04-16 14:36:09 +02:00
|
|
|
gethnode "github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2020-01-02 10:10:19 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-06-08 13:29:50 +02:00
|
|
|
"github.com/status-im/status-go/params"
|
2024-10-28 21:54:17 +01:00
|
|
|
"github.com/status-im/status-go/protocol/tt"
|
2018-06-27 09:55:25 +02:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
2018-09-13 18:31:29 +02:00
|
|
|
"github.com/status-im/status-go/t/utils"
|
2018-04-16 14:36:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatusNodeStart(t *testing.T) {
|
2018-09-21 16:09:31 +02:00
|
|
|
config, err := utils.MakeTestNodeConfigWithDataDir("", "", params.StatusChainNetworkID)
|
2018-09-13 18:31:29 +02:00
|
|
|
require.NoError(t, err)
|
2024-10-28 21:54:17 +01:00
|
|
|
n := New(nil, tt.MustCreateTestLogger())
|
2018-04-16 14:36:09 +02: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())
|
2023-03-27 16:33:00 +02:00
|
|
|
|
2023-08-23 09:39:49 +02:00
|
|
|
appDB, walletDB, stop, err := setupTestDBs()
|
2023-03-27 16:33:00 +02:00
|
|
|
defer func() {
|
|
|
|
err := stop()
|
|
|
|
if err != nil {
|
2024-10-28 21:54:17 +01:00
|
|
|
n.logger.Error("stopping db", zap.Error(err))
|
2023-03-27 16:33:00 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
require.NoError(t, err)
|
2023-08-23 09:39:49 +02:00
|
|
|
n.appDB = appDB
|
|
|
|
n.walletDB = walletDB
|
2023-03-27 16:33:00 +02:00
|
|
|
|
2018-04-16 14:36:09 +02:00
|
|
|
// start node
|
2019-08-20 18:38:40 +03:00
|
|
|
require.NoError(t, n.Start(config, nil))
|
2018-04-16 14:36:09 +02: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 18:38:40 +03:00
|
|
|
require.EqualError(t, n.Start(config, nil), ErrNodeRunning.Error())
|
2018-04-16 14:36:09 +02: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) {
|
2023-04-27 04:39:51 +08:00
|
|
|
dir := t.TempDir()
|
2018-04-16 14:36:09 +02:00
|
|
|
|
|
|
|
// keystore directory
|
|
|
|
keyStoreDir := path.Join(dir, "keystore")
|
2023-04-27 04:39:51 +08:00
|
|
|
err := os.MkdirAll(keyStoreDir, os.ModePerm)
|
2018-04-16 14:36:09 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
config := params.NodeConfig{
|
|
|
|
DataDir: dir,
|
|
|
|
KeyStoreDir: keyStoreDir,
|
|
|
|
}
|
2023-03-27 16:33:00 +02:00
|
|
|
|
|
|
|
n, stop1, stop2, err := createStatusNode()
|
|
|
|
defer func() {
|
|
|
|
err := stop1()
|
|
|
|
if err != nil {
|
2024-10-28 21:54:17 +01:00
|
|
|
n.logger.Error("stopping db", zap.Error(err))
|
2023-03-27 16:33:00 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := stop2()
|
|
|
|
if err != nil {
|
2024-10-28 21:54:17 +01:00
|
|
|
n.logger.Error("stopping multiaccount db", zap.Error(err))
|
2023-03-27 16:33:00 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
require.NoError(t, err)
|
2018-04-16 14:36:09 +02:00
|
|
|
|
2019-08-20 18:38:40 +03:00
|
|
|
require.NoError(t, n.Start(&config, nil))
|
2018-04-16 14:36:09 +02: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 18:02:52 +01:00
|
|
|
NoUSB: true,
|
2018-04-16 14:36:09 +02:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, peer.Start())
|
2021-07-09 15:19:33 +02:00
|
|
|
defer func() { require.NoError(t, peer.Close()) }()
|
2019-10-04 17:21:24 +02:00
|
|
|
peerURL := peer.Server().Self().URLv4()
|
2018-04-16 14:36:09 +02:00
|
|
|
|
2023-03-27 16:33:00 +02:00
|
|
|
n, stop1, stop2, err := createStatusNode()
|
|
|
|
defer func() {
|
|
|
|
err := stop1()
|
|
|
|
if err != nil {
|
2024-10-28 21:54:17 +01:00
|
|
|
n.logger.Error("stopping db", zap.Error(err))
|
2023-03-27 16:33:00 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := stop2()
|
|
|
|
if err != nil {
|
2024-10-28 21:54:17 +01:00
|
|
|
n.logger.Error("stopping multiaccount db", zap.Error(err))
|
2023-03-27 16:33:00 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
require.NoError(t, err)
|
2018-04-16 14:36:09 +02: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 18:38:40 +03:00
|
|
|
require.NoError(t, n.Start(&config, nil))
|
2018-05-14 15:16:45 +02:00
|
|
|
defer func() { require.NoError(t, n.Stop()) }()
|
2018-04-16 14:36:09 +02:00
|
|
|
|
2018-06-27 09:55:25 +02:00
|
|
|
errCh := helpers.WaitForPeerAsync(n.Server(), peerURL, p2p.PeerEventTypeAdd, time.Second*5)
|
2018-04-16 14:36:09 +02:00
|
|
|
|
|
|
|
// checks after node is started
|
|
|
|
require.NoError(t, n.AddPeer(peerURL))
|
|
|
|
require.NoError(t, <-errCh)
|
|
|
|
require.Equal(t, 1, n.PeerCount())
|
|
|
|
}
|
|
|
|
|
2018-08-17 08:25:55 +02:00
|
|
|
func TestStatusNodeDiscoverNode(t *testing.T) {
|
|
|
|
config := params.NodeConfig{
|
|
|
|
NoDiscovery: true,
|
|
|
|
ListenAddr: "127.0.0.1:0",
|
|
|
|
}
|
2023-03-27 16:33:00 +02:00
|
|
|
|
|
|
|
n, stop1, stop2, err := createStatusNode()
|
|
|
|
defer func() {
|
|
|
|
err := stop1()
|
|
|
|
if err != nil {
|
2024-10-28 21:54:17 +01:00
|
|
|
n.logger.Error("stopping db", zap.Error(err))
|
2023-03-27 16:33:00 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := stop2()
|
|
|
|
if err != nil {
|
2024-10-28 21:54:17 +01:00
|
|
|
n.logger.Error("stopping multiaccount db", zap.Error(err))
|
2023-03-27 16:33:00 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-08-20 18:38:40 +03:00
|
|
|
require.NoError(t, n.Start(&config, nil))
|
2018-11-14 08:03:58 +01:00
|
|
|
node, err := n.discoverNode()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, net.ParseIP("127.0.0.1").To4(), node.IP())
|
2018-08-17 08:25:55 +02:00
|
|
|
|
|
|
|
config = params.NodeConfig{
|
|
|
|
NoDiscovery: true,
|
|
|
|
AdvertiseAddr: "127.0.0.2",
|
|
|
|
ListenAddr: "127.0.0.1:0",
|
|
|
|
}
|
2023-03-27 16:33:00 +02:00
|
|
|
|
|
|
|
n1, stop11, stop12, err := createStatusNode()
|
|
|
|
defer func() {
|
|
|
|
err := stop11()
|
|
|
|
if err != nil {
|
2024-10-28 21:54:17 +01:00
|
|
|
n1.logger.Error("stopping db", zap.Error(err))
|
2023-03-27 16:33:00 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := stop12()
|
|
|
|
if err != nil {
|
2024-10-28 21:54:17 +01:00
|
|
|
n1.logger.Error("stopping multiaccount db", zap.Error(err))
|
2023-03-27 16:33:00 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, n1.Start(&config, nil))
|
|
|
|
node, err = n1.discoverNode()
|
2018-11-14 08:03:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, net.ParseIP("127.0.0.2").To4(), node.IP())
|
2018-08-17 08:25:55 +02:00
|
|
|
}
|