Skip to content

Commit

Permalink
fix verkle trie iterator to not miss leaf values in the first index o…
Browse files Browse the repository at this point in the history
…f a leaf node (ethereum#88)
  • Loading branch information
jwasinger authored Mar 1, 2022
1 parent 54442c0 commit bf50506
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions trie/verkle_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package trie

import (
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"

Expand Down Expand Up @@ -88,12 +86,13 @@ func (it *verkleNodeIterator) Next(descend bool) bool {
return it.Next(descend)
case *verkle.LeafNode:
// Look for the next non-empty value
for i := it.stack[len(it.stack)-1].Index + 1; i < 256; i++ {
for i := it.stack[len(it.stack)-1].Index; i < 256; i++ {
if node.Value(i) != nil {
it.stack[len(it.stack)-1].Index = i
it.stack[len(it.stack)-1].Index = i + 1
return true
}
}

// go back to parent to get the next leaf
it.stack = it.stack[:len(it.stack)-1]
it.current = it.stack[len(it.stack)-1].Node
Expand All @@ -116,7 +115,6 @@ func (it *verkleNodeIterator) Next(descend bool) bool {
parent.Node.(*verkle.InternalNode).SetChild(parent.Index, it.current)
return true
default:
fmt.Println(node)
panic("invalid node type")
}
}
Expand Down Expand Up @@ -163,7 +161,7 @@ func (it *verkleNodeIterator) LeafKey() []byte {
panic("Leaf() called on an verkle node iterator not at a leaf location")
}

return leaf.Key(it.stack[len(it.stack)-1].Index)
return leaf.Key(it.stack[len(it.stack)-1].Index - 1)
}

// LeafBlob returns the content of the leaf. The method panics if the iterator
Expand All @@ -175,7 +173,7 @@ func (it *verkleNodeIterator) LeafBlob() []byte {
panic("LeafBlob() called on an verkle node iterator not at a leaf location")
}

return leaf.Value(it.stack[len(it.stack)-1].Index)
return leaf.Value(it.stack[len(it.stack)-1].Index - 1)
}

// LeafProof returns the Merkle proof of the leaf. The method panics if the
Expand Down

0 comments on commit bf50506

Please sign in to comment.