github.com/MetalBlockchain/subnet-evm@v0.4.9/core/state/iterator.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2015 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package state
    28  
    29  import (
    30  	"bytes"
    31  	"fmt"
    32  
    33  	"github.com/MetalBlockchain/subnet-evm/core/types"
    34  	"github.com/MetalBlockchain/subnet-evm/trie"
    35  	"github.com/ethereum/go-ethereum/common"
    36  	"github.com/ethereum/go-ethereum/rlp"
    37  )
    38  
    39  // NodeIterator is an iterator to traverse the entire state trie post-order,
    40  // including all of the contract code and contract state tries.
    41  type NodeIterator struct {
    42  	state *StateDB // State being iterated
    43  
    44  	stateIt trie.NodeIterator // Primary iterator for the global state trie
    45  	dataIt  trie.NodeIterator // Secondary iterator for the data trie of a contract
    46  
    47  	accountHash common.Hash // Hash of the node containing the account
    48  	codeHash    common.Hash // Hash of the contract source code
    49  	code        []byte      // Source code associated with a contract
    50  
    51  	Hash   common.Hash // Hash of the current entry being iterated (nil if not standalone)
    52  	Parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
    53  
    54  	Error error // Failure set in case of an internal error in the iterator
    55  }
    56  
    57  // NewNodeIterator creates an post-order state node iterator.
    58  func NewNodeIterator(state *StateDB) *NodeIterator {
    59  	return &NodeIterator{
    60  		state: state,
    61  	}
    62  }
    63  
    64  // Next moves the iterator to the next node, returning whether there are any
    65  // further nodes. In case of an internal error this method returns false and
    66  // sets the Error field to the encountered failure.
    67  func (it *NodeIterator) Next() bool {
    68  	// If the iterator failed previously, don't do anything
    69  	if it.Error != nil {
    70  		return false
    71  	}
    72  	// Otherwise step forward with the iterator and report any errors
    73  	if err := it.step(); err != nil {
    74  		it.Error = err
    75  		return false
    76  	}
    77  	return it.retrieve()
    78  }
    79  
    80  // step moves the iterator to the next entry of the state trie.
    81  func (it *NodeIterator) step() error {
    82  	// Abort if we reached the end of the iteration
    83  	if it.state == nil {
    84  		return nil
    85  	}
    86  	// Initialize the iterator if we've just started
    87  	if it.stateIt == nil {
    88  		it.stateIt = it.state.trie.NodeIterator(nil)
    89  	}
    90  	// If we had data nodes previously, we surely have at least state nodes
    91  	if it.dataIt != nil {
    92  		if cont := it.dataIt.Next(true); !cont {
    93  			if it.dataIt.Error() != nil {
    94  				return it.dataIt.Error()
    95  			}
    96  			it.dataIt = nil
    97  		}
    98  		return nil
    99  	}
   100  	// If we had source code previously, discard that
   101  	if it.code != nil {
   102  		it.code = nil
   103  		return nil
   104  	}
   105  	// Step to the next state trie node, terminating if we're out of nodes
   106  	if cont := it.stateIt.Next(true); !cont {
   107  		if it.stateIt.Error() != nil {
   108  			return it.stateIt.Error()
   109  		}
   110  		it.state, it.stateIt = nil, nil
   111  		return nil
   112  	}
   113  	// If the state trie node is an internal entry, leave as is
   114  	if !it.stateIt.Leaf() {
   115  		return nil
   116  	}
   117  	// Otherwise we've reached an account node, initiate data iteration
   118  	var account types.StateAccount
   119  	if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil {
   120  		return err
   121  	}
   122  	dataTrie, err := it.state.db.OpenStorageTrie(common.BytesToHash(it.stateIt.LeafKey()), account.Root)
   123  	if err != nil {
   124  		return err
   125  	}
   126  	it.dataIt = dataTrie.NodeIterator(nil)
   127  	if !it.dataIt.Next(true) {
   128  		it.dataIt = nil
   129  	}
   130  	if !bytes.Equal(account.CodeHash, emptyCodeHash) {
   131  		it.codeHash = common.BytesToHash(account.CodeHash)
   132  		addrHash := common.BytesToHash(it.stateIt.LeafKey())
   133  		it.code, err = it.state.db.ContractCode(addrHash, common.BytesToHash(account.CodeHash))
   134  		if err != nil {
   135  			return fmt.Errorf("code %x: %v", account.CodeHash, err)
   136  		}
   137  	}
   138  	it.accountHash = it.stateIt.Parent()
   139  	return nil
   140  }
   141  
   142  // retrieve pulls and caches the current state entry the iterator is traversing.
   143  // The method returns whether there are any more data left for inspection.
   144  func (it *NodeIterator) retrieve() bool {
   145  	// Clear out any previously set values
   146  	it.Hash = common.Hash{}
   147  
   148  	// If the iteration's done, return no available data
   149  	if it.state == nil {
   150  		return false
   151  	}
   152  	// Otherwise retrieve the current entry
   153  	switch {
   154  	case it.dataIt != nil:
   155  		it.Hash, it.Parent = it.dataIt.Hash(), it.dataIt.Parent()
   156  		if it.Parent == (common.Hash{}) {
   157  			it.Parent = it.accountHash
   158  		}
   159  	case it.code != nil:
   160  		it.Hash, it.Parent = it.codeHash, it.accountHash
   161  	case it.stateIt != nil:
   162  		it.Hash, it.Parent = it.stateIt.Hash(), it.stateIt.Parent()
   163  	}
   164  	return true
   165  }