Skip to content

Commit

Permalink
trie: fix two issues in trie iterator (ethereum#24539)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rjl493456442 authored and 0xmountaintop committed May 28, 2024
1 parent 9f961dd commit 2b9e2c5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
14 changes: 9 additions & 5 deletions trie/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,11 @@ func (e seekError) Error() string {
}

func newNodeIterator(trie *Trie, start []byte) NodeIterator {
if trie.Hash() == emptyState {
return new(nodeIterator)
if trie.Hash() == emptyRoot {
return &nodeIterator{
trie: trie,
err: errIteratorEnd,
}
}
it := &nodeIterator{trie: trie}
it.err = it.seek(start)
Expand Down Expand Up @@ -402,7 +405,7 @@ func findChild(n *fullNode, index int, path []byte, ancestor common.Hash) (node,
func (it *nodeIterator) nextChild(parent *nodeIteratorState, ancestor common.Hash) (*nodeIteratorState, []byte, bool) {
switch node := parent.node.(type) {
case *fullNode:
//Full node, move to the first non-nil child.
// Full node, move to the first non-nil child.
if child, state, path, index := findChild(node, parent.index+1, it.path, ancestor); child != nil {
parent.index = index - 1
return state, path, true
Expand Down Expand Up @@ -480,8 +483,9 @@ func (it *nodeIterator) push(state *nodeIteratorState, parentIndex *int, path []
}

func (it *nodeIterator) pop() {
parent := it.stack[len(it.stack)-1]
it.path = it.path[:parent.pathlen]
last := it.stack[len(it.stack)-1]
it.path = it.path[:last.pathlen]
it.stack[len(it.stack)-1] = nil
it.stack = it.stack[:len(it.stack)-1]
}

Expand Down
13 changes: 13 additions & 0 deletions trie/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ import (
"github.com/scroll-tech/go-ethereum/ethdb/memorydb"
)

func TestEmptyIterator(t *testing.T) {
trie := newEmpty()
iter := trie.NodeIterator(nil)

seen := make(map[string]struct{})
for iter.Next(true) {
seen[string(iter.Path())] = struct{}{}
}
if len(seen) != 0 {
t.Fatal("Unexpected trie node iterated")
}
}

func TestIterator(t *testing.T) {
trie := newEmpty()
vals := []struct{ k, v string }{
Expand Down

0 comments on commit 2b9e2c5

Please sign in to comment.