github.com/ethereum/go-ethereum@v1.16.1/core/state/snapshot/iterator_fast.go (about)

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