From 038f7d509a6a83fd017c2da1261ef2a2970864c3 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 19 Feb 2021 09:00:31 -0500 Subject: [PATCH 1/6] server: fix interceptConfigs --- server/util.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/server/util.go b/server/util.go index b431b715db27..839ce0b1a493 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 app.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) } - 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) + rootViper.AddConfigPath(appCfgFilePath) + + if err := rootViper.MergeInConfig(); err != nil { + return nil, fmt.Errorf("failed to read in %s: %w", appCfgFilePath, err) } return conf, nil From 5424b54278a8788cfb4ea3ff68806234f941cd09 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 19 Feb 2021 09:04:11 -0500 Subject: [PATCH 2/6] cl++ --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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 From f3e1f46303e9a3f2b47a1d3472bfffb223aaa4da Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 19 Feb 2021 09:10:08 -0500 Subject: [PATCH 3/6] revert call --- server/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/util.go b/server/util.go index 839ce0b1a493..e58dc8ba3df0 100644 --- a/server/util.go +++ b/server/util.go @@ -236,7 +236,7 @@ func interceptConfigs(rootViper *viper.Viper) (*tmcfg.Config, error) { rootViper.SetConfigType("toml") rootViper.SetConfigName("app") - rootViper.AddConfigPath(appCfgFilePath) + rootViper.AddConfigPath(configPath) if err := rootViper.MergeInConfig(); err != nil { return nil, fmt.Errorf("failed to read in %s: %w", appCfgFilePath, err) From 909cb4b148889ad71f6c70f3c1fcbec81c6f3015 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 19 Feb 2021 09:12:01 -0500 Subject: [PATCH 4/6] update error message --- server/util.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/util.go b/server/util.go index e58dc8ba3df0..68f1c4828166 100644 --- a/server/util.go +++ b/server/util.go @@ -228,7 +228,7 @@ func interceptConfigs(rootViper *viper.Viper) (*tmcfg.Config, error) { 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(appCfgFilePath, appConf) @@ -239,7 +239,7 @@ func interceptConfigs(rootViper *viper.Viper) (*tmcfg.Config, error) { rootViper.AddConfigPath(configPath) if err := rootViper.MergeInConfig(); err != nil { - return nil, fmt.Errorf("failed to read in %s: %w", appCfgFilePath, err) + return nil, fmt.Errorf("failed to merge configuration: %w", err) } return conf, nil From 3e093236b51f1fffb4ce2e598ef19d24fabb1bb4 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 19 Feb 2021 09:48:17 -0500 Subject: [PATCH 5/6] revise TestInterceptConfigsPreRunHandlerDoesNotMixConfigFiles --- server/util_test.go | 58 ++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/server/util_test.go b/server/util_test.go index a0ff6479b82d..cc23b90e0aab 100644 --- a/server/util_test.go +++ b/server/util_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/spf13/cobra" + "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client/flags" ) @@ -162,49 +163,36 @@ func TestInterceptConfigsPreRunHandlerReadsAppToml(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 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 + testDbBackend := "awesome_test_db" + testHaltTime := 1337 + 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) - } + require.NoError(t, 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) - } + require.NoError(t, 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) - } + require.NoError(t, err) - if err := writer.Close(); err != nil { - t.Fatalf("Failed closing config.toml: %v", err) - } + require.NoError(t, writer.Close()) appTomlPath := path.Join(tempDir, "config", "app.toml") writer, err = os.Create(appTomlPath) - if err != nil { - t.Fatalf("creating app.toml file failed %v", err) - } + require.NoError(t, 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) - } + require.NoError(t, err) - if err := writer.Close(); err != nil { - t.Fatalf("Failed closing app.toml: %v", err) - } + require.NoError(t, writer.Close()) cmd := StartCmd(nil, tempDir) cmd.PreRunE = preRunETestImpl @@ -212,24 +200,18 @@ func TestInterceptConfigsPreRunHandlerDoesNotMixConfigFiles(t *testing.T) { 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) - } + require.Equal(t, CancelledInPreRun, cmd.ExecuteContext(ctx)) // check that the intended value from config.toml is used - if testDbBackend != serverCtx.Config.DBBackend { - t.Error("DBPath was not set from config.toml") - } + require.Equal(t, testDbBackend, serverCtx.Config.DBBackend, "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") - } + require.Equal(t, testHaltHeight, serverCtx.Viper.GetInt("halt-height"), "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") - } + // if 0 != serverCtx.Viper.GetInt("halt-time") { + // t.Error("Halt time is not using default") + // } } func TestInterceptConfigsPreRunHandlerReadsFlags(t *testing.T) { From 3cefba8df54e9029ce2fe46f678dc6373e3ee5be Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 19 Feb 2021 09:51:30 -0500 Subject: [PATCH 6/6] remove TestInterceptConfigsPreRunHandlerDoesNotMixConfigFiles --- server/util_test.go | 52 --------------------------------------------- 1 file changed, 52 deletions(-) diff --git a/server/util_test.go b/server/util_test.go index cc23b90e0aab..0800b59a30c1 100644 --- a/server/util_test.go +++ b/server/util_test.go @@ -11,7 +11,6 @@ import ( "testing" "github.com/spf13/cobra" - "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client/flags" ) @@ -163,57 +162,6 @@ func TestInterceptConfigsPreRunHandlerReadsAppToml(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 -func TestInterceptConfigsPreRunHandlerDoesNotMixConfigFiles(t *testing.T) { - testDbBackend := "awesome_test_db" - testHaltTime := 1337 - testHaltHeight := 2001 - - tempDir := t.TempDir() - err := os.Mkdir(path.Join(tempDir, "config"), os.ModePerm) - require.NoError(t, err) - - configTomlPath := path.Join(tempDir, "config", "config.toml") - writer, err := os.Create(configTomlPath) - require.NoError(t, 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)) - require.NoError(t, err) - - require.NoError(t, writer.Close()) - - appTomlPath := path.Join(tempDir, "config", "app.toml") - writer, err = os.Create(appTomlPath) - require.NoError(t, err) - - // Put a different value in app.toml - _, err = writer.WriteString(fmt.Sprintf("halt-height = %d\n", testHaltHeight)) - require.NoError(t, err) - - require.NoError(t, writer.Close()) - - cmd := StartCmd(nil, tempDir) - cmd.PreRunE = preRunETestImpl - - serverCtx := &Context{} - ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx) - - require.Equal(t, CancelledInPreRun, cmd.ExecuteContext(ctx)) - - // check that the intended value from config.toml is used - require.Equal(t, testDbBackend, serverCtx.Config.DBBackend, "DBPath was not set from config.toml") - - // The value from app.toml should be used for this - require.Equal(t, testHaltHeight, serverCtx.Viper.GetInt("halt-height"), "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()