trie: track progress of walking trie nodes during contract

This commit is contained in:
lightclient 2026-02-24 13:32:11 +00:00
parent 3b8e7b6221
commit 1a0a4c4f61
No known key found for this signature in database
GPG key ID: 91289C7FE4ADADBD
2 changed files with 18 additions and 4 deletions

View file

@ -67,6 +67,7 @@ type inspector struct {
dumpMu sync.Mutex dumpMu sync.Mutex
dumpBuf *bufio.Writer dumpBuf *bufio.Writer
dumpFile *os.File dumpFile *os.File
storageRecordsWritten atomic.Uint64
errMu sync.Mutex errMu sync.Mutex
err error err error
@ -116,7 +117,7 @@ func Inspect(triedb database.NodeDatabase, root common.Hash, config *InspectConf
select { select {
case <-ticker.C: case <-ticker.C:
accountNodes := in.accountStat.TotalNodes() accountNodes := in.accountStat.TotalNodes()
storageRecords := atomic.LoadUint64(&in.storageRecordsWritten) storageRecords := in.storageRecordsWritten.Load()
log.Info("Inspecting trie", log.Info("Inspecting trie",
"accountNodes", accountNodes, "accountNodes", accountNodes,
"storageRecords", storageRecords, "storageRecords", storageRecords,
@ -201,15 +202,13 @@ func InspectContract(triedb database.NodeDatabase, db ethdb.Database, stateRoot
}) })
// Goroutine 2: Storage trie walk using the existing inspector. // Goroutine 2: Storage trie walk using the existing inspector.
var storageStat *LevelStats storageStat := NewLevelStats()
g.Go(func() error { g.Go(func() error {
owner := accountHash owner := accountHash
storage, err := New(StorageTrieID(stateRoot, owner, account.Root), triedb) storage, err := New(StorageTrieID(stateRoot, owner, account.Root), triedb)
if err != nil { if err != nil {
return fmt.Errorf("failed to open storage trie: %w", err) return fmt.Errorf("failed to open storage trie: %w", err)
} }
storageStat = NewLevelStats()
in := &inspector{ in := &inspector{
triedb: triedb, triedb: triedb,
root: stateRoot, root: stateRoot,
@ -238,6 +237,7 @@ func InspectContract(triedb database.NodeDatabase, db ethdb.Database, stateRoot
case <-ticker.C: case <-ticker.C:
log.Info("Inspecting contract", log.Info("Inspecting contract",
"snapSlots", snapSlots.Load(), "snapSlots", snapSlots.Load(),
"trieNodes", storageStat.TotalNodes(),
"elapsed", common.PrettyDuration(time.Since(start))) "elapsed", common.PrettyDuration(time.Since(start)))
case <-done: case <-done:
return return
@ -403,6 +403,11 @@ func (in *inspector) writeDumpRecord(owner common.Hash, s *LevelStats) {
if err != nil { if err != nil {
in.setError(fmt.Errorf("failed writing trie dump record: %w", err)) in.setError(fmt.Errorf("failed writing trie dump record: %w", err))
} }
// Increment counter for storage tries only (not for account trie)
if owner != (common.Hash{}) {
in.storageRecordsWritten.Add(1)
}
} }
// inspect walks the trie rooted at n and records node statistics into stat. // inspect walks the trie rooted at n and records node statistics into stat.

View file

@ -48,6 +48,15 @@ func (s *LevelStats) MaxDepth() int {
return depth return depth
} }
// TotalNodes returns the total number of nodes across all levels and types.
func (s *LevelStats) TotalNodes() uint64 {
var total uint64
for i := range s.level {
total += s.level[i].short.Load() + s.level[i].full.Load() + s.level[i].value.Load()
}
return total
}
// add increases the node count by one for the specified node type and depth. // add increases the node count by one for the specified node type and depth.
func (s *LevelStats) add(n node, depth uint32) { func (s *LevelStats) add(n node, depth uint32) {
d := int(depth) d := int(depth)