fix(client,server): consistently set env prefix between client/server (#18345)
This commit is contained in:
parent
c664daaf55
commit
9e91c7b419
@ -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.
|
||||
|
||||
@ -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)
|
||||
}()
|
||||
|
||||
/*
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user