mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-02-26 15:47:21 +00:00
This pr adds a tool names `inpsect-trie`, aimed to analyze the mpt and its node storage more efficiently. ## Example ./geth db inspect-trie --datadir server/data-seed/ latest 4000 ## Result - MPT shape - Account Trie - Top N Storage Trie ``` +-------+-------+--------------+-------------+--------------+ | - | LEVEL | SHORTNODECNT | FULLNODECNT | VALUENODECNT | +-------+-------+--------------+-------------+--------------+ | - | 0 | 0 | 1 | 0 | | - | 1 | 0 | 16 | 0 | | - | 2 | 76 | 32 | 74 | | - | 3 | 66 | 1 | 66 | | - | 4 | 2 | 0 | 2 | | Total | 144 | 50 | 142 | +-------+-------+--------------+-------------+--------------+ AccountTrie +-------+-------+--------------+-------------+--------------+ | - | LEVEL | SHORTNODECNT | FULLNODECNT | VALUENODECNT | +-------+-------+--------------+-------------+--------------+ | - | 0 | 0 | 1 | 0 | | - | 1 | 0 | 16 | 0 | | - | 2 | 108 | 84 | 104 | | - | 3 | 195 | 5 | 195 | | - | 4 | 10 | 0 | 10 | | Total | 313 | 106 | 309 | +-------+-------+--------------+-------------+--------------+ ContractTrie-0xc874e65ccffb133d9db4ff637e62532ef6ecef3223845d02f522c55786782911 +-------+-------+--------------+-------------+--------------+ | - | LEVEL | SHORTNODECNT | FULLNODECNT | VALUENODECNT | +-------+-------+--------------+-------------+--------------+ | - | 0 | 0 | 1 | 0 | | - | 1 | 0 | 16 | 0 | | - | 2 | 57 | 14 | 56 | | - | 3 | 33 | 0 | 33 | | Total | 90 | 31 | 89 | +-------+-------+--------------+-------------+--------------+ ContractTrie-0x1d7dcb6a0ce5227c5379fc5b0e004561d7833b063355f69bfea3178f08fbaab4 +-------+-------+--------------+-------------+--------------+ | - | LEVEL | SHORTNODECNT | FULLNODECNT | VALUENODECNT | +-------+-------+--------------+-------------+--------------+ | - | 0 | 0 | 1 | 0 | | - | 1 | 5 | 8 | 5 | | - | 2 | 16 | 1 | 16 | | - | 3 | 2 | 0 | 2 | | Total | 23 | 10 | 23 | +-------+-------+--------------+-------------+--------------+ ContractTrie-0xaa8a4783ebbb3bec45d3e804b3c59bfd486edfa39cbeda1d42bf86c08a0ebc0f +-------+-------+--------------+-------------+--------------+ | - | LEVEL | SHORTNODECNT | FULLNODECNT | VALUENODECNT | +-------+-------+--------------+-------------+--------------+ | - | 0 | 0 | 1 | 0 | | - | 1 | 9 | 3 | 9 | | - | 2 | 7 | 1 | 7 | | - | 3 | 2 | 0 | 2 | | Total | 18 | 5 | 18 | +-------+-------+--------------+-------------+--------------+ ContractTrie-0x9d2804d0562391d7cfcfaf0013f0352e176a94403a58577ebf82168a21514441 +-------+-------+--------------+-------------+--------------+ | - | LEVEL | SHORTNODECNT | FULLNODECNT | VALUENODECNT | +-------+-------+--------------+-------------+--------------+ | - | 0 | 0 | 1 | 0 | | - | 1 | 6 | 4 | 6 | | - | 2 | 8 | 0 | 8 | | Total | 14 | 5 | 14 | +-------+-------+--------------+-------------+--------------+ ContractTrie-0x17e3eb95d0e6e92b42c0b3e95c6e75080c9fcd83e706344712e9587375de96e1 +-------+-------+--------------+-------------+--------------+ | - | LEVEL | SHORTNODECNT | FULLNODECNT | VALUENODECNT | +-------+-------+--------------+-------------+--------------+ | - | 0 | 0 | 1 | 0 | | - | 1 | 5 | 3 | 5 | | - | 2 | 7 | 0 | 7 | | Total | 12 | 4 | 12 | +-------+-------+--------------+-------------+--------------+ ContractTrie-0xc017ca90c8aa37693c38f80436bb15bde46d7b30a503aa808cb7814127468a44 Contract Trie, total trie num: 142, ShortNodeCnt: 620, FullNodeCnt: 204, ValueNodeCnt: 615 ``` --------- Co-authored-by: lightclient <lightclient@protonmail.com> Co-authored-by: MariusVanDerWijden <m.vanderwijden@live.de>
109 lines
3.5 KiB
Go
109 lines
3.5 KiB
Go
// Copyright 2025 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package stateless
|
|
|
|
import (
|
|
"encoding/json"
|
|
"maps"
|
|
"slices"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/log"
|
|
"github.com/ethereum/go-ethereum/metrics"
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
)
|
|
|
|
var accountTrieLeavesAtDepth [16]*metrics.Counter
|
|
var storageTrieLeavesAtDepth [16]*metrics.Counter
|
|
|
|
func init() {
|
|
for i := 0; i < 16; i++ {
|
|
accountTrieLeavesAtDepth[i] = metrics.NewRegisteredCounter("witness/trie/account/leaves/depth_"+strconv.Itoa(i), nil)
|
|
storageTrieLeavesAtDepth[i] = metrics.NewRegisteredCounter("witness/trie/storage/leaves/depth_"+strconv.Itoa(i), nil)
|
|
}
|
|
}
|
|
|
|
// WitnessStats aggregates statistics for account and storage trie accesses.
|
|
type WitnessStats struct {
|
|
accountTrie *trie.LevelStats
|
|
storageTrie *trie.LevelStats
|
|
}
|
|
|
|
// NewWitnessStats creates a new WitnessStats collector.
|
|
func NewWitnessStats() *WitnessStats {
|
|
return &WitnessStats{
|
|
accountTrie: trie.NewLevelStats(),
|
|
storageTrie: trie.NewLevelStats(),
|
|
}
|
|
}
|
|
|
|
func (s *WitnessStats) init() {
|
|
if s.accountTrie == nil {
|
|
s.accountTrie = trie.NewLevelStats()
|
|
}
|
|
if s.storageTrie == nil {
|
|
s.storageTrie = trie.NewLevelStats()
|
|
}
|
|
}
|
|
|
|
// Add records trie access depths from the given node paths.
|
|
// If `owner` is the zero hash, accesses are attributed to the account trie;
|
|
// otherwise, they are attributed to the storage trie of that account.
|
|
func (s *WitnessStats) Add(nodes map[string][]byte, owner common.Hash) {
|
|
s.init()
|
|
|
|
// Extract paths from the nodes map.
|
|
paths := slices.Collect(maps.Keys(nodes))
|
|
sort.Strings(paths)
|
|
|
|
ownerStat := s.accountTrie
|
|
if owner != (common.Hash{}) {
|
|
ownerStat = s.storageTrie
|
|
}
|
|
|
|
for i, path := range paths {
|
|
// If current path is a prefix of the next path, it's not a leaf.
|
|
// The last path is always a leaf.
|
|
if i == len(paths)-1 || !strings.HasPrefix(paths[i+1], paths[i]) {
|
|
ownerStat.AddLeaf(len(path))
|
|
}
|
|
}
|
|
}
|
|
|
|
// ReportMetrics reports the collected statistics to the global metrics registry.
|
|
func (s *WitnessStats) ReportMetrics(blockNumber uint64) {
|
|
s.init()
|
|
|
|
accountTrieLeaves := s.accountTrie.LeafDepths()
|
|
storageTrieLeaves := s.storageTrie.LeafDepths()
|
|
|
|
// Encode the metrics as JSON for easier consumption.
|
|
accountLeavesJSON, _ := json.Marshal(accountTrieLeaves)
|
|
storageLeavesJSON, _ := json.Marshal(storageTrieLeaves)
|
|
|
|
// Log account trie depth statistics.
|
|
log.Info("Account trie depth stats", "block", blockNumber, "leavesAtDepth", string(accountLeavesJSON))
|
|
log.Info("Storage trie depth stats", "block", blockNumber, "leavesAtDepth", string(storageLeavesJSON))
|
|
|
|
for i := 0; i < len(accountTrieLeavesAtDepth); i++ {
|
|
accountTrieLeavesAtDepth[i].Inc(accountTrieLeaves[i])
|
|
storageTrieLeavesAtDepth[i].Inc(storageTrieLeaves[i])
|
|
}
|
|
}
|