feat!: support custom r/w gRPC options (#11889)
This commit is contained in:
parent
514dcb5cee
commit
e3e65acf0f
|
@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
|||
|
||||
### Features
|
||||
|
||||
* (gRPC) [#11889](https://github.com/cosmos/cosmos-sdk/pull/11889) Support custom read and write gRPC options in `app.toml`. See `max-recv-msg-size` and `max-send-msg-size` respectively.
|
||||
* (cli) [\#11738](https://github.com/cosmos/cosmos-sdk/pull/11738) Add `tx auth multi-sign` as alias of `tx auth multisign` for consistency with `multi-send`.
|
||||
* (cli) [\#11738](https://github.com/cosmos/cosmos-sdk/pull/11738) Add `tx bank multi-send` command for bulk send of coins to multiple accounts.
|
||||
* (grpc) [\#11642](https://github.com/cosmos/cosmos-sdk/pull/11642) Implement `ABCIQuery` in the Tendermint gRPC service, which proxies ABCI `Query` requests directly to the application.
|
||||
|
|
|
@ -2,6 +2,7 @@ package config
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
@ -24,6 +25,14 @@ const (
|
|||
|
||||
// DefaultGRPCWebAddress defines the default address to bind the gRPC-web server to.
|
||||
DefaultGRPCWebAddress = "0.0.0.0:9091"
|
||||
|
||||
// DefaultGRPCMaxRecvMsgSize defines the default gRPC max message size in
|
||||
// bytes the server can receive.
|
||||
DefaultGRPCMaxRecvMsgSize = 1024 * 1024 * 10
|
||||
|
||||
// DefaultGRPCMaxSendMsgSize defines the default gRPC max message size in
|
||||
// bytes the server can send.
|
||||
DefaultGRPCMaxSendMsgSize = math.MaxInt32
|
||||
)
|
||||
|
||||
// BaseConfig defines the server's basic configuration
|
||||
|
@ -150,6 +159,14 @@ type GRPCConfig struct {
|
|||
|
||||
// Address defines the API server to listen on
|
||||
Address string `mapstructure:"address"`
|
||||
|
||||
// MaxRecvMsgSize defines the max message size in bytes the server can receive.
|
||||
// The default value is 10MB.
|
||||
MaxRecvMsgSize int `mapstructure:"max-recv-msg-size"`
|
||||
|
||||
// MaxSendMsgSize defines the max message size in bytes the server can send.
|
||||
// The default value is math.MaxInt32.
|
||||
MaxSendMsgSize int `mapstructure:"max-send-msg-size"`
|
||||
}
|
||||
|
||||
// GRPCWebConfig defines configuration for the gRPC-web server.
|
||||
|
@ -244,6 +261,8 @@ func DefaultConfig() *Config {
|
|||
GRPC: GRPCConfig{
|
||||
Enable: true,
|
||||
Address: DefaultGRPCAddress,
|
||||
MaxRecvMsgSize: DefaultGRPCMaxRecvMsgSize,
|
||||
MaxSendMsgSize: DefaultGRPCMaxSendMsgSize,
|
||||
},
|
||||
Rosetta: RosettaConfig{
|
||||
Enable: false,
|
||||
|
@ -325,6 +344,8 @@ func GetConfig(v *viper.Viper) Config {
|
|||
GRPC: GRPCConfig{
|
||||
Enable: v.GetBool("grpc.enable"),
|
||||
Address: v.GetString("grpc.address"),
|
||||
MaxRecvMsgSize: v.GetInt("grpc.max-recv-msg-size"),
|
||||
MaxSendMsgSize: v.GetInt("grpc.max-send-msg-size"),
|
||||
},
|
||||
GRPCWeb: GRPCWebConfig{
|
||||
Enable: v.GetBool("grpc-web.enable"),
|
||||
|
|
|
@ -194,6 +194,14 @@ enable = {{ .GRPC.Enable }}
|
|||
# Address defines the gRPC server address to bind to.
|
||||
address = "{{ .GRPC.Address }}"
|
||||
|
||||
# MaxRecvMsgSize defines the max message size in bytes the server can receive.
|
||||
# The default value is 10MB.
|
||||
max-recv-msg-size = "{{ .GRPC.MaxRecvMsgSize }}"
|
||||
|
||||
# MaxSendMsgSize defines the max message size in bytes the server can send.
|
||||
# The default value is math.MaxInt32.
|
||||
max-send-msg-size = "{{ .GRPC.MaxSendMsgSize }}"
|
||||
|
||||
###############################################################################
|
||||
### gRPC Web Configuration ###
|
||||
###############################################################################
|
||||
|
|
|
@ -2,13 +2,13 @@ package grpc
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/server/config"
|
||||
"github.com/cosmos/cosmos-sdk/server/grpc/gogoreflection"
|
||||
reflection "github.com/cosmos/cosmos-sdk/server/grpc/reflection/v2alpha1"
|
||||
"github.com/cosmos/cosmos-sdk/server/types"
|
||||
|
@ -16,14 +16,27 @@ import (
|
|||
)
|
||||
|
||||
// StartGRPCServer starts a gRPC server on the given address.
|
||||
func StartGRPCServer(clientCtx client.Context, app types.Application, address string) (*grpc.Server, error) {
|
||||
func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config.GRPCConfig) (*grpc.Server, error) {
|
||||
maxSendMsgSize := cfg.MaxSendMsgSize
|
||||
if maxSendMsgSize == 0 {
|
||||
maxSendMsgSize = config.DefaultGRPCMaxSendMsgSize
|
||||
}
|
||||
|
||||
maxRecvMsgSize := cfg.MaxRecvMsgSize
|
||||
if maxRecvMsgSize == 0 {
|
||||
maxRecvMsgSize = config.DefaultGRPCMaxRecvMsgSize
|
||||
}
|
||||
|
||||
grpcSrv := grpc.NewServer(
|
||||
grpc.ForceServerCodec(codec.NewProtoCodec(clientCtx.InterfaceRegistry).GRPCCodec()),
|
||||
grpc.MaxSendMsgSize(maxSendMsgSize),
|
||||
grpc.MaxRecvMsgSize(maxRecvMsgSize),
|
||||
)
|
||||
|
||||
app.RegisterGRPCServer(grpcSrv)
|
||||
// reflection allows consumers to build dynamic clients that can write
|
||||
// to any cosmos-sdk application without relying on application packages at compile time
|
||||
|
||||
// Reflection allows consumers to build dynamic clients that can write to any
|
||||
// Cosmos SDK application without relying on application packages at compile
|
||||
// time.
|
||||
err := reflection.Register(grpcSrv, reflection.Config{
|
||||
SigningModes: func() map[string]int32 {
|
||||
modes := make(map[string]int32, len(clientCtx.TxConfig.SignModeHandler().Modes()))
|
||||
|
@ -39,10 +52,12 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, address st
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Reflection allows external clients to see what services and methods
|
||||
// the gRPC server exposes.
|
||||
gogoreflection.Register(grpcSrv)
|
||||
listener, err := net.Listen("tcp", address)
|
||||
|
||||
listener, err := net.Listen("tcp", cfg.Address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -58,7 +73,9 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, address st
|
|||
select {
|
||||
case err := <-errCh:
|
||||
return nil, err
|
||||
case <-time.After(types.ServerStartTime): // assume server started successfully
|
||||
|
||||
case <-time.After(types.ServerStartTime):
|
||||
// assume server started successfully
|
||||
return grpcSrv, nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ import (
|
|||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types"
|
||||
"github.com/cosmos/cosmos-sdk/server/api"
|
||||
"github.com/cosmos/cosmos-sdk/server/config"
|
||||
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
|
||||
servergrpc "github.com/cosmos/cosmos-sdk/server/grpc"
|
||||
"github.com/cosmos/cosmos-sdk/server/rosetta"
|
||||
crgserver "github.com/cosmos/cosmos-sdk/server/rosetta/lib/server"
|
||||
|
@ -168,7 +168,7 @@ is performed. Note, when enabled, gRPC will also be automatically enabled.
|
|||
|
||||
cmd.Flags().Bool(FlagAPIEnable, false, "Define if the API server should be enabled")
|
||||
cmd.Flags().Bool(FlagAPISwagger, false, "Define if swagger documentation should automatically be registered (Note: api must also be enabled.)")
|
||||
cmd.Flags().String(FlagAPIAddress, config.DefaultAPIAddress, "the API server address to listen on")
|
||||
cmd.Flags().String(FlagAPIAddress, serverconfig.DefaultAPIAddress, "the API server address to listen on")
|
||||
cmd.Flags().Uint(FlagAPIMaxOpenConnections, 1000, "Define the number of maximum open connections")
|
||||
cmd.Flags().Uint(FlagRPCReadTimeout, 10, "Define the Tendermint RPC read timeout (in seconds)")
|
||||
cmd.Flags().Uint(FlagRPCWriteTimeout, 0, "Define the Tendermint RPC write timeout (in seconds)")
|
||||
|
@ -177,10 +177,10 @@ is performed. Note, when enabled, gRPC will also be automatically enabled.
|
|||
|
||||
cmd.Flags().Bool(flagGRPCOnly, false, "Start the node in gRPC query only mode (no Tendermint process is started)")
|
||||
cmd.Flags().Bool(flagGRPCEnable, true, "Define if the gRPC server should be enabled")
|
||||
cmd.Flags().String(flagGRPCAddress, config.DefaultGRPCAddress, "the gRPC server address to listen on")
|
||||
cmd.Flags().String(flagGRPCAddress, serverconfig.DefaultGRPCAddress, "the gRPC server address to listen on")
|
||||
|
||||
cmd.Flags().Bool(flagGRPCWebEnable, true, "Define if the gRPC-Web server should be enabled. (Note: gRPC must also be enabled.)")
|
||||
cmd.Flags().String(flagGRPCWebAddress, config.DefaultGRPCWebAddress, "The gRPC-Web server address to listen on")
|
||||
cmd.Flags().String(flagGRPCWebAddress, serverconfig.DefaultGRPCWebAddress, "The gRPC-Web server address to listen on")
|
||||
|
||||
cmd.Flags().Uint64(FlagStateSyncSnapshotInterval, 0, "State sync snapshot interval")
|
||||
cmd.Flags().Uint32(FlagStateSyncSnapshotKeepRecent, 2, "State sync snapshot to keep")
|
||||
|
@ -266,7 +266,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
|
|||
return err
|
||||
}
|
||||
|
||||
config := config.GetConfig(ctx.Viper)
|
||||
config := serverconfig.GetConfig(ctx.Viper)
|
||||
if err := config.ValidateBasic(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -333,13 +333,27 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
|
|||
return err
|
||||
}
|
||||
|
||||
maxSendMsgSize := config.GRPC.MaxSendMsgSize
|
||||
if maxSendMsgSize == 0 {
|
||||
maxSendMsgSize = serverconfig.DefaultGRPCMaxSendMsgSize
|
||||
}
|
||||
|
||||
maxRecvMsgSize := config.GRPC.MaxRecvMsgSize
|
||||
if maxRecvMsgSize == 0 {
|
||||
maxRecvMsgSize = serverconfig.DefaultGRPCMaxRecvMsgSize
|
||||
}
|
||||
|
||||
grpcAddress := fmt.Sprintf("127.0.0.1:%s", port)
|
||||
|
||||
// If grpc is enabled, configure grpc client for grpc gateway.
|
||||
grpcClient, err := grpc.Dial(
|
||||
grpcAddress,
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(clientCtx.InterfaceRegistry).GRPCCodec())),
|
||||
grpc.WithDefaultCallOptions(
|
||||
grpc.ForceCodec(codec.NewProtoCodec(clientCtx.InterfaceRegistry).GRPCCodec()),
|
||||
grpc.MaxCallRecvMsgSize(maxRecvMsgSize),
|
||||
grpc.MaxCallSendMsgSize(maxSendMsgSize),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -373,7 +387,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
|
|||
)
|
||||
|
||||
if config.GRPC.Enable {
|
||||
grpcSrv, err = servergrpc.StartGRPCServer(clientCtx, app, config.GRPC.Address)
|
||||
grpcSrv, err = servergrpc.StartGRPCServer(clientCtx, app, config.GRPC)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -97,7 +97,7 @@ func startInProcess(cfg Config, val *Validator) error {
|
|||
}
|
||||
|
||||
if val.AppConfig.GRPC.Enable {
|
||||
grpcSrv, err := servergrpc.StartGRPCServer(val.ClientCtx, app, val.AppConfig.GRPC.Address)
|
||||
grpcSrv, err := servergrpc.StartGRPCServer(val.ClientCtx, app, val.AppConfig.GRPC)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue