github.com/klaytn/klaytn@v1.12.1/snapshot/iterator_fast.go (about)

     1  // Modifications Copyright 2021 The klaytn Authors
     2  // Copyright 2019 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/state/snapshot/iterator_fast.go (2021/10/21).
    19  // Modified and improved for the klaytn development.
    20  
    21  package snapshot
    22  
    23  import (
    24  	"bytes"
    25  	"fmt"
    26  	"sort"
    27  
    28  	"github.com/klaytn/klaytn/common"
    29  )
    30  
    31  // weightedIterator is a iterator with an assigned weight. It is used to prioritise
    32  // which account or storage slot is the correct one if multiple iterators find the
    33  // same one (modified in multiple consecutive blocks).
    34  type weightedIterator struct {
    35  	it       Iterator
    36  	priority int
    37  }
    38  
    39  // weightedIterators is a set of iterators implementing the sort.Interface.
    40  type weightedIterators []*weightedIterator
    41  
    42  // Len implements sort.Interface, returning the number of active iterators.
    43  func (its weightedIterators) Len() int { return len(its) }
    44  
    45  // Less implements sort.Interface, returning which of two iterators in the stack
    46  // is before the other.
    47  func (its weightedIterators) Less(i, j int) bool {
    48  	// Order the iterators primarily by the account hashes
    49  	hashI := its[i].it.Hash()
    50  	hashJ := its[j].it.Hash()
    51  
    52  	switch bytes.Compare(hashI[:], hashJ[:]) {
    53  	case -1:
    54  		return true
    55  	case 1:
    56  		return false
    57  	}
    58  	// Same account/storage-slot in multiple layers, split by priority
    59  	return its[i].priority < its[j].priority
    60  }
    61  
    62  // Swap implements sort.Interface, swapping two entries in the iterator stack.
    63  func (its weightedIterators) Swap(i, j int) {
    64  	its[i], its[j] = its[j], its[i]
    65  }
    66  
    67  // fastIterator is a more optimized multi-layer iterator which maintains a
    68  // direct mapping of all iterators leading down to the bottom layer.
    69  type fastIterator struct {
    70  	tree *Tree       // Snapshot tree to reinitialize stale sub-iterators with
    71  	root common.Hash // Root hash to reinitialize stale sub-iterators through
    72  
    73  	curAccount []byte
    74  	curSlot    []byte
    75  
    76  	iterators weightedIterators
    77  	initiated bool
    78  	account   bool
    79  	fail      error
    80  }
    81  
    82  // newFastIterator creates a new hierarchical account or storage iterator with one
    83  // element per diff layer. The returned combo iterator can be used to walk over
    84  // the entire snapshot diff stack simultaneously.
    85  func newFastIterator(tree *Tree, root common.Hash, account common.Hash, seek common.Hash, accountIterator bool) (*fastIterator, error) {
    86  	snap := tree.Snapshot(root)
    87  	if snap == nil {
    88  		return nil, fmt.Errorf("unknown snapshot: %x", root)
    89  	}
    90  	fi := &fastIterator{
    91  		tree:    tree,
    92  		root:    root,
    93  		account: accountIterator,
    94  	}
    95  	current := snap.(snapshot)
    96  	for depth := 0; current != nil; depth++ {
    97  		if accountIterator {
    98  			fi.iterators = append(fi.iterators, &weightedIterator{
    99  				it:       current.AccountIterator(seek),
   100  				priority: depth,
   101  			})
   102  		} else {
   103  			// If the whole storage is destructed in this layer, don't
   104  			// bother deeper layer anymore. But we should still keep
   105  			// the iterator for this layer, since the iterator can contain
   106  			// some valid slots which belongs to the re-created account.
   107  			it, destructed := current.StorageIterator(account, seek)
   108  			fi.iterators = append(fi.iterators, &weightedIterator{
   109  				it:       it,
   110  				priority: depth,
   111  			})
   112  			if destructed {
   113  				break
   114  			}
   115  		}
   116  		current = current.Parent()
   117  	}
   118  	fi.init()
   119  	return fi, nil
   120  }
   121  
   122  // init walks over all the iterators and resolves any clashes between them, after
   123  // which it prepares the stack for step-by-step iteration.
   124  func (fi *fastIterator) init() {
   125  	// Track which account hashes are iterators positioned on
   126  	positioned := make(map[common.Hash]int)
   127  
   128  	// Position all iterators and track how many remain live
   129  	for i := 0; i < len(fi.iterators); i++ {
   130  		// Retrieve the first element and if it clashes with a previous iterator,
   131  		// advance either the current one or the old one. Repeat until nothing is
   132  		// clashing any more.
   133  		it := fi.iterators[i]
   134  		for {
   135  			// If the iterator is exhausted, drop it off the end
   136  			if !it.it.Next() {
   137  				it.it.Release()
   138  				last := len(fi.iterators) - 1
   139  
   140  				fi.iterators[i] = fi.iterators[last]
   141  				fi.iterators[last] = nil
   142  				fi.iterators = fi.iterators[:last]
   143  
   144  				i--
   145  				break
   146  			}
   147  			// The iterator is still alive, check for collisions with previous ones
   148  			hash := it.it.Hash()
   149  			if other, exist := positioned[hash]; !exist {
   150  				positioned[hash] = i
   151  				break
   152  			} else {
   153  				// Iterators collide, one needs to be progressed, use priority to
   154  				// determine which.
   155  				//
   156  				// This whole else-block can be avoided, if we instead
   157  				// do an initial priority-sort of the iterators. If we do that,
   158  				// then we'll only wind up here if a lower-priority (preferred) iterator
   159  				// has the same value, and then we will always just continue.
   160  				// However, it costs an extra sort, so it's probably not better
   161  				if fi.iterators[other].priority < it.priority {
   162  					// The 'it' should be progressed
   163  					continue
   164  				} else {
   165  					// The 'other' should be progressed, swap them
   166  					it = fi.iterators[other]
   167  					fi.iterators[other], fi.iterators[i] = fi.iterators[i], fi.iterators[other]
   168  					continue
   169  				}
   170  			}
   171  		}
   172  	}
   173  	// Re-sort the entire list
   174  	sort.Sort(fi.iterators)
   175  	fi.initiated = false
   176  }
   177  
   178  // Next steps the iterator forward one element, returning false if exhausted.
   179  func (fi *fastIterator) Next() bool {
   180  	if len(fi.iterators) == 0 {
   181  		return false
   182  	}
   183  	if !fi.initiated {
   184  		// Don't forward first time -- we had to 'Next' once in order to
   185  		// do the sorting already
   186  		fi.initiated = true
   187  		if fi.account {
   188  			fi.curAccount = fi.iterators[0].it.(AccountIterator).Account()
   189  		} else {
   190  			fi.curSlot = fi.iterators[0].it.(StorageIterator).Slot()
   191  		}
   192  		if innerErr := fi.iterators[0].it.Error(); innerErr != nil {
   193  			fi.fail = innerErr
   194  			return false
   195  		}
   196  		if fi.curAccount != nil || fi.curSlot != nil {
   197  			return true
   198  		}
   199  		// Implicit else: we've hit a nil-account or nil-slot, and need to
   200  		// fall through to the loop below to land on something non-nil
   201  	}
   202  	// If an account or a slot is deleted in one of the layers, the key will
   203  	// still be there, but the actual value will be nil. However, the iterator
   204  	// should not export nil-values (but instead simply omit the key), so we
   205  	// need to loop here until we either
   206  	//  - get a non-nil value,
   207  	//  - hit an error,
   208  	//  - or exhaust the iterator
   209  	for {
   210  		if !fi.next(0) {
   211  			return false // exhausted
   212  		}
   213  		if fi.account {
   214  			fi.curAccount = fi.iterators[0].it.(AccountIterator).Account()
   215  		} else {
   216  			fi.curSlot = fi.iterators[0].it.(StorageIterator).Slot()
   217  		}
   218  		if innerErr := fi.iterators[0].it.Error(); innerErr != nil {
   219  			fi.fail = innerErr
   220  			return false // error
   221  		}
   222  		if fi.curAccount != nil || fi.curSlot != nil {
   223  			break // non-nil value found
   224  		}
   225  	}
   226  	return true
   227  }
   228  
   229  // next handles the next operation internally and should be invoked when we know
   230  // that two elements in the list may have the same value.
   231  //
   232  // For example, if the iterated hashes become [2,3,5,5,8,9,10], then we should
   233  // invoke next(3), which will call Next on elem 3 (the second '5') and will
   234  // cascade along the list, applying the same operation if needed.
   235  func (fi *fastIterator) next(idx int) bool {
   236  	// If this particular iterator got exhausted, remove it and return true (the
   237  	// next one is surely not exhausted yet, otherwise it would have been removed
   238  	// already).
   239  	if it := fi.iterators[idx].it; !it.Next() {
   240  		it.Release()
   241  
   242  		fi.iterators = append(fi.iterators[:idx], fi.iterators[idx+1:]...)
   243  		return len(fi.iterators) > 0
   244  	}
   245  	// If there's no one left to cascade into, return
   246  	if idx == len(fi.iterators)-1 {
   247  		return true
   248  	}
   249  	// We next-ed the iterator at 'idx', now we may have to re-sort that element
   250  	var (
   251  		cur, next         = fi.iterators[idx], fi.iterators[idx+1]
   252  		curHash, nextHash = cur.it.Hash(), next.it.Hash()
   253  	)
   254  	if diff := bytes.Compare(curHash[:], nextHash[:]); diff < 0 {
   255  		// It is still in correct place
   256  		return true
   257  	} else if diff == 0 && cur.priority < next.priority {
   258  		// So still in correct place, but we need to iterate on the next
   259  		fi.next(idx + 1)
   260  		return true
   261  	}
   262  	// At this point, the iterator is in the wrong location, but the remaining
   263  	// list is sorted. Find out where to move the item.
   264  	clash := -1
   265  	index := sort.Search(len(fi.iterators), func(n int) bool {
   266  		// The iterator always advances forward, so anything before the old slot
   267  		// is known to be behind us, so just skip them altogether. This actually
   268  		// is an important clause since the sort order got invalidated.
   269  		if n < idx {
   270  			return false
   271  		}
   272  		if n == len(fi.iterators)-1 {
   273  			// Can always place an elem last
   274  			return true
   275  		}
   276  		nextHash := fi.iterators[n+1].it.Hash()
   277  		if diff := bytes.Compare(curHash[:], nextHash[:]); diff < 0 {
   278  			return true
   279  		} else if diff > 0 {
   280  			return false
   281  		}
   282  		// The elem we're placing it next to has the same value,
   283  		// so whichever winds up on n+1 will need further iteraton
   284  		clash = n + 1
   285  
   286  		return cur.priority < fi.iterators[n+1].priority
   287  	})
   288  	fi.move(idx, index)
   289  	if clash != -1 {
   290  		fi.next(clash)
   291  	}
   292  	return true
   293  }
   294  
   295  // move advances an iterator to another position in the list.
   296  func (fi *fastIterator) move(index, newpos int) {
   297  	elem := fi.iterators[index]
   298  	copy(fi.iterators[index:], fi.iterators[index+1:newpos+1])
   299  	fi.iterators[newpos] = elem
   300  }
   301  
   302  // Error returns any failure that occurred during iteration, which might have
   303  // caused a premature iteration exit (e.g. snapshot stack becoming stale).
   304  func (fi *fastIterator) Error() error {
   305  	return fi.fail
   306  }
   307  
   308  // Hash returns the current key
   309  func (fi *fastIterator) Hash() common.Hash {
   310  	return fi.iterators[0].it.Hash()
   311  }
   312  
   313  // Account returns the current account blob.
   314  // Note the returned account is not a copy, please don't modify it.
   315  func (fi *fastIterator) Account() []byte {
   316  	return fi.curAccount
   317  }
   318  
   319  // Slot returns the current storage slot.
   320  // Note the returned slot is not a copy, please don't modify it.
   321  func (fi *fastIterator) Slot() []byte {
   322  	return fi.curSlot
   323  }
   324  
   325  // Release iterates over all the remaining live layer iterators and releases each
   326  // of thme individually.
   327  func (fi *fastIterator) Release() {
   328  	for _, it := range fi.iterators {
   329  		it.it.Release()
   330  	}
   331  	fi.iterators = nil
   332  }
   333  
   334  // Debug is a convencience helper during testing
   335  func (fi *fastIterator) Debug() {
   336  	for _, it := range fi.iterators {
   337  		fmt.Printf("[p=%v v=%v] ", it.priority, it.it.Hash()[0])
   338  	}
   339  	fmt.Println()
   340  }
   341  
   342  // newFastAccountIterator creates a new hierarchical account iterator with one
   343  // element per diff layer. The returned combo iterator can be used to walk over
   344  // the entire snapshot diff stack simultaneously.
   345  func newFastAccountIterator(tree *Tree, root common.Hash, seek common.Hash) (AccountIterator, error) {
   346  	return newFastIterator(tree, root, common.Hash{}, seek, true)
   347  }
   348  
   349  // newFastStorageIterator creates a new hierarchical storage iterator with one
   350  // element per diff layer. The returned combo iterator can be used to walk over
   351  // the entire snapshot diff stack simultaneously.
   352  func newFastStorageIterator(tree *Tree, root common.Hash, account common.Hash, seek common.Hash) (StorageIterator, error) {
   353  	return newFastIterator(tree, root, account, seek, false)
   354  }