Skip to content

Commit

Permalink
Merge branch 'develop' into improve/rpc-limit-batch-requests
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Nedkov <[email protected]>
  • Loading branch information
Psykepro committed Aug 22, 2023
2 parents 99cb98b + 101da01 commit 9132e55
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 8 deletions.
4 changes: 4 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ func Test_Defaults(t *testing.T) {
path: "RPC.WebSockets.Port",
expectedValue: int(8546),
},
{
path: "RPC.WebSockets.ReadLimit",
expectedValue: int64(104857600),
},
{
path: "Executor.URI",
expectedValue: "zkevm-prover:50071",
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ BatchRequestsLimit = 20
Enabled = true
Host = "0.0.0.0"
Port = 8546
ReadLimit = 104857600
[Synchronizer]
SyncInterval = "1s"
Expand Down
2 changes: 1 addition & 1 deletion docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

25 changes: 20 additions & 5 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -849,11 +849,12 @@ MaxCumulativeGasUsed=0
**Type:** : `object`
**Description:** WebSockets configuration

| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| ------------------------------------- | ------- | ------- | ---------- | ---------- | --------------------------------------------------------------------------- |
| - [Enabled](#RPC_WebSockets_Enabled ) | No | boolean | No | - | Enabled defines if the WebSocket requests are enabled or disabled |
| - [Host](#RPC_WebSockets_Host ) | No | string | No | - | Host defines the network adapter that will be used to serve the WS requests |
| - [Port](#RPC_WebSockets_Port ) | No | integer | No | - | Port defines the port to serve the endpoints via WS |
| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| ----------------------------------------- | ------- | ------- | ---------- | ---------- | ------------------------------------------------------------------------------- |
| - [Enabled](#RPC_WebSockets_Enabled ) | No | boolean | No | - | Enabled defines if the WebSocket requests are enabled or disabled |
| - [Host](#RPC_WebSockets_Host ) | No | string | No | - | Host defines the network adapter that will be used to serve the WS requests |
| - [Port](#RPC_WebSockets_Port ) | No | integer | No | - | Port defines the port to serve the endpoints via WS |
| - [ReadLimit](#RPC_WebSockets_ReadLimit ) | No | integer | No | - | ReadLimit defines the maximum size of a message read from the client (in bytes) |

#### <a name="RPC_WebSockets_Enabled"></a>8.8.1. `RPC.WebSockets.Enabled`

Expand Down Expand Up @@ -897,6 +898,20 @@ Host="0.0.0.0"
Port=8546
```

#### <a name="RPC_WebSockets_ReadLimit"></a>8.8.4. `RPC.WebSockets.ReadLimit`

**Type:** : `integer`

**Default:** `104857600`

**Description:** ReadLimit defines the maximum size of a message read from the client (in bytes)

**Example setting the default value** (104857600):
```
[RPC.WebSockets]
ReadLimit=104857600
```

### <a name="RPC_EnableL2SuggestedGasPricePolling"></a>8.9. `RPC.EnableL2SuggestedGasPricePolling`

**Type:** : `boolean`
Expand Down
5 changes: 5 additions & 0 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@
"type": "integer",
"description": "Port defines the port to serve the endpoints via WS",
"default": 8546
},
"ReadLimit": {
"type": "integer",
"description": "ReadLimit defines the maximum size of a message read from the client (in bytes)",
"default": 104857600
}
},
"additionalProperties": false,
Expand Down
2 changes: 1 addition & 1 deletion docs/production-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Explained step by step:
2. Define installation path: `ZKEVM_DIR=./path/to/install`
3. Define a config directory: `ZKEVM_CONFIG_DIR=./path/to/config`
4. It's recommended to source this env vars in your `~/.bashrc`, `~/.zshrc` or whatever you're using
5. Download and extract the artifacts: `curl -L https://github.com/0xPolygonHermez/zkevm-node/releases/latest/download/$ZKEVM_NET.zip > $ZKEVM_NET.zip && unzip -o $ZKEVM_NET.zip -d $ZKEVM_DIR && rm $ZKEVM_NET.zip`. Note you may need to install `unzip` for this command to work
5. Download and extract the artifacts: `curl -L https://github.com/0xPolygonHermez/zkevm-node/releases/latest/download/$ZKEVM_NET.zip > $ZKEVM_NET.zip && unzip -o $ZKEVM_NET.zip -d $ZKEVM_DIR && rm $ZKEVM_NET.zip`. Note you may need to install `unzip` for this command to work. Also take into account this works for the latest release, in case you want to deploy a pre-release you should get the artifacts directly for that release and not using the "latest" link depicted here. [Here](https://github.com/0xPolygonHermez) you can check the node version deployed for each network.
6. Copy the file with the env parameters: `cp $ZKEVM_DIR/$ZKEVM_NET/example.env $ZKEVM_CONFIG_DIR/.env`
7. Edit the env file, with your favourite editor. The example will use nano: `nano $ZKEVM_CONFIG_DIR/.env`. This file contains the configuration that anyone should modify. For advanced configuration:
1. Copy the config files into the config directory `cp $ZKEVM_DIR/$ZKEVM_NET/config/environments/testnet/* $ZKEVM_CONFIG_DIR/`
Expand Down
3 changes: 3 additions & 0 deletions jsonrpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ type WebSocketsConfig struct {

// Port defines the port to serve the endpoints via WS
Port int `mapstructure:"Port"`

// ReadLimit defines the maximum size of a message read from the client (in bytes)
ReadLimit int64 `mapstructure:"ReadLimit"`
}
5 changes: 5 additions & 0 deletions jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ func (s *Server) handleWs(w http.ResponseWriter, req *http.Request) {
return
}

// Set read limit
wsConn.SetReadLimit(s.config.WebSockets.ReadLimit)

// Defer WS closure
defer func(ws *websocket.Conn) {
err = ws.Close()
Expand All @@ -370,6 +373,8 @@ func (s *Server) handleWs(w http.ResponseWriter, req *http.Request) {
if err != nil {
if websocket.IsCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure, websocket.CloseAbnormalClosure) {
log.Info("Closing WS connection gracefully")
} else if errors.Is(err, websocket.ErrReadLimit) {
log.Info("Closing WS connection due to read limit exceeded")
} else {
log.Error(fmt.Sprintf("Unable to read WS message, %s", err.Error()))
log.Info("Closing WS connection with error")
Expand Down
25 changes: 24 additions & 1 deletion test/e2e/jsonrpc2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,11 @@ func TestWebSocketsConcurrentWrites(t *testing.T) {
log.Infof("Network %s", network.Name)

wsConn, _, err := websocket.DefaultDialer.Dial(network.WebSocketURL, nil)
require.NoError(t, err)
defer func() {
err := wsConn.Close()
require.NoError(t, err)
}()
require.NoError(t, err)

wg := sync.WaitGroup{}
wg.Add(msgQty)
Expand Down Expand Up @@ -527,6 +527,29 @@ func TestWebSocketsConcurrentWrites(t *testing.T) {
}
}

func TestWebSocketsReadLimit(t *testing.T) {
if testing.Short() {
t.Skip()
}
setup()
defer teardown()

wsConn, _, err := websocket.DefaultDialer.Dial(operations.DefaultL2NetworkWebSocketURL, nil)
require.NoError(t, err)
defer func() {
err := wsConn.Close()
require.NoError(t, err)
}()

jReq := make([]byte, 104857601)
err = wsConn.WriteMessage(websocket.TextMessage, jReq)
require.NoError(t, err)

_, _, err = wsConn.ReadMessage()
require.NotNil(t, err)
require.Equal(t, websocket.CloseMessageTooBig, err.(*websocket.CloseError).Code)
}

// waitTimeout waits for the waitgroup for the specified max timeout.
// Returns true if waiting timed out.
func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
Expand Down

0 comments on commit 9132e55

Please sign in to comment.