-
Notifications
You must be signed in to change notification settings - Fork 20.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/{geth,utils}: add cmd to export preimages in snap enumeration order #28256
Changes from 4 commits
eba5366
3e49ceb
2fdc0da
e7ed73f
997eb2e
8eb2786
a915bda
272a0cc
b541aa5
bf92a50
e904b01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -374,6 +374,85 @@ func ExportPreimages(db ethdb.Database, fn string) error { | |
return nil | ||
} | ||
|
||
// ExportSnapshotPreimages exports the preimages corresponding to the enumeration of | ||
// the snapshot for a given root. | ||
func ExportSnapshotPreimages(chain *core.BlockChain, fn string, root common.Hash) error { | ||
log.Info("Exporting preimages", "file", fn) | ||
|
||
fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
defer fh.Close() | ||
|
||
writer := bufio.NewWriter(fh) | ||
defer writer.Flush() | ||
|
||
statedb, err := chain.State() | ||
if err != nil { | ||
return fmt.Errorf("failed to open statedb: %w", err) | ||
} | ||
|
||
if root == (common.Hash{}) { | ||
root = chain.CurrentBlock().Root | ||
} | ||
|
||
type hashAndPreimageSize struct { | ||
Hash common.Hash | ||
Size int | ||
} | ||
hashCh := make(chan hashAndPreimageSize) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should probably have a buffer here, so the source doesn't have to wait for the sink as much ? |
||
|
||
go func() { | ||
defer close(hashCh) | ||
accIt, err := chain.Snapshots().AccountIterator(root, common.Hash{}) | ||
if err != nil { | ||
log.Error("failed to create account iterator", "error", err) | ||
return | ||
} | ||
defer accIt.Release() | ||
|
||
count := 0 | ||
for accIt.Next() { | ||
acc, err := types.FullAccount(accIt.Account()) | ||
if err != nil { | ||
log.Error("failed to get full account", "error", err) | ||
return | ||
} | ||
hashCh <- hashAndPreimageSize{Hash: accIt.Hash(), Size: 20} | ||
|
||
if acc.Root != (common.Hash{}) && acc.Root != types.EmptyRootHash { | ||
stIt, err := chain.Snapshots().StorageIterator(root, accIt.Hash(), common.Hash{}) | ||
if err != nil { | ||
log.Error("failed to create storage iterator", "error", err) | ||
return | ||
} | ||
for stIt.Next() { | ||
hashCh <- hashAndPreimageSize{Hash: stIt.Hash(), Size: 32} | ||
} | ||
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") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we're missing a preimage, now it would just die with `invalid preimage size'. Wouldn't it be nicer to spit out at least the missing preimage (and maybe even continue: printing out all missing preimages, so the user can look them up and then redo the conversion?) |
||
if _, err := writer.Write(preimage); err != nil { | ||
return fmt.Errorf("failed to write preimage: %w", err) | ||
} | ||
} | ||
|
||
log.Info("Exported preimages", "file", fn) | ||
return nil | ||
} | ||
|
||
// exportHeader is used in the export/import flow. When we do an export, | ||
// the first element we output is the exportHeader. | ||
// Whenever a backwards-incompatible change is made, the Version header | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps
ExportActivePreimages
to signify that it is the preimages that are all active or relevant for the given state