2018-04-16 12:36:09 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
2018-08-17 06:25:55 +00:00
|
|
|
"net"
|
2018-04-16 12:36:09 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
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-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/protocol/tt"
|
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)
|
2024-10-28 20:54:17 +00:00
|
|
|
n := New(nil, tt.MustCreateTestLogger())
|
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())
|
2023-03-27 14:33:00 +00:00
|
|
|
|
2023-08-23 07:39:49 +00:00
|
|
|
appDB, walletDB, stop, err := setupTestDBs()
|
2023-03-27 14:33:00 +00:00
|
|
|
defer func() {
|
|
|
|
err := stop()
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
n.logger.Error("stopping db", zap.Error(err))
|
2023-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
require.NoError(t, err)
|
2023-08-23 07:39:49 +00:00
|
|
|
n.appDB = appDB
|
|
|
|
n.walletDB = walletDB
|
2023-03-27 14:33:00 +00:00
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
// 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) {
|
2023-04-26 20:39:51 +00:00
|
|
|
dir := t.TempDir()
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
// keystore directory
|
|
|
|
keyStoreDir := path.Join(dir, "keystore")
|
2023-04-26 20:39:51 +00:00
|
|
|
err := os.MkdirAll(keyStoreDir, os.ModePerm)
|
2018-04-16 12:36:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
config := params.NodeConfig{
|
|
|
|
DataDir: dir,
|
|
|
|
KeyStoreDir: keyStoreDir,
|
|
|
|
}
|
2023-03-27 14:33:00 +00:00
|
|
|
|
|
|
|
n, stop1, stop2, err := createStatusNode()
|
|
|
|
defer func() {
|
|
|
|
err := stop1()
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
n.logger.Error("stopping db", zap.Error(err))
|
2023-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := stop2()
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
n.logger.Error("stopping multiaccount db", zap.Error(err))
|
2023-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
require.NoError(t, err)
|
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
|
|
|
|
2023-03-27 14:33:00 +00:00
|
|
|
n, stop1, stop2, err := createStatusNode()
|
|
|
|
defer func() {
|
|
|
|
err := stop1()
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
n.logger.Error("stopping db", zap.Error(err))
|
2023-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := stop2()
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
n.logger.Error("stopping multiaccount db", zap.Error(err))
|
2023-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
require.NoError(t, err)
|
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-08-17 06:25:55 +00:00
|
|
|
func TestStatusNodeDiscoverNode(t *testing.T) {
|
|
|
|
config := params.NodeConfig{
|
|
|
|
NoDiscovery: true,
|
|
|
|
ListenAddr: "127.0.0.1:0",
|
|
|
|
}
|
2023-03-27 14:33:00 +00:00
|
|
|
|
|
|
|
n, stop1, stop2, err := createStatusNode()
|
|
|
|
defer func() {
|
|
|
|
err := stop1()
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
n.logger.Error("stopping db", zap.Error(err))
|
2023-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := stop2()
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
n.logger.Error("stopping multiaccount db", zap.Error(err))
|
2023-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
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",
|
|
|
|
}
|
2023-03-27 14:33:00 +00:00
|
|
|
|
|
|
|
n1, stop11, stop12, err := createStatusNode()
|
|
|
|
defer func() {
|
|
|
|
err := stop11()
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
n1.logger.Error("stopping db", zap.Error(err))
|
2023-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := stop12()
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
n1.logger.Error("stopping multiaccount db", zap.Error(err))
|
2023-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, n1.Start(&config, nil))
|
|
|
|
node, err = n1.discoverNode()
|
2018-11-14 07:03:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, net.ParseIP("127.0.0.2").To4(), node.IP())
|
2018-08-17 06:25:55 +00:00
|
|
|
}
|