Skip to content

Commit 28d5521

Browse files
authored
cmd/geth: parseDumpConfig should not return closed db (ethereum#29100)
* cmd: parseDumpConfig should not return closed db * fix lint
1 parent dbc27a1 commit 28d5521

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

cmd/geth/chaincmd.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,10 @@ func importPreimages(ctx *cli.Context) error {
514514
return nil
515515
}
516516

517-
func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, ethdb.Database, common.Hash, error) {
518-
db := utils.MakeChainDatabase(ctx, stack, true)
519-
defer db.Close()
520-
517+
func parseDumpConfig(ctx *cli.Context, stack *node.Node, db ethdb.Database) (*state.DumpConfig, common.Hash, error) {
521518
var header *types.Header
522519
if ctx.NArg() > 1 {
523-
return nil, nil, common.Hash{}, fmt.Errorf("expected 1 argument (number or hash), got %d", ctx.NArg())
520+
return nil, common.Hash{}, fmt.Errorf("expected 1 argument (number or hash), got %d", ctx.NArg())
524521
}
525522
if ctx.NArg() == 1 {
526523
arg := ctx.Args().First()
@@ -529,25 +526,25 @@ func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, eth
529526
if number := rawdb.ReadHeaderNumber(db, hash); number != nil {
530527
header = rawdb.ReadHeader(db, hash, *number)
531528
} else {
532-
return nil, nil, common.Hash{}, fmt.Errorf("block %x not found", hash)
529+
return nil, common.Hash{}, fmt.Errorf("block %x not found", hash)
533530
}
534531
} else {
535532
number, err := strconv.ParseUint(arg, 10, 64)
536533
if err != nil {
537-
return nil, nil, common.Hash{}, err
534+
return nil, common.Hash{}, err
538535
}
539536
if hash := rawdb.ReadCanonicalHash(db, number); hash != (common.Hash{}) {
540537
header = rawdb.ReadHeader(db, hash, number)
541538
} else {
542-
return nil, nil, common.Hash{}, fmt.Errorf("header for block %d not found", number)
539+
return nil, common.Hash{}, fmt.Errorf("header for block %d not found", number)
543540
}
544541
}
545542
} else {
546543
// Use latest
547544
header = rawdb.ReadHeadHeader(db)
548545
}
549546
if header == nil {
550-
return nil, nil, common.Hash{}, errors.New("no head block found")
547+
return nil, common.Hash{}, errors.New("no head block found")
551548
}
552549
startArg := common.FromHex(ctx.String(utils.StartKeyFlag.Name))
553550
var start common.Hash
@@ -559,7 +556,7 @@ func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, eth
559556
start = crypto.Keccak256Hash(startArg)
560557
log.Info("Converting start-address to hash", "address", common.BytesToAddress(startArg), "hash", start.Hex())
561558
default:
562-
return nil, nil, common.Hash{}, fmt.Errorf("invalid start argument: %x. 20 or 32 hex-encoded bytes required", startArg)
559+
return nil, common.Hash{}, fmt.Errorf("invalid start argument: %x. 20 or 32 hex-encoded bytes required", startArg)
563560
}
564561
var conf = &state.DumpConfig{
565562
SkipCode: ctx.Bool(utils.ExcludeCodeFlag.Name),
@@ -571,14 +568,17 @@ func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, eth
571568
log.Info("State dump configured", "block", header.Number, "hash", header.Hash().Hex(),
572569
"skipcode", conf.SkipCode, "skipstorage", conf.SkipStorage,
573570
"start", hexutil.Encode(conf.Start), "limit", conf.Max)
574-
return conf, db, header.Root, nil
571+
return conf, header.Root, nil
575572
}
576573

577574
func dump(ctx *cli.Context) error {
578575
stack, _ := makeConfigNode(ctx)
579576
defer stack.Close()
580577

581-
conf, db, root, err := parseDumpConfig(ctx, stack)
578+
db := utils.MakeChainDatabase(ctx, stack, true)
579+
defer db.Close()
580+
581+
conf, root, err := parseDumpConfig(ctx, stack, db)
582582
if err != nil {
583583
return err
584584
}

cmd/geth/snapshot.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,10 @@ func dumpState(ctx *cli.Context) error {
541541
stack, _ := makeConfigNode(ctx)
542542
defer stack.Close()
543543

544-
conf, db, root, err := parseDumpConfig(ctx, stack)
544+
db := utils.MakeChainDatabase(ctx, stack, true)
545+
defer db.Close()
546+
547+
conf, root, err := parseDumpConfig(ctx, stack, db)
545548
if err != nil {
546549
return err
547550
}

0 commit comments

Comments
 (0)