From 14733fbe68693a3a49679e9fdf91025a067ce58b Mon Sep 17 00:00:00 2001 From: Nikolay Nedkov Date: Mon, 21 Aug 2023 16:39:39 +0300 Subject: [PATCH] improve: adding config params and check for batch requests for enabling/disabling (disabled by default) and max requests count limit. Signed-off-by: Nikolay Nedkov --- config/config_test.go | 8 +++++++ config/default.go | 2 ++ docs/config-file/node-config-doc.html | 2 +- docs/config-file/node-config-doc.md | 30 ++++++++++++++++++++++++ docs/config-file/node-config-schema.json | 10 ++++++++ jsonrpc/config.go | 6 +++++ jsonrpc/server.go | 14 +++++++++++ 7 files changed, 71 insertions(+), 1 deletion(-) diff --git a/config/config_test.go b/config/config_test.go index 46e84d4a42..e74edd1cbe 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -321,6 +321,14 @@ func Test_Defaults(t *testing.T) { path: "RPC.EnableL2SuggestedGasPricePolling", expectedValue: true, }, + { + path: "RPC.BatchRequestsEnabled", + expectedValue: false, + }, + { + path: "RPC.BatchRequestsLimit", + expectedValue: uint(20), + }, { path: "RPC.WebSockets.Enabled", expectedValue: true, diff --git a/config/default.go b/config/default.go index b20c379a80..e3970b1866 100644 --- a/config/default.go +++ b/config/default.go @@ -85,6 +85,8 @@ MaxRequestsPerIPAndSecond = 500 SequencerNodeURI = "" EnableL2SuggestedGasPricePolling = true TraceBatchUseHTTPS = true +BatchRequestsEnabled = false +BatchRequestsLimit = 20 [RPC.WebSockets] Enabled = true Host = "0.0.0.0" diff --git a/docs/config-file/node-config-doc.html b/docs/config-file/node-config-doc.html index 3bad482c56..cee72b000e 100644 --- a/docs/config-file/node-config-doc.html +++ b/docs/config-file/node-config-doc.html @@ -14,7 +14,7 @@
"300ms"
 

Default: "1m0s"Type: string

WriteTimeout is the HTTP server write timeout
check net/http.server.WriteTimeout


Examples:

"1m"
 
"300ms"
-

Default: 500Type: number

MaxRequestsPerIPAndSecond defines how much requests a single IP can
send within a single second


Default: ""Type: string

SequencerNodeURI is used allow Non-Sequencer nodes
to relay transactions to the Sequencer node


Default: 0Type: integer

MaxCumulativeGasUsed is the max gas allowed per batch


WebSockets configuration
Default: trueType: boolean

Enabled defines if the WebSocket requests are enabled or disabled


Default: "0.0.0.0"Type: string

Host defines the network adapter that will be used to serve the WS requests


Default: 8546Type: integer

Port defines the port to serve the endpoints via WS


Default: trueType: boolean

EnableL2SuggestedGasPricePolling enables polling of the L2 gas price to block tx in the RPC with lower gas price.


Default: trueType: boolean

TraceBatchUseHTTPS enables, in the debugtraceBatchByNum endpoint, the use of the HTTPS protocol (instead of HTTP)
to do the parallel requests to RPC.debug
traceTransaction endpoint


Configuration of service `Syncrhonizer`. For this service is also really important the value of `IsTrustedSequencer` because depending of this values is going to ask to a trusted node for trusted transactions or not
Default: "1s"Type: string

SyncInterval is the delay interval between reading new rollup information


Examples:

"1m"
+

Default: 500Type: number

MaxRequestsPerIPAndSecond defines how much requests a single IP can
send within a single second


Default: ""Type: string

SequencerNodeURI is used allow Non-Sequencer nodes
to relay transactions to the Sequencer node


Default: 0Type: integer

MaxCumulativeGasUsed is the max gas allowed per batch


WebSockets configuration
Default: trueType: boolean

Enabled defines if the WebSocket requests are enabled or disabled


Default: "0.0.0.0"Type: string

Host defines the network adapter that will be used to serve the WS requests


Default: 8546Type: integer

Port defines the port to serve the endpoints via WS


Default: trueType: boolean

EnableL2SuggestedGasPricePolling enables polling of the L2 gas price to block tx in the RPC with lower gas price.


Default: trueType: boolean

TraceBatchUseHTTPS enables, in the debugtraceBatchByNum endpoint, the use of the HTTPS protocol (instead of HTTP)
to do the parallel requests to RPC.debug
traceTransaction endpoint


Default: falseType: boolean

BatchRequestsEnabled defines if the Batch requests are enabled or disabled


Default: 20Type: integer

BatchRequestsLimit defines the limit of requests that can be incorporated into each batch request


Configuration of service `Syncrhonizer`. For this service is also really important the value of `IsTrustedSequencer` because depending of this values is going to ask to a trusted node for trusted transactions or not
Default: "1s"Type: string

SyncInterval is the delay interval between reading new rollup information


Examples:

"1m"
 
"300ms"
 

Default: 100Type: integer

SyncChunkSize is the number of blocks to sync on each chunk


Default: ""Type: string

TrustedSequencerURL is the rpc url to connect and sync the trusted state


Configuration of the sequencer service
Default: "1s"Type: string

WaitPeriodPoolIsEmpty is the time the sequencer waits until
trying to add new txs to the state


Examples:

"1m"
 
"300ms"
diff --git a/docs/config-file/node-config-doc.md b/docs/config-file/node-config-doc.md
index 27c9bbd404..7e5b0689ae 100644
--- a/docs/config-file/node-config-doc.md
+++ b/docs/config-file/node-config-doc.md
@@ -715,6 +715,8 @@ GlobalQueue=1024
 | - [WebSockets](#RPC_WebSockets )                                             | No      | object  | No         | -          | WebSockets configuration                                                                                                                                                                   |
 | - [EnableL2SuggestedGasPricePolling](#RPC_EnableL2SuggestedGasPricePolling ) | No      | boolean | No         | -          | EnableL2SuggestedGasPricePolling enables polling of the L2 gas price to block tx in the RPC with lower gas price.                                                                          |
 | - [TraceBatchUseHTTPS](#RPC_TraceBatchUseHTTPS )                             | No      | boolean | No         | -          | TraceBatchUseHTTPS enables, in the debug_traceBatchByNum endpoint, the use of the HTTPS protocol (instead of HTTP)
to do the parallel requests to RPC.debug_traceTransaction endpoint | +| - [BatchRequestsEnabled](#RPC_BatchRequestsEnabled ) | No | boolean | No | - | BatchRequestsEnabled defines if the Batch requests are enabled or disabled | +| - [BatchRequestsLimit](#RPC_BatchRequestsLimit ) | No | integer | No | - | BatchRequestsLimit defines the limit of requests that can be incorporated into each batch request | ### 8.1. `RPC.Host` @@ -924,6 +926,34 @@ to do the parallel requests to RPC.debug_traceTransaction endpoint TraceBatchUseHTTPS=true ``` +### 8.11. `RPC.BatchRequestsEnabled` + +**Type:** : `boolean` + +**Default:** `false` + +**Description:** BatchRequestsEnabled defines if the Batch requests are enabled or disabled + +**Example setting the default value** (false): +``` +[RPC] +BatchRequestsEnabled=false +``` + +### 8.12. `RPC.BatchRequestsLimit` + +**Type:** : `integer` + +**Default:** `20` + +**Description:** BatchRequestsLimit defines the limit of requests that can be incorporated into each batch request + +**Example setting the default value** (20): +``` +[RPC] +BatchRequestsLimit=20 +``` + ## 9. `[Synchronizer]` **Type:** : `object` diff --git a/docs/config-file/node-config-schema.json b/docs/config-file/node-config-schema.json index bf837b443f..1bd84607c4 100644 --- a/docs/config-file/node-config-schema.json +++ b/docs/config-file/node-config-schema.json @@ -344,6 +344,16 @@ "type": "boolean", "description": "TraceBatchUseHTTPS enables, in the debug_traceBatchByNum endpoint, the use of the HTTPS protocol (instead of HTTP)\nto do the parallel requests to RPC.debug_traceTransaction endpoint", "default": true + }, + "BatchRequestsEnabled": { + "type": "boolean", + "description": "BatchRequestsEnabled defines if the Batch requests are enabled or disabled", + "default": false + }, + "BatchRequestsLimit": { + "type": "integer", + "description": "BatchRequestsLimit defines the limit of requests that can be incorporated into each batch request", + "default": 20 } }, "additionalProperties": false, diff --git a/jsonrpc/config.go b/jsonrpc/config.go index d347ba41d2..206539fdc5 100644 --- a/jsonrpc/config.go +++ b/jsonrpc/config.go @@ -38,6 +38,12 @@ type Config struct { // TraceBatchUseHTTPS enables, in the debug_traceBatchByNum endpoint, the use of the HTTPS protocol (instead of HTTP) // to do the parallel requests to RPC.debug_traceTransaction endpoint TraceBatchUseHTTPS bool `mapstructure:"TraceBatchUseHTTPS"` + + // BatchRequestsEnabled defines if the Batch requests are enabled or disabled + BatchRequestsEnabled bool `mapstructure:"BatchRequestsEnabled"` + + // BatchRequestsLimit defines the limit of requests that can be incorporated into each batch request + BatchRequestsLimit uint `mapstructure:"BatchRequestsLimit"` } // WebSocketsConfig has parameters to config the rpc websocket support diff --git a/jsonrpc/server.go b/jsonrpc/server.go index 1b80eb13ad..fbfad81159 100644 --- a/jsonrpc/server.go +++ b/jsonrpc/server.go @@ -276,6 +276,12 @@ func (s *Server) handleSingleRequest(httpRequest *http.Request, w http.ResponseW } func (s *Server) handleBatchRequest(httpRequest *http.Request, w http.ResponseWriter, data []byte) int { + // Checking if batch requests are enabled + if !s.config.BatchRequestsEnabled { + s.handleInvalidRequest(w, types.NewRPCError(types.InvalidRequestErrorCode, "batch requests are disabled")) + return 0 + } + defer metrics.RequestHandled(metrics.RequestHandledLabelBatch) requests, err := s.parseRequests(data) if err != nil { @@ -283,6 +289,14 @@ func (s *Server) handleBatchRequest(httpRequest *http.Request, w http.ResponseWr return 0 } + // Checking if batch requests limit is exceeded + if s.config.BatchRequestsLimit > 0 { + if len(requests) > int(s.config.BatchRequestsLimit) { + s.handleInvalidRequest(w, types.NewRPCError(types.InvalidRequestErrorCode, "batch requests limit exceeded")) + return 0 + } + } + responses := make([]types.Response, 0, len(requests)) for _, request := range requests {