Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: check width in setRowSlice and setColSlice #174

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions datasquare.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@
return errors.New("invalid chunk size")
}
}
if y+uint(len(newRow)) > ds.width {
return fmt.Errorf("cannot set row slice at (%d, %d) of length %d: because it would exceed the data square width %d", x, y, len(newRow), ds.width)

Check warning on line 137 in datasquare.go

View check run for this annotation

Codecov / codecov/patch

datasquare.go#L137

Added line #L137 was not covered by tests
}

ds.dataMutex.Lock()
defer ds.dataMutex.Unlock()
Expand Down Expand Up @@ -163,6 +166,9 @@
return errors.New("invalid chunk size")
}
}
if x+uint(len(newCol)) > ds.width {
return fmt.Errorf("cannot set col slice at (%d, %d) of length %d: because it would exceed the data square width %d", x, y, len(newCol), ds.width)

Check warning on line 170 in datasquare.go

View check run for this annotation

Codecov / codecov/patch

datasquare.go#L170

Added line #L170 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the codecov still complains? As far as I can say, your tests cover this line 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk, it look like the test does cover this

Screenshot 2023-06-23 at 2 31 37 PM

}

ds.dataMutex.Lock()
defer ds.dataMutex.Unlock()
Expand Down
114 changes: 114 additions & 0 deletions datasquare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,120 @@ func TestDefaultTreeProofs(t *testing.T) {
}
}

func Test_setRowSlice(t *testing.T) {
type testCase struct {
name string
newRow [][]byte
x uint
y uint
want [][]byte
wantErr bool
}
testCases := []testCase{
{
name: "overwrite the first row",
newRow: [][]byte{{5}, {6}},
x: 0,
y: 0,
want: [][]byte{{5}, {6}, {3}, {4}},
wantErr: false,
},
{
name: "overwrite the last row",
newRow: [][]byte{{5}, {6}},
x: 1,
y: 0,
want: [][]byte{{1}, {2}, {5}, {6}},
wantErr: false,
},
{
name: "returns an error if the new row has an invalid chunk size",
newRow: [][]byte{{5, 6}},
x: 0,
y: 0,
wantErr: true,
},
{
name: "returns an error if the new row would surpass the data square's width",
newRow: [][]byte{{5}, {6}},
x: 0,
y: 1,
wantErr: true,
},
}

for _, tc := range testCases {
ds, err := newDataSquare([][]byte{{1}, {2}, {3}, {4}}, NewDefaultTree)
assert.NoError(t, err)
err = ds.setRowSlice(tc.x, tc.y, tc.newRow)

if tc.wantErr {
assert.Error(t, err)
return
} else {
assert.NoError(t, err)
assert.Equal(t, tc.want, ds.Flattened())
}
}
}

func Test_setColSlice(t *testing.T) {
type testCase struct {
name string
newCol [][]byte
x uint
y uint
want [][]byte
wantErr bool
}
testCases := []testCase{
{
name: "overwrite the first col",
newCol: [][]byte{{5}, {6}},
x: 0,
y: 0,
want: [][]byte{{5}, {2}, {6}, {4}},
wantErr: false,
},
{
name: "overwrite the last col",
newCol: [][]byte{{5}, {6}},
x: 0,
y: 1,
want: [][]byte{{1}, {5}, {3}, {6}},
wantErr: false,
},
{
name: "returns an error if the new col has an invalid chunk size",
newCol: [][]byte{{5, 6}},
x: 0,
y: 0,
wantErr: true,
},
{
name: "returns an error if the new col would surpass the data square's width",
newCol: [][]byte{{5}, {6}},
x: 1,
y: 0,
wantErr: true,
},
}

for _, tc := range testCases {
ds, err := newDataSquare([][]byte{{1}, {2}, {3}, {4}}, NewDefaultTree)
assert.NoError(t, err)
err = ds.setColSlice(tc.x, tc.y, tc.newCol)

if tc.wantErr {
assert.Error(t, err)
return
} else {
assert.NoError(t, err)
assert.Equal(t, tc.want, ds.Flattened())
}
}
}

func BenchmarkEDSRoots(b *testing.B) {
for i := 32; i < 513; i *= 2 {
square, err := newDataSquare(genRandDS(i*2), NewDefaultTree)
Expand Down
Loading