Skip to content

Commit

Permalink
fix(client,server): consistently set env prefix between client/server (
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Nov 3, 2023
1 parent c664daa commit 9e91c7b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 5 additions & 8 deletions client/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

const (
chainID = "test-chain"
nodeEnv = "NODE"
nodeEnv = "CONFIG_TEST_NODE"
testNode1 = "http://localhost:1"
testNode2 = "http://localhost:2"
)
Expand All @@ -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) {
Expand Down Expand Up @@ -90,7 +92,6 @@ note = "{{ .Note }}"
clientCtx, cleanup, err := initClientContextWithTemplate(t, "", customClientConfigTemplate, customClientConfig)
defer func() {
cleanup()
_ = os.Unsetenv(nodeEnv)
}()

require.NoError(t, err)
Expand All @@ -103,7 +104,6 @@ note = "{{ .Note }}"
_, cleanup, err := initClientContextWithTemplate(t, "", "", customClientConfig)
defer func() {
cleanup()
_ = os.Unsetenv(nodeEnv)
}()

require.Error(t, err)
Expand All @@ -113,7 +113,6 @@ note = "{{ .Note }}"
clientCtx, cleanup, err := initClientContextWithTemplate(t, "", config.DefaultClientConfigTemplate, customClientConfig)
defer func() {
cleanup()
_ = os.Unsetenv(nodeEnv)
}()

require.NoError(t, err)
Expand All @@ -125,7 +124,6 @@ note = "{{ .Note }}"
clientCtx, cleanup, err := initClientContextWithTemplate(t, "", "", nil)
defer func() {
cleanup()
_ = os.Unsetenv(nodeEnv)
}()

require.NoError(t, err)
Expand Down Expand Up @@ -166,7 +164,6 @@ func TestConfigCmdEnvFlag(t *testing.T) {
clientCtx, cleanup := initClientContext(t, tc.envVar)
defer func() {
cleanup()
_ = os.Unsetenv(nodeEnv)
}()

/*
Expand Down
9 changes: 9 additions & 0 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"io"
"os"
"path"
"strings"

"github.com/cosmos/gogoproto/proto"
"github.com/spf13/viper"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions client/tx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/cosmos/go-bip39"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"cosmossdk.io/math"

Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 3 additions & 10 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/root_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9e91c7b

Please sign in to comment.