|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "time" |
| 7 | + |
| 8 | + tmcfg "github.com/cometbft/cometbft/config" |
| 9 | + "github.com/cosmos/cosmos-sdk/client/flags" |
| 10 | + serverconfig "github.com/cosmos/cosmos-sdk/server/config" |
| 11 | + "github.com/spf13/viper" |
| 12 | +) |
| 13 | + |
| 14 | +const flagOverwriteConfigWithDefaults = "overwrite-config-with-defaults" |
| 15 | + |
| 16 | +// applyRecommendedValues sets default values for specific configuration types. |
| 17 | +func applyRecommendedValues(cfg interface{}) { |
| 18 | + switch c := cfg.(type) { |
| 19 | + case *serverconfig.Config: |
| 20 | + c.BaseConfig.MinGasPrices = "0.1udvpn" |
| 21 | + c.StateSync.SnapshotInterval = 1000 |
| 22 | + case *tmcfg.Config: |
| 23 | + c.Consensus.TimeoutCommit = 3 * time.Second |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// initAppConfig initializes the application configuration with defaults. |
| 28 | +func initAppConfig() (string, interface{}) { |
| 29 | + cfg := serverconfig.DefaultConfig() |
| 30 | + cfgTemplate := serverconfig.DefaultConfigTemplate |
| 31 | + applyRecommendedValues(cfg) |
| 32 | + return cfgTemplate, cfg |
| 33 | +} |
| 34 | + |
| 35 | +// initTendermintConfig initializes the Tendermint configuration with defaults. |
| 36 | +func initTendermintConfig() *tmcfg.Config { |
| 37 | + cfg := tmcfg.DefaultConfig() |
| 38 | + applyRecommendedValues(cfg) |
| 39 | + return cfg |
| 40 | +} |
| 41 | + |
| 42 | +// overwriteConfig reads, updates, and writes a configuration file. |
| 43 | +func overwriteConfig(name string, cfg interface{}, write func(string, interface{}) error) error { |
| 44 | + homeDir := viper.GetString(flags.FlagHome) |
| 45 | + cfgPath := filepath.Join(homeDir, "config", name) |
| 46 | + |
| 47 | + if _, err := os.Stat(cfgPath); err != nil { |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + v := viper.New() |
| 52 | + v.SetConfigFile(cfgPath) |
| 53 | + if err := v.ReadInConfig(); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + if err := v.Unmarshal(cfg); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + applyRecommendedValues(cfg) |
| 61 | + return write(cfgPath, cfg) |
| 62 | +} |
| 63 | + |
| 64 | +// overwriteAppConfig updates and writes the app configuration. |
| 65 | +func overwriteAppConfig() error { |
| 66 | + return overwriteConfig("app.toml", &serverconfig.Config{}, func(cfgPath string, cfg interface{}) error { |
| 67 | + serverconfig.WriteConfigFile(cfgPath, cfg.(*serverconfig.Config)) |
| 68 | + return nil |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +// overwriteTendermintConfig updates and writes the Tendermint configuration. |
| 73 | +func overwriteTendermintConfig() error { |
| 74 | + return overwriteConfig("config.toml", &tmcfg.Config{}, func(cfgPath string, cfg interface{}) error { |
| 75 | + tmcfg.WriteConfigFile(cfgPath, cfg.(*tmcfg.Config)) |
| 76 | + return nil |
| 77 | + }) |
| 78 | +} |
0 commit comments