Skip to content

Commit c19cea4

Browse files
committed
chore(core/rawdb): InspectDatabase uses libevm
- Part of the core/rawdb migration to use libevm - Using libevm `InspectDatabase` - Define options passed to libevm `InspectDatabase` See original PR ava-labs/coreth#791
1 parent bd2a1e8 commit c19cea4

File tree

1 file changed

+53
-182
lines changed

1 file changed

+53
-182
lines changed

core/rawdb/database.go

Lines changed: 53 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ import (
3131
"fmt"
3232
"os"
3333
"path/filepath"
34-
"time"
3534

3635
"github.com/ava-labs/libevm/common"
36+
ethrawdb "github.com/ava-labs/libevm/core/rawdb"
3737
"github.com/ava-labs/libevm/ethdb"
3838
"github.com/ava-labs/libevm/ethdb/leveldb"
3939
"github.com/ava-labs/libevm/ethdb/memorydb"
4040
"github.com/ava-labs/libevm/ethdb/pebble"
4141
"github.com/ava-labs/libevm/log"
42-
"github.com/olekukonko/tablewriter"
4342
)
4443

4544
// nofreezedb is a database wrapper that disables freezer data retrievals.
@@ -242,194 +241,66 @@ func Open(o OpenOptions) (ethdb.Database, error) {
242241
return kvdb, nil
243242
}
244243

245-
type counter uint64
246-
247-
func (c counter) String() string {
248-
return fmt.Sprintf("%d", c)
249-
}
250-
251-
func (c counter) Percentage(current uint64) string {
252-
return fmt.Sprintf("%d", current*100/uint64(c))
253-
}
254-
255-
// stat stores sizes and count for a parameter
256-
type stat struct {
257-
size common.StorageSize
258-
count counter
259-
}
260-
261-
// Add size to the stat and increase the counter by 1
262-
func (s *stat) Add(size common.StorageSize) {
263-
s.size += size
264-
s.count++
265-
}
266-
267-
func (s *stat) Size() string {
268-
return s.size.String()
269-
}
270-
271-
func (s *stat) Count() string {
272-
return s.count.String()
273-
}
274-
275244
// InspectDatabase traverses the entire database and checks the size
276245
// of all different categories of data.
277246
func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error {
278-
it := db.NewIterator(keyPrefix, keyStart)
279-
defer it.Release()
280-
281247
var (
282-
count int64
283-
start = time.Now()
284-
logged = time.Now()
285-
286-
// Key-value store statistics
287-
headers stat
288-
bodies stat
289-
receipts stat
290-
numHashPairings stat
291-
hashNumPairings stat
292-
legacyTries stat
293-
stateLookups stat
294-
accountTries stat
295-
storageTries stat
296-
codes stat
297-
txLookups stat
298-
accountSnaps stat
299-
storageSnaps stat
300-
preimages stat
301-
bloomBits stat
302-
cliqueSnaps stat
303-
304-
// State sync statistics
305-
codeToFetch stat
306-
syncProgress stat
307-
syncSegments stat
308-
syncPerformed stat
309-
310-
// Les statistic
311-
chtTrieNodes stat
312-
bloomTrieNodes stat
313-
314-
// Meta- and unaccounted data
315-
metadata stat
316-
unaccounted stat
317-
318-
// Totals
319-
total common.StorageSize
248+
codeToFetch ethrawdb.DatabaseStat
249+
syncPerformed ethrawdb.DatabaseStat
250+
syncProgress ethrawdb.DatabaseStat
251+
syncSegments ethrawdb.DatabaseStat
320252
)
321-
// Inspect key-value database first.
322-
for it.Next() {
323-
var (
324-
key = it.Key()
325-
size = common.StorageSize(len(key) + len(it.Value()))
326-
)
327-
total += size
328-
switch {
329-
case bytes.HasPrefix(key, headerPrefix) && len(key) == (len(headerPrefix)+8+common.HashLength):
330-
headers.Add(size)
331-
case bytes.HasPrefix(key, blockBodyPrefix) && len(key) == (len(blockBodyPrefix)+8+common.HashLength):
332-
bodies.Add(size)
333-
case bytes.HasPrefix(key, blockReceiptsPrefix) && len(key) == (len(blockReceiptsPrefix)+8+common.HashLength):
334-
receipts.Add(size)
335-
case bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerHashSuffix):
336-
numHashPairings.Add(size)
337-
case bytes.HasPrefix(key, headerNumberPrefix) && len(key) == (len(headerNumberPrefix)+common.HashLength):
338-
hashNumPairings.Add(size)
339-
case IsLegacyTrieNode(key, it.Value()):
340-
legacyTries.Add(size)
341-
case bytes.HasPrefix(key, stateIDPrefix) && len(key) == len(stateIDPrefix)+common.HashLength:
342-
stateLookups.Add(size)
343-
case IsAccountTrieNode(key):
344-
accountTries.Add(size)
345-
case IsStorageTrieNode(key):
346-
storageTries.Add(size)
347-
case bytes.HasPrefix(key, CodePrefix) && len(key) == len(CodePrefix)+common.HashLength:
348-
codes.Add(size)
349-
case bytes.HasPrefix(key, txLookupPrefix) && len(key) == (len(txLookupPrefix)+common.HashLength):
350-
txLookups.Add(size)
351-
case bytes.HasPrefix(key, SnapshotAccountPrefix) && len(key) == (len(SnapshotAccountPrefix)+common.HashLength):
352-
accountSnaps.Add(size)
353-
case bytes.HasPrefix(key, SnapshotStoragePrefix) && len(key) == (len(SnapshotStoragePrefix)+2*common.HashLength):
354-
storageSnaps.Add(size)
355-
case bytes.HasPrefix(key, PreimagePrefix) && len(key) == (len(PreimagePrefix)+common.HashLength):
356-
preimages.Add(size)
357-
case bytes.HasPrefix(key, configPrefix) && len(key) == (len(configPrefix)+common.HashLength):
358-
metadata.Add(size)
359-
case bytes.HasPrefix(key, upgradeConfigPrefix) && len(key) == (len(upgradeConfigPrefix)+common.HashLength):
360-
metadata.Add(size)
361-
case bytes.HasPrefix(key, bloomBitsPrefix) && len(key) == (len(bloomBitsPrefix)+10+common.HashLength):
362-
bloomBits.Add(size)
363-
case bytes.HasPrefix(key, BloomBitsIndexPrefix):
364-
bloomBits.Add(size)
365-
case bytes.HasPrefix(key, syncStorageTriesPrefix) && len(key) == syncStorageTriesKeyLength:
366-
syncProgress.Add(size)
367-
case bytes.HasPrefix(key, syncSegmentsPrefix) && len(key) == syncSegmentsKeyLength:
368-
syncSegments.Add(size)
369-
case bytes.HasPrefix(key, CodeToFetchPrefix) && len(key) == codeToFetchKeyLength:
370-
codeToFetch.Add(size)
371-
case bytes.HasPrefix(key, syncPerformedPrefix) && len(key) == syncPerformedKeyLength:
372-
syncPerformed.Add(size)
373-
default:
374-
var accounted bool
375-
for _, meta := range [][]byte{
376-
databaseVersionKey, headHeaderKey, headBlockKey,
377-
snapshotRootKey, snapshotBlockHashKey, snapshotGeneratorKey,
378-
uncleanShutdownKey, syncRootKey, txIndexTailKey,
379-
persistentStateIDKey, trieJournalKey,
380-
} {
381-
if bytes.Equal(key, meta) {
382-
metadata.Add(size)
383-
accounted = true
384-
break
385-
}
253+
254+
options := []ethrawdb.InspectDatabaseOption{
255+
ethrawdb.WithDatabaseMetadataKeys(func(key []byte) bool {
256+
return bytes.Equal(key, snapshotBlockHashKey) ||
257+
bytes.Equal(key, syncRootKey) ||
258+
(bytes.HasPrefix(key, upgradeConfigPrefix) && len(key) == len(upgradeConfigPrefix)+common.HashLength)
259+
}),
260+
ethrawdb.WithDatabaseStatRecorder(func(key []byte, size common.StorageSize) bool {
261+
switch {
262+
case bytes.HasPrefix(key, syncSegmentsPrefix) && len(key) == syncSegmentsKeyLength:
263+
syncSegments.Add(size)
264+
return true
265+
case bytes.HasPrefix(key, syncStorageTriesPrefix) && len(key) == syncStorageTriesKeyLength:
266+
syncProgress.Add(size)
267+
return true
268+
case bytes.HasPrefix(key, CodeToFetchPrefix) && len(key) == codeToFetchKeyLength:
269+
codeToFetch.Add(size)
270+
return true
271+
case bytes.HasPrefix(key, syncPerformedPrefix) && len(key) == syncPerformedKeyLength:
272+
syncPerformed.Add(size)
273+
return true
274+
default:
275+
return false
386276
}
387-
if !accounted {
388-
unaccounted.Add(size)
277+
}),
278+
ethrawdb.WithDatabaseStatsTransformer(func(rows [][]string) [][]string {
279+
newRows := make([][]string, 0, len(rows))
280+
for _, row := range rows {
281+
database := row[0]
282+
category := row[1]
283+
switch {
284+
case database == "Key-Value store" && category == "Difficulties",
285+
database == "Key-Value store" && category == "Beacon sync headers",
286+
database == "Ancient store (Chain)":
287+
// Discard rows specific to libevm (geth) but irrelevant to coreth.
288+
continue
289+
}
290+
newRows = append(newRows, row)
389291
}
390-
}
391-
count++
392-
if count%1000 == 0 && time.Since(logged) > 8*time.Second {
393-
log.Info("Inspecting database", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
394-
logged = time.Now()
395-
}
396-
}
397-
// Display the database statistic.
398-
stats := [][]string{
399-
{"Key-Value store", "Headers", headers.Size(), headers.Count()},
400-
{"Key-Value store", "Bodies", bodies.Size(), bodies.Count()},
401-
{"Key-Value store", "Receipt lists", receipts.Size(), receipts.Count()},
402-
{"Key-Value store", "Block number->hash", numHashPairings.Size(), numHashPairings.Count()},
403-
{"Key-Value store", "Block hash->number", hashNumPairings.Size(), hashNumPairings.Count()},
404-
{"Key-Value store", "Transaction index", txLookups.Size(), txLookups.Count()},
405-
{"Key-Value store", "Bloombit index", bloomBits.Size(), bloomBits.Count()},
406-
{"Key-Value store", "Contract codes", codes.Size(), codes.Count()},
407-
{"Key-Value store", "Hash trie nodes", legacyTries.Size(), legacyTries.Count()},
408-
{"Key-Value store", "Path trie state lookups", stateLookups.Size(), stateLookups.Count()},
409-
{"Key-Value store", "Path trie account nodes", accountTries.Size(), accountTries.Count()},
410-
{"Key-Value store", "Path trie storage nodes", storageTries.Size(), storageTries.Count()},
411-
{"Key-Value store", "Trie preimages", preimages.Size(), preimages.Count()},
412-
{"Key-Value store", "Account snapshot", accountSnaps.Size(), accountSnaps.Count()},
413-
{"Key-Value store", "Storage snapshot", storageSnaps.Size(), storageSnaps.Count()},
414-
{"Key-Value store", "Clique snapshots", cliqueSnaps.Size(), cliqueSnaps.Count()},
415-
{"Key-Value store", "Singleton metadata", metadata.Size(), metadata.Count()},
416-
{"Light client", "CHT trie nodes", chtTrieNodes.Size(), chtTrieNodes.Count()},
417-
{"Light client", "Bloom trie nodes", bloomTrieNodes.Size(), bloomTrieNodes.Count()},
418-
{"State sync", "Trie segments", syncSegments.Size(), syncSegments.Count()},
419-
{"State sync", "Storage tries to fetch", syncProgress.Size(), syncProgress.Count()},
420-
{"State sync", "Code to fetch", codeToFetch.Size(), codeToFetch.Count()},
421-
{"State sync", "Block numbers synced to", syncPerformed.Size(), syncPerformed.Count()},
422-
}
423-
table := tablewriter.NewWriter(os.Stdout)
424-
table.SetHeader([]string{"Database", "Category", "Size", "Items"})
425-
table.SetFooter([]string{"", "Total", total.String(), " "})
426-
table.AppendBulk(stats)
427-
table.Render()
428-
429-
if unaccounted.size > 0 {
430-
log.Error("Database contains unaccounted data", "size", unaccounted.size, "count", unaccounted.count)
292+
293+
return append(
294+
newRows,
295+
[]string{"State sync", "Trie segments", syncSegments.Size(), syncSegments.Count()},
296+
[]string{"State sync", "Storage tries to fetch", syncProgress.Size(), syncProgress.Count()},
297+
[]string{"State sync", "Code to fetch", codeToFetch.Size(), codeToFetch.Count()},
298+
[]string{"State sync", "Block numbers synced to", syncPerformed.Size(), syncPerformed.Count()},
299+
)
300+
}),
431301
}
432-
return nil
302+
303+
return ethrawdb.InspectDatabase(db, keyPrefix, keyStart, options...)
433304
}
434305

435306
// ClearPrefix removes all keys in db that begin with prefix and match an

0 commit comments

Comments
 (0)