diff --git a/CHANGELOG.md b/CHANGELOG.md index 2131b19ca59a..c5b11ceb2d32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (client/server) [#18345](https://github.com/cosmos/cosmos-sdk/pull/18345) Consistently set viper prefix in client and server. It defaults for the binary name for both client and server. * (server) [#18254](https://github.com/cosmos/cosmos-sdk/pull/18254) Don't hardcode gRPC address to localhost. * (x/slashing) [#18016](https://github.com/cosmos/cosmos-sdk/pull/18016) Fixed builder function for missed blocks key (`validatorMissedBlockBitArrayPrefixKey`) in slashing/migration/v4 * (x/gov) [#17873](https://github.com/cosmos/cosmos-sdk/pull/17873) Fail any inactive and active proposals whose messages cannot be decoded. diff --git a/client/config/config_test.go b/client/config/config_test.go index d4cd84aaa974..7eea0a791518 100644 --- a/client/config/config_test.go +++ b/client/config/config_test.go @@ -18,7 +18,7 @@ import ( const ( chainID = "test-chain" - nodeEnv = "NODE" + nodeEnv = "CONFIG_TEST_NODE" testNode1 = "http://localhost:1" testNode2 = "http://localhost:2" ) @@ -44,13 +44,15 @@ func initClientContextWithTemplate(t *testing.T, envVar, customTemplate string, WithCodec(codec.NewProtoCodec(codectypes.NewInterfaceRegistry())). WithChainID(chainID) - require.NoError(t, clientCtx.Viper.BindEnv(nodeEnv)) if envVar != "" { require.NoError(t, os.Setenv(nodeEnv, envVar)) } clientCtx, err := config.CreateClientConfig(clientCtx, customTemplate, customConfig) - return clientCtx, func() { _ = os.RemoveAll(home) }, err + return clientCtx, func() { + _ = os.RemoveAll(home) + _ = os.Unsetenv(nodeEnv) + }, err } func TestCustomTemplateAndConfig(t *testing.T) { @@ -90,7 +92,6 @@ note = "{{ .Note }}" clientCtx, cleanup, err := initClientContextWithTemplate(t, "", customClientConfigTemplate, customClientConfig) defer func() { cleanup() - _ = os.Unsetenv(nodeEnv) }() require.NoError(t, err) @@ -103,7 +104,6 @@ note = "{{ .Note }}" _, cleanup, err := initClientContextWithTemplate(t, "", "", customClientConfig) defer func() { cleanup() - _ = os.Unsetenv(nodeEnv) }() require.Error(t, err) @@ -113,7 +113,6 @@ note = "{{ .Note }}" clientCtx, cleanup, err := initClientContextWithTemplate(t, "", config.DefaultClientConfigTemplate, customClientConfig) defer func() { cleanup() - _ = os.Unsetenv(nodeEnv) }() require.NoError(t, err) @@ -125,7 +124,6 @@ note = "{{ .Note }}" clientCtx, cleanup, err := initClientContextWithTemplate(t, "", "", nil) defer func() { cleanup() - _ = os.Unsetenv(nodeEnv) }() require.NoError(t, err) @@ -166,7 +164,6 @@ func TestConfigCmdEnvFlag(t *testing.T) { clientCtx, cleanup := initClientContext(t, tc.envVar) defer func() { cleanup() - _ = os.Unsetenv(nodeEnv) }() /* diff --git a/client/context.go b/client/context.go index d75ea6771b24..cafc180dc20b 100644 --- a/client/context.go +++ b/client/context.go @@ -7,6 +7,8 @@ import ( "fmt" "io" "os" + "path" + "strings" "github.com/cosmos/gogoproto/proto" "github.com/spf13/viper" @@ -280,7 +282,14 @@ func (ctx Context) WithInterfaceRegistry(interfaceRegistry codectypes.InterfaceR // client-side config from the config file. func (ctx Context) WithViper(prefix string) Context { v := viper.New() + + if prefix == "" { + executableName, _ := os.Executable() + prefix = path.Base(executableName) + } + v.SetEnvPrefix(prefix) + v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")) v.AutomaticEnv() ctx.Viper = v return ctx diff --git a/client/tx/factory.go b/client/tx/factory.go index cb02c95d570b..02f6a41e159d 100644 --- a/client/tx/factory.go +++ b/client/tx/factory.go @@ -8,7 +8,6 @@ import ( "github.com/cosmos/go-bip39" "github.com/spf13/pflag" - "github.com/spf13/viper" "cosmossdk.io/math" @@ -50,7 +49,7 @@ type Factory struct { // NewFactoryCLI creates a new Factory. func NewFactoryCLI(clientCtx client.Context, flagSet *pflag.FlagSet) (Factory, error) { if clientCtx.Viper == nil { - clientCtx.Viper = viper.New() + clientCtx = clientCtx.WithViper("") } if err := clientCtx.Viper.BindPFlags(flagSet); err != nil { diff --git a/server/start.go b/server/start.go index 4195b0543cbd..60a461b8516e 100644 --- a/server/start.go +++ b/server/start.go @@ -147,20 +147,13 @@ API services are enabled via the 'grpc-only' flag. In this mode, CometBFT is bypassed and can be used when legacy queries are needed after an on-chain upgrade is performed. Note, when enabled, gRPC will also be automatically enabled. `, - PreRunE: func(cmd *cobra.Command, _ []string) error { + RunE: func(cmd *cobra.Command, _ []string) error { serverCtx := GetServerContextFromCmd(cmd) - - // Bind flags to the Context's Viper so the app construction can set - // options accordingly. - if err := serverCtx.Viper.BindPFlags(cmd.Flags()); err != nil { + _, err := GetPruningOptionsFromFlags(serverCtx.Viper) + if err != nil { return err } - _, err := GetPruningOptionsFromFlags(serverCtx.Viper) - return err - }, - RunE: func(cmd *cobra.Command, _ []string) error { - serverCtx := GetServerContextFromCmd(cmd) clientCtx, err := client.GetClientQueryContext(cmd) if err != nil { return err diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index c1a2654579da..a82e39b97fdb 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -49,7 +49,7 @@ func NewRootCmd() *cobra.Command { WithValidatorAddressCodec(addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix())). WithConsensusAddressCodec(addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix())). WithHomeDir(simapp.DefaultNodeHome). - WithViper("") // In simapp, we don't use any prefix for env variables. + WithViper("") // uses by default the binary name as prefix rootCmd := &cobra.Command{ Use: "simd", diff --git a/simapp/simd/cmd/root_v2.go b/simapp/simd/cmd/root_v2.go index 47113624b1cf..455c0843c933 100644 --- a/simapp/simd/cmd/root_v2.go +++ b/simapp/simd/cmd/root_v2.go @@ -116,7 +116,7 @@ func ProvideClientContext( WithValidatorAddressCodec(validatorAddressCodec). WithConsensusAddressCodec(consensusAddressCodec). WithHomeDir(simapp.DefaultNodeHome). - WithViper("") // In simapp, we don't use any prefix for env variables. + WithViper("") // uses by default the binary name as prefix // Read the config to overwrite the default values with the values from the config file customClientTemplate, customClientConfig := initClientConfig()