tests: updated due to updating old accounts as a part of migration process

This commit is contained in:
Sale Djenic 2023-03-27 16:33:00 +02:00 committed by saledjenic
parent 5d79b3514c
commit e9482e3974
7 changed files with 335 additions and 35 deletions

View File

@ -2,6 +2,7 @@ package api
import ( import (
"crypto/sha256" "crypto/sha256"
"database/sql"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -19,6 +20,7 @@ import (
gethcrypto "github.com/ethereum/go-ethereum/crypto" gethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/connection" "github.com/status-im/status-go/connection"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
@ -29,6 +31,7 @@ import (
"github.com/status-im/status-go/params" "github.com/status-im/status-go/params"
"github.com/status-im/status-go/rpc" "github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/services/typeddata" "github.com/status-im/status-go/services/typeddata"
"github.com/status-im/status-go/sqlite"
"github.com/status-im/status-go/t/utils" "github.com/status-im/status-go/t/utils"
"github.com/status-im/status-go/transactions" "github.com/status-im/status-go/transactions"
) )
@ -37,6 +40,7 @@ var (
networks = json.RawMessage("{}") networks = json.RawMessage("{}")
testSettings = settings.Settings{ testSettings = settings.Settings{
Address: types.HexToAddress("0xeC540f3745Ff2964AFC1171a5A0DD726d1F6B472"), Address: types.HexToAddress("0xeC540f3745Ff2964AFC1171a5A0DD726d1F6B472"),
DisplayName: "UserDisplayName",
CurrentNetwork: "mainnet_rpc", CurrentNetwork: "mainnet_rpc",
DappsAddress: types.HexToAddress("0xe1300f99fDF7346986CbC766903245087394ecd0"), DappsAddress: types.HexToAddress("0xe1300f99fDF7346986CbC766903245087394ecd0"),
EIP1581Address: types.HexToAddress("0xe1DDDE9235a541d1344550d969715CF43982de9f"), EIP1581Address: types.HexToAddress("0xe1DDDE9235a541d1344550d969715CF43982de9f"),
@ -52,10 +56,74 @@ var (
WalletRootAddress: types.HexToAddress("0xeB591fd819F86D0A6a2EF2Bcb94f77807a7De1a6")} WalletRootAddress: types.HexToAddress("0xeB591fd819F86D0A6a2EF2Bcb94f77807a7De1a6")}
) )
func setupTestDB() (*sql.DB, func() error, error) {
tmpfile, err := ioutil.TempFile("", "tests")
if err != nil {
return nil, nil, err
}
db, err := appdatabase.InitializeDB(tmpfile.Name(), "tests", sqlite.ReducedKDFIterationsNumber)
if err != nil {
return nil, nil, err
}
return db, func() error {
err := db.Close()
if err != nil {
return err
}
return os.Remove(tmpfile.Name())
}, nil
}
func setupTestMultiDB() (*multiaccounts.Database, func() error, error) {
tmpfile, err := ioutil.TempFile("", "tests")
if err != nil {
return nil, nil, err
}
db, err := multiaccounts.InitializeDB(tmpfile.Name())
if err != nil {
return nil, nil, err
}
return db, func() error {
err := db.Close()
if err != nil {
return err
}
return os.Remove(tmpfile.Name())
}, nil
}
func setupGethStatusBackend() (*GethStatusBackend, func() error, func() error, error) {
db, stop1, err := setupTestDB()
if err != nil {
return nil, nil, nil, err
}
backend := NewGethStatusBackend()
backend.StatusNode().SetAppDB(db)
ma, stop2, err := setupTestMultiDB()
backend.StatusNode().SetMultiaccountsDB(ma)
return backend, stop1, stop2, err
}
func TestBackendStartNodeConcurrently(t *testing.T) { func TestBackendStartNodeConcurrently(t *testing.T) {
utils.Init() utils.Init()
backend := NewGethStatusBackend() backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID) config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir)) require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -90,7 +158,21 @@ func TestBackendStartNodeConcurrently(t *testing.T) {
func TestBackendRestartNodeConcurrently(t *testing.T) { func TestBackendRestartNodeConcurrently(t *testing.T) {
utils.Init() utils.Init()
backend := NewGethStatusBackend() backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID) config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err) require.NoError(t, err)
count := 3 count := 3
@ -118,7 +200,21 @@ func TestBackendRestartNodeConcurrently(t *testing.T) {
func TestBackendGettersConcurrently(t *testing.T) { func TestBackendGettersConcurrently(t *testing.T) {
utils.Init() utils.Init()
backend := NewGethStatusBackend() backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID) config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir)) require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -203,7 +299,21 @@ func TestBackendConnectionChangesToOffline(t *testing.T) {
func TestBackendCallRPCConcurrently(t *testing.T) { func TestBackendCallRPCConcurrently(t *testing.T) {
utils.Init() utils.Init()
backend := NewGethStatusBackend() backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID) config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir)) require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -279,7 +389,21 @@ func TestAppStateChange(t *testing.T) {
func TestBlockedRPCMethods(t *testing.T) { func TestBlockedRPCMethods(t *testing.T) {
utils.Init() utils.Init()
backend := NewGethStatusBackend() backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID) config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir)) require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -319,7 +443,21 @@ func TestCallRPCWithStoppedNode(t *testing.T) {
func TestStartStopMultipleTimes(t *testing.T) { func TestStartStopMultipleTimes(t *testing.T) {
utils.Init() utils.Init()
backend := NewGethStatusBackend() backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID) config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir)) require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -338,7 +476,21 @@ func TestStartStopMultipleTimes(t *testing.T) {
func TestHashTypedData(t *testing.T) { func TestHashTypedData(t *testing.T) {
utils.Init() utils.Init()
backend := NewGethStatusBackend() backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID) config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir)) require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -651,15 +803,29 @@ func TestConvertAccount(t *testing.T) {
keyStoreDir := filepath.Join(rootDataDir, "keystore") keyStoreDir := filepath.Join(rootDataDir, "keystore")
utils.Init() utils.Init()
backend := NewGethStatusBackend()
backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
backend.rootDataDir = rootDataDir backend.rootDataDir = rootDataDir
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID) config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err)
config.DataDir = rootDataDir config.DataDir = rootDataDir
config.KeyStoreDir = keyStoreDir config.KeyStoreDir = keyStoreDir
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
err = backend.StartNode(config)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
require.NoError(t, backend.StartNode(config))
defer func() { defer func() {
require.NoError(t, backend.StopNode()) require.NoError(t, backend.StopNode())
}() }()
@ -693,7 +859,7 @@ func TestConvertAccount(t *testing.T) {
PublicKey: types.Hex2Bytes(accountInfo.PublicKey), PublicKey: types.Hex2Bytes(accountInfo.PublicKey),
Path: pathEIP1581Chat, Path: pathEIP1581Chat,
Wallet: false, Wallet: false,
Chat: false, Chat: true,
Name: "GeneratedAccount", Name: "GeneratedAccount",
}) })
@ -710,6 +876,7 @@ func TestConvertAccount(t *testing.T) {
KeyUID: genAccInfo.KeyUID, KeyUID: genAccInfo.KeyUID,
Wallet: false, Wallet: false,
Chat: false, Chat: false,
Type: accounts.AccountTypeGenerated,
Path: p, Path: p,
Name: "derivacc" + p, Name: "derivacc" + p,
Hidden: false, Hidden: false,
@ -730,6 +897,7 @@ func TestConvertAccount(t *testing.T) {
s := settings.Settings{ s := settings.Settings{
Address: types.HexToAddress(masterAddress), Address: types.HexToAddress(masterAddress),
DisplayName: "UserDisplayName",
CurrentNetwork: "mainnet_rpc", CurrentNetwork: "mainnet_rpc",
DappsAddress: types.HexToAddress(derivedAccounts[pathDefaultWalletAccount].Address), DappsAddress: types.HexToAddress(derivedAccounts[pathDefaultWalletAccount].Address),
EIP1581Address: types.HexToAddress(derivedAccounts[pathEIP1581Root].Address), EIP1581Address: types.HexToAddress(derivedAccounts[pathEIP1581Root].Address),

View File

@ -74,6 +74,7 @@ func setupWalletTest(t *testing.T, password string) (backend *GethStatusBackend,
networks := json.RawMessage("{}") networks := json.RawMessage("{}")
s := settings.Settings{ s := settings.Settings{
Address: types.HexToAddress(walletRootAddress), Address: types.HexToAddress(walletRootAddress),
DisplayName: "UserDisplayName",
CurrentNetwork: "mainnet_rpc", CurrentNetwork: "mainnet_rpc",
DappsAddress: types.HexToAddress(walletRootAddress), DappsAddress: types.HexToAddress(walletRootAddress),
EIP1581Address: types.HexToAddress(walletRootAddress), EIP1581Address: types.HexToAddress(walletRootAddress),

View File

@ -14,7 +14,23 @@ import (
) )
func TestHashMessage(t *testing.T) { func TestHashMessage(t *testing.T) {
backend := NewGethStatusBackend() utils.Init()
backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID) config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir)) require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))

View File

@ -183,6 +183,10 @@ func updateKeypairNameAndLastDerivationIndexIfNeeded(db *sql.DB) error {
return err return err
} }
if displayName == "" {
displayName = "Status"
}
var ( var (
seedKeyPairIndex int seedKeyPairIndex int
keyKeyPairIndex int keyKeyPairIndex int

View File

@ -3,28 +3,26 @@ package node
import ( import (
"testing" "testing"
"github.com/ethereum/go-ethereum/accounts"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/status-im/status-go/params" "github.com/status-im/status-go/params"
) )
func TestWakuLightModeEnabledSetsEmptyBloomFilter(t *testing.T) { func TestWakuLightModeEnabledSetsEmptyBloomFilter(t *testing.T) {
config := params.NodeConfig{ statusNode, err := createAndStartStatusNode(&params.NodeConfig{
EnableNTPSync: true, EnableNTPSync: true,
WakuConfig: params.WakuConfig{ WakuConfig: params.WakuConfig{
Enabled: true, Enabled: true,
LightClient: true, LightClient: true,
}, },
} })
node := New(nil) require.NoError(t, err)
require.NoError(t, node.Start(&config, &accounts.Manager{}))
defer func() { defer func() {
require.NoError(t, node.Stop()) err := statusNode.Stop()
require.NoError(t, err)
}() }()
waku := node.WakuService() waku := statusNode.WakuService()
require.NotNil(t, waku) require.NotNil(t, waku)
bloomFilter := waku.BloomFilter() bloomFilter := waku.BloomFilter()
@ -34,20 +32,20 @@ func TestWakuLightModeEnabledSetsEmptyBloomFilter(t *testing.T) {
} }
func TestWakuLightModeEnabledSetsNilBloomFilter(t *testing.T) { func TestWakuLightModeEnabledSetsNilBloomFilter(t *testing.T) {
config := params.NodeConfig{ statusNode, err := createAndStartStatusNode(&params.NodeConfig{
EnableNTPSync: true, EnableNTPSync: true,
WakuConfig: params.WakuConfig{ WakuConfig: params.WakuConfig{
Enabled: true, Enabled: true,
LightClient: false, LightClient: false,
}, },
} })
node := New(nil) require.NoError(t, err)
require.NoError(t, node.Start(&config, &accounts.Manager{}))
defer func() { defer func() {
require.NoError(t, node.Stop()) err := statusNode.Stop()
require.NoError(t, err)
}() }()
waku := node.WakuService() waku := statusNode.WakuService()
require.NotNil(t, waku) require.NotNil(t, waku)
require.Nil(t, waku.BloomFilter()) require.Nil(t, waku.BloomFilter())
} }

View File

@ -29,6 +29,18 @@ func TestStatusNodeStart(t *testing.T) {
require.Nil(t, n.Config()) require.Nil(t, n.Config())
require.Nil(t, n.RPCClient()) require.Nil(t, n.RPCClient())
require.Equal(t, 0, n.PeerCount()) require.Equal(t, 0, n.PeerCount())
db, stop, err := setupTestDB()
defer func() {
err := stop()
if err != nil {
n.log.Error("stopping db", err)
}
}()
require.NoError(t, err)
require.NotNil(t, db)
n.appDB = db
// start node // start node
require.NoError(t, n.Start(config, nil)) require.NoError(t, n.Start(config, nil))
@ -73,7 +85,21 @@ func TestStatusNodeWithDataDir(t *testing.T) {
DataDir: dir, DataDir: dir,
KeyStoreDir: keyStoreDir, KeyStoreDir: keyStoreDir,
} }
n := New(nil)
n, stop1, stop2, err := createStatusNode()
defer func() {
err := stop1()
if err != nil {
n.log.Error("stopping db", err)
}
}()
defer func() {
err := stop2()
if err != nil {
n.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
require.NoError(t, n.Start(&config, nil)) require.NoError(t, n.Start(&config, nil))
require.NoError(t, n.Stop()) require.NoError(t, n.Stop())
@ -95,7 +121,20 @@ func TestStatusNodeAddPeer(t *testing.T) {
defer func() { require.NoError(t, peer.Close()) }() defer func() { require.NoError(t, peer.Close()) }()
peerURL := peer.Server().Self().URLv4() peerURL := peer.Server().Self().URLv4()
n := New(nil) n, stop1, stop2, err := createStatusNode()
defer func() {
err := stop1()
if err != nil {
n.log.Error("stopping db", err)
}
}()
defer func() {
err := stop2()
if err != nil {
n.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
// checks before node is started // checks before node is started
require.EqualError(t, n.AddPeer(peerURL), ErrNoRunningNode.Error()) require.EqualError(t, n.AddPeer(peerURL), ErrNoRunningNode.Error())
@ -127,7 +166,22 @@ func TestStatusNodeRendezvousDiscovery(t *testing.T) {
// use custom address to test the all possibilities // use custom address to test the all possibilities
AdvertiseAddr: "127.0.0.1", AdvertiseAddr: "127.0.0.1",
} }
n := New(nil)
n, stop1, stop2, err := createStatusNode()
defer func() {
err := stop1()
if err != nil {
n.log.Error("stopping db", err)
}
}()
defer func() {
err := stop2()
if err != nil {
n.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
require.NoError(t, n.Start(&config, nil)) require.NoError(t, n.Start(&config, nil))
require.NotNil(t, n.discovery) require.NotNil(t, n.discovery)
require.True(t, n.discovery.Running()) require.True(t, n.discovery.Running())
@ -146,7 +200,22 @@ func TestStatusNodeStartDiscoveryManual(t *testing.T) {
// use custom address to test the all possibilities // use custom address to test the all possibilities
AdvertiseAddr: "127.0.0.1", AdvertiseAddr: "127.0.0.1",
} }
n := New(nil)
n, stop1, stop2, err := createStatusNode()
defer func() {
err := stop1()
if err != nil {
n.log.Error("stopping db", err)
}
}()
defer func() {
err := stop2()
if err != nil {
n.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
require.NoError(t, n.StartWithOptions(&config, StartOptions{})) require.NoError(t, n.StartWithOptions(&config, StartOptions{}))
require.Nil(t, n.discovery) require.Nil(t, n.discovery)
// start discovery manually // start discovery manually
@ -161,7 +230,22 @@ func TestStatusNodeDiscoverNode(t *testing.T) {
NoDiscovery: true, NoDiscovery: true,
ListenAddr: "127.0.0.1:0", ListenAddr: "127.0.0.1:0",
} }
n := New(nil)
n, stop1, stop2, err := createStatusNode()
defer func() {
err := stop1()
if err != nil {
n.log.Error("stopping db", err)
}
}()
defer func() {
err := stop2()
if err != nil {
n.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
require.NoError(t, n.Start(&config, nil)) require.NoError(t, n.Start(&config, nil))
node, err := n.discoverNode() node, err := n.discoverNode()
require.NoError(t, err) require.NoError(t, err)
@ -172,9 +256,24 @@ func TestStatusNodeDiscoverNode(t *testing.T) {
AdvertiseAddr: "127.0.0.2", AdvertiseAddr: "127.0.0.2",
ListenAddr: "127.0.0.1:0", ListenAddr: "127.0.0.1:0",
} }
n = New(nil)
require.NoError(t, n.Start(&config, nil)) n1, stop11, stop12, err := createStatusNode()
node, err = n.discoverNode() defer func() {
err := stop11()
if err != nil {
n1.log.Error("stopping db", err)
}
}()
defer func() {
err := stop12()
if err != nil {
n1.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
require.NoError(t, n1.Start(&config, nil))
node, err = n1.discoverNode()
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, net.ParseIP("127.0.0.2").To4(), node.IP()) require.Equal(t, net.ParseIP("127.0.0.2").To4(), node.IP())
} }

View File

@ -93,6 +93,20 @@ func createAndStartStatusNode(config *params.NodeConfig) (*StatusNode, error) {
return statusNode, nil return statusNode, nil
} }
func createStatusNode() (*StatusNode, func() error, func() error, error) {
db, stop1, err := setupTestDB()
if err != nil {
return nil, nil, nil, err
}
statusNode := New(nil)
statusNode.SetAppDB(db)
ma, stop2, err := setupTestMultiDB()
statusNode.SetMultiaccountsDB(ma)
return statusNode, stop1, stop2, err
}
func TestNodeRPCClientCallOnlyPublicAPIs(t *testing.T) { func TestNodeRPCClientCallOnlyPublicAPIs(t *testing.T) {
var err error var err error