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