Skip to content

Commit

Permalink
Merge branch 'main' into unconvert
Browse files Browse the repository at this point in the history
  • Loading branch information
cmwaters authored Jan 10, 2024
2 parents 1df4c0b + 4cfe0de commit 2aab11d
Show file tree
Hide file tree
Showing 14 changed files with 587 additions and 504 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: celestiaorg/.github/.github/actions/yamllint@v0.2.8
- uses: celestiaorg/.github/.github/actions/yamllint@v0.3.0
2 changes: 1 addition & 1 deletion .github/workflows/pr-review-requester.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
jobs:
auto-request-review:
name: Auto request reviews
uses: celestiaorg/.github/.github/workflows/reusable_housekeeping.yml@v0.2.8 # yamllint disable-line rule:line-length
uses: celestiaorg/.github/.github/workflows/reusable_housekeeping.yml@v0.3.0 # yamllint disable-line rule:line-length
secrets: inherit
# write access for issues and pull requests is needed because the called
# workflow requires write access to issues and pull requests and the
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/celestiaorg/nmt v0.20.0
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb
google.golang.org/protobuf v1.31.0
google.golang.org/protobuf v1.32.0
)

require (
Expand Down
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
Expand Down Expand Up @@ -97,9 +95,8 @@ google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyac
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
112 changes: 112 additions & 0 deletions internal/test/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package test

import (
crand "crypto/rand"
"encoding/binary"
"fmt"
"math/rand"

"github.com/celestiaorg/go-square/pkg/blob"
"github.com/celestiaorg/go-square/pkg/namespace"
"github.com/celestiaorg/go-square/pkg/shares"
)

var DefaultTestNamespace = namespace.MustNewV0([]byte("test"))

func GenerateTxs(minSize, maxSize, numTxs int) [][]byte {
txs := make([][]byte, numTxs)
for i := 0; i < numTxs; i++ {
txs[i] = GenerateRandomTx(minSize, maxSize)
}
return txs
}

func GenerateRandomTx(minSize, maxSize int) []byte {
size := minSize
if maxSize > minSize {
size = rand.Intn(maxSize-minSize) + minSize
}
return RandomBytes(size)
}

func RandomBytes(size int) []byte {
b := make([]byte, size)
_, err := crand.Read(b)
if err != nil {
panic(err)
}
return b
}

func GenerateBlobTxWithNamespace(namespaces []namespace.Namespace, blobSizes []int) []byte {
blobs := make([]*blob.Blob, len(blobSizes))
if len(namespaces) != len(blobSizes) {
panic("number of namespaces should match number of blob sizes")
}
for i, size := range blobSizes {
blobs[i] = blob.New(namespaces[i], RandomBytes(size), shares.DefaultShareVersion)
}
blobTx, err := blob.MarshalBlobTx(MockPFB(toUint32(blobSizes)), blobs...)
if err != nil {
panic(err)
}
return blobTx
}

func GenerateBlobTx(blobSizes []int) []byte {
return GenerateBlobTxWithNamespace(Repeat(DefaultTestNamespace, len(blobSizes)), blobSizes)
}

func GenerateBlobTxs(numTxs, blobsPerPfb, blobSize int) [][]byte {
blobSizes := make([]int, blobsPerPfb)
for i := range blobSizes {
blobSizes[i] = blobSize
}
txs := make([][]byte, numTxs)
for i := 0; i < numTxs; i++ {
txs[i] = GenerateBlobTx(blobSizes)
}
return txs
}

const mockPFBExtraBytes = 329

func MockPFB(blobSizes []uint32) []byte {
if len(blobSizes) == 0 {
panic("must have at least one blob")
}
tx := make([]byte, len(blobSizes)*4)
for i, size := range blobSizes {
binary.BigEndian.PutUint32(tx[i*4:], uint32(size))
}

return append(RandomBytes(mockPFBExtraBytes), tx...)
}

func DecodeMockPFB(pfb []byte) ([]uint32, error) {
if len(pfb) < mockPFBExtraBytes+4 {
return nil, fmt.Errorf("must have a length of at least %d bytes, got %d", mockPFBExtraBytes+4, len(pfb))
}
pfb = pfb[mockPFBExtraBytes:]
blobSizes := make([]uint32, len(pfb)/4)
for i := 0; i < len(blobSizes); i++ {
blobSizes[i] = binary.BigEndian.Uint32(pfb[i*4 : (i+1)*4])
}
return blobSizes, nil
}

func toUint32(arr []int) []uint32 {
output := make([]uint32, len(arr))
for i, value := range arr {
output[i] = uint32(value)
}
return output
}

func Repeat[T any](s T, count int) []T {
ss := make([]T, count)
for i := 0; i < count; i++ {
ss[i] = s
}
return ss
}
21 changes: 21 additions & 0 deletions internal/test/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package test_test

import (
"testing"

"github.com/celestiaorg/go-square/internal/test"
"github.com/stretchr/testify/require"
)

func TestPFBParity(t *testing.T) {
blobSizes := []uint32{20, 30, 10}
pfb := test.MockPFB(blobSizes)
output, err := test.DecodeMockPFB(pfb)
require.NoError(t, err)
require.Equal(t, blobSizes, output)

require.Panics(t, func() { test.MockPFB(nil) })

_, err = test.DecodeMockPFB(test.RandomBytes(20))
require.Error(t, err)
}
20 changes: 13 additions & 7 deletions merkle/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,18 @@ func TestProofValidateBasic(t *testing.T) {
{"Good", func(sp *Proof) {}, ""},
{"Negative Total", func(sp *Proof) { sp.Total = -1 }, "negative Total"},
{"Negative Index", func(sp *Proof) { sp.Index = -1 }, "negative Index"},
{"Invalid LeafHash", func(sp *Proof) { sp.LeafHash = make([]byte, 10) },
"expected LeafHash size to be 32, got 10"},
{"Too many Aunts", func(sp *Proof) { sp.Aunts = make([][]byte, MaxAunts+1) },
"expected no more than 100 aunts, got 101"},
{"Invalid Aunt", func(sp *Proof) { sp.Aunts[0] = make([]byte, 10) },
"expected Aunts#0 size to be 32, got 10"},
{
"Invalid LeafHash", func(sp *Proof) { sp.LeafHash = make([]byte, 10) },
"expected LeafHash size to be 32, got 10",
},
{
"Too many Aunts", func(sp *Proof) { sp.Aunts = make([][]byte, MaxAunts+1) },
"expected no more than 100 aunts, got 101",
},
{
"Invalid Aunt", func(sp *Proof) { sp.Aunts[0] = make([]byte, 10) },
"expected Aunts#0 size to be 32, got 10",
},
}

for _, tc := range testCases {
Expand All @@ -235,8 +241,8 @@ func TestProofValidateBasic(t *testing.T) {
})
}
}
func TestVoteProtobuf(t *testing.T) {

func TestVoteProtobuf(t *testing.T) {
_, proofs := ProofsFromByteSlices([][]byte{
[]byte("apple"),
[]byte("watermelon"),
Expand Down
2 changes: 0 additions & 2 deletions merkle/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func TestHashFromByteSlices(t *testing.T) {
}

func TestProof(t *testing.T) {

// Try an empty proof first
rootHash, proofs := ProofsFromByteSlices([][]byte{})
require.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(rootHash))
Expand Down Expand Up @@ -100,7 +99,6 @@ func TestProof(t *testing.T) {
}

func TestHashAlternatives(t *testing.T) {

total := 100

items := make([][]byte, total)
Expand Down
6 changes: 2 additions & 4 deletions pkg/blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import (
"google.golang.org/protobuf/proto"
)

var (
// SupportedBlobNamespaceVersions is a list of namespace versions that can be specified by a user for blobs.
SupportedBlobNamespaceVersions = []uint8{namespace.NamespaceVersionZero}
)
// SupportedBlobNamespaceVersions is a list of namespace versions that can be specified by a user for blobs.
var SupportedBlobNamespaceVersions = []uint8{namespace.NamespaceVersionZero}

// ProtoBlobTxTypeID is included in each encoded BlobTx to help prevent
// decoding binaries that are not actually BlobTxs.
Expand Down
6 changes: 2 additions & 4 deletions pkg/shares/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,5 @@ const (
MaxShareVersion = 127
)

var (
// SupportedShareVersions is a list of supported share versions.
SupportedShareVersions = []uint8{ShareVersionZero}
)
// SupportedShareVersions is a list of supported share versions.
var SupportedShareVersions = []uint8{ShareVersionZero}
4 changes: 2 additions & 2 deletions pkg/shares/split_compact_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCount(t *testing.T) {
}
testCases := []testCase{
{transactions: [][]byte{}, wantShareCount: 0},
{transactions: [][]byte{[]byte{0}}, wantShareCount: 1},
{transactions: [][]byte{{0}}, wantShareCount: 1},
{transactions: [][]byte{bytes.Repeat([]byte{1}, 100)}, wantShareCount: 1},
// Test with 1 byte over 1 share
{transactions: [][]byte{bytes.Repeat([]byte{1}, RawTxSize(FirstCompactShareContentSize+1))}, wantShareCount: 2},
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestWriteAndExportIdempotence(t *testing.T) {
bytes.Repeat([]byte{0xf}, RawTxSize(FirstCompactShareContentSize)),
bytes.Repeat([]byte{0xf}, RawTxSize(ContinuationCompactShareContentSize)),
bytes.Repeat([]byte{0xf}, RawTxSize(ContinuationCompactShareContentSize)),
[]byte{0xf},
{0xf},
},
wantLen: 4,
},
Expand Down
Loading

0 comments on commit 2aab11d

Please sign in to comment.