From 4d057b2c4992ca5848fa80a61fdeb21e8dfc5f8b Mon Sep 17 00:00:00 2001 From: weiihann Date: Thu, 29 Jan 2026 23:05:08 +0800 Subject: [PATCH] cmd/geth, core/rawdb: add inspect-trie --- cmd/geth/dbcmd.go | 6 +++--- core/rawdb/database.go | 37 ++++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/cmd/geth/dbcmd.go b/cmd/geth/dbcmd.go index f71e57b7ba..97d6e7337b 100644 --- a/cmd/geth/dbcmd.go +++ b/cmd/geth/dbcmd.go @@ -70,7 +70,7 @@ Remove blockchain and state databases`, ArgsUsage: "", Subcommands: []*cli.Command{ dbInspectCmd, - dbInspectTrieDepthCmd, + dbInspectTrieCmd, dbStatCmd, dbCompactCmd, dbGetCmd, @@ -93,9 +93,9 @@ Remove blockchain and state databases`, Usage: "Inspect the storage size for each type of data in the database", Description: `This commands iterates the entire database. If the optional 'prefix' and 'start' arguments are provided, then the iteration is limited to the given subset of data.`, } - dbInspectTrieDepthCmd = &cli.Command{ + dbInspectTrieCmd = &cli.Command{ Action: inspectTrieDepth, - Name: "inspect-trie-depth", + Name: "inspect-trie", Usage: "Inspect the depth of the trie", ArgsUsage: " ", Flags: slices.Concat(utils.NetworkFlags, utils.DatabaseFlags), diff --git a/core/rawdb/database.go b/core/rawdb/database.go index ec13c7997b..e536625f7c 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -807,15 +807,13 @@ func InspectTrieDepth(db ethdb.Database) error { var ( start = time.Now() - accountDepths [65]int64 - storageDepths [65]int64 - accountCount int64 - storageCount int64 - storageTriesCnt int64 - nodeCount int64 + accountDepths [65]stat + storageDepths [65]stat + storageTriesCnt uint64 + nodeCount uint64 ) - log.Info("Calculating trie depth distribution (streaming)...") + log.Info("Calculating trie depth distribution...") // Process account trie nodes (prefix "A") // Keys are already in lexicographic order from the database iterator @@ -839,8 +837,7 @@ func InspectTrieDepth(db ethdb.Database) error { } depth := len(accountStack) - accountDepths[depth]++ - accountCount++ + accountDepths[depth].add(common.StorageSize(len(it.Key()) + len(it.Value()))) nodeCount++ accountStack = append(accountStack, pathStr) @@ -854,6 +851,10 @@ func InspectTrieDepth(db ethdb.Database) error { return err } + var accountCount uint64 + for i := range accountDepths { + accountCount += atomic.LoadUint64(&accountDepths[i].count) + } log.Info("Finished account trie", "nodes", accountCount, "elapsed", common.PrettyDuration(time.Since(start))) // Process storage trie nodes (prefix "O") @@ -894,8 +895,7 @@ func InspectTrieDepth(db ethdb.Database) error { } depth := len(storageStack) - storageDepths[depth]++ - storageCount++ + storageDepths[depth].add(common.StorageSize(len(it.Key()) + len(it.Value()))) nodeCount++ storageStack = append(storageStack, pathStr) @@ -914,6 +914,11 @@ func InspectTrieDepth(db ethdb.Database) error { storageTriesCnt++ } + var storageCount uint64 + for i := range storageDepths { + storageCount += atomic.LoadUint64(&storageDepths[i].count) + } + log.Info("Depth calculation complete", "accountNodes", accountCount, "storageNodes", storageCount, @@ -925,11 +930,13 @@ func InspectTrieDepth(db ethdb.Database) error { fmt.Printf("\nAccount Trie: %d nodes\n", accountCount) fmt.Printf("Storage Tries: %d nodes across %d tries\n\n", storageCount, storageTriesCnt) - fmt.Println("Depth | Account Nodes | Storage Nodes") - fmt.Println("------|---------------|---------------") + fmt.Println("Depth | Account Nodes | Account Size | Storage Nodes | Storage Size") + fmt.Println("------|---------------|---------------|---------------|---------------") for i := 0; i < 65; i++ { - if accountDepths[i] > 0 || storageDepths[i] > 0 { - fmt.Printf("%5d | %13d | %13d\n", i, accountDepths[i], storageDepths[i]) + if !accountDepths[i].empty() || !storageDepths[i].empty() { + fmt.Printf("%5d | %13s | %13s | %13s | %13s\n", + i, accountDepths[i].countString(), accountDepths[i].sizeString(), + storageDepths[i].countString(), storageDepths[i].sizeString()) } } fmt.Println()