Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmd/gossamer): embed default toml config files #3091

Merged
merged 12 commits into from
Apr 5, 2023
10 changes: 10 additions & 0 deletions chain/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2023 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package chain

import "embed"

// DefaultConfigTomlFiles is the embedded file system containing the default toml configurations.
//
//go:embed */*.toml
var DefaultConfigTomlFiles embed.FS
20 changes: 10 additions & 10 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ import (
var (
// DefaultCfg is the default configuration for the node.
DefaultCfg = dot.PolkadotConfig
defaultKusamaConfigPath = "./chain/kusama/config.toml"
defaultPolkadotConfigPath = "./chain/polkadot/config.toml"
defaultWestendDevConfigPath = "./chain/westend-dev/config.toml"
defaultWestendConfigPath = "./chain/westend/config.toml"
defaultKusamaConfigPath = "kusama/config.toml"
defaultPolkadotConfigPath = "polkadot/config.toml"
defaultWestendDevConfigPath = "westend-dev/config.toml"
defaultWestendConfigPath = "westend/config.toml"
)

// loadConfigFile loads a default config file if --chain is specified, a specific
// config if --config is specified, or the default gossamer config otherwise.
func loadConfigFile(ctx *cli.Context, cfg *ctoml.Config) (err error) {
cfgPath := ctx.String(ConfigFlag.Name)
if cfgPath == "" {
return loadConfig(cfg, defaultPolkadotConfigPath)
return loadConfigFromResource(cfg, defaultPolkadotConfigPath)
}

logger.Info("loading toml configuration from " + cfgPath + "...")
Expand All @@ -48,7 +48,7 @@ func loadConfigFile(ctx *cli.Context, cfg *ctoml.Config) (err error) {
"overwriting default configuration with id " + cfg.Global.ID +
" with toml configuration values from " + cfgPath)
}
return loadConfig(cfg, cfgPath)
return loadConfigFromFile(cfg, cfgPath)
}

func setupConfigFromChain(ctx *cli.Context) (*ctoml.Config, *dot.Config, error) {
Expand All @@ -68,22 +68,22 @@ func setupConfigFromChain(ctx *cli.Context) (*ctoml.Config, *dot.Config, error)
logger.Info("loading toml configuration from " + defaultKusamaConfigPath + "...")
tomlCfg = &ctoml.Config{}
cfg = dot.KusamaConfig()
err = loadConfig(tomlCfg, defaultKusamaConfigPath)
err = loadConfigFromResource(tomlCfg, defaultKusamaConfigPath)
case "polkadot":
logger.Info("loading toml configuration from " + defaultPolkadotConfigPath + "...")
tomlCfg = &ctoml.Config{}
cfg = dot.PolkadotConfig()
err = loadConfig(tomlCfg, defaultPolkadotConfigPath)
err = loadConfigFromResource(tomlCfg, defaultPolkadotConfigPath)
case "westend-dev":
logger.Info("loading toml configuration from " + defaultWestendDevConfigPath + "...")
tomlCfg = &ctoml.Config{}
cfg = dot.WestendDevConfig()
err = loadConfig(tomlCfg, defaultWestendDevConfigPath)
err = loadConfigFromResource(tomlCfg, defaultWestendDevConfigPath)
case "westend":
logger.Info("loading toml configuration from " + defaultWestendConfigPath + "...")
tomlCfg = &ctoml.Config{}
cfg = dot.WestendConfig()
err = loadConfig(tomlCfg, defaultWestendConfigPath)
err = loadConfigFromResource(tomlCfg, defaultWestendConfigPath)
default:
logger.Info("loading chain config from " + id + "...")
fileInfo, err := os.Stat(id)
Expand Down
2 changes: 1 addition & 1 deletion cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ func TestGlobalNodeName_WhenNodeAlreadyHasStoredName(t *testing.T) {
cfg := newTestConfig(t, westendDevConfig)
cfg.Global.Name = globalName

runtimeFilePath, err := runtime.GetRuntime(context.Background(), runtime.NODE_RUNTIME)
runtimeFilePath, err := runtime.GetRuntime(context.Background(), runtime.WESTEND_RUNTIME_v0929)
require.NoError(t, err)
runtimeData, err := os.ReadFile(runtimeFilePath)
require.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions cmd/gossamer/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

"github.com/ChainSafe/gossamer/dot"

ctoml "github.com/ChainSafe/gossamer/dot/config/toml"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -165,7 +164,7 @@ func TestExportCommand(t *testing.T) {
config := ctx.String(ConfigFlag.Name)

cfg := new(ctoml.Config)
err = loadConfig(cfg, config)
err = loadConfigFromFile(cfg, config)
require.NoError(t, err)

require.Equal(t, dotConfigToToml(c.expected), cfg)
Expand Down
11 changes: 5 additions & 6 deletions cmd/gossamer/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ func runTestGossamer(t *testing.T, args ...string) *testgossamer {
return tt
}

var testWestendDevConfigPath string

func TestMain(m *testing.M) {
if reexec.Init() {
return
Expand All @@ -202,10 +204,7 @@ func TestMain(m *testing.M) {
panic(err)
}

defaultKusamaConfigPath = filepath.Join(rootPath, "./chain/kusama/config.toml")
defaultPolkadotConfigPath = filepath.Join(rootPath, "./chain/polkadot/config.toml")
defaultWestendDevConfigPath = filepath.Join(rootPath, "./chain/westend-dev/config.toml")

testWestendDevConfigPath = filepath.Join(rootPath, "./chain/westend-dev/config.toml")
edwardmack marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(m.Run())
}

Expand Down Expand Up @@ -234,7 +233,7 @@ func TestInitCommand_RenameNodeWhenCalled(t *testing.T) {
"--basepath", tempDir,
"--genesis", genesisPath,
"--name", nodeName,
"--config", defaultWestendDevConfigPath,
"--config", testWestendDevConfigPath,
"--force",
)

Expand All @@ -249,7 +248,7 @@ func TestInitCommand_RenameNodeWhenCalled(t *testing.T) {
"init",
"--basepath", tempDir,
"--genesis", genesisPath,
"--config", defaultWestendDevConfigPath,
"--config", testWestendDevConfigPath,
"--force",
)

Expand Down
20 changes: 15 additions & 5 deletions cmd/gossamer/toml_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,35 @@ import (
"reflect"
"unicode"

"github.com/naoina/toml"

"github.com/ChainSafe/gossamer/chain"
ctoml "github.com/ChainSafe/gossamer/dot/config/toml"
"github.com/naoina/toml"
)

// loadConfig loads the values from the toml configuration file into the provided configuration
func loadConfig(cfg *ctoml.Config, fp string) error {
func loadConfigFromResource(cfg *ctoml.Config, resourcePath string) error {
file, err := chain.DefaultConfigTomlFiles.Open(resourcePath)
if err != nil {
return fmt.Errorf("opening toml configuration file: %w", err)
}
return loadConfig(cfg, file)
}

func loadConfigFromFile(cfg *ctoml.Config, fp string) error {
fp, err := filepath.Abs(fp)
if err != nil {
logger.Errorf("failed to create absolute path for toml configuration file: %s", err)
return err
}

file, err := os.Open(filepath.Clean(fp))
if err != nil {
logger.Errorf("failed to open toml configuration file: %s", err)
return err
}
return loadConfig(cfg, file)
}

// loadConfig loads the values from the toml configuration file into the provided configuration
func loadConfig(cfg *ctoml.Config, file fs.File) (err error) {
var tomlSettings = toml.Config{
NormFieldName: func(rt reflect.Type, key string) string {
return key
Expand Down
6 changes: 3 additions & 3 deletions cmd/gossamer/toml_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestLoadConfig(t *testing.T) {
err := dot.InitNode(cfg)
require.NoError(t, err)

err = loadConfig(dotConfigToToml(cfg), cfgFile)
err = loadConfigFromFile(dotConfigToToml(cfg), cfgFile)
require.NoError(t, err)
}

Expand All @@ -43,7 +43,7 @@ func TestLoadConfigWestendDev(t *testing.T) {
projectRootPath := utils.GetProjectRootPathTest(t)
configPath := filepath.Join(projectRootPath, "./chain/westend-dev/config.toml")

err = loadConfig(dotConfigToToml(cfg), configPath)
err = loadConfigFromFile(dotConfigToToml(cfg), configPath)
require.NoError(t, err)
}

Expand All @@ -60,6 +60,6 @@ func TestLoadConfigKusama(t *testing.T) {
projectRootPath := utils.GetProjectRootPathTest(t)
kusamaConfigPath := filepath.Join(projectRootPath, "./chain/kusama/config.toml")

err = loadConfig(dotConfigToToml(cfg), kusamaConfigPath)
err = loadConfigFromFile(dotConfigToToml(cfg), kusamaConfigPath)
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func TestHandleChainReorg_WithReorg_Transactions(t *testing.T) {
t.Skip() // need to update this test to use a valid transaction

cfg := &Config{
Runtime: wasmer.NewTestInstance(t, runtime.NODE_RUNTIME),
Runtime: wasmer.NewTestInstance(t, runtime.WESTEND_RUNTIME_v0929),
}

s := NewTestService(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion dot/node_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func TestNode_PersistGlobalName_WhenInitialize(t *testing.T) {
// newTestGenesisAndRuntime create a new test runtime and a new test genesis
// file with the test runtime stored in raw data and returns the genesis file
func newTestGenesisAndRuntime(t *testing.T) (filename string) {
runtimeFilePath, err := runtime.GetRuntime(context.Background(), runtime.NODE_RUNTIME)
runtimeFilePath, err := runtime.GetRuntime(context.Background(), runtime.WESTEND_RUNTIME_v0929)
require.NoError(t, err)
runtimeData, err := os.ReadFile(runtimeFilePath)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/dev_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func newBABEService(t *testing.T) *babe.Service {

bs, es := newState(t)
tt := trie.NewEmptyTrie()
rt := wasmer.NewTestInstanceWithTrie(t, runtime.NODE_RUNTIME, tt)
rt := wasmer.NewTestInstanceWithTrie(t, runtime.WESTEND_RUNTIME_v0929, tt)
bs.StoreRuntime(bs.GenesisHash(), rt)
tt.Put(
common.MustHexToBytes("0x886726f904d8372fdabb7707870c2fad"),
Expand Down
32 changes: 19 additions & 13 deletions dot/rpc/modules/state_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,35 @@ const (
)

func TestStateModule_GetRuntimeVersion(t *testing.T) {
// expected results based on responses from prior tests
/* expected results based on responses from prior tests
We can get this data from polkadot runtime release source code
https://github.com/paritytech/polkadot/blob/v0.9.29/runtime/westend/src/lib.rs#L105-L117
*/
expected := StateRuntimeVersionResponse{
SpecName: "node",
ImplName: "substrate-node",
AuthoringVersion: 10,
SpecVersion: 264,
SpecName: "westend",
ImplName: "parity-westend",
AuthoringVersion: 2,
SpecVersion: 9290,
ImplVersion: 0,
Apis: []interface{}{
[]interface{}{"0xdf6acb689907609b", uint32(3)},
[]interface{}{"0xdf6acb689907609b", uint32(4)},
[]interface{}{"0x37e397fc7c91f5e4", uint32(1)},
[]interface{}{"0x40fe3ad401f8959a", uint32(4)},
[]interface{}{"0xd2bc9897eed08f15", uint32(2)},
[]interface{}{"0x40fe3ad401f8959a", uint32(6)},
[]interface{}{"0xd2bc9897eed08f15", uint32(3)},
[]interface{}{"0xf78b278be53f454c", uint32(2)},
[]interface{}{"0xed99c5acb25eedf5", uint32(2)},
[]interface{}{"0xaf2c0297a23e6d3d", uint32(2)},
[]interface{}{"0x49eaaf1b548a0cb0", uint32(1)},
[]interface{}{"0x91d5df18b0d2cf58", uint32(1)},
[]interface{}{"0xed99c5acb25eedf5", uint32(3)},
[]interface{}{"0xcbca25e39f142387", uint32(2)},
[]interface{}{"0x687ad44ad37f03c2", uint32(1)},
[]interface{}{"0xab3c0572291feb8b", uint32(1)},
[]interface{}{"0xbc9d89904f5b923f", uint32(1)},
[]interface{}{"0x68b66ba122c93fa7", uint32(1)},
[]interface{}{"0x37c8bb1350a9a2a8", uint32(1)},
[]interface{}{"0x91d5df18b0d2cf58", uint32(1)},
[]interface{}{"0xab3c0572291feb8b", uint32(1)},
[]interface{}{"0xf3ff14d5ab527059", uint32(1)},
[]interface{}{"0x17a6bc0d0062aeb3", uint32(1)},
},
TransactionVersion: 2,
TransactionVersion: 12,
}

sm, hash, _ := setupStateModule(t)
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/system_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func setupSystemModule(t *testing.T) *SystemModule {
func newCoreService(t *testing.T, srvc *state.Service) *core.Service {
// setup service
tt := trie.NewEmptyTrie()
rt := wasmer.NewTestInstanceWithTrie(t, runtime.NODE_RUNTIME, tt)
rt := wasmer.NewTestInstanceWithTrie(t, runtime.WESTEND_RUNTIME_v0929, tt)
ks := keystore.NewGlobalKeystore()
t.Cleanup(func() {
rt.Stop()
Expand Down
10 changes: 0 additions & 10 deletions lib/runtime/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ package runtime

//nolint:revive
const (
// v0.9 substrate runtime
NODE_RUNTIME = "node_runtime"
NODE_RUNTIME_FP = "node_runtime.compact.wasm"
NODE_RUNTIME_URL = "https://github.com/noot/substrate/blob/noot/v0.9/target/debug/wbuild/node-runtime/node_runtime.compact.wasm?raw=true" //nolint:lll

// v0.9.8 substrate runtime
NODE_RUNTIME_v098 = "node_runtime-v0.9.8"
NODE_RUNTIME_FP_v098 = "node_runtime-v0.9.8.compact.wasm"
NODE_RUNTIME_URL_v098 = "https://github.com/noot/substrate/blob/noot/v0.9.8/target/debug/wbuild/node-runtime/node_runtime.compact.wasm?raw=true" //nolint:lll

// v0.9 test API wasm
HOST_API_TEST_RUNTIME = "hostapi_runtime"
HOST_API_TEST_RUNTIME_FP = "hostapi_runtime.compact.wasm"
Expand Down
6 changes: 0 additions & 6 deletions lib/runtime/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ func GetRuntime(ctx context.Context, runtime string) (

var runtimeFilename, url string
switch runtime {
case NODE_RUNTIME:
runtimeFilename = NODE_RUNTIME_FP
url = NODE_RUNTIME_URL
case NODE_RUNTIME_v098:
runtimeFilename = NODE_RUNTIME_FP_v098
url = NODE_RUNTIME_URL_v098
case HOST_API_TEST_RUNTIME:
runtimeFilename = HOST_API_TEST_RUNTIME_FP
url = HOST_API_TEST_RUNTIME_URL
Expand Down
Loading