@@ -153,9 +153,10 @@ func (self *Iterator) key(node interface{}) []byte {
153153// nodeIteratorState represents the iteration state at one particular node of the
154154// trie, which can be resumed at a later invocation.
155155type nodeIteratorState struct {
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
156+ hash common.Hash // Hash of the node being iterated (nil if not standalone)
157+ node node // Trie node being iterated
158+ parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
159+ child int // Child to be processed next
159160}
160161
161162// NodeIterator is an iterator to traverse the trie post-order.
@@ -165,6 +166,7 @@ type NodeIterator struct {
165166
166167 Hash common.Hash // Hash of the current node being iterated (nil if not standalone)
167168 Node node // Current node being iterated (internal representation)
169+ Parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
168170 Leaf bool // Flag whether the current node is a value (data) node
169171 LeafBlob []byte // Data blob contained within a leaf (otherwise nil)
170172}
@@ -206,14 +208,18 @@ func (it *NodeIterator) step() {
206208 // Continue iteration to the next child
207209 for {
208210 parent := it .stack [len (it .stack )- 1 ]
211+ ancestor := parent .hash
212+ if (ancestor == common.Hash {}) {
213+ ancestor = parent .parent
214+ }
209215 if node , ok := parent .node .(fullNode ); ok {
210216 // Full node, traverse all children, then the node itself
211217 if parent .child >= len (node ) {
212218 break
213219 }
214220 for parent .child ++ ; parent .child < len (node ); parent .child ++ {
215221 if current := node [parent .child ]; current != nil {
216- it .stack = append (it .stack , & nodeIteratorState {node : current , child : - 1 })
222+ it .stack = append (it .stack , & nodeIteratorState {node : current , parent : ancestor , child : - 1 })
217223 break
218224 }
219225 }
@@ -223,7 +229,7 @@ func (it *NodeIterator) step() {
223229 break
224230 }
225231 parent .child ++
226- it .stack = append (it .stack , & nodeIteratorState {node : node .Val , child : - 1 })
232+ it .stack = append (it .stack , & nodeIteratorState {node : node .Val , parent : ancestor , child : - 1 })
227233 } else if hash , ok := parent .node .(hashNode ); ok {
228234 // Hash node, resolve the hash child from the database, then the node itself
229235 if parent .child >= 0 {
@@ -235,7 +241,7 @@ func (it *NodeIterator) step() {
235241 if err != nil {
236242 panic (err )
237243 }
238- it .stack = append (it .stack , & nodeIteratorState {hash : common .BytesToHash (hash ), node : node , child : - 1 })
244+ it .stack = append (it .stack , & nodeIteratorState {hash : common .BytesToHash (hash ), node : node , parent : ancestor , child : - 1 })
239245 } else {
240246 break
241247 }
@@ -249,7 +255,7 @@ func (it *NodeIterator) step() {
249255// The method returns whether there are any more data left for inspection.
250256func (it * NodeIterator ) retrieve () bool {
251257 // Clear out any previously set values
252- it .Hash , it .Node , it .Leaf , it .LeafBlob = common.Hash {}, nil , false , nil
258+ it .Hash , it .Node , it .Parent , it . Leaf , it .LeafBlob = common.Hash {}, nil , common. Hash {} , false , nil
253259
254260 // If the iteration's done, return no available data
255261 if it .trie == nil {
@@ -258,7 +264,7 @@ func (it *NodeIterator) retrieve() bool {
258264 // Otherwise retrieve the current node and resolve leaf accessors
259265 state := it .stack [len (it .stack )- 1 ]
260266
261- it .Hash , it .Node = state .hash , state .node
267+ it .Hash , it .Node , it . Parent = state .hash , state .node , state . parent
262268 if value , ok := it .Node .(valueNode ); ok {
263269 it .Leaf , it .LeafBlob = true , []byte (value )
264270 }
0 commit comments