From 9a70836fd490fcb05fd80175cb0b7a9319f597ba Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Tue, 28 May 2024 19:05:55 -0700 Subject: [PATCH] make BatchSerialize not return an error --- tree.go | 18 ++++++------------ tree_test.go | 13 +++---------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/tree.go b/tree.go index e7c6ba46..eda7ab10 100644 --- a/tree.go +++ b/tree.go @@ -1657,7 +1657,7 @@ type SerializedNode struct { // BatchSerialize is an optimized serialization API when multiple VerkleNodes serializations are required, and all are // available in memory. -func (n *InternalNode) BatchSerialize() ([]SerializedNode, error) { +func (n *InternalNode) BatchSerialize() []SerializedNode { // Commit to the node to update all the nodes commitments. n.Commit() @@ -1690,10 +1690,7 @@ func (n *InternalNode) BatchSerialize() ([]SerializedNode, error) { for i := range nodes { switch n := nodes[i].(type) { case *InternalNode: - serialized, err := n.serializeInternalWithUncompressedCommitment(serializedPointsIdxs, serializedPoints) - if err != nil { - return nil, err - } + serialized := n.serializeInternalWithUncompressedCommitment(serializedPointsIdxs, serializedPoints) sn := SerializedNode{ Node: n, Path: paths[i], @@ -1717,7 +1714,7 @@ func (n *InternalNode) BatchSerialize() ([]SerializedNode, error) { } } - return ret, nil + return ret } func (n *InternalNode) collectNonHashedNodes(list []VerkleNode, paths [][]byte, path []byte) ([]VerkleNode, [][]byte) { @@ -1739,7 +1736,7 @@ func (n *InternalNode) collectNonHashedNodes(list []VerkleNode, paths [][]byte, } // unpack one compressed commitment from the list of batch-compressed commitments -func (n *InternalNode) serializeInternalWithUncompressedCommitment(pointsIdx map[VerkleNode]int, serializedPoints [][banderwagon.UncompressedSize]byte) ([]byte, error) { +func (n *InternalNode) serializeInternalWithUncompressedCommitment(pointsIdx map[VerkleNode]int, serializedPoints [][banderwagon.UncompressedSize]byte) []byte { serialized := make([]byte, nodeTypeSize+bitlistSize+banderwagon.UncompressedSize) bitlist := serialized[internalBitlistOffset:internalCommitmentOffset] for i, c := range n.children { @@ -1748,13 +1745,10 @@ func (n *InternalNode) serializeInternalWithUncompressedCommitment(pointsIdx map } } serialized[nodeTypeOffset] = internalRLPType - pointidx, ok := pointsIdx[n] - if !ok { - return nil, fmt.Errorf("child node not found in cache") - } + pointidx := pointsIdx[n] copy(serialized[internalCommitmentOffset:], serializedPoints[pointidx][:]) - return serialized, nil + return serialized } func (n *LeafNode) serializeLeafWithUncompressedCommitments(cBytes, c1Bytes, c2Bytes [banderwagon.UncompressedSize]byte) []byte { diff --git a/tree_test.go b/tree_test.go index 594d6a75..8f889dd5 100644 --- a/tree_test.go +++ b/tree_test.go @@ -1419,9 +1419,7 @@ func TestBatchMigratedKeyValues(t *testing.T) { } } unbatchedRoot := tree.Commit().Bytes() - if _, err := tree.(*InternalNode).BatchSerialize(); err != nil { - t.Fatalf("failed to serialize unbatched tree: %v", err) - } + tree.(*InternalNode).BatchSerialize() unbatchedDuration += time.Since(now) // ***Insert the key pairs with optimized strategy & methods*** @@ -1465,9 +1463,7 @@ func TestBatchMigratedKeyValues(t *testing.T) { t.Fatalf("failed to insert key: %v", err) } batchedRoot := tree.Commit().Bytes() - if _, err := tree.(*InternalNode).BatchSerialize(); err != nil { - t.Fatalf("failed to serialize batched tree: %v", err) - } + tree.(*InternalNode).BatchSerialize() batchedDuration += time.Since(now) if unbatchedRoot != batchedRoot { @@ -1551,10 +1547,7 @@ func BenchmarkBatchLeavesInsert(b *testing.B) { if err := tree.(*InternalNode).InsertMigratedLeaves(newLeaves, nil); err != nil { b.Fatalf("failed to insert key: %v", err) } - - if _, err := tree.(*InternalNode).BatchSerialize(); err != nil { - b.Fatalf("failed to serialize batched tree: %v", err) - } + tree.(*InternalNode).BatchSerialize() } }