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

fix: NodeIterator return error from empty trie root #119

Merged
merged 1 commit into from
Nov 6, 2024
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
2 changes: 1 addition & 1 deletion trie/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ func zkMerkleTreeNodeBlobFunctions(findBlobByHash func(key []byte) ([]byte, erro
) {
return func(hash common.Hash) ([]byte, error) {
if bytes.Equal(hash.Bytes(), zkt.HashZero[:]) {
return nil, nil
return zk.EmptyNodeValue.CanonicalValue(), nil
}
return findBlobByHash(zkt.ReverseByteOrder(hash.Bytes()))
},
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) {
Comment on lines +667 to +668
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve error handling in iterator creation.

The iterator creation error is currently being ignored with it, _ := trie.NodeIterator(nil). Consider handling this error explicitly since the PR's objective is related to NodeIterator errors.

Apply this improvement to both test cases:

-        it, _ := trie.NodeIterator(nil)
+        it, err := trie.NodeIterator(nil)
+        if err != nil {
+            t.Fatalf("failed to create iterator: %v", err)
+        }

Also applies to: 677-678

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
Loading