perf(blockchain): serve chain height and L1 head from memory - #3921
perf(blockchain): serve chain height and L1 head from memory#3921brbrr wants to merge 5 commits into
Conversation
|
Claude finished @brbrr's task in 6m 19s —— View job Review: serve chain height and L1 head from memory
No blockers — the caching direction is sound and the invalidation ordering is right where it matters. Four inline comments; the first is the one I'd want addressed before merge. SummaryImportant
Nits
What I checked and found clean
|
6a4d4f7 to
03c7cbb
Compare
|
Claude finished @brbrr's task in 8m 28s —— View job Review: serve chain height and L1 head from memory
Note on this run: the pushed tree is byte-identical to the revision I reviewed earlier (same blob SHAs for all three files, still one commit SummaryNo blockers. The direction is sound and the invalidation ordering is right where it matters. Important
Nits
Verified clean
Build / test
I could not run |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3921 +/- ##
==========================================
- Coverage 79.28% 79.15% -0.13%
==========================================
Files 464 464
Lines 35793 35833 +40
==========================================
- Hits 28377 28364 -13
- Misses 7407 7460 +53
Partials 9 9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
03c7cbb to
1c2c1a8
Compare
|
Claude finished @brbrr's task in 11m 48s —— View job Review: serve chain height and L1 head from memory
What changed since the last pass. Contrary to the previous run's note, this tree is not identical — four of the six earlier points were taken up: the No blockers. Three new comments below, plus notes on what's still open and two earlier nits I'd now drop. SummaryImportant
Nits
Two earlier nits I'd now drop — I chased both further and they're smaller than they looked:
Verified clean
Build / test
I could not run |
|
Claude finished @brbrr's task in 5m 32s —— View job Review: serve chain height and L1 head from memory
What
I checked the two new tests are non-vacuous: drop the constructor priming (lines 210-214) and the first goes red; drop SummaryNo blockers. Two comments, one carried over. Important
Nits
Verified clean
Build / test
I could not run |
|
Claude finished @brbrr's task in 4m 40s —— View job Review: serve chain height and L1 head from memory
This revision is clean — both remaining items from the previous pass are resolved, and I found nothing new. No inline comments this round. What
|
| Previous finding | Resolution |
|---|---|
update.BlockNumber was a cacheHeads-only nil deref in SetL1Head |
Fixed — blockchain.go:433-436 stores nil (unknown) for a nil update, so both modes agree, plus TestClearingTheL1HeadEmptiesTheCache |
| Field doc named the bypass case but not the concurrency case | Fixed — blockchain.go:109-113 now names both: "written through Blockchain by one writer at a time: a migration on the raw db.KeyValueStore, or a concurrent store and revert, leave it stale" |
On the headsMu mutex I'd raised three times: documenting the constraint instead of enforcing it is a legitimate call here, and the reworded comment now says the thing a future reader actually needs. I re-verified the invariant independently once more and it holds tightly — sync/sync.go:359 is the only Store caller in the tree, both revertTask call sites are conc/stream callbacks on the same verifiers stream (sync/sync.go:191, and :364 from inside storeTask on that goroutine), p2p/sync holds a *blockchain.Blockchain but only ever reads it (Height, GlobalStateRootByBlockNumber, StateAtBlockNumber, SanityCheckNewHeight — grep finds no Store( anywhere under p2p/), and sequencer mode takes the if cfg.Sequencer branch at node/node.go:326 so synchronizer is never constructed. If a second writer ever does appear, the comment is now the thing that flags it.
Verified clean
- Writer inventory (re-grepped).
core.WriteChainHeight/DeleteChainHeightexist only atblockchain/statebackend/block_ops.go:157,199,202, reachable only viaStore/Finalise/RevertHead;core.WriteL1Headonly atblockchain/blockchain.go:425. No migration, pruner, mempool, or p2p path touches either key, so the documented invariant is grep-checkable. - The nil-update path is genuinely correct, not just non-panicking.
WriteL1Head(db, nil)marshals a nil pointer,GetL1Headunmarshals it back to a zeroL1Headwith no error (core/accessors.go:206-220) — so cachingnil("unknown") and letting readers fall through to the database gives exactly the same observable result as the pre-PR code. The test asserts that. - No feed or callback inside the write window.
stateBackend.Store(statebackend.go:79-108) does everything in onedatabase.Writebatch and notifies nobody, so the gap between the commit and the deferredcacheChainHeight()is not observable by any in-process subscriber — a stale-low cache in that window still names a block that exists. - Invalidation ordering.
RevertHeadnils before the commit and refreshes after, so readers fall back for the whole window;Store/Finalisecan only be stale-low. The asymmetry is deliberate and correctly commented. EventFilterdeliberately back on the database (blockchain.go:519-521) with the reason written down —Eventsre-reads latest every call by design, so the constructor must not disagree with it.- Tests are non-vacuous. Spot-checked each: drop the
update == nilguard andTestClearingTheL1HeadEmptiesTheCachepanics; dropdefer b.cacheChainHeight()fromRevertHeadand the newTestRevertsubtest atblockchain_test.go:1294counts a read and fails; drop the constructor priming andTestHeadsAreCachedWhenTheDatabaseIsAlreadyPopulatedfails.headReadCounteronly interceptsGet, which is the right shape for both accessors today. --remote-dbwiring (node/node.go:258-260) is correct and correctly scoped;node.go:427also makes a remote node's synchronizer read-only, so it never reaches the write paths at all.- No value copies of
Blockchainnow that it holdsatomic.Pointerfields — vet's copylocks is clean.
One thing for the PR description (not a code issue)
HeadState() still resolves the head itself (statebackend/statebackend.go:17, deprecated.go:22), which is where every block_id: "latest" state query lands via stateByBlockID — getNonce, getStorageAt, getClass*, estimateFee, simulateTransactions, traceCall. EventFilter.Events (event_filter.go:161) and SetRangeEndBlockToL1Head (event_filter.go:106) also still read the database, the first now deliberately. That's a reasonable scope — Head/HeadsHeader/Height are worth caching on their own, and the constructor priming helps a restarted node immediately — but the title reads as "no more head reads", so a line saying which reads were measured and which remain would save the next person a benchmark.
Build / test
go vet ./blockchain/... — clean (it type-checks the test files, so the new tests compile). On this commit golangci-lint, CodeQL, Code Quality, Dependency Review and Test Documentation Build are green; Juno Test, Deploy and Test on Dev and Container Security were still in progress when I finished.
I could not run go test ./blockchain/ -race — the command needed approval in this environment — so I verified the new tests by reading them rather than executing them. Please confirm Juno Test goes green.
• branch perf/blockchain-head-cache
EgeCaner
left a comment
There was a problem hiding this comment.
Here I worry about cache DB consistency, lets somehow make it more robust. Also I guess we can update the caches directly after writes without requiring to read the ChainHeight or L1Head back from DB in order to populate the cache
| @@ -1,8 +1,9 @@ | |||
| package blockchain | |||
|
|
|||
| import ( | |||
There was a problem hiding this comment.
Here I worry about the tight window where db is updated and cache is not, we might have to synchronize them, or invalidate cache before starting writing to DB, so we don't read stale cache value, at the moment nothing hints cache is stale. We will still have concurrent DB read/write case but it might still worth
There was a problem hiding this comment.
A reader does a lock-free atomic load, so a reader is safe as it is. The pair {database commit, cache refresh} in a writer is not atomic, but the node has only one writer.
Store and RevertHead are stream callbacks on the same stream, and storeTask calls revertTask on that same goroutine. There is only one caller of Store in the tree.
Thus a mutex would protect an interleave that no call path can cause, and it would tell a future reader that this package expects concurrent writers. I prefer to keep the rule in the field comment, where a grep can check it. It would make sense adding a mutex when we'll get a second writer.
You also asked to set the cache directly instead of a read of the database. Store and Finalise can do this, because it writes exactly block.Number. RevertHead cannot: Blockchain does not know the new height unless it reads the database or repeats the rule "delete at genesis, else N-1" in a second place. RevertHead also cannot calculate the new height when the cache is already nil. Thus the choice is one read-back rule for all three methods, or a mixed scheme with the height rule in two places. I decided to keep the read-back.
|
Claude finished @brbrr's task in 3m 47s —— View job Review: serve chain height and L1 head from memory
This revision is clean — I found nothing new. No inline comments this round. What
|
263a9ea to
e92df15
Compare
|
Claude finished @brbrr's task in 4m 22s —— View job Review: serve chain height and L1 head from memory
This push is a rebase, not a code change. The merge base moved from No blockers, and nothing new. Since the tree is unchanged, the useful work this round was re-verifying the invariants against the new base rather than restating findings, so I've posted no inline comments. Re-verified against
|
|
Claude finished @brbrr's task in 3m 39s —— View job Review: serve chain height and L1 head from memory
This push is a merge, not a code change — nothing new to review.
Where this PR standsNo blockers across all prior passes. The one recurring Important item — {backend write, cache refresh} in The one non-code item still worth a line in the PR description: |
|
some K6 experiements: 1. Sequential latency —
|
| method | cached read | main µs | pr µs | Δ |
|---|---|---|---|---|
blockNumber |
Height |
47 | 44 | -3.0 |
blockHashAndNumber |
HeadsHeader |
55 | 52 | -3.0 |
getTransactionStatus |
L1Head |
64 | 61 | -3.5 |
getBlockTransactionCount (latest) |
Height |
54 | 52 | -2.0 |
getStorageAt (by number) |
none — reference | 62 | 62 | 0.0 |
chainId |
none — control | 44 | 44 | 0.0 |
Both no-op rows sit at zero; all five affected methods move -1.5 to -3.5 µs.
2. Under load — --vus 200, one call per request
| method | cached read | main req/s | pr req/s | Δ req/s | main p99 ms | pr p99 ms | Δ p99 |
|---|---|---|---|---|---|---|---|
blockNumber |
Height |
58,967 | 60,214 | +1.2% | 16.3 | 15.3 | -4.9% |
blockHashAndNumber |
HeadsHeader |
53,352 | 54,249 | -1.0% | 18.6 | 18.4 | -0.8% |
getBlockTransactionCount (latest) |
Height |
53,835 | 53,062 | -2.0% | 18.6 | 18.3 | +0.2% |
getTransactionStatus |
L1Head |
44,787 | 45,409 | -1.8% | 22.7 | 22.2 | -1.3% |
getStorageAt (by number) |
none — reference | 44,633 | 44,203 | -3.7% | 22.8 | 22.8 | +0.1% |
User description
This caches the reads behind
Height(),Head(),HeadsHeader()and theEventFilterupper bound.HeadState(),EventFilter.EventsandSetRangeEndBlockToL1Headstill read the database on each call -Eventsdeliberately, so its range logic and retention check use one source of "latest".fsepolia based benchamk results:
heads_headerheads_headerheads_headerheightheightheightl1_headl1_headl1_headPR Type
Enhancement, Tests
Description
Cache chain height and L1 head in memory via
atomic.PointerAdd
WithRemoteDatabaseoption to disable caching for remote-db followersRefresh/clear cached height on
Store,Finalise,RevertHead,StoreGenesisDeep-copy L1 head on cache to isolate it from caller mutations
Wire
--remote-dbflag toblockchain.WithRemoteDatabasein node setupExtensive new tests covering caching, isolation, and remote-db fallback
File Walkthrough
blockchain.go
Cache chain height and L1 head in memoryblockchain/blockchain.go
chainHeightandl1Headatomic pointer fields toBlockchain, plus acacheHeadsflagWithRemoteDatabaseoption to disable caching for databases writtenby another process
height()/cacheChainHeight()/cacheL1Head()helpersHeight(),Head(),HeadsHeader(),L1Head(),SetL1Head(),Store(),Finalise(),RevertHead()to read/refresh the in-memory cacheinstead of always hitting the database
EventFilter's height read against the database to share the"latest" source with
Eventsnode.go
Wire remote-db flag to blockchain optionnode/node.go
blockchain.WithRemoteDatabase()option whendbIsRemoteis trueblockchain_test.go
Add tests for head caching behaviorblockchain/blockchain_test.go
chainHeightCountertoheadReadCounter, tracking both chainheight and L1 head reads
hitting the database
cache on nil update, and failed writes not reaching cache/subscribers
behavior, and cache invalidation/refresh across revert scenarios