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