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
dumpBuf *bufio.Writer
dumpFile *os.File
storageRecordsWritten atomic.Uint64
errMu sync.Mutex
err error
@ -116,7 +117,7 @@ func Inspect(triedb database.NodeDatabase, root common.Hash, config *InspectConf
select {
case <-ticker.C:
accountNodes := in.accountStat.TotalNodes()
storageRecords := atomic.LoadUint64(&in.storageRecordsWritten)
storageRecords := in.storageRecordsWritten.Load()
log.Info("Inspecting trie",
"accountNodes", accountNodes,
"storageRecords", storageRecords,
@ -201,15 +202,13 @@ func InspectContract(triedb database.NodeDatabase, db ethdb.Database, stateRoot
})
// Goroutine 2: Storage trie walk using the existing inspector.
var storageStat *LevelStats
storageStat := NewLevelStats()
g.Go(func() error {
owner := accountHash
storage, err := New(StorageTrieID(stateRoot, owner, account.Root), triedb)
if err != nil {
return fmt.Errorf("failed to open storage trie: %w", err)
}
storageStat = NewLevelStats()
in := &inspector{
triedb: triedb,
root: stateRoot,
@ -238,6 +237,7 @@ func InspectContract(triedb database.NodeDatabase, db ethdb.Database, stateRoot
case <-ticker.C:
log.Info("Inspecting contract",
"snapSlots", snapSlots.Load(),
"trieNodes", storageStat.TotalNodes(),
"elapsed", common.PrettyDuration(time.Since(start)))
case <-done:
return
@ -403,6 +403,11 @@ func (in *inspector) writeDumpRecord(owner common.Hash, s *LevelStats) {
if err != nil {
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.

View file

@ -48,6 +48,15 @@ func (s *LevelStats) MaxDepth() int {
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.
func (s *LevelStats) add(n node, depth uint32) {
d := int(depth)