@@ -20,6 +20,7 @@ import (
2020 "bytes"
2121 "fmt"
2222
23+ "github.com/ethereum/go-ethereum/common"
2324 "github.com/ethereum/go-ethereum/logger"
2425 "github.com/ethereum/go-ethereum/logger/glog"
2526)
@@ -152,18 +153,20 @@ func (self *Iterator) key(node interface{}) []byte {
152153// nodeIteratorState represents the iteration state at one particular node of the
153154// trie, which can be resumed at a later invocation.
154155type nodeIteratorState struct {
155- node node // Trie node being iterated
156- child int // Child to be processed next
156+ hash common.Hash // Hash of the node being iterated (nil if not standalone)
157+ node node // Trie node being iterated
158+ child int // Child to be processed next
157159}
158160
159161// NodeIterator is an iterator to traverse the trie post-order.
160162type NodeIterator struct {
161163 trie * Trie // Trie being iterated
162164 stack []* nodeIteratorState // Hierarchy of trie nodes persisting the iteration state
163165
164- Node node // Current node being iterated (internal representation)
165- Leaf bool // Flag whether the current node is a value (data) node
166- LeafBlob []byte // Data blob contained within a leaf (otherwise nil)
166+ Hash common.Hash // Hash of the current node being iterated (nil if not standalone)
167+ Node node // Current node being iterated (internal representation)
168+ Leaf bool // Flag whether the current node is a value (data) node
169+ LeafBlob []byte // Data blob contained within a leaf (otherwise nil)
167170}
168171
169172// NewNodeIterator creates an post-order trie iterator.
@@ -221,18 +224,18 @@ func (it *NodeIterator) step() {
221224 }
222225 parent .child ++
223226 it .stack = append (it .stack , & nodeIteratorState {node : node .Val , child : - 1 })
224- } else if node , ok := parent .node .(hashNode ); ok {
227+ } else if hash , ok := parent .node .(hashNode ); ok {
225228 // Hash node, resolve the hash child from the database, then the node itself
226229 if parent .child >= 0 {
227230 break
228231 }
229232 parent .child ++
230233
231- node , err := it .trie .resolveHash (node , nil , nil )
234+ node , err := it .trie .resolveHash (hash , nil , nil )
232235 if err != nil {
233236 panic (err )
234237 }
235- it .stack = append (it .stack , & nodeIteratorState {node : node , child : - 1 })
238+ it .stack = append (it .stack , & nodeIteratorState {hash : common . BytesToHash ( hash ), node : node , child : - 1 })
236239 } else {
237240 break
238241 }
@@ -246,14 +249,16 @@ func (it *NodeIterator) step() {
246249// The method returns whether there are any more data left for inspection.
247250func (it * NodeIterator ) retrieve () bool {
248251 // Clear out any previously set values
249- it .Node , it .Leaf , it .LeafBlob = nil , false , nil
252+ it .Hash , it . Node , it .Leaf , it .LeafBlob = common. Hash {}, nil , false , nil
250253
251254 // If the iteration's done, return no available data
252255 if it .trie == nil {
253256 return false
254257 }
255258 // Otherwise retrieve the current node and resolve leaf accessors
256- it .Node = it .stack [len (it .stack )- 1 ].node
259+ state := it .stack [len (it .stack )- 1 ]
260+
261+ it .Hash , it .Node = state .hash , state .node
257262 if value , ok := it .Node .(valueNode ); ok {
258263 it .Leaf , it .LeafBlob = true , []byte (value )
259264 }
0 commit comments