Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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/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{}{}
}
if len(seen) != 0 {
t.Fatal("Unexpected trie node iterated")
}
}

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