github.com/theQRL/go-zond@v0.1.1/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/theQRL/go-zond/common"
    25  	"github.com/theQRL/go-zond/core/types"
    26  )
    27  
    28  // NodeResolver is used for looking up trie nodes before reaching into the real
    29  // persistent layer. This is not mandatory, rather is an optimization for cases
    30  // where trie nodes can be recovered from some external mechanism without reading
    31  // from disk. In those cases, this resolver allows short circuiting accesses and
    32  // returning them from memory.
    33  type NodeResolver func(owner common.Hash, path []byte, hash common.Hash) []byte
    34  
    35  // Iterator is a key-value trie iterator that traverses a Trie.
    36  type Iterator struct {
    37  	nodeIt NodeIterator
    38  
    39  	Key   []byte // Current data key on which the iterator is positioned on
    40  	Value []byte // Current data value on which the iterator is positioned on
    41  	Err   error
    42  }
    43  
    44  // NewIterator creates a new key-value iterator from a node iterator.
    45  // Note that the value returned by the iterator is raw. If the content is encoded
    46  // (e.g. storage value is RLP-encoded), it's caller's duty to decode it.
    47  func NewIterator(it NodeIterator) *Iterator {
    48  	return &Iterator{
    49  		nodeIt: it,
    50  	}
    51  }
    52  
    53  // Next moves the iterator forward one key-value entry.
    54  func (it *Iterator) Next() bool {
    55  	for it.nodeIt.Next(true) {
    56  		if it.nodeIt.Leaf() {
    57  			it.Key = it.nodeIt.LeafKey()
    58  			it.Value = it.nodeIt.LeafBlob()
    59  			return true
    60  		}
    61  	}
    62  	it.Key = nil
    63  	it.Value = nil
    64  	it.Err = it.nodeIt.Error()
    65  	return false
    66  }
    67  
    68  // Prove generates the Merkle proof for the leaf node the iterator is currently
    69  // positioned on.
    70  func (it *Iterator) Prove() [][]byte {
    71  	return it.nodeIt.LeafProof()
    72  }
    73  
    74  // NodeIterator is an iterator to traverse the trie pre-order.
    75  type NodeIterator interface {
    76  	// Next moves the iterator to the next node. If the parameter is false, any child
    77  	// nodes will be skipped.
    78  	Next(bool) bool
    79  
    80  	// Error returns the error status of the iterator.
    81  	Error() error
    82  
    83  	// Hash returns the hash of the current node.
    84  	Hash() common.Hash
    85  
    86  	// Parent returns the hash of the parent of the current node. The hash may be the one
    87  	// grandparent if the immediate parent is an internal node with no hash.
    88  	Parent() common.Hash
    89  
    90  	// Path returns the hex-encoded path to the current node.
    91  	// Callers must not retain references to the return value after calling Next.
    92  	// For leaf nodes, the last element of the path is the 'terminator symbol' 0x10.
    93  	Path() []byte
    94  
    95  	// NodeBlob returns the rlp-encoded value of the current iterated node.
    96  	// If the node is an embedded node in its parent, nil is returned then.
    97  	NodeBlob() []byte
    98  
    99  	// Leaf returns true iff the current node is a leaf node.
   100  	Leaf() bool
   101  
   102  	// LeafKey returns the key of the leaf. The method panics if the iterator is not
   103  	// positioned at a leaf. Callers must not retain references to the value after
   104  	// calling Next.
   105  	LeafKey() []byte
   106  
   107  	// LeafBlob returns the content of the leaf. The method panics if the iterator
   108  	// is not positioned at a leaf. Callers must not retain references to the value
   109  	// after calling Next.
   110  	LeafBlob() []byte
   111  
   112  	// LeafProof returns the Merkle proof of the leaf. The method panics if the
   113  	// iterator is not positioned at a leaf. Callers must not retain references
   114  	// to the value after calling Next.
   115  	LeafProof() [][]byte
   116  
   117  	// AddResolver sets a node resolver to use for looking up trie nodes before
   118  	// reaching into the real persistent layer.
   119  	//
   120  	// This is not required for normal operation, rather is an optimization for
   121  	// cases where trie nodes can be recovered from some external mechanism without
   122  	// reading from disk. In those cases, this resolver allows short circuiting
   123  	// accesses and returning them from memory.
   124  	//
   125  	// Before adding a similar mechanism to any other place in Geth, consider
   126  	// making trie.Database an interface and wrapping at that level. It's a huge
   127  	// refactor, but it could be worth it if another occurrence arises.
   128  	AddResolver(NodeResolver)
   129  }
   130  
   131  // nodeIteratorState represents the iteration state at one particular node of the
   132  // trie, which can be resumed at a later invocation.
   133  type nodeIteratorState struct {
   134  	hash    common.Hash // Hash of the node being iterated (nil if not standalone)
   135  	node    node        // Trie node being iterated
   136  	parent  common.Hash // Hash of the first full ancestor node (nil if current is the root)
   137  	index   int         // Child to be processed next
   138  	pathlen int         // Length of the path to this node
   139  }
   140  
   141  type nodeIterator struct {
   142  	trie  *Trie                // Trie being iterated
   143  	stack []*nodeIteratorState // Hierarchy of trie nodes persisting the iteration state
   144  	path  []byte               // Path to the current node
   145  	err   error                // Failure set in case of an internal error in the iterator
   146  
   147  	resolver NodeResolver // optional node resolver for avoiding disk hits
   148  }
   149  
   150  // errIteratorEnd is stored in nodeIterator.err when iteration is done.
   151  var errIteratorEnd = errors.New("end of iteration")
   152  
   153  // seekError is stored in nodeIterator.err if the initial seek has failed.
   154  type seekError struct {
   155  	key []byte
   156  	err error
   157  }
   158  
   159  func (e seekError) Error() string {
   160  	return "seek error: " + e.err.Error()
   161  }
   162  
   163  func newNodeIterator(trie *Trie, start []byte) NodeIterator {
   164  	if trie.Hash() == types.EmptyRootHash {
   165  		return &nodeIterator{
   166  			trie: trie,
   167  			err:  errIteratorEnd,
   168  		}
   169  	}
   170  	it := &nodeIterator{trie: trie}
   171  	it.err = it.seek(start)
   172  	return it
   173  }
   174  
   175  func (it *nodeIterator) AddResolver(resolver NodeResolver) {
   176  	it.resolver = resolver
   177  }
   178  
   179  func (it *nodeIterator) Hash() common.Hash {
   180  	if len(it.stack) == 0 {
   181  		return common.Hash{}
   182  	}
   183  	return it.stack[len(it.stack)-1].hash
   184  }
   185  
   186  func (it *nodeIterator) Parent() common.Hash {
   187  	if len(it.stack) == 0 {
   188  		return common.Hash{}
   189  	}
   190  	return it.stack[len(it.stack)-1].parent
   191  }
   192  
   193  func (it *nodeIterator) Leaf() bool {
   194  	return hasTerm(it.path)
   195  }
   196  
   197  func (it *nodeIterator) LeafKey() []byte {
   198  	if len(it.stack) > 0 {
   199  		if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok {
   200  			return hexToKeybytes(it.path)
   201  		}
   202  	}
   203  	panic("not at leaf")
   204  }
   205  
   206  func (it *nodeIterator) LeafBlob() []byte {
   207  	if len(it.stack) > 0 {
   208  		if node, ok := it.stack[len(it.stack)-1].node.(valueNode); ok {
   209  			return node
   210  		}
   211  	}
   212  	panic("not at leaf")
   213  }
   214  
   215  func (it *nodeIterator) LeafProof() [][]byte {
   216  	if len(it.stack) > 0 {
   217  		if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok {
   218  			hasher := newHasher(false)
   219  			defer returnHasherToPool(hasher)
   220  			proofs := make([][]byte, 0, len(it.stack))
   221  
   222  			for i, item := range it.stack[:len(it.stack)-1] {
   223  				// Gather nodes that end up as hash nodes (or the root)
   224  				node, hashed := hasher.proofHash(item.node)
   225  				if _, ok := hashed.(hashNode); ok || i == 0 {
   226  					proofs = append(proofs, nodeToBytes(node))
   227  				}
   228  			}
   229  			return proofs
   230  		}
   231  	}
   232  	panic("not at leaf")
   233  }
   234  
   235  func (it *nodeIterator) Path() []byte {
   236  	return it.path
   237  }
   238  
   239  func (it *nodeIterator) NodeBlob() []byte {
   240  	if it.Hash() == (common.Hash{}) {
   241  		return nil // skip the non-standalone node
   242  	}
   243  	blob, err := it.resolveBlob(it.Hash().Bytes(), it.Path())
   244  	if err != nil {
   245  		it.err = err
   246  		return nil
   247  	}
   248  	return blob
   249  }
   250  
   251  func (it *nodeIterator) Error() error {
   252  	if it.err == errIteratorEnd {
   253  		return nil
   254  	}
   255  	if seek, ok := it.err.(seekError); ok {
   256  		return seek.err
   257  	}
   258  	return it.err
   259  }
   260  
   261  // Next moves the iterator to the next node, returning whether there are any
   262  // further nodes. In case of an internal error this method returns false and
   263  // sets the Error field to the encountered failure. If `descend` is false,
   264  // skips iterating over any subnodes of the current node.
   265  func (it *nodeIterator) Next(descend bool) bool {
   266  	if it.err == errIteratorEnd {
   267  		return false
   268  	}
   269  	if seek, ok := it.err.(seekError); ok {
   270  		if it.err = it.seek(seek.key); it.err != nil {
   271  			return false
   272  		}
   273  	}
   274  	// Otherwise step forward with the iterator and report any errors.
   275  	state, parentIndex, path, err := it.peek(descend)
   276  	it.err = err
   277  	if it.err != nil {
   278  		return false
   279  	}
   280  	it.push(state, parentIndex, path)
   281  	return true
   282  }
   283  
   284  func (it *nodeIterator) seek(prefix []byte) error {
   285  	// The path we're looking for is the hex encoded key without terminator.
   286  	key := keybytesToHex(prefix)
   287  	key = key[:len(key)-1]
   288  	// Move forward until we're just before the closest match to key.
   289  	for {
   290  		state, parentIndex, path, err := it.peekSeek(key)
   291  		if err == errIteratorEnd {
   292  			return errIteratorEnd
   293  		} else if err != nil {
   294  			return seekError{prefix, err}
   295  		} else if bytes.Compare(path, key) >= 0 {
   296  			return nil
   297  		}
   298  		it.push(state, parentIndex, path)
   299  	}
   300  }
   301  
   302  // init initializes the iterator.
   303  func (it *nodeIterator) init() (*nodeIteratorState, error) {
   304  	root := it.trie.Hash()
   305  	state := &nodeIteratorState{node: it.trie.root, index: -1}
   306  	if root != types.EmptyRootHash {
   307  		state.hash = root
   308  	}
   309  	return state, state.resolve(it, nil)
   310  }
   311  
   312  // peek creates the next state of the iterator.
   313  func (it *nodeIterator) peek(descend bool) (*nodeIteratorState, *int, []byte, error) {
   314  	// Initialize the iterator if we've just started.
   315  	if len(it.stack) == 0 {
   316  		state, err := it.init()
   317  		return state, nil, nil, err
   318  	}
   319  	if !descend {
   320  		// If we're skipping children, pop the current node first
   321  		it.pop()
   322  	}
   323  
   324  	// Continue iteration to the next child
   325  	for len(it.stack) > 0 {
   326  		parent := it.stack[len(it.stack)-1]
   327  		ancestor := parent.hash
   328  		if (ancestor == common.Hash{}) {
   329  			ancestor = parent.parent
   330  		}
   331  		state, path, ok := it.nextChild(parent, ancestor)
   332  		if ok {
   333  			if err := state.resolve(it, path); err != nil {
   334  				return parent, &parent.index, path, err
   335  			}
   336  			return state, &parent.index, path, nil
   337  		}
   338  		// No more child nodes, move back up.
   339  		it.pop()
   340  	}
   341  	return nil, nil, nil, errIteratorEnd
   342  }
   343  
   344  // peekSeek is like peek, but it also tries to skip resolving hashes by skipping
   345  // over the siblings that do not lead towards the desired seek position.
   346  func (it *nodeIterator) peekSeek(seekKey []byte) (*nodeIteratorState, *int, []byte, error) {
   347  	// Initialize the iterator if we've just started.
   348  	if len(it.stack) == 0 {
   349  		state, err := it.init()
   350  		return state, nil, nil, err
   351  	}
   352  	if !bytes.HasPrefix(seekKey, it.path) {
   353  		// If we're skipping children, pop the current node first
   354  		it.pop()
   355  	}
   356  
   357  	// Continue iteration to the next child
   358  	for len(it.stack) > 0 {
   359  		parent := it.stack[len(it.stack)-1]
   360  		ancestor := parent.hash
   361  		if (ancestor == common.Hash{}) {
   362  			ancestor = parent.parent
   363  		}
   364  		state, path, ok := it.nextChildAt(parent, ancestor, seekKey)
   365  		if ok {
   366  			if err := state.resolve(it, path); err != nil {
   367  				return parent, &parent.index, path, err
   368  			}
   369  			return state, &parent.index, path, nil
   370  		}
   371  		// No more child nodes, move back up.
   372  		it.pop()
   373  	}
   374  	return nil, nil, nil, errIteratorEnd
   375  }
   376  
   377  func (it *nodeIterator) resolveHash(hash hashNode, path []byte) (node, error) {
   378  	if it.resolver != nil {
   379  		if blob := it.resolver(it.trie.owner, path, common.BytesToHash(hash)); len(blob) > 0 {
   380  			if resolved, err := decodeNode(hash, blob); err == nil {
   381  				return resolved, nil
   382  			}
   383  		}
   384  	}
   385  	// Retrieve the specified node from the underlying node reader.
   386  	// it.trie.resolveAndTrack is not used since in that function the
   387  	// loaded blob will be tracked, while it's not required here since
   388  	// all loaded nodes won't be linked to trie at all and track nodes
   389  	// may lead to out-of-memory issue.
   390  	blob, err := it.trie.reader.node(path, common.BytesToHash(hash))
   391  	if err != nil {
   392  		return nil, err
   393  	}
   394  	// The raw-blob format nodes are loaded either from the
   395  	// clean cache or the database, they are all in their own
   396  	// copy and safe to use unsafe decoder.
   397  	return mustDecodeNodeUnsafe(hash, blob), nil
   398  }
   399  
   400  func (it *nodeIterator) resolveBlob(hash hashNode, path []byte) ([]byte, error) {
   401  	if it.resolver != nil {
   402  		if blob := it.resolver(it.trie.owner, path, common.BytesToHash(hash)); len(blob) > 0 {
   403  			return blob, nil
   404  		}
   405  	}
   406  	// Retrieve the specified node from the underlying node reader.
   407  	// it.trie.resolveAndTrack is not used since in that function the
   408  	// loaded blob will be tracked, while it's not required here since
   409  	// all loaded nodes won't be linked to trie at all and track nodes
   410  	// may lead to out-of-memory issue.
   411  	return it.trie.reader.node(path, common.BytesToHash(hash))
   412  }
   413  
   414  func (st *nodeIteratorState) resolve(it *nodeIterator, path []byte) error {
   415  	if hash, ok := st.node.(hashNode); ok {
   416  		resolved, err := it.resolveHash(hash, path)
   417  		if err != nil {
   418  			return err
   419  		}
   420  		st.node = resolved
   421  		st.hash = common.BytesToHash(hash)
   422  	}
   423  	return nil
   424  }
   425  
   426  func findChild(n *fullNode, index int, path []byte, ancestor common.Hash) (node, *nodeIteratorState, []byte, int) {
   427  	var (
   428  		child     node
   429  		state     *nodeIteratorState
   430  		childPath []byte
   431  	)
   432  	for ; index < len(n.Children); index++ {
   433  		if n.Children[index] != nil {
   434  			child = n.Children[index]
   435  			hash, _ := child.cache()
   436  			state = &nodeIteratorState{
   437  				hash:    common.BytesToHash(hash),
   438  				node:    child,
   439  				parent:  ancestor,
   440  				index:   -1,
   441  				pathlen: len(path),
   442  			}
   443  			childPath = append(childPath, path...)
   444  			childPath = append(childPath, byte(index))
   445  			return child, state, childPath, index
   446  		}
   447  	}
   448  	return nil, nil, nil, 0
   449  }
   450  
   451  func (it *nodeIterator) nextChild(parent *nodeIteratorState, ancestor common.Hash) (*nodeIteratorState, []byte, bool) {
   452  	switch node := parent.node.(type) {
   453  	case *fullNode:
   454  		// Full node, move to the first non-nil child.
   455  		if child, state, path, index := findChild(node, parent.index+1, it.path, ancestor); child != nil {
   456  			parent.index = index - 1
   457  			return state, path, true
   458  		}
   459  	case *shortNode:
   460  		// Short node, return the pointer singleton child
   461  		if parent.index < 0 {
   462  			hash, _ := node.Val.cache()
   463  			state := &nodeIteratorState{
   464  				hash:    common.BytesToHash(hash),
   465  				node:    node.Val,
   466  				parent:  ancestor,
   467  				index:   -1,
   468  				pathlen: len(it.path),
   469  			}
   470  			path := append(it.path, node.Key...)
   471  			return state, path, true
   472  		}
   473  	}
   474  	return parent, it.path, false
   475  }
   476  
   477  // nextChildAt is similar to nextChild, except that it targets a child as close to the
   478  // target key as possible, thus skipping siblings.
   479  func (it *nodeIterator) nextChildAt(parent *nodeIteratorState, ancestor common.Hash, key []byte) (*nodeIteratorState, []byte, bool) {
   480  	switch n := parent.node.(type) {
   481  	case *fullNode:
   482  		// Full node, move to the first non-nil child before the desired key position
   483  		child, state, path, index := findChild(n, parent.index+1, it.path, ancestor)
   484  		if child == nil {
   485  			// No more children in this fullnode
   486  			return parent, it.path, false
   487  		}
   488  		// If the child we found is already past the seek position, just return it.
   489  		if bytes.Compare(path, key) >= 0 {
   490  			parent.index = index - 1
   491  			return state, path, true
   492  		}
   493  		// The child is before the seek position. Try advancing
   494  		for {
   495  			nextChild, nextState, nextPath, nextIndex := findChild(n, index+1, it.path, ancestor)
   496  			// If we run out of children, or skipped past the target, return the
   497  			// previous one
   498  			if nextChild == nil || bytes.Compare(nextPath, key) >= 0 {
   499  				parent.index = index - 1
   500  				return state, path, true
   501  			}
   502  			// We found a better child closer to the target
   503  			state, path, index = nextState, nextPath, nextIndex
   504  		}
   505  	case *shortNode:
   506  		// Short node, return the pointer singleton child
   507  		if parent.index < 0 {
   508  			hash, _ := n.Val.cache()
   509  			state := &nodeIteratorState{
   510  				hash:    common.BytesToHash(hash),
   511  				node:    n.Val,
   512  				parent:  ancestor,
   513  				index:   -1,
   514  				pathlen: len(it.path),
   515  			}
   516  			path := append(it.path, n.Key...)
   517  			return state, path, true
   518  		}
   519  	}
   520  	return parent, it.path, false
   521  }
   522  
   523  func (it *nodeIterator) push(state *nodeIteratorState, parentIndex *int, path []byte) {
   524  	it.path = path
   525  	it.stack = append(it.stack, state)
   526  	if parentIndex != nil {
   527  		*parentIndex++
   528  	}
   529  }
   530  
   531  func (it *nodeIterator) pop() {
   532  	last := it.stack[len(it.stack)-1]
   533  	it.path = it.path[:last.pathlen]
   534  	it.stack[len(it.stack)-1] = nil
   535  	it.stack = it.stack[:len(it.stack)-1]
   536  }
   537  
   538  func compareNodes(a, b NodeIterator) int {
   539  	if cmp := bytes.Compare(a.Path(), b.Path()); cmp != 0 {
   540  		return cmp
   541  	}
   542  	if a.Leaf() && !b.Leaf() {
   543  		return -1
   544  	} else if b.Leaf() && !a.Leaf() {
   545  		return 1
   546  	}
   547  	if cmp := bytes.Compare(a.Hash().Bytes(), b.Hash().Bytes()); cmp != 0 {
   548  		return cmp
   549  	}
   550  	if a.Leaf() && b.Leaf() {
   551  		return bytes.Compare(a.LeafBlob(), b.LeafBlob())
   552  	}
   553  	return 0
   554  }
   555  
   556  type differenceIterator struct {
   557  	a, b  NodeIterator // Nodes returned are those in b - a.
   558  	eof   bool         // Indicates a has run out of elements
   559  	count int          // Number of nodes scanned on either trie
   560  }
   561  
   562  // NewDifferenceIterator constructs a NodeIterator that iterates over elements in b that
   563  // are not in a. Returns the iterator, and a pointer to an integer recording the number
   564  // of nodes seen.
   565  func NewDifferenceIterator(a, b NodeIterator) (NodeIterator, *int) {
   566  	a.Next(true)
   567  	it := &differenceIterator{
   568  		a: a,
   569  		b: b,
   570  	}
   571  	return it, &it.count
   572  }
   573  
   574  func (it *differenceIterator) Hash() common.Hash {
   575  	return it.b.Hash()
   576  }
   577  
   578  func (it *differenceIterator) Parent() common.Hash {
   579  	return it.b.Parent()
   580  }
   581  
   582  func (it *differenceIterator) Leaf() bool {
   583  	return it.b.Leaf()
   584  }
   585  
   586  func (it *differenceIterator) LeafKey() []byte {
   587  	return it.b.LeafKey()
   588  }
   589  
   590  func (it *differenceIterator) LeafBlob() []byte {
   591  	return it.b.LeafBlob()
   592  }
   593  
   594  func (it *differenceIterator) LeafProof() [][]byte {
   595  	return it.b.LeafProof()
   596  }
   597  
   598  func (it *differenceIterator) Path() []byte {
   599  	return it.b.Path()
   600  }
   601  
   602  func (it *differenceIterator) NodeBlob() []byte {
   603  	return it.b.NodeBlob()
   604  }
   605  
   606  func (it *differenceIterator) AddResolver(resolver NodeResolver) {
   607  	panic("not implemented")
   608  }
   609  
   610  func (it *differenceIterator) Next(bool) bool {
   611  	// Invariants:
   612  	// - We always advance at least one element in b.
   613  	// - At the start of this function, a's path is lexically greater than b's.
   614  	if !it.b.Next(true) {
   615  		return false
   616  	}
   617  	it.count++
   618  
   619  	if it.eof {
   620  		// a has reached eof, so we just return all elements from b
   621  		return true
   622  	}
   623  
   624  	for {
   625  		switch compareNodes(it.a, it.b) {
   626  		case -1:
   627  			// b jumped past a; advance a
   628  			if !it.a.Next(true) {
   629  				it.eof = true
   630  				return true
   631  			}
   632  			it.count++
   633  		case 1:
   634  			// b is before a
   635  			return true
   636  		case 0:
   637  			// a and b are identical; skip this whole subtree if the nodes have hashes
   638  			hasHash := it.a.Hash() == common.Hash{}
   639  			if !it.b.Next(hasHash) {
   640  				return false
   641  			}
   642  			it.count++
   643  			if !it.a.Next(hasHash) {
   644  				it.eof = true
   645  				return true
   646  			}
   647  			it.count++
   648  		}
   649  	}
   650  }
   651  
   652  func (it *differenceIterator) Error() error {
   653  	if err := it.a.Error(); err != nil {
   654  		return err
   655  	}
   656  	return it.b.Error()
   657  }
   658  
   659  type nodeIteratorHeap []NodeIterator
   660  
   661  func (h nodeIteratorHeap) Len() int            { return len(h) }
   662  func (h nodeIteratorHeap) Less(i, j int) bool  { return compareNodes(h[i], h[j]) < 0 }
   663  func (h nodeIteratorHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
   664  func (h *nodeIteratorHeap) Push(x interface{}) { *h = append(*h, x.(NodeIterator)) }
   665  func (h *nodeIteratorHeap) Pop() interface{} {
   666  	n := len(*h)
   667  	x := (*h)[n-1]
   668  	*h = (*h)[0 : n-1]
   669  	return x
   670  }
   671  
   672  type unionIterator struct {
   673  	items *nodeIteratorHeap // Nodes returned are the union of the ones in these iterators
   674  	count int               // Number of nodes scanned across all tries
   675  }
   676  
   677  // NewUnionIterator constructs a NodeIterator that iterates over elements in the union
   678  // of the provided NodeIterators. Returns the iterator, and a pointer to an integer
   679  // recording the number of nodes visited.
   680  func NewUnionIterator(iters []NodeIterator) (NodeIterator, *int) {
   681  	h := make(nodeIteratorHeap, len(iters))
   682  	copy(h, iters)
   683  	heap.Init(&h)
   684  
   685  	ui := &unionIterator{items: &h}
   686  	return ui, &ui.count
   687  }
   688  
   689  func (it *unionIterator) Hash() common.Hash {
   690  	return (*it.items)[0].Hash()
   691  }
   692  
   693  func (it *unionIterator) Parent() common.Hash {
   694  	return (*it.items)[0].Parent()
   695  }
   696  
   697  func (it *unionIterator) Leaf() bool {
   698  	return (*it.items)[0].Leaf()
   699  }
   700  
   701  func (it *unionIterator) LeafKey() []byte {
   702  	return (*it.items)[0].LeafKey()
   703  }
   704  
   705  func (it *unionIterator) LeafBlob() []byte {
   706  	return (*it.items)[0].LeafBlob()
   707  }
   708  
   709  func (it *unionIterator) LeafProof() [][]byte {
   710  	return (*it.items)[0].LeafProof()
   711  }
   712  
   713  func (it *unionIterator) Path() []byte {
   714  	return (*it.items)[0].Path()
   715  }
   716  
   717  func (it *unionIterator) NodeBlob() []byte {
   718  	return (*it.items)[0].NodeBlob()
   719  }
   720  
   721  func (it *unionIterator) AddResolver(resolver NodeResolver) {
   722  	panic("not implemented")
   723  }
   724  
   725  // Next returns the next node in the union of tries being iterated over.
   726  //
   727  // It does this by maintaining a heap of iterators, sorted by the iteration
   728  // order of their next elements, with one entry for each source trie. Each
   729  // time Next() is called, it takes the least element from the heap to return,
   730  // advancing any other iterators that also point to that same element. These
   731  // iterators are called with descend=false, since we know that any nodes under
   732  // these nodes will also be duplicates, found in the currently selected iterator.
   733  // Whenever an iterator is advanced, it is pushed back into the heap if it still
   734  // has elements remaining.
   735  //
   736  // In the case that descend=false - eg, we're asked to ignore all subnodes of the
   737  // current node - we also advance any iterators in the heap that have the current
   738  // path as a prefix.
   739  func (it *unionIterator) Next(descend bool) bool {
   740  	if len(*it.items) == 0 {
   741  		return false
   742  	}
   743  
   744  	// Get the next key from the union
   745  	least := heap.Pop(it.items).(NodeIterator)
   746  
   747  	// Skip over other nodes as long as they're identical, or, if we're not descending, as
   748  	// long as they have the same prefix as the current node.
   749  	for len(*it.items) > 0 && ((!descend && bytes.HasPrefix((*it.items)[0].Path(), least.Path())) || compareNodes(least, (*it.items)[0]) == 0) {
   750  		skipped := heap.Pop(it.items).(NodeIterator)
   751  		// Skip the whole subtree if the nodes have hashes; otherwise just skip this node
   752  		if skipped.Next(skipped.Hash() == common.Hash{}) {
   753  			it.count++
   754  			// If there are more elements, push the iterator back on the heap
   755  			heap.Push(it.items, skipped)
   756  		}
   757  	}
   758  	if least.Next(descend) {
   759  		it.count++
   760  		heap.Push(it.items, least)
   761  	}
   762  	return len(*it.items) > 0
   763  }
   764  
   765  func (it *unionIterator) Error() error {
   766  	for i := 0; i < len(*it.items); i++ {
   767  		if err := (*it.items)[i].Error(); err != nil {
   768  			return err
   769  		}
   770  	}
   771  	return nil
   772  }