mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-02-26 07:37:20 +00:00
cmd, tests: fix snapshot dump and export-preimages (#32650)
Address #32646
This commit is contained in:
parent
ada2db4304
commit
aa37bd063d
4 changed files with 67 additions and 23 deletions
|
|
@ -561,17 +561,11 @@ func dumpState(ctx *cli.Context) error {
|
||||||
triedb := utils.MakeTrieDatabase(ctx, stack, db, false, true, false)
|
triedb := utils.MakeTrieDatabase(ctx, stack, db, false, true, false)
|
||||||
defer triedb.Close()
|
defer triedb.Close()
|
||||||
|
|
||||||
snapConfig := snapshot.Config{
|
stateIt, err := utils.NewStateIterator(triedb, db, root)
|
||||||
CacheSize: 256,
|
|
||||||
Recovery: false,
|
|
||||||
NoBuild: true,
|
|
||||||
AsyncBuild: false,
|
|
||||||
}
|
|
||||||
snaptree, err := snapshot.New(snapConfig, db, triedb, root)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
accIt, err := snaptree.AccountIterator(root, common.BytesToHash(conf.Start))
|
accIt, err := stateIt.AccountIterator(root, common.BytesToHash(conf.Start))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -605,7 +599,7 @@ func dumpState(ctx *cli.Context) error {
|
||||||
if !conf.SkipStorage {
|
if !conf.SkipStorage {
|
||||||
da.Storage = make(map[common.Hash]string)
|
da.Storage = make(map[common.Hash]string)
|
||||||
|
|
||||||
stIt, err := snaptree.StorageIterator(root, accIt.Hash(), common.Hash{})
|
stIt, err := stateIt.StorageIterator(root, accIt.Hash(), common.Hash{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -658,17 +652,11 @@ func snapshotExportPreimages(ctx *cli.Context) error {
|
||||||
}
|
}
|
||||||
root = headBlock.Root()
|
root = headBlock.Root()
|
||||||
}
|
}
|
||||||
snapConfig := snapshot.Config{
|
stateIt, err := utils.NewStateIterator(triedb, chaindb, root)
|
||||||
CacheSize: 256,
|
|
||||||
Recovery: false,
|
|
||||||
NoBuild: true,
|
|
||||||
AsyncBuild: false,
|
|
||||||
}
|
|
||||||
snaptree, err := snapshot.New(snapConfig, chaindb, triedb, root)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return utils.ExportSnapshotPreimages(chaindb, snaptree, ctx.Args().First(), root)
|
return utils.ExportSnapshotPreimages(chaindb, stateIt, ctx.Args().First(), root)
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkAccount iterates the snap data layers, and looks up the given account
|
// checkAccount iterates the snap data layers, and looks up the given account
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
|
"github.com/ethereum/go-ethereum/triedb"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -567,9 +568,64 @@ func ExportPreimages(db ethdb.Database, fn string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StateIterator is a temporary structure for traversing state in order. It serves
|
||||||
|
// as an aggregator for both path scheme and hash scheme implementations and should
|
||||||
|
// be removed once the hash scheme is fully deprecated.
|
||||||
|
type StateIterator struct {
|
||||||
|
scheme string
|
||||||
|
root common.Hash
|
||||||
|
triedb *triedb.Database
|
||||||
|
snapshots *snapshot.Tree
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewStateIterator constructs the state iterator with the specific root.
|
||||||
|
func NewStateIterator(triedb *triedb.Database, db ethdb.Database, root common.Hash) (*StateIterator, error) {
|
||||||
|
if triedb.Scheme() == rawdb.PathScheme {
|
||||||
|
return &StateIterator{
|
||||||
|
scheme: rawdb.PathScheme,
|
||||||
|
root: root,
|
||||||
|
triedb: triedb,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
config := snapshot.Config{
|
||||||
|
CacheSize: 256,
|
||||||
|
Recovery: false,
|
||||||
|
NoBuild: true,
|
||||||
|
AsyncBuild: false,
|
||||||
|
}
|
||||||
|
snapshots, err := snapshot.New(config, db, triedb, root)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &StateIterator{
|
||||||
|
scheme: rawdb.HashScheme,
|
||||||
|
root: root,
|
||||||
|
triedb: triedb,
|
||||||
|
snapshots: snapshots,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccountIterator creates a new account iterator for the specified root hash and
|
||||||
|
// seeks to a starting account hash.
|
||||||
|
func (it *StateIterator) AccountIterator(root common.Hash, start common.Hash) (snapshot.AccountIterator, error) {
|
||||||
|
if it.scheme == rawdb.PathScheme {
|
||||||
|
return it.triedb.AccountIterator(root, start)
|
||||||
|
}
|
||||||
|
return it.snapshots.AccountIterator(root, start)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StorageIterator creates a new storage iterator for the specified root hash and
|
||||||
|
// account. The iterator will be moved to the specific start position.
|
||||||
|
func (it *StateIterator) StorageIterator(root common.Hash, accountHash common.Hash, start common.Hash) (snapshot.StorageIterator, error) {
|
||||||
|
if it.scheme == rawdb.PathScheme {
|
||||||
|
return it.triedb.StorageIterator(root, accountHash, start)
|
||||||
|
}
|
||||||
|
return it.snapshots.StorageIterator(root, accountHash, start)
|
||||||
|
}
|
||||||
|
|
||||||
// ExportSnapshotPreimages exports the preimages corresponding to the enumeration of
|
// ExportSnapshotPreimages exports the preimages corresponding to the enumeration of
|
||||||
// the snapshot for a given root.
|
// the snapshot for a given root.
|
||||||
func ExportSnapshotPreimages(chaindb ethdb.Database, snaptree *snapshot.Tree, fn string, root common.Hash) error {
|
func ExportSnapshotPreimages(chaindb ethdb.Database, stateIt *StateIterator, fn string, root common.Hash) error {
|
||||||
log.Info("Exporting preimages", "file", fn)
|
log.Info("Exporting preimages", "file", fn)
|
||||||
|
|
||||||
fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
|
fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
|
||||||
|
|
@ -602,7 +658,7 @@ func ExportSnapshotPreimages(chaindb ethdb.Database, snaptree *snapshot.Tree, fn
|
||||||
)
|
)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(hashCh)
|
defer close(hashCh)
|
||||||
accIt, err := snaptree.AccountIterator(root, common.Hash{})
|
accIt, err := stateIt.AccountIterator(root, common.Hash{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to create account iterator", "error", err)
|
log.Error("Failed to create account iterator", "error", err)
|
||||||
return
|
return
|
||||||
|
|
@ -619,7 +675,7 @@ func ExportSnapshotPreimages(chaindb ethdb.Database, snaptree *snapshot.Tree, fn
|
||||||
hashCh <- hashAndPreimageSize{Hash: accIt.Hash(), Size: common.AddressLength}
|
hashCh <- hashAndPreimageSize{Hash: accIt.Hash(), Size: common.AddressLength}
|
||||||
|
|
||||||
if acc.Root != (common.Hash{}) && acc.Root != types.EmptyRootHash {
|
if acc.Root != (common.Hash{}) && acc.Root != types.EmptyRootHash {
|
||||||
stIt, err := snaptree.StorageIterator(root, accIt.Hash(), common.Hash{})
|
stIt, err := stateIt.StorageIterator(root, accIt.Hash(), common.Hash{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to create storage iterator", "error", err)
|
log.Error("Failed to create storage iterator", "error", err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -156,8 +156,8 @@ func execStateTest(t *testing.T, st *testMatcher, test *StateTest) {
|
||||||
withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
|
withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
|
||||||
var result error
|
var result error
|
||||||
test.Run(subtest, vmconfig, true, rawdb.PathScheme, func(err error, state *StateTestState) {
|
test.Run(subtest, vmconfig, true, rawdb.PathScheme, func(err error, state *StateTestState) {
|
||||||
if state.Snapshots != nil && state.StateDB != nil {
|
if state.TrieDB != nil && state.StateDB != nil {
|
||||||
if _, err := state.Snapshots.Journal(state.StateDB.IntermediateRoot(false)); err != nil {
|
if err := state.TrieDB.Journal(state.StateDB.IntermediateRoot(false)); err != nil {
|
||||||
result = err
|
result = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -523,7 +523,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc, snapshotter bo
|
||||||
|
|
||||||
// If snapshot is requested, initialize the snapshotter and use it in state.
|
// If snapshot is requested, initialize the snapshotter and use it in state.
|
||||||
var snaps *snapshot.Tree
|
var snaps *snapshot.Tree
|
||||||
if snapshotter {
|
if snapshotter && scheme == rawdb.HashScheme {
|
||||||
snapconfig := snapshot.Config{
|
snapconfig := snapshot.Config{
|
||||||
CacheSize: 1,
|
CacheSize: 1,
|
||||||
Recovery: false,
|
Recovery: false,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue