-
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eba5366
cmd/{geth,utils}: add cmd to export preimages in snap enumeration order
gballet 3e49ceb
Update cmd/geth/chaincmd.go
gballet 2fdc0da
review feedback
gballet e7ed73f
fix channel init
gballet 997eb2e
cmd/geth: add gz support, improve error messages
holiman 8eb2786
cmd/geth: fix capitalization log messages
holiman a915bda
remove "ex|import-preimages" and move snapshot preimages to snapshot.go
gballet 272a0cc
fix complaint about Ethash
gballet b541aa5
cmd: polish exportPreimage command
rjl493456442 bf92a50
review feedback: reinstate import function and dump preimages as an R…
gballet e904b01
fix: reactivate preimage import command
gballet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ import ( | |
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core" | ||
"github.com/ethereum/go-ethereum/core/rawdb" | ||
"github.com/ethereum/go-ethereum/core/state/snapshot" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/eth/ethconfig" | ||
|
@@ -374,6 +375,88 @@ 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(chaindb ethdb.Database, snaptree *snapshot.Tree, 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() | ||
|
||
var writer io.Writer = fh | ||
|
||
if strings.HasSuffix(fn, ".gz") { | ||
gz := gzip.NewWriter(writer) | ||
defer gz.Close() | ||
writer = gz | ||
} | ||
|
||
buf := bufio.NewWriter(writer) | ||
defer buf.Flush() | ||
writer = buf | ||
|
||
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 := snaptree.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 := snaptree.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(chaindb, item.Hash) | ||
if len(preimage) == 0 { | ||
return fmt.Errorf("missing preimage for %v", item.Hash) | ||
} | ||
if len(preimage) != item.Size { | ||
return fmt.Errorf("invalid preimage size, have %d", len(preimage)) | ||
} | ||
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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I am fine with removing
exportPreimagesCommand
, but let's keep theimportPreimageComand
for a while, otherwise we can't import them back.