From f885b5d6f57c70af2fed71630c5e8e73d4cb7b47 Mon Sep 17 00:00:00 2001 From: Andrei Ivasko Date: Wed, 24 Mar 2021 11:37:26 -0700 Subject: [PATCH] refactored,ready for review --- client/cmd.go | 15 +++++++++++++++ client/config/cmd.go | 4 ---- client/config/config.go | 16 ++++++---------- client/config/toml.go | 3 +-- client/context.go | 21 ++------------------- client/utils.go | 1 + simapp/simd/cmd/root.go | 6 +++--- 7 files changed, 28 insertions(+), 38 deletions(-) diff --git a/client/cmd.go b/client/cmd.go index 0e401a9250f6..8685fefb0ac5 100644 --- a/client/cmd.go +++ b/client/cmd.go @@ -254,6 +254,21 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err return clientCtx, nil } +// ReadHomeFlag checks if home flag is changed. +// If this is a case, we update HomeDir field of Client Context +/* Discovered a bug with Cory +./build/simd init andrei --home ./test +cd test/config there is no client.toml configuration file +*/ +func ReadHomeFlag(clientCtx Context, cmd *cobra.Command) Context { + if cmd.Flags().Changed(flags.FlagHome) { + rootDir, _ := cmd.Flags().GetString(flags.FlagHome) + clientCtx = clientCtx.WithHomeDir(rootDir) + } + + return clientCtx +} + // GetClientQueryContext returns a Context from a command with fields set based on flags // defined in AddQueryFlagsToCmd. An error is returned if any flag query fails. // diff --git a/client/config/cmd.go b/client/config/cmd.go index dd6c224ff50e..523939f39ed0 100644 --- a/client/config/cmd.go +++ b/client/config/cmd.go @@ -47,8 +47,6 @@ func runConfigCmd(cmd *cobra.Command, args []string) error { switch key { case flags.FlagChainID: cmd.Println(conf.ChainID) - case flags.FlagKeyringDir: - cmd.Println(conf.KeyringDir) case flags.FlagKeyringBackend: cmd.Println(conf.KeyringBackend) case tmcli.OutputFlag: @@ -69,8 +67,6 @@ func runConfigCmd(cmd *cobra.Command, args []string) error { switch key { case flags.FlagChainID: conf.SetChainID(value) - case flags.FlagKeyringDir: - conf.SetKeyringDir(value) case flags.FlagKeyringBackend: conf.SetKeyringBackend(value) case tmcli.OutputFlag: diff --git a/client/config/config.go b/client/config/config.go index cdb3a0b8c779..a7254d759726 100644 --- a/client/config/config.go +++ b/client/config/config.go @@ -19,26 +19,21 @@ const ( type ClientConfig struct { ChainID string `mapstructure:"chain-id" json:"chain-id"` - KeyringDir string `mapstructure:"keyringdir" json:"keyringdir"` KeyringBackend string `mapstructure:"keyring-backend" json:"keyring-backend"` Output string `mapstructure:"output" json:"output"` Node string `mapstructure:"node" json:"node"` BroadcastMode string `mapstructure:"broadcast-mode" json:"broadcast-mode"` } -// DefaultClientConfig returns the reference to ClientConfig with default values. -func DefaultClientConfig(keyringDir string) *ClientConfig { - return &ClientConfig{chainID, keyringDir, keyringBackend, output, node, broadcastMode} +// defaultClientConfig returns the reference to ClientConfig with default values. +func defaultClientConfig() *ClientConfig { + return &ClientConfig{chainID, keyringBackend, output, node, broadcastMode} } func (c *ClientConfig) SetChainID(chainID string) { c.ChainID = chainID } -func (c *ClientConfig) SetKeyringDir(keyringDir string) { - c.KeyringDir = keyringDir -} - func (c *ClientConfig) SetKeyringBackend(keyringBackend string) { c.KeyringBackend = keyringBackend } @@ -59,7 +54,7 @@ func (c *ClientConfig) SetBroadcastMode(broadcastMode string) { func ReadFromClientConfig(ctx client.Context) (client.Context, error) { configPath := filepath.Join(ctx.HomeDir, "config") configFilePath := filepath.Join(configPath, "client.toml") - conf := DefaultClientConfig(ctx.HomeDir) + conf := defaultClientConfig() // if config.toml file does not exist we create it and write default ClientConfig values into it. if _, err := os.Stat(configFilePath); os.IsNotExist(err) { @@ -78,7 +73,7 @@ func ReadFromClientConfig(ctx client.Context) (client.Context, error) { } // we need to update KeyringDir field on Client Context first cause it is used in NewKeyringFromBackend ctx = ctx.WithOutputFormat(conf.Output). - WithKeyringDir(conf.KeyringDir). + WithKeyringDir(ctx.HomeDir). WithChainID(conf.ChainID) keyring, err := client.NewKeyringFromBackend(ctx, conf.KeyringBackend) @@ -88,6 +83,7 @@ func ReadFromClientConfig(ctx client.Context) (client.Context, error) { ctx = ctx.WithKeyring(keyring) + // https://github.com/cosmos/cosmos-sdk/issues/8986 client, err := client.NewClientFromNode(conf.Node) if err != nil { return ctx, fmt.Errorf("couldn't get client from nodeURI: %v", err) diff --git a/client/config/toml.go b/client/config/toml.go index 5e0d2f1bb8b6..35ea2d88c5a7 100644 --- a/client/config/toml.go +++ b/client/config/toml.go @@ -18,7 +18,6 @@ const defaultConfigTemplate = `# This is a TOML config file. chain-id = "{{ .ChainID }}" -keyringdir = "{{ .KeyringDir }}" keyring-backend = "{{ .KeyringBackend }}" output = "{{ .Output }}" node = "{{ .Node }}" @@ -40,7 +39,7 @@ func writeConfigToFile(configFilePath string, config *ClientConfig) error { return err } - return ioutil.WriteFile(configFilePath, buffer.Bytes(), 0644) + return ioutil.WriteFile(configFilePath, buffer.Bytes(), 0600) } // ensureConfigPath creates a directory configPath if it does not exist diff --git a/client/context.go b/client/context.go index b809db4b072e..74a5f682d3b4 100644 --- a/client/context.go +++ b/client/context.go @@ -5,7 +5,6 @@ import ( "io" "os" - "github.com/spf13/cobra" "github.com/spf13/viper" "gopkg.in/yaml.v2" @@ -14,7 +13,6 @@ import ( "github.com/pkg/errors" rpcclient "github.com/tendermint/tendermint/rpc/client" - "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -227,29 +225,14 @@ func (ctx Context) WithInterfaceRegistry(interfaceRegistry codectypes.InterfaceR return ctx } -// WithViper returns the context with Viper field +// WithViper returns the context with Viper field. This Viper instance is used to read +// client-side config from the config file. func (ctx Context) WithViper() Context { v := viper.New() ctx.Viper = v return ctx } -// WithHomeFlag checks if home flag is changed. -// If this is a case, we update HomeDir field of Client Context -/* Discovered a bug with Cory -./build/simd init andrei --home ./test -cd test/config there is no client.toml configuration file -*/ - -func (ctx Context) WithHomeFlag(cmd *cobra.Command) Context { - if cmd.Flags().Changed(flags.FlagHome) { - rootDir, _ := cmd.Flags().GetString(flags.FlagHome) - ctx = ctx.WithHomeDir(rootDir) - } - - return ctx -} - // PrintString prints the raw string to ctx.Output if it's defined, otherwise to os.Stdout func (ctx Context) PrintString(str string) error { return ctx.PrintBytes([]byte(str)) diff --git a/client/utils.go b/client/utils.go index be6836b8aaa6..03d4b55d83d0 100644 --- a/client/utils.go +++ b/client/utils.go @@ -74,6 +74,7 @@ func ReadPageRequest(flagSet *pflag.FlagSet) (*query.PageRequest, error) { // NewClientFromNode sets up Client implementation that communicates with a Tendermint node over // JSON RPC and WebSockets +// https://github.com/cosmos/cosmos-sdk/issues/8986 func NewClientFromNode(nodeURI string) (*rpchttp.HTTP, error) { return rpchttp.New(nodeURI, "/websocket") } diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index eb6394ad95f3..11add6f1c584 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -54,7 +54,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { Use: "simd", Short: "simulation app", PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { - initClientCtx = initClientCtx.WithHomeFlag(cmd) + initClientCtx = client.ReadHomeFlag(initClientCtx, cmd) initClientCtx, err := config.ReadFromClientConfig(initClientCtx) if err != nil { @@ -116,7 +116,7 @@ func queryCommand() *cobra.Command { Short: "Querying subcommands", DisableFlagParsing: true, SuggestionsMinimumDistance: 2, - RunE: client.ValidateCmd, + RunE: client.ValidateCmd, } cmd.AddCommand( @@ -139,7 +139,7 @@ func txCommand() *cobra.Command { Short: "Transactions subcommands", DisableFlagParsing: true, SuggestionsMinimumDistance: 2, - RunE: client.ValidateCmd, + RunE: client.ValidateCmd, } cmd.AddCommand(