forked from celestiaorg/rsmt2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extendeddatacrossword_test.go
198 lines (179 loc) · 6.75 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
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) {
for codecName, codec := range codecs {
bufferSize := 64
ones := bytes.Repeat([]byte{1}, bufferSize)
twos := bytes.Repeat([]byte{2}, bufferSize)
threes := bytes.Repeat([]byte{3}, bufferSize)
fours := bytes.Repeat([]byte{4}, bufferSize)
original, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, codec, NewDefaultTree)
if err != nil {
panic(err)
}
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
var result *ExtendedDataSquare
result, err = RepairExtendedDataSquare(original.getRowRoots(), original.getColRoots(), flattened, codec, NewDefaultTree)
if err != nil {
t.Errorf("unexpected err while repairing data square: %v, codec: :%s", err, codecName)
} else {
assert.Equal(t, result.getCell(0, 0), ones)
assert.Equal(t, result.getCell(0, 1), twos)
assert.Equal(t, result.getCell(1, 0), threes)
assert.Equal(t, result.getCell(1, 1), fours)
}
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
_, err = RepairExtendedDataSquare(original.getRowRoots(), original.getColRoots(), flattened, codec, NewDefaultTree)
if err == nil {
t.Errorf("did not return an error on trying to repair an unrepairable square")
}
var corrupted ExtendedDataSquare
corrupted, err = original.deepCopy(codec)
if err != nil {
t.Fatalf("unexpected err while copying original data: %v, codec: :%s", err, codecName)
}
corruptChunk := bytes.Repeat([]byte{66}, bufferSize)
corrupted.setCell(0, 0, corruptChunk)
_, err = RepairExtendedDataSquare(original.getRowRoots(), original.getColRoots(), corrupted.flattened(), codec, NewDefaultTree)
if err == nil {
t.Errorf("did not return an error on trying to repair a square with bad roots")
}
corrupted, err = original.deepCopy(codec)
if err != nil {
t.Fatalf("unexpected err while copying original data: %v, codec: :%s", err, codecName)
}
corrupted.setCell(0, 0, corruptChunk)
_, err = RepairExtendedDataSquare(corrupted.getRowRoots(), corrupted.getColRoots(), corrupted.flattened(), codec, NewDefaultTree)
var byzRow *ErrByzantineRow
if !errors.As(err, &byzRow) {
t.Errorf("did not return a ErrByzantineRow for a bad row; got: %v", err)
}
// Construct the fraud proof
fraudProof := PseudoFraudProof{row, byzRow.RowNumber, byzRow.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, 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)
}
}
corrupted, err = original.deepCopy(codec)
if err != nil {
t.Fatalf("unexpected err while copying original data: %v, codec: :%s", err, codecName)
}
corrupted.setCell(0, 3, corruptChunk)
_, err = RepairExtendedDataSquare(corrupted.getRowRoots(), corrupted.getColRoots(), corrupted.flattened(), codec, NewDefaultTree)
if !errors.As(err, &byzRow) {
t.Errorf("did not return a ErrByzantineRow for a bad row; got %v", err)
}
corrupted, err = original.deepCopy(codec)
if err != nil {
t.Fatalf("unexpected err while copying original data: %v, codec: :%s", err, codecName)
}
corrupted.setCell(0, 0, corruptChunk)
flattened = corrupted.flattened()
flattened[1], flattened[2], flattened[3] = nil, nil, nil
_, err = RepairExtendedDataSquare(corrupted.getRowRoots(), corrupted.getColRoots(), flattened, codec, NewDefaultTree)
var byzCol *ErrByzantineCol
if !errors.As(err, &byzCol) {
t.Errorf("did not return a ErrByzantineCol for a bad column; got %v", err)
}
corrupted, err = original.deepCopy(codec)
if err != nil {
t.Fatalf("unexpected err while copying original data: %v, codec: :%s", err, codecName)
}
corrupted.setCell(3, 0, corruptChunk)
flattened = corrupted.flattened()
flattened[1], flattened[2], flattened[3] = nil, nil, nil
_, err = RepairExtendedDataSquare(corrupted.getRowRoots(), corrupted.getColRoots(), flattened, codec, NewDefaultTree)
if !errors.As(err, &byzCol) {
t.Errorf("did not return a ErrByzantineCol for a bad column; got %v", err)
}
}
}
func BenchmarkRepair(b *testing.B) {
// For different ODS sizes
for originalDataWidth := 16; originalDataWidth <= 128; originalDataWidth *= 2 {
for codecName, codec := range codecs {
// 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
b.Run(
fmt.Sprintf(
"Repairing %dx%d ODS using %s",
originalDataWidth,
originalDataWidth,
codecName,
),
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++
}
}
b.StartTimer()
_, err := RepairExtendedDataSquare(
eds.getRowRoots(),
eds.getColRoots(),
flattened,
codec,
NewDefaultTree,
)
if err != nil {
b.Error(err)
}
}
},
)
}
}
}