Skip to content

Commit 5126adb

Browse files
committed
conversion: make adjustements to assume at least two leves in partial tree building
Signed-off-by: Ignacio Hagopian <[email protected]>
1 parent 2373a69 commit 5126adb

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

conversion.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package verkle
22

33
import (
44
"bytes"
5-
"fmt"
65
"sort"
76
)
87

@@ -64,6 +63,8 @@ func BatchNewLeafNode(nodesValues []BatchNewLeafNodeData) []LeafNode {
6463
}
6564

6665
// BatchInsertOrderedLeaves creates a tree under from an ordered and deduplicated list of leaves.
66+
// There's weak assumption that each subtree of the first stem-byte has more than 1 leaf node.
67+
// If the whole tree has more than 2000 leaves the chance of that not being true is 0.033~=0.
6768
func BatchInsertOrderedLeaves(leaves []LeafNode) *InternalNode {
6869
// currentBranch is a representation of the current branch we're in.
6970
// The length of the branch is at most StemSize, and it might only
@@ -72,9 +73,13 @@ func BatchInsertOrderedLeaves(leaves []LeafNode) *InternalNode {
7273

7374
// Initial state is a branch with only a root node at the top, pointing to
7475
// the first leaf.
76+
currentBranch[1] = newInternalNode(1).(*InternalNode)
77+
currentBranch[1].cowChild(leaves[0].stem[1])
78+
currentBranch[1].children[leaves[0].stem[1]] = &leaves[0]
79+
7580
currentBranch[0] = New().(*InternalNode)
7681
currentBranch[0].cowChild(leaves[0].stem[0])
77-
currentBranch[0].children[leaves[0].stem[0]] = &leaves[0]
82+
currentBranch[0].children[leaves[0].stem[0]] = currentBranch[1]
7883

7984
prevLeaf := &leaves[0]
8085
leaves = leaves[1:]
@@ -145,16 +150,16 @@ func firstDiffByteIdx(stem1 []byte, stem2 []byte) int {
145150
// the partialStem. e.g: if partialStem is [a, b] it will walk to the a-th
146151
// children of the node, and then to the b-th children of that node, returning
147152
// its commitment..
148-
func GetInternalNodeCommitment(node *InternalNode, partialStem []byte) (*Point, error) {
153+
func GetInternalNodeCommitment(node *InternalNode, partialStem []byte) *Point {
149154
for i := range partialStem {
150-
var ok bool
151-
node, ok = node.children[partialStem[i]].(*InternalNode)
155+
nextNode, ok := node.children[partialStem[i]].(*InternalNode)
152156
if !ok {
153-
return nil, fmt.Errorf("partial stem is not a prefix of the tree")
157+
return node.children[partialStem[i]].(*LeafNode).commitment
154158
}
159+
node = nextNode
155160
}
156161

157-
return node.commitment, nil
162+
return node.commitment
158163
}
159164

160165
// BuildFirstTwoLayers builds the first two layers of the tree from all the precalculated
@@ -163,6 +168,7 @@ func GetInternalNodeCommitment(node *InternalNode, partialStem []byte) (*Point,
163168
// the whole tree in memory.
164169
func BuildFirstTwoLayers(commitments [256][256][32]byte) *InternalNode {
165170
var secondLevelInternalNodes [256]*InternalNode
171+
166172
for stemFirstByte := range commitments {
167173
for stemSecondByte := range commitments[stemFirstByte] {
168174
if commitments[stemFirstByte][stemSecondByte] == [32]byte{} {
@@ -176,6 +182,7 @@ func BuildFirstTwoLayers(commitments [256][256][32]byte) *InternalNode {
176182
secondLevelInternalNodes[stemFirstByte].SetChild(stemSecondByte, &hashedNode)
177183
}
178184
}
185+
179186
root := newInternalNode(0).(*InternalNode)
180187
for i, node := range secondLevelInternalNodes {
181188
if node == nil {

0 commit comments

Comments
 (0)