Skip to content

Commit

Permalink
replay fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Ballet <[email protected]>
  • Loading branch information
gballet committed Sep 13, 2024
1 parent ee9413f commit 4deabaa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func parseSkipList(serialized []byte, depth byte) (VerkleNode, error) {
return nil, fmt.Errorf("leaf node commitments are not the correct size, expected at least %d, got %d", 3*banderwagon.UncompressedSize, len(serialized[leafSkipListCOffset:]))
}

if err := ln.c1.SetBytesUncompressed(serialized[leafC1CommitmentOffset:leafSkipListC2Offset], true); err != nil {
if err := ln.c1.SetBytesUncompressed(serialized[leafSkipListC1Offset:leafSkipListC2Offset], true); err != nil {
return nil, fmt.Errorf("setting c1 commitment: %w", err)
}
ln.c2 = new(Point)
Expand Down
16 changes: 12 additions & 4 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,10 @@ var (
EmptyCodeHash, _ = hex.DecodeString("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")
)

// skipListMaxGapCount is the maximum allowed number of
// gaps before one has to fall back to the bitmap.
const skipListMaxGapCount = 16

func (n *LeafNode) serializeLeafWithUncompressedCommitments(cBytes, c1Bytes, c2Bytes [banderwagon.UncompressedSize]byte) []byte {
// Empty value in LeafNode used for padding.
var emptyValue [LeafValueSize]byte
Expand All @@ -1777,7 +1781,7 @@ func (n *LeafNode) serializeLeafWithUncompressedCommitments(cBytes, c1Bytes, c2B
isEoA = true
count, lastIdx int
gapcount int
gaps [32]struct {
gaps [skipListMaxGapCount]struct {
Skip byte // How many slots to skip before the next range
Count int // Size of the next range. `int` because a full leaf has 256 entries
}
Expand All @@ -1786,14 +1790,18 @@ func (n *LeafNode) serializeLeafWithUncompressedCommitments(cBytes, c1Bytes, c2B
if v != nil {
count++
lastIdx = i
gaps[gapcount].Count++
if gapcount < skipListMaxGapCount {
gaps[gapcount].Count++
}

setBit(bitlist[:], i)
children = append(children, v...)
if padding := emptyValue[:LeafValueSize-len(v)]; len(padding) != 0 {
children = append(children, padding...)
}
} else {
} else if gapcount < skipListMaxGapCount {
// If we reach the 256th empty leaf in the node, this means
// that the whole node is empty, which should not happen.
if gaps[gapcount].Skip == 255 {
panic("empty leaf node")
}
Expand Down Expand Up @@ -1839,7 +1847,7 @@ func (n *LeafNode) serializeLeafWithUncompressedCommitments(cBytes, c1Bytes, c2B
copy(result[leafStemOffset+StemSize:], c1Bytes[:])
copy(result[leafStemOffset+StemSize+banderwagon.UncompressedSize:], cBytes[:])
copy(result[leafStemOffset+StemSize+2*banderwagon.UncompressedSize:], n.values[0]) // copy basic data
case gapcount < 16:
case gapcount < skipListMaxGapCount:
// If there are less than 16 gaps, it's worth using skiplists
result = make([]byte, 1, nodeTypeSize+StemSize+bitlistSize+3*banderwagon.UncompressedSize+len(children))
result[0] = skipListType
Expand Down

0 comments on commit 4deabaa

Please sign in to comment.