Skip to content

Commit 90d182b

Browse files
committed
core: track state size in blockchain.go
Signed-off-by: jsvisa <[email protected]>
1 parent 4da9e8a commit 90d182b

File tree

4 files changed

+280
-158
lines changed

4 files changed

+280
-158
lines changed

core/blockchain.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ type BlockChain struct {
331331
processor Processor // Block transaction processor interface
332332
logger *tracing.Hooks
333333

334+
stateSizeGen *state.StateSizeGenerator // State size tracking
335+
334336
lastForkReadyAlert time.Time // Last time there was a fork readiness print out
335337
}
336338

@@ -523,6 +525,11 @@ func NewBlockChain(db ethdb.Database, genesis *Genesis, engine consensus.Engine,
523525
if bc.cfg.TxLookupLimit >= 0 {
524526
bc.txIndexer = newTxIndexer(uint64(bc.cfg.TxLookupLimit), bc)
525527
}
528+
529+
// Start state size tracker
530+
bc.stateSizeGen = state.NewStateSizeGenerator(bc.statedb.DiskDB(), bc.triedb, head.Root)
531+
log.Info("Started state size generator", "root", head.Root)
532+
526533
return bc, nil
527534
}
528535

@@ -1310,6 +1317,12 @@ func (bc *BlockChain) Stop() {
13101317
}
13111318
}
13121319
}
1320+
// Stop state size generator if running
1321+
if bc.stateSizeGen != nil {
1322+
bc.stateSizeGen.Stop()
1323+
log.Info("Stopped state size generator")
1324+
}
1325+
13131326
// Allow tracers to clean-up and release resources.
13141327
if bc.logger != nil && bc.logger.OnClose != nil {
13151328
bc.logger.OnClose()
@@ -1583,10 +1596,15 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
15831596
log.Crit("Failed to write block into disk", "err", err)
15841597
}
15851598
// Commit all cached state changes into underlying memory database.
1586-
root, err := statedb.Commit(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()), bc.chainConfig.IsCancun(block.Number(), block.Time()))
1599+
root, stateUpdate, err := statedb.CommitWithUpdate(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()), bc.chainConfig.IsCancun(block.Number(), block.Time()))
15871600
if err != nil {
15881601
return err
15891602
}
1603+
1604+
// Track state size changes if generator is running
1605+
if bc.stateSizeGen != nil && stateUpdate != nil {
1606+
bc.stateSizeGen.Track(stateUpdate)
1607+
}
15901608
// If node is running in path mode, skip explicit gc operation
15911609
// which is unnecessary in this mode.
15921610
if bc.triedb.Scheme() == rawdb.PathScheme {

core/state/database.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ func (db *CachingDB) TrieDB() *triedb.Database {
269269
return db.triedb
270270
}
271271

272+
// DiskDB returns the underlying disk database for direct access.
273+
func (db *CachingDB) DiskDB() ethdb.KeyValueStore {
274+
return db.disk
275+
}
276+
272277
// PointCache returns the cache of evaluated curve points.
273278
func (db *CachingDB) PointCache() *utils.PointCache {
274279
return db.pointCache

core/state/statedb.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,6 @@ type StateDB struct {
138138
// State witness if cross validation is needed
139139
witness *stateless.Witness
140140

141-
// State size tracking
142-
stateSizeGen *stateSizeGenerator
143-
144141
// Measurements gathered during execution for debugging purposes
145142
AccountReads time.Duration
146143
AccountHashes time.Duration
@@ -189,14 +186,6 @@ func NewWithReader(root common.Hash, db Database, reader Reader) (*StateDB, erro
189186
sdb.accessEvents = NewAccessEvents(db.PointCache())
190187
}
191188

192-
// Initialize state size tracking
193-
if cachingDB, ok := db.(*CachingDB); ok {
194-
sdb.stateSizeGen = newStateSizeGenerator(cachingDB.disk, db, root)
195-
if !db.TrieDB().IsVerkle() {
196-
sdb.stateSizeGen.run()
197-
}
198-
}
199-
200189
return sdb, nil
201190
}
202191

@@ -1340,11 +1329,6 @@ func (s *StateDB) commitAndFlush(block uint64, deleteEmptyObjects bool, noStorag
13401329
return nil, err
13411330
}
13421331
s.TrieDBCommits += time.Since(start)
1343-
1344-
// Update state size metrics
1345-
if s.stateSizeGen != nil {
1346-
s.stateSizeGen.updateMetrics(ret)
1347-
}
13481332
}
13491333
}
13501334
s.reader, _ = s.db.Reader(s.originalRoot)
@@ -1373,6 +1357,16 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool, noStorageWiping
13731357
return ret.root, nil
13741358
}
13751359

1360+
// CommitWithUpdate writes the state mutations and returns both the root hash and the state update.
1361+
// This is useful for tracking state changes at the blockchain level.
1362+
func (s *StateDB) CommitWithUpdate(block uint64, deleteEmptyObjects bool, noStorageWiping bool) (common.Hash, *stateUpdate, error) {
1363+
ret, err := s.commitAndFlush(block, deleteEmptyObjects, noStorageWiping)
1364+
if err != nil {
1365+
return common.Hash{}, nil, err
1366+
}
1367+
return ret.root, ret, nil
1368+
}
1369+
13761370
// Prepare handles the preparatory steps for executing a state transition with.
13771371
// This method must be invoked before state transition.
13781372
//

0 commit comments

Comments
 (0)