@@ -18,15 +18,17 @@ package main
18
18
19
19
import (
20
20
"encoding/json"
21
+ "path/filepath"
21
22
"fmt"
22
23
"os"
23
24
"runtime"
24
25
"strconv"
25
26
"sync/atomic"
26
27
"time"
27
28
28
- "github.com/CortexFoundation/CortexTheseus/cmd/utils "
29
+ "github.com/CortexFoundation/CortexTheseus/core/rawdb "
29
30
"github.com/CortexFoundation/CortexTheseus/common"
31
+ "github.com/CortexFoundation/CortexTheseus/cmd/utils"
30
32
"github.com/CortexFoundation/CortexTheseus/console"
31
33
"github.com/CortexFoundation/CortexTheseus/core"
32
34
"github.com/CortexFoundation/CortexTheseus/core/state"
@@ -36,7 +38,7 @@ import (
36
38
"github.com/CortexFoundation/CortexTheseus/event"
37
39
"github.com/CortexFoundation/CortexTheseus/log"
38
40
"github.com/CortexFoundation/CortexTheseus/trie"
39
- "github.com/syndtr/goleveldb/leveldb/util"
41
+ // "github.com/syndtr/goleveldb/leveldb/util"
40
42
"gopkg.in/urfave/cli.v1"
41
43
)
42
44
@@ -169,6 +171,19 @@ Remove blockchain and state databases`,
169
171
The arguments are interpreted as block numbers or hashes.
170
172
Use "cortex dump 0" to dump the genesis block.` ,
171
173
}
174
+ inspectCommand = cli.Command {
175
+ Action : utils .MigrateFlags (inspect ),
176
+ Name : "inspect" ,
177
+ Usage : "Inspect the storage size for each type of data in the database" ,
178
+ ArgsUsage : " " ,
179
+ Flags : []cli.Flag {
180
+ utils .DataDirFlag ,
181
+ utils .AncientFlag ,
182
+ utils .CacheFlag ,
183
+ utils .SyncModeFlag ,
184
+ },
185
+ Category : "BLOCKCHAIN COMMANDS" ,
186
+ }
172
187
)
173
188
174
189
// initGenesis will initialise the given JSON format genesis file and writes it as
@@ -192,7 +207,7 @@ func initGenesis(ctx *cli.Context) error {
192
207
// Open an initialise both full and light databases
193
208
stack := makeFullNode (ctx )
194
209
for _ , name := range []string {"chaindata" } {
195
- chaindb , err := stack .OpenDatabase (name , 0 , 0 )
210
+ chaindb , err := stack .OpenDatabase (name , 0 , 0 , "" )
196
211
if err != nil {
197
212
utils .Fatalf ("Failed to open database: %v" , err )
198
213
}
@@ -246,15 +261,17 @@ func importChain(ctx *cli.Context) error {
246
261
fmt .Printf ("Import done in %v.\n \n " , time .Since (start ))
247
262
248
263
// Output pre-compaction stats mostly to see the import trashing
249
- db := chainDb .(* ctxcdb.LDBDatabase )
264
+ db := chainDb .(ctxcdb.Database )
250
265
251
- stats , err := db .LDB ().GetProperty ("leveldb.stats" )
266
+ //stats, err := db.LDB().GetProperty("leveldb.stats")
267
+ stats , err := db .Stat ("leveldb.stats" )
252
268
if err != nil {
253
269
utils .Fatalf ("Failed to read database stats: %v" , err )
254
270
}
255
271
fmt .Println (stats )
256
272
257
- ioStats , err := db .LDB ().GetProperty ("leveldb.iostats" )
273
+ //ioStats, err := db.LDB().GetProperty("leveldb.iostats")
274
+ ioStats , err := db .Stat ("leveldb.iostats" )
258
275
if err != nil {
259
276
utils .Fatalf ("Failed to read database iostats: %v" , err )
260
277
}
@@ -279,18 +296,18 @@ func importChain(ctx *cli.Context) error {
279
296
// Compact the entire database to more accurately measure disk io and print the stats
280
297
start = time .Now ()
281
298
fmt .Println ("Compacting entire database..." )
282
- if err = db .LDB (). CompactRange (util. Range {} ); err != nil {
283
- utils .Fatalf ("Compaction failed: %v" , err )
284
- }
299
+ if err = db .Compact ( nil , nil ); err != nil {
300
+ utils .Fatalf ("Compaction failed: %v" , err )
301
+ }
285
302
fmt .Printf ("Compaction done in %v.\n \n " , time .Since (start ))
286
303
287
- stats , err = db .LDB (). GetProperty ("leveldb.stats" )
304
+ stats , err = db .Stat ("leveldb.stats" )
288
305
if err != nil {
289
306
utils .Fatalf ("Failed to read database stats: %v" , err )
290
307
}
291
308
fmt .Println (stats )
292
309
293
- ioStats , err = db .LDB (). GetProperty ("leveldb.iostats" )
310
+ ioStats , err = db .Stat ("leveldb.iostats" )
294
311
if err != nil {
295
312
utils .Fatalf ("Failed to read database iostats: %v" , err )
296
313
}
@@ -337,7 +354,7 @@ func importPreimages(ctx *cli.Context) error {
337
354
utils .Fatalf ("This command requires an argument." )
338
355
}
339
356
stack := makeFullNode (ctx )
340
- diskdb := utils .MakeChainDatabase (ctx , stack ).(* ctxcdb.LDBDatabase )
357
+ diskdb := utils .MakeChainDatabase (ctx , stack ).(ctxcdb.Database )
341
358
342
359
start := time .Now ()
343
360
if err := utils .ImportPreimages (diskdb , ctx .Args ().First ()); err != nil {
@@ -353,7 +370,7 @@ func exportPreimages(ctx *cli.Context) error {
353
370
utils .Fatalf ("This command requires an argument." )
354
371
}
355
372
stack := makeFullNode (ctx )
356
- diskdb := utils .MakeChainDatabase (ctx , stack ).(* ctxcdb.LDBDatabase )
373
+ diskdb := utils .MakeChainDatabase (ctx , stack ).(ctxcdb.Database )
357
374
358
375
start := time .Now ()
359
376
if err := utils .ExportPreimages (diskdb , ctx .Args ().First ()); err != nil {
@@ -365,9 +382,13 @@ func exportPreimages(ctx *cli.Context) error {
365
382
366
383
func copyDb (ctx * cli.Context ) error {
367
384
// Ensure we have a source chain directory to copy
368
- if len (ctx .Args ()) != 1 {
385
+ if len (ctx .Args ()) < 1 {
369
386
utils .Fatalf ("Source chaindata directory path argument missing" )
370
387
}
388
+
389
+ if len (ctx .Args ()) < 2 {
390
+ utils .Fatalf ("Source ancient chain directory path argument missing" )
391
+ }
371
392
// Initialize a new chain for the running node to sync into
372
393
stack := makeFullNode (ctx )
373
394
chain , chainDb := utils .MakeChain (ctx , stack )
@@ -376,7 +397,8 @@ func copyDb(ctx *cli.Context) error {
376
397
dl := downloader .New (syncmode , 0 , chainDb , new (event.TypeMux ), chain , nil )
377
398
378
399
// Create a source peer to satisfy downloader requests from
379
- db , err := ctxcdb .NewLDBDatabase (ctx .Args ().First (), ctx .GlobalInt (utils .CacheFlag .Name ), 256 )
400
+ //db, err := ctxcdb.NewLevelDatabase(ctx.Args().First(), ctx.GlobalInt(utils.CacheFlag.Name), 256)
401
+ db , err := rawdb .NewLevelDBDatabaseWithFreezer (ctx .Args ().First (), ctx .GlobalInt (utils .CacheFlag .Name )/ 2 , 256 , ctx .Args ().Get (1 ), "" )
380
402
if err != nil {
381
403
return err
382
404
}
@@ -403,41 +425,65 @@ func copyDb(ctx *cli.Context) error {
403
425
// Compact the entire database to remove any sync overhead
404
426
start = time .Now ()
405
427
fmt .Println ("Compacting entire database..." )
406
- if err = chainDb .( * ctxcdb. LDBDatabase ). LDB (). CompactRange (util. Range {} ); err != nil {
407
- utils .Fatalf ("Compaction failed: %v" , err )
408
- }
428
+ if err = db . Compact ( nil , nil ); err != nil {
429
+ utils .Fatalf ("Compaction failed: %v" , err )
430
+ }
409
431
fmt .Printf ("Compaction done in %v.\n \n " , time .Since (start ))
410
432
411
433
return nil
412
434
}
413
435
414
436
func removeDB (ctx * cli.Context ) error {
415
- stack , _ := makeConfigNode (ctx )
416
-
417
- for _ , name := range []string {"chaindata" } {
418
- // Ensure the database exists in the first place
419
- logger := log .New ("database" , name )
437
+ stack , config := makeConfigNode (ctx )
438
+
439
+ // Remove the full node state database
440
+ path := stack .ResolvePath ("chaindata" )
441
+ if common .FileExist (path ) {
442
+ confirmAndRemoveDB (path , "full node state database" )
443
+ } else {
444
+ log .Info ("Full node state database missing" , "path" , path )
445
+ }
446
+ // Remove the full node ancient database
447
+ path = config .Cortex .DatabaseFreezer
448
+ switch {
449
+ case path == "" :
450
+ path = filepath .Join (stack .ResolvePath ("chaindata" ), "ancient" )
451
+ case ! filepath .IsAbs (path ):
452
+ path = config .Node .ResolvePath (path )
453
+ }
454
+ if common .FileExist (path ) {
455
+ confirmAndRemoveDB (path , "full node ancient database" )
456
+ } else {
457
+ log .Info ("Full node ancient database missing" , "path" , path )
458
+ }
459
+ return nil
460
+ }
420
461
421
- dbdir := stack .ResolvePath (name )
422
- if ! common .FileExist (dbdir ) {
423
- logger .Info ("Database doesn't exist, skipping" , "path" , dbdir )
424
- continue
425
- }
426
- // Confirm removal and execute
427
- fmt .Println (dbdir )
428
- confirm , err := console .Stdin .PromptConfirm ("Remove this database?" )
429
- switch {
430
- case err != nil :
431
- utils .Fatalf ("%v" , err )
432
- case ! confirm :
433
- logger .Warn ("Database deletion aborted" )
434
- default :
435
- start := time .Now ()
436
- os .RemoveAll (dbdir )
437
- logger .Info ("Database successfully deleted" , "elapsed" , common .PrettyDuration (time .Since (start )))
438
- }
439
- }
440
- return nil
462
+ // confirmAndRemoveDB prompts the user for a last confirmation and removes the
463
+ // folder if accepted.
464
+ func confirmAndRemoveDB (database string , kind string ) {
465
+ confirm , err := console .Stdin .PromptConfirm (fmt .Sprintf ("Remove %s (%s)?" , kind , database ))
466
+ switch {
467
+ case err != nil :
468
+ utils .Fatalf ("%v" , err )
469
+ case ! confirm :
470
+ log .Info ("Database deletion skipped" , "path" , database )
471
+ default :
472
+ start := time .Now ()
473
+ filepath .Walk (database , func (path string , info os.FileInfo , err error ) error {
474
+ // If we're at the top level folder, recurse into
475
+ if path == database {
476
+ return nil
477
+ }
478
+ // Delete all the files, but not subfolders
479
+ if ! info .IsDir () {
480
+ os .Remove (path )
481
+ return nil
482
+ }
483
+ return filepath .SkipDir
484
+ })
485
+ log .Info ("Database successfully deleted" , "path" , database , "elapsed" , common .PrettyDuration (time .Since (start )))
486
+ }
441
487
}
442
488
443
489
func dump (ctx * cli.Context ) error {
@@ -466,6 +512,17 @@ func dump(ctx *cli.Context) error {
466
512
return nil
467
513
}
468
514
515
+ func inspect (ctx * cli.Context ) error {
516
+ node , _ := makeConfigNode (ctx )
517
+ defer node .Close ()
518
+
519
+ _ , chainDb := utils .MakeChain (ctx , node )
520
+ defer chainDb .Close ()
521
+
522
+ return rawdb .InspectDatabase (chainDb )
523
+ }
524
+
525
+
469
526
// hashish returns true for strings that look like hashes.
470
527
func hashish (x string ) bool {
471
528
_ , err := strconv .Atoi (x )
0 commit comments