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
…#18345)

(cherry picked from commit 9e91c7b)

# Conflicts:
#	client/config/config_test.go
#	client/tx/factory.go
  • Loading branch information
julienrbrt authored and mergify[bot] committed Nov 3, 2023
1 parent f6d4b2e commit 8518cb4
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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/gov) [#18173](https://github.com/cosmos/cosmos-sdk/pull/18173) Gov hooks now return an error and are *blocking* when they fail. Expect for `AfterProposalFailedMinDeposit` and `AfterProposalVotingPeriodEnded` which log the error and continue.
* (x/gov) [#17873](https://github.com/cosmos/cosmos-sdk/pull/17873) Fail any inactive and active proposals that cannot be decoded.
Expand Down
92 changes: 90 additions & 2 deletions client/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import (
)

const (
<<<<<<< HEAD

Check failure on line 20 in client/config/config_test.go

View workflow job for this annotation

GitHub Actions / tests (00)

expected 'IDENT', found '<<'
nodeEnv = "NODE"
=======
chainID = "test-chain"
nodeEnv = "CONFIG_TEST_NODE"
>>>>>>> 9e91c7b41 (fix(client,server): consistently set env prefix between client/server (#18345))
testNode1 = "http://localhost:1"
testNode2 = "http://localhost:2"
)
Expand All @@ -32,16 +37,100 @@ func initClientContext(t *testing.T, envVar string) (client.Context, func()) {
WithCodec(codec.NewProtoCodec(codectypes.NewInterfaceRegistry())).
WithChainID(chainID)

require.NoError(t, clientCtx.Viper.BindEnv(nodeEnv))
if envVar != "" {
require.NoError(t, os.Setenv(nodeEnv, envVar))
}

<<<<<<< HEAD
clientCtx, err := config.ReadFromClientConfig(clientCtx)
require.NoError(t, err)
require.Equal(t, clientCtx.ChainID, chainID)

return clientCtx, func() { _ = os.RemoveAll(home) }
=======
clientCtx, err := config.CreateClientConfig(clientCtx, customTemplate, customConfig)
return clientCtx, func() {
_ = os.RemoveAll(home)
_ = os.Unsetenv(nodeEnv)
}, err
}

func TestCustomTemplateAndConfig(t *testing.T) {
type GasConfig struct {
GasAdjustment float64 `mapstructure:"gas-adjustment"`
}

type CustomClientConfig struct {
config.Config `mapstructure:",squash"`

GasConfig GasConfig `mapstructure:"gas"`

Note string `mapstructure:"note"`
}

clientCfg := config.DefaultConfig()
// Overwrite the default keyring backend.
clientCfg.KeyringBackend = "test"

customClientConfig := CustomClientConfig{
Config: *clientCfg,
GasConfig: GasConfig{
GasAdjustment: 1.5,
},
Note: "Sent from the CLI.",
}

customClientConfigTemplate := config.DefaultClientConfigTemplate + `
# This is the gas adjustment factor used by the tx commands.
# Sets the default and can be overwriten by the --gas-adjustment flag in tx commands.
gas-adjustment = {{ .GasConfig.GasAdjustment }}
# Memo to include in all transactions.
note = "{{ .Note }}"
`

t.Run("custom template and config provided", func(t *testing.T) {
clientCtx, cleanup, err := initClientContextWithTemplate(t, "", customClientConfigTemplate, customClientConfig)
defer func() {
cleanup()
}()

require.NoError(t, err)
require.Equal(t, customClientConfig.KeyringBackend, clientCtx.Viper.Get(flags.FlagKeyringBackend))
require.Equal(t, customClientConfig.GasConfig.GasAdjustment, clientCtx.Viper.GetFloat64(flags.FlagGasAdjustment))
require.Equal(t, customClientConfig.Note, clientCtx.Viper.GetString(flags.FlagNote))
})

t.Run("no template and custom config provided", func(t *testing.T) {
_, cleanup, err := initClientContextWithTemplate(t, "", "", customClientConfig)
defer func() {
cleanup()
}()

require.Error(t, err)
})

t.Run("default template and custom config provided", func(t *testing.T) {
clientCtx, cleanup, err := initClientContextWithTemplate(t, "", config.DefaultClientConfigTemplate, customClientConfig)
defer func() {
cleanup()
}()

require.NoError(t, err)
require.Equal(t, customClientConfig.KeyringBackend, clientCtx.Viper.Get(flags.FlagKeyringBackend))
require.Nil(t, clientCtx.Viper.Get(flags.FlagGasAdjustment)) // nil because we do not read the flags
})

t.Run("no template and no config provided", func(t *testing.T) {
clientCtx, cleanup, err := initClientContextWithTemplate(t, "", "", nil)
defer func() {
cleanup()
}()

require.NoError(t, err)
require.Equal(t, config.DefaultConfig().KeyringBackend, clientCtx.Viper.Get(flags.FlagKeyringBackend))
require.Nil(t, clientCtx.Viper.Get(flags.FlagGasAdjustment)) // nil because we do not read the flags
})
>>>>>>> 9e91c7b41 (fix(client,server): consistently set env prefix between client/server (#18345))
}

func TestConfigCmdEnvFlag(t *testing.T) {
Expand Down Expand Up @@ -76,7 +165,6 @@ func TestConfigCmdEnvFlag(t *testing.T) {
clientCtx, cleanup := initClientContext(t, tc.envVar)
defer func() {
cleanup()
_ = os.Unsetenv(nodeEnv)
}()
/*
env var is set with a flag
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 @@ -266,7 +268,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
11 changes: 11 additions & 0 deletions client/tx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ type Factory struct {

// NewFactoryCLI creates a new Factory.
func NewFactoryCLI(clientCtx client.Context, flagSet *pflag.FlagSet) (Factory, error) {
<<<<<<< HEAD

Check failure on line 51 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (02)

syntax error: unexpected <<, expected }

Check failure on line 51 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected <<, expected }

Check failure on line 51 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected <<, expected }

Check failure on line 51 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected <<, expected }

Check failure on line 51 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected <<, expected }

Check failure on line 51 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected <<, expected }

Check failure on line 51 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected <<, expected }
=======
if clientCtx.Viper == nil {
clientCtx = clientCtx.WithViper("")
}

if err := clientCtx.Viper.BindPFlags(flagSet); err != nil {
return Factory{}, fmt.Errorf("failed to bind flags to viper: %w", err)
}

>>>>>>> 9e91c7b41 (fix(client,server): consistently set env prefix between client/server (#18345))

Check failure on line 61 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (02)

invalid character U+0023 '#'

Check failure on line 61 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (03)

invalid character U+0023 '#'

Check failure on line 61 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (01)

invalid character U+0023 '#'

Check failure on line 61 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (01)

invalid character U+0023 '#'

Check failure on line 61 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (00)

invalid character U+0023 '#'

Check failure on line 61 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (00)

invalid character U+0023 '#'

Check failure on line 61 in client/tx/factory.go

View workflow job for this annotation

GitHub Actions / tests (00)

invalid character U+0023 '#'
signMode := signing.SignMode_SIGN_MODE_UNSPECIFIED
switch clientCtx.SignModeStr {
case flags.SignModeDirect:
Expand Down
13 changes: 3 additions & 10 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,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 @@ -43,7 +43,7 @@ func NewRootCmd() *cobra.Command {
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
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 @@ -106,7 +106,7 @@ func ProvideClientContext(
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
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 again to overwrite the default values with the values from the config file
clientCtx, _ = config.ReadFromClientConfig(clientCtx)
Expand Down

0 comments on commit 8518cb4

Please sign in to comment.