|
| 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 | +} |
0 commit comments