2021-07-09 13:19:33 +00:00
|
|
|
package node
|
2018-04-12 16:17:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-23 18:47:00 +00:00
|
|
|
"database/sql"
|
2023-08-23 07:39:49 +00:00
|
|
|
"fmt"
|
2022-03-23 18:47:00 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2021-07-09 13:19:33 +00:00
|
|
|
"strings"
|
2018-04-12 16:17:10 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/appdatabase"
|
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2023-08-11 11:28:45 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
2023-08-23 07:39:49 +00:00
|
|
|
"github.com/status-im/status-go/walletdatabase"
|
2018-04-12 16:17:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestServiceAPI struct{}
|
|
|
|
|
|
|
|
func (api *TestServiceAPI) SomeMethod(_ context.Context) (string, error) {
|
|
|
|
return "some method result", nil
|
|
|
|
}
|
|
|
|
|
2023-08-23 07:39:49 +00:00
|
|
|
func setupTestDBs() (appDB *sql.DB, walletDB *sql.DB, closeFn func() error, err error) {
|
|
|
|
appDB, err = helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("failed to setup app db: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
walletDB, err = helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("failed to setup wallet db: %w", err)
|
|
|
|
}
|
|
|
|
return appDB, walletDB, func() error {
|
|
|
|
appErr := appDB.Close()
|
|
|
|
walletErr := walletDB.Close()
|
|
|
|
if appErr != nil {
|
|
|
|
return fmt.Errorf("failed to close app db: %w", appErr)
|
|
|
|
}
|
|
|
|
if walletErr != nil {
|
|
|
|
return fmt.Errorf("failed to close wallet db: %w", walletErr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
func createAndStartStatusNode(config *params.NodeConfig) (*StatusNode, error) {
|
2021-12-21 15:44:37 +00:00
|
|
|
statusNode := New(nil)
|
2022-03-23 18:47:00 +00:00
|
|
|
|
2023-08-23 07:39:49 +00:00
|
|
|
appDB, walletDB, stop, err := setupTestDBs()
|
2022-03-23 18:47:00 +00:00
|
|
|
defer func() {
|
|
|
|
err := stop()
|
|
|
|
if err != nil {
|
|
|
|
statusNode.log.Error("stopping db", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-23 07:39:49 +00:00
|
|
|
statusNode.appDB = appDB
|
|
|
|
statusNode.walletDB = walletDB
|
2022-03-23 18:47:00 +00:00
|
|
|
|
|
|
|
ma, stop2, err := setupTestMultiDB()
|
|
|
|
defer func() {
|
|
|
|
err := stop2()
|
|
|
|
if err != nil {
|
|
|
|
statusNode.log.Error("stopping multiaccount db", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-07-09 13:19:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-04-12 16:17:10 +00:00
|
|
|
}
|
2022-03-23 18:47:00 +00:00
|
|
|
statusNode.multiaccountsDB = ma
|
|
|
|
|
|
|
|
err = statusNode.Start(config, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
return statusNode, nil
|
2018-04-12 16:17:10 +00:00
|
|
|
}
|
|
|
|
|
2023-03-27 14:33:00 +00:00
|
|
|
func createStatusNode() (*StatusNode, func() error, func() error, error) {
|
2023-08-23 07:39:49 +00:00
|
|
|
appDB, walletDB, stop1, err := setupTestDBs()
|
2023-03-27 14:33:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
statusNode := New(nil)
|
2023-08-23 07:39:49 +00:00
|
|
|
statusNode.SetAppDB(appDB)
|
|
|
|
statusNode.SetWalletDB(walletDB)
|
2023-03-27 14:33:00 +00:00
|
|
|
|
|
|
|
ma, stop2, err := setupTestMultiDB()
|
|
|
|
statusNode.SetMultiaccountsDB(ma)
|
|
|
|
|
|
|
|
return statusNode, stop1, stop2, err
|
|
|
|
}
|
|
|
|
|
2018-04-12 16:17:10 +00:00
|
|
|
func TestNodeRPCClientCallOnlyPublicAPIs(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
statusNode, err := createAndStartStatusNode(¶ms.NodeConfig{
|
|
|
|
APIModules: "", // no whitelisted API modules; use only public APIs
|
2021-07-09 13:19:33 +00:00
|
|
|
UpstreamConfig: params.UpstreamRPCConfig{
|
|
|
|
URL: "https://infura.io",
|
|
|
|
Enabled: true},
|
|
|
|
WakuConfig: params.WakuConfig{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
2018-04-16 12:36:09 +00:00
|
|
|
})
|
2018-04-12 16:17:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
err := statusNode.Stop()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
client := statusNode.RPCClient()
|
|
|
|
require.NotNil(t, client)
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
// call public API with public RPC Client
|
|
|
|
result, err := statusNode.CallRPC(`{"jsonrpc": "2.0", "id": 1, "method": "eth_uninstallFilter", "params": ["id"]}`)
|
2018-04-12 16:17:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
// the call is successful
|
|
|
|
require.False(t, strings.Contains(result, "error"))
|
2018-04-12 16:17:10 +00:00
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
result, err = statusNode.CallRPC(`{"jsonrpc": "2.0", "id": 1, "method": "waku_info"}`)
|
2018-04-12 16:17:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
// call private API with public RPC client
|
|
|
|
require.Equal(t, ErrRPCMethodUnavailable, result)
|
2018-04-12 16:17:10 +00:00
|
|
|
|
|
|
|
}
|
2018-04-16 08:01:37 +00:00
|
|
|
|
|
|
|
func TestNodeRPCPrivateClientCallPrivateService(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2021-07-12 14:16:33 +00:00
|
|
|
statusNode, err := createAndStartStatusNode(¶ms.NodeConfig{
|
|
|
|
WakuConfig: params.WakuConfig{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
})
|
2018-04-16 08:01:37 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
err := statusNode.Stop()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
result, err := statusNode.CallPrivateRPC(`{"jsonrpc": "2.0", "id": 1, "method": "waku_info"}`)
|
2018-04-16 08:01:37 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-09 13:19:33 +00:00
|
|
|
|
|
|
|
// the call is successful
|
|
|
|
require.False(t, strings.Contains(result, "error"))
|
2022-03-23 18:47:00 +00:00
|
|
|
|
|
|
|
_, err = statusNode.CallPrivateRPC(`{"jsonrpc": "2.0", "id": 1, "method": "settings_getSettings"}`)
|
|
|
|
require.NoError(t, err)
|
2018-04-16 08:01:37 +00:00
|
|
|
}
|