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