Skip to content

Commit

Permalink
chore: export GenerateRandomBlobOfShareCount (#1623)
Browse files Browse the repository at this point in the history
Closes #1622
  • Loading branch information
rootulp authored Apr 13, 2023
1 parent 29563f7 commit 70eb8e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
22 changes: 4 additions & 18 deletions pkg/shares/sparse_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
},
}
Expand All @@ -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))
}
21 changes: 21 additions & 0 deletions testutil/testfactory/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testfactory

import (
"bytes"
"encoding/binary"
"sort"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
Expand Down Expand Up @@ -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)
}

0 comments on commit 70eb8e5

Please sign in to comment.