Skip to content

Commit 151c7be

Browse files
committed
core/state, trie: node iterator reports parent hashes too
1 parent 5a057a8 commit 151c7be

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

‎core/state/iterator.go‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ type NodeIterator struct {
3434
stateIt *trie.NodeIterator // Primary iterator for the global state trie
3535
dataIt *trie.NodeIterator // Secondary iterator for the data trie of a contract
3636

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

40-
Hash common.Hash // Hash of the current entry being iterated (nil if not standalone)
41-
Entry interface{} // Current state entry being iterated (internal representation)
41+
Hash common.Hash // Hash of the current entry being iterated (nil if not standalone)
42+
Entry interface{} // Current state entry being iterated (internal representation)
43+
Parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
4244
}
4345

4446
// NewNodeIterator creates an post-order state node iterator.
@@ -112,6 +114,7 @@ func (it *NodeIterator) step() {
112114
panic(fmt.Sprintf("code %x: %v", account.CodeHash, err))
113115
}
114116
}
117+
it.accountHash = it.stateIt.Parent
115118
}
116119

117120
// retrieve pulls and caches the current state entry the iterator is traversing.
@@ -127,11 +130,14 @@ func (it *NodeIterator) retrieve() bool {
127130
// Otherwise retrieve the current entry
128131
switch {
129132
case it.dataIt != nil:
130-
it.Hash, it.Entry = it.dataIt.Hash, it.dataIt.Node
133+
it.Hash, it.Entry, it.Parent = it.dataIt.Hash, it.dataIt.Node, it.dataIt.Parent
134+
if it.Parent == (common.Hash{}) {
135+
it.Parent = it.accountHash
136+
}
131137
case it.code != nil:
132-
it.Hash, it.Entry = it.codeHash, it.code
138+
it.Hash, it.Entry, it.Parent = it.codeHash, it.code, it.accountHash
133139
case it.stateIt != nil:
134-
it.Hash, it.Entry = it.stateIt.Hash, it.stateIt.Node
140+
it.Hash, it.Entry, it.Parent = it.stateIt.Hash, it.stateIt.Node, it.stateIt.Parent
135141
}
136142
return true
137143
}

‎trie/iterator.go‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
155155
type 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.
250256
func (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

Comments
 (0)