diff --git a/trie/inspect.go b/trie/inspect.go index 4cfb0554c7..4688ce500f 100644 --- a/trie/inspect.go +++ b/trie/inspect.go @@ -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. diff --git a/trie/levelstats.go b/trie/levelstats.go index 2d8bec956d..9168e3fbaf 100644 --- a/trie/levelstats.go +++ b/trie/levelstats.go @@ -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)