@@ -31,15 +31,14 @@ import (
31
31
"fmt"
32
32
"os"
33
33
"path/filepath"
34
- "time"
35
34
36
35
"github.com/ava-labs/libevm/common"
36
+ ethrawdb "github.com/ava-labs/libevm/core/rawdb"
37
37
"github.com/ava-labs/libevm/ethdb"
38
38
"github.com/ava-labs/libevm/ethdb/leveldb"
39
39
"github.com/ava-labs/libevm/ethdb/memorydb"
40
40
"github.com/ava-labs/libevm/ethdb/pebble"
41
41
"github.com/ava-labs/libevm/log"
42
- "github.com/olekukonko/tablewriter"
43
42
)
44
43
45
44
// nofreezedb is a database wrapper that disables freezer data retrievals.
@@ -275,159 +274,61 @@ func (s *stat) Count() string {
275
274
// InspectDatabase traverses the entire database and checks the size
276
275
// of all different categories of data.
277
276
func InspectDatabase (db ethdb.Database , keyPrefix , keyStart []byte ) error {
278
- it := db .NewIterator (keyPrefix , keyStart )
279
- defer it .Release ()
280
-
281
277
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
278
+ codeToFetch ethrawdb.DatabaseStat
279
+ syncPerformed ethrawdb.DatabaseStat
280
+ syncProgress ethrawdb.DatabaseStat
281
+ syncSegments ethrawdb.DatabaseStat
320
282
)
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 , bloomBitsPrefix ) && len (key ) == (len (bloomBitsPrefix )+ 10 + common .HashLength ):
360
- bloomBits .Add (size )
361
- case bytes .HasPrefix (key , BloomBitsIndexPrefix ):
362
- bloomBits .Add (size )
363
- case bytes .HasPrefix (key , syncStorageTriesPrefix ) && len (key ) == syncStorageTriesKeyLength :
364
- syncProgress .Add (size )
365
- case bytes .HasPrefix (key , syncSegmentsPrefix ) && len (key ) == syncSegmentsKeyLength :
366
- syncSegments .Add (size )
367
- case bytes .HasPrefix (key , CodeToFetchPrefix ) && len (key ) == codeToFetchKeyLength :
368
- codeToFetch .Add (size )
369
- case bytes .HasPrefix (key , syncPerformedPrefix ) && len (key ) == syncPerformedKeyLength :
370
- syncPerformed .Add (size )
371
- default :
372
- var accounted bool
373
- for _ , meta := range [][]byte {
374
- databaseVersionKey , headHeaderKey , headBlockKey ,
375
- snapshotRootKey , snapshotBlockHashKey , snapshotGeneratorKey ,
376
- uncleanShutdownKey , syncRootKey , txIndexTailKey ,
377
- persistentStateIDKey , trieJournalKey ,
378
- } {
379
- if bytes .Equal (key , meta ) {
380
- metadata .Add (size )
381
- accounted = true
382
- break
383
- }
283
+
284
+ options := []ethrawdb.InspectDatabaseOption {
285
+ ethrawdb .WithDatabaseMetadataKeys (func (key []byte ) bool {
286
+ return bytes .Equal (key , snapshotBlockHashKey ) ||
287
+ bytes .Equal (key , syncRootKey )
288
+ }),
289
+ ethrawdb .WithDatabaseStatRecorder (func (key []byte , size common.StorageSize ) bool {
290
+ switch {
291
+ case bytes .HasPrefix (key , syncSegmentsPrefix ) && len (key ) == syncSegmentsKeyLength :
292
+ syncSegments .Add (size )
293
+ return true
294
+ case bytes .HasPrefix (key , syncStorageTriesPrefix ) && len (key ) == syncStorageTriesKeyLength :
295
+ syncProgress .Add (size )
296
+ return true
297
+ case bytes .HasPrefix (key , CodeToFetchPrefix ) && len (key ) == codeToFetchKeyLength :
298
+ codeToFetch .Add (size )
299
+ return true
300
+ case bytes .HasPrefix (key , syncPerformedPrefix ) && len (key ) == syncPerformedKeyLength :
301
+ syncPerformed .Add (size )
302
+ return true
303
+ default :
304
+ return false
384
305
}
385
- if ! accounted {
386
- unaccounted .Add (size )
306
+ }),
307
+ ethrawdb .WithDatabaseStatsTransformer (func (rows [][]string ) [][]string {
308
+ newRows := make ([][]string , 0 , len (rows ))
309
+ for _ , row := range rows {
310
+ database := row [0 ]
311
+ category := row [1 ]
312
+ switch {
313
+ case database == "Key-Value store" && category == "Difficulties" ,
314
+ database == "Key-Value store" && category == "Beacon sync headers" ,
315
+ database == "Ancient store (Chain)" :
316
+ continue
317
+ }
318
+ newRows = append (newRows , row )
387
319
}
388
- }
389
- count ++
390
- if count % 1000 == 0 && time .Since (logged ) > 8 * time .Second {
391
- log .Info ("Inspecting database" , "count" , count , "elapsed" , common .PrettyDuration (time .Since (start )))
392
- logged = time .Now ()
393
- }
394
- }
395
- // Display the database statistic.
396
- stats := [][]string {
397
- {"Key-Value store" , "Headers" , headers .Size (), headers .Count ()},
398
- {"Key-Value store" , "Bodies" , bodies .Size (), bodies .Count ()},
399
- {"Key-Value store" , "Receipt lists" , receipts .Size (), receipts .Count ()},
400
- {"Key-Value store" , "Block number->hash" , numHashPairings .Size (), numHashPairings .Count ()},
401
- {"Key-Value store" , "Block hash->number" , hashNumPairings .Size (), hashNumPairings .Count ()},
402
- {"Key-Value store" , "Transaction index" , txLookups .Size (), txLookups .Count ()},
403
- {"Key-Value store" , "Bloombit index" , bloomBits .Size (), bloomBits .Count ()},
404
- {"Key-Value store" , "Contract codes" , codes .Size (), codes .Count ()},
405
- {"Key-Value store" , "Hash trie nodes" , legacyTries .Size (), legacyTries .Count ()},
406
- {"Key-Value store" , "Path trie state lookups" , stateLookups .Size (), stateLookups .Count ()},
407
- {"Key-Value store" , "Path trie account nodes" , accountTries .Size (), accountTries .Count ()},
408
- {"Key-Value store" , "Path trie storage nodes" , storageTries .Size (), storageTries .Count ()},
409
- {"Key-Value store" , "Trie preimages" , preimages .Size (), preimages .Count ()},
410
- {"Key-Value store" , "Account snapshot" , accountSnaps .Size (), accountSnaps .Count ()},
411
- {"Key-Value store" , "Storage snapshot" , storageSnaps .Size (), storageSnaps .Count ()},
412
- {"Key-Value store" , "Clique snapshots" , cliqueSnaps .Size (), cliqueSnaps .Count ()},
413
- {"Key-Value store" , "Singleton metadata" , metadata .Size (), metadata .Count ()},
414
- {"Light client" , "CHT trie nodes" , chtTrieNodes .Size (), chtTrieNodes .Count ()},
415
- {"Light client" , "Bloom trie nodes" , bloomTrieNodes .Size (), bloomTrieNodes .Count ()},
416
- {"State sync" , "Trie segments" , syncSegments .Size (), syncSegments .Count ()},
417
- {"State sync" , "Storage tries to fetch" , syncProgress .Size (), syncProgress .Count ()},
418
- {"State sync" , "Code to fetch" , codeToFetch .Size (), codeToFetch .Count ()},
419
- {"State sync" , "Block numbers synced to" , syncPerformed .Size (), syncPerformed .Count ()},
420
- }
421
- table := tablewriter .NewWriter (os .Stdout )
422
- table .SetHeader ([]string {"Database" , "Category" , "Size" , "Items" })
423
- table .SetFooter ([]string {"" , "Total" , total .String (), " " })
424
- table .AppendBulk (stats )
425
- table .Render ()
426
-
427
- if unaccounted .size > 0 {
428
- log .Error ("Database contains unaccounted data" , "size" , unaccounted .size , "count" , unaccounted .count )
320
+
321
+ return append (
322
+ newRows ,
323
+ []string {"State sync" , "Trie segments" , syncSegments .Size (), syncSegments .Count ()},
324
+ []string {"State sync" , "Storage tries to fetch" , syncProgress .Size (), syncProgress .Count ()},
325
+ []string {"State sync" , "Code to fetch" , codeToFetch .Size (), codeToFetch .Count ()},
326
+ []string {"State sync" , "Block numbers synced to" , syncPerformed .Size (), syncPerformed .Count ()},
327
+ )
328
+ }),
429
329
}
430
- return nil
330
+
331
+ return ethrawdb .InspectDatabase (db , keyPrefix , keyStart , options ... )
431
332
}
432
333
433
334
// ClearPrefix removes all keys in db that begin with prefix and match an
0 commit comments