perf(state): avoid full header decode in deprecated state backend
Extends the partial-header-decode optimization from #3782 to the
production-default deprecatedStateBackend, avoiding full header decodes
(including EventsBloom) when only one field or an existence check is needed.
HeadState: dropped the discardedGetBlockHeaderByNumberfetch;GetChainHeightis now only used as the head-exists guard.StateAtBlockNumber: gated onRequireStateRetainedByBlockNumber, which reads only the block hash + hash→number index.StateAtBlockHash: resolves the block number via the hash→number index without decoding the header.
Why
These paths back latest/head and historical-block state RPCs (getNonce,
getStorageAt, getClass*, call, estimateFee, tracing, etc.). The full
header decode was pure overhead on the hot read path.
Behavior note
HeadState and StateAtBlockHash no longer perform a full header decode
as an incidental DB-corruption/header-existence guard. The GetChainHeight
check and the hash→number index lookup remain as guards, which is correct
for a consistent DB.
Tests
- Added focused tests for the new partial accessor and retention guard.
Codecov Report
:x: Patch coverage is 91.30435% with 2 lines in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 75.24%. Comparing base (7dbcfd9) to head (7bd87fa).
:warning: Report is 1 commits behind head on main.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| blockchain/statebackend/deprecated.go | 75.00% | 0 Missing and 1 partial :warning: |
| pruner/retention.go | 87.50% | 0 Missing and 1 partial :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## main #3789 +/- ##
==========================================
- Coverage 75.34% 75.24% -0.10%
==========================================
Files 438 438
Lines 39528 39569 +41
==========================================
- Hits 29781 29773 -8
- Misses 7676 7707 +31
- Partials 2071 2089 +18
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Claude finished @ongyimeng's task in 2m 32s —— View job
PR Review: perf(state): avoid full header decode in deprecated state backend
- [x] Gather context (diff, changed files)
- [x] Review
core/accessors.gopartial accessor - [x] Review
pruner/retention.goretention guard - [x] Review
blockchain/statebackend/deprecated.gochanges - [x] Verify retention/pruning semantics
- [x] Review tests
- [x] Post findings
I traced the retention semantics end-to-end to confirm the behavior change is safe. The optimization is correct — no blockers.
Correctness verification
The key question is whether dropping the full header decode weakens the state-retention guard. It doesn't:
- The pruner signals state pruned by deleting the hash→number index (
DeleteBlockHeaderNumberByHashinpruner/accessors.go:183,203), while keeping the header-by-number (needed for block-level data). So the index lookup is the retention signal. StateAtBlockNumber→RequireStateRetainedByBlockNumber: doesGetBlockHeaderHashByNumber(header exists) +GetBlockHeaderNumberByHash(hash)(the retention signal). Same two lookups as the oldHeaderByNumberIfStateRetained, just a partial decode. ✅StateAtBlockHash→BlockNumberByHashIfStateRetained: the old path resolved the index then decoded the header only to readheader.Number; on a consistent DBindex[hash] == header.Number, so returning the index value directly is equivalent. The dropped header decode was an incidental corruption guard, correctly called out in the PR description. ✅HeadState: dropping the discardedGetBlockHeaderByNumber(height)is pure win —GetChainHeightremains the head-exists guard. ✅- Partial CBOR decode into
struct{ Hash *felt.Felt }is sound:Headeruses canonical named-field (map) CBOR, so unknown fields are ignored — same established pattern asGetGlobalStateRootByBlockNumber. The addednil-check makes a corrupt/missingHashstricter than before, not weaker. ✅ - Old
HeaderByNumberIfStateRetained/HeaderByHashIfStateRetainedare still used by the non-deprecatedstateBackend(which genuinely needsGlobalStateRoot/Number), so they're not dead code and the scoping is right.
Findings
Blocker: none.
Important: none.
Nit (posted inline):
core/accessors_test.go:299— the table-driven refactor dropped the old"missing global state root"subtest, so theheader.GlobalStateRoot == nil/header.Hash == nilguards are now uncovered (matches Codecov's missing-lines report). Worth re-adding a malformed-header case.pruner/retention.go:75— doc comments onRequireStateRetainedByBlockNumberandBlockNumberByHashIfStateRetaineddon't start with the function name (unlike the neighbouring helpers); minor godoc/revive consistency.
Optional follow-up (out of scope): the non-deprecated stateBackend.StateAtBlockNumber (statebackend.go:37) could adopt the same GetGlobalStateRootByBlockNumber partial decode for the same win.
· branch perf/deprecated-state-partial-header
Claude finished @ongyimeng's task in 56s —— View job
PR Re-review: perf(state): avoid full header decode in deprecated state backend
The two new commits address both nits from my previous review — re-checked the current tree.
- [x] Verify doc-comment fix (
pruner/retention.go) - [x] Verify missing-field test coverage (
core/accessors_test.go) - [x] Re-confirm correctness of the optimization
Follow-ups from prior review — both resolved ✅
- Doc convention (
7bd87fa):RequireStateRetainedByBlockNumberandBlockNumberByHashIfStateRetainednow start with the function name, matching the neighbouring helpers and revile/godoc convention. ✅ - Test coverage (
a5da185):TestPartialBlockHeaderAccessorsByNumberis now table-driven over both partial accessors and re-adds a"missing field returns error"case that writes a header omitting the target field, restoring coverage of theheader.Hash == nil/header.GlobalStateRoot == nilguards that the earlier refactor had dropped. ✅
Correctness (unchanged from prior review)
The optimization remains sound. Retention semantics are preserved:
RequireStateRetainedByBlockNumberdoes the same two lookups as the oldHeaderByNumberIfStateRetained(header-hash exists + hash→number index = the retention signal), just with a partial CBOR decode.BlockNumberByHashIfStateRetainedreturns the index value directly; on a consistent DBindex[hash] == header.Number, so it's equivalent to the old header-decode path.HeadStatedropping the discardedGetBlockHeaderByNumberis pure win;GetChainHeightremains the head-exists guard.- Partial named-field CBOR decode is the established pattern (
GetGlobalStateRootByBlockNumber); the addednil-check makes a corruptHashstricter, not weaker.
Blocker: none · Important: none · Nit: none outstanding.
LGTM — ready to merge once CI is green. (I couldn't run go test in this environment due to sandbox permissions, but CI covers it.)
Optional out-of-scope follow-up still stands: the non-deprecated stateBackend.StateAtBlockNumber could adopt the same partial-decode win.
· branch perf/deprecated-state-partial-header