cmd/geth: add gz support, improve error messages

This commit is contained in:
Martin Holst Swende 2023-10-25 14:45:13 +02:00
parent e7ed73f0fe
commit 997eb2eb2c
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 18 additions and 6 deletions

View file

@ -419,7 +419,7 @@ func exportPreimages(ctx *cli.Context) error {
// exportSnapshotPreimages dumps the preimage data to a flat file. // exportSnapshotPreimages dumps the preimage data to a flat file.
func exportSnapshotPreimages(ctx *cli.Context) error { func exportSnapshotPreimages(ctx *cli.Context) error {
if ctx.Args().Len() < 1 { if ctx.NArg() < 1 {
utils.Fatalf("This command requires an argument.") utils.Fatalf("This command requires an argument.")
} }
stack, _ := makeConfigNode(ctx) stack, _ := makeConfigNode(ctx)
@ -428,10 +428,10 @@ func exportSnapshotPreimages(ctx *cli.Context) error {
chain, _ := utils.MakeChain(ctx, stack, true) chain, _ := utils.MakeChain(ctx, stack, true)
var root common.Hash var root common.Hash
if ctx.Args().Len() > 1 { if ctx.NArg() > 1 {
rootBytes := common.FromHex(ctx.Args().Get(1)) rootBytes := common.FromHex(ctx.Args().Get(1))
if len(rootBytes) != common.HashLength { if len(rootBytes) != common.HashLength {
return fmt.Errorf("invalid root hash length") return fmt.Errorf("invalid hash: %s", ctx.Args().Get(1))
} }
root = common.BytesToHash(rootBytes) root = common.BytesToHash(rootBytes)
} }

View file

@ -385,8 +385,17 @@ func ExportSnapshotPreimages(chain *core.BlockChain, fn string, root common.Hash
} }
defer fh.Close() defer fh.Close()
writer := bufio.NewWriter(fh) var writer io.Writer = fh
defer writer.Flush()
if strings.HasSuffix(fn, ".gz") {
gz := gzip.NewWriter(writer)
defer gz.Close()
writer = gz
}
buf := bufio.NewWriter(writer)
defer buf.Flush()
writer = buf
statedb, err := chain.State() statedb, err := chain.State()
if err != nil { if err != nil {
@ -441,8 +450,11 @@ func ExportSnapshotPreimages(chain *core.BlockChain, fn string, root common.Hash
for item := range hashCh { for item := range hashCh {
preimage := rawdb.ReadPreimage(statedb.Database().DiskDB(), item.Hash) preimage := rawdb.ReadPreimage(statedb.Database().DiskDB(), item.Hash)
if len(preimage) == 0 {
return fmt.Errorf("missing preimage for %v", item.Hash)
}
if len(preimage) != item.Size { if len(preimage) != item.Size {
return fmt.Errorf("invalid preimage size") return fmt.Errorf("invalid preimage size, have %d", len(preimage))
} }
if _, err := writer.Write(preimage); err != nil { if _, err := writer.Write(preimage); err != nil {
return fmt.Errorf("failed to write preimage: %w", err) return fmt.Errorf("failed to write preimage: %w", err)