mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-02-26 07:37:20 +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>
255 lines
6.8 KiB
Go
255 lines
6.8 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 (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
)
|
|
|
|
func expectedLeaves(counts map[int]int64) [16]int64 {
|
|
var leaves [16]int64
|
|
for depth, count := range counts {
|
|
leaves[depth] = count
|
|
}
|
|
return leaves
|
|
}
|
|
|
|
func TestWitnessStatsAdd(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
nodes map[string][]byte
|
|
owner common.Hash
|
|
expectedAccountLeaves map[int]int64
|
|
expectedStorageLeaves map[int]int64
|
|
}{
|
|
{
|
|
name: "empty nodes",
|
|
nodes: map[string][]byte{},
|
|
owner: common.Hash{},
|
|
},
|
|
{
|
|
name: "single account trie leaf at depth 0",
|
|
nodes: map[string][]byte{
|
|
"": []byte("data"),
|
|
},
|
|
owner: common.Hash{},
|
|
expectedAccountLeaves: map[int]int64{0: 1},
|
|
},
|
|
{
|
|
name: "single account trie leaf",
|
|
nodes: map[string][]byte{
|
|
"abc": []byte("data"),
|
|
},
|
|
owner: common.Hash{},
|
|
expectedAccountLeaves: map[int]int64{3: 1},
|
|
},
|
|
{
|
|
name: "account trie with internal nodes",
|
|
nodes: map[string][]byte{
|
|
"a": []byte("data1"),
|
|
"ab": []byte("data2"),
|
|
"abc": []byte("data3"),
|
|
},
|
|
owner: common.Hash{},
|
|
expectedAccountLeaves: map[int]int64{3: 1}, // Only "abc" is a leaf
|
|
},
|
|
{
|
|
name: "multiple account trie branches",
|
|
nodes: map[string][]byte{
|
|
"a": []byte("data1"),
|
|
"ab": []byte("data2"),
|
|
"abc": []byte("data3"),
|
|
"b": []byte("data4"),
|
|
"bc": []byte("data5"),
|
|
"bcd": []byte("data6"),
|
|
},
|
|
owner: common.Hash{},
|
|
expectedAccountLeaves: map[int]int64{3: 2}, // "abc" (3) + "bcd" (3)
|
|
},
|
|
{
|
|
name: "siblings are all leaves",
|
|
nodes: map[string][]byte{
|
|
"aa": []byte("data1"),
|
|
"ab": []byte("data2"),
|
|
"ac": []byte("data3"),
|
|
},
|
|
owner: common.Hash{},
|
|
expectedAccountLeaves: map[int]int64{2: 3},
|
|
},
|
|
{
|
|
name: "storage trie leaves",
|
|
nodes: map[string][]byte{
|
|
"1": []byte("data1"),
|
|
"12": []byte("data2"),
|
|
"123": []byte("data3"),
|
|
"124": []byte("data4"),
|
|
},
|
|
owner: common.HexToHash("0x1234"),
|
|
expectedStorageLeaves: map[int]int64{3: 2}, // "123" (3) + "124" (3)
|
|
},
|
|
{
|
|
name: "complex trie structure",
|
|
nodes: map[string][]byte{
|
|
"1": []byte("data1"),
|
|
"12": []byte("data2"),
|
|
"123": []byte("data3"),
|
|
"124": []byte("data4"),
|
|
"2": []byte("data5"),
|
|
"23": []byte("data6"),
|
|
"234": []byte("data7"),
|
|
"235": []byte("data8"),
|
|
"3": []byte("data9"),
|
|
},
|
|
owner: common.Hash{},
|
|
expectedAccountLeaves: map[int]int64{1: 1, 3: 4}, // "123"(3) + "124"(3) + "234"(3) + "235"(3) + "3"(1)
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
stats := NewWitnessStats()
|
|
stats.Add(tt.nodes, tt.owner)
|
|
|
|
if got, want := stats.accountTrie.LeafDepths(), expectedLeaves(tt.expectedAccountLeaves); got != want {
|
|
t.Errorf("account trie leaves = %v, want %v", got, want)
|
|
}
|
|
if got, want := stats.storageTrie.LeafDepths(), expectedLeaves(tt.expectedStorageLeaves); got != want {
|
|
t.Errorf("storage trie leaves = %v, want %v", got, want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWitnessStatsStorageTrieAggregation(t *testing.T) {
|
|
stats := NewWitnessStats()
|
|
ownerA := common.HexToHash("0xa")
|
|
ownerB := common.HexToHash("0xb")
|
|
|
|
stats.Add(map[string][]byte{
|
|
"a": []byte("data1"),
|
|
"ab": []byte("data2"),
|
|
"abc": []byte("data3"),
|
|
}, ownerA)
|
|
stats.Add(map[string][]byte{
|
|
"xy": []byte("data4"),
|
|
}, ownerA)
|
|
stats.Add(map[string][]byte{
|
|
"1": []byte("data5"),
|
|
"12": []byte("data6"),
|
|
"123": []byte("data7"),
|
|
"124": []byte("data8"),
|
|
}, ownerB)
|
|
|
|
if got, want := stats.storageTrie.LeafDepths(), expectedLeaves(map[int]int64{2: 1, 3: 3}); got != want {
|
|
t.Errorf("storage leaves = %v, want %v", got, want)
|
|
}
|
|
if got, want := stats.accountTrie.LeafDepths(), expectedLeaves(nil); got != want {
|
|
t.Errorf("account leaves = %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestWitnessStatsPanicsOnDeepLeaf(t *testing.T) {
|
|
stats := NewWitnessStats()
|
|
|
|
defer func() {
|
|
if r := recover(); r == nil {
|
|
t.Fatal("expected panic for depth >= 16")
|
|
}
|
|
}()
|
|
stats.Add(map[string][]byte{strings.Repeat("a", 16): []byte("data")}, common.Hash{})
|
|
}
|
|
|
|
func TestWitnessStatsMinMax(t *testing.T) {
|
|
stats := NewWitnessStats()
|
|
|
|
// Add some account trie nodes with varying depths.
|
|
stats.Add(map[string][]byte{
|
|
"a": []byte("data1"),
|
|
"ab": []byte("data2"),
|
|
"abc": []byte("data3"),
|
|
"abcd": []byte("data4"),
|
|
"abcde": []byte("data5"),
|
|
}, common.Hash{})
|
|
|
|
// Only "abcde" is a leaf (depth 5).
|
|
for i, v := range stats.accountTrie.LeafDepths() {
|
|
if v != 0 && i != 5 {
|
|
t.Errorf("leaf found at invalid depth %d", i)
|
|
}
|
|
}
|
|
|
|
// Add more leaves with different depths.
|
|
stats.Add(map[string][]byte{
|
|
"x": []byte("data6"),
|
|
"yz": []byte("data7"),
|
|
}, common.Hash{})
|
|
|
|
// Now we have leaves at depths 1, 2, and 5.
|
|
for i, v := range stats.accountTrie.LeafDepths() {
|
|
if v != 0 && (i != 5 && i != 2 && i != 1) {
|
|
t.Errorf("leaf found at invalid depth %d", i)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWitnessStatsAverage(t *testing.T) {
|
|
stats := NewWitnessStats()
|
|
|
|
// Add nodes that will create leaves at depths 2, 3, and 4.
|
|
stats.Add(map[string][]byte{
|
|
"aa": []byte("data1"),
|
|
"bb": []byte("data2"),
|
|
"ccc": []byte("data3"),
|
|
"dddd": []byte("data4"),
|
|
}, common.Hash{})
|
|
|
|
// All are leaves: 2 + 2 + 3 + 4 = 11 total, 4 samples.
|
|
expectedAvg := int64(11) / int64(4)
|
|
var actualAvg, totalSamples int64
|
|
for i, c := range stats.accountTrie.LeafDepths() {
|
|
actualAvg += c * int64(i)
|
|
totalSamples += c
|
|
}
|
|
actualAvg = actualAvg / totalSamples
|
|
|
|
if actualAvg != expectedAvg {
|
|
t.Errorf("account trie average depth = %d, want %d", actualAvg, expectedAvg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkWitnessStatsAdd(b *testing.B) {
|
|
// Create a realistic trie node structure.
|
|
nodes := make(map[string][]byte)
|
|
for i := 0; i < 100; i++ {
|
|
base := string(rune('a' + i%26))
|
|
nodes[base] = []byte("data")
|
|
for j := 0; j < 9; j++ {
|
|
key := base + string(rune('0'+j))
|
|
nodes[key] = []byte("data")
|
|
}
|
|
}
|
|
|
|
stats := NewWitnessStats()
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
stats.Add(nodes, common.Hash{})
|
|
}
|
|
}
|