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

fix: eds mutation #134

Merged
merged 6 commits into from
Jan 22, 2023
Merged
Changes from 1 commit
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
63 changes: 45 additions & 18 deletions extendeddatacrossword.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (eds *ExtendedDataSquare) solveCrosswordRow(
rowRoots [][]byte,
colRoots [][]byte,
) (bool, bool, error) {
isComplete := noMissingData(eds.row(uint(r)))
isComplete := noMissingData(eds.row(uint(r)), -1)
liamsi marked this conversation as resolved.
Show resolved Hide resolved
if isComplete {
return true, false, nil
}
Expand All @@ -140,7 +140,7 @@ func (eds *ExtendedDataSquare) solveCrosswordRow(
}

// Check that rebuilt shares matches appropriate root
err = eds.verifyAgainstRowRoots(rowRoots, uint(r), rebuiltShares)
err = eds.verifyAgainstRowRoots(rowRoots, uint(r), rebuiltShares, -1, nil)
if err != nil {
var byzErr *ErrByzantineData
if errors.As(err, &byzErr) {
Expand All @@ -155,9 +155,8 @@ func (eds *ExtendedDataSquare) solveCrosswordRow(
if col[r] != nil {
continue // not newly completed
}
col[r] = rebuiltShares[c]
if noMissingData(col) { // not completed
err := eds.verifyAgainstColRoots(colRoots, uint(c), col)
if noMissingData(col, r) { // not completed
err := eds.verifyAgainstColRoots(colRoots, uint(c), col, r, rebuiltShares[c])
if err != nil {
return false, false, err
}
Expand All @@ -182,7 +181,7 @@ func (eds *ExtendedDataSquare) solveCrosswordCol(
rowRoots [][]byte,
colRoots [][]byte,
) (bool, bool, error) {
isComplete := noMissingData(eds.col(uint(c)))
isComplete := noMissingData(eds.col(uint(c)), -1)
if isComplete {
return true, false, nil
}
Expand All @@ -206,7 +205,7 @@ func (eds *ExtendedDataSquare) solveCrosswordCol(
}

// Check that rebuilt shares matches appropriate root
err = eds.verifyAgainstColRoots(colRoots, uint(c), rebuiltShares)
err = eds.verifyAgainstColRoots(colRoots, uint(c), rebuiltShares, -1, nil)
if err != nil {
var byzErr *ErrByzantineData
if errors.As(err, &byzErr) {
Expand All @@ -221,9 +220,8 @@ func (eds *ExtendedDataSquare) solveCrosswordCol(
if row[c] != nil {
continue // not newly completed
}
row[c] = rebuiltShares[r]
if noMissingData(row) { // not completed
err := eds.verifyAgainstRowRoots(rowRoots, uint(r), row)
if noMissingData(row, c) { // not completed
err := eds.verifyAgainstRowRoots(rowRoots, uint(r), row, c, rebuiltShares[r])
if err != nil {
return false, false, err
}
Expand Down Expand Up @@ -274,9 +272,16 @@ func (eds *ExtendedDataSquare) rebuildShares(
func (eds *ExtendedDataSquare) verifyAgainstRowRoots(
rowRoots [][]byte,
r uint,
shares [][]byte,
oldShares [][]byte,
c int,
rebuiltShare []byte,
) error {
root := eds.computeSharesRoot(shares, Row, r)
var root []byte
if c < 0 || rebuiltShare == nil {
root = eds.computeSharesRoot(oldShares, Row, r)
} else {
root = eds.computeSharesRootWithRebuiltShare(oldShares, Row, r, c, rebuiltShare)
}

if !bytes.Equal(root, rowRoots[r]) {
return &ErrByzantineData{Row, r, nil}
Expand All @@ -288,9 +293,16 @@ func (eds *ExtendedDataSquare) verifyAgainstRowRoots(
func (eds *ExtendedDataSquare) verifyAgainstColRoots(
colRoots [][]byte,
c uint,
shares [][]byte,
oldShares [][]byte,
r int,
rebuiltShare []byte,
) error {
root := eds.computeSharesRoot(shares, Col, c)
var root []byte
if r < 0 || rebuiltShare == nil {
root = eds.computeSharesRoot(oldShares, Col, c)
} else {
root = eds.computeSharesRootWithRebuiltShare(oldShares, Col, c, r, rebuiltShare)
}

if !bytes.Equal(root, colRoots[c]) {
return &ErrByzantineData{Col, c, nil}
Expand All @@ -307,8 +319,8 @@ func (eds *ExtendedDataSquare) prerepairSanityCheck(

for i := uint(0); i < eds.width; i++ {
i := i
rowIsComplete := noMissingData(eds.row(i))
colIsComplete := noMissingData(eds.col(i))
rowIsComplete := noMissingData(eds.row(i), -1)
colIsComplete := noMissingData(eds.col(i), -1)

// if there's no missing data in the this row
if rowIsComplete {
Expand Down Expand Up @@ -362,8 +374,11 @@ func (eds *ExtendedDataSquare) prerepairSanityCheck(
return errs.Wait()
}

func noMissingData(input [][]byte) bool {
for _, d := range input {
func noMissingData(input [][]byte, rebuiltIndex int) bool {
for index, d := range input {
if index == rebuiltIndex {
continue
}
if d == nil {
return false
}
Expand All @@ -379,6 +394,18 @@ func (eds *ExtendedDataSquare) computeSharesRoot(shares [][]byte, axis Axis, i u
return tree.Root()
}

func (eds *ExtendedDataSquare) computeSharesRootWithRebuiltShare(shares [][]byte, axis Axis, i uint, rebuiltIndex int, rebuiltShare []byte) []byte {
tree := eds.createTreeFn(axis, i)
for _, d := range shares[:rebuiltIndex] {
tree.Push(d)
}
tree.Push(rebuiltShare)
for _, d := range shares[rebuiltIndex+1:] {
tree.Push(d)
}
return tree.Root()
}

func (eds *ExtendedDataSquare) rowRangeNoMissingData(r, start, end uint) bool {
for c := start; c <= end && c < eds.width; c++ {
if eds.squareRow[r][c] == nil {
Expand Down