Skip to content

Commit

Permalink
chore: apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Jul 6, 2023
1 parent b50abc3 commit d8ce446
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
28 changes: 16 additions & 12 deletions share/eds/byzantine/bad_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package byzantine

import (
"bytes"
"errors"
"fmt"

"github.com/celestiaorg/celestia-app/pkg/wrapper"
Expand Down Expand Up @@ -113,10 +112,10 @@ func (p *BadEncodingProof) UnmarshalBinary(data []byte) error {
func (p *BadEncodingProof) Validate(hdr libhead.Header) error {
header, ok := hdr.(*header.ExtendedHeader)
if !ok {
panic(fmt.Sprintf("invalid header type during the befp validation: expected %T, got %T", header, hdr))
panic(fmt.Sprintf("invalid header type received during BEFP validation: expected %T, got %T", header, hdr))
}
if header.Height() != int64(p.BlockHeight) {
return errors.New("incorrect block height during the befp validation")
return fmt.Errorf("incorrect block height during BEFP validation: expected %d, got %d", p.BlockHeight, header.Height())
}

if len(header.DAH.RowRoots) != len(header.DAH.ColumnRoots) {
Expand All @@ -129,35 +128,40 @@ func (p *BadEncodingProof) Validate(hdr libhead.Header) error {
)
}

// change the order of dah roots as we are now collecting nmt proofs against orthogonal axis
roots := [][][]byte{header.DAH.ColumnRoots, header.DAH.RowRoots}
if int(p.Index) >= len(roots[rsmt2d.Row]) {
// merkleRoots are the roots against which we are going to check the inclusion of the received shares.
// changing the order of the roots to prove the shares relative to the orthogonal axis, because
// inside the rsmt2d library rsmt2d.Row = 0 and rsmt2d.Col = 1
merkleRoots := header.DAH.RowRoots
if p.Axis == rsmt2d.Row {
merkleRoots = header.DAH.ColumnRoots
}
if int(p.Index) >= len(merkleRoots) {
return fmt.Errorf("invalid %s proof: index out of bounds (%d >= %d)",
BadEncoding, int(p.Index), len(roots[rsmt2d.Row]),
BadEncoding, int(p.Index), len(merkleRoots),
)
}
if len(p.Shares) != len(roots[rsmt2d.Row]) {
if len(p.Shares) != len(merkleRoots) {
return fmt.Errorf("invalid %s proof: incorrect number of shares %d != %d",
BadEncoding, len(p.Shares), len(roots[rsmt2d.Row]),
BadEncoding, len(p.Shares), len(merkleRoots),
)
}

// verify that Merkle proofs correspond to particular shares.
shares := make([][]byte, len(roots[rsmt2d.Row]))
shares := make([][]byte, len(merkleRoots))
for index, shr := range p.Shares {
if shr == nil {
continue
}
// validate inclusion of the share into one of the DAHeader roots
if ok := shr.Validate(ipld.MustCidFromNamespacedSha256(roots[p.Axis][index])); !ok {
if ok := shr.Validate(ipld.MustCidFromNamespacedSha256(merkleRoots[index])); !ok {
return fmt.Errorf("invalid %s proof: incorrect share received at index %d", BadEncoding, index)
}
// NMTree commits the additional namespace while rsmt2d does not know about, so we trim it
// this is ugliness from NMTWrapper that we have to embrace ¯\_(ツ)_/¯
shares[index] = share.GetData(shr.Share)
}

odsWidth := uint64(len(roots[rsmt2d.Row]) / 2)
odsWidth := uint64(len(merkleRoots) / 2)
codec := share.DefaultRSMT2DCodec()

// rebuild a row or col.
Expand Down
23 changes: 11 additions & 12 deletions share/eds/byzantine/byzantine.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@ func NewErrByzantine(

sharesWithProof := make([]*ShareWithProof, len(errByz.Shares))
sharesAmount := 0
errGr, ctx := errgroup.WithContext(ctx)

errGr, ctx := errgroup.WithContext(ctx)
for index, share := range errByz.Shares {
if share == nil {
continue
}
// skip further shares if we already requested half of them, which is enough to recompute the row
// or col
if sharesAmount == len(dah.RowRoots)/2 {
break
}

if share == nil {
continue
}
sharesAmount++

index := index
Expand All @@ -70,14 +71,12 @@ func NewErrByzantine(
}

if err := errGr.Wait(); err != nil {
if err != nil {
// Fatal as rsmt2d proved that error is byzantine,
// but we cannot properly collect the proof,
// so verification will fail and thus services won't be stopped
// while we still have to stop them.
// TODO(@Wondertan): Find a better way to handle
log.Fatalw("getting proof for ErrByzantine", "err", err)
}
// Fatal as rsmt2d proved that error is byzantine,
// but we cannot properly collect the proof,
// so verification will fail and thus services won't be stopped
// while we still have to stop them.
// TODO(@Wondertan): Find a better way to handle
log.Fatalw("getting proof for ErrByzantine", "err", err)
}
return &ErrByzantine{
Index: uint32(errByz.Index),
Expand Down

0 comments on commit d8ce446

Please sign in to comment.