From bf92a50dd4fe3d06052deec3f7d7bcb25fac1da0 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 8 Nov 2023 10:34:17 +0100 Subject: [PATCH] review feedback: reinstate import function and dump preimages as an RLP stream --- cmd/geth/chaincmd.go | 38 ++++++++++++++++++++++++++++++++++++++ cmd/utils/cmd.go | 6 +++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index bfa91095f2..61f07db141 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -121,6 +121,21 @@ last block to write. In this mode, the file will be appended if already existing. If the file ends with .gz, the output will be gzipped.`, } + importPreimagesCommand = &cli.Command{ + Action: importPreimages, + Name: "import-preimages", + Usage: "Import the preimage database from an RLP stream", + ArgsUsage: "", + Flags: flags.Merge([]cli.Flag{ + utils.CacheFlag, + utils.SyncModeFlag, + }, utils.DatabaseFlags), + Description: ` +The import-preimages command imports hash preimages from an RLP encoded stream. +It's deprecated, please use "geth db import" instead. +`, + } + dumpCommand = &cli.Command{ Action: dump, Name: "dump", @@ -339,6 +354,29 @@ func exportChain(ctx *cli.Context) error { return nil } +// importPreimages imports preimage data from the specified file. +// it is deprecated, and the export function has been removed, but +// the import function is kept around for the time being so that +// older file formats can still be imported. +func importPreimages(ctx *cli.Context) error { + if ctx.Args().Len() < 1 { + utils.Fatalf("This command requires an argument.") + } + + stack, _ := makeConfigNode(ctx) + defer stack.Close() + + db := utils.MakeChainDatabase(ctx, stack, false) + defer db.Close() + start := time.Now() + + if err := utils.ImportPreimages(db, ctx.Args().First()); err != nil { + utils.Fatalf("Import error: %v\n", err) + } + fmt.Printf("Import done in %v\n", time.Since(start)) + return nil +} + func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, ethdb.Database, common.Hash, error) { db := utils.MakeChainDatabase(ctx, stack, true) defer db.Close() diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 4c265b74d8..1461174329 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -458,7 +458,11 @@ func ExportSnapshotPreimages(chaindb ethdb.Database, snaptree *snapshot.Tree, fn if len(preimage) != item.Size { return fmt.Errorf("invalid preimage size, have %d", len(preimage)) } - if _, err := writer.Write(preimage); err != nil { + rlpenc, err := rlp.EncodeToBytes(preimage) + if err != nil { + return fmt.Errorf("error encoding preimage: %w", err) + } + if _, err := writer.Write(rlpenc); err != nil { return fmt.Errorf("failed to write preimage: %w", err) } }