From de628407602342f1bfc0174c9f6671edca363cdd Mon Sep 17 00:00:00 2001 From: ZeljkoBenovic <47507986+ZeljkoBenovic@users.noreply.github.com> Date: Wed, 17 Aug 2022 11:13:41 +0200 Subject: [PATCH] [Fix] Add the options to disable json-rpc batch request limits (#682) * added disable for bach json requests * enable setting bulk json-rpc limits from conf file * fixed flags issue and added test --- command/server/config/config.go | 9 ++++---- command/server/params.go | 7 ++---- command/server/server.go | 10 ++++----- jsonrpc/dispatcher.go | 5 +++-- jsonrpc/dispatcher_test.go | 39 +++++++++++++++++++++++++++++++++ jsonrpc/filter_manager.go | 4 ++-- 6 files changed, 56 insertions(+), 18 deletions(-) diff --git a/command/server/config/config.go b/command/server/config/config.go index 9afd418caa..755d2637be 100644 --- a/command/server/config/config.go +++ b/command/server/config/config.go @@ -61,17 +61,18 @@ type Headers struct { } const ( - // minimum block generation time in seconds + // DefaultBlockTime minimum block generation time in seconds DefaultBlockTime uint64 = 2 - // Multiplier to get IBFT timeout from block time + // BlockTimeMultiplierForTimeout Multiplier to get IBFT timeout from block time // timeout is calculated when IBFT timeout is not specified BlockTimeMultiplierForTimeout uint64 = 5 - // maximum length allowed for json_rpc batch requests + // DefaultJSONRPCBatchRequestLimit maximum length allowed for json_rpc batch requests DefaultJSONRPCBatchRequestLimit uint64 = 20 - // maximum block range allowed for json_rpc requests with fromBlock/toBlock values (e.g. eth_getLogs) + // DefaultJSONRPCBlockRangeLimit maximum block range allowed for json_rpc + // requests with fromBlock/toBlock values (e.g. eth_getLogs) DefaultJSONRPCBlockRangeLimit uint64 = 1000 ) diff --git a/command/server/params.go b/command/server/params.go index 332bad5c8d..c89eb953a6 100644 --- a/command/server/params.go +++ b/command/server/params.go @@ -80,9 +80,6 @@ type serverParams struct { corsAllowedOrigins []string - jsonRPCBatchLengthLimit uint64 - jsonRPCBlockRangeLimit uint64 - ibftBaseTimeoutLegacy uint64 genesisConfig *chain.Chain @@ -146,8 +143,8 @@ func (p *serverParams) generateConfig() *server.Config { JSONRPC: &server.JSONRPC{ JSONRPCAddr: p.jsonRPCAddress, AccessControlAllowOrigin: p.corsAllowedOrigins, - BatchLengthLimit: p.jsonRPCBatchLengthLimit, - BlockRangeLimit: p.jsonRPCBlockRangeLimit, + BatchLengthLimit: p.rawConfig.JSONRPCBatchRequestLimit, + BlockRangeLimit: p.rawConfig.JSONRPCBlockRangeLimit, }, GRPCAddr: p.grpcAddress, LibP2PAddr: p.libp2pAddress, diff --git a/command/server/server.go b/command/server/server.go index bddc111428..8da2c2f991 100644 --- a/command/server/server.go +++ b/command/server/server.go @@ -194,18 +194,18 @@ func setFlags(cmd *cobra.Command) { ) cmd.Flags().Uint64Var( - ¶ms.jsonRPCBatchLengthLimit, + ¶ms.rawConfig.JSONRPCBatchRequestLimit, jsonRPCBatchRequestLimitFlag, defaultConfig.JSONRPCBatchRequestLimit, - "the max length to be considered when handling json-rpc batch requests", + "max length to be considered when handling json-rpc batch requests, value of 0 disables it", ) - //nolint:lll cmd.Flags().Uint64Var( - ¶ms.jsonRPCBlockRangeLimit, + ¶ms.rawConfig.JSONRPCBlockRangeLimit, jsonRPCBlockRangeLimitFlag, defaultConfig.JSONRPCBlockRangeLimit, - "the max block range to be considered when executing json-rpc requests that consider fromBlock/toBlock values (e.g. eth_getLogs)", + "max block range to be considered when executing json-rpc requests "+ + "that consider fromBlock/toBlock values (e.g. eth_getLogs), value of 0 disables it", ) cmd.Flags().StringVar( diff --git a/jsonrpc/dispatcher.go b/jsonrpc/dispatcher.go index cc979392d2..ba27aae374 100644 --- a/jsonrpc/dispatcher.go +++ b/jsonrpc/dispatcher.go @@ -262,8 +262,9 @@ func (d *Dispatcher) Handle(reqBody []byte) ([]byte, error) { return NewRPCResponse(nil, "2.0", nil, NewInvalidRequestError("Invalid json request")).Bytes() } - // avoid handling long batch requests - if len(requests) > int(d.jsonRPCBatchLengthLimit) { + // if not disabled, avoid handling long batch requests + if d.jsonRPCBatchLengthLimit != 0 && + len(requests) > int(d.jsonRPCBatchLengthLimit) { return NewRPCResponse(nil, "2.0", nil, NewInvalidRequestError("Batch request length too long")).Bytes() } diff --git a/jsonrpc/dispatcher_test.go b/jsonrpc/dispatcher_test.go index 674b9bc08c..de58e7fc64 100644 --- a/jsonrpc/dispatcher_test.go +++ b/jsonrpc/dispatcher_test.go @@ -330,6 +330,40 @@ func TestDispatcherBatchRequest(t *testing.T) { &ObjectError{Code: -32600, Message: "Batch request length too long"}, nil, }, + { + "no-limits", + "test when limits are not set", + newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 0, 0, 0), + []byte(`[ + {"id":1,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}, + {"id":2,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}, + {"id":3,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}, + {"id":4,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}, + {"id":5,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}, + {"id":6,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}, + {"id":7,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}, + {"id":8,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}, + {"id":9,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}, + {"id":10,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}, + {"id":11,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}, + {"id":12,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]}]`, + ), + nil, + []*SuccessResponse{ + {Error: nil}, + {Error: nil}, + {Error: nil}, + {Error: nil}, + {Error: nil}, + {Error: nil}, + {Error: nil}, + {Error: nil}, + {Error: nil}, + {Error: nil}, + {Error: nil}, + {Error: nil}, + }, + }, } for _, c := range cases { @@ -354,6 +388,11 @@ func TestDispatcherBatchRequest(t *testing.T) { for index, resp := range batchResp { assert.Equal(t, resp.Error, c.batchResponse[index].Error) } + } else if c.name == "no-limits" { + assert.Len(t, batchResp, 12) + for index, resp := range batchResp { + assert.Equal(t, resp.Error, c.batchResponse[index].Error) + } } } } diff --git a/jsonrpc/filter_manager.go b/jsonrpc/filter_manager.go index f26a1db2c5..dbed1a22a7 100644 --- a/jsonrpc/filter_manager.go +++ b/jsonrpc/filter_manager.go @@ -429,8 +429,8 @@ func (f *FilterManager) getLogsFromBlocks(query *LogQuery) ([]*Log, error) { from = 1 } - // avoid handling large block ranges - if to-from > f.blockRangeLimit { + // if not disabled, avoid handling large block ranges + if f.blockRangeLimit != 0 && to-from > f.blockRangeLimit { return nil, ErrBlockRangeTooHigh }