2017-03-15 21:03:01 +00:00
|
|
|
package params_test
|
|
|
|
|
|
|
|
import (
|
2017-05-02 14:30:11 +00:00
|
|
|
"encoding/json"
|
2017-12-04 16:11:14 +00:00
|
|
|
"fmt"
|
2017-03-15 21:03:01 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-08-10 15:31:29 +00:00
|
|
|
|
|
|
|
"gopkg.in/go-playground/validator.v9"
|
2017-03-15 21:03:01 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2017-03-18 18:23:58 +00:00
|
|
|
gethparams "github.com/ethereum/go-ethereum/params"
|
2017-03-15 21:03:01 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2017-08-04 16:14:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-03-15 21:03:01 +00:00
|
|
|
)
|
|
|
|
|
2018-03-19 16:22:09 +00:00
|
|
|
var clusterConfigData = []byte(`[
|
|
|
|
{
|
|
|
|
"networkID": 3,
|
|
|
|
"prod": {
|
2018-03-20 14:05:21 +00:00
|
|
|
"staticnodes": [
|
2018-03-19 16:22:09 +00:00
|
|
|
"enode://7ab298cedc4185a894d21d8a4615262ec6bdce66c9b6783878258e0d5b31013d30c9038932432f70e5b2b6a5cd323bf820554fcb22fbc7b45367889522e9c449@10.1.1.1:30303",
|
|
|
|
"enode://f59e8701f18c79c5cbc7618dc7bb928d44dc2f5405c7d693dad97da2d8585975942ec6fd36d3fe608bfdc7270a34a4dd00f38cfe96b2baa24f7cd0ac28d382a1@10.1.1.2:30303"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"dev": {
|
2018-03-20 14:05:21 +00:00
|
|
|
"staticnodes": [
|
2018-03-19 16:22:09 +00:00
|
|
|
"enode://7ab298cedc4185a894d21d8a4615262ec6bdce66c9b6783878258e0d5b31013d30c9038932432f70e5b2b6a5cd323bf820554fcb22fbc7b45367889522e9c449@10.1.1.1:30303",
|
|
|
|
"enode://f59e8701f18c79c5cbc7618dc7bb928d44dc2f5405c7d693dad97da2d8585975942ec6fd36d3fe608bfdc7270a34a4dd00f38cfe96b2baa24f7cd0ac28d382a1@10.1.1.2:30303"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`)
|
|
|
|
|
2017-03-15 21:03:01 +00:00
|
|
|
var loadConfigTestCases = []struct {
|
|
|
|
name string
|
|
|
|
configJSON string
|
|
|
|
validator func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error)
|
|
|
|
}{
|
|
|
|
{
|
2017-08-10 15:31:29 +00:00
|
|
|
`invalid input JSON (missing comma at the end of key:value pair)`,
|
2017-03-15 21:03:01 +00:00
|
|
|
`{
|
|
|
|
"NetworkId": 3
|
|
|
|
"DataDir": "$TMPDIR"
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-10 15:31:29 +00:00
|
|
|
require.Error(t, err, "error is expected, not thrown")
|
2017-03-15 21:03:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-03-18 18:23:58 +00:00
|
|
|
`check static DataDir passing`,
|
2017-03-15 21:03:01 +00:00
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
2017-03-18 18:23:58 +00:00
|
|
|
"DataDir": "/storage/emulated/0/ethereum/"
|
2017-03-15 21:03:01 +00:00
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "/storage/emulated/0/ethereum/", nodeConfig.DataDir)
|
2017-03-15 21:03:01 +00:00
|
|
|
},
|
|
|
|
},
|
2017-04-28 08:49:15 +00:00
|
|
|
{
|
|
|
|
`use default KeyStoreDir`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"DataDir": "$TMPDIR"
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
2017-04-28 08:49:15 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
_, err = os.Stat(dataDir)
|
|
|
|
require.False(t, os.IsNotExist(err), "data directory doesn't exist")
|
|
|
|
require.Equal(t, dataDir, nodeConfig.DataDir)
|
2017-04-28 08:49:15 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
require.Equal(t, filepath.Join(dataDir, params.KeyStoreDir), filepath.Join(dataDir, params.KeyStoreDir))
|
2017-04-28 08:49:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`use non-default KeyStoreDir`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"KeyStoreDir": "/foo/bar"
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, dataDir, nodeConfig.DataDir)
|
|
|
|
require.Equal(t, "/foo/bar", nodeConfig.KeyStoreDir)
|
2017-04-28 08:49:15 +00:00
|
|
|
},
|
|
|
|
},
|
2017-08-15 10:27:12 +00:00
|
|
|
{
|
|
|
|
`test Upstream config setting`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"Name": "TestStatusNode",
|
|
|
|
"WSPort": 4242,
|
|
|
|
"IPCEnabled": true,
|
|
|
|
"WSEnabled": false,
|
|
|
|
"UpstreamConfig": {
|
|
|
|
"Enabled": true,
|
|
|
|
"URL": "http://upstream.loco.net/nodes"
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodeConfig.NetworkID != 3 {
|
|
|
|
t.Fatal("wrong NetworkId")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !nodeConfig.UpstreamConfig.Enabled {
|
|
|
|
t.Fatal("wrong UpstreamConfig.Enabled state")
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodeConfig.UpstreamConfig.URL != "http://upstream.loco.net/nodes" {
|
|
|
|
t.Fatal("wrong UpstreamConfig.URL value")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2017-03-15 21:03:01 +00:00
|
|
|
{
|
|
|
|
`test parameter overriding`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"Name": "TestStatusNode",
|
|
|
|
"WSPort": 4242,
|
|
|
|
"IPCEnabled": true,
|
|
|
|
"WSEnabled": false,
|
2017-07-13 06:54:10 +00:00
|
|
|
"RPCEnabled": true,
|
2017-03-15 21:03:01 +00:00
|
|
|
"LightEthConfig": {
|
|
|
|
"DatabaseCache": 64
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.EqualValues(t, 3, nodeConfig.NetworkID)
|
|
|
|
require.Equal(t, "TestStatusNode", nodeConfig.Name)
|
|
|
|
require.Equal(t, params.HTTPPort, nodeConfig.HTTPPort)
|
|
|
|
require.Equal(t, params.HTTPHost, nodeConfig.HTTPHost)
|
|
|
|
require.True(t, nodeConfig.RPCEnabled)
|
|
|
|
require.False(t, nodeConfig.WSEnabled)
|
|
|
|
require.Equal(t, 4242, nodeConfig.WSPort)
|
|
|
|
require.True(t, nodeConfig.IPCEnabled)
|
|
|
|
require.Equal(t, 64, nodeConfig.LightEthConfig.DatabaseCache)
|
2017-03-15 21:03:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`test loading Testnet config`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"Name": "TestStatusNode",
|
|
|
|
"WSPort": 8546,
|
|
|
|
"IPCEnabled": true,
|
|
|
|
"WSEnabled": false,
|
|
|
|
"LightEthConfig": {
|
|
|
|
"DatabaseCache": 64
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
2017-03-15 21:03:01 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
genesis := new(core.Genesis)
|
2017-08-04 16:14:17 +00:00
|
|
|
err = json.Unmarshal([]byte(nodeConfig.LightEthConfig.Genesis), genesis)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
chainConfig := genesis.Config
|
2017-03-15 21:03:01 +00:00
|
|
|
refChainConfig := gethparams.TestnetChainConfig
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
require.Empty(t, chainConfig.HomesteadBlock.Cmp(refChainConfig.HomesteadBlock), "invalid chainConfig.HomesteadBlock")
|
|
|
|
require.Nil(t, chainConfig.DAOForkBlock)
|
|
|
|
require.Equal(t, refChainConfig.DAOForkSupport, chainConfig.DAOForkSupport)
|
|
|
|
|
|
|
|
require.Empty(t, chainConfig.EIP150Block.Cmp(refChainConfig.EIP150Block))
|
|
|
|
require.Equal(t, refChainConfig.EIP150Hash, chainConfig.EIP150Hash)
|
|
|
|
|
|
|
|
require.Empty(t, chainConfig.EIP155Block.Cmp(refChainConfig.EIP155Block))
|
|
|
|
require.Empty(t, chainConfig.EIP158Block.Cmp(refChainConfig.EIP158Block))
|
|
|
|
require.Empty(t, chainConfig.ChainId.Cmp(refChainConfig.ChainId))
|
2017-03-15 21:03:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`test loading Mainnet config`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 1,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"Name": "TestStatusNode",
|
|
|
|
"WSPort": 8546,
|
|
|
|
"IPCEnabled": true,
|
|
|
|
"WSEnabled": false,
|
|
|
|
"LightEthConfig": {
|
|
|
|
"DatabaseCache": 64
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
2017-03-15 21:03:01 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
genesis := new(core.Genesis)
|
2017-08-04 16:14:17 +00:00
|
|
|
err = json.Unmarshal([]byte(nodeConfig.LightEthConfig.Genesis), genesis)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
chainConfig := genesis.Config
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-08-07 10:48:14 +00:00
|
|
|
require.Empty(t, chainConfig.HomesteadBlock.Cmp(gethparams.MainnetChainConfig.HomesteadBlock))
|
|
|
|
require.Empty(t, chainConfig.DAOForkBlock.Cmp(gethparams.MainnetChainConfig.DAOForkBlock))
|
2017-08-04 16:14:17 +00:00
|
|
|
require.True(t, chainConfig.DAOForkSupport)
|
2017-08-07 10:48:14 +00:00
|
|
|
require.Empty(t, chainConfig.EIP150Block.Cmp(gethparams.MainnetChainConfig.EIP150Block))
|
|
|
|
require.Equal(t, gethparams.MainnetChainConfig.EIP150Hash, chainConfig.EIP150Hash)
|
|
|
|
require.Empty(t, chainConfig.EIP155Block.Cmp(gethparams.MainnetChainConfig.EIP155Block))
|
|
|
|
require.Empty(t, chainConfig.EIP158Block.Cmp(gethparams.MainnetChainConfig.EIP158Block))
|
|
|
|
require.Empty(t, chainConfig.ChainId.Cmp(gethparams.MainnetChainConfig.ChainId))
|
2017-03-15 21:03:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`test loading Privatenet config`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 311,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"Name": "TestStatusNode",
|
|
|
|
"WSPort": 8546,
|
|
|
|
"IPCEnabled": true,
|
2017-05-02 14:30:11 +00:00
|
|
|
"WSEnabled": false
|
2017-03-15 21:03:01 +00:00
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 311, nodeConfig.NetworkID)
|
2017-03-15 21:03:01 +00:00
|
|
|
},
|
|
|
|
},
|
2017-05-11 13:20:27 +00:00
|
|
|
{
|
2018-03-20 14:05:21 +00:00
|
|
|
`default static nodes (Ropsten Dev)`,
|
2017-05-11 13:20:27 +00:00
|
|
|
`{
|
2017-05-16 03:24:56 +00:00
|
|
|
"NetworkId": 3,
|
2017-05-11 13:20:27 +00:00
|
|
|
"DataDir": "$TMPDIR"
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-20 14:05:21 +00:00
|
|
|
require.True(t, nodeConfig.ClusterConfig.Enabled, "static nodes are expected to be enabled by default")
|
2018-03-19 16:22:09 +00:00
|
|
|
|
2018-03-20 14:05:21 +00:00
|
|
|
enodes := nodeConfig.ClusterConfig.StaticNodes
|
|
|
|
t.Logf("LEN SN %d", len(enodes))
|
2018-03-19 16:22:09 +00:00
|
|
|
require.Len(t, enodes, 2)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-03-20 14:05:21 +00:00
|
|
|
`illegal cluster configuration file`,
|
2018-03-19 16:22:09 +00:00
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"ClusterConfigFile": "/file/does/not.exist"
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
|
|
|
require.Error(t, err, "error is expected, not thrown")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-03-20 14:05:21 +00:00
|
|
|
`valid cluster configuration file`,
|
2018-03-19 16:22:09 +00:00
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"ClusterConfigFile": "$TMPDIR/cluster.json"
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
|
|
|
require.NoError(t, err)
|
2018-03-20 14:05:21 +00:00
|
|
|
require.True(t, nodeConfig.ClusterConfig.Enabled, "cluster configuration is expected to be enabled after loading file")
|
2017-05-16 03:24:56 +00:00
|
|
|
|
2018-03-20 14:05:21 +00:00
|
|
|
enodes := nodeConfig.ClusterConfig.StaticNodes
|
2018-03-19 16:22:09 +00:00
|
|
|
require.True(t, len(enodes) == 2)
|
2017-05-11 13:20:27 +00:00
|
|
|
},
|
|
|
|
},
|
2017-05-16 03:24:56 +00:00
|
|
|
{
|
2018-03-20 14:05:21 +00:00
|
|
|
`default cluster configuration (Ropsten Prod)`,
|
2017-05-16 03:24:56 +00:00
|
|
|
`{
|
2018-02-26 18:36:04 +00:00
|
|
|
"NetworkId": 3,
|
2017-05-16 03:24:56 +00:00
|
|
|
"DataDir": "$TMPDIR",
|
2018-02-26 18:36:04 +00:00
|
|
|
"DevMode": false
|
2017-05-16 03:24:56 +00:00
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-20 14:05:21 +00:00
|
|
|
require.True(t, nodeConfig.ClusterConfig.Enabled, "cluster configuration is expected to be enabled by default")
|
2018-02-26 18:36:04 +00:00
|
|
|
|
2018-03-20 14:05:21 +00:00
|
|
|
enodes := nodeConfig.ClusterConfig.StaticNodes
|
2018-03-19 16:22:09 +00:00
|
|
|
require.Len(t, enodes, 2)
|
2017-05-16 03:24:56 +00:00
|
|
|
},
|
|
|
|
},
|
2017-05-11 13:20:27 +00:00
|
|
|
{
|
2018-03-20 14:05:21 +00:00
|
|
|
`disabled cluster configuration`,
|
2017-05-11 13:20:27 +00:00
|
|
|
`{
|
2018-02-26 18:36:04 +00:00
|
|
|
"NetworkId": 311,
|
2017-05-11 13:20:27 +00:00
|
|
|
"DataDir": "$TMPDIR",
|
2018-03-20 14:05:21 +00:00
|
|
|
"ClusterConfig": {
|
2018-02-26 18:36:04 +00:00
|
|
|
"Enabled": false
|
|
|
|
}
|
2017-05-11 13:20:27 +00:00
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-20 14:05:21 +00:00
|
|
|
require.False(t, nodeConfig.ClusterConfig.Enabled, "cluster configuration is expected to be disabled")
|
2017-05-11 13:20:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-03-20 14:05:21 +00:00
|
|
|
`select cluster configuration (Rinkeby Dev)`,
|
2017-05-11 13:20:27 +00:00
|
|
|
`{
|
2017-08-04 16:14:17 +00:00
|
|
|
"NetworkId": 4,
|
|
|
|
"DataDir": "$TMPDIR"
|
2017-05-11 13:20:27 +00:00
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-20 14:05:21 +00:00
|
|
|
require.True(t, nodeConfig.ClusterConfig.Enabled, "cluster configuration is expected to be enabled by default")
|
2017-05-11 13:20:27 +00:00
|
|
|
|
2018-03-20 14:05:21 +00:00
|
|
|
enodes := nodeConfig.ClusterConfig.StaticNodes
|
2018-02-26 18:36:04 +00:00
|
|
|
require.True(t, len(enodes) >= 3)
|
2017-05-11 13:20:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-03-20 14:05:21 +00:00
|
|
|
`select cluster configuration (Rinkeby Prod)`,
|
2017-05-11 13:20:27 +00:00
|
|
|
`{
|
2017-08-04 16:14:17 +00:00
|
|
|
"NetworkId": 4,
|
2017-05-11 13:20:27 +00:00
|
|
|
"DataDir": "$TMPDIR",
|
2017-08-04 16:14:17 +00:00
|
|
|
"DevMode": false
|
2017-05-11 13:20:27 +00:00
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-20 14:05:21 +00:00
|
|
|
require.True(t, nodeConfig.ClusterConfig.Enabled, "cluster configuration is expected to be enabled by default")
|
2017-05-11 13:20:27 +00:00
|
|
|
|
2018-03-20 14:05:21 +00:00
|
|
|
enodes := nodeConfig.ClusterConfig.StaticNodes
|
2018-02-26 18:36:04 +00:00
|
|
|
require.True(t, len(enodes) >= 3)
|
2017-05-11 13:20:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-03-20 14:05:21 +00:00
|
|
|
`select cluster configuration (Mainnet dev)`,
|
2017-05-11 13:20:27 +00:00
|
|
|
`{
|
2017-08-04 16:14:17 +00:00
|
|
|
"NetworkId": 1,
|
|
|
|
"DataDir": "$TMPDIR"
|
2017-05-11 13:20:27 +00:00
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-20 14:05:21 +00:00
|
|
|
require.True(t, nodeConfig.ClusterConfig.Enabled, "cluster configuration is expected to be enabled by default")
|
2017-05-11 13:20:27 +00:00
|
|
|
|
2018-03-20 14:05:21 +00:00
|
|
|
enodes := nodeConfig.ClusterConfig.StaticNodes
|
2018-02-26 18:36:04 +00:00
|
|
|
require.True(t, len(enodes) >= 2)
|
2017-05-11 13:20:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-03-20 14:05:21 +00:00
|
|
|
`select cluster configuration (Mainnet Prod)`,
|
2017-05-11 13:20:27 +00:00
|
|
|
`{
|
2017-08-04 16:14:17 +00:00
|
|
|
"NetworkId": 1,
|
2017-05-11 13:20:27 +00:00
|
|
|
"DataDir": "$TMPDIR",
|
2017-08-04 16:14:17 +00:00
|
|
|
"DevMode": false
|
2017-05-11 13:20:27 +00:00
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-20 14:05:21 +00:00
|
|
|
require.True(t, nodeConfig.ClusterConfig.Enabled, "cluster confguration is expected to be enabled by default")
|
2017-05-11 13:20:27 +00:00
|
|
|
|
2018-03-20 14:05:21 +00:00
|
|
|
enodes := nodeConfig.ClusterConfig.StaticNodes
|
2018-03-22 14:58:28 +00:00
|
|
|
require.True(t, len(enodes) == 0)
|
2017-05-11 13:20:27 +00:00
|
|
|
},
|
|
|
|
},
|
2017-05-16 03:24:56 +00:00
|
|
|
{
|
|
|
|
`default DevMode (true)`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 311,
|
|
|
|
"DataDir": "$TMPDIR"
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, nodeConfig.DevMode)
|
2018-03-20 14:05:21 +00:00
|
|
|
require.True(t, nodeConfig.ClusterConfig.Enabled)
|
2017-05-16 03:24:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`explicit DevMode = false`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"DevMode": false
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, nodeConfig.DevMode)
|
2018-03-20 14:05:21 +00:00
|
|
|
require.True(t, nodeConfig.ClusterConfig.Enabled)
|
2017-05-16 03:24:56 +00:00
|
|
|
},
|
|
|
|
},
|
2018-03-02 09:25:30 +00:00
|
|
|
{
|
|
|
|
`explicit WhisperConfig.LightClient = true`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"WhisperConfig": {
|
|
|
|
"LightClient": true
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, nodeConfig.WhisperConfig.LightClient)
|
|
|
|
},
|
|
|
|
},
|
2017-03-15 21:03:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 15:31:29 +00:00
|
|
|
// TestLoadNodeConfig tests loading JSON configuration and setting default values.
|
2017-03-15 21:03:01 +00:00
|
|
|
func TestLoadNodeConfig(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "geth-config-tests")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
defer os.RemoveAll(tmpDir) // nolint: errcheck
|
2017-03-15 21:03:01 +00:00
|
|
|
|
2018-03-19 16:22:09 +00:00
|
|
|
// create sample bootnodes config
|
|
|
|
err = ioutil.WriteFile(filepath.Join(tmpDir, "cluster.json"), clusterConfigData, os.ModePerm)
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-11 13:20:27 +00:00
|
|
|
t.Log(tmpDir)
|
|
|
|
|
2017-03-15 21:03:01 +00:00
|
|
|
for _, testCase := range loadConfigTestCases {
|
|
|
|
t.Log("test: " + testCase.name)
|
|
|
|
testCase.configJSON = strings.Replace(testCase.configJSON, "$TMPDIR", tmpDir, -1)
|
|
|
|
nodeConfig, err := params.LoadNodeConfig(testCase.configJSON)
|
|
|
|
testCase.validator(t, tmpDir, nodeConfig, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigWriteRead(t *testing.T) {
|
2017-12-04 16:11:14 +00:00
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "geth-config-tests")
|
|
|
|
require.Nil(t, err)
|
|
|
|
defer os.RemoveAll(tmpDir) // nolint: errcheck
|
2017-08-07 10:48:14 +00:00
|
|
|
|
2018-03-19 16:22:09 +00:00
|
|
|
nodeConfig, err := params.NewNodeConfig(tmpDir, "", params.RopstenNetworkID, true)
|
2017-12-04 16:11:14 +00:00
|
|
|
require.Nil(t, err, "cannot create new config object")
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-12-04 16:11:14 +00:00
|
|
|
err = nodeConfig.Save()
|
|
|
|
require.Nil(t, err, "cannot persist configuration")
|
2017-05-02 14:30:11 +00:00
|
|
|
|
2017-12-04 16:11:14 +00:00
|
|
|
loadedConfigData, err := ioutil.ReadFile(filepath.Join(nodeConfig.DataDir, "config.json"))
|
|
|
|
require.Nil(t, err, "cannot read configuration from disk")
|
|
|
|
require.Contains(t, string(loadedConfigData), fmt.Sprintf(`"DevMode": %t`, true))
|
|
|
|
require.Contains(t, string(loadedConfigData), fmt.Sprintf(`"NetworkId": %d`, params.RopstenNetworkID))
|
|
|
|
require.Contains(t, string(loadedConfigData), fmt.Sprintf(`"DataDir": "%s"`, tmpDir))
|
2017-03-15 21:03:01 +00:00
|
|
|
}
|
2017-08-10 15:31:29 +00:00
|
|
|
|
|
|
|
// TestNodeConfigValidate checks validation of individual fields.
|
|
|
|
func TestNodeConfigValidate(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
Config string
|
|
|
|
Error string
|
|
|
|
FieldErrors map[string]string // map[Field]Tag
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "Valid JSON config",
|
|
|
|
Config: `{
|
|
|
|
"NetworkId": 1,
|
|
|
|
"DataDir": "/tmp/data"
|
|
|
|
}`,
|
|
|
|
Error: "",
|
|
|
|
FieldErrors: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Invalid JSON config",
|
|
|
|
Config: `{"NetworkId": }`,
|
|
|
|
Error: "invalid character '}'",
|
|
|
|
FieldErrors: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Invalid field type",
|
|
|
|
Config: `{"NetworkId": "abc"}`,
|
|
|
|
Error: "json: cannot unmarshal string into Go struct field",
|
|
|
|
FieldErrors: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Validate all required fields",
|
|
|
|
Config: `{}`,
|
|
|
|
Error: "",
|
|
|
|
FieldErrors: map[string]string{
|
|
|
|
"NetworkID": "required",
|
|
|
|
"DataDir": "required",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Validate Name does not contain slash",
|
|
|
|
Config: `{
|
|
|
|
"NetworkId": 1,
|
|
|
|
"DataDir": "/some/dir",
|
|
|
|
"Name": "invalid/name"
|
|
|
|
}`,
|
|
|
|
Error: "",
|
|
|
|
FieldErrors: map[string]string{
|
|
|
|
"Name": "excludes",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Logf("Test Case %s", tc.Name)
|
|
|
|
|
|
|
|
_, err := params.LoadNodeConfig(tc.Config)
|
|
|
|
|
|
|
|
switch err := err.(type) {
|
|
|
|
case validator.ValidationErrors:
|
|
|
|
for _, ve := range err {
|
|
|
|
require.Contains(t, tc.FieldErrors, ve.Field())
|
|
|
|
require.Equal(t, tc.FieldErrors[ve.Field()], ve.Tag())
|
|
|
|
}
|
|
|
|
case error:
|
|
|
|
require.Contains(t, err.Error(), tc.Error)
|
|
|
|
case nil:
|
|
|
|
require.Empty(t, tc.Error)
|
|
|
|
require.Nil(t, tc.FieldErrors)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|