2017-05-16 12:09:52 +00:00
|
|
|
package node_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
|
|
|
"github.com/ethereum/go-ethereum/les"
|
|
|
|
gethnode "github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
|
|
|
|
"github.com/status-im/status-go/geth/node"
|
|
|
|
"github.com/status-im/status-go/geth/params"
|
2018-02-09 13:37:56 +00:00
|
|
|
|
2018-02-08 12:52:47 +00:00
|
|
|
e2e "github.com/status-im/status-go/t/e2e"
|
|
|
|
. "github.com/status-im/status-go/t/utils"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestManagerTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ManagerTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type ManagerTestSuite struct {
|
2017-10-11 14:20:51 +00:00
|
|
|
e2e.NodeManagerTestSuite
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ManagerTestSuite) SetupTest() {
|
|
|
|
s.NodeManager = node.NewNodeManager()
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *ManagerTestSuite) TestReferencesWithoutStartedNode() {
|
|
|
|
var testCases = []struct {
|
2017-05-16 12:09:52 +00:00
|
|
|
name string
|
|
|
|
initFn func() (interface{}, error)
|
|
|
|
expectedErr error
|
|
|
|
}{
|
2017-05-25 13:14:52 +00:00
|
|
|
{
|
|
|
|
"non-null manager, no running node, PopulateStaticPeers()",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return nil, s.NodeManager.PopulateStaticPeers()
|
|
|
|
},
|
|
|
|
node.ErrNoRunningNode,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"non-null manager, no running node, AddPeer()",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return nil, s.NodeManager.AddPeer("enode://da3bf389a031f33fb55c9f5f54fde8473912402d27fffaa50efd74c0d0515f3a61daf6d52151f2876b19c15828e6f670352bff432b5ec457652e74755e8c864f@51.15.62.116:30303")
|
|
|
|
},
|
|
|
|
node.ErrNoRunningNode,
|
|
|
|
},
|
2017-05-16 12:09:52 +00:00
|
|
|
{
|
|
|
|
"non-null manager, no running node, get NodeConfig",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.NodeConfig()
|
|
|
|
},
|
|
|
|
node.ErrNoRunningNode,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"non-null manager, no running node, get Node",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.Node()
|
|
|
|
},
|
|
|
|
node.ErrNoRunningNode,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"non-null manager, no running node, get LES",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.LightEthereumService()
|
|
|
|
},
|
|
|
|
node.ErrNoRunningNode,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"non-null manager, no running node, get Whisper",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.WhisperService()
|
|
|
|
},
|
|
|
|
node.ErrNoRunningNode,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"non-null manager, no running node, get AccountManager",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.AccountManager()
|
|
|
|
},
|
|
|
|
node.ErrNoRunningNode,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"non-null manager, no running node, get AccountKeyStore",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.AccountKeyStore()
|
|
|
|
},
|
|
|
|
node.ErrNoRunningNode,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"non-null manager, no running node, get RPC Client",
|
|
|
|
func() (interface{}, error) {
|
2017-09-14 20:14:31 +00:00
|
|
|
return s.NodeManager.RPCClient(), nil
|
2017-05-28 13:57:30 +00:00
|
|
|
},
|
2017-09-19 11:40:43 +00:00
|
|
|
nil,
|
2017-05-28 13:57:30 +00:00
|
|
|
},
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2017-10-11 14:20:51 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
s.T().Log(tc.name)
|
|
|
|
obj, err := tc.initFn()
|
2017-05-16 12:09:52 +00:00
|
|
|
s.Nil(obj)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.Equal(tc.expectedErr, err)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *ManagerTestSuite) TestReferencesWithStartedNode() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestNode()
|
2017-11-07 17:43:49 +00:00
|
|
|
defer func() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
s.StopTestNode()
|
|
|
|
}()
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
var testCases = []struct {
|
2017-05-16 12:09:52 +00:00
|
|
|
name string
|
|
|
|
initFn func() (interface{}, error)
|
|
|
|
expectedType interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"node is running, get NodeConfig",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.NodeConfig()
|
|
|
|
},
|
|
|
|
¶ms.NodeConfig{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"node is running, get Node",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.Node()
|
|
|
|
},
|
|
|
|
&gethnode.Node{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"node is running, get LES",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.LightEthereumService()
|
|
|
|
},
|
|
|
|
&les.LightEthereum{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"node is running, get Whisper",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.WhisperService()
|
|
|
|
},
|
|
|
|
&whisper.Whisper{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"node is running, get AccountManager",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.AccountManager()
|
|
|
|
},
|
|
|
|
&accounts.Manager{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"node is running, get AccountKeyStore",
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return s.NodeManager.AccountKeyStore()
|
|
|
|
},
|
|
|
|
&keystore.KeyStore{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"node is running, get RPC Client",
|
|
|
|
func() (interface{}, error) {
|
2017-09-14 20:14:31 +00:00
|
|
|
return s.NodeManager.RPCClient(), nil
|
2017-05-16 12:09:52 +00:00
|
|
|
},
|
|
|
|
&rpc.Client{},
|
|
|
|
},
|
|
|
|
}
|
2017-10-11 14:20:51 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
s.T().Log(tc.name)
|
|
|
|
obj, err := tc.initFn()
|
2017-05-16 12:09:52 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(obj)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.IsType(tc.expectedType, obj)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ManagerTestSuite) TestNodeStartStop() {
|
2017-10-24 10:23:53 +00:00
|
|
|
nodeConfig, err := e2e.MakeTestNodeConfig(GetNetworkID())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// try stopping non-started node
|
2017-10-11 14:20:51 +00:00
|
|
|
s.False(s.NodeManager.IsNodeRunning())
|
2017-11-07 17:43:49 +00:00
|
|
|
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
|
2018-02-09 13:37:56 +00:00
|
|
|
s.Equal(node.ErrNoRunningNode, s.NodeManager.StopNode())
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// start node
|
|
|
|
s.False(s.NodeManager.IsNodeRunning())
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StartNode(nodeConfig))
|
2017-10-11 14:20:51 +00:00
|
|
|
// wait till node is started
|
|
|
|
s.True(s.NodeManager.IsNodeRunning())
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// try starting another node (w/o stopping the previously started node)
|
2018-02-09 13:37:56 +00:00
|
|
|
s.Equal(node.ErrNodeExists, s.NodeManager.StartNode(nodeConfig))
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// now stop node
|
2017-11-07 17:43:49 +00:00
|
|
|
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StopNode())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.False(s.NodeManager.IsNodeRunning())
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// start new node with exactly the same config
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StartNode(nodeConfig))
|
2017-10-11 14:20:51 +00:00
|
|
|
s.True(s.NodeManager.IsNodeRunning())
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// finally stop the node
|
2017-11-07 17:43:49 +00:00
|
|
|
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StopNode())
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ManagerTestSuite) TestNetworkSwitching() {
|
|
|
|
// get Ropsten config
|
2017-10-25 12:08:33 +00:00
|
|
|
nodeConfig, err := e2e.MakeTestNodeConfig(GetNetworkID())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.False(s.NodeManager.IsNodeRunning())
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StartNode(nodeConfig))
|
2017-10-11 14:20:51 +00:00
|
|
|
// wait till node is started
|
|
|
|
s.True(s.NodeManager.IsNodeRunning())
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
firstHash, err := e2e.FirstBlockHash(s.NodeManager)
|
|
|
|
s.NoError(err)
|
2017-10-26 13:11:24 +00:00
|
|
|
s.Equal(GetHeadHash(), firstHash)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// now stop node, and make sure that a new node, on different network can be started
|
2017-11-07 17:43:49 +00:00
|
|
|
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StopNode())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.False(s.NodeManager.IsNodeRunning())
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// start new node with completely different config
|
2017-10-23 17:18:09 +00:00
|
|
|
nodeConfig, err = e2e.MakeTestNodeConfig(params.RinkebyNetworkID)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StartNode(nodeConfig))
|
2017-10-11 14:20:51 +00:00
|
|
|
s.True(s.NodeManager.IsNodeRunning())
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// make sure we are on another network indeed
|
2017-10-11 14:20:51 +00:00
|
|
|
firstHash, err = e2e.FirstBlockHash(s.NodeManager)
|
|
|
|
s.NoError(err)
|
2017-10-25 22:07:50 +00:00
|
|
|
s.Equal(GetHeadHashFromNetworkID(params.RinkebyNetworkID), firstHash)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-11-07 17:43:49 +00:00
|
|
|
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StopNode())
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *ManagerTestSuite) TestStartNodeWithUpstreamEnabled() {
|
2017-10-26 12:33:42 +00:00
|
|
|
if GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
s.T().Skip()
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeConfig, err := e2e.MakeTestNodeConfig(GetNetworkID())
|
2017-10-25 12:08:33 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
2017-10-26 13:11:24 +00:00
|
|
|
networkURL, err := GetRemoteURL()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
nodeConfig.UpstreamConfig.Enabled = true
|
2017-10-25 12:08:33 +00:00
|
|
|
nodeConfig.UpstreamConfig.URL = networkURL
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StartNode(nodeConfig))
|
2017-10-11 14:20:51 +00:00
|
|
|
s.True(s.NodeManager.IsNodeRunning())
|
2017-11-07 17:43:49 +00:00
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StopNode())
|
2017-05-25 13:14:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// TODO(adam): race conditions should be tested with -race flag and unit tests, if possible.
|
2017-11-07 17:43:49 +00:00
|
|
|
// TODO(boris): going via https://github.com/status-im/status-go/pull/433#issuecomment-342232645 . Testing should be with -race flag
|
2017-10-11 14:20:51 +00:00
|
|
|
// Research if it's possible to do the same with unit tests.
|
2017-11-07 17:43:49 +00:00
|
|
|
//func (s *ManagerTestSuite) TestRaceConditions() {
|
|
|
|
// cnt := 25
|
|
|
|
// progress := make(chan struct{}, cnt)
|
|
|
|
// rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
//
|
|
|
|
// nodeConfig1, e := e2e.MakeTestNodeConfig(GetNetworkID())
|
|
|
|
// s.NoError(e)
|
|
|
|
//
|
|
|
|
// nodeConfig2, e := e2e.MakeTestNodeConfig(GetNetworkID())
|
|
|
|
// s.NoError(e)
|
|
|
|
//
|
|
|
|
// nodeConfigs := []*params.NodeConfig{nodeConfig1, nodeConfig2}
|
|
|
|
//
|
|
|
|
// var funcsToTest = []func(*params.NodeConfig){
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("StartNode()")
|
|
|
|
// _, err := s.NodeManager.StartNode(config)
|
|
|
|
// s.T().Logf("StartNode() for network: %d, error: %v", config.NetworkID, err)
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// _, err := s.NodeManager.StopNode()
|
|
|
|
// s.T().Logf("StopNode() for network: %d, error: %v", config.NetworkID, err)
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("Node()")
|
|
|
|
// _, err := s.NodeManager.Node()
|
|
|
|
// s.T().Logf("Node(), error: %v", err)
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("IsNodeRunning()")
|
|
|
|
// s.T().Logf("IsNodeRunning(), result: %v", s.NodeManager.IsNodeRunning())
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("PopulateStaticPeers()")
|
|
|
|
// s.T().Logf("PopulateStaticPeers(), error: %v", s.NodeManager.PopulateStaticPeers())
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// // TODO(adam): quarantined until it uses a different datadir
|
|
|
|
// // as otherwise it wipes out cached blockchain data.
|
|
|
|
// // func(config *params.NodeConfig) {
|
2018-02-09 13:37:56 +00:00
|
|
|
// // log.Info("ResetChainData()")
|
|
|
|
// // _, err := s.NodeManager.ResetChainData()
|
|
|
|
// // s.T().Logf("ResetChainData(), error: %v", err)
|
|
|
|
// // progress <- struct{}{}
|
2017-11-07 17:43:49 +00:00
|
|
|
// // },
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("RestartNode()")
|
|
|
|
// _, err := s.NodeManager.RestartNode()
|
|
|
|
// s.T().Logf("RestartNode(), error: %v", err)
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("NodeConfig()")
|
|
|
|
// _, err := s.NodeManager.NodeConfig()
|
|
|
|
// s.T().Logf("NodeConfig(), error: %v", err)
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("LightEthereumService()")
|
|
|
|
// _, err := s.NodeManager.LightEthereumService()
|
|
|
|
// s.T().Logf("LightEthereumService(), error: %v", err)
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("WhisperService()")
|
|
|
|
// _, err := s.NodeManager.WhisperService()
|
|
|
|
// s.T().Logf("WhisperService(), error: %v", err)
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("AccountManager()")
|
|
|
|
// _, err := s.NodeManager.AccountManager()
|
|
|
|
// s.T().Logf("AccountManager(), error: %v", err)
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("AccountKeyStore()")
|
|
|
|
// _, err := s.NodeManager.AccountKeyStore()
|
|
|
|
// s.T().Logf("AccountKeyStore(), error: %v", err)
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("RPCClient()")
|
|
|
|
// s.NodeManager.RPCClient()
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // increase StartNode()/StopNode() population
|
|
|
|
// for i := 0; i < 5; i++ {
|
|
|
|
// funcsToTest = append(funcsToTest, funcsToTest[0], funcsToTest[1])
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// for i := 0; i < cnt; i++ {
|
|
|
|
// randConfig := nodeConfigs[rnd.Intn(len(nodeConfigs))]
|
|
|
|
// randFunc := funcsToTest[rnd.Intn(len(funcsToTest))]
|
|
|
|
//
|
|
|
|
// if rnd.Intn(100) > 75 { // introduce random delays
|
|
|
|
// time.Sleep(500 * time.Millisecond)
|
|
|
|
// }
|
|
|
|
// go randFunc(randConfig)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// for range progress {
|
|
|
|
// cnt -= 1
|
|
|
|
// if cnt <= 0 {
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// time.Sleep(2 * time.Second) // so that we see some logs
|
|
|
|
// nodeStopped, _ := s.NodeManager.StopNode() // just in case we have a node running
|
|
|
|
//
|
|
|
|
// if nodeStopped != nil {
|
|
|
|
// <-nodeStopped
|
|
|
|
// }
|
|
|
|
//}
|