core: refresh BAL Metrics() snapshot after writeBlockWithState

The first Metrics() call inside calcAndVerifyRoot snapshots accountReadNS
and storageReadNS into the cached AccountReadTime/StorageReadTime fields.
But commitAccount (called from writeBlockWithState's CommitWithUpdate
path) increments storageReadNS *after* that snapshot, so reading
m.StorageReadTime later would silently drop those reads.

Re-call Metrics() before reading the read-time fields so the cache
reflects the post-commit atomics. Other metric fields (AccountUpdate,
AccountCommits, etc.) are written directly to s.metrics elsewhere and
remain untouched by Metrics().

Found via the metric-correctness audit.
This commit is contained in:
CPerezz 2026-04-30 23:14:35 +02:00
parent 1afcea992c
commit 16e98f5d93
No known key found for this signature in database
GPG key ID: 62045F34B97177DD

View file

@ -677,11 +677,12 @@ func (bc *BlockChain) processBlockWithAccessList(parentRoot common.Hash, block *
stats.DatabaseCommit = m.TrieDBCommits stats.DatabaseCommit = m.TrieDBCommits
stats.Prefetch = m.StatePrefetch stats.Prefetch = m.StatePrefetch
} }
// Read durations: sum across all three sources (per-tx execution, BAL // Refresh BAL read-time cache: commitAccount runs storage reads during
// state-transition recomputation, prefetcher async fetches). This is // writeBlockWithState, after the first Metrics() snapshot.
// sum-of-CPU-time across parallel workers, not wall-clock — it can stateTransition.Metrics()
// exceed TotalTime, which is the intended interpretation under parallel
// execution: "total CPU-time spent reading state across the block". // Sum read times across per-tx execution, BAL state-transition, and
// prefetcher async fetches. Sum-of-CPU-time, not wall-clock.
var prefetchAccountReads, prefetchStorageReads time.Duration var prefetchAccountReads, prefetchStorageReads time.Duration
if pr, ok := prefetchReader.(interface { if pr, ok := prefetchReader.(interface {
PrefetchReadTimes() (time.Duration, time.Duration) PrefetchReadTimes() (time.Duration, time.Duration)