Skip to content

Commit

Permalink
chore: internalize unneeded go apis (#92)
Browse files Browse the repository at this point in the history
This PR does an initial pass of the `share` repo and internalizes things
like test methods or the `Builder` struct that should not be public.

---------

Co-authored-by: Rootul P <[email protected]>
  • Loading branch information
cmwaters and rootulp authored Jul 2, 2024
1 parent 0f26a4e commit 1ef3988
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 256 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Package | Description
----------|---------------------------------------------------------------------------------------------------------------------
inclusion | Package inclusion contains functions to generate the blob share commitment from a given blob.
merkle | Package merkle computes a deterministic minimal height Merkle tree hash.
share | Package share contains the Share data structure.
square | Package square implements the logic to construct the original data square based on a list of transactions.

Expand Down
27 changes: 24 additions & 3 deletions inclusion/blob_share_commitment_rules.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package inclusion

import (
"fmt"
"math"

"github.com/celestiaorg/go-square/share"
"golang.org/x/exp/constraints"
)

Expand Down Expand Up @@ -48,10 +48,31 @@ func RoundUpByMultipleOf(cursor, v int) int {
return ((cursor / v) + 1) * v
}

// RoundUpPowerOfTwo returns the next power of two greater than or equal to input.
func RoundUpPowerOfTwo[I constraints.Integer](input I) I {
var result I = 1
for result < input {
result <<= 1
}
return result
}

// RoundDownPowerOfTwo returns the next power of two less than or equal to input.
func RoundDownPowerOfTwo[I constraints.Integer](input I) (I, error) {
if input <= 0 {
return 0, fmt.Errorf("input %v must be positive", input)
}
roundedUp := RoundUpPowerOfTwo(input)
if roundedUp == input {
return roundedUp, nil
}
return roundedUp / 2, nil
}

// BlobMinSquareSize returns the minimum square size that can contain shareCount
// number of shares.
func BlobMinSquareSize(shareCount int) int {
return share.RoundUpPowerOfTwo(int(math.Ceil(math.Sqrt(float64(shareCount)))))
return RoundUpPowerOfTwo(int(math.Ceil(math.Sqrt(float64(shareCount)))))
}

// SubTreeWidth returns the maximum number of leaves per subtree in the share
Expand All @@ -69,7 +90,7 @@ func SubTreeWidth(shareCount, subtreeRootThreshold int) int {

// use a power of two equal to or larger than the multiple of the subtree
// root threshold
s = share.RoundUpPowerOfTwo(s)
s = RoundUpPowerOfTwo(s)

// use the minimum of the subtree width and the min square size, this
// gurarantees that a valid value is returned
Expand Down
47 changes: 45 additions & 2 deletions inclusion/blob_share_commitment_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/celestiaorg/go-square/inclusion"
"github.com/celestiaorg/go-square/share"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestNextShareIndex(t *testing.T) {
name: "at threshold",
cursor: 11,
blobLen: defaultSubtreeRootThreshold,
squareSize: share.RoundUpPowerOfTwo(defaultSubtreeRootThreshold),
squareSize: inclusion.RoundUpPowerOfTwo(defaultSubtreeRootThreshold),
expectedIndex: 11,
},
{
Expand Down Expand Up @@ -257,6 +257,28 @@ func TestRoundUpByMultipleOf(t *testing.T) {
}
}

func TestRoundUpPowerOfTwo(t *testing.T) {
type testCase struct {
input int
want int
}
testCases := []testCase{
{input: -1, want: 1},
{input: 0, want: 1},
{input: 1, want: 1},
{input: 2, want: 2},
{input: 4, want: 4},
{input: 5, want: 8},
{input: 8, want: 8},
{input: 11, want: 16},
{input: 511, want: 512},
}
for _, tc := range testCases {
got := inclusion.RoundUpPowerOfTwo(tc.input)
assert.Equal(t, tc.want, got)
}
}

func TestBlobMinSquareSize(t *testing.T) {
type testCase struct {
shareCount int
Expand Down Expand Up @@ -366,3 +388,24 @@ func TestSubTreeWidth(t *testing.T) {
})
}
}

func TestRoundDownPowerOfTwo(t *testing.T) {
type testCase struct {
input int
want int
}
testCases := []testCase{
{input: 1, want: 1},
{input: 2, want: 2},
{input: 4, want: 4},
{input: 5, want: 4},
{input: 8, want: 8},
{input: 11, want: 8},
{input: 511, want: 256},
}
for _, tc := range testCases {
got, err := inclusion.RoundDownPowerOfTwo(tc.input)
require.NoError(t, err)
assert.Equal(t, tc.want, got)
}
}
2 changes: 1 addition & 1 deletion inclusion/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func MerkleMountainRangeSizes(totalSize, maxTreeSize uint64) ([]uint64, error) {
treeSizes = append(treeSizes, maxTreeSize)
totalSize -= maxTreeSize
case totalSize < maxTreeSize:
treeSize, err := sh.RoundDownPowerOfTwo(totalSize)
treeSize, err := RoundDownPowerOfTwo(totalSize)
if err != nil {
return treeSizes, err
}
Expand Down
61 changes: 50 additions & 11 deletions share/compact_shares_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package share

import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"math/rand"
"testing"
"time"

Expand All @@ -15,7 +17,7 @@ func TestCompactShareSplitter(t *testing.T) {
// note that this test is mainly for debugging purposes, the main round trip
// tests occur in TestMerge and Test_processCompactShares
css := NewCompactShareSplitter(TxNamespace, ShareVersionZero)
txs := GenerateRandomTxs(33, 200)
txs := generateRandomTxs(33, 200)
for _, tx := range txs {
err := css.WriteTx(tx)
require.NoError(t, err)
Expand Down Expand Up @@ -73,7 +75,7 @@ func Test_processCompactShares(t *testing.T) {

// run the tests with identically sized txs
t.Run(fmt.Sprintf("%s idendically sized", tc.name), func(t *testing.T) {
txs := GenerateRandomTxs(tc.txCount, tc.txSize)
txs := generateRandomTxs(tc.txCount, tc.txSize)

shares, _, _, err := SplitTxs(txs)
require.NoError(t, err)
Expand All @@ -91,7 +93,7 @@ func Test_processCompactShares(t *testing.T) {

// run the same tests using randomly sized txs with caps of tc.txSize
t.Run(fmt.Sprintf("%s randomly sized", tc.name), func(t *testing.T) {
txs := GenerateRandomlySizedTxs(tc.txCount, tc.txSize)
txs := generateRandomlySizedTxs(tc.txCount, tc.txSize)

txShares, _, _, err := SplitTxs(txs)
require.NoError(t, err)
Expand All @@ -109,7 +111,7 @@ func Test_processCompactShares(t *testing.T) {
}

func TestAllSplit(t *testing.T) {
txs := GenerateRandomlySizedTxs(1000, 150)
txs := generateRandomlySizedTxs(1000, 150)
txShares, _, _, err := SplitTxs(txs)
require.NoError(t, err)
resTxs, err := ParseTxs(txShares)
Expand All @@ -118,21 +120,46 @@ func TestAllSplit(t *testing.T) {
}

func TestParseRandomOutOfContextShares(t *testing.T) {
txs := GenerateRandomlySizedTxs(1000, 150)
txs := generateRandomlySizedTxs(1000, 150)
txShares, _, _, err := SplitTxs(txs)
require.NoError(t, err)

for i := 0; i < 1000; i++ {
start, length := GetRandomSubSlice(len(txShares))
start, length := getRandomSubSlice(len(txShares))
randomRange := NewRange(start, start+length)
resTxs, err := ParseTxs(txShares[randomRange.Start:randomRange.End])
require.NoError(t, err)
assert.True(t, CheckSubArray(txs, resTxs))
assert.True(t, checkSubArray(txs, resTxs))
}
}

// getRandomSubSlice returns two integers representing a randomly sized range in the interval [0, size]
func getRandomSubSlice(size int) (start int, length int) {
length = rand.Intn(size + 1)
start = rand.Intn(size - length + 1)
return start, length
}

// checkSubArray returns whether subTxList is a subarray of txList
func checkSubArray(txList [][]byte, subTxList [][]byte) bool {
for i := 0; i <= len(txList)-len(subTxList); i++ {
j := 0
for j = 0; j < len(subTxList); j++ {
tx := txList[i+j]
subTx := subTxList[j]
if !bytes.Equal(tx, subTx) {
break
}
}
if j == len(subTxList) {
return true
}
}
return false
}

func TestParseOutOfContextSharesUsingShareRanges(t *testing.T) {
txs := GenerateRandomlySizedTxs(1000, 150)
txs := generateRandomlySizedTxs(1000, 150)
txShares, _, shareRanges, err := SplitTxs(txs)
require.NoError(t, err)

Expand All @@ -152,7 +179,7 @@ func TestParseOutOfContextSharesUsingShareRanges(t *testing.T) {

func TestCompactShareContainsInfoByte(t *testing.T) {
css := NewCompactShareSplitter(TxNamespace, ShareVersionZero)
txs := GenerateRandomTxs(1, ContinuationCompactShareContentSize/4)
txs := generateRandomTxs(1, ContinuationCompactShareContentSize/4)

for _, tx := range txs {
err := css.WriteTx(tx)
Expand All @@ -174,7 +201,7 @@ func TestCompactShareContainsInfoByte(t *testing.T) {

func TestContiguousCompactShareContainsInfoByte(t *testing.T) {
css := NewCompactShareSplitter(TxNamespace, ShareVersionZero)
txs := GenerateRandomTxs(1, ContinuationCompactShareContentSize*4)
txs := generateRandomTxs(1, ContinuationCompactShareContentSize*4)

for _, tx := range txs {
err := css.WriteTx(tx)
Expand All @@ -200,7 +227,7 @@ func Test_parseCompactSharesErrors(t *testing.T) {
shares []Share
}

txs := GenerateRandomTxs(2, ContinuationCompactShareContentSize*4)
txs := generateRandomTxs(2, ContinuationCompactShareContentSize*4)
txShares, _, _, err := SplitTxs(txs)
require.NoError(t, err)
rawShares := ToBytes(txShares)
Expand Down Expand Up @@ -229,3 +256,15 @@ func Test_parseCompactSharesErrors(t *testing.T) {
})
}
}

func generateRandomlySizedTxs(count, maxSize int) [][]byte {
txs := make([][]byte, count)
for i := 0; i < count; i++ {
size := rand.Intn(maxSize)
if size == 0 {
size = 1
}
txs[i] = generateRandomTxs(1, size)[0]
}
return txs
}
2 changes: 1 addition & 1 deletion share/padding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// provided should be the namespace and shareVersion of the blob that precedes
// this padding in the data square.
func NamespacePaddingShare(ns Namespace, shareVersion uint8) (Share, error) {
b, err := NewBuilder(ns, shareVersion, true)
b, err := newBuilder(ns, shareVersion, true)
if err != nil {
return Share{}, err
}
Expand Down
44 changes: 0 additions & 44 deletions share/powers_of_two.go

This file was deleted.

Loading

0 comments on commit 1ef3988

Please sign in to comment.