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(eds): Flattened, FlattenedODS, Equals #241

Merged
merged 10 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
27 changes: 27 additions & 0 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,30 @@ func generateMissingData(count int, codec Codec) [][]byte {

return output
}

// testCodec is a codec that is used for testing purposes.
type testCodec struct{}

func newTestCodec() Codec {
return &testCodec{}
}

func (c *testCodec) Encode(chunk [][]byte) ([][]byte, error) {
return chunk, nil
}

func (c *testCodec) Decode(chunk [][]byte) ([][]byte, error) {
return chunk, nil
}

func (c *testCodec) MaxChunks() int {
return 0
}

func (c *testCodec) Name() string {
return "testCodec"
}

func (c *testCodec) ValidateChunkSize(_ int) error {
return nil
}
46 changes: 46 additions & 0 deletions extendeddatasquare.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
}
err = codec.ValidateChunkSize(int(chunkSize))
if err != nil {
return nil, err

Check warning on line 119 in extendeddatasquare.go

View check run for this annotation

Codecov / codecov/patch

extendeddatasquare.go#L119

Added line #L119 was not covered by tests
}

data := make([][]byte, edsWidth*edsWidth)
Expand Down Expand Up @@ -274,6 +274,52 @@
return eds.width
}

// FlattenedEDS returns the extended data square as a flattened slice of bytes.
rootulp marked this conversation as resolved.
Show resolved Hide resolved
func (eds *ExtendedDataSquare) FlattenedEDS() [][]byte {
return eds.Flattened()
}
rootulp marked this conversation as resolved.
Show resolved Hide resolved

// FlattenedODS returns the original data square as a flattened slice of bytes.
func (eds *ExtendedDataSquare) FlattenedODS() (flattened [][]byte) {
flattened = make([][]byte, eds.originalDataWidth*eds.originalDataWidth)
for i := uint(0); i < eds.originalDataWidth; i++ {
row := eds.Row(i)
for j := uint(0); j < eds.originalDataWidth; j++ {
flattened[(i*eds.originalDataWidth)+j] = row[j]
}
}
return flattened
}

// Equals returns true if other is equal to eds.
func (eds *ExtendedDataSquare) Equals(other *ExtendedDataSquare) bool {
cmwaters marked this conversation as resolved.
Show resolved Hide resolved
if eds.originalDataWidth != other.originalDataWidth {
return false
}
if eds.codec.Name() != other.codec.Name() {
return false
}
if eds.chunkSize != other.chunkSize {
return false
}
if eds.width != other.width {
rootulp marked this conversation as resolved.
Show resolved Hide resolved
return false

Check warning on line 306 in extendeddatasquare.go

View check run for this annotation

Codecov / codecov/patch

extendeddatasquare.go#L306

Added line #L306 was not covered by tests
}
Comment on lines +296 to +307
Copy link
Member

Choose a reason for hiding this comment

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

I would combine in one multi line if, but thats a nit/preference.


for rowIndex := uint(0); rowIndex < eds.Width(); rowIndex++ {
edsRow := eds.Row(rowIndex)
otherRow := other.Row(rowIndex)

for colIndex := 0; colIndex < len(edsRow); colIndex++ {
if !bytes.Equal(edsRow[colIndex], otherRow[colIndex]) {
return false

Check warning on line 315 in extendeddatasquare.go

View check run for this annotation

Codecov / codecov/patch

extendeddatasquare.go#L315

Added line #L315 was not covered by tests
}
}
}

return true
}

// validateEdsWidth returns an error if edsWidth is not a valid width for an
// extended data square.
func validateEdsWidth(edsWidth uint) error {
Expand Down
78 changes: 77 additions & 1 deletion extendeddatasquare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestComputeExtendedDataSquare(t *testing.T) {

func TestImportExtendedDataSquare(t *testing.T) {
t.Run("is able to import an EDS", func(t *testing.T) {
eds := createExampleEds(t, ShardSize)
eds := createExampleEds(t, shareSize)
got, err := ImportExtendedDataSquare(eds.Flattened(), NewLeoRSCodec(), NewDefaultTree)
assert.NoError(t, err)
assert.Equal(t, eds.Flattened(), got.Flattened())
Expand Down Expand Up @@ -293,6 +293,82 @@ func genRandDS(width int, chunkSize int) [][]byte {
return ds
}

func TestFlattenedEDS(t *testing.T) {
example := createExampleEds(t, shareSize)
want := [][]byte{
ones, twos, zeros, threes,
threes, fours, eights, fifteens,
twos, elevens, thirteens, fours,
zeros, thirteens, fives, eights,
}

got := example.FlattenedEDS()
assert.Equal(t, want, got)
}

func TestFlattenedODS(t *testing.T) {
example := createExampleEds(t, shareSize)
want := [][]byte{
ones, twos,
threes, fours,
}

got := example.FlattenedODS()
assert.Equal(t, want, got)
}

func TestEquals(t *testing.T) {
t.Run("returns true for two equal EDS", func(t *testing.T) {
a := createExampleEds(t, shareSize)
b := createExampleEds(t, shareSize)
assert.True(t, a.Equals(b))
})
t.Run("returns false for two unequal EDS", func(t *testing.T) {
a := createExampleEds(t, shareSize)

type testCase struct {
name string
other *ExtendedDataSquare
}

unequalOriginalDataWidth := createExampleEds(t, shareSize)
unequalOriginalDataWidth.originalDataWidth = 1

unequalCodecs := createExampleEds(t, shareSize)
unequalCodecs.codec = newTestCodec()

unequalChunkSize := createExampleEds(t, shareSize*2)

unequalEds, err := ComputeExtendedDataSquare([][]byte{ones}, NewLeoRSCodec(), NewDefaultTree)
require.NoError(t, err)

testCases := []testCase{
{
name: "unequal original data width",
other: unequalOriginalDataWidth,
},
{
name: "unequal codecs",
other: unequalCodecs,
},
{
name: "unequal chunkSize",
other: unequalChunkSize,
},
{
name: "unequalEds",
other: unequalEds,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.False(t, a.Equals(tc.other))
assert.False(t, reflect.DeepEqual(a, tc.other))
})
}
})
}

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