2017-03-15 21:03:01 +00:00
|
|
|
package params_test
|
|
|
|
|
|
|
|
import (
|
2017-05-02 14:30:11 +00:00
|
|
|
"encoding/json"
|
2017-03-15 21:03:01 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
|
|
|
"github.com/status-im/status-go/geth/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
var loadConfigTestCases = []struct {
|
|
|
|
name string
|
|
|
|
configJSON string
|
|
|
|
validator func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`invalid input configuration`,
|
|
|
|
`{
|
|
|
|
"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) {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error is expected, not thrown")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`missing required field (DataDir)`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"Name": "TestStatusNode"
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
|
|
|
if err != params.ErrMissingDataDir {
|
|
|
|
t.Fatalf("expected error not thrown, expected: %v, thrown: %v", params.ErrMissingDataDir, err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`missing required field (NetworkId)`,
|
|
|
|
`{
|
|
|
|
"DataDir": "$TMPDIR"
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
|
|
|
if err != params.ErrMissingNetworkId {
|
|
|
|
t.Fatalf("expected error not thrown, expected: %v, thrown: %v", params.ErrMissingNetworkId, err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
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) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2017-03-18 18:23:58 +00:00
|
|
|
expectedDataDir := "/storage/emulated/0/ethereum/"
|
|
|
|
if nodeConfig.DataDir != expectedDataDir {
|
|
|
|
t.Fatalf("incorrect DataDir used, expected: %v, got: %v", expectedDataDir, 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) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(dataDir); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("data directory doesn't exist: %s", dataDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedDataDir := dataDir
|
|
|
|
if nodeConfig.DataDir != expectedDataDir {
|
|
|
|
t.Fatalf("incorrect DataDir used, expected: %v, got: %v", expectedDataDir, nodeConfig.DataDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedKeyStoreDir := filepath.Join(dataDir, params.KeyStoreDir)
|
|
|
|
if nodeConfig.KeyStoreDir != expectedKeyStoreDir {
|
|
|
|
t.Fatalf("incorrect KeyStoreDir used, expected: %v, got: %v", expectedKeyStoreDir, nodeConfig.KeyStoreDir)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`use non-default KeyStoreDir`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"KeyStoreDir": "/foo/bar"
|
|
|
|
}`,
|
|
|
|
func(t *testing.T, dataDir string, nodeConfig *params.NodeConfig, err error) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
expectedDataDir := dataDir
|
|
|
|
if nodeConfig.DataDir != expectedDataDir {
|
|
|
|
t.Fatalf("incorrect DataDir used, expected: %v, got: %v", expectedDataDir, nodeConfig.DataDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedKeyStoreDir := "/foo/bar"
|
|
|
|
if nodeConfig.KeyStoreDir != expectedKeyStoreDir {
|
|
|
|
t.Fatalf("incorrect KeyStoreDir used, expected: %v, got: %v", expectedKeyStoreDir, nodeConfig.KeyStoreDir)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2017-03-15 21:03:01 +00:00
|
|
|
{
|
|
|
|
`test parameter overriding`,
|
|
|
|
`{
|
|
|
|
"NetworkId": 3,
|
|
|
|
"DataDir": "$TMPDIR",
|
|
|
|
"Name": "TestStatusNode",
|
|
|
|
"WSPort": 4242,
|
|
|
|
"IPCEnabled": true,
|
|
|
|
"WSEnabled": false,
|
|
|
|
"LightEthConfig": {
|
|
|
|
"DatabaseCache": 64
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
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.Name != "TestStatusNode" {
|
|
|
|
t.Fatal("wrong Name")
|
|
|
|
}
|
|
|
|
|
2017-04-09 22:16:05 +00:00
|
|
|
if nodeConfig.HTTPPort != params.HTTPPort {
|
2017-03-15 21:03:01 +00:00
|
|
|
t.Fatal("wrong HTTPPort")
|
|
|
|
}
|
|
|
|
|
2017-04-09 22:16:05 +00:00
|
|
|
if nodeConfig.HTTPHost != params.HTTPHost {
|
2017-03-15 21:03:01 +00:00
|
|
|
t.Fatal("wrong HTTPHost")
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodeConfig.WSPort != 4242 {
|
|
|
|
t.Fatal("wrong WSPort")
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodeConfig.WSEnabled != false {
|
|
|
|
t.Fatal("wrong WSEnabled")
|
|
|
|
}
|
|
|
|
|
2017-03-18 18:23:58 +00:00
|
|
|
if nodeConfig.IPCEnabled != true {
|
2017-03-15 21:03:01 +00:00
|
|
|
t.Fatal("wrong IPCEnabled")
|
|
|
|
}
|
|
|
|
if nodeConfig.LightEthConfig.DatabaseCache != 64 {
|
|
|
|
t.Fatal("wrong LightEthConfig.DatabaseCache")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`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) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
genesis := new(core.Genesis)
|
|
|
|
if err := json.Unmarshal([]byte(nodeConfig.LightEthConfig.Genesis), genesis); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
chainConfig := genesis.Config
|
2017-03-15 21:03:01 +00:00
|
|
|
refChainConfig := gethparams.TestnetChainConfig
|
|
|
|
|
|
|
|
if chainConfig.HomesteadBlock.Cmp(refChainConfig.HomesteadBlock) != 0 {
|
|
|
|
t.Fatal("invalid chainConfig.HomesteadBlock")
|
|
|
|
}
|
|
|
|
if chainConfig.DAOForkBlock != nil { // already forked
|
|
|
|
t.Fatal("invalid chainConfig.DAOForkBlock")
|
|
|
|
}
|
|
|
|
if chainConfig.DAOForkSupport != refChainConfig.DAOForkSupport {
|
|
|
|
t.Fatal("invalid chainConfig.DAOForkSupport")
|
|
|
|
}
|
|
|
|
if chainConfig.EIP150Block.Cmp(refChainConfig.EIP150Block) != 0 {
|
|
|
|
t.Fatal("invalid chainConfig.EIP150Block")
|
|
|
|
}
|
|
|
|
if chainConfig.EIP150Hash != refChainConfig.EIP150Hash {
|
|
|
|
t.Fatal("invalid chainConfig.EIP150Hash")
|
|
|
|
}
|
|
|
|
if chainConfig.EIP155Block.Cmp(refChainConfig.EIP155Block) != 0 {
|
|
|
|
t.Fatal("invalid chainConfig.EIP155Block")
|
|
|
|
}
|
|
|
|
if chainConfig.EIP158Block.Cmp(refChainConfig.EIP158Block) != 0 {
|
|
|
|
t.Fatal("invalid chainConfig.EIP158Block")
|
|
|
|
}
|
|
|
|
if chainConfig.ChainId.Cmp(refChainConfig.ChainId) != 0 {
|
|
|
|
t.Fatal("invalid chainConfig.ChainId")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`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) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
genesis := new(core.Genesis)
|
|
|
|
if err := json.Unmarshal([]byte(nodeConfig.LightEthConfig.Genesis), genesis); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
chainConfig := genesis.Config
|
2017-03-15 21:03:01 +00:00
|
|
|
if chainConfig.HomesteadBlock.Cmp(gethparams.MainNetHomesteadBlock) != 0 {
|
|
|
|
t.Fatal("invalid chainConfig.HomesteadBlock")
|
|
|
|
}
|
|
|
|
if chainConfig.DAOForkBlock.Cmp(gethparams.MainNetDAOForkBlock) != 0 {
|
|
|
|
t.Fatal("invalid chainConfig.DAOForkBlock")
|
|
|
|
}
|
|
|
|
if chainConfig.DAOForkSupport != true {
|
|
|
|
t.Fatal("invalid chainConfig.DAOForkSupport")
|
|
|
|
}
|
|
|
|
if chainConfig.EIP150Block.Cmp(gethparams.MainNetHomesteadGasRepriceBlock) != 0 {
|
|
|
|
t.Fatal("invalid chainConfig.EIP150Block")
|
|
|
|
}
|
|
|
|
if chainConfig.EIP150Hash != gethparams.MainNetHomesteadGasRepriceHash {
|
|
|
|
t.Fatal("invalid chainConfig.EIP150Hash")
|
|
|
|
}
|
|
|
|
if chainConfig.EIP155Block.Cmp(gethparams.MainNetSpuriousDragon) != 0 {
|
|
|
|
t.Fatal("invalid chainConfig.EIP155Block")
|
|
|
|
}
|
|
|
|
if chainConfig.EIP158Block.Cmp(gethparams.MainNetSpuriousDragon) != 0 {
|
|
|
|
t.Fatal("invalid chainConfig.EIP158Block")
|
|
|
|
}
|
|
|
|
if chainConfig.ChainId.Cmp(gethparams.MainNetChainID) != 0 {
|
|
|
|
t.Fatal("invalid chainConfig.ChainId")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`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) {
|
|
|
|
//nodeConfig.LightEthConfig.Genesis = nodeConfig.LightEthConfig.Genesis[:125]
|
|
|
|
//fmt.Println(nodeConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
networkId := uint64(311)
|
2017-03-15 21:03:01 +00:00
|
|
|
if nodeConfig.NetworkId != networkId {
|
|
|
|
t.Fatalf("unexpected NetworkId, expected: %v, got: %v", networkId, nodeConfig.NetworkId)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadNodeConfig(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "geth-config-tests")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
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-05-02 14:30:11 +00:00
|
|
|
configReadWrite := func(networkId uint64, refFile string) {
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "geth-config-tests")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
2017-03-15 21:03:01 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
nodeConfig, err := params.NewNodeConfig(tmpDir, networkId)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot create new config object: %v", err)
|
|
|
|
}
|
2017-03-15 21:03:01 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
if err := nodeConfig.Save(); err != nil {
|
|
|
|
t.Fatalf("cannot persist configuration: %v", err)
|
|
|
|
}
|
2017-03-15 21:03:01 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
loadedConfigData, err := ioutil.ReadFile(filepath.Join(nodeConfig.DataDir, "config.json"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot read configuration from disk: %v", err)
|
|
|
|
}
|
2017-03-15 21:03:01 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
refConfigData := geth.LoadFromFile(refFile)
|
2017-03-15 21:03:01 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
refConfigData = strings.Replace(refConfigData, "$TMPDIR", nodeConfig.DataDir, -1)
|
|
|
|
refConfigData = strings.Replace(refConfigData, "$VERSION", params.Version, -1)
|
|
|
|
if string(loadedConfigData) != refConfigData {
|
|
|
|
t.Fatalf("configuration mismatch,\nexpected: %v\ngot: %v", refConfigData, string(loadedConfigData))
|
|
|
|
}
|
2017-03-15 21:03:01 +00:00
|
|
|
}
|
2017-05-02 14:30:11 +00:00
|
|
|
|
|
|
|
configReadWrite(params.TestNetworkId, "testdata/config.testnet.json")
|
|
|
|
configReadWrite(params.MainNetworkId, "testdata/config.mainnet.json")
|
2017-03-15 21:03:01 +00:00
|
|
|
}
|