@@ -48,6 +48,7 @@ import (
48
48
"github.com/ethereum/go-ethereum/node"
49
49
"github.com/ethereum/go-ethereum/params"
50
50
"github.com/ethereum/go-ethereum/rlp"
51
+ "github.com/ethereum/go-ethereum/triedb"
51
52
"github.com/urfave/cli/v2"
52
53
)
53
54
@@ -567,9 +568,64 @@ func ExportPreimages(db ethdb.Database, fn string) error {
567
568
return nil
568
569
}
569
570
571
+ // StateIterator is a temporary structure for traversing state in order. It serves
572
+ // as an aggregator for both path scheme and hash scheme implementations and should
573
+ // be removed once the hash scheme is fully deprecated.
574
+ type StateIterator struct {
575
+ scheme string
576
+ root common.Hash
577
+ triedb * triedb.Database
578
+ snapshots * snapshot.Tree
579
+ }
580
+
581
+ // NewStateIterator constructs the state iterator with the specific root.
582
+ func NewStateIterator (triedb * triedb.Database , db ethdb.Database , root common.Hash ) (* StateIterator , error ) {
583
+ if triedb .Scheme () == rawdb .PathScheme {
584
+ return & StateIterator {
585
+ scheme : rawdb .PathScheme ,
586
+ root : root ,
587
+ triedb : triedb ,
588
+ }, nil
589
+ }
590
+ config := snapshot.Config {
591
+ CacheSize : 256 ,
592
+ Recovery : false ,
593
+ NoBuild : true ,
594
+ AsyncBuild : false ,
595
+ }
596
+ snapshots , err := snapshot .New (config , db , triedb , root )
597
+ if err != nil {
598
+ return nil , err
599
+ }
600
+ return & StateIterator {
601
+ scheme : rawdb .HashScheme ,
602
+ root : root ,
603
+ triedb : triedb ,
604
+ snapshots : snapshots ,
605
+ }, nil
606
+ }
607
+
608
+ // AccountIterator creates a new account iterator for the specified root hash and
609
+ // seeks to a starting account hash.
610
+ func (it * StateIterator ) AccountIterator (root common.Hash , start common.Hash ) (snapshot.AccountIterator , error ) {
611
+ if it .scheme == rawdb .PathScheme {
612
+ return it .triedb .AccountIterator (root , start )
613
+ }
614
+ return it .snapshots .AccountIterator (root , start )
615
+ }
616
+
617
+ // StorageIterator creates a new storage iterator for the specified root hash and
618
+ // account. The iterator will be moved to the specific start position.
619
+ func (it * StateIterator ) StorageIterator (root common.Hash , accountHash common.Hash , start common.Hash ) (snapshot.StorageIterator , error ) {
620
+ if it .scheme == rawdb .PathScheme {
621
+ return it .triedb .StorageIterator (root , accountHash , start )
622
+ }
623
+ return it .snapshots .StorageIterator (root , accountHash , start )
624
+ }
625
+
570
626
// ExportSnapshotPreimages exports the preimages corresponding to the enumeration of
571
627
// the snapshot for a given root.
572
- func ExportSnapshotPreimages (chaindb ethdb.Database , snaptree * snapshot. Tree , fn string , root common.Hash ) error {
628
+ func ExportSnapshotPreimages (chaindb ethdb.Database , stateIt * StateIterator , fn string , root common.Hash ) error {
573
629
log .Info ("Exporting preimages" , "file" , fn )
574
630
575
631
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
602
658
)
603
659
go func () {
604
660
defer close (hashCh )
605
- accIt , err := snaptree .AccountIterator (root , common.Hash {})
661
+ accIt , err := stateIt .AccountIterator (root , common.Hash {})
606
662
if err != nil {
607
663
log .Error ("Failed to create account iterator" , "error" , err )
608
664
return
@@ -619,7 +675,7 @@ func ExportSnapshotPreimages(chaindb ethdb.Database, snaptree *snapshot.Tree, fn
619
675
hashCh <- hashAndPreimageSize {Hash : accIt .Hash (), Size : common .AddressLength }
620
676
621
677
if acc .Root != (common.Hash {}) && acc .Root != types .EmptyRootHash {
622
- stIt , err := snaptree .StorageIterator (root , accIt .Hash (), common.Hash {})
678
+ stIt , err := stateIt .StorageIterator (root , accIt .Hash (), common.Hash {})
623
679
if err != nil {
624
680
log .Error ("Failed to create storage iterator" , "error" , err )
625
681
return
0 commit comments