[Bug] State queries (eth_getBalance, eth_getTransactionCount, eth_getCode, eth_getStorageAt) treat "earliest" as "latest"
- Dominant language
- Go
- Stars
- 164
- Forks
- 213
- Avg merge
- 3d 58m
- Merged PRs (30d)
- 12
Description
## Type
- [x] Bug
- [ ] Feature
- [ ] Proposal / Discussion
## Summary
State queries (`eth_getBalance`, `eth_getTransactionCount`, `eth_getCode`, `eth_getStorageAt`) treat the `earliest` block tag as `latest`. They derive the gRPC query height from `BlockNumber.Int64()`, which maps every negative sentinel (including `earliest` = -5) to 0, i.e. "use the latest height":
https://github.com/cosmos/evm/blob/469142db41aee479102cf878f8ac2bbe2b0c7a22/rpc/types/block.go#L110-L118
```go
func (bn BlockNumber) Int64() int64 {
if bn < 0 {
return 0
} else if bn == 0 {
return 1
}
return int64(bn)
}
```
Block queries handle the tag correctly because `getHeightByBlockNum` special-cases `EthEarliestBlockNumber` via `status.SyncInfo.EarliestBlockHeight`, so `eth_getBlockByNumber("earliest")` returns block 1 while `eth_getBalance(addr, "earliest")` returns the head state.
## Reproduction (for bugs)
`main` (`469142d`), `./local_node.sh -y`, after sending a few txs from dev0 (`0xc6fe…e101`):
```
eth_getBlockByNumber ["earliest", false] -> number: 0x1
eth_getBalance ["0xc6fe…e101", "0x1"] -> 0x3635c9adc5dea00000 (genesis balance)
eth_getBalance ["0xc6fe…e101", "earliest"] -> 0x3635c9ad2132058000 (== "latest")
eth_getTransactionCount ["0xc6fe…e101", "0x1"] -> 0x0
eth_getTransactionCount ["0xc6fe…e101", "earliest"] -> 0x3 (== "latest")
```
## Impact
Low; spec deviation only (`earliest` must return the genesis/earliest state, as geth does). Fix: resolve `EthEarliestBlockNumber` in the state queries with the same helper the block queries use, and guard `GetTransactionCount`'s future-height check accordingly. Can send a PR if wanted.
## Related
- #1279 (`eth_getProof` has the same tag-handling problem in a different form)
## Checklist
- [x] Linked to a GitHub Issue (or this is the Issue)
- [x] Repro steps included (for bugs)
- [x] Impact described
- [x] I understand minor typo/style doc fixes will not be accepted
Contributor guide
Research direction
Start with rpc/types/block.go and trace the state-query handlers for eth_getBalance, eth_getTransactionCount, eth_getCode, and eth_getStorageAt, comparing them with getHeightByBlockNum and its earliest-block handling. Reproduce the calls against ./local_node.sh -y, then verify that the earliest tag resolves to the earliest state while future-height checks remain correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100