cmd/geth: parseDumpConfig should not return closed db (#29100)

* cmd: parseDumpConfig should not return closed db

* fix lint
This commit is contained in:
Ng Wei Han 2024-02-29 17:56:17 +08:00 committed by GitHub
parent dbc27a199f
commit 28d55218f7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 13 deletions

View file

@ -514,13 +514,10 @@ func importPreimages(ctx *cli.Context) error {
return nil return nil
} }
func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, ethdb.Database, common.Hash, error) { func parseDumpConfig(ctx *cli.Context, stack *node.Node, db ethdb.Database) (*state.DumpConfig, common.Hash, error) {
db := utils.MakeChainDatabase(ctx, stack, true)
defer db.Close()
var header *types.Header var header *types.Header
if ctx.NArg() > 1 { if ctx.NArg() > 1 {
return nil, nil, common.Hash{}, fmt.Errorf("expected 1 argument (number or hash), got %d", ctx.NArg()) return nil, common.Hash{}, fmt.Errorf("expected 1 argument (number or hash), got %d", ctx.NArg())
} }
if ctx.NArg() == 1 { if ctx.NArg() == 1 {
arg := ctx.Args().First() arg := ctx.Args().First()
@ -529,17 +526,17 @@ func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, eth
if number := rawdb.ReadHeaderNumber(db, hash); number != nil { if number := rawdb.ReadHeaderNumber(db, hash); number != nil {
header = rawdb.ReadHeader(db, hash, *number) header = rawdb.ReadHeader(db, hash, *number)
} else { } else {
return nil, nil, common.Hash{}, fmt.Errorf("block %x not found", hash) return nil, common.Hash{}, fmt.Errorf("block %x not found", hash)
} }
} else { } else {
number, err := strconv.ParseUint(arg, 10, 64) number, err := strconv.ParseUint(arg, 10, 64)
if err != nil { if err != nil {
return nil, nil, common.Hash{}, err return nil, common.Hash{}, err
} }
if hash := rawdb.ReadCanonicalHash(db, number); hash != (common.Hash{}) { if hash := rawdb.ReadCanonicalHash(db, number); hash != (common.Hash{}) {
header = rawdb.ReadHeader(db, hash, number) header = rawdb.ReadHeader(db, hash, number)
} else { } else {
return nil, nil, common.Hash{}, fmt.Errorf("header for block %d not found", number) return nil, common.Hash{}, fmt.Errorf("header for block %d not found", number)
} }
} }
} else { } else {
@ -547,7 +544,7 @@ func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, eth
header = rawdb.ReadHeadHeader(db) header = rawdb.ReadHeadHeader(db)
} }
if header == nil { if header == nil {
return nil, nil, common.Hash{}, errors.New("no head block found") return nil, common.Hash{}, errors.New("no head block found")
} }
startArg := common.FromHex(ctx.String(utils.StartKeyFlag.Name)) startArg := common.FromHex(ctx.String(utils.StartKeyFlag.Name))
var start common.Hash var start common.Hash
@ -559,7 +556,7 @@ func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, eth
start = crypto.Keccak256Hash(startArg) start = crypto.Keccak256Hash(startArg)
log.Info("Converting start-address to hash", "address", common.BytesToAddress(startArg), "hash", start.Hex()) log.Info("Converting start-address to hash", "address", common.BytesToAddress(startArg), "hash", start.Hex())
default: default:
return nil, nil, common.Hash{}, fmt.Errorf("invalid start argument: %x. 20 or 32 hex-encoded bytes required", startArg) return nil, common.Hash{}, fmt.Errorf("invalid start argument: %x. 20 or 32 hex-encoded bytes required", startArg)
} }
var conf = &state.DumpConfig{ var conf = &state.DumpConfig{
SkipCode: ctx.Bool(utils.ExcludeCodeFlag.Name), SkipCode: ctx.Bool(utils.ExcludeCodeFlag.Name),
@ -571,14 +568,17 @@ func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, eth
log.Info("State dump configured", "block", header.Number, "hash", header.Hash().Hex(), log.Info("State dump configured", "block", header.Number, "hash", header.Hash().Hex(),
"skipcode", conf.SkipCode, "skipstorage", conf.SkipStorage, "skipcode", conf.SkipCode, "skipstorage", conf.SkipStorage,
"start", hexutil.Encode(conf.Start), "limit", conf.Max) "start", hexutil.Encode(conf.Start), "limit", conf.Max)
return conf, db, header.Root, nil return conf, header.Root, nil
} }
func dump(ctx *cli.Context) error { func dump(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx) stack, _ := makeConfigNode(ctx)
defer stack.Close() defer stack.Close()
conf, db, root, err := parseDumpConfig(ctx, stack) db := utils.MakeChainDatabase(ctx, stack, true)
defer db.Close()
conf, root, err := parseDumpConfig(ctx, stack, db)
if err != nil { if err != nil {
return err return err
} }

View file

@ -541,7 +541,10 @@ func dumpState(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx) stack, _ := makeConfigNode(ctx)
defer stack.Close() defer stack.Close()
conf, db, root, err := parseDumpConfig(ctx, stack) db := utils.MakeChainDatabase(ctx, stack, true)
defer db.Close()
conf, root, err := parseDumpConfig(ctx, stack, db)
if err != nil { if err != nil {
return err return err
} }