Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new endpoint zkevm_getNativeBlockHashesInRange #2661

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ func newState(ctx context.Context, c *config.Config, l2ChainID uint64, forkIDInt
ForkUpgradeNewForkId: c.ForkUpgradeNewForkId,
MaxLogsCount: c.RPC.MaxLogsCount,
MaxLogsBlockRange: c.RPC.MaxLogsBlockRange,
MaxNativeBlockHashBlockRange: c.RPC.MaxNativeBlockHashBlockRange,
}

st := state.NewState(stateCfg, stateDb, executorClient, stateTree, eventLog)
Expand Down
4 changes: 4 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ func Test_Defaults(t *testing.T) {
path: "RPC.MaxLogsBlockRange",
expectedValue: uint64(10000),
},
{
path: "RPC.MaxNativeBlockHashBlockRange",
expectedValue: uint64(60000),
},
{
path: "RPC.WebSockets.Enabled",
expectedValue: true,
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ BatchRequestsEnabled = false
BatchRequestsLimit = 20
MaxLogsCount = 10000
MaxLogsBlockRange = 10000
MaxNativeBlockHashBlockRange = 60000
[RPC.WebSockets]
Enabled = true
Host = "0.0.0.0"
Expand Down
2 changes: 1 addition & 1 deletion docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

48 changes: 32 additions & 16 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,22 +718,23 @@ GlobalQueue=1024
**Type:** : `object`
**Description:** Configuration for RPC service. THis one offers a extended Ethereum JSON-RPC API interface to interact with the node

| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| ---------------------------------------------------------------------------- | ------- | ---------------- | ---------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| - [Host](#RPC_Host ) | No | string | No | - | Host defines the network adapter that will be used to serve the HTTP requests |
| - [Port](#RPC_Port ) | No | integer | No | - | Port defines the port to serve the endpoints via HTTP |
| - [ReadTimeout](#RPC_ReadTimeout ) | No | string | No | - | Duration |
| - [WriteTimeout](#RPC_WriteTimeout ) | No | string | No | - | Duration |
| - [MaxRequestsPerIPAndSecond](#RPC_MaxRequestsPerIPAndSecond ) | No | number | No | - | MaxRequestsPerIPAndSecond defines how much requests a single IP can<br />send within a single second |
| - [SequencerNodeURI](#RPC_SequencerNodeURI ) | No | string | No | - | SequencerNodeURI is used allow Non-Sequencer nodes<br />to relay transactions to the Sequencer node |
| - [MaxCumulativeGasUsed](#RPC_MaxCumulativeGasUsed ) | No | integer | No | - | MaxCumulativeGasUsed is the max gas allowed per batch |
| - [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. |
| - [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 |
| - [L2Coinbase](#RPC_L2Coinbase ) | No | array of integer | No | - | L2Coinbase defines which address is going to receive the fees |
| - [MaxLogsCount](#RPC_MaxLogsCount ) | No | integer | No | - | MaxLogsCount is a configuration to set the max number of logs that can be returned<br />in a single call to the state, if zero it means no limit |
| - [MaxLogsBlockRange](#RPC_MaxLogsBlockRange ) | No | integer | No | - | MaxLogsBlockRange is a configuration to set the max range for block number when querying TXs<br />logs in a single call to the state, if zero it means no limit |
| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| ---------------------------------------------------------------------------- | ------- | ---------------- | ---------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| - [Host](#RPC_Host ) | No | string | No | - | Host defines the network adapter that will be used to serve the HTTP requests |
| - [Port](#RPC_Port ) | No | integer | No | - | Port defines the port to serve the endpoints via HTTP |
| - [ReadTimeout](#RPC_ReadTimeout ) | No | string | No | - | Duration |
| - [WriteTimeout](#RPC_WriteTimeout ) | No | string | No | - | Duration |
| - [MaxRequestsPerIPAndSecond](#RPC_MaxRequestsPerIPAndSecond ) | No | number | No | - | MaxRequestsPerIPAndSecond defines how much requests a single IP can<br />send within a single second |
| - [SequencerNodeURI](#RPC_SequencerNodeURI ) | No | string | No | - | SequencerNodeURI is used allow Non-Sequencer nodes<br />to relay transactions to the Sequencer node |
| - [MaxCumulativeGasUsed](#RPC_MaxCumulativeGasUsed ) | No | integer | No | - | MaxCumulativeGasUsed is the max gas allowed per batch |
| - [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. |
| - [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 |
| - [L2Coinbase](#RPC_L2Coinbase ) | No | array of integer | No | - | L2Coinbase defines which address is going to receive the fees |
| - [MaxLogsCount](#RPC_MaxLogsCount ) | No | integer | No | - | MaxLogsCount is a configuration to set the max number of logs that can be returned<br />in a single call to the state, if zero it means no limit |
| - [MaxLogsBlockRange](#RPC_MaxLogsBlockRange ) | No | integer | No | - | MaxLogsBlockRange is a configuration to set the max range for block number when querying TXs<br />logs in a single call to the state, if zero it means no limit |
| - [MaxNativeBlockHashBlockRange](#RPC_MaxNativeBlockHashBlockRange ) | No | integer | No | - | MaxNativeBlockHashBlockRange is a configuration to set the max range for block number when querying<br />native block hashes in a single call to the state, if zero it means no limit |

### <a name="RPC_Host"></a>8.1. `RPC.Host`

Expand Down Expand Up @@ -1006,6 +1007,21 @@ logs in a single call to the state, if zero it means no limit
MaxLogsBlockRange=10000
```

### <a name="RPC_MaxNativeBlockHashBlockRange"></a>8.15. `RPC.MaxNativeBlockHashBlockRange`

**Type:** : `integer`

**Default:** `60000`

**Description:** MaxNativeBlockHashBlockRange is a configuration to set the max range for block number when querying
native block hashes in a single call to the state, if zero it means no limit

**Example setting the default value** (60000):
```
[RPC]
MaxNativeBlockHashBlockRange=60000
```

## <a name="Synchronizer"></a>9. `[Synchronizer]`

**Type:** : `object`
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 @@ -378,6 +378,11 @@
"type": "integer",
"description": "MaxLogsBlockRange is a configuration to set the max range for block number when querying TXs\nlogs in a single call to the state, if zero it means no limit",
"default": 10000
},
"MaxNativeBlockHashBlockRange": {
"type": "integer",
"description": "MaxNativeBlockHashBlockRange is a configuration to set the max range for block number when querying\nnative block hashes in a single call to the state, if zero it means no limit",
"default": 60000
}
},
"additionalProperties": false,
Expand Down
1 change: 1 addition & 0 deletions docs/json-rpc-endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ If the endpoint is not in the list below, it means this specific endpoint is not
- `zkevm_getBatchByNumber`
- `zkevm_getFullBlockByHash`
- `zkevm_getFullBlockByNumber`
- `zkevm_getNativeBlockHashesInRange`
- `zkevm_isBlockConsolidated`
- `zkevm_isBlockVirtualized`
- `zkevm_verifiedBatchNumber`
Expand Down
4 changes: 4 additions & 0 deletions jsonrpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type Config struct {
// MaxLogsBlockRange is a configuration to set the max range for block number when querying TXs
// logs in a single call to the state, if zero it means no limit
MaxLogsBlockRange uint64 `mapstructure:"MaxLogsBlockRange"`

// MaxNativeBlockHashBlockRange is a configuration to set the max range for block number when querying
// native block hashes in a single call to the state, if zero it means no limit
MaxNativeBlockHashBlockRange uint64 `mapstructure:"MaxNativeBlockHashBlockRange"`
}

// WebSocketsConfig has parameters to config the rpc websocket support
Expand Down
35 changes: 5 additions & 30 deletions jsonrpc/endpoints_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,22 +486,13 @@ func (e *EthEndpoints) GetLogs(filter LogFilter) (interface{}, types.Error) {
}

func (e *EthEndpoints) internalGetLogs(ctx context.Context, dbTx pgx.Tx, filter LogFilter) (interface{}, types.Error) {
var err error
var fromBlock uint64 = 0
if filter.FromBlock != nil {
var rpcErr types.Error
fromBlock, rpcErr = filter.FromBlock.GetNumericBlockNumber(ctx, e.state, e.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
}

toBlock, rpcErr := filter.ToBlock.GetNumericBlockNumber(ctx, e.state, e.etherman, dbTx)
fromBlockNumber, toBlockNumber, rpcErr := filter.GetNumericBlockNumbers(ctx, e.cfg, e.state, e.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}

logs, err := e.state.GetLogs(ctx, fromBlock, toBlock, filter.Addresses, filter.Topics, filter.BlockHash, filter.Since, dbTx)
var err error
logs, err := e.state.GetLogs(ctx, fromBlockNumber, toBlockNumber, filter.Addresses, filter.Topics, filter.BlockHash, filter.Since, dbTx)
if errors.Is(err, state.ErrMaxLogsCountLimitExceeded) {
errMsg := fmt.Sprintf(state.ErrMaxLogsCountLimitExceeded.Error(), e.cfg.MaxLogsCount)
return RPCErrorResponse(types.InvalidParamsErrorCode, errMsg, nil, false)
Expand Down Expand Up @@ -845,27 +836,11 @@ func (e *EthEndpoints) NewFilter(filter LogFilter) (interface{}, types.Error) {

// internal
func (e *EthEndpoints) newFilter(ctx context.Context, wsConn *atomic.Pointer[websocket.Conn], filter LogFilter, dbTx pgx.Tx) (interface{}, types.Error) {
shouldFilterByBlockRange := filter.FromBlock != nil || filter.ToBlock != nil

if shouldFilterByBlockRange {
toBlockNumber, rpcErr := filter.ToBlock.GetNumericBlockNumber(ctx, e.state, e.etherman, dbTx)
if filter.ShouldFilterByBlockRange() {
_, _, rpcErr := filter.GetNumericBlockNumbers(ctx, e.cfg, e.state, e.etherman, nil)
if rpcErr != nil {
return nil, rpcErr
}
fromBlockNumber, rpcErr := filter.FromBlock.GetNumericBlockNumber(ctx, e.state, e.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}

if toBlockNumber < fromBlockNumber {
return RPCErrorResponse(types.InvalidParamsErrorCode, state.ErrInvalidBlockRange.Error(), nil, false)
}

blockRange := toBlockNumber - fromBlockNumber
if e.cfg.MaxLogsBlockRange > 0 && blockRange > e.cfg.MaxLogsBlockRange {
errMsg := fmt.Sprintf(state.ErrMaxLogsBlockRangeLimitExceeded.Error(), e.cfg.MaxLogsBlockRange)
return RPCErrorResponse(types.InvalidParamsErrorCode, errMsg, nil, false)
}
}

id, err := e.storage.NewLogFilter(wsConn, filter)
Expand Down
8 changes: 1 addition & 7 deletions jsonrpc/endpoints_eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4095,7 +4095,6 @@ func TestGetLogs(t *testing.T) {
tc.ExpectedError = types.NewRPCError(types.InvalidParamsErrorCode, "logs are limited to a 10000 block range")
},
SetupMocks: func(m *mocksWrapper, tc testCase) {
var since *time.Time
m.DbTx.
On("Rollback", context.Background()).
Return(nil).
Expand All @@ -4105,18 +4104,13 @@ func TestGetLogs(t *testing.T) {
On("BeginStateTransaction", context.Background()).
Return(m.DbTx, nil).
Once()

m.State.
On("GetLogs", context.Background(), tc.Filter.FromBlock.Uint64(), tc.Filter.ToBlock.Uint64(), tc.Filter.Addresses, tc.Filter.Topics, tc.Filter.BlockHash, since, m.DbTx).
Return(nil, state.ErrMaxLogsBlockRangeLimitExceeded).
Once()
},
},
{
Name: "Get logs fails due to max log count limit exceeded",
Prepare: func(t *testing.T, tc *testCase) {
tc.Filter = ethereum.FilterQuery{
FromBlock: big.NewInt(1), ToBlock: big.NewInt(10002),
FromBlock: big.NewInt(1), ToBlock: big.NewInt(2),
Addresses: []common.Address{common.HexToAddress("0x111")},
Topics: [][]common.Hash{{common.HexToHash("0x222")}},
}
Expand Down
22 changes: 22 additions & 0 deletions jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,25 @@ func (z *ZKEVMEndpoints) GetFullBlockByHash(hash types.ArgHash, fullTx bool) (in
return rpcBlock, nil
})
}

// GetNativeBlockHashesInRange return the state root for the blocks in range
func (z *ZKEVMEndpoints) GetNativeBlockHashesInRange(filter NativeBlockHashBlockRangeFilter) (interface{}, types.Error) {
return z.txMan.NewDbTxScope(z.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
fromBlockNumber, toBlockNumber, rpcErr := filter.GetNumericBlockNumbers(ctx, z.cfg, z.state, z.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}

nativeBlockHashes, err := z.state.GetNativeBlockHashesInRange(ctx, fromBlockNumber, toBlockNumber, dbTx)
joanestebanr marked this conversation as resolved.
Show resolved Hide resolved
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if errors.Is(err, state.ErrMaxNativeBlockHashBlockRangeLimitExceeded) {
errMsg := fmt.Sprintf(state.ErrMaxNativeBlockHashBlockRangeLimitExceeded.Error(), z.cfg.MaxNativeBlockHashBlockRange)
return RPCErrorResponse(types.InvalidParamsErrorCode, errMsg, nil, false)
} else if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to get block by hash from state", err, true)
}

return nativeBlockHashes, nil
})
}
38 changes: 38 additions & 0 deletions jsonrpc/endpoints_zkevm.openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,24 @@
"$ref": "#/components/schemas/FullBlockOrNull"
}
}
},
{
"name": "zkevm_getNativeBlockHashesInRange",
"summary": "Returns the list of native block hashes.",
"params": [
{
"name": "filter",
"schema": {
"$ref": "#/components/schemas/NativeBlockHashBlockRangeFilter"
}
}
],
"result": {
"name": "filter",
"schema": {
"$ref": "#/components/schemas/NativeBlockHashes"
}
}
}
],
"components": {
Expand Down Expand Up @@ -1115,6 +1133,26 @@
"type": "string",
"description": "Hex representation of a variable length byte array",
"pattern": "^0x([a-fA-F0-9]?)+$"
},
"NativeBlockHashes": {
"title": "native block hashes",
"description": "An array of hashes",
"type": "array",
"items": {
"$ref": "#/components/schemas/Keccak"
}
},
"NativeBlockHashBlockRangeFilter": {
"title": "NativeBlockHashBlockRangeFilter",
"type": "object",
"properties": {
"fromBlock": {
"$ref": "#/components/schemas/BlockNumber"
},
"toBlock": {
"$ref": "#/components/schemas/BlockNumber"
}
}
}
}
}
Expand Down
Loading
Loading