@@ -18,6 +18,7 @@ package snapshot
1818
1919import (
2020 "encoding/binary"
21+ "bytes"
2122 "fmt"
2223 "math"
2324 "math/rand"
@@ -475,3 +476,291 @@ func (dl *diffLayer) StorageList(accountHash common.Hash) []common.Hash {
475476 dl .storageList [accountHash ] = accountStorageList
476477 return accountStorageList
477478}
479+
480+ type Iterator interface {
481+ // Next steps the iterator forward one element, and returns false if
482+ // the iterator is exhausted
483+ Next () bool
484+ // Key returns the current key
485+ Key () common.Hash
486+ // Seek steps the iterator forward as many elements as needed, so that after
487+ // calling Next(), the iterator will be at a key higher than the given hash
488+ Seek (common.Hash )
489+ }
490+
491+ func (dl * diffLayer ) newIterator () Iterator {
492+ dl .AccountList ()
493+ return & dlIterator {dl , - 1 }
494+ }
495+
496+ type dlIterator struct {
497+ layer * diffLayer
498+ index int
499+ }
500+
501+ func (it * dlIterator ) Next () bool {
502+ if it .index < len (it .layer .accountList ) {
503+ it .index ++
504+ }
505+ return it .index < len (it .layer .accountList )
506+ }
507+
508+ func (it * dlIterator ) Key () common.Hash {
509+ if it .index < len (it .layer .accountList ) {
510+ return it .layer .accountList [it .index ]
511+ }
512+ return common.Hash {}
513+ }
514+
515+ func (it * dlIterator ) Seek (key common.Hash ) {
516+ // Search uses binary search to find and return the smallest index i
517+ // in [0, n) at which f(i) is true
518+ size := len (it .layer .accountList )
519+ index := sort .Search (size ,
520+ func (i int ) bool {
521+ v := it .layer .accountList [i ]
522+ return bytes .Compare (key [:], v [:]) < 0
523+ })
524+ it .index = index - 1
525+ }
526+
527+ type binaryIterator struct {
528+ a Iterator
529+ b Iterator
530+ aDone bool
531+ bDone bool
532+ k common.Hash
533+ }
534+
535+ func (dl * diffLayer ) newBinaryIterator () Iterator {
536+ parent , ok := dl .parent .(* diffLayer )
537+ if ! ok {
538+ // parent is the disk layer
539+ return dl .newIterator ()
540+ }
541+ l := & binaryIterator {
542+ a : dl .newIterator (),
543+ b : parent .newBinaryIterator ()}
544+
545+ l .aDone = ! l .a .Next ()
546+ l .bDone = ! l .b .Next ()
547+ return l
548+ }
549+
550+ func (it * binaryIterator ) Next () bool {
551+
552+ if it .aDone && it .bDone {
553+ return false
554+ }
555+ nextB := it .b .Key ()
556+ first:
557+ nextA := it .a .Key ()
558+ if it .aDone {
559+ it .bDone = ! it .b .Next ()
560+ it .k = nextB
561+ return true
562+ }
563+ if it .bDone {
564+ it .aDone = ! it .a .Next ()
565+ it .k = nextA
566+ return true
567+ }
568+ if diff := bytes .Compare (nextA [:], nextB [:]); diff < 0 {
569+ it .aDone = ! it .a .Next ()
570+ it .k = nextA
571+ return true
572+ } else if diff == 0 {
573+ // Now we need to advance one of them
574+ it .aDone = ! it .a .Next ()
575+ goto first
576+ }
577+ it .bDone = ! it .b .Next ()
578+ it .k = nextB
579+ return true
580+ }
581+
582+ func (it * binaryIterator ) Key () common.Hash {
583+ return it .k
584+ }
585+ func (it * binaryIterator ) Seek (key common.Hash ) {
586+ panic ("todo: implement" )
587+ }
588+
589+ func (dl * diffLayer ) iterators () []Iterator {
590+ if parent , ok := dl .parent .(* diffLayer ); ok {
591+ iterators := parent .iterators ()
592+ return append (iterators , dl .newIterator ())
593+ }
594+ return []Iterator {dl .newIterator ()}
595+ }
596+
597+ // fastIterator is a more optimized multi-layer iterator which maintains a
598+ // direct mapping of all iterators leading down to the bottom layer
599+ type fastIterator struct {
600+ iterators []Iterator
601+ initiated bool
602+ }
603+
604+ // Len returns the number of active iterators
605+ func (fi * fastIterator ) Len () int {
606+ return len (fi .iterators )
607+ }
608+
609+ // Less implements sort.Interface
610+ func (fi * fastIterator ) Less (i , j int ) bool {
611+ a := fi .iterators [i ].Key ()
612+ b := fi .iterators [j ].Key ()
613+ return bytes .Compare (a [:], b [:]) < 0
614+ }
615+
616+ // Swap implements sort.Interface
617+ func (fi * fastIterator ) Swap (i , j int ) {
618+ fi .iterators [i ], fi .iterators [j ] = fi .iterators [j ], fi .iterators [i ]
619+ }
620+
621+ // Next implements the Iterator interface. It returns false if no more elemnts
622+ // can be retrieved (false == exhausted)
623+ func (fi * fastIterator ) Next () bool {
624+ if len (fi .iterators ) == 0 {
625+ return false
626+ }
627+ if ! fi .initiated {
628+ // Don't forward first time -- we had to 'Next' once in order to
629+ // do the sorting already
630+ fi .initiated = true
631+ return true
632+ }
633+ return fi .innerNext (0 )
634+ }
635+
636+ // innerNext handles the next operation internally,
637+ // and should be invoked when we know that two elements in the list may have
638+ // the same value.
639+ // For example, if the list becomes [2,3,5,5,8,9,10], then we should invoke
640+ // innerNext(3), which will call Next on elem 3 (the second '5'). It will continue
641+ // along the list and apply the same operation if needed
642+ func (fi * fastIterator ) innerNext (pos int ) bool {
643+ if ! fi .iterators [pos ].Next () {
644+ //Exhausted, remove this iterator
645+ fi .remove (pos )
646+ if len (fi .iterators ) == 0 {
647+ return false
648+ }
649+ return true
650+ }
651+ if pos == len (fi .iterators )- 1 {
652+ // Only one iterator left
653+ return true
654+ }
655+ // We next:ed the elem at 'pos'. Now we may have to re-sort that elem
656+ val , neighbour := fi .iterators [pos ].Key (), fi .iterators [pos + 1 ].Key ()
657+ diff := bytes .Compare (val [:], neighbour [:])
658+ if diff < 0 {
659+ // It is still in correct place
660+ return true
661+ }
662+ if diff == 0 {
663+ // It has same value as the neighbour. So still in correct place, but
664+ // we need to iterate on the neighbour
665+ fi .innerNext (pos + 1 )
666+ return true
667+ }
668+ // At this point, the elem is in the wrong location, but the
669+ // remaining list is sorted. Find out where to move the elem
670+ iterationNeeded := false
671+ index := sort .Search (len (fi .iterators ), func (n int ) bool {
672+ if n <= pos {
673+ // No need to search 'behind' us
674+ return false
675+ }
676+ if n == len (fi .iterators )- 1 {
677+ // Can always place an elem last
678+ return true
679+ }
680+ neighbour := fi .iterators [n + 1 ].Key ()
681+ diff := bytes .Compare (val [:], neighbour [:])
682+ if diff == 0 {
683+ // The elem we're placing it next to has the same value,
684+ // so it's going to need further iteration
685+ iterationNeeded = true
686+ }
687+ return diff < 0
688+ })
689+ fi .move (pos , index )
690+ if iterationNeeded {
691+ fi .innerNext (index )
692+ }
693+ return true
694+ }
695+
696+ // move moves an iterator to another position in the list
697+ func (fi * fastIterator ) move (index , newpos int ) {
698+ if newpos > len (fi .iterators )- 1 {
699+ newpos = len (fi .iterators ) - 1
700+ }
701+ var (
702+ elem = fi .iterators [index ]
703+ middle = fi .iterators [index + 1 : newpos + 1 ]
704+ suffix []Iterator
705+ )
706+ if newpos < len (fi .iterators )- 1 {
707+ suffix = fi .iterators [newpos + 1 :]
708+ }
709+ fi .iterators = append (fi .iterators [:index ], middle ... )
710+ fi .iterators = append (fi .iterators , elem )
711+ fi .iterators = append (fi .iterators , suffix ... )
712+ }
713+
714+ // remove drops an iterator from the list
715+ func (fi * fastIterator ) remove (index int ) {
716+ fi .iterators = append (fi .iterators [:index ], fi .iterators [index + 1 :]... )
717+ }
718+
719+ // Key returns the current key
720+ func (fi * fastIterator ) Key () common.Hash {
721+ return fi .iterators [0 ].Key ()
722+ }
723+
724+ func (fi * fastIterator ) Seek (key common.Hash ) {
725+ // We need to apply this across all iterators
726+ var seen = make (map [common.Hash ]struct {})
727+
728+ length := len (fi .iterators )
729+ for i , it := range fi .iterators {
730+ it .Seek (key )
731+ for {
732+ if ! it .Next () {
733+ // To be removed
734+ // swap it to the last position for now
735+ fi .iterators [i ], fi .iterators [length - 1 ] = fi .iterators [length - 1 ], fi .iterators [i ]
736+ length --
737+ break
738+ }
739+ v := it .Key ()
740+ if _ , exist := seen [v ]; ! exist {
741+ seen [v ] = struct {}{}
742+ break
743+ }
744+ }
745+ }
746+ // Now remove those that were placed in the end
747+ fi .iterators = fi .iterators [:length ]
748+ // The list is now totally unsorted, need to re-sort the entire list
749+ sort .Sort (fi )
750+ fi .initiated = false
751+ }
752+
753+ // The fast iterator does not query parents as much.
754+ func (dl * diffLayer ) newFastIterator () Iterator {
755+ f := & fastIterator {dl .iterators (), false }
756+ f .Seek (common.Hash {})
757+ return f
758+ }
759+
760+ // Debug is a convencience helper during testing
761+ func (fi * fastIterator ) Debug () {
762+ for _ , it := range fi .iterators {
763+ fmt .Printf (" %v " , it .Key ()[31 ])
764+ }
765+ fmt .Println ()
766+ }
0 commit comments