From 9b104183122426e131669b007af9afff4727b70a Mon Sep 17 00:00:00 2001 From: Guillaume Ballet Date: Tue, 16 Jul 2019 13:34:20 +0200 Subject: [PATCH] cmd/geth: let retesteth increase the payload size --- cmd/geth/retesteth.go | 5 +++++ rpc/http.go | 21 +++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/cmd/geth/retesteth.go b/cmd/geth/retesteth.go index 0615f446fc..4663c04c52 100644 --- a/cmd/geth/retesteth.go +++ b/cmd/geth/retesteth.go @@ -68,6 +68,7 @@ var ( ) type RetestethTestAPI interface { + SetRPCPayloadSize(ctx context.Context, size int) (int, error) SetChainParams(ctx context.Context, chainParams ChainParams) (bool, error) MineBlocks(ctx context.Context, number uint64) (bool, error) ModifyTimestamp(ctx context.Context, interval uint64) (bool, error) @@ -271,6 +272,10 @@ func (e *NoRewardEngine) Close() error { return e.inner.Close() } +func (api *RetestethAPI) SetRPCPayloadSize(ctx context.Context, size int) (int, error) { + return rpc.SetMaxRequestContentLength(size) +} + func (api *RetestethAPI) SetChainParams(ctx context.Context, chainParams ChainParams) (bool, error) { // Clean up if api.blockchain != nil { diff --git a/rpc/http.go b/rpc/http.go index 518b3b874f..baa844d4fa 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -35,14 +35,23 @@ import ( "github.com/rs/cors" ) -const ( - maxRequestContentLength = 1024 * 512 - contentType = "application/json" -) +const contentType = "application/json" + +var maxRequestContentLength = 1024 * 512 // https://www.jsonrpc.org/historical/json-rpc-over-http.html#id13 var acceptedContentTypes = []string{contentType, "application/json-rpc", "application/jsonrequest"} +// SetMaxRequestContentLength changes the maximum payload size to be accepted +// by the RPC system. This is only to be changed for tests. +func SetMaxRequestContentLength(length int) (int, error) { + if length < 0 { + return maxRequestContentLength, fmt.Errorf("Invalid RPC payload length %d", length) + } + maxRequestContentLength = length + return maxRequestContentLength, nil +} + type httpConn struct { client *http.Client req *http.Request @@ -193,7 +202,7 @@ type httpServerConn struct { } func newHTTPServerConn(r *http.Request, w http.ResponseWriter) ServerCodec { - body := io.LimitReader(r.Body, maxRequestContentLength) + body := io.LimitReader(r.Body, int64(maxRequestContentLength)) conn := &httpServerConn{Reader: body, Writer: w, r: r} return NewJSONCodec(conn) } @@ -275,7 +284,7 @@ func validateRequest(r *http.Request) (int, error) { if r.Method == http.MethodPut || r.Method == http.MethodDelete { return http.StatusMethodNotAllowed, errors.New("method not allowed") } - if r.ContentLength > maxRequestContentLength { + if r.ContentLength > int64(maxRequestContentLength) { err := fmt.Errorf("content length too large (%d>%d)", r.ContentLength, maxRequestContentLength) return http.StatusRequestEntityTooLarge, err }