Skip to content

rpc/jsonrpc: default omitted block to latest on state methods#21586

Merged
lupin012 merged 5 commits into
erigontech:mainfrom
MysticRyuujin:fix/default-block-latest-state-methods
Jun 15, 2026
Merged

rpc/jsonrpc: default omitted block to latest on state methods#21586
lupin012 merged 5 commits into
erigontech:mainfrom
MysticRyuujin:fix/default-block-latest-state-methods

Conversation

@MysticRyuujin

@MysticRyuujin MysticRyuujin commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Make the Block parameter optional (defaulting to latest when omitted) on the six state-reading methods:

  • eth_getBalance
  • eth_getCode
  • eth_getStorageAt
  • eth_getTransactionCount
  • eth_getProof
  • eth_getStorageValues

Each took a value-type rpc.BlockNumberOrHash, so omitting the block parameter failed with -32602 missing value for required argument N — Erigon's rpc package (like go-ethereum's) only permits omitting a trailing argument when it is a pointer. This changes the six handlers (and the EthAPI interface) to *rpc.BlockNumberOrHash and defaults nil to latest via a small orLatest helper, reusing the existing latestNumOrHash var. eth_call already behaves this way.

Internal callers of the interface are updated accordingly (rpc/contracts/direct_backend.go, rpc/mcp/*) along with the affected tests.

Motivation

This aligns Erigon with ethereum/execution-apis#812 by @manusw7, which marks the Block parameter required: false (default latest) on these methods. Full credit for the proposal and rationale to @manusw7.

Testing

  • go vet ./rpc/jsonrpc/ passes (handlers + updated tests compile).
  • Verified against the hive rpc-compat simulator with default-block conformance fixtures (block parameter omitted): a locally-built Erigon with this change returns the latest-block values and all six pass (tests=7 failed=0). Before the change, all six failed.

Related

eth_getBalance, eth_getCode, eth_getStorageAt, eth_getTransactionCount,
eth_getProof and eth_getStorageValues took a value-type
rpc.BlockNumberOrHash, so omitting the block parameter failed with
-32602 'missing value for required argument N' (the rpc package only
permits omitting a trailing pointer argument). Change them to
*rpc.BlockNumberOrHash and default nil to latest via a small orLatest
helper, matching eth_call and execution-apis (Block required:false,
default 'latest'). Update internal callers (mcp, contracts).
Adjust the eth_getStorageAt/getStorageValues/getProof/getBalance test
callers to pass *rpc.BlockNumberOrHash after the signature change.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Erigon’s JSON-RPC state-reading methods to treat the Block parameter as optional (defaulting to latest when omitted), aligning behavior with the Execution APIs spec for eth_getBalance, eth_getCode, eth_getStorageAt, eth_getTransactionCount, eth_getProof, and eth_getStorageValues.

Changes:

  • Switch the six affected EthAPI methods (and implementations) from rpc.BlockNumberOrHash to *rpc.BlockNumberOrHash so the trailing argument can be omitted by JSON-RPC callers.
  • Add an orLatest helper to default nil block selectors to latest.
  • Update internal callers (MCP server + contract backend) and adjust tests to pass pointers.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
rpc/mcp/resources.go Updates MCP resource handler calls to pass *BlockNumberOrHash (latest).
rpc/mcp/mcp.go Updates MCP tool handlers to pass *BlockNumberOrHash to state methods.
rpc/jsonrpc/eth_call.go Introduces orLatest helper and updates GetProof to accept optional block selector.
rpc/jsonrpc/eth_call_test.go Updates GetProof tests to pass a *BlockNumberOrHash.
rpc/jsonrpc/eth_api.go Changes EthAPI interface signatures for the six methods to use pointers.
rpc/jsonrpc/eth_api_test.go Updates tests for pointer-based signatures; adds bnhPtr helper.
rpc/jsonrpc/eth_accounts.go Updates state-reading method implementations to accept optional block selector and default via orLatest.
rpc/jsonrpc/corner_cases_support_test.go Updates GetBalance test call to pass *BlockNumberOrHash.
rpc/contracts/direct_backend.go Updates internal backend calls (GetCode, GetTransactionCount) to pass *BlockNumberOrHash.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rpc/jsonrpc/eth_call.go
Comment on lines +59 to +63
// orLatest resolves an optional block selector, defaulting to the latest block
// when the caller omitted the parameter (nil). Used by the state-reading methods
// whose Block parameter is optional per execution-apis (default 'latest').
func orLatest(blockNrOrHash *rpc.BlockNumberOrHash) rpc.BlockNumberOrHash {
if blockNrOrHash != nil {
Assert that calling each state method with a nil block selector returns the same
result as an explicit latest selector, exercising the orLatest(nil) path.
@MysticRyuujin

Copy link
Copy Markdown
Contributor Author

Good call — added in c4bef18. TestStateMethods_OmittedBlockDefaultsToLatest (in rpc/jsonrpc/eth_api_test.go) calls each of the six state methods with a nil block selector and asserts the result matches an explicit latest selector, exercising the orLatest(nil) path directly so a future caller passing a zero-value (or a refactor of the default) is caught. Verified locally: go test ./rpc/jsonrpc/ -run TestStateMethods_OmittedBlockDefaultsToLatest passes.

@yperbasis yperbasis added the RPC label Jun 3, 2026
@MysticRyuujin

Copy link
Copy Markdown
Contributor Author

cc @canepat

@lupin012 lupin012 added this pull request to the merge queue Jun 15, 2026
Merged via the queue into erigontech:main with commit 80df5e6 Jun 15, 2026
89 checks passed
lupin012 added a commit to erigontech/rpc-tests that referenced this pull request Jun 20, 2026
Tests for erigontech/erigon#21586: eth_getBalance, eth_getCode,
eth_getStorageAt, eth_getTransactionCount, eth_getProof,
eth_getStorageValues now accept an omitted block parameter (defaults
to latest). Each fixture omits the block param and carries
metadata.latest=true so it runs under -L.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants