Skip to content

Commit

Permalink
cli: add embadded node config
Browse files Browse the repository at this point in the history
If config-path not pass then defualt configs is used according to set
network. in case of VM cli the default privnet config and InMemory db is
used.

Close #3450

Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
  • Loading branch information
AliceInHunterland committed Jun 26, 2024
1 parent d9d9d00 commit edbb172
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
44 changes: 43 additions & 1 deletion cli/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (

"github.com/nspcc-dev/neo-go/cli/flags"
"github.com/nspcc-dev/neo-go/cli/input"
configFiles "github.com/nspcc-dev/neo-go/config"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io"
Expand Down Expand Up @@ -194,6 +196,10 @@ func GetRPCWithInvoker(gctx context.Context, ctx *cli.Context, signers []transac
// GetConfigFromContext looks at the path and the mode flags in the given config and
// returns an appropriate config.
func GetConfigFromContext(ctx *cli.Context) (config.Config, error) {
if ctx.NumFlags() == 0 {
return getDefaultVMConfig()
}

var (
configFile = ctx.String("config-file")
relativePath = ctx.String("relative-path")
Expand All @@ -205,7 +211,43 @@ func GetConfigFromContext(ctx *cli.Context) (config.Config, error) {
if argCp := ctx.String("config-path"); argCp != "" {
configPath = argCp
}
return config.Load(configPath, GetNetwork(ctx), relativePath)

network := GetNetwork(ctx)
switch network {
case netmode.MainNet:
return getEmbeddedConfig(configFiles.MainnetConfig)
case netmode.TestNet:
return getEmbeddedConfig(configFiles.TestnetConfig)
case netmode.UnitTestNet:
return getEmbeddedConfig(configFiles.UnitTestnetConfig)
case netmode.PrivNet:
return getEmbeddedConfig(configFiles.PrivnetConfig)
default:
return config.Load(configPath, network, relativePath)
}
}

// getDefaultVMConfig returns the default VM config
func getDefaultVMConfig() (config.Config, error) {
var cfgData = configFiles.PrivnetConfig

var cfg config.Config
err := yaml.Unmarshal(cfgData, &cfg)
if err != nil {
return cfg, fmt.Errorf("failed to unmarshal default config: %w", err)
}
cfg.ApplicationConfiguration.DBConfiguration.Type = dbconfig.InMemoryDB
return cfg, nil
}

// getEmbeddedConfig returns the embedded configuration based on the provided data
func getEmbeddedConfig(cfgData []byte) (config.Config, error) {
var cfg config.Config
err := yaml.Unmarshal(cfgData, &cfg)
if err != nil {
return cfg, fmt.Errorf("failed to unmarshal embedded config: %w", err)
}
return cfg, nil
}

var (
Expand Down
19 changes: 19 additions & 0 deletions config/config_embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
_ "embed"
)

// Embedding configuration files
//
//go:embed protocol.mainnet.yml
var MainnetConfig []byte

//go:embed protocol.testnet.yml
var TestnetConfig []byte

//go:embed protocol.unit_testnet.yml
var UnitTestnetConfig []byte

//go:embed protocol.privnet.yml
var PrivnetConfig []byte

0 comments on commit edbb172

Please sign in to comment.