Skip to content

Commit

Permalink
feat: add overwrite config with recommended defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
ironman0x7b2 committed Dec 19, 2024
1 parent 767e06b commit 8e2d531
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 25 deletions.
78 changes: 78 additions & 0 deletions cmd/sentinelhub/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"os"
"path/filepath"
"time"

tmcfg "github.com/cometbft/cometbft/config"
"github.com/cosmos/cosmos-sdk/client/flags"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/spf13/viper"
)

const flagOverwriteConfigWithDefaults = "overwrite-config-with-defaults"

// applyRecommendedValues sets default values for specific configuration types.
func applyRecommendedValues(cfg interface{}) {
switch c := cfg.(type) {
case *serverconfig.Config:
c.BaseConfig.MinGasPrices = "0.1udvpn"
c.StateSync.SnapshotInterval = 1000
case *tmcfg.Config:
c.Consensus.TimeoutCommit = 3 * time.Second
}
}

// initAppConfig initializes the application configuration with defaults.
func initAppConfig() (string, interface{}) {
cfg := serverconfig.DefaultConfig()
cfgTemplate := serverconfig.DefaultConfigTemplate
applyRecommendedValues(cfg)
return cfgTemplate, cfg
}

// initTendermintConfig initializes the Tendermint configuration with defaults.
func initTendermintConfig() *tmcfg.Config {
cfg := tmcfg.DefaultConfig()
applyRecommendedValues(cfg)
return cfg
}

// overwriteConfig reads, updates, and writes a configuration file.
func overwriteConfig(name string, cfg interface{}, write func(string, interface{}) error) error {
homeDir := viper.GetString(flags.FlagHome)
cfgPath := filepath.Join(homeDir, "config", name)

if _, err := os.Stat(cfgPath); err != nil {
return nil
}

v := viper.New()
v.SetConfigFile(cfgPath)
if err := v.ReadInConfig(); err != nil {
return err
}
if err := v.Unmarshal(cfg); err != nil {
return err
}

applyRecommendedValues(cfg)
return write(cfgPath, cfg)
}

// overwriteAppConfig updates and writes the app configuration.
func overwriteAppConfig() error {
return overwriteConfig("app.toml", &serverconfig.Config{}, func(cfgPath string, cfg interface{}) error {
serverconfig.WriteConfigFile(cfgPath, cfg.(*serverconfig.Config))
return nil
})
}

// overwriteTendermintConfig updates and writes the Tendermint configuration.
func overwriteTendermintConfig() error {
return overwriteConfig("config.toml", &tmcfg.Config{}, func(cfgPath string, cfg interface{}) error {
tmcfg.WriteConfigFile(cfgPath, cfg.(*tmcfg.Config))
return nil
})
}
38 changes: 14 additions & 24 deletions cmd/sentinelhub/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"

"github.com/CosmWasm/wasmd/x/wasm"
tmcfg "github.com/cometbft/cometbft/config"
tmcli "github.com/cometbft/cometbft/libs/cli"
"github.com/cosmos/cosmos-sdk/client"
clientconfig "github.com/cosmos/cosmos-sdk/client/config"
Expand All @@ -13,39 +12,20 @@ import (
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/server"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
authcli "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/sentinel-official/hub/v12/app"
)

func initAppConfig() (string, interface{}) {
type Config struct {
*serverconfig.Config
}

cfg := Config{Config: serverconfig.DefaultConfig()}
cfg.BaseConfig.MinGasPrices = "0.1udvpn"
cfg.StateSync.SnapshotInterval = 1000

cfgTemplate := serverconfig.DefaultConfigTemplate

return cfgTemplate, cfg
}

func initTendermintConfig() *tmcfg.Config {
cfg := tmcfg.DefaultConfig()

return cfg
}

func moduleInitFlags(cmd *cobra.Command) {
crisis.AddModuleInitFlags(cmd)
wasm.AddModuleInitFlags(cmd)
cmd.Flags().Bool(flagOverwriteConfigWithDefaults, true, "If set to true, recommended default values will overwrite any existing settings in config.toml and app.toml.")
}

func queryCommand() *cobra.Command {
Expand Down Expand Up @@ -100,9 +80,19 @@ func txCommand() *cobra.Command {
func NewRootCmd(homeDir string) *cobra.Command {
encCfg := app.DefaultEncodingConfig()
cmd := &cobra.Command{
Use: "sentinelhub",
Short: "Sentinel Hub application",
Use: "sentinelhub",
Short: "Sentinel Hub application",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, _ []string) (err error) {
if viper.GetBool(flagOverwriteConfigWithDefaults) {
if err := overwriteTendermintConfig(); err != nil {
return err
}
if err := overwriteAppConfig(); err != nil {
return err
}
}

clientCtx := client.Context{}.
WithAccountRetriever(authtypes.AccountRetriever{}).
WithCodec(encCfg.Codec).
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/spf13/cast v1.6.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142
google.golang.org/grpc v1.67.0
Expand Down Expand Up @@ -162,7 +163,6 @@ require (
github.com/sasha-s/go-deadlock v0.3.5 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand Down

0 comments on commit 8e2d531

Please sign in to comment.