github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/core/state/iterator.go (about)

     1  // Copyright 2015 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 state
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"github.com/ethereumproject/go-ethereum/common"
    23  	"github.com/ethereumproject/go-ethereum/rlp"
    24  	"github.com/ethereumproject/go-ethereum/trie"
    25  )
    26  
    27  // NodeIterator is an iterator to traverse the entire state trie post-order,
    28  // including all of the contract code and contract state tries.
    29  type NodeIterator struct {
    30  	state *StateDB // State being iterated
    31  
    32  	stateIt trie.NodeIterator // Primary iterator for the global state trie
    33  	dataIt  trie.NodeIterator // Secondary iterator for the data trie of a contract
    34  
    35  	accountHash common.Hash // Hash of the node containing the account
    36  	codeHash    common.Hash // Hash of the contract source code
    37  	code        []byte      // Source code associated with a contract
    38  
    39  	Hash   common.Hash // Hash of the current entry being iterated (nil if not standalone)
    40  	Parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
    41  
    42  	Error error // Failure set in case of an internal error in the iterator
    43  }
    44  
    45  // NewNodeIterator creates an post-order state node iterator.
    46  func NewNodeIterator(state *StateDB) *NodeIterator {
    47  	return &NodeIterator{
    48  		state: state,
    49  	}
    50  }
    51  
    52  // Next moves the iterator to the next node, returning whether there are any
    53  // further nodes. In case of an internal error this method returns false and
    54  // sets the Error field to the encountered failure.
    55  func (it *NodeIterator) Next() bool {
    56  	// If the iterator failed previously, don't do anything
    57  	if it.Error != nil {
    58  		return false
    59  	}
    60  	// Otherwise step forward with the iterator and report any errors
    61  	if err := it.step(); err != nil {
    62  		it.Error = err
    63  		return false
    64  	}
    65  	return it.retrieve()
    66  }
    67  
    68  // step moves the iterator to the next entry of the state trie.
    69  func (it *NodeIterator) step() error {
    70  	// Abort if we reached the end of the iteration
    71  	if it.state == nil {
    72  		return nil
    73  	}
    74  	// Initialize the iterator if we've just started
    75  	if it.stateIt == nil {
    76  		it.stateIt = it.state.trie.NodeIterator(nil)
    77  	}
    78  	// If we had data nodes previously, we surely have at least state nodes
    79  	if it.dataIt != nil {
    80  		if cont := it.dataIt.Next(true); !cont {
    81  			if it.dataIt.Error() != nil {
    82  				return it.dataIt.Error()
    83  			}
    84  			it.dataIt = nil
    85  		}
    86  		return nil
    87  	}
    88  	// If we had source code previously, discard that
    89  	if it.code != nil {
    90  		it.code = nil
    91  		return nil
    92  	}
    93  	// Step to the next state trie node, terminating if we're out of nodes
    94  	if cont := it.stateIt.Next(true); !cont {
    95  		if it.stateIt.Error() != nil {
    96  			return it.stateIt.Error()
    97  		}
    98  		it.state, it.stateIt = nil, nil
    99  		return nil
   100  	}
   101  	// If the state trie node is an internal entry, leave as is
   102  	if !it.stateIt.Leaf() {
   103  		return nil
   104  	}
   105  	// Otherwise we've reached an account node, initiate data iteration
   106  	var account Account
   107  	if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil {
   108  		return err
   109  	}
   110  	dataTrie, err := it.state.db.OpenStorageTrie(common.BytesToHash(it.stateIt.LeafKey()), account.Root)
   111  	if err != nil {
   112  		return err
   113  	}
   114  	it.dataIt = dataTrie.NodeIterator(nil)
   115  	if !it.dataIt.Next(true) {
   116  		it.dataIt = nil
   117  	}
   118  	if !bytes.Equal(account.CodeHash, emptyCodeHash) {
   119  		it.codeHash = common.BytesToHash(account.CodeHash)
   120  		addrHash := common.BytesToHash(it.stateIt.LeafKey())
   121  		it.code, err = it.state.db.ContractCode(addrHash, common.BytesToHash(account.CodeHash))
   122  		if err != nil {
   123  			return fmt.Errorf("code %x: %v", account.CodeHash, err)
   124  		}
   125  	}
   126  	it.accountHash = it.stateIt.Parent()
   127  	return nil
   128  }
   129  
   130  // retrieve pulls and caches the current state entry the iterator is traversing.
   131  // The method returns whether there are any more data left for inspection.
   132  func (it *NodeIterator) retrieve() bool {
   133  	// Clear out any previously set values
   134  	it.Hash = common.Hash{}
   135  
   136  	// If the iteration's done, return no available data
   137  	if it.state == nil {
   138  		return false
   139  	}
   140  	// Otherwise retrieve the current entry
   141  	switch {
   142  	case it.dataIt != nil:
   143  		it.Hash, it.Parent = it.dataIt.Hash(), it.dataIt.Parent()
   144  		if it.Parent == (common.Hash{}) {
   145  			it.Parent = it.accountHash
   146  		}
   147  	case it.code != nil:
   148  		it.Hash, it.Parent = it.codeHash, it.accountHash
   149  	case it.stateIt != nil:
   150  		it.Hash, it.Parent = it.stateIt.Hash(), it.stateIt.Parent()
   151  	}
   152  	return true
   153  }