From 34bcd7b35b02672ec8e30eb46593928493ebe0c7 Mon Sep 17 00:00:00 2001 From: Vlad <13818348+walldiss@users.noreply.github.com> Date: Thu, 18 Apr 2024 23:42:22 +0400 Subject: [PATCH] refactor: make consistent names for row and col indexes (#317) This PR standardizes index naming for rows and columns across the codebase to `rowIdx` and `colIdx`. Resolves https://github.com/celestiaorg/rsmt2d/issues/314 --- datasquare.go | 92 +++++++++++++++++------------------ datasquare_test.go | 58 +++++++++++----------- extendeddatacrossword.go | 80 +++++++++++++++--------------- extendeddatacrossword_test.go | 12 ++--- extendeddatasquare.go | 38 +++++++-------- 5 files changed, 140 insertions(+), 140 deletions(-) diff --git a/datasquare.go b/datasquare.go index 5c94c86..5a472ec 100644 --- a/datasquare.go +++ b/datasquare.go @@ -44,21 +44,21 @@ func newDataSquare(data [][]byte, treeCreator TreeConstructorFn, shareSize uint) } squareRow := make([][][]byte, width) - for i := 0; i < width; i++ { - squareRow[i] = data[i*width : i*width+width] + for rowIdx := 0; rowIdx < width; rowIdx++ { + squareRow[rowIdx] = data[rowIdx*width : rowIdx*width+width] - for j := 0; j < width; j++ { - if squareRow[i][j] != nil && len(squareRow[i][j]) != int(shareSize) { + for colIdx := 0; colIdx < width; colIdx++ { + if squareRow[rowIdx][colIdx] != nil && len(squareRow[rowIdx][colIdx]) != int(shareSize) { return nil, ErrUnevenChunks } } } squareCol := make([][][]byte, width) - for j := 0; j < width; j++ { - squareCol[j] = make([][]byte, width) - for i := 0; i < width; i++ { - squareCol[j][i] = data[i*width+j] + for colIdx := 0; colIdx < width; colIdx++ { + squareCol[colIdx] = make([][]byte, width) + for rowIdx := 0; rowIdx < width; rowIdx++ { + squareCol[colIdx][rowIdx] = data[rowIdx*width+colIdx] } } @@ -106,10 +106,10 @@ func (ds *dataSquare) extendSquare(extendedWidth uint, fillerShare []byte) error ds.squareRow = newSquareRow newSquareCol := make([][][]byte, newWidth) - for j := uint(0); j < newWidth; j++ { - newSquareCol[j] = make([][]byte, newWidth) - for i := uint(0); i < newWidth; i++ { - newSquareCol[j][i] = newSquareRow[i][j] + for colIdx := uint(0); colIdx < newWidth; colIdx++ { + newSquareCol[colIdx] = make([][]byte, newWidth) + for rowIdx := uint(0); rowIdx < newWidth; rowIdx++ { + newSquareCol[colIdx][rowIdx] = newSquareRow[rowIdx][colIdx] } } ds.squareCol = newSquareCol @@ -120,33 +120,33 @@ func (ds *dataSquare) extendSquare(extendedWidth uint, fillerShare []byte) error return nil } -func (ds *dataSquare) rowSlice(x uint, y uint, length uint) [][]byte { - return ds.squareRow[x][y : y+length] +func (ds *dataSquare) rowSlice(rowIdx uint, fromIdx uint, length uint) [][]byte { + return ds.squareRow[rowIdx][fromIdx : fromIdx+length] } // row returns a row slice. // Do not modify this slice directly, instead use SetCell. -func (ds *dataSquare) row(x uint) [][]byte { - return ds.rowSlice(x, 0, ds.width) +func (ds *dataSquare) row(rowIdx uint) [][]byte { + return ds.rowSlice(rowIdx, 0, ds.width) } -func (ds *dataSquare) setRowSlice(x uint, y uint, newRow [][]byte) error { +func (ds *dataSquare) setRowSlice(rowIdx uint, fromIdx uint, newRow [][]byte) error { for i := uint(0); i < uint(len(newRow)); i++ { if len(newRow[i]) != int(ds.shareSize) { // TODO: export this error and rename chunk to share 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) + if fromIdx+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", rowIdx, fromIdx, len(newRow), ds.width) } ds.dataMutex.Lock() defer ds.dataMutex.Unlock() for i := uint(0); i < uint(len(newRow)); i++ { - ds.squareRow[x][y+i] = newRow[i] - ds.squareCol[y+i][x] = newRow[i] + ds.squareRow[rowIdx][fromIdx+i] = newRow[i] + ds.squareCol[fromIdx+i][rowIdx] = newRow[i] } ds.resetRoots() @@ -154,33 +154,33 @@ func (ds *dataSquare) setRowSlice(x uint, y uint, newRow [][]byte) error { return nil } -func (ds *dataSquare) colSlice(x uint, y uint, length uint) [][]byte { - return ds.squareCol[y][x : x+length] +func (ds *dataSquare) colSlice(rowIdx uint, colIdx uint, length uint) [][]byte { + return ds.squareCol[colIdx][rowIdx : rowIdx+length] } // col returns a column slice. // Do not modify this slice directly, instead use SetCell. -func (ds *dataSquare) col(y uint) [][]byte { - return ds.colSlice(0, y, ds.width) +func (ds *dataSquare) col(colIdx uint) [][]byte { + return ds.colSlice(0, colIdx, ds.width) } -func (ds *dataSquare) setColSlice(x uint, y uint, newCol [][]byte) error { +func (ds *dataSquare) setColSlice(colIdx uint, fromIdx uint, newCol [][]byte) error { for i := uint(0); i < uint(len(newCol)); i++ { if len(newCol[i]) != int(ds.shareSize) { // TODO: export this error and rename chunk to share 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) + if fromIdx+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", fromIdx, colIdx, len(newCol), ds.width) } ds.dataMutex.Lock() defer ds.dataMutex.Unlock() for i := uint(0); i < uint(len(newCol)); i++ { - ds.squareRow[x+i][y] = newCol[i] - ds.squareCol[y][x+i] = newCol[i] + ds.squareRow[fromIdx+i][colIdx] = newCol[i] + ds.squareCol[colIdx][fromIdx+i] = newCol[i] } ds.resetRoots() @@ -252,13 +252,13 @@ func (ds *dataSquare) getRowRoots() ([][]byte, error) { // getRowRoot calculates and returns the root of the selected row. Note: unlike // the getRowRoots method, getRowRoot does not write to the built-in cache. // Returns an error if the row is incomplete (i.e. some shares are nil). -func (ds *dataSquare) getRowRoot(x uint) ([]byte, error) { +func (ds *dataSquare) getRowRoot(rowIdx uint) ([]byte, error) { if ds.rowRoots != nil { - return ds.rowRoots[x], nil + return ds.rowRoots[rowIdx], nil } - tree := ds.createTreeFn(Row, x) - row := ds.row(x) + tree := ds.createTreeFn(Row, rowIdx) + row := ds.row(rowIdx) if !isComplete(row) { return nil, errors.New("can not compute root of incomplete row") } @@ -287,13 +287,13 @@ func (ds *dataSquare) getColRoots() ([][]byte, error) { // getColRoot calculates and returns the root of the selected row. Note: unlike // the getColRoots method, getColRoot does not write to the built-in cache. // Returns an error if the column is incomplete (i.e. some shares are nil). -func (ds *dataSquare) getColRoot(y uint) ([]byte, error) { +func (ds *dataSquare) getColRoot(colIdx uint) ([]byte, error) { if ds.colRoots != nil { - return ds.colRoots[y], nil + return ds.colRoots[colIdx], nil } - tree := ds.createTreeFn(Col, y) - col := ds.col(y) + tree := ds.createTreeFn(Col, colIdx) + col := ds.col(colIdx) if !isComplete(col) { return nil, errors.New("can not compute root of incomplete column") } @@ -308,27 +308,27 @@ func (ds *dataSquare) getColRoot(y uint) ([]byte, error) { } // GetCell returns a copy of a specific cell. -func (ds *dataSquare) GetCell(x uint, y uint) []byte { - if ds.squareRow[x][y] == nil { +func (ds *dataSquare) GetCell(rowIdx uint, colIdx uint) []byte { + if ds.squareRow[rowIdx][colIdx] == nil { return nil } cell := make([]byte, ds.shareSize) - copy(cell, ds.squareRow[x][y]) + copy(cell, ds.squareRow[rowIdx][colIdx]) return cell } // SetCell sets a specific cell. The cell to set must be `nil`. Returns an error // if the cell to set is not `nil` or newShare is not the correct size. -func (ds *dataSquare) SetCell(x uint, y uint, newShare []byte) error { - if ds.squareRow[x][y] != nil { - return fmt.Errorf("cannot set cell (%d, %d) as it already has a value %x", x, y, ds.squareRow[x][y]) +func (ds *dataSquare) SetCell(rowIdx uint, colIdx uint, newShare []byte) error { + if ds.squareRow[rowIdx][colIdx] != nil { + return fmt.Errorf("cannot set cell (%d, %d) as it already has a value %x", rowIdx, colIdx, ds.squareRow[rowIdx][colIdx]) } if len(newShare) != int(ds.shareSize) { // TODO: export this error and rename chunk to share return fmt.Errorf("cannot set cell with chunk size %d because dataSquare chunk size is %d", len(newShare), ds.shareSize) } - ds.squareRow[x][y] = newShare - ds.squareCol[y][x] = newShare + ds.squareRow[rowIdx][colIdx] = newShare + ds.squareCol[colIdx][rowIdx] = newShare ds.resetRoots() return nil } diff --git a/datasquare_test.go b/datasquare_test.go index 47170ae..63b2958 100644 --- a/datasquare_test.go +++ b/datasquare_test.go @@ -295,8 +295,8 @@ func Test_setRowSlice(t *testing.T) { type testCase struct { name string newRow [][]byte - x uint - y uint + rowIdx uint + colIdx uint want [][]byte wantErr bool } @@ -304,31 +304,31 @@ func Test_setRowSlice(t *testing.T) { { name: "overwrite the first row", newRow: [][]byte{{5}, {6}}, - x: 0, - y: 0, + rowIdx: 0, + colIdx: 0, want: [][]byte{{5}, {6}, {3}, {4}}, wantErr: false, }, { name: "overwrite the last row", newRow: [][]byte{{5}, {6}}, - x: 1, - y: 0, + rowIdx: 1, + colIdx: 0, want: [][]byte{{1}, {2}, {5}, {6}}, wantErr: false, }, { name: "returns an error if the new row has an invalid share size", newRow: [][]byte{{5, 6}}, - x: 0, - y: 0, + rowIdx: 0, + colIdx: 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, + rowIdx: 0, + colIdx: 1, wantErr: true, }, } @@ -336,7 +336,7 @@ func Test_setRowSlice(t *testing.T) { for _, tc := range testCases { ds, err := newDataSquare([][]byte{{1}, {2}, {3}, {4}}, NewDefaultTree, 1) assert.NoError(t, err) - err = ds.setRowSlice(tc.x, tc.y, tc.newRow) + err = ds.setRowSlice(tc.rowIdx, tc.colIdx, tc.newRow) if tc.wantErr { assert.Error(t, err) @@ -351,8 +351,8 @@ func Test_setColSlice(t *testing.T) { type testCase struct { name string newCol [][]byte - x uint - y uint + rowIdx uint + colIdx uint want [][]byte wantErr bool } @@ -360,31 +360,31 @@ func Test_setColSlice(t *testing.T) { { name: "overwrite the first col", newCol: [][]byte{{5}, {6}}, - x: 0, - y: 0, + rowIdx: 0, + colIdx: 0, want: [][]byte{{5}, {2}, {6}, {4}}, wantErr: false, }, { name: "overwrite the last col", newCol: [][]byte{{5}, {6}}, - x: 0, - y: 1, + rowIdx: 0, + colIdx: 1, want: [][]byte{{1}, {5}, {3}, {6}}, wantErr: false, }, { name: "returns an error if the new col has an invalid share size", newCol: [][]byte{{5, 6}}, - x: 0, - y: 0, + rowIdx: 0, + colIdx: 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, + rowIdx: 1, + colIdx: 0, wantErr: true, }, } @@ -392,7 +392,7 @@ func Test_setColSlice(t *testing.T) { for _, tc := range testCases { ds, err := newDataSquare([][]byte{{1}, {2}, {3}, {4}}, NewDefaultTree, 1) assert.NoError(t, err) - err = ds.setColSlice(tc.x, tc.y, tc.newCol) + err = ds.setColSlice(tc.colIdx, tc.rowIdx, tc.newCol) if tc.wantErr { assert.Error(t, err) @@ -471,9 +471,9 @@ func BenchmarkEDSRootsWithErasuredNMT(b *testing.B) { } } -func computeRowProof(ds *dataSquare, x uint, y uint) ([]byte, [][]byte, uint, uint, error) { - tree := ds.createTreeFn(Row, x) - data := ds.row(x) +func computeRowProof(ds *dataSquare, rowIdx uint, colIdx uint) ([]byte, [][]byte, uint, uint, error) { + tree := ds.createTreeFn(Row, rowIdx) + data := ds.row(rowIdx) for i := uint(0); i < ds.width; i++ { err := tree.Push(data[i]) @@ -482,7 +482,7 @@ func computeRowProof(ds *dataSquare, x uint, y uint) ([]byte, [][]byte, uint, ui } } - merkleRoot, proof, proofIndex, numLeaves := treeProve(tree.(*DefaultTree), int(y)) + merkleRoot, proof, proofIndex, numLeaves := treeProve(tree.(*DefaultTree), int(colIdx)) return merkleRoot, proof, uint(proofIndex), uint(numLeaves), nil } @@ -521,8 +521,8 @@ func (d *errorTree) Root() ([]byte, error) { // setCell overwrites the contents of a specific cell. setCell does not perform // any input validation so most use cases should use `SetCell` instead of // `setCell`. This method exists strictly for testing. -func (ds *dataSquare) setCell(x uint, y uint, newShare []byte) { - ds.squareRow[x][y] = newShare - ds.squareCol[y][x] = newShare +func (ds *dataSquare) setCell(rowIdx uint, colIdx uint, newShare []byte) { + ds.squareRow[rowIdx][colIdx] = newShare + ds.squareCol[colIdx][rowIdx] = newShare ds.resetRoots() } diff --git a/extendeddatacrossword.go b/extendeddatacrossword.go index 7ff301c..7114321 100644 --- a/extendeddatacrossword.go +++ b/extendeddatacrossword.go @@ -127,18 +127,18 @@ func (eds *ExtendedDataSquare) solveCrossword( // - if the row was previously unsolved and now solved // - an error if the repair is unsuccessful func (eds *ExtendedDataSquare) solveCrosswordRow( - r int, + rowIdx int, rowRoots [][]byte, colRoots [][]byte, ) (bool, bool, error) { - isComplete := noMissingData(eds.row(uint(r)), noShareInsertion) + isComplete := noMissingData(eds.row(uint(rowIdx)), noShareInsertion) if isComplete { return true, false, nil } // Prepare shares shares := make([][]byte, eds.width) - copy(shares, eds.row(uint(r))) + copy(shares, eds.row(uint(rowIdx))) // Attempt rebuild the row rebuiltShares, isDecoded, err := eds.rebuildShares(shares) @@ -150,7 +150,7 @@ func (eds *ExtendedDataSquare) solveCrosswordRow( } // Check that rebuilt shares matches appropriate root - err = eds.verifyAgainstRowRoots(rowRoots, uint(r), rebuiltShares, noShareInsertion, nil) + err = eds.verifyAgainstRowRoots(rowRoots, uint(rowIdx), rebuiltShares, noShareInsertion, nil) if err != nil { var byzErr *ErrByzantineData if errors.As(err, &byzErr) { @@ -160,13 +160,13 @@ func (eds *ExtendedDataSquare) solveCrosswordRow( } // Check that newly completed orthogonal vectors match their new merkle roots - for c := 0; c < int(eds.width); c++ { - col := eds.col(uint(c)) - if col[r] != nil { + for colIdx := 0; colIdx < int(eds.width); colIdx++ { + col := eds.col(uint(colIdx)) + if col[rowIdx] != nil { continue // not newly completed } - if noMissingData(col, r) { // completed - err := eds.verifyAgainstColRoots(colRoots, uint(c), col, r, rebuiltShares[c]) + if noMissingData(col, rowIdx) { // completed + err := eds.verifyAgainstColRoots(colRoots, uint(colIdx), col, rowIdx, rebuiltShares[colIdx]) if err != nil { var byzErr *ErrByzantineData if errors.As(err, &byzErr) { @@ -175,17 +175,17 @@ func (eds *ExtendedDataSquare) solveCrosswordRow( return false, false, err } - if eds.verifyEncoding(col, r, rebuiltShares[c]) != nil { - return false, false, &ErrByzantineData{Col, uint(c), col} + if eds.verifyEncoding(col, rowIdx, rebuiltShares[colIdx]) != nil { + return false, false, &ErrByzantineData{Col, uint(colIdx), col} } } } // Insert rebuilt shares into square. - for c, s := range rebuiltShares { - cellToSet := eds.GetCell(uint(r), uint(c)) + for colIdx, s := range rebuiltShares { + cellToSet := eds.GetCell(uint(rowIdx), uint(colIdx)) if cellToSet == nil { - err := eds.SetCell(uint(r), uint(c), s) + err := eds.SetCell(uint(rowIdx), uint(colIdx), s) if err != nil { return false, false, err } @@ -201,18 +201,18 @@ func (eds *ExtendedDataSquare) solveCrosswordRow( // - if the column was previously unsolved and now solved // - an error if the repair is unsuccessful func (eds *ExtendedDataSquare) solveCrosswordCol( - c int, + colIdx int, rowRoots [][]byte, colRoots [][]byte, ) (bool, bool, error) { - isComplete := noMissingData(eds.col(uint(c)), noShareInsertion) + isComplete := noMissingData(eds.col(uint(colIdx)), noShareInsertion) if isComplete { return true, false, nil } // Prepare shares shares := make([][]byte, eds.width) - copy(shares, eds.col(uint(c))) + copy(shares, eds.col(uint(colIdx))) // Attempt rebuild rebuiltShares, isDecoded, err := eds.rebuildShares(shares) @@ -224,7 +224,7 @@ func (eds *ExtendedDataSquare) solveCrosswordCol( } // Check that rebuilt shares matches appropriate root - err = eds.verifyAgainstColRoots(colRoots, uint(c), rebuiltShares, noShareInsertion, nil) + err = eds.verifyAgainstColRoots(colRoots, uint(colIdx), rebuiltShares, noShareInsertion, nil) if err != nil { var byzErr *ErrByzantineData if errors.As(err, &byzErr) { @@ -234,13 +234,13 @@ func (eds *ExtendedDataSquare) solveCrosswordCol( } // Check that newly completed orthogonal vectors match their new merkle roots - for r := 0; r < int(eds.width); r++ { - row := eds.row(uint(r)) - if row[c] != nil { + for rowIdx := 0; rowIdx < int(eds.width); rowIdx++ { + row := eds.row(uint(rowIdx)) + if row[colIdx] != nil { continue // not newly completed } - if noMissingData(row, c) { // completed - err := eds.verifyAgainstRowRoots(rowRoots, uint(r), row, c, rebuiltShares[r]) + if noMissingData(row, colIdx) { // completed + err := eds.verifyAgainstRowRoots(rowRoots, uint(rowIdx), row, colIdx, rebuiltShares[rowIdx]) if err != nil { var byzErr *ErrByzantineData if errors.As(err, &byzErr) { @@ -249,17 +249,17 @@ func (eds *ExtendedDataSquare) solveCrosswordCol( return false, false, err } - if eds.verifyEncoding(row, c, rebuiltShares[r]) != nil { - return false, false, &ErrByzantineData{Row, uint(r), row} + if eds.verifyEncoding(row, colIdx, rebuiltShares[rowIdx]) != nil { + return false, false, &ErrByzantineData{Row, uint(rowIdx), row} } } } // Insert rebuilt shares into square. - for r, s := range rebuiltShares { - cellToSet := eds.GetCell(uint(r), uint(c)) + for rowIdx, s := range rebuiltShares { + cellToSet := eds.GetCell(uint(rowIdx), uint(colIdx)) if cellToSet == nil { - err := eds.SetCell(uint(r), uint(c), s) + err := eds.SetCell(uint(rowIdx), uint(colIdx), s) if err != nil { return false, false, err } @@ -289,7 +289,7 @@ func (eds *ExtendedDataSquare) rebuildShares( func (eds *ExtendedDataSquare) verifyAgainstRowRoots( rowRoots [][]byte, - r uint, + rowIdx uint, oldShares [][]byte, rebuiltIndex int, rebuiltShare []byte, @@ -297,19 +297,19 @@ func (eds *ExtendedDataSquare) verifyAgainstRowRoots( var root []byte var err error if rebuiltIndex < 0 || rebuiltShare == nil { - root, err = eds.computeSharesRoot(oldShares, Row, r) + root, err = eds.computeSharesRoot(oldShares, Row, rowIdx) } else { - root, err = eds.computeSharesRootWithRebuiltShare(oldShares, Row, r, rebuiltIndex, rebuiltShare) + root, err = eds.computeSharesRootWithRebuiltShare(oldShares, Row, rowIdx, rebuiltIndex, rebuiltShare) } if err != nil { // any error during the computation of the root is considered byzantine // the shares are set to nil, as the caller will populate them - return &ErrByzantineData{Row, r, nil} + return &ErrByzantineData{Row, rowIdx, nil} } - if !bytes.Equal(root, rowRoots[r]) { + if !bytes.Equal(root, rowRoots[rowIdx]) { // the shares are set to nil, as the caller will populate them - return &ErrByzantineData{Row, r, nil} + return &ErrByzantineData{Row, rowIdx, nil} } return nil @@ -323,7 +323,7 @@ func (eds *ExtendedDataSquare) verifyAgainstRowRoots( // Returns a ErrByzantineData error if the computed root does not match the expected root or if the root computation fails. func (eds *ExtendedDataSquare) verifyAgainstColRoots( colRoots [][]byte, - c uint, + colIdx uint, shares [][]byte, rebuiltIndex int, rebuiltShare []byte, @@ -331,18 +331,18 @@ func (eds *ExtendedDataSquare) verifyAgainstColRoots( var root []byte var err error if rebuiltIndex < 0 || rebuiltShare == nil { - root, err = eds.computeSharesRoot(shares, Col, c) + root, err = eds.computeSharesRoot(shares, Col, colIdx) } else { - root, err = eds.computeSharesRootWithRebuiltShare(shares, Col, c, rebuiltIndex, rebuiltShare) + root, err = eds.computeSharesRootWithRebuiltShare(shares, Col, colIdx, rebuiltIndex, rebuiltShare) } if err != nil { // the shares are set to nil, as the caller will populate them - return &ErrByzantineData{Col, c, nil} + return &ErrByzantineData{Col, colIdx, nil} } - if !bytes.Equal(root, colRoots[c]) { + if !bytes.Equal(root, colRoots[colIdx]) { // the shares are set to nil, as the caller will populate them - return &ErrByzantineData{Col, c, nil} + return &ErrByzantineData{Col, colIdx, nil} } return nil diff --git a/extendeddatacrossword_test.go b/extendeddatacrossword_test.go index cb4ed67..4ad7be3 100644 --- a/extendeddatacrossword_test.go +++ b/extendeddatacrossword_test.go @@ -210,9 +210,9 @@ func TestCorruptedEdsReturnsErrByzantineData(t *testing.T) { assert.NoError(t, err) for i, coords := range test.coords { - x := coords[0] - y := coords[1] - eds.setCell(x, y, test.values[i]) + rowIdx := coords[0] + colIdx := coords[1] + eds.setCell(rowIdx, colIdx, test.values[i]) } err = eds.Repair(rowRoots, colRoots) @@ -406,9 +406,9 @@ func TestCorruptedEdsReturnsErrByzantineData_UnorderedShares(t *testing.T) { assert.NotNil(t, corruptEds) // corrupt it by setting the values at the given coordinates for i, coords := range test.coords { - x := coords[0] - y := coords[1] - corruptEds.setCell(x, y, test.values[i]) + rowIdx := coords[0] + colIdx := coords[1] + corruptEds.setCell(rowIdx, colIdx, test.values[i]) } err = corruptEds.Repair(dAHeaderRoots, dAHeaderCols) diff --git a/extendeddatasquare.go b/extendeddatasquare.go index 4691ba7..99f40d5 100644 --- a/extendeddatasquare.go +++ b/extendeddatasquare.go @@ -211,20 +211,20 @@ func (eds *ExtendedDataSquare) erasureExtendSquare(codec Codec) error { return errs.Wait() } -func (eds *ExtendedDataSquare) erasureExtendRow(codec Codec, i uint) error { - parityShares, err := codec.Encode(eds.rowSlice(i, 0, eds.originalDataWidth)) +func (eds *ExtendedDataSquare) erasureExtendRow(codec Codec, rowIdx uint) error { + parityShares, err := codec.Encode(eds.rowSlice(rowIdx, 0, eds.originalDataWidth)) if err != nil { return err } - return eds.setRowSlice(i, eds.originalDataWidth, parityShares) + return eds.setRowSlice(rowIdx, eds.originalDataWidth, parityShares) } -func (eds *ExtendedDataSquare) erasureExtendCol(codec Codec, i uint) error { - parityShares, err := codec.Encode(eds.colSlice(0, i, eds.originalDataWidth)) +func (eds *ExtendedDataSquare) erasureExtendCol(codec Codec, colIdx uint) error { + parityShares, err := codec.Encode(eds.colSlice(0, colIdx, eds.originalDataWidth)) if err != nil { return err } - return eds.setColSlice(eds.originalDataWidth, i, parityShares) + return eds.setColSlice(colIdx, eds.originalDataWidth, parityShares) } func (eds *ExtendedDataSquare) deepCopy(codec Codec) (ExtendedDataSquare, error) { @@ -234,8 +234,8 @@ func (eds *ExtendedDataSquare) deepCopy(codec Codec) (ExtendedDataSquare, error) // Col returns a column slice. // This slice is a copy of the internal column slice. -func (eds *ExtendedDataSquare) Col(y uint) [][]byte { - return deepCopy(eds.col(y)) +func (eds *ExtendedDataSquare) Col(colIdx uint) [][]byte { + return deepCopy(eds.col(colIdx)) } // ColRoots returns the Merkle roots of all the columns in the square. Returns @@ -250,8 +250,8 @@ func (eds *ExtendedDataSquare) ColRoots() ([][]byte, error) { // Row returns a row slice. // This slice is a copy of the internal row slice. -func (eds *ExtendedDataSquare) Row(x uint) [][]byte { - return deepCopy(eds.row(x)) +func (eds *ExtendedDataSquare) Row(rowIdx uint) [][]byte { + return deepCopy(eds.row(rowIdx)) } // RowRoots returns the Merkle roots of all the rows in the square. Returns an @@ -289,10 +289,10 @@ func (eds *ExtendedDataSquare) Flattened() [][]byte { // 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] + for rowIdx := uint(0); rowIdx < eds.originalDataWidth; rowIdx++ { + row := eds.Row(rowIdx) + for colIdx := uint(0); colIdx < eds.originalDataWidth; colIdx++ { + flattened[(rowIdx*eds.originalDataWidth)+colIdx] = row[colIdx] } } return flattened @@ -313,12 +313,12 @@ func (eds *ExtendedDataSquare) Equals(other *ExtendedDataSquare) bool { return false } - for rowIndex := uint(0); rowIndex < eds.Width(); rowIndex++ { - edsRow := eds.Row(rowIndex) - otherRow := other.Row(rowIndex) + for rowIdx := uint(0); rowIdx < eds.Width(); rowIdx++ { + edsRow := eds.Row(rowIdx) + otherRow := other.Row(rowIdx) - for colIndex := 0; colIndex < len(edsRow); colIndex++ { - if !bytes.Equal(edsRow[colIndex], otherRow[colIndex]) { + for colIdx := 0; colIdx < len(edsRow); colIdx++ { + if !bytes.Equal(edsRow[colIdx], otherRow[colIdx]) { return false } }