@@ -24,27 +24,24 @@ import (
2424
2525func TestWitnessStatsAdd (t * testing.T ) {
2626 tests := []struct {
27- name string
28- nodes map [string ][]byte
29- owner common.Hash
30- expectedAccountDepth int64
31- expectedStorageDepth int64
27+ name string
28+ nodes map [string ][]byte
29+ owner common.Hash
30+ expectedAccountLeaves map [ int64 ] int64
31+ expectedStorageLeaves map [ int64 ] int64
3232 }{
3333 {
34- name : "empty nodes" ,
35- nodes : map [string ][]byte {},
36- owner : common.Hash {},
37- expectedAccountDepth : 0 ,
38- expectedStorageDepth : 0 ,
34+ name : "empty nodes" ,
35+ nodes : map [string ][]byte {},
36+ owner : common.Hash {},
3937 },
4038 {
4139 name : "single account trie leaf" ,
4240 nodes : map [string ][]byte {
4341 "abc" : []byte ("data" ),
4442 },
45- owner : common.Hash {},
46- expectedAccountDepth : 3 ,
47- expectedStorageDepth : 0 ,
43+ owner : common.Hash {},
44+ expectedAccountLeaves : map [int64 ]int64 {3 : 1 },
4845 },
4946 {
5047 name : "account trie with internal nodes" ,
@@ -53,9 +50,8 @@ func TestWitnessStatsAdd(t *testing.T) {
5350 "ab" : []byte ("data2" ),
5451 "abc" : []byte ("data3" ),
5552 },
56- owner : common.Hash {},
57- expectedAccountDepth : 3 , // Only "abc" is a leaf
58- expectedStorageDepth : 0 ,
53+ owner : common.Hash {},
54+ expectedAccountLeaves : map [int64 ]int64 {3 : 1 }, // Only "abc" is a leaf
5955 },
6056 {
6157 name : "multiple account trie branches" ,
@@ -67,9 +63,8 @@ func TestWitnessStatsAdd(t *testing.T) {
6763 "bc" : []byte ("data5" ),
6864 "bcd" : []byte ("data6" ),
6965 },
70- owner : common.Hash {},
71- expectedAccountDepth : 6 , // "abc" (3) + "bcd" (3) = 6
72- expectedStorageDepth : 0 ,
66+ owner : common.Hash {},
67+ expectedAccountLeaves : map [int64 ]int64 {3 : 2 }, // "abc" (3) + "bcd" (3)
7368 },
7469 {
7570 name : "siblings are all leaves" ,
@@ -78,9 +73,8 @@ func TestWitnessStatsAdd(t *testing.T) {
7873 "ab" : []byte ("data2" ),
7974 "ac" : []byte ("data3" ),
8075 },
81- owner : common.Hash {},
82- expectedAccountDepth : 6 , // 2 + 2 + 2 = 6
83- expectedStorageDepth : 0 ,
76+ owner : common.Hash {},
77+ expectedAccountLeaves : map [int64 ]int64 {2 : 3 },
8478 },
8579 {
8680 name : "storage trie leaves" ,
@@ -90,9 +84,8 @@ func TestWitnessStatsAdd(t *testing.T) {
9084 "123" : []byte ("data3" ),
9185 "124" : []byte ("data4" ),
9286 },
93- owner : common .HexToHash ("0x1234" ),
94- expectedAccountDepth : 0 ,
95- expectedStorageDepth : 6 , // "123" (3) + "124" (3) = 6
87+ owner : common .HexToHash ("0x1234" ),
88+ expectedStorageLeaves : map [int64 ]int64 {3 : 2 }, // "123" (3) + "124" (3)
9689 },
9790 {
9891 name : "complex trie structure" ,
@@ -107,9 +100,8 @@ func TestWitnessStatsAdd(t *testing.T) {
107100 "235" : []byte ("data8" ),
108101 "3" : []byte ("data9" ),
109102 },
110- owner : common.Hash {},
111- expectedAccountDepth : 13 , // "123"(3) + "124"(3) + "234"(3) + "235"(3) + "3"(1) = 13
112- expectedStorageDepth : 0 ,
103+ owner : common.Hash {},
104+ expectedAccountLeaves : map [int64 ]int64 {1 : 1 , 3 : 4 }, // "123"(3) + "124"(3) + "234"(3) + "235"(3) + "3"(1)
113105 },
114106 }
115107
@@ -118,74 +110,28 @@ func TestWitnessStatsAdd(t *testing.T) {
118110 stats := NewWitnessStats ()
119111 stats .Add (tt .nodes , tt .owner )
120112
113+ var expectedAccountTrieLeaves [16 ]int64
114+ for depth , count := range tt .expectedAccountLeaves {
115+ expectedAccountTrieLeaves [depth ] = count
116+ }
117+ var expectedStorageTrieLeaves [16 ]int64
118+ for depth , count := range tt .expectedStorageLeaves {
119+ expectedStorageTrieLeaves [depth ] = count
120+ }
121+
121122 // Check account trie depth
122- if stats .accountTrie . totalDepth != tt . expectedAccountDepth {
123- t .Errorf ("Account trie total depth = %d , want %d " , stats .accountTrie . totalDepth , tt . expectedAccountDepth )
123+ if stats .accountTrieLeaves != expectedAccountTrieLeaves {
124+ t .Errorf ("Account trie total depth = %v , want %v " , stats .accountTrieLeaves , expectedAccountTrieLeaves )
124125 }
125126
126127 // Check storage trie depth
127- if stats .storageTrie . totalDepth != tt . expectedStorageDepth {
128- t .Errorf ("Storage trie total depth = %d , want %d " , stats .storageTrie . totalDepth , tt . expectedStorageDepth )
128+ if stats .storageTrieLeaves != expectedStorageTrieLeaves {
129+ t .Errorf ("Storage trie total depth = %v , want %v " , stats .storageTrieLeaves , expectedStorageTrieLeaves )
129130 }
130131 })
131132 }
132133}
133134
134- func TestWitnessStatsMinMax (t * testing.T ) {
135- stats := NewWitnessStats ()
136-
137- // Add some account trie nodes with varying depths
138- stats .Add (map [string ][]byte {
139- "a" : []byte ("data1" ),
140- "ab" : []byte ("data2" ),
141- "abc" : []byte ("data3" ),
142- "abcd" : []byte ("data4" ),
143- "abcde" : []byte ("data5" ),
144- }, common.Hash {})
145-
146- // Only "abcde" is a leaf (depth 5)
147- if stats .accountTrie .minDepth != 5 {
148- t .Errorf ("Account trie min depth = %d, want %d" , stats .accountTrie .minDepth , 5 )
149- }
150- if stats .accountTrie .maxDepth != 5 {
151- t .Errorf ("Account trie max depth = %d, want %d" , stats .accountTrie .maxDepth , 5 )
152- }
153-
154- // Add more leaves with different depths
155- stats .Add (map [string ][]byte {
156- "x" : []byte ("data6" ),
157- "yz" : []byte ("data7" ),
158- }, common.Hash {})
159-
160- // Now we have leaves at depths 1, 2, and 5
161- if stats .accountTrie .minDepth != 1 {
162- t .Errorf ("Account trie min depth after update = %d, want %d" , stats .accountTrie .minDepth , 1 )
163- }
164- if stats .accountTrie .maxDepth != 5 {
165- t .Errorf ("Account trie max depth after update = %d, want %d" , stats .accountTrie .maxDepth , 5 )
166- }
167- }
168-
169- func TestWitnessStatsAverage (t * testing.T ) {
170- stats := NewWitnessStats ()
171-
172- // Add nodes that will create leaves at depths 2, 3, and 4
173- stats .Add (map [string ][]byte {
174- "aa" : []byte ("data1" ),
175- "bb" : []byte ("data2" ),
176- "ccc" : []byte ("data3" ),
177- "dddd" : []byte ("data4" ),
178- }, common.Hash {})
179-
180- // All are leaves: 2 + 2 + 3 + 4 = 11 total, 4 samples
181- expectedAvg := int64 (11 ) / int64 (4 )
182- actualAvg := stats .accountTrie .totalDepth / stats .accountTrie .samples
183-
184- if actualAvg != expectedAvg {
185- t .Errorf ("Account trie average depth = %d, want %d" , actualAvg , expectedAvg )
186- }
187- }
188-
189135func BenchmarkWitnessStatsAdd (b * testing.B ) {
190136 // Create a realistic trie node structure
191137 nodes := make (map [string ][]byte )
0 commit comments