iterator.go 4.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package state

import (
	"bytes"
	"fmt"

	"github.com/ethereum/go-ethereum/common"
24
	"github.com/ethereum/go-ethereum/core/types"
25 26 27 28 29 30 31 32 33
	"github.com/ethereum/go-ethereum/rlp"
	"github.com/ethereum/go-ethereum/trie"
)

// NodeIterator is an iterator to traverse the entire state trie post-order,
// including all of the contract code and contract state tries.
type NodeIterator struct {
	state *StateDB // State being iterated

34 35
	stateIt trie.NodeIterator // Primary iterator for the global state trie
	dataIt  trie.NodeIterator // Secondary iterator for the data trie of a contract
36

37 38 39
	accountHash common.Hash // Hash of the node containing the account
	codeHash    common.Hash // Hash of the contract source code
	code        []byte      // Source code associated with a contract
40

41 42
	Hash   common.Hash // Hash of the current entry being iterated (nil if not standalone)
	Parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
43 44

	Error error // Failure set in case of an internal error in the iterator
45 46 47 48 49 50 51 52 53 54
}

// NewNodeIterator creates an post-order state node iterator.
func NewNodeIterator(state *StateDB) *NodeIterator {
	return &NodeIterator{
		state: state,
	}
}

// Next moves the iterator to the next node, returning whether there are any
55 56
// further nodes. In case of an internal error this method returns false and
// sets the Error field to the encountered failure.
57
func (it *NodeIterator) Next() bool {
58 59 60 61 62 63 64 65 66
	// If the iterator failed previously, don't do anything
	if it.Error != nil {
		return false
	}
	// Otherwise step forward with the iterator and report any errors
	if err := it.step(); err != nil {
		it.Error = err
		return false
	}
67 68 69 70
	return it.retrieve()
}

// step moves the iterator to the next entry of the state trie.
71
func (it *NodeIterator) step() error {
72 73
	// Abort if we reached the end of the iteration
	if it.state == nil {
74
		return nil
75 76 77
	}
	// Initialize the iterator if we've just started
	if it.stateIt == nil {
78
		it.stateIt = it.state.trie.NodeIterator(nil)
79 80 81
	}
	// If we had data nodes previously, we surely have at least state nodes
	if it.dataIt != nil {
82 83 84
		if cont := it.dataIt.Next(true); !cont {
			if it.dataIt.Error() != nil {
				return it.dataIt.Error()
85
			}
86 87
			it.dataIt = nil
		}
88
		return nil
89 90 91 92
	}
	// If we had source code previously, discard that
	if it.code != nil {
		it.code = nil
93
		return nil
94 95
	}
	// Step to the next state trie node, terminating if we're out of nodes
96 97 98
	if cont := it.stateIt.Next(true); !cont {
		if it.stateIt.Error() != nil {
			return it.stateIt.Error()
99
		}
100
		it.state, it.stateIt = nil, nil
101
		return nil
102 103
	}
	// If the state trie node is an internal entry, leave as is
104
	if !it.stateIt.Leaf() {
105
		return nil
106 107
	}
	// Otherwise we've reached an account node, initiate data iteration
108
	var account types.StateAccount
109
	if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil {
110
		return err
111
	}
112
	dataTrie, err := it.state.db.OpenStorageTrie(common.BytesToHash(it.stateIt.LeafKey()), account.Root)
113
	if err != nil {
114
		return err
115
	}
116
	it.dataIt = dataTrie.NodeIterator(nil)
117
	if !it.dataIt.Next(true) {
118 119
		it.dataIt = nil
	}
120
	if !bytes.Equal(account.CodeHash, emptyCodeHash) {
121
		it.codeHash = common.BytesToHash(account.CodeHash)
122 123
		addrHash := common.BytesToHash(it.stateIt.LeafKey())
		it.code, err = it.state.db.ContractCode(addrHash, common.BytesToHash(account.CodeHash))
124
		if err != nil {
125
			return fmt.Errorf("code %x: %v", account.CodeHash, err)
126 127
		}
	}
128
	it.accountHash = it.stateIt.Parent()
129
	return nil
130 131 132 133 134 135
}

// retrieve pulls and caches the current state entry the iterator is traversing.
// The method returns whether there are any more data left for inspection.
func (it *NodeIterator) retrieve() bool {
	// Clear out any previously set values
136
	it.Hash = common.Hash{}
137 138 139 140 141 142 143 144

	// If the iteration's done, return no available data
	if it.state == nil {
		return false
	}
	// Otherwise retrieve the current entry
	switch {
	case it.dataIt != nil:
145
		it.Hash, it.Parent = it.dataIt.Hash(), it.dataIt.Parent()
146 147 148
		if it.Parent == (common.Hash{}) {
			it.Parent = it.accountHash
		}
149
	case it.code != nil:
150
		it.Hash, it.Parent = it.codeHash, it.accountHash
151
	case it.stateIt != nil:
152
		it.Hash, it.Parent = it.stateIt.Hash(), it.stateIt.Parent()
153 154 155
	}
	return true
}