Skip to content

Commit

Permalink
extract edsFromRows and write a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Jul 25, 2024
1 parent 5045ac6 commit 9f9377b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
58 changes: 40 additions & 18 deletions share/shwap/p2p/bitswap/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"context"
"fmt"

"github.com/celestiaorg/celestia-app/pkg/wrapper"
"github.com/celestiaorg/rsmt2d"
"github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/exchange"

"github.com/celestiaorg/celestia-app/pkg/wrapper"
"github.com/celestiaorg/rsmt2d"

"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/pruner"
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/shwap"
)

// Getter implements share.Getter.
Expand Down Expand Up @@ -139,25 +141,14 @@ func (g *Getter) GetEDS(
return nil, err
}

shrs := make([]share.Share, 0, sqrLn/2*sqrLn/2)
for i, row := range blks {
rowShrs, err := row.(*RowBlock).Container.Shares()
if err != nil {
return nil, fmt.Errorf("decoding Shares out of Row: %w", err)
}

for j, shr := range rowShrs {
shrs[i*j] = shr
}
rows := make([]shwap.Row, len(blks))
for i, blk := range blks {
rows[i] = blk.(*RowBlock).Container
}

square, err := rsmt2d.ImportExtendedDataSquare(
shrs,
share.DefaultRSMT2DCodec(),
wrapper.NewConstructor(uint64(sqrLn/2)),
)
square, err := edsFromRows(hdr.DAH, rows)
if err != nil {
return nil, fmt.Errorf("computing EDS: %w", err)
return nil, err
}

return square, nil
Expand Down Expand Up @@ -212,3 +203,34 @@ func (g *Getter) session(hdr *header.ExtendedHeader) exchange.Fetcher {

return g.archivalSession
}

// edsFromRows imports given Rows and computes EDS out of them, assuming enough were Rows provided.
func edsFromRows(roots *share.Root, rows []shwap.Row) (*rsmt2d.ExtendedDataSquare, error) {

Check failure on line 208 in share/shwap/p2p/bitswap/getter.go

View workflow job for this annotation

GitHub Actions / go-ci / Lint

undefined: share.Root
shrs := make([]share.Share, len(roots.RowRoots)*len(roots.RowRoots))
for i, row := range rows {
rowShrs, err := row.Shares()
if err != nil {
return nil, fmt.Errorf("decoding Shares out of Row: %w", err)
}

for j, shr := range rowShrs {
shrs[j+(i*len(roots.RowRoots))] = shr
}
}

square, err := rsmt2d.ImportExtendedDataSquare(
shrs,
share.DefaultRSMT2DCodec(),
wrapper.NewConstructor(uint64(len(roots.RowRoots)/2)),
)
if err != nil {
return nil, fmt.Errorf("importing EDS: %w", err)
}

err = square.Repair(roots.RowRoots, roots.ColumnRoots)
if err != nil {
return nil, fmt.Errorf("repairing EDS: %w", err)
}

return square, nil
}
27 changes: 27 additions & 0 deletions share/shwap/p2p/bitswap/getter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package bitswap

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/eds/edstest"
"github.com/celestiaorg/celestia-node/share/shwap"
)

func TestEDSFromRows(t *testing.T) {
edsIn := edstest.RandEDS(t, 8)
roots, err := share.NewRoot(edsIn)

Check failure on line 15 in share/shwap/p2p/bitswap/getter_test.go

View workflow job for this annotation

GitHub Actions / go-ci / Lint

undefined: share.NewRoot (typecheck)
require.NoError(t, err)

rows := make([]shwap.Row, edsIn.Width()/2)
for i := range edsIn.Width() / 2 {
rowShrs := edsIn.Row(i)[:edsIn.Width()/2]
rows[i] = shwap.NewRow(rowShrs, shwap.Left)
}

edsOut, err := edsFromRows(roots, rows)
require.NoError(t, err)
require.True(t, edsIn.Equals(edsOut))
}

0 comments on commit 9f9377b

Please sign in to comment.