Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trie: fixes two issues in trie iterator #24539

Merged
merged 4 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions trie/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,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 @@ -425,7 +428,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 @@ -503,8 +506,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
14 changes: 14 additions & 0 deletions trie/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ import (
"github.com/ethereum/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{}{}
fmt.Println("iter.path", iter.Path(), "val", iter.NodeBlob())
}
if len(seen) != 0 {
t.Fatal("Unexpected trie node iterated")
}
}

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