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

feat: add roots helper function #270

Merged
merged 3 commits into from
Oct 31, 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
4 changes: 2 additions & 2 deletions datasquare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ func TestInvalidSquareExtension(t *testing.T) {
}
}

// TestRoots verifies that the row roots and column roots are equal for a 1x1
// Test_getRoots verifies that the row roots and column roots are equal for a 1x1
// square.
func TestRoots(t *testing.T) {
func Test_getRoots(t *testing.T) {
result, err := newDataSquare([][]byte{{1, 2}}, NewDefaultTree, 2)
assert.NoError(t, err)

Expand Down
18 changes: 18 additions & 0 deletions extendeddatasquare.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,24 @@
return true
}

// Roots returns a byte slice with this eds's RowRoots and ColRoots
// concatenated.
func (eds *ExtendedDataSquare) Roots() (roots [][]byte, err error) {
rowRoots, err := eds.RowRoots()
if err != nil {
return nil, err
}
colRoots, err := eds.ColRoots()
if err != nil {
return nil, err
}

Check warning on line 335 in extendeddatasquare.go

View check run for this annotation

Codecov / codecov/patch

extendeddatasquare.go#L334-L335

Added lines #L334 - L335 were not covered by tests

roots = make([][]byte, 0, len(rowRoots)+len(colRoots))
roots = append(roots, rowRoots...)
roots = append(roots, colRoots...)
return roots, nil
}

// validateEdsWidth returns an error if edsWidth is not a valid width for an
// extended data square.
func validateEdsWidth(edsWidth uint) error {
Expand Down
43 changes: 43 additions & 0 deletions extendeddatasquare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,49 @@ func TestEquals(t *testing.T) {
})
}

func TestRoots(t *testing.T) {
t.Run("returns roots for a 4x4 EDS", func(t *testing.T) {
eds, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, NewLeoRSCodec(), NewDefaultTree)
require.NoError(t, err)

roots, err := eds.Roots()
require.NoError(t, err)
assert.Len(t, roots, 8)
rootulp marked this conversation as resolved.
Show resolved Hide resolved

rowRoots, err := eds.RowRoots()
require.NoError(t, err)

colRoots, err := eds.ColRoots()
require.NoError(t, err)

assert.Equal(t, roots[0], rowRoots[0])
assert.Equal(t, roots[1], rowRoots[1])
assert.Equal(t, roots[2], rowRoots[2])
assert.Equal(t, roots[3], rowRoots[3])
assert.Equal(t, roots[4], colRoots[0])
assert.Equal(t, roots[5], colRoots[1])
assert.Equal(t, roots[6], colRoots[2])
assert.Equal(t, roots[7], colRoots[3])
})

t.Run("returns an error for an incomplete EDS", func(t *testing.T) {
eds, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, NewLeoRSCodec(), NewDefaultTree)
require.NoError(t, err)

// set a cell to nil to make the EDS incomplete
eds.setCell(0, 0, nil)

_, err = eds.Roots()
assert.Error(t, err)
})
}

func createExampleEds(t *testing.T, chunkSize int) (eds *ExtendedDataSquare) {
ones := bytes.Repeat([]byte{1}, chunkSize)
twos := bytes.Repeat([]byte{2}, chunkSize)
Expand Down
Loading