From ef2fca30067eb695b714bc0b4e051768a7dc7311 Mon Sep 17 00:00:00 2001 From: Logan Date: Fri, 25 Oct 2024 11:54:24 +0900 Subject: [PATCH] fix: NodeIterator return error from empty trie root --- trie/iterator.go | 4 +++- trie/iterator_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/trie/iterator.go b/trie/iterator.go index 26e8e48d4..b6003275f 100644 --- a/trie/iterator.go +++ b/trie/iterator.go @@ -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) } diff --git a/trie/iterator_test.go b/trie/iterator_test.go index 8322904fc..d60d49ae0 100644 --- a/trie/iterator_test.go +++ b/trie/iterator_test.go @@ -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()