diff --git a/cmd/sentinelhub/config.go b/cmd/sentinelhub/config.go new file mode 100644 index 00000000..effa4dc3 --- /dev/null +++ b/cmd/sentinelhub/config.go @@ -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 + }) +} diff --git a/cmd/sentinelhub/root.go b/cmd/sentinelhub/root.go index ed1fdae9..d8b84822 100644 --- a/cmd/sentinelhub/root.go +++ b/cmd/sentinelhub/root.go @@ -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" @@ -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 { @@ -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). diff --git a/go.mod b/go.mod index 2d33e6ba..b5cd4983 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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