Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Integrate grpc configuration into client.toml #19905

Merged
merged 7 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
54 changes: 48 additions & 6 deletions client/config/config.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -26,12 +31,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:",squash"`
}

// GRPCConfig holds the gRPC client configuration.
type GRPCConfig struct {
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
Expand Down Expand Up @@ -138,5 +150,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
}
21 changes: 20 additions & 1 deletion client/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ 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() {
Expand Down Expand Up @@ -181,3 +180,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"))
})
}
12 changes: 11 additions & 1 deletion client/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"github.com/spf13/viper"
)

const DefaultClientConfigTemplate = `# This is a TOML config file.
const (
DefaultClientConfigTemplate = `# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml

###############################################################################
Expand All @@ -27,7 +28,16 @@ output = "{{ .Output }}"
node = "{{ .Node }}"
# Transaction broadcasting mode (sync|async)
broadcast-mode = "{{ .BroadcastMode }}"

# 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 }}"

# Allow the gRPC client to connect over insecure channels.
# It can be overwritten by the --grpc-insecure flag in each command.
grpc-insecure = {{ .GRPC.Insecure }}
`
)

var configTemplate *template.Template

Expand Down
Loading