Skip to content

Commit 205a94e

Browse files
authored
trie: fix two issues in trie iterator ethereum#24539 (#1086)
* trie: fix memory leak in trie iterator In the trie iterator, live nodes are tracked in a stack while iterating. Popped node states should be explictly set to nil in order to get garbage-collected. * trie: fix empty trie iterator
1 parent f07824d commit 205a94e

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

trie/iterator.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,11 @@ func (e seekError) Error() string {
151151
}
152152

153153
func newNodeIterator(trie *Trie, start []byte) NodeIterator {
154-
if trie.Hash() == types.EmptyCodeHash {
155-
return new(nodeIterator)
154+
if trie.Hash() == types.EmptyRootHash {
155+
return &nodeIterator{
156+
trie: trie,
157+
err: errIteratorEnd,
158+
}
156159
}
157160
it := &nodeIterator{trie: trie}
158161
it.err = it.seek(start)
@@ -401,7 +404,7 @@ func findChild(n *fullNode, index int, path []byte, ancestor common.Hash) (node,
401404
func (it *nodeIterator) nextChild(parent *nodeIteratorState, ancestor common.Hash) (*nodeIteratorState, []byte, bool) {
402405
switch node := parent.node.(type) {
403406
case *fullNode:
404-
// Full Node, move to the first non-nil child.
407+
// Full node, move to the first non-nil child.
405408
if child, state, path, index := findChild(node, parent.index+1, it.path, ancestor); child != nil {
406409
parent.index = index - 1
407410
return state, path, true
@@ -479,8 +482,9 @@ func (it *nodeIterator) push(state *nodeIteratorState, parentIndex *int, path []
479482
}
480483

481484
func (it *nodeIterator) pop() {
482-
parent := it.stack[len(it.stack)-1]
483-
it.path = it.path[:parent.pathlen]
485+
last := it.stack[len(it.stack)-1]
486+
it.path = it.path[:last.pathlen]
487+
it.stack[len(it.stack)-1] = nil
484488
it.stack = it.stack[:len(it.stack)-1]
485489
}
486490

trie/iterator_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ import (
3030
"github.com/XinFinOrg/XDPoSChain/ethdb/memorydb"
3131
)
3232

33+
func TestEmptyIterator(t *testing.T) {
34+
trie := newEmpty()
35+
iter := trie.NodeIterator(nil)
36+
37+
seen := make(map[string]struct{})
38+
for iter.Next(true) {
39+
seen[string(iter.Path())] = struct{}{}
40+
}
41+
if len(seen) != 0 {
42+
t.Fatal("Unexpected trie node iterated")
43+
}
44+
}
45+
3346
func TestIterator(t *testing.T) {
3447
trie := newEmpty()
3548
vals := []struct{ k, v string }{

0 commit comments

Comments
 (0)