|
14 | 14 | // along with the go-ethereum library. If not, see |
15 | 15 | // <http://www.gnu.org/licenses/>. |
16 | 16 |
|
17 | | -package rawdb_test |
| 17 | +package rawdb |
18 | 18 |
|
19 | 19 | import ( |
20 | | - "bytes" |
21 | | - "fmt" |
22 | | - "sort" |
| 20 | + "testing" |
23 | 21 |
|
24 | | - "github.com/ava-labs/libevm/common" |
25 | | - // To ensure that all methods are available to importing packages, this test |
26 | | - // is defined in package `rawdb_test` instead of `rawdb`. |
27 | | - "github.com/ava-labs/libevm/core/rawdb" |
28 | | - "github.com/ava-labs/libevm/ethdb" |
| 22 | + "github.com/stretchr/testify/assert" |
29 | 23 | ) |
30 | 24 |
|
31 | | -// ExampleDatabaseStat demonstrates the method signatures of DatabaseStat, which |
32 | | -// exposes an otherwise unexported type that won't have its methods documented. |
33 | | -func ExampleDatabaseStat() { |
34 | | - var stat rawdb.DatabaseStat |
| 25 | +func TestSkipFreezers(t *testing.T) { |
| 26 | + db := NewMemoryDatabase() |
35 | 27 |
|
36 | | - stat.Add(common.StorageSize(1)) // only to demonstrate param type |
37 | | - stat.Add(2) |
38 | | - |
39 | | - fmt.Println("Sum:", stat.Size()) // sum of all values passed to Add() |
40 | | - fmt.Println("Count:", stat.Count()) // number of calls to Add() |
41 | | - |
42 | | - // Output: |
43 | | - // Sum: 3.00 B |
44 | | - // Count: 2 |
45 | | -} |
46 | | - |
47 | | -func ExampleInspectDatabase() { |
48 | | - db := &stubDatabase{ |
49 | | - iterator: &stubIterator{ |
50 | | - kvs: []keyValue{ |
51 | | - // Bloom bits total = 5 + 1 = 6 |
52 | | - {key: []byte("iBxxx"), value: []byte("m")}, |
53 | | - // Optional stat record total = 5 + 7 = 12 |
54 | | - {key: []byte("mykey"), value: []byte("myvalue")}, |
55 | | - // metadata total = (13 + 7) + (14 + 7) = 41 |
56 | | - {key: []byte("mymetadatakey"), value: []byte("myvalue")}, |
57 | | - {key: []byte("mymetadatakey2"), value: []byte("myvalue")}, |
58 | | - }, |
| 28 | + tests := []struct { |
| 29 | + skipFreezers bool |
| 30 | + wantErr error |
| 31 | + }{ |
| 32 | + { |
| 33 | + skipFreezers: false, |
| 34 | + wantErr: errNotSupported, |
| 35 | + }, |
| 36 | + { |
| 37 | + skipFreezers: true, |
| 38 | + wantErr: nil, |
59 | 39 | }, |
60 | 40 | } |
61 | 41 |
|
62 | | - keyPrefix := []byte(nil) |
63 | | - keyStart := []byte(nil) |
64 | | - |
65 | | - var ( |
66 | | - myStat rawdb.DatabaseStat |
67 | | - ) |
68 | | - options := []rawdb.InspectDatabaseOption{ |
69 | | - rawdb.WithDatabaseStatRecorder(func(key []byte, size common.StorageSize) bool { |
70 | | - if bytes.Equal(key, []byte("mykey")) { |
71 | | - myStat.Add(size) |
72 | | - return true |
73 | | - } |
74 | | - return false |
75 | | - }), |
76 | | - rawdb.WithDatabaseMetadataKeys(func(key []byte) bool { |
77 | | - return bytes.Equal(key, []byte("mymetadatakey")) |
78 | | - }), |
79 | | - rawdb.WithDatabaseMetadataKeys(func(key []byte) bool { |
80 | | - return bytes.Equal(key, []byte("mymetadatakey2")) |
81 | | - }), |
82 | | - rawdb.WithDatabaseStatsTransformer(func(rows [][]string) [][]string { |
83 | | - sort.Slice(rows, func(i, j int) bool { |
84 | | - ri, rj := rows[i], rows[j] |
85 | | - if ri[0] != rj[0] { |
86 | | - return ri[0] < rj[0] |
87 | | - } |
88 | | - return ri[1] < rj[1] |
89 | | - }) |
90 | | - |
91 | | - return append( |
92 | | - rows, |
93 | | - []string{"My database", "My category", myStat.Size(), myStat.Count()}, |
94 | | - ) |
95 | | - }), |
96 | | - } |
97 | | - |
98 | | - err := rawdb.InspectDatabase(db, keyPrefix, keyStart, options...) |
99 | | - if err != nil { |
100 | | - fmt.Println(err) |
| 42 | + for _, tt := range tests { |
| 43 | + var opts []InspectDatabaseOption |
| 44 | + if tt.skipFreezers { |
| 45 | + opts = append(opts, WithSkipFreezers()) |
| 46 | + } |
| 47 | + assert.ErrorIsf(t, InspectDatabase(db, nil, nil, opts...), tt.wantErr, "InspectDatabase(%T, nil, nil, [WithSkipFreezers = %t])", db, tt.skipFreezers) |
101 | 48 | } |
102 | | - // Output: |
103 | | - // +-----------------------+-------------------------+---------+-------+ |
104 | | - // | DATABASE | CATEGORY | SIZE | ITEMS | |
105 | | - // +-----------------------+-------------------------+---------+-------+ |
106 | | - // | Ancient store (Chain) | Bodies | 0.00 B | 0 | |
107 | | - // | Ancient store (Chain) | Diffs | 0.00 B | 0 | |
108 | | - // | Ancient store (Chain) | Hashes | 0.00 B | 0 | |
109 | | - // | Ancient store (Chain) | Headers | 0.00 B | 0 | |
110 | | - // | Ancient store (Chain) | Receipts | 0.00 B | 0 | |
111 | | - // | Key-Value store | Account snapshot | 0.00 B | 0 | |
112 | | - // | Key-Value store | Beacon sync headers | 0.00 B | 0 | |
113 | | - // | Key-Value store | Block hash->number | 0.00 B | 0 | |
114 | | - // | Key-Value store | Block number->hash | 0.00 B | 0 | |
115 | | - // | Key-Value store | Bloombit index | 6.00 B | 1 | |
116 | | - // | Key-Value store | Bodies | 0.00 B | 0 | |
117 | | - // | Key-Value store | Clique snapshots | 0.00 B | 0 | |
118 | | - // | Key-Value store | Contract codes | 0.00 B | 0 | |
119 | | - // | Key-Value store | Difficulties | 0.00 B | 0 | |
120 | | - // | Key-Value store | Hash trie nodes | 0.00 B | 0 | |
121 | | - // | Key-Value store | Headers | 0.00 B | 0 | |
122 | | - // | Key-Value store | Path trie account nodes | 0.00 B | 0 | |
123 | | - // | Key-Value store | Path trie state lookups | 0.00 B | 0 | |
124 | | - // | Key-Value store | Path trie storage nodes | 0.00 B | 0 | |
125 | | - // | Key-Value store | Receipt lists | 0.00 B | 0 | |
126 | | - // | Key-Value store | Singleton metadata | 41.00 B | 2 | |
127 | | - // | Key-Value store | Storage snapshot | 0.00 B | 0 | |
128 | | - // | Key-Value store | Transaction index | 0.00 B | 0 | |
129 | | - // | Key-Value store | Trie preimages | 0.00 B | 0 | |
130 | | - // | Light client | Bloom trie nodes | 0.00 B | 0 | |
131 | | - // | Light client | CHT trie nodes | 0.00 B | 0 | |
132 | | - // | My database | My category | 12.00 B | 1 | |
133 | | - // +-----------------------+-------------------------+---------+-------+ |
134 | | - // | TOTAL | 59.00 B | | |
135 | | - // +-----------------------+-------------------------+---------+-------+ |
136 | | -} |
137 | | - |
138 | | -type stubDatabase struct { |
139 | | - ethdb.Database |
140 | | - iterator ethdb.Iterator |
141 | | -} |
142 | | - |
143 | | -func (s *stubDatabase) NewIterator(keyPrefix, keyStart []byte) ethdb.Iterator { |
144 | | - return s.iterator |
145 | | -} |
146 | | - |
147 | | -// AncientSize is used in [InspectDatabase] to determine the ancient sizes. |
148 | | -func (s *stubDatabase) AncientSize(kind string) (uint64, error) { |
149 | | - return 0, nil |
150 | | -} |
151 | | - |
152 | | -func (s *stubDatabase) Ancients() (uint64, error) { |
153 | | - return 0, nil |
154 | | -} |
155 | | - |
156 | | -func (s *stubDatabase) Tail() (uint64, error) { |
157 | | - return 0, nil |
158 | | -} |
159 | | - |
160 | | -func (s *stubDatabase) Get(key []byte) ([]byte, error) { |
161 | | - return nil, nil |
162 | | -} |
163 | | - |
164 | | -func (s *stubDatabase) ReadAncients(fn func(ethdb.AncientReaderOp) error) error { |
165 | | - return nil |
166 | | -} |
167 | | - |
168 | | -type stubIterator struct { |
169 | | - ethdb.Iterator |
170 | | - i int // see [stubIterator.pos] |
171 | | - kvs []keyValue |
172 | | -} |
173 | | - |
174 | | -type keyValue struct { |
175 | | - key []byte |
176 | | - value []byte |
177 | | -} |
178 | | - |
179 | | -// pos returns the true iterator position, which is otherwise off by one because |
180 | | -// Next() is called _before_ usage. |
181 | | -func (s *stubIterator) pos() int { |
182 | | - return s.i - 1 |
183 | | -} |
184 | | - |
185 | | -func (s *stubIterator) Next() bool { |
186 | | - s.i++ |
187 | | - return s.pos() < len(s.kvs) |
188 | | -} |
189 | | - |
190 | | -func (s *stubIterator) Release() {} |
191 | | - |
192 | | -func (s *stubIterator) Key() []byte { |
193 | | - return s.kvs[s.pos()].key |
194 | | -} |
195 | | - |
196 | | -func (s *stubIterator) Value() []byte { |
197 | | - return s.kvs[s.pos()].value |
198 | 49 | } |
0 commit comments