diff --git a/CHANGELOG.md b/CHANGELOG.md index 45c93da693c5..65d0f352aa04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/evidence) [#8461](https://github.com/cosmos/cosmos-sdk/pull/8461) Fix bech32 prefix in evidence validator address conversion * (x/slashing) [\#8427](https://github.com/cosmos/cosmos-sdk/pull/8427) Fix query signing infos command * (server) [\#8399](https://github.com/cosmos/cosmos-sdk/pull/8399) fix gRPC-web flag default value +* (server) [\#8641](https://github.com/cosmos/cosmos-sdk/pull/8641) Fix Tendermint and application configuration reading from file ## [v0.41.1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.41.1) - 2021-02-17 diff --git a/server/util.go b/server/util.go index bc913eb2962c..68f1c4828166 100644 --- a/server/util.go +++ b/server/util.go @@ -184,11 +184,11 @@ func SetCmdServerContext(cmd *cobra.Command, serverCtx *Context) error { func interceptConfigs(rootViper *viper.Viper) (*tmcfg.Config, error) { rootDir := rootViper.GetString(flags.FlagHome) configPath := filepath.Join(rootDir, "config") - configFile := filepath.Join(configPath, "config.toml") + tmCfgFile := filepath.Join(configPath, "config.toml") conf := tmcfg.DefaultConfig() - switch _, err := os.Stat(configFile); { + switch _, err := os.Stat(tmCfgFile); { case os.IsNotExist(err): tmcfg.EnsureRoot(rootDir) @@ -200,7 +200,7 @@ func interceptConfigs(rootViper *viper.Viper) (*tmcfg.Config, error) { conf.P2P.RecvRate = 5120000 conf.P2P.SendRate = 5120000 conf.Consensus.TimeoutCommit = 5 * time.Second - tmcfg.WriteConfigFile(configFile, conf) + tmcfg.WriteConfigFile(tmCfgFile, conf) case err != nil: return nil, err @@ -209,34 +209,37 @@ func interceptConfigs(rootViper *viper.Viper) (*tmcfg.Config, error) { rootViper.SetConfigType("toml") rootViper.SetConfigName("config") rootViper.AddConfigPath(configPath) + if err := rootViper.ReadInConfig(); err != nil { - return nil, fmt.Errorf("failed to read in config.toml: %w", err) + return nil, fmt.Errorf("failed to read in %s: %w", tmCfgFile, err) } } - // Read into the configuration whatever data the viper instance has for it - // This may come from the configuration file above but also any of the other sources - // viper uses + // Read into the configuration whatever data the viper instance has for it. + // This may come from the configuration file above but also any of the other + // sources viper uses. if err := rootViper.Unmarshal(conf); err != nil { return nil, err } + conf.SetRoot(rootDir) - appConfigFilePath := filepath.Join(configPath, "app.toml") - if _, err := os.Stat(appConfigFilePath); os.IsNotExist(err) { + appCfgFilePath := filepath.Join(configPath, "app.toml") + if _, err := os.Stat(appCfgFilePath); os.IsNotExist(err) { appConf, err := config.ParseConfig(rootViper) if err != nil { - return nil, fmt.Errorf("failed to parse app.toml: %w", err) + return nil, fmt.Errorf("failed to parse %s: %w", appCfgFilePath, err) } - config.WriteConfigFile(appConfigFilePath, appConf) + config.WriteConfigFile(appCfgFilePath, appConf) } rootViper.SetConfigType("toml") rootViper.SetConfigName("app") rootViper.AddConfigPath(configPath) - if err := rootViper.ReadInConfig(); err != nil { - return nil, fmt.Errorf("failed to read in app.toml: %w", err) + + if err := rootViper.MergeInConfig(); err != nil { + return nil, fmt.Errorf("failed to merge configuration: %w", err) } return conf, nil diff --git a/server/util_test.go b/server/util_test.go index a0ff6479b82d..0800b59a30c1 100644 --- a/server/util_test.go +++ b/server/util_test.go @@ -162,76 +162,6 @@ func TestInterceptConfigsPreRunHandlerReadsAppToml(t *testing.T) { } } -func TestInterceptConfigsPreRunHandlerDoesNotMixConfigFiles(t *testing.T) { - // The goal of this test is to make sure that app.toml and config.toml - // are separate files and that mixing values does not work - const testDbBackend = "awesome_test_db" - const testHaltTime = 1337 - const testHaltHeight = 2001 - - tempDir := t.TempDir() - err := os.Mkdir(path.Join(tempDir, "config"), os.ModePerm) - if err != nil { - t.Fatalf("creating config dir failed: %v", err) - } - configTomlPath := path.Join(tempDir, "config", "config.toml") - writer, err := os.Create(configTomlPath) - if err != nil { - t.Fatalf("creating config.toml file failed: %v", err) - } - - // Put a value in config.toml that should be in app.toml - _, err = writer.WriteString(fmt.Sprintf("halt-time = %d\ndb_backend = \"%s\"\n", testHaltTime, testDbBackend)) - if err != nil { - t.Fatalf("Failed writing string to config.toml: %v", err) - } - - if err := writer.Close(); err != nil { - t.Fatalf("Failed closing config.toml: %v", err) - } - - appTomlPath := path.Join(tempDir, "config", "app.toml") - writer, err = os.Create(appTomlPath) - if err != nil { - t.Fatalf("creating app.toml file failed %v", err) - } - - // Put a different value in app.toml - _, err = writer.WriteString(fmt.Sprintf("halt-height = %d\n", testHaltHeight)) - if err != nil { - t.Fatalf("Failed writing string to app.toml: %v", err) - } - - if err := writer.Close(); err != nil { - t.Fatalf("Failed closing app.toml: %v", err) - } - - cmd := StartCmd(nil, tempDir) - cmd.PreRunE = preRunETestImpl - - serverCtx := &Context{} - ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx) - - if err := cmd.ExecuteContext(ctx); err != CancelledInPreRun { - t.Fatalf("function failed with [%T] %v", err, err) - } - - // check that the intended value from config.toml is used - if testDbBackend != serverCtx.Config.DBBackend { - t.Error("DBPath was not set from config.toml") - } - - // The value from app.toml should be used for this - if testHaltHeight != serverCtx.Viper.GetInt("halt-height") { - t.Error("Halt height is not using provided value") - } - - // The value from config.toml should not be used, default is used instead - if 0 != serverCtx.Viper.GetInt("halt-time") { - t.Error("Halt time is not using default") - } -} - func TestInterceptConfigsPreRunHandlerReadsFlags(t *testing.T) { const testAddr = "tcp://127.1.2.3:12345" tempDir := t.TempDir()