From 0d144857a2e39522ba8a983daa567e3fb797c431 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 7 May 2020 13:24:00 -0400 Subject: [PATCH 1/3] Add max-body-bytes flag for REST service --- client/flags/flags.go | 2 ++ client/lcd/root.go | 49 +++++++++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/client/flags/flags.go b/client/flags/flags.go index 9544e4baf39..a3e78ed6732 100644 --- a/client/flags/flags.go +++ b/client/flags/flags.go @@ -63,6 +63,7 @@ const ( FlagMaxOpenConnections = "max-open" FlagRPCReadTimeout = "read-timeout" FlagRPCWriteTimeout = "write-timeout" + FlagRPCMaxBodyBytes = "max-body-bytes" FlagOutputDocument = "output-document" // inspired by wget -O FlagSkipConfirmation = "yes" FlagProve = "prove" @@ -148,6 +149,7 @@ func RegisterRestServerFlags(cmd *cobra.Command) *cobra.Command { cmd.Flags().Uint(FlagMaxOpenConnections, 1000, "The number of maximum open connections") cmd.Flags().Uint(FlagRPCReadTimeout, 10, "The RPC read timeout (in seconds)") cmd.Flags().Uint(FlagRPCWriteTimeout, 10, "The RPC write timeout (in seconds)") + cmd.Flags().Int64(FlagRPCMaxBodyBytes, 1000000, "The RPC max body bytes (in MB)") cmd.Flags().Bool(FlagUnsafeCORS, false, "Allows CORS requests from all domains. For development purposes only, use it at your own risk.") return cmd diff --git a/client/lcd/root.go b/client/lcd/root.go index aa3556bcda7..843411bc928 100644 --- a/client/lcd/root.go +++ b/client/lcd/root.go @@ -46,28 +46,23 @@ func NewRestServer(cdc *codec.Codec) *RestServer { } } -// Start starts the rest server -func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTimeout uint, cors bool) (err error) { +// StartWithConfig starts the REST server that listens on the provided listenAddr. +// It will use the provided RPC configuration. +func (rs *RestServer) StartWithConfig(listenAddr string, cors bool, cfg *rpcserver.Config) error { server.TrapSignal(func() { err := rs.listener.Close() rs.log.Error("error closing listener", "err", err) }) - cfg := rpcserver.DefaultConfig() - cfg.MaxOpenConnections = maxOpen - cfg.ReadTimeout = time.Duration(readTimeout) * time.Second - cfg.WriteTimeout = time.Duration(writeTimeout) * time.Second - - rs.listener, err = rpcserver.Listen(listenAddr, cfg) + listener, err := rpcserver.Listen(listenAddr, cfg) if err != nil { - return + return err } + rs.listener = listener + rs.log.Info( - fmt.Sprintf( - "Starting application REST service (chain-id: %q)...", - viper.GetString(flags.FlagChainID), - ), + fmt.Sprintf("Starting application REST service (chain-id: %q)...", viper.GetString(flags.FlagChainID)), ) var h http.Handler = rs.Mux @@ -79,6 +74,18 @@ func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTi return rpcserver.StartHTTPServer(rs.listener, rs.Mux, rs.log, cfg) } +// Start starts the REST server that listens on the provided listenAddr. The REST +// service will use Tendermint's default RPC configuration, where the R/W timeout +// and max open connections are overridden. +func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTimeout uint, cors bool) error { + cfg := rpcserver.DefaultConfig() + cfg.MaxOpenConnections = maxOpen + cfg.ReadTimeout = time.Duration(readTimeout) * time.Second + cfg.WriteTimeout = time.Duration(writeTimeout) * time.Second + + return rs.StartWithConfig(listenAddr, cors, cfg) +} + // ServeCommand will start the application REST service as a blocking process. It // takes a codec to create a RestServer object and a function to register all // necessary routes. @@ -92,16 +99,18 @@ func ServeCommand(cdc *codec.Codec, registerRoutesFn func(*RestServer)) *cobra.C registerRoutesFn(rs) rs.registerSwaggerUI() - // Start the rest server and return error if one exists - err = rs.Start( + cfg := rpcserver.DefaultConfig() + cfg.MaxOpenConnections = viper.GetInt(flags.FlagMaxOpenConnections) + cfg.ReadTimeout = time.Duration(viper.GetUint(flags.FlagRPCReadTimeout)) * time.Second + cfg.WriteTimeout = time.Duration(viper.GetUint(flags.FlagRPCWriteTimeout)) * time.Second + cfg.MaxBodyBytes = viper.GetInt64(flags.FlagRPCMaxBodyBytes) + + // start the rest server and return error if one exists + return rs.StartWithConfig( viper.GetString(flags.FlagListenAddr), - viper.GetInt(flags.FlagMaxOpenConnections), - uint(viper.GetInt(flags.FlagRPCReadTimeout)), - uint(viper.GetInt(flags.FlagRPCWriteTimeout)), viper.GetBool(flags.FlagUnsafeCORS), + cfg, ) - - return err }, } From 1d47b6a7cdf21d3e305905819ef9d379d471c194 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 7 May 2020 13:26:18 -0400 Subject: [PATCH 2/3] Add changelog entries --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 147d721becb..7557f89ad24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,7 @@ information on how to implement the new `Keyring` interface. ### Features +* (rest) [\#6167](https://github.com/cosmos/cosmos-sdk/pull/6167) Support `max-body-bytes` CLI flag for the REST service. * (x/ibc) [\#5588](https://github.com/cosmos/cosmos-sdk/pull/5588) Add [ICS 024 - Host State Machine Requirements](https://github.com/cosmos/ics/tree/master/spec/ics-024-host-requirements) subpackage to `x/ibc` module. * (x/ibc) [\#5277](https://github.com/cosmos/cosmos-sdk/pull/5277) `x/ibc` changes from IBC alpha. For more details check the the [`x/ibc/spec`](https://github.com/cosmos/tree/master/x/ibc/spec) directory: * [ICS 002 - Client Semantics](https://github.com/cosmos/ics/tree/master/spec/ics-002-client-semantics) subpackage From ea5ca3fd1c0f4ebbdf08ad9b1f79ee2b3d95e6d5 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 7 May 2020 13:35:48 -0400 Subject: [PATCH 3/3] Change types --- client/flags/flags.go | 2 +- client/lcd/root.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/flags/flags.go b/client/flags/flags.go index a3e78ed6732..c45a0ac1458 100644 --- a/client/flags/flags.go +++ b/client/flags/flags.go @@ -149,7 +149,7 @@ func RegisterRestServerFlags(cmd *cobra.Command) *cobra.Command { cmd.Flags().Uint(FlagMaxOpenConnections, 1000, "The number of maximum open connections") cmd.Flags().Uint(FlagRPCReadTimeout, 10, "The RPC read timeout (in seconds)") cmd.Flags().Uint(FlagRPCWriteTimeout, 10, "The RPC write timeout (in seconds)") - cmd.Flags().Int64(FlagRPCMaxBodyBytes, 1000000, "The RPC max body bytes (in MB)") + cmd.Flags().Uint(FlagRPCMaxBodyBytes, 1000000, "The RPC max body bytes (in MB)") cmd.Flags().Bool(FlagUnsafeCORS, false, "Allows CORS requests from all domains. For development purposes only, use it at your own risk.") return cmd diff --git a/client/lcd/root.go b/client/lcd/root.go index 843411bc928..70c8e4f31e1 100644 --- a/client/lcd/root.go +++ b/client/lcd/root.go @@ -101,8 +101,8 @@ func ServeCommand(cdc *codec.Codec, registerRoutesFn func(*RestServer)) *cobra.C cfg := rpcserver.DefaultConfig() cfg.MaxOpenConnections = viper.GetInt(flags.FlagMaxOpenConnections) - cfg.ReadTimeout = time.Duration(viper.GetUint(flags.FlagRPCReadTimeout)) * time.Second - cfg.WriteTimeout = time.Duration(viper.GetUint(flags.FlagRPCWriteTimeout)) * time.Second + cfg.ReadTimeout = time.Duration(viper.GetInt64(flags.FlagRPCReadTimeout)) * time.Second + cfg.WriteTimeout = time.Duration(viper.GetInt64(flags.FlagRPCWriteTimeout)) * time.Second cfg.MaxBodyBytes = viper.GetInt64(flags.FlagRPCMaxBodyBytes) // start the rest server and return error if one exists