forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 13
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
Overlay Tree preimages exporting and usage #246
Merged
gballet
merged 5 commits into
transition-in-cachingdb-rebased
from
jsign-preimages-flatfile
Aug 1, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aade791
export overlay preimages tool
jsign 148086b
use preimages flat file in overlay tree migration logic
jsign 08b731b
cmd/geth: add --roothash to overlay tree preimage exporting command
jsign 5dc1a63
cleanup
jsign c99f535
review feedback
jsign 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,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" | ||
|
@@ -379,6 +380,75 @@ func ExportPreimages(db ethdb.Database, fn string) error { | |
return nil | ||
} | ||
|
||
// ExportOverlayPreimages exports all known hash preimages into the specified file, | ||
// in the same order as expected by the overlay tree migration. | ||
func ExportOverlayPreimages(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() | ||
} | ||
Comment on lines
+402
to
+404
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. How the flag works. |
||
|
||
accIt, err := statedb.Snaps().AccountIterator(root, common.Hash{}) | ||
if err != nil { | ||
return err | ||
} | ||
defer accIt.Release() | ||
|
||
count := 0 | ||
for accIt.Next() { | ||
acc, err := snapshot.FullAccount(accIt.Account()) | ||
if err != nil { | ||
return fmt.Errorf("invalid account encountered during traversal: %s", err) | ||
} | ||
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) | ||
} | ||
|
||
if acc.HasStorage() { | ||
stIt, err := statedb.Snaps().StorageIterator(root, accIt.Hash(), common.Hash{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create storage iterator: %w", err) | ||
} | ||
for stIt.Next() { | ||
slotnr := rawdb.ReadPreimage(statedb.Database().DiskDB(), stIt.Hash()) | ||
if len(slotnr) != 32 { | ||
return fmt.Errorf("slotnr not 32 len") | ||
} | ||
if _, err := writer.Write(slotnr); err != nil { | ||
return fmt.Errorf("failed to write slotnr preimage: %w", err) | ||
} | ||
} | ||
stIt.Release() | ||
} | ||
count++ | ||
if count%100000 == 0 { | ||
log.Info("Last exported account", "account", accIt.Hash()) | ||
} | ||
} | ||
Comment on lines
+413
to
+446
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. Nothing entirely interesting. We're doing the same ordered walk as in the Overlay Migration logic. |
||
|
||
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 | ||
|
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
Oops, something went wrong.
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.
@gballet, I've added an optional flag
--roothash
that can be provided to choose a specific hash to walk the MPT.The flag will use the latest known state if it isn't provided.
This can be helpful in the future.