refactor(jsonrpc): centralize block selector parsing into JsonRpcApiUtil

Closed
0X
5mo ago
+691-229

What does this PR do?
Centralize block tag and number resolution into JsonRpcApiUtil to eliminate code duplication, remove Wallet's reverse dependency on the JSON-RPC layer, and simplify complex logic in LogFilterWrapper.

Why are these changes required?
JSON-RPC block selector parsing (tag-to-block-number resolution) is scattered across three layers (Wallet, JsonRpcApiUtil, TronJsonRpcImpl) with duplicated, inconsistent logic. The core Wallet class reverse-depends on the jsonrpc API layer for tag constants, and a -1 sentinel value for "latest" forces every caller to re-interpret results differently. This makes the code fragile and hard to reason about.

This PR has been tested by:

  • Unit Tests
  • Manual Testing

Follow up

Extra details


Summary by cubic

Centralizes JSON‑RPC block selector parsing in JsonRpcApiUtil, adds strict tag/hex handling (incl. unsupported safe), and aligns “latest” behavior across block and state reads. Simplifies log filter ranges and removes Wallet JSON‑RPC helpers.

  • Refactors
    • Added JsonRpcApiUtil tag constants/errors and helpers: isBlockTag, parseBlockTag, parseBlockNumber (strict 0x hex; overflow rejected via longValueExact). “latest” resolves to head via wallet.getHeadBlockNum(), while actual block reads use wallet.getNowBlock().
    • Centralized block selection in TronJsonRpcImpl#getBlockByNumOrTag; updated eth_getBlockTransactionCountByNumber, eth_getBlockByNumber, getTransactionByBlockNumberAndIndex, and getBlockReceipts to use it; unified errors, including safe (“TAG [earliest | pending | finalized | safe] not supported”).
    • Enforced requireLatestBlockTag for state reads (getTrxBalance, getStorageAt, getABIOfSmartContract, eth_call): reject numeric quantities (“QUANTITY not supported…”) and malformed hex (“invalid block number”); only “latest” accepted.
    • Removed Wallet JSON‑RPC helpers and the “-1 means latest” sentinel; added getHeadBlockNum().
    • LogFilterWrapper: four strategies; fromBlock="latest" uses a snapshot (current head); toBlock="latest" is open‑ended (tracks future blocks); consistent range validation.
    • Tests: expanded interface coverage (blocks/txs/receipts/logs/state reads/eth_call object form), safe tag rejections, and long overflow cases; added LogFilterWrapperStrategyTest.

Summary by CodeRabbit

  • Refactor
    • Consolidated and standardized JSON-RPC block identifier parsing across API endpoints for improved consistency.
    • Enhanced block tag recognition and error handling with clearer messages for unsupported or invalid block parameters.
    • Improved block filtering logic for more reliable range-based queries.