github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/state/iterator.go (about)

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