review feedback

This commit is contained in:
Guillaume Ballet 2023-10-24 12:43:40 +02:00
parent 3e49cebdef
commit 2fdc0da8b8
3 changed files with 42 additions and 37 deletions

View file

@ -171,8 +171,8 @@ This command dumps out the state for a given block (or latest, if none provided)
Action: exportSnapshotPreimages, Action: exportSnapshotPreimages,
Name: "export-snapshot-preimages", Name: "export-snapshot-preimages",
Usage: "Export the preimage in snapshot enumeration order", Usage: "Export the preimage in snapshot enumeration order",
ArgsUsage: "<dumpfile>", ArgsUsage: "<dumpfile> [<root>]",
Flags: flags.Merge([]cli.Flag{utils.TreeRootFlag}, utils.DatabaseFlags), Flags: utils.DatabaseFlags,
Description: ` Description: `
The export-snapshot-preimages command exports hash preimages to a flat file, in exactly The export-snapshot-preimages command exports hash preimages to a flat file, in exactly
the expected order for the overlay tree migration. the expected order for the overlay tree migration.
@ -428,8 +428,8 @@ 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.String(utils.TreeRootFlag.Name) != "" { if ctx.Args().Len() > 1 {
rootBytes := common.FromHex(ctx.String(utils.StartKeyFlag.Name)) 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 root hash length")
} }

View file

@ -397,45 +397,55 @@ func ExportSnapshotPreimages(chain *core.BlockChain, fn string, root common.Hash
root = chain.CurrentBlock().Root root = chain.CurrentBlock().Root
} }
accIt, err := chain.Snapshots().AccountIterator(root, common.Hash{}) type hashAndPreimageSize struct {
if err != nil { Hash common.Hash
return err Size int
} }
defer accIt.Release() var hashCh chan hashAndPreimageSize
count := 0 go func() {
for accIt.Next() { defer close(hashCh)
acc, err := types.FullAccount(accIt.Account()) accIt, err := chain.Snapshots().AccountIterator(root, common.Hash{})
if err != nil { if err != nil {
return fmt.Errorf("invalid account encountered during traversal: %s", err) log.Error("failed to create account iterator", "error", err)
} return
addr := rawdb.ReadPreimage(statedb.Database().DiskDB(), accIt.Hash())
if len(addr) != 20 {
return fmt.Errorf("addr len is zero is not 32: %d", len(addr))
}
if _, err := writer.Write(addr); err != nil {
return fmt.Errorf("failed to write addr preimage: %w", err)
} }
defer accIt.Release()
if acc.Root != (common.Hash{}) && acc.Root != types.EmptyRootHash { count := 0
stIt, err := chain.Snapshots().StorageIterator(root, accIt.Hash(), common.Hash{}) for accIt.Next() {
acc, err := types.FullAccount(accIt.Account())
if err != nil { if err != nil {
return fmt.Errorf("failed to create storage iterator: %w", err) log.Error("failed to get full account", "error", err)
return
} }
for stIt.Next() { hashCh <- hashAndPreimageSize{Hash: accIt.Hash(), Size: 20}
slotnr := rawdb.ReadPreimage(statedb.Database().DiskDB(), stIt.Hash())
if len(slotnr) != 32 { if acc.Root != (common.Hash{}) && acc.Root != types.EmptyRootHash {
return fmt.Errorf("slotnr not 32 len") stIt, err := chain.Snapshots().StorageIterator(root, accIt.Hash(), common.Hash{})
if err != nil {
log.Error("failed to create storage iterator", "error", err)
return
} }
if _, err := writer.Write(slotnr); err != nil { for stIt.Next() {
return fmt.Errorf("failed to write slotnr preimage: %w", err) hashCh <- hashAndPreimageSize{Hash: stIt.Hash(), Size: 32}
} }
stIt.Release()
}
count++
if count%100000 == 0 {
log.Info("Last exported account", "account", accIt.Hash())
} }
stIt.Release()
} }
count++ }()
if count%100000 == 0 {
log.Info("Last exported account", "account", accIt.Hash()) for item := range hashCh {
preimage := rawdb.ReadPreimage(statedb.Database().DiskDB(), item.Hash)
if len(preimage) != item.Size {
return fmt.Errorf("invalid preimage size")
}
if _, err := writer.Write(preimage); err != nil {
return fmt.Errorf("failed to write preimage: %w", err)
} }
} }

View file

@ -219,11 +219,6 @@ var (
Usage: "Max number of elements (0 = no limit)", Usage: "Max number of elements (0 = no limit)",
Value: 0, Value: 0,
} }
TreeRootFlag = &cli.StringFlag{
Name: "roothash",
Usage: "Root hash of the tree (if empty, use that of current block)",
Value: "",
}
defaultSyncMode = ethconfig.Defaults.SyncMode defaultSyncMode = ethconfig.Defaults.SyncMode
SnapshotFlag = &cli.BoolFlag{ SnapshotFlag = &cli.BoolFlag{