forked from celestiaorg/rsmt2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextendeddatacrossword_test.go
292 lines (257 loc) · 8.78 KB
/
extendeddatacrossword_test.go
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package rsmt2d
import (
"bytes"
"errors"
"fmt"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
)
// PseudoFraudProof is an example fraud proof.
// TODO a real fraud proof would have a Merkle proof for each share.
type PseudoFraudProof struct {
Mode int // Row (0) or column (1)
Index uint // Row or column index
Shares [][]byte // Bad shares (nil are missing)
}
func TestRepairExtendedDataSquare(t *testing.T) {
bufferSize := 64
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
codec Codec
}{
{"leopard", bufferSize, NewLeoRSCodec()},
{"infectiousGF8", bufferSize, NewRSGF8Codec()},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
name, codec, shareSize := test.name, test.codec, test.shareSize
original := createTestEds(codec, shareSize)
rowRoots := original.RowRoots()
colRoots := original.ColRoots()
// Verify that an EDS can be repaired after the maximum amount of erasures
t.Run("MaximumErasures", func(t *testing.T) {
flattened := original.Flattened()
flattened[0], flattened[2], flattened[3] = nil, nil, nil
flattened[4], flattened[5], flattened[6], flattened[7] = nil, nil, nil, nil
flattened[8], flattened[9], flattened[10] = nil, nil, nil
flattened[12], flattened[13] = nil, nil
// Re-import the data square.
eds, err := ImportExtendedDataSquare(flattened, codec, NewDefaultTree)
if err != nil {
t.Errorf("ImportExtendedDataSquare failed: %v", err)
}
err = eds.Repair(rowRoots, colRoots)
if err != nil {
t.Errorf("unexpected err while repairing data square: %v, codec: :%s", err, name)
} else {
assert.Equal(t, original.GetCell(0, 0), bytes.Repeat([]byte{1}, shareSize))
assert.Equal(t, original.GetCell(0, 1), bytes.Repeat([]byte{2}, shareSize))
assert.Equal(t, original.GetCell(1, 0), bytes.Repeat([]byte{3}, shareSize))
assert.Equal(t, original.GetCell(1, 1), bytes.Repeat([]byte{4}, shareSize))
}
})
// Verify that an EDS returns an error when there are too many erasures
t.Run("Unrepairable", func(t *testing.T) {
flattened := original.Flattened()
flattened[0], flattened[2], flattened[3] = nil, nil, nil
flattened[4], flattened[5], flattened[6], flattened[7] = nil, nil, nil, nil
flattened[8], flattened[9], flattened[10] = nil, nil, nil
flattened[12], flattened[13], flattened[14] = nil, nil, nil
// Re-import the data square.
eds, err := ImportExtendedDataSquare(flattened, codec, NewDefaultTree)
if err != nil {
t.Errorf("ImportExtendedDataSquare failed: %v", err)
}
err = eds.Repair(rowRoots, colRoots)
if err != ErrUnrepairableDataSquare {
t.Errorf("did not return an error on trying to repair an unrepairable square")
}
})
})
}
}
func TestValidFraudProof(t *testing.T) {
bufferSize := 64
corruptChunk := bytes.Repeat([]byte{66}, bufferSize)
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
codec Codec
}{
{"leopard", bufferSize, NewLeoRSCodec()},
{"infectiousGF8", bufferSize, NewRSGF8Codec()},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
name, codec, shareSize := test.name, test.codec, test.shareSize
original := createTestEds(codec, shareSize)
var byzData *ErrByzantineData
corrupted, err := original.deepCopy(codec)
if err != nil {
t.Fatalf("unexpected err while copying original data: %v, codec: :%s", err, name)
}
corrupted.setCell(0, 0, corruptChunk)
err = corrupted.Repair(corrupted.getRowRoots(), corrupted.getColRoots())
errors.As(err, &byzData)
// Construct the fraud proof
fraudProof := PseudoFraudProof{0, byzData.Index, byzData.Shares}
// Verify the fraud proof
// TODO in a real fraud proof, also verify Merkle proof for each non-nil share.
rebuiltShares, err := codec.Decode(fraudProof.Shares)
if err != nil {
t.Errorf("could not decode fraud proof shares; got: %v", err)
}
root := corrupted.computeSharesRoot(rebuiltShares, byzData.Axis, fraudProof.Index)
if bytes.Equal(root, corrupted.getRowRoot(fraudProof.Index)) {
// If the roots match, then the fraud proof should be for invalid erasure coding.
parityShares, err := codec.Encode(rebuiltShares[0:corrupted.originalDataWidth])
if err != nil {
t.Errorf("could not encode fraud proof shares; %v", fraudProof)
}
startIndex := len(rebuiltShares) - int(corrupted.originalDataWidth)
if bytes.Equal(flattenChunks(parityShares), flattenChunks(rebuiltShares[startIndex:])) {
t.Errorf("invalid fraud proof %v", fraudProof)
}
}
})
}
}
func TestCannotRepairSquareWithBadRoots(t *testing.T) {
bufferSize := 64
corruptChunk := bytes.Repeat([]byte{66}, bufferSize)
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
codec Codec
}{
{"leopard", bufferSize, NewLeoRSCodec()},
{"infectiousGF8", bufferSize, NewRSGF8Codec()},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
codec, shareSize := test.codec, test.shareSize
original := createTestEds(codec, shareSize)
rowRoots := original.RowRoots()
colRoots := original.ColRoots()
original.setCell(0, 0, corruptChunk)
err := original.Repair(rowRoots, colRoots)
if err == nil {
t.Errorf("did not return an error on trying to repair a square with bad roots")
}
})
}
}
func TestCorruptedEdsReturnsErrByzantineData(t *testing.T) {
bufferSize := 64
corruptChunk := bytes.Repeat([]byte{66}, bufferSize)
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
cells [][]byte
values [][]byte
axis Axis
}{
{"BadRow/OriginalData", bufferSize, [][]byte{{0, 0}}, [][]byte{corruptChunk}, Row},
{"BadRow/ExtendedData", bufferSize, [][]byte{{0, 3}}, [][]byte{corruptChunk}, Row},
{"BadColumn/OriginalData", bufferSize, [][]byte{{0, 0}, {0, 1}, {0, 2}, {0, 3}}, [][]byte{corruptChunk, nil, nil, nil}, Col},
{"BadColumn/OriginalData", bufferSize, [][]byte{{3, 0}, {0, 1}, {0, 2}, {0, 3}}, [][]byte{corruptChunk, nil, nil, nil}, Col},
}
for codecName, codec := range codecs {
t.Run(codecName, func(t *testing.T) {
original := createTestEds(codec, bufferSize)
var byzData *ErrByzantineData
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
corrupted, err := original.deepCopy(codec)
if err != nil {
t.Fatalf("unexpected err while copying original data: %v, codec: :%s", err, codecName)
}
for i := 0; i < len(test.cells); i++ {
corrupted.setCell(uint(test.cells[i][0]), uint(test.cells[i][1]), test.values[i])
}
err = corrupted.Repair(corrupted.getRowRoots(), corrupted.getColRoots())
if !errors.As(err, &byzData) {
// due to parallelisation, the ErrByzantineData axis may be either row or col
t.Errorf("did not return a ErrByzantineData for a bad col or row; got %v", err)
}
})
}
})
}
}
func BenchmarkRepair(b *testing.B) {
// For different ODS sizes
for originalDataWidth := 4; originalDataWidth <= 512; originalDataWidth *= 2 {
for codecName, codec := range codecs {
if codec.maxChunks() < originalDataWidth*originalDataWidth {
// Only test codecs that support this many chunks
continue
}
// Generate a new range original data square then extend it
square := genRandDS(originalDataWidth)
eds, err := ComputeExtendedDataSquare(square, codec, NewDefaultTree)
if err != nil {
b.Error(err)
}
extendedDataWidth := originalDataWidth * 2
rowRoots := eds.RowRoots()
colRoots := eds.ColRoots()
b.Run(
fmt.Sprintf(
"%s %dx%dx%d ODS",
codecName,
originalDataWidth,
originalDataWidth,
len(square[0]),
),
func(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
flattened := eds.Flattened()
// Randomly remove 1/2 of the shares of each row
for r := 0; r < extendedDataWidth; r++ {
for c := 0; c < originalDataWidth; {
ind := rand.Intn(extendedDataWidth)
if flattened[r*extendedDataWidth+ind] == nil {
continue
}
flattened[r*extendedDataWidth+ind] = nil
c++
}
}
// Re-import the data square.
eds, _ = ImportExtendedDataSquare(flattened, codec, NewDefaultTree)
b.StartTimer()
err := eds.Repair(
rowRoots,
colRoots,
)
if err != nil {
b.Error(err)
}
}
},
)
}
}
}
func createTestEds(codec Codec, bufferSize int) *ExtendedDataSquare {
ones := bytes.Repeat([]byte{1}, bufferSize)
twos := bytes.Repeat([]byte{2}, bufferSize)
threes := bytes.Repeat([]byte{3}, bufferSize)
fours := bytes.Repeat([]byte{4}, bufferSize)
eds, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, codec, NewDefaultTree)
if err != nil {
panic(err)
}
return eds
}