From 70eb8e59b1f3b1365f3386e795f462a0a306db2d Mon Sep 17 00:00:00 2001 From: Rootul P Date: Thu, 13 Apr 2023 08:02:02 -0400 Subject: [PATCH] chore: export GenerateRandomBlobOfShareCount (#1623) Closes https://github.com/celestiaorg/celestia-app/issues/1622 --- pkg/shares/sparse_shares_test.go | 22 ++++------------------ testutil/testfactory/blob.go | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/pkg/shares/sparse_shares_test.go b/pkg/shares/sparse_shares_test.go index f922673b8f..278cf432a7 100644 --- a/pkg/shares/sparse_shares_test.go +++ b/pkg/shares/sparse_shares_test.go @@ -11,7 +11,7 @@ import ( ) func TestSparseShareContainsInfoByte(t *testing.T) { - blob := generateRandomBlobOfShareCount(4) + blob := testfactory.GenerateRandomBlobOfShareCount(4) sequenceStartInfoByte, err := NewInfoByte(appconsts.ShareVersionZero, true) require.NoError(t, err) @@ -59,17 +59,17 @@ func TestSparseShareSplitterCount(t *testing.T) { testCases := []testCase{ { name: "one share", - blob: generateRandomBlobOfShareCount(1), + blob: testfactory.GenerateRandomBlobOfShareCount(1), expected: 1, }, { name: "two shares", - blob: generateRandomBlobOfShareCount(2), + blob: testfactory.GenerateRandomBlobOfShareCount(2), expected: 2, }, { name: "ten shares", - blob: generateRandomBlobOfShareCount(10), + blob: testfactory.GenerateRandomBlobOfShareCount(10), expected: 10, }, } @@ -84,17 +84,3 @@ func TestSparseShareSplitterCount(t *testing.T) { }) } } - -// generateRandomBlobOfShareCount returns a blob that spans the given -// number of shares -func generateRandomBlobOfShareCount(count int) coretypes.Blob { - size := rawBlobSize(appconsts.FirstSparseShareContentSize * count) - return testfactory.GenerateRandomBlob(size) -} - -// rawBlobSize returns the raw blob size that can be used to construct a -// blob of totalSize bytes. This function is useful in tests to account for -// the delimiter length that is prefixed to a blob's data. -func rawBlobSize(totalSize int) int { - return totalSize - DelimLen(uint64(totalSize)) -} diff --git a/testutil/testfactory/blob.go b/testutil/testfactory/blob.go index 52c2e5a86a..9af6248736 100644 --- a/testutil/testfactory/blob.go +++ b/testutil/testfactory/blob.go @@ -2,6 +2,7 @@ package testfactory import ( "bytes" + "encoding/binary" "sort" "github.com/celestiaorg/celestia-app/pkg/appconsts" @@ -57,3 +58,23 @@ func GenerateRandomBlob(dataSize int) types.Blob { } return blob } + +// GenerateRandomBlobOfShareCount returns a blob that spans the given +// number of shares +func GenerateRandomBlobOfShareCount(count int) types.Blob { + size := rawBlobSize(appconsts.FirstSparseShareContentSize * count) + return GenerateRandomBlob(size) +} + +// rawBlobSize returns the raw blob size that can be used to construct a +// blob of totalSize bytes. This function is useful in tests to account for +// the delimiter length that is prefixed to a blob's data. +func rawBlobSize(totalSize int) int { + return totalSize - DelimLen(uint64(totalSize)) +} + +// DelimLen calculates the length of the delimiter for a given unit size +func DelimLen(size uint64) int { + lenBuf := make([]byte, binary.MaxVarintLen64) + return binary.PutUvarint(lenBuf, size) +}