Skip to content

Commit 7bd113d

Browse files
authored
test: enable square tests (celestiaorg#12)
Also adds a test package for stubbing out blob and blob txs to be used by other packages in the repo
1 parent 9c430a4 commit 7bd113d

10 files changed

+582
-496
lines changed

internal/test/factory.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package test
2+
3+
import (
4+
crand "crypto/rand"
5+
"encoding/binary"
6+
"fmt"
7+
"math/rand"
8+
9+
"github.com/celestiaorg/go-square/pkg/blob"
10+
"github.com/celestiaorg/go-square/pkg/namespace"
11+
"github.com/celestiaorg/go-square/pkg/shares"
12+
)
13+
14+
var DefaultTestNamespace = namespace.MustNewV0([]byte("test"))
15+
16+
func GenerateTxs(minSize, maxSize, numTxs int) [][]byte {
17+
txs := make([][]byte, numTxs)
18+
for i := 0; i < numTxs; i++ {
19+
txs[i] = GenerateRandomTx(minSize, maxSize)
20+
}
21+
return txs
22+
}
23+
24+
func GenerateRandomTx(minSize, maxSize int) []byte {
25+
size := minSize
26+
if maxSize > minSize {
27+
size = rand.Intn(maxSize-minSize) + minSize
28+
}
29+
return RandomBytes(size)
30+
}
31+
32+
func RandomBytes(size int) []byte {
33+
b := make([]byte, size)
34+
_, err := crand.Read(b)
35+
if err != nil {
36+
panic(err)
37+
}
38+
return b
39+
}
40+
41+
func GenerateBlobTxWithNamespace(namespaces []namespace.Namespace, blobSizes []int) []byte {
42+
blobs := make([]*blob.Blob, len(blobSizes))
43+
if len(namespaces) != len(blobSizes) {
44+
panic("number of namespaces should match number of blob sizes")
45+
}
46+
for i, size := range blobSizes {
47+
blobs[i] = blob.New(namespaces[i], RandomBytes(size), shares.DefaultShareVersion)
48+
}
49+
blobTx, err := blob.MarshalBlobTx(MockPFB(toUint32(blobSizes)), blobs...)
50+
if err != nil {
51+
panic(err)
52+
}
53+
return blobTx
54+
}
55+
56+
func GenerateBlobTx(blobSizes []int) []byte {
57+
return GenerateBlobTxWithNamespace(Repeat(DefaultTestNamespace, len(blobSizes)), blobSizes)
58+
}
59+
60+
func GenerateBlobTxs(numTxs, blobsPerPfb, blobSize int) [][]byte {
61+
blobSizes := make([]int, blobsPerPfb)
62+
for i := range blobSizes {
63+
blobSizes[i] = blobSize
64+
}
65+
txs := make([][]byte, numTxs)
66+
for i := 0; i < numTxs; i++ {
67+
txs[i] = GenerateBlobTx(blobSizes)
68+
}
69+
return txs
70+
}
71+
72+
const mockPFBExtraBytes = 329
73+
74+
func MockPFB(blobSizes []uint32) []byte {
75+
if len(blobSizes) == 0 {
76+
panic("must have at least one blob")
77+
}
78+
tx := make([]byte, len(blobSizes)*4)
79+
for i, size := range blobSizes {
80+
binary.BigEndian.PutUint32(tx[i*4:], uint32(size))
81+
}
82+
83+
return append(RandomBytes(mockPFBExtraBytes), tx...)
84+
}
85+
86+
func DecodeMockPFB(pfb []byte) ([]uint32, error) {
87+
if len(pfb) < mockPFBExtraBytes+4 {
88+
return nil, fmt.Errorf("must have a length of at least %d bytes, got %d", mockPFBExtraBytes+4, len(pfb))
89+
}
90+
pfb = pfb[mockPFBExtraBytes:]
91+
blobSizes := make([]uint32, len(pfb)/4)
92+
for i := 0; i < len(blobSizes); i++ {
93+
blobSizes[i] = binary.BigEndian.Uint32(pfb[i*4 : (i+1)*4])
94+
}
95+
return blobSizes, nil
96+
}
97+
98+
func toUint32(arr []int) []uint32 {
99+
output := make([]uint32, len(arr))
100+
for i, value := range arr {
101+
output[i] = uint32(value)
102+
}
103+
return output
104+
}
105+
106+
func Repeat[T any](s T, count int) []T {
107+
ss := make([]T, count)
108+
for i := 0; i < count; i++ {
109+
ss[i] = s
110+
}
111+
return ss
112+
}

internal/test/factory_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package test_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/celestiaorg/go-square/internal/test"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestPFBParity(t *testing.T) {
11+
blobSizes := []uint32{20, 30, 10}
12+
pfb := test.MockPFB(blobSizes)
13+
output, err := test.DecodeMockPFB(pfb)
14+
require.NoError(t, err)
15+
require.Equal(t, blobSizes, output)
16+
17+
require.Panics(t, func() { test.MockPFB(nil) })
18+
19+
_, err = test.DecodeMockPFB(test.RandomBytes(20))
20+
require.Error(t, err)
21+
}

merkle/proof_test.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,18 @@ func TestProofValidateBasic(t *testing.T) {
211211
{"Good", func(sp *Proof) {}, ""},
212212
{"Negative Total", func(sp *Proof) { sp.Total = -1 }, "negative Total"},
213213
{"Negative Index", func(sp *Proof) { sp.Index = -1 }, "negative Index"},
214-
{"Invalid LeafHash", func(sp *Proof) { sp.LeafHash = make([]byte, 10) },
215-
"expected LeafHash size to be 32, got 10"},
216-
{"Too many Aunts", func(sp *Proof) { sp.Aunts = make([][]byte, MaxAunts+1) },
217-
"expected no more than 100 aunts, got 101"},
218-
{"Invalid Aunt", func(sp *Proof) { sp.Aunts[0] = make([]byte, 10) },
219-
"expected Aunts#0 size to be 32, got 10"},
214+
{
215+
"Invalid LeafHash", func(sp *Proof) { sp.LeafHash = make([]byte, 10) },
216+
"expected LeafHash size to be 32, got 10",
217+
},
218+
{
219+
"Too many Aunts", func(sp *Proof) { sp.Aunts = make([][]byte, MaxAunts+1) },
220+
"expected no more than 100 aunts, got 101",
221+
},
222+
{
223+
"Invalid Aunt", func(sp *Proof) { sp.Aunts[0] = make([]byte, 10) },
224+
"expected Aunts#0 size to be 32, got 10",
225+
},
220226
}
221227

222228
for _, tc := range testCases {
@@ -235,8 +241,8 @@ func TestProofValidateBasic(t *testing.T) {
235241
})
236242
}
237243
}
238-
func TestVoteProtobuf(t *testing.T) {
239244

245+
func TestVoteProtobuf(t *testing.T) {
240246
_, proofs := ProofsFromByteSlices([][]byte{
241247
[]byte("apple"),
242248
[]byte("watermelon"),

merkle/tree_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ func TestHashFromByteSlices(t *testing.T) {
4242
}
4343

4444
func TestProof(t *testing.T) {
45-
4645
// Try an empty proof first
4746
rootHash, proofs := ProofsFromByteSlices([][]byte{})
4847
require.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(rootHash))
@@ -100,7 +99,6 @@ func TestProof(t *testing.T) {
10099
}
101100

102101
func TestHashAlternatives(t *testing.T) {
103-
104102
total := 100
105103

106104
items := make([][]byte, total)

pkg/blob/blob.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import (
1111
"google.golang.org/protobuf/proto"
1212
)
1313

14-
var (
15-
// SupportedBlobNamespaceVersions is a list of namespace versions that can be specified by a user for blobs.
16-
SupportedBlobNamespaceVersions = []uint8{namespace.NamespaceVersionZero}
17-
)
14+
// SupportedBlobNamespaceVersions is a list of namespace versions that can be specified by a user for blobs.
15+
var SupportedBlobNamespaceVersions = []uint8{namespace.NamespaceVersionZero}
1816

1917
// ProtoBlobTxTypeID is included in each encoded BlobTx to help prevent
2018
// decoding binaries that are not actually BlobTxs.

pkg/shares/consts.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,5 @@ const (
5454
MaxShareVersion = 127
5555
)
5656

57-
var (
58-
// SupportedShareVersions is a list of supported share versions.
59-
SupportedShareVersions = []uint8{ShareVersionZero}
60-
)
57+
// SupportedShareVersions is a list of supported share versions.
58+
var SupportedShareVersions = []uint8{ShareVersionZero}

pkg/shares/split_compact_shares_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestCount(t *testing.T) {
1717
}
1818
testCases := []testCase{
1919
{transactions: [][]byte{}, wantShareCount: 0},
20-
{transactions: [][]byte{[]byte{0}}, wantShareCount: 1},
20+
{transactions: [][]byte{{0}}, wantShareCount: 1},
2121
{transactions: [][]byte{bytes.Repeat([]byte{1}, 100)}, wantShareCount: 1},
2222
// Test with 1 byte over 1 share
2323
{transactions: [][]byte{bytes.Repeat([]byte{1}, RawTxSize(FirstCompactShareContentSize+1))}, wantShareCount: 2},
@@ -179,7 +179,7 @@ func TestWriteAndExportIdempotence(t *testing.T) {
179179
bytes.Repeat([]byte{0xf}, RawTxSize(FirstCompactShareContentSize)),
180180
bytes.Repeat([]byte{0xf}, RawTxSize(ContinuationCompactShareContentSize)),
181181
bytes.Repeat([]byte{0xf}, RawTxSize(ContinuationCompactShareContentSize)),
182-
[]byte{0xf},
182+
{0xf},
183183
},
184184
wantLen: 4,
185185
},

0 commit comments

Comments
 (0)