Skip to content

[Feature] Introduce resource limits for JSON-RPC (batch size, response size, address size, timeout) #6632

@317787106

Description

@317787106

Problem Statement

The current JSON-RPC service in java-tron lacks unified constraints on both request scale and response payload size. While this “unbounded” design works in simple scenarios, it introduces significant stability risks when the node is exposed to public traffic or operates under high concurrency.

Specifically:

  • For batch array requests, it may contain a large array of sub-requests. A single request can occupy processing threads for an extended period, degrading overall throughput.
  • Certain APIs (e.g., eth_getLogs, eth_getFilterChanges) may return extremely large result sets. Without response size limits, this can lead to excessive memory consumption and even trigger OOM conditions.
  • Some requests may become long-running due to I/O latency, holding node resources for prolonged periods.
  • Log-related APIs (eth_getLogs, eth_newFilter) currently allow an unbounded number of address filters, significantly increasing query complexity and directly impacting node performance.

These issues are further amplified when handling untrusted external requests. Therefore, it is necessary to introduce fundamental constraint mechanisms at the JSON-RPC layer.

Proposed Solution

The implementation prioritizes low-overhead approaches focusing on early validation and resource control.

  • For all JSON-RPC requests, if its json type is array, the length should be validated during the parsing phase to enable early rejection, avoiding unnecessary downstream processing.
  • For eth_getLogs and eth_newFilter, the address array should be validated before entering query execution, preventing expensive database or index scans.
  • Response size control should not rely on constructing the full payload in memory before validation. Instead, size tracking should be performed incrementally during serialization, using streaming or chunked output to enforce limits and reduce memory pressure.
  • Request execution time should be controlled via asynchronous execution. Requests exceeding a configured timeout should be interrupted.

Error handling should remain consistent with the JSON-RPC specification and align with Ethereum conventions for better client interoperability. Different constraint violations should map to clear error codes:

  • Address limit exceeded → -32602 (JsonRpcInvalidParamsException)
  • Batch size exceeded → -32005
  • Response too large → -32003
  • Request timeout → -32002

Specification

Introduce a set of configurable limits for the JSON-RPC service to constrain request size and response payload, with validation performed as early as possible in the request lifecycle to improve system stability and predictability.

The following configuration items are proposed:

  1. node.jsonrpc.maxBatchSize: Limits the maximum number of sub-requests in a batch JSON-RPC call. Default: 1. Requests exceeding this limit should be rejected immediately after parsing to avoid further processing. Note: values greater than 1 may bypass existing per-method QPS controls.

  2. node.jsonrpc.maxAddressSize: Limits the number of address filters in log-related APIs (eth_getLogs, eth_newFilter). Default: 1000. Validation must occur before query execution to avoid high-cost operations. This follows the same design pattern as the existing node.jsonrpc.maxSubTopics.

  3. node.jsonrpc.maxResponseSize: Limits the maximum size (in bytes) of a JSON-RPC response. Default: 25MB. The size should be tracked incrementally during serialization. Once the limit is exceeded, the response should be terminated immediately instead of continuing to build the full payload.

  4. node.jsonrpc.maxRequestTimeout: Limits the execution time of a JSON-RPC request. Default: 30s. Requests exceeding the timeout should be interrupted and return an error.

All configurations should be integrated into the existing configuration system and follow the current JSON-RPC configuration conventions.

Configuration Changes

Add the following configuration items:

  • node.jsonrpc.maxBatchSize
  • node.jsonrpc.maxResponseSize
  • node.jsonrpc.maxAddressSize
  • node.jsonrpc.maxRequestTimeout

Scope of Impact

  • API/RPC

Breaking Changes
These constraints effectively reduce the impact of oversized or abnormal requests on node resources, mitigate memory risks, and improve stability under high-load conditions. They also enhance the predictability of the JSON-RPC service, making it more suitable for production environments exposed to external users.

Backward Compatibility
There are no changes to API fields, so protocol-level compatibility is preserved. However, additional constraints on request inputs and response outputs may require minor client-side adaptations.

Implementation

Do you have ideas regarding the implementation?
Yes

Are you willing to implement this feature?
Yes

Estimated Complexity
Medium (moderate changes)

Testing Strategy

Focus primarily on boundary conditions, ensuring that exceptions are triggered as early as possible in the request lifecycle.

Test Scenarios

Since request timeout enforcement introduces asynchronous control and may shift execution toward a more serialized model, there could be an impact on performance. Changes in QPS should be evaluated and benchmarked accordingly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions