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