From 5cdc72598250337e3b8d21f3d2bcb04cbe40c4cf Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Sat, 30 Mar 2024 00:55:33 +0100 Subject: [PATCH 1/6] Add grpc client config to client.toml --- client/config/config.go | 58 ++++++++++++++++++++++++++++++++---- client/config/config_test.go | 20 +++++++++++++ client/config/toml.go | 13 ++++++++ 3 files changed, 85 insertions(+), 6 deletions(-) diff --git a/client/config/config.go b/client/config/config.go index efe65bd34ec7..f9d9608fa340 100644 --- a/client/config/config.go +++ b/client/config/config.go @@ -1,10 +1,15 @@ package config import ( + "crypto/tls" "fmt" "os" "path/filepath" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials" + "google.golang.org/grpc/credentials/insecure" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" ) @@ -18,6 +23,10 @@ func DefaultConfig() *Config { Output: "text", Node: "tcp://localhost:26657", BroadcastMode: "sync", + GRPC: GRPCConfig{ + Address: "", + Insecure: false, + }, } } @@ -26,12 +35,19 @@ func DefaultConfig() *Config { type ClientConfig Config type Config struct { - ChainID string `mapstructure:"chain-id" json:"chain-id"` - KeyringBackend string `mapstructure:"keyring-backend" json:"keyring-backend"` - KeyringDefaultKeyName string `mapstructure:"keyring-default-keyname" json:"keyring-default-keyname"` - Output string `mapstructure:"output" json:"output"` - Node string `mapstructure:"node" json:"node"` - BroadcastMode string `mapstructure:"broadcast-mode" json:"broadcast-mode"` + ChainID string `mapstructure:"chain-id" json:"chain-id"` + KeyringBackend string `mapstructure:"keyring-backend" json:"keyring-backend"` + KeyringDefaultKeyName string `mapstructure:"keyring-default-keyname" json:"keyring-default-keyname"` + Output string `mapstructure:"output" json:"output"` + Node string `mapstructure:"node" json:"node"` + BroadcastMode string `mapstructure:"broadcast-mode" json:"broadcast-mode"` + GRPC GRPCConfig `mapstructure:"grpc" json:"grpc"` +} + +// GRPCConfig holds the gRPC client configuration. +type GRPCConfig struct { + Address string `mapstructure:"address" json:"address"` + Insecure bool `mapstructure:"insecure" json:"insecure"` } // ReadFromClientConfig reads values from client.toml file and updates them in client.Context @@ -138,5 +154,35 @@ func CreateClientConfig(ctx client.Context, customClientTemplate string, customC WithClient(client). WithKeyring(keyring) + if conf.GRPC.Address != "" { + grpcClient, err := getGRPCClient(conf.GRPC) + if err != nil { + return ctx, fmt.Errorf("couldn't get grpc client: %w", err) + } + + ctx = ctx.WithGRPCClient(grpcClient) + } + return ctx, nil } + +// getGRPCClient creates and returns a new gRPC client connection based on the GRPCConfig. +// It determines the type of connection (secure or insecure) from the GRPCConfig and +// uses the specified server address to establish the connection. +func getGRPCClient(grpcConfig GRPCConfig) (*grpc.ClientConn, error) { + transport := grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{ + MinVersion: tls.VersionTLS12, + })) + + if grpcConfig.Insecure { + transport = grpc.WithTransportCredentials(insecure.NewCredentials()) + } + + dialOptions := []grpc.DialOption{transport} + grpcClient, err := grpc.Dial(grpcConfig.Address, dialOptions...) + if err != nil { + return nil, fmt.Errorf("failed to dial gRPC server at %s: %w", grpcConfig.Address, err) + } + + return grpcClient, nil +} diff --git a/client/config/config_test.go b/client/config/config_test.go index ceb052e8acd5..76f8bd7eb171 100644 --- a/client/config/config_test.go +++ b/client/config/config_test.go @@ -181,3 +181,23 @@ func TestConfigCmdEnvFlag(t *testing.T) { }) } } + +func TestGRPCConfig(t *testing.T) { + expectedGRPCConfig := config.GRPCConfig{ + Address: "localhost:7070", + Insecure: true, + } + + clientCfg := config.DefaultConfig() + clientCfg.GRPC = expectedGRPCConfig + + t.Run("custom template with gRPC config", func(t *testing.T) { + clientCtx, cleanup, err := initClientContextWithTemplate(t, "", config.DefaultClientConfigTemplate, clientCfg) + defer cleanup() + + require.NoError(t, err) + + require.Equal(t, expectedGRPCConfig.Address, clientCtx.Viper.GetString("grpc.address")) + require.Equal(t, expectedGRPCConfig.Insecure, clientCtx.Viper.GetBool("grpc.insecure")) + }) +} diff --git a/client/config/toml.go b/client/config/toml.go index 7c43eccc15b8..21a09d4f8d65 100644 --- a/client/config/toml.go +++ b/client/config/toml.go @@ -27,6 +27,19 @@ output = "{{ .Output }}" node = "{{ .Node }}" # Transaction broadcasting mode (sync|async) broadcast-mode = "{{ .BroadcastMode }}" + +############################################################################### +### gRPC Configuration ### +############################################################################### + +[grpc] +# gRPC server endpoint to which the client will connect. +# It can be overwritten by the --grpc-addr flag in each command. +address = "{{ .GRPC.Address }}" + +# Allow the gRPC client to connect over insecure channels. +# It can be overwritten by the --grpc-insecure flag in each command. +insecure = {{ .GRPC.Insecure }} ` var configTemplate *template.Template From 34eff4fc6c0287d9f24eaa1273d820674c0069e6 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Sat, 30 Mar 2024 02:09:31 +0100 Subject: [PATCH 2/6] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d72b6cd7494..c1fa63e5c91b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i ### Features +* (client) [#19905](https://github.com/cosmos/cosmos-sdk/pull/19905) Add grpc client config to `client.toml`. * (runtime) [#19571](https://github.com/cosmos/cosmos-sdk/pull/19571) Implement `core/router.Service` it in runtime. This service is present in all modules (when using depinject). * (types) [#19164](https://github.com/cosmos/cosmos-sdk/pull/19164) Add a ValueCodec for the math.Uint type that can be used in collections maps. * (types) [#19281](https://github.com/cosmos/cosmos-sdk/pull/19281) Added a new method, `IsGT`, for `types.Coin`. This method is used to check if a `types.Coin` is greater than another `types.Coin`. From 8179af243c05732cacfd8f8ee7e3f3257377c976 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Sat, 30 Mar 2024 03:47:50 +0100 Subject: [PATCH 3/6] Refactor config template customization to enhance scalability --- client/config/config.go | 7 +++++++ client/config/config_test.go | 4 ++-- client/config/toml.go | 13 ++++++++++++- simapp/simd/cmd/config.go | 10 ++++++---- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/client/config/config.go b/client/config/config.go index f9d9608fa340..56c69914c2cb 100644 --- a/client/config/config.go +++ b/client/config/config.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -166,6 +167,12 @@ func CreateClientConfig(ctx client.Context, customClientTemplate string, customC return ctx, nil } +// CustomizeConfigTemplate inserts custom configuration settings into the default config template by replacing a predefined placeholder +// This approach prevents issues that could arise from direct concatenation, such as incorrect section categorization of custom settings. +func CustomizeConfigTemplate(customConfig string) string { + return strings.Replace(DefaultClientConfigTemplate, CustomConfigKey, customConfig, -1) +} + // getGRPCClient creates and returns a new gRPC client connection based on the GRPCConfig. // It determines the type of connection (secure or insecure) from the GRPCConfig and // uses the specified server address to establish the connection. diff --git a/client/config/config_test.go b/client/config/config_test.go index 76f8bd7eb171..bfe6b1d3e2d8 100644 --- a/client/config/config_test.go +++ b/client/config/config_test.go @@ -80,14 +80,14 @@ func TestCustomTemplateAndConfig(t *testing.T) { Note: "Sent from the CLI.", } - customClientConfigTemplate := config.DefaultClientConfigTemplate + ` + customConfig := ` # This is the gas adjustment factor used by the tx commands. # Sets the default and can be overwritten by the --gas-adjustment flag in tx commands. gas-adjustment = {{ .GasConfig.GasAdjustment }} # Memo to include in all transactions. note = "{{ .Note }}" ` - + customClientConfigTemplate := config.CustomizeConfigTemplate(customConfig) t.Run("custom template and config provided", func(t *testing.T) { clientCtx, cleanup, err := initClientContextWithTemplate(t, "", customClientConfigTemplate, customClientConfig) defer func() { diff --git a/client/config/toml.go b/client/config/toml.go index 21a09d4f8d65..fb4709a849b3 100644 --- a/client/config/toml.go +++ b/client/config/toml.go @@ -8,7 +8,9 @@ import ( "github.com/spf13/viper" ) -const DefaultClientConfigTemplate = `# This is a TOML config file. +const ( + CustomConfigKey = "#{CustomConfigs}" + DefaultClientConfigTemplate = `# This is a TOML config file. # For more information, see https://github.com/toml-lang/toml ############################################################################### @@ -28,6 +30,14 @@ node = "{{ .Node }}" # Transaction broadcasting mode (sync|async) broadcast-mode = "{{ .BroadcastMode }}" +############################################################################### +### custom config ### +############################################################################### + +### Custom Configurations +# The placeholder below is used for injecting custom configurations. +#{CustomConfigs} + ############################################################################### ### gRPC Configuration ### ############################################################################### @@ -41,6 +51,7 @@ address = "{{ .GRPC.Address }}" # It can be overwritten by the --grpc-insecure flag in each command. insecure = {{ .GRPC.Insecure }} ` +) var configTemplate *template.Template diff --git a/simapp/simd/cmd/config.go b/simapp/simd/cmd/config.go index cda73a922dd1..f065f6f44ae2 100644 --- a/simapp/simd/cmd/config.go +++ b/simapp/simd/cmd/config.go @@ -56,15 +56,17 @@ func initClientConfig() (string, interface{}) { }, } - // The default SDK app template is defined in serverconfig.DefaultConfigTemplate. - // We append the custom config template to the default one. - // And we set the default config to the custom app template. - customClientConfigTemplate := clientconfig.DefaultClientConfigTemplate + strings.TrimSpace(` + // Create a customConfig to define specific settings. + customConfig := strings.TrimSpace(` # This is default the gas adjustment factor used in tx commands. # It can be overwritten by the --gas-adjustment flag in each tx command. gas-adjustment = {{ .GasConfig.GasAdjustment }} `) + // The CustomizeConfigTemplate function is then employed to seamlessly integrate these custom settings + // into the default configuration template. + customClientConfigTemplate := clientconfig.CustomizeConfigTemplate(customConfig) + return customClientConfigTemplate, customClientConfig } From fde3134dadd2b538d20c32589719d073c06700ae Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Sat, 30 Mar 2024 04:07:20 +0100 Subject: [PATCH 4/6] update changelog --- CHANGELOG.md | 1 + client/config/config.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1fa63e5c91b..87eb415656f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i ### Improvements +* (client) [#19905](https://github.com/cosmos/cosmos-sdk/pull/19905) Enhanced configuration template customization mechanism to use a placeholder-based approach, ensuring proper categorization and scalability of custom settings within the configuration file. * (types) [#19869](https://github.com/cosmos/cosmos-sdk/pull/19869) Removed `Any` type from `codec/types` and replaced it with an alias for `cosmos/gogoproto/types/any`. * (server) [#19854](https://github.com/cosmos/cosmos-sdk/pull/19854) Add customizability to start command. * Add `StartCmdOptions` in `server.AddCommands` instead of `servertypes.ModuleInitFlags`. To set custom flags set them in the `StartCmdOptions` struct on the `AddFlags` field. diff --git a/client/config/config.go b/client/config/config.go index 56c69914c2cb..feee31afd088 100644 --- a/client/config/config.go +++ b/client/config/config.go @@ -170,7 +170,7 @@ func CreateClientConfig(ctx client.Context, customClientTemplate string, customC // CustomizeConfigTemplate inserts custom configuration settings into the default config template by replacing a predefined placeholder // This approach prevents issues that could arise from direct concatenation, such as incorrect section categorization of custom settings. func CustomizeConfigTemplate(customConfig string) string { - return strings.Replace(DefaultClientConfigTemplate, CustomConfigKey, customConfig, -1) + return strings.ReplaceAll(DefaultClientConfigTemplate, CustomConfigKey, customConfig) } // getGRPCClient creates and returns a new gRPC client connection based on the GRPCConfig. From 7da2294332c059d47e599ff95392df0f4736fc2d Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Sat, 30 Mar 2024 18:34:53 +0100 Subject: [PATCH 5/6] rollback last commit and minor changes --- CHANGELOG.md | 1 - client/config/config.go | 17 +++-------------- client/config/config_test.go | 7 +++---- client/config/toml.go | 14 ++------------ simapp/simd/cmd/config.go | 10 ++++------ 5 files changed, 12 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87eb415656f6..c1fa63e5c91b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,7 +60,6 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i ### Improvements -* (client) [#19905](https://github.com/cosmos/cosmos-sdk/pull/19905) Enhanced configuration template customization mechanism to use a placeholder-based approach, ensuring proper categorization and scalability of custom settings within the configuration file. * (types) [#19869](https://github.com/cosmos/cosmos-sdk/pull/19869) Removed `Any` type from `codec/types` and replaced it with an alias for `cosmos/gogoproto/types/any`. * (server) [#19854](https://github.com/cosmos/cosmos-sdk/pull/19854) Add customizability to start command. * Add `StartCmdOptions` in `server.AddCommands` instead of `servertypes.ModuleInitFlags`. To set custom flags set them in the `StartCmdOptions` struct on the `AddFlags` field. diff --git a/client/config/config.go b/client/config/config.go index feee31afd088..ae8cbb70584a 100644 --- a/client/config/config.go +++ b/client/config/config.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "path/filepath" - "strings" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -24,10 +23,6 @@ func DefaultConfig() *Config { Output: "text", Node: "tcp://localhost:26657", BroadcastMode: "sync", - GRPC: GRPCConfig{ - Address: "", - Insecure: false, - }, } } @@ -42,13 +37,13 @@ type Config struct { Output string `mapstructure:"output" json:"output"` Node string `mapstructure:"node" json:"node"` BroadcastMode string `mapstructure:"broadcast-mode" json:"broadcast-mode"` - GRPC GRPCConfig `mapstructure:"grpc" json:"grpc"` + GRPC GRPCConfig `mapstructure:",squash"` } // GRPCConfig holds the gRPC client configuration. type GRPCConfig struct { - Address string `mapstructure:"address" json:"address"` - Insecure bool `mapstructure:"insecure" json:"insecure"` + Address string `mapstructure:"grpc-address" json:"grpc-address"` + Insecure bool `mapstructure:"grpc-insecure" json:"grpc-insecure"` } // ReadFromClientConfig reads values from client.toml file and updates them in client.Context @@ -167,12 +162,6 @@ func CreateClientConfig(ctx client.Context, customClientTemplate string, customC return ctx, nil } -// CustomizeConfigTemplate inserts custom configuration settings into the default config template by replacing a predefined placeholder -// This approach prevents issues that could arise from direct concatenation, such as incorrect section categorization of custom settings. -func CustomizeConfigTemplate(customConfig string) string { - return strings.ReplaceAll(DefaultClientConfigTemplate, CustomConfigKey, customConfig) -} - // getGRPCClient creates and returns a new gRPC client connection based on the GRPCConfig. // It determines the type of connection (secure or insecure) from the GRPCConfig and // uses the specified server address to establish the connection. diff --git a/client/config/config_test.go b/client/config/config_test.go index bfe6b1d3e2d8..248ed47cea9c 100644 --- a/client/config/config_test.go +++ b/client/config/config_test.go @@ -80,14 +80,13 @@ func TestCustomTemplateAndConfig(t *testing.T) { Note: "Sent from the CLI.", } - customConfig := ` + customClientConfigTemplate := config.DefaultClientConfigTemplate + ` # This is the gas adjustment factor used by the tx commands. # Sets the default and can be overwritten by the --gas-adjustment flag in tx commands. gas-adjustment = {{ .GasConfig.GasAdjustment }} # Memo to include in all transactions. note = "{{ .Note }}" ` - customClientConfigTemplate := config.CustomizeConfigTemplate(customConfig) t.Run("custom template and config provided", func(t *testing.T) { clientCtx, cleanup, err := initClientContextWithTemplate(t, "", customClientConfigTemplate, customClientConfig) defer func() { @@ -197,7 +196,7 @@ func TestGRPCConfig(t *testing.T) { require.NoError(t, err) - require.Equal(t, expectedGRPCConfig.Address, clientCtx.Viper.GetString("grpc.address")) - require.Equal(t, expectedGRPCConfig.Insecure, clientCtx.Viper.GetBool("grpc.insecure")) + require.Equal(t, expectedGRPCConfig.Address, clientCtx.Viper.GetString("grpc-address")) + require.Equal(t, expectedGRPCConfig.Insecure, clientCtx.Viper.GetBool("grpc-insecure")) }) } diff --git a/client/config/toml.go b/client/config/toml.go index fb4709a849b3..f0e3f33ecfa6 100644 --- a/client/config/toml.go +++ b/client/config/toml.go @@ -9,7 +9,6 @@ import ( ) const ( - CustomConfigKey = "#{CustomConfigs}" DefaultClientConfigTemplate = `# This is a TOML config file. # For more information, see https://github.com/toml-lang/toml @@ -30,26 +29,17 @@ node = "{{ .Node }}" # Transaction broadcasting mode (sync|async) broadcast-mode = "{{ .BroadcastMode }}" -############################################################################### -### custom config ### -############################################################################### - -### Custom Configurations -# The placeholder below is used for injecting custom configurations. -#{CustomConfigs} - ############################################################################### ### gRPC Configuration ### ############################################################################### -[grpc] # gRPC server endpoint to which the client will connect. # It can be overwritten by the --grpc-addr flag in each command. -address = "{{ .GRPC.Address }}" +grpc-address = "{{ .GRPC.Address }}" # Allow the gRPC client to connect over insecure channels. # It can be overwritten by the --grpc-insecure flag in each command. -insecure = {{ .GRPC.Insecure }} +grpc-insecure = {{ .GRPC.Insecure }} ` ) diff --git a/simapp/simd/cmd/config.go b/simapp/simd/cmd/config.go index f065f6f44ae2..cda73a922dd1 100644 --- a/simapp/simd/cmd/config.go +++ b/simapp/simd/cmd/config.go @@ -56,17 +56,15 @@ func initClientConfig() (string, interface{}) { }, } - // Create a customConfig to define specific settings. - customConfig := strings.TrimSpace(` + // The default SDK app template is defined in serverconfig.DefaultConfigTemplate. + // We append the custom config template to the default one. + // And we set the default config to the custom app template. + customClientConfigTemplate := clientconfig.DefaultClientConfigTemplate + strings.TrimSpace(` # This is default the gas adjustment factor used in tx commands. # It can be overwritten by the --gas-adjustment flag in each tx command. gas-adjustment = {{ .GasConfig.GasAdjustment }} `) - // The CustomizeConfigTemplate function is then employed to seamlessly integrate these custom settings - // into the default configuration template. - customClientConfigTemplate := clientconfig.CustomizeConfigTemplate(customConfig) - return customClientConfigTemplate, customClientConfig } From 9ee9cbd1d7feb950d941b13086d552ae7fe2ca09 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Mon, 1 Apr 2024 10:57:09 +0200 Subject: [PATCH 6/6] remove unnecessary comment --- client/config/toml.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/client/config/toml.go b/client/config/toml.go index f0e3f33ecfa6..35787219a236 100644 --- a/client/config/toml.go +++ b/client/config/toml.go @@ -29,10 +29,6 @@ node = "{{ .Node }}" # Transaction broadcasting mode (sync|async) broadcast-mode = "{{ .BroadcastMode }}" -############################################################################### -### gRPC Configuration ### -############################################################################### - # gRPC server endpoint to which the client will connect. # It can be overwritten by the --grpc-addr flag in each command. grpc-address = "{{ .GRPC.Address }}"