refactor(jsonrpc): centralize block selector parsing into JsonRpcApiUtil
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
JsonRpcApiUtiltag constants/errors and helpers:isBlockTag,parseBlockTag,parseBlockNumber(strict0xhex; overflow rejected vialongValueExact). “latest” resolves to head viawallet.getHeadBlockNum(), while actual block reads usewallet.getNowBlock(). - Centralized block selection in
TronJsonRpcImpl#getBlockByNumOrTag; updatedeth_getBlockTransactionCountByNumber,eth_getBlockByNumber,getTransactionByBlockNumberAndIndex, andgetBlockReceiptsto use it; unified errors, includingsafe(“TAG [earliest | pending | finalized | safe] not supported”). - Enforced
requireLatestBlockTagfor state reads (getTrxBalance,getStorageAt,getABIOfSmartContract,eth_call): reject numeric quantities (“QUANTITY not supported…”) and malformed hex (“invalid block number”); only “latest” accepted. - Removed
WalletJSON‑RPC helpers and the “-1 means latest” sentinel; addedgetHeadBlockNum(). 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_callobject form),safetag rejections, and long overflow cases; addedLogFilterWrapperStrategyTest.
- Added
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.