Repository navigation
Expand file tree
/
Copy pathsquare_fuzz_test.go
More file actions
164 lines (157 loc) · 6.18 KB
/
Copy pathsquare_fuzz_test.go
File metadata and controls
164 lines (157 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package square_test
import (
"bytes"
"crypto/sha256"
"testing"
square "github.com/celestiaorg/go-square/v4"
"github.com/celestiaorg/go-square/v4/share"
"github.com/celestiaorg/go-square/v4/tx"
"github.com/stretchr/testify/require"
)
func FuzzConstruct(f *testing.F) {
f.Add([]byte{}, []byte{}, uint8(0), uint8(0))
f.Add([]byte("tx"), []byte("other"), uint8(1), uint8(1))
f.Add(bytes.Repeat([]byte{1}, 1000), []byte("tx"), uint8(0), uint8(64))
blob, err := share.NewV0Blob(share.MustNewV0Namespace([]byte{1, 0}), []byte("blob"))
require.NoError(f, err)
blobTx, err := tx.MarshalBlobTx([]byte("pfb"), blob)
require.NoError(f, err)
f.Add([]byte("tx"), blobTx, uint8(2), uint8(64))
f.Add(blobTx, []byte("tx"), uint8(2), uint8(64)) // invalid ordering
f.Add(blobTx[:len(blobTx)-1], []byte{}, uint8(2), uint8(0))
f.Fuzz(func(t *testing.T, a, b []byte, size, threshold uint8) {
if len(a)+len(b) > 32*1024 {
t.Skip()
}
txs := make([]square.ClassifiedTx, 0, 2)
validBlobNamespaces := true
for _, raw := range [][]byte{a, b} {
if len(raw) > 0 {
txs = append(txs, square.NewClassifiedTx(raw))
validBlobNamespaces = validBlobNamespaces && hasValidBlobNamespaces(raw)
}
}
maxSize := 1 << (size % 5)
result, err := square.Construct(txs, maxSize, int(threshold))
if err != nil {
return // malformed blobs, ordering, capacity, or threshold
}
// Construct does not validate blob namespaces; callers reject reserved
// ones beforehand, so the square layout only holds for valid namespaces.
if validBlobNamespaces {
assertFuzzSquare(t, result, maxSize)
}
again, err := square.Construct(txs, maxSize, int(threshold))
require.NoError(t, err)
require.True(t, result.Equals(again), "construction must be deterministic")
})
}
// Structured inputs reach blob placement and index calculation even when
// arbitrary transaction bytes rarely decode as valid BlobTx envelopes.
func FuzzConstructBlobRoundTrip(f *testing.F) {
f.Add([]byte("tx"), []byte("blob"), false, false, uint8(2), uint8(64))
f.Add(bytes.Repeat([]byte{1}, 473), bytes.Repeat([]byte{2}, 479), false, true, uint8(3), uint8(1))
f.Add([]byte("tx"), bytes.Repeat([]byte{2}, 459), true, true, uint8(4), uint8(64))
f.Add([]byte("tx"), bytes.Repeat([]byte{2}, 4096), true, false, uint8(0), uint8(2))
f.Fuzz(func(t *testing.T, normal, data []byte, withSigner, withFibre bool, size, threshold uint8) {
if len(normal) == 0 || len(data) == 0 || len(normal)+len(data) > 32*1024 {
t.Skip()
}
version := share.ShareVersionZero
var signer []byte
if withSigner {
version = share.ShareVersionOne
signer = bytes.Repeat([]byte{1}, share.SignerSize)
}
ns := share.MustNewV0Namespace([]byte{1, 0})
blob, err := share.NewBlob(ns, data, version, signer)
require.NoError(t, err)
encoded, err := tx.MarshalBlobTx([]byte("pfb"), blob)
require.NoError(t, err)
// Prefix the normal tx so generated protobuf cannot classify it as a BlobTx.
normal = append([]byte{0xff}, normal...)
txs := []square.ClassifiedTx{square.NewClassifiedTx(normal), square.NewClassifiedTx(encoded)}
var systemBlob *share.Blob
if withFibre {
commitment := sha256.Sum256(data)
systemBlob, err = share.NewV2Blob(share.MustNewV0Namespace([]byte{2, 0}), 1, commitment[:], bytes.Repeat([]byte{2}, share.SignerSize))
require.NoError(t, err)
fibre, err := square.NewClassifiedFibreTx(&tx.FibreTx{Tx: []byte("fibre"), SystemBlob: systemBlob})
require.NoError(t, err)
txs = append(txs, fibre)
}
maxSize := 1 << (size % 5)
result, err := square.Construct(txs, maxSize, 1+int(threshold))
if maxSize == 16 {
// 256 shares always accommodate the bounded 32 KiB payload plus
// compact metadata, subtree alignment and a one-share system blob.
require.NoError(t, err)
}
if err != nil {
return // bounded squares may not have capacity
}
assertFuzzSquare(t, result, maxSize)
normalRange := share.GetShareRangeForNamespace(result, share.TxNamespace)
parsed, err := share.ParseTxs(result[normalRange.Start:normalRange.End])
require.NoError(t, err)
require.Equal(t, [][]byte{normal}, parsed)
blobRange, err := square.BlobShareRange(txs, 1, 0, maxSize, 1+int(threshold))
require.NoError(t, err)
require.GreaterOrEqual(t, blobRange.Start, 0)
require.LessOrEqual(t, blobRange.End, len(result))
blobs, err := share.ParseBlobs(result[blobRange.Start:blobRange.End])
require.NoError(t, err)
require.Len(t, blobs, 1)
require.Equal(t, data, blobs[0].Data())
require.Equal(t, signer, blobs[0].Signer())
require.True(t, ns.Equals(blobs[0].Namespace()))
wrapped, err := result.WrappedPFBs()
require.NoError(t, err)
require.Len(t, wrapped, 1)
index, ok := tx.UnmarshalIndexWrapper(wrapped[0])
require.True(t, ok)
require.Equal(t, []uint32{uint32(blobRange.Start)}, index.ShareIndexes)
require.Equal(t, []byte("pfb"), index.Tx)
if withFibre {
r := share.GetShareRangeForNamespace(result, systemBlob.Namespace())
blobs, err := share.ParseBlobs(result[r.Start:r.End])
require.NoError(t, err)
require.Len(t, blobs, 1)
require.Equal(t, systemBlob.Data(), blobs[0].Data())
require.Equal(t, systemBlob.Signer(), blobs[0].Signer())
fibreRange := share.GetShareRangeForNamespace(result, share.PayForFibreNamespace)
parsed, err := share.ParseTxs(result[fibreRange.Start:fibreRange.End])
require.NoError(t, err)
require.Equal(t, [][]byte{[]byte("fibre")}, parsed)
}
})
}
func assertFuzzSquare(t *testing.T, result square.Square, maxSize int) {
t.Helper()
size, err := result.Size()
require.NoError(t, err)
require.Positive(t, size)
require.LessOrEqual(t, size, maxSize)
require.Zero(t, size&(size-1))
require.Len(t, result, size*size)
for i := range result {
require.Len(t, result[i].ToBytes(), share.ShareSize)
if i > 0 {
require.True(t, result[i-1].Namespace().IsLessOrEqualThan(result[i].Namespace()))
}
}
}
// hasValidBlobNamespaces reports whether every blob in raw, if raw is a blob
// tx, has a namespace that callers of Construct would accept.
func hasValidBlobNamespaces(raw []byte) bool {
blobTx, isBlobTx, err := tx.UnmarshalBlobTx(raw)
if !isBlobTx || err != nil {
return true
}
for _, blob := range blobTx.Blobs {
if blob.Namespace().ValidateForBlob() != nil {
return false
}
}
return true
}