Skip to content

Commit

Permalink
fix: NodeIterator return error from empty trie root
Browse files Browse the repository at this point in the history
  • Loading branch information
jyc228 committed Oct 25, 2024
1 parent 71b2588 commit ef2fca3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion trie/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,9 @@ func newMerkleTreeIterator(
it := &merkleTreeIterator{findNodeBlobByHash: findNodeBlobByHash, nodeBlobToIteratorNode: nodeBlobToIteratorNode}
var rootNode merkleTreeIteratorNode
var blob []byte
blob, it.err = findNodeBlobByHash(root)
if blob, it.err = findNodeBlobByHash(root); len(blob) == 0 {
return it
}
if it.err == nil {
rootNode, it.err = nodeBlobToIteratorNode(root, blob)
}
Expand Down
23 changes: 23 additions & 0 deletions trie/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,29 @@ func TestMerkleTreeIterator(t *testing.T) {
return tree, db
}

t.Run("empty root", func(t *testing.T) {
t.Run("zk merkle tree", func(t *testing.T) {
trie := NewEmptyMerkleTrie(NewZkDatabase(rawdb.NewMemoryDatabase()))
it, _ := trie.NodeIterator(nil)
for it.Next(true) {
t.Fail()
}
if it.Error() != nil {
t.Error(it.Error())
}
})
t.Run("zk trie", func(t *testing.T) {
trie, _ := NewZkTrie(common.Hash{}, NewZkDatabase(rawdb.NewDatabase(memorydb.New())))
it, _ := trie.NodeIterator(nil)
for it.Next(true) {
t.Fail()
}
if it.Error() != nil {
t.Error(it.Error())
}
})
})

t.Run("zk merkle tree", func(t *testing.T) {
tree, db := makeMerkleTreeWithData(testdata1)
expected := db.Len()
Expand Down

0 comments on commit ef2fca3

Please sign in to comment.