github.com/xfond/eth-implementation@v1.8.9-0.20180514135602-f6bc65fc6811/trie/iterator.go (about)

     1  // Copyright 2014 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 trie
    18  
    19  import (
    20  	"bytes"
    21  	"container/heap"
    22  	"errors"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  )
    26  
    27  // Iterator is a key-value trie iterator that traverses a Trie.
    28  type Iterator struct {
    29  	nodeIt NodeIterator
    30  
    31  	Key   []byte // Current data key on which the iterator is positioned on
    32  	Value []byte // Current data value on which the iterator is positioned on
    33  	Err   error
    34  }
    35  
    36  // NewIterator creates a new key-value iterator from a node iterator
    37  func NewIterator(it NodeIterator) *Iterator {
    38  	return &Iterator{
    39  		nodeIt: it,
    40  	}
    41  }
    42  
    43  // Next moves the iterator forward one key-value entry.
    44  func (it *Iterator) Next() bool {
    45  	for it.nodeIt.Next(true) {
    46  		if it.nodeIt.Leaf() {
    47  			it.Key = it.nodeIt.LeafKey()
    48  			it.Value = it.nodeIt.LeafBlob()
    49  			return true
    50  		}
    51  	}
    52  	it.Key = nil
    53  	it.Value = nil
    54  	it.Err = it.nodeIt.Error()
    55  	return false
    56  }
    57  
    58  // NodeIterator is an iterator to traverse the trie pre-order.
    59  type NodeIterator interface {
    60  	// Next moves the iterator to the next node. If the parameter is false, any child
    61  	// nodes will be skipped.
    62  	Next(bool) bool
    63  	// Error returns the error status of the iterator.
    64  	Error() error
    65  
    66  	// Hash returns the hash of the current node.
    67  	Hash() common.Hash
    68  	// Parent returns the hash of the parent of the current node. The hash may be the one
    69  	// grandparent if the immediate parent is an internal node with no hash.
    70  	Parent() common.Hash
    71  	// Path returns the hex-encoded path to the current node.
    72  	// Callers must not retain references to the return value after calling Next.
    73  	// For leaf nodes, the last element of the path is the 'terminator symbol' 0x10.
    74  	Path() []byte
    75  
    76  	// Leaf returns true iff the current node is a leaf node.
    77  	// LeafBlob, LeafKey return the contents and key of the leaf node. These
    78  	// method panic if the iterator is not positioned at a leaf.
    79  	// Callers must not retain references to their return value after calling Next
    80  	Leaf() bool
    81  	LeafBlob() []byte
    82  	LeafKey() []byte
    83  }
    84  
    85  // nodeIteratorState represents the iteration state at one particular node of the
    86  // trie, which can be resumed at a later invocation.
    87  type nodeIteratorState struct {
    88  	hash    common.Hash // Hash of the node being iterated (nil if not standalone)
    89  	node    node        // Trie node being iterated
    90  	parent  common.Hash // Hash of the first full ancestor node (nil if current is the root)
    91  	index   int         // Child to be processed next
    92  	pathlen int         // Length of the path to this node
    93  }
    94  
    95  type nodeIterator struct {
    96  	trie  *Trie                // Trie being iterated
    97  	stack []*nodeIteratorState // Hierarchy of trie nodes persisting the iteration state
    98  	path  []byte               // Path to the current node
    99  	err   error                // Failure set in case of an internal error in the iterator
   100  }
   101  
   102  // iteratorEnd is stored in nodeIterator.err when iteration is done.
   103  var iteratorEnd = errors.New("end of iteration")
   104  
   105  // seekError is stored in nodeIterator.err if the initial seek has failed.
   106  type seekError struct {
   107  	key []byte
   108  	err error
   109  }
   110  
   111  func (e seekError) Error() string {
   112  	return "seek error: " + e.err.Error()
   113  }
   114  
   115  func newNodeIterator(trie *Trie, start []byte) NodeIterator {
   116  	if trie.Hash() == emptyState {
   117  		return new(nodeIterator)
   118  	}
   119  	it := &nodeIterator{trie: trie}
   120  	it.err = it.seek(start)
   121  	return it
   122  }
   123  
   124  func (it *nodeIterator) Hash() common.Hash {
   125  	if len(it.stack) == 0 {
   126  		return common.Hash{}
   127  	}
   128  	return it.stack[len(it.stack)-1].hash
   129  }
   130  
   131  func (it *nodeIterator) Parent() common.Hash {
   132  	if len(it.stack) == 0 {
   133  		return common.Hash{}
   134  	}
   135  	return it.stack[len(it.stack)-1].parent
   136  }
   137  
   138  func (it *nodeIterator) Leaf() bool {
   139  	return hasTerm(it.path)
   140  }
   141  
   142  func (it *nodeIterator) LeafBlob() []byte {
   143  	if len(it.stack) > 0 {
   144  		if node, ok := it.stack[len(it.stack)-1].node.(valueNode); ok {
   145  			return []byte(node)
   146  		}
   147  	}
   148  	panic("not at leaf")
   149  }
   150  
   151  func (it *nodeIterator) LeafKey() []byte {
   152  	if len(it.stack) > 0 {
   153  		if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok {
   154  			return hexToKeybytes(it.path)
   155  		}
   156  	}
   157  	panic("not at leaf")
   158  }
   159  
   160  func (it *nodeIterator) Path() []byte {
   161  	return it.path
   162  }
   163  
   164  func (it *nodeIterator) Error() error {
   165  	if it.err == iteratorEnd {
   166  		return nil
   167  	}
   168  	if seek, ok := it.err.(seekError); ok {
   169  		return seek.err
   170  	}
   171  	return it.err
   172  }
   173  
   174  // Next moves the iterator to the next node, returning whether there are any
   175  // further nodes. In case of an internal error this method returns false and
   176  // sets the Error field to the encountered failure. If `descend` is false,
   177  // skips iterating over any subnodes of the current node.
   178  func (it *nodeIterator) Next(descend bool) bool {
   179  	if it.err == iteratorEnd {
   180  		return false
   181  	}
   182  	if seek, ok := it.err.(seekError); ok {
   183  		if it.err = it.seek(seek.key); it.err != nil {
   184  			return false
   185  		}
   186  	}
   187  	// Otherwise step forward with the iterator and report any errors.
   188  	state, parentIndex, path, err := it.peek(descend)
   189  	it.err = err
   190  	if it.err != nil {
   191  		return false
   192  	}
   193  	it.push(state, parentIndex, path)
   194  	return true
   195  }
   196  
   197  func (it *nodeIterator) seek(prefix []byte) error {
   198  	// The path we're looking for is the hex encoded key without terminator.
   199  	key := keybytesToHex(prefix)
   200  	key = key[:len(key)-1]
   201  	// Move forward until we're just before the closest match to key.
   202  	for {
   203  		state, parentIndex, path, err := it.peek(bytes.HasPrefix(key, it.path))
   204  		if err == iteratorEnd {
   205  			return iteratorEnd
   206  		} else if err != nil {
   207  			return seekError{prefix, err}
   208  		} else if bytes.Compare(path, key) >= 0 {
   209  			return nil
   210  		}
   211  		it.push(state, parentIndex, path)
   212  	}
   213  }
   214  
   215  // peek creates the next state of the iterator.
   216  func (it *nodeIterator) peek(descend bool) (*nodeIteratorState, *int, []byte, error) {
   217  	if len(it.stack) == 0 {
   218  		// Initialize the iterator if we've just started.
   219  		root := it.trie.Hash()
   220  		state := &nodeIteratorState{node: it.trie.root, index: -1}
   221  		if root != emptyRoot {
   222  			state.hash = root
   223  		}
   224  		err := state.resolve(it.trie, nil)
   225  		return state, nil, nil, err
   226  	}
   227  	if !descend {
   228  		// If we're skipping children, pop the current node first
   229  		it.pop()
   230  	}
   231  
   232  	// Continue iteration to the next child
   233  	for len(it.stack) > 0 {
   234  		parent := it.stack[len(it.stack)-1]
   235  		ancestor := parent.hash
   236  		if (ancestor == common.Hash{}) {
   237  			ancestor = parent.parent
   238  		}
   239  		state, path, ok := it.nextChild(parent, ancestor)
   240  		if ok {
   241  			if err := state.resolve(it.trie, path); err != nil {
   242  				return parent, &parent.index, path, err
   243  			}
   244  			return state, &parent.index, path, nil
   245  		}
   246  		// No more child nodes, move back up.
   247  		it.pop()
   248  	}
   249  	return nil, nil, nil, iteratorEnd
   250  }
   251  
   252  func (st *nodeIteratorState) resolve(tr *Trie, path []byte) error {
   253  	if hash, ok := st.node.(hashNode); ok {
   254  		resolved, err := tr.resolveHash(hash, path)
   255  		if err != nil {
   256  			return err
   257  		}
   258  		st.node = resolved
   259  		st.hash = common.BytesToHash(hash)
   260  	}
   261  	return nil
   262  }
   263  
   264  func (it *nodeIterator) nextChild(parent *nodeIteratorState, ancestor common.Hash) (*nodeIteratorState, []byte, bool) {
   265  	switch node := parent.node.(type) {
   266  	case *fullNode:
   267  		// Full node, move to the first non-nil child.
   268  		for i := parent.index + 1; i < len(node.Children); i++ {
   269  			child := node.Children[i]
   270  			if child != nil {
   271  				hash, _ := child.cache()
   272  				state := &nodeIteratorState{
   273  					hash:    common.BytesToHash(hash),
   274  					node:    child,
   275  					parent:  ancestor,
   276  					index:   -1,
   277  					pathlen: len(it.path),
   278  				}
   279  				path := append(it.path, byte(i))
   280  				parent.index = i - 1
   281  				return state, path, true
   282  			}
   283  		}
   284  	case *shortNode:
   285  		// Short node, return the pointer singleton child
   286  		if parent.index < 0 {
   287  			hash, _ := node.Val.cache()
   288  			state := &nodeIteratorState{
   289  				hash:    common.BytesToHash(hash),
   290  				node:    node.Val,
   291  				parent:  ancestor,
   292  				index:   -1,
   293  				pathlen: len(it.path),
   294  			}
   295  			path := append(it.path, node.Key...)
   296  			return state, path, true
   297  		}
   298  	}
   299  	return parent, it.path, false
   300  }
   301  
   302  func (it *nodeIterator) push(state *nodeIteratorState, parentIndex *int, path []byte) {
   303  	it.path = path
   304  	it.stack = append(it.stack, state)
   305  	if parentIndex != nil {
   306  		*parentIndex++
   307  	}
   308  }
   309  
   310  func (it *nodeIterator) pop() {
   311  	parent := it.stack[len(it.stack)-1]
   312  	it.path = it.path[:parent.pathlen]
   313  	it.stack = it.stack[:len(it.stack)-1]
   314  }
   315  
   316  func compareNodes(a, b NodeIterator) int {
   317  	if cmp := bytes.Compare(a.Path(), b.Path()); cmp != 0 {
   318  		return cmp
   319  	}
   320  	if a.Leaf() && !b.Leaf() {
   321  		return -1
   322  	} else if b.Leaf() && !a.Leaf() {
   323  		return 1
   324  	}
   325  	if cmp := bytes.Compare(a.Hash().Bytes(), b.Hash().Bytes()); cmp != 0 {
   326  		return cmp
   327  	}
   328  	if a.Leaf() && b.Leaf() {
   329  		return bytes.Compare(a.LeafBlob(), b.LeafBlob())
   330  	}
   331  	return 0
   332  }
   333  
   334  type differenceIterator struct {
   335  	a, b  NodeIterator // Nodes returned are those in b - a.
   336  	eof   bool         // Indicates a has run out of elements
   337  	count int          // Number of nodes scanned on either trie
   338  }
   339  
   340  // NewDifferenceIterator constructs a NodeIterator that iterates over elements in b that
   341  // are not in a. Returns the iterator, and a pointer to an integer recording the number
   342  // of nodes seen.
   343  func NewDifferenceIterator(a, b NodeIterator) (NodeIterator, *int) {
   344  	a.Next(true)
   345  	it := &differenceIterator{
   346  		a: a,
   347  		b: b,
   348  	}
   349  	return it, &it.count
   350  }
   351  
   352  func (it *differenceIterator) Hash() common.Hash {
   353  	return it.b.Hash()
   354  }
   355  
   356  func (it *differenceIterator) Parent() common.Hash {
   357  	return it.b.Parent()
   358  }
   359  
   360  func (it *differenceIterator) Leaf() bool {
   361  	return it.b.Leaf()
   362  }
   363  
   364  func (it *differenceIterator) LeafBlob() []byte {
   365  	return it.b.LeafBlob()
   366  }
   367  
   368  func (it *differenceIterator) LeafKey() []byte {
   369  	return it.b.LeafKey()
   370  }
   371  
   372  func (it *differenceIterator) Path() []byte {
   373  	return it.b.Path()
   374  }
   375  
   376  func (it *differenceIterator) Next(bool) bool {
   377  	// Invariants:
   378  	// - We always advance at least one element in b.
   379  	// - At the start of this function, a's path is lexically greater than b's.
   380  	if !it.b.Next(true) {
   381  		return false
   382  	}
   383  	it.count++
   384  
   385  	if it.eof {
   386  		// a has reached eof, so we just return all elements from b
   387  		return true
   388  	}
   389  
   390  	for {
   391  		switch compareNodes(it.a, it.b) {
   392  		case -1:
   393  			// b jumped past a; advance a
   394  			if !it.a.Next(true) {
   395  				it.eof = true
   396  				return true
   397  			}
   398  			it.count++
   399  		case 1:
   400  			// b is before a
   401  			return true
   402  		case 0:
   403  			// a and b are identical; skip this whole subtree if the nodes have hashes
   404  			hasHash := it.a.Hash() == common.Hash{}
   405  			if !it.b.Next(hasHash) {
   406  				return false
   407  			}
   408  			it.count++
   409  			if !it.a.Next(hasHash) {
   410  				it.eof = true
   411  				return true
   412  			}
   413  			it.count++
   414  		}
   415  	}
   416  }
   417  
   418  func (it *differenceIterator) Error() error {
   419  	if err := it.a.Error(); err != nil {
   420  		return err
   421  	}
   422  	return it.b.Error()
   423  }
   424  
   425  type nodeIteratorHeap []NodeIterator
   426  
   427  func (h nodeIteratorHeap) Len() int            { return len(h) }
   428  func (h nodeIteratorHeap) Less(i, j int) bool  { return compareNodes(h[i], h[j]) < 0 }
   429  func (h nodeIteratorHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
   430  func (h *nodeIteratorHeap) Push(x interface{}) { *h = append(*h, x.(NodeIterator)) }
   431  func (h *nodeIteratorHeap) Pop() interface{} {
   432  	n := len(*h)
   433  	x := (*h)[n-1]
   434  	*h = (*h)[0 : n-1]
   435  	return x
   436  }
   437  
   438  type unionIterator struct {
   439  	items *nodeIteratorHeap // Nodes returned are the union of the ones in these iterators
   440  	count int               // Number of nodes scanned across all tries
   441  }
   442  
   443  // NewUnionIterator constructs a NodeIterator that iterates over elements in the union
   444  // of the provided NodeIterators. Returns the iterator, and a pointer to an integer
   445  // recording the number of nodes visited.
   446  func NewUnionIterator(iters []NodeIterator) (NodeIterator, *int) {
   447  	h := make(nodeIteratorHeap, len(iters))
   448  	copy(h, iters)
   449  	heap.Init(&h)
   450  
   451  	ui := &unionIterator{items: &h}
   452  	return ui, &ui.count
   453  }
   454  
   455  func (it *unionIterator) Hash() common.Hash {
   456  	return (*it.items)[0].Hash()
   457  }
   458  
   459  func (it *unionIterator) Parent() common.Hash {
   460  	return (*it.items)[0].Parent()
   461  }
   462  
   463  func (it *unionIterator) Leaf() bool {
   464  	return (*it.items)[0].Leaf()
   465  }
   466  
   467  func (it *unionIterator) LeafBlob() []byte {
   468  	return (*it.items)[0].LeafBlob()
   469  }
   470  
   471  func (it *unionIterator) LeafKey() []byte {
   472  	return (*it.items)[0].LeafKey()
   473  }
   474  
   475  func (it *unionIterator) Path() []byte {
   476  	return (*it.items)[0].Path()
   477  }
   478  
   479  // Next returns the next node in the union of tries being iterated over.
   480  //
   481  // It does this by maintaining a heap of iterators, sorted by the iteration
   482  // order of their next elements, with one entry for each source trie. Each
   483  // time Next() is called, it takes the least element from the heap to return,
   484  // advancing any other iterators that also point to that same element. These
   485  // iterators are called with descend=false, since we know that any nodes under
   486  // these nodes will also be duplicates, found in the currently selected iterator.
   487  // Whenever an iterator is advanced, it is pushed back into the heap if it still
   488  // has elements remaining.
   489  //
   490  // In the case that descend=false - eg, we're asked to ignore all subnodes of the
   491  // current node - we also advance any iterators in the heap that have the current
   492  // path as a prefix.
   493  func (it *unionIterator) Next(descend bool) bool {
   494  	if len(*it.items) == 0 {
   495  		return false
   496  	}
   497  
   498  	// Get the next key from the union
   499  	least := heap.Pop(it.items).(NodeIterator)
   500  
   501  	// Skip over other nodes as long as they're identical, or, if we're not descending, as
   502  	// long as they have the same prefix as the current node.
   503  	for len(*it.items) > 0 && ((!descend && bytes.HasPrefix((*it.items)[0].Path(), least.Path())) || compareNodes(least, (*it.items)[0]) == 0) {
   504  		skipped := heap.Pop(it.items).(NodeIterator)
   505  		// Skip the whole subtree if the nodes have hashes; otherwise just skip this node
   506  		if skipped.Next(skipped.Hash() == common.Hash{}) {
   507  			it.count++
   508  			// If there are more elements, push the iterator back on the heap
   509  			heap.Push(it.items, skipped)
   510  		}
   511  	}
   512  
   513  	if least.Next(descend) {
   514  		it.count++
   515  		heap.Push(it.items, least)
   516  	}
   517  
   518  	return len(*it.items) > 0
   519  }
   520  
   521  func (it *unionIterator) Error() error {
   522  	for i := 0; i < len(*it.items); i++ {
   523  		if err := (*it.items)[i].Error(); err != nil {
   524  			return err
   525  		}
   526  	}
   527  	return nil
   528  }