Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
[Fix] Add the options to disable json-rpc batch request limits (#682)
Browse files Browse the repository at this point in the history
* added disable for bach json requests
* enable setting bulk json-rpc limits from conf file
* fixed flags issue and added test
  • Loading branch information
ZeljkoBenovic authored Aug 17, 2022
1 parent fb219ba commit de62840
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 18 deletions.
9 changes: 5 additions & 4 deletions command/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
7 changes: 2 additions & 5 deletions command/server/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ type serverParams struct {

corsAllowedOrigins []string

jsonRPCBatchLengthLimit uint64
jsonRPCBlockRangeLimit uint64

ibftBaseTimeoutLegacy uint64

genesisConfig *chain.Chain
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions command/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,18 @@ func setFlags(cmd *cobra.Command) {
)

cmd.Flags().Uint64Var(
&params.jsonRPCBatchLengthLimit,
&params.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(
&params.jsonRPCBlockRangeLimit,
&params.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(
Expand Down
5 changes: 3 additions & 2 deletions jsonrpc/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
39 changes: 39 additions & 0 deletions jsonrpc/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/filter_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit de62840

Please sign in to comment.