cmd/geth, core/rawdb: add inspect-trie

This commit is contained in:
weiihann 2026-01-29 23:05:08 +08:00
parent 9276cfdb9f
commit 4d057b2c49
2 changed files with 25 additions and 18 deletions

View file

@ -70,7 +70,7 @@ Remove blockchain and state databases`,
ArgsUsage: "", ArgsUsage: "",
Subcommands: []*cli.Command{ Subcommands: []*cli.Command{
dbInspectCmd, dbInspectCmd,
dbInspectTrieDepthCmd, dbInspectTrieCmd,
dbStatCmd, dbStatCmd,
dbCompactCmd, dbCompactCmd,
dbGetCmd, dbGetCmd,
@ -93,9 +93,9 @@ Remove blockchain and state databases`,
Usage: "Inspect the storage size for each type of data in the database", 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.`, 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, Action: inspectTrieDepth,
Name: "inspect-trie-depth", Name: "inspect-trie",
Usage: "Inspect the depth of the trie", Usage: "Inspect the depth of the trie",
ArgsUsage: "<start> <end>", ArgsUsage: "<start> <end>",
Flags: slices.Concat(utils.NetworkFlags, utils.DatabaseFlags), Flags: slices.Concat(utils.NetworkFlags, utils.DatabaseFlags),

View file

@ -807,15 +807,13 @@ func InspectTrieDepth(db ethdb.Database) error {
var ( var (
start = time.Now() start = time.Now()
accountDepths [65]int64 accountDepths [65]stat
storageDepths [65]int64 storageDepths [65]stat
accountCount int64 storageTriesCnt uint64
storageCount int64 nodeCount uint64
storageTriesCnt int64
nodeCount int64
) )
log.Info("Calculating trie depth distribution (streaming)...") log.Info("Calculating trie depth distribution...")
// Process account trie nodes (prefix "A") // Process account trie nodes (prefix "A")
// Keys are already in lexicographic order from the database iterator // Keys are already in lexicographic order from the database iterator
@ -839,8 +837,7 @@ func InspectTrieDepth(db ethdb.Database) error {
} }
depth := len(accountStack) depth := len(accountStack)
accountDepths[depth]++ accountDepths[depth].add(common.StorageSize(len(it.Key()) + len(it.Value())))
accountCount++
nodeCount++ nodeCount++
accountStack = append(accountStack, pathStr) accountStack = append(accountStack, pathStr)
@ -854,6 +851,10 @@ func InspectTrieDepth(db ethdb.Database) error {
return err 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))) log.Info("Finished account trie", "nodes", accountCount, "elapsed", common.PrettyDuration(time.Since(start)))
// Process storage trie nodes (prefix "O") // Process storage trie nodes (prefix "O")
@ -894,8 +895,7 @@ func InspectTrieDepth(db ethdb.Database) error {
} }
depth := len(storageStack) depth := len(storageStack)
storageDepths[depth]++ storageDepths[depth].add(common.StorageSize(len(it.Key()) + len(it.Value())))
storageCount++
nodeCount++ nodeCount++
storageStack = append(storageStack, pathStr) storageStack = append(storageStack, pathStr)
@ -914,6 +914,11 @@ func InspectTrieDepth(db ethdb.Database) error {
storageTriesCnt++ storageTriesCnt++
} }
var storageCount uint64
for i := range storageDepths {
storageCount += atomic.LoadUint64(&storageDepths[i].count)
}
log.Info("Depth calculation complete", log.Info("Depth calculation complete",
"accountNodes", accountCount, "accountNodes", accountCount,
"storageNodes", storageCount, "storageNodes", storageCount,
@ -925,11 +930,13 @@ func InspectTrieDepth(db ethdb.Database) error {
fmt.Printf("\nAccount Trie: %d nodes\n", accountCount) fmt.Printf("\nAccount Trie: %d nodes\n", accountCount)
fmt.Printf("Storage Tries: %d nodes across %d tries\n\n", storageCount, storageTriesCnt) fmt.Printf("Storage Tries: %d nodes across %d tries\n\n", storageCount, storageTriesCnt)
fmt.Println("Depth | Account Nodes | Storage Nodes") fmt.Println("Depth | Account Nodes | Account Size | Storage Nodes | Storage Size")
fmt.Println("------|---------------|---------------") fmt.Println("------|---------------|---------------|---------------|---------------")
for i := 0; i < 65; i++ { for i := 0; i < 65; i++ {
if accountDepths[i] > 0 || storageDepths[i] > 0 { if !accountDepths[i].empty() || !storageDepths[i].empty() {
fmt.Printf("%5d | %13d | %13d\n", i, accountDepths[i], storageDepths[i]) fmt.Printf("%5d | %13s | %13s | %13s | %13s\n",
i, accountDepths[i].countString(), accountDepths[i].sizeString(),
storageDepths[i].countString(), storageDepths[i].sizeString())
} }
} }
fmt.Println() fmt.Println()