Skip to content

Commit

Permalink
collect namespaced data from cached proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Nov 30, 2023
1 parent 196cd3a commit d8425e3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
29 changes: 28 additions & 1 deletion share/eds/cache_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (f *CacheFile) axisWithProofs(idx int, axis rsmt2d.Axis) (inMemoryAxis, err
return inMemoryAxis{}, err
}

fmt.Println("building proofs for axis", idx, axis)
// calculate proofs
adder := ipld.NewProofsAdder(f.Size(), ipld.CollectShares)
tree := wrapper.NewErasuredNamespacedMerkleTree(uint64(f.Size()/2), uint(idx),
Expand Down Expand Up @@ -183,6 +182,34 @@ func (f *CacheFile) EDS() (*rsmt2d.ExtendedDataSquare, error) {
return eds, nil
}

func (f *CacheFile) SharesByNamespace(
ctx context.Context,
root *share.Root,
namespace share.Namespace,
) (share.NamespacedShares, error) {
// collect all shares with proofs within the namespace
var shares []share.NamespacedRow
for i, row := range root.RowRoots {
if !namespace.IsOutsideRange(row, row) {
ax, err := f.axisWithProofs(i, rsmt2d.Row)
if err != nil {
return nil, err
}

rowCid := ipld.MustCidFromNamespacedSha256(row)
row, proof, err := ipld.GetSharesByNamespace(ctx, ax.proofs, rowCid, namespace, f.Size())
if err != nil {
return nil, fmt.Errorf("retrieving shares by namespace %s for row %x: %w", namespace.String(), row, err)
}
shares = append(shares, share.NamespacedRow{
Shares: row,
Proof: proof,
})
}
}
return shares, nil
}

// rowProofsGetter implements blockservice.BlockGetter interface
type rowProofsGetter struct {
proofs map[cid.Cid]blocks.Block
Expand Down
39 changes: 39 additions & 0 deletions share/eds/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import (
"context"
"crypto/sha256"
"fmt"
mrand "math/rand"
"runtime"
"strconv"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

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

func TestCreateFile(t *testing.T) {
Expand Down Expand Up @@ -162,6 +167,40 @@ func TestCachedEDS(t *testing.T) {
require.Equal(t, r1, r2)
}

func TestGetNamespacedData(t *testing.T) {
size := 32

// generate random shares
shares := sharetest.RandShares(t, size*size)
rand := mrand.New(mrand.NewSource(time.Now().UnixNano()))

// choose random range in shares slice and set namespace to be the same for all shares in range
from := rand.Intn(size * size)
to := rand.Intn(size * size)
if to < from {
from, to = to, from
}
expected := shares[from]
namespace := share.GetNamespace(expected)

// change namespace for all shares in range
for i := from; i <= to; i++ {
shares[i] = expected
}

eds, err := rsmt2d.ComputeExtendedDataSquare(shares, share.DefaultRSMT2DCodec(), wrapper.NewConstructor(uint64(size)))
require.NoError(t, err)
dah, err := da.NewDataAvailabilityHeader(eds)
require.NoError(t, err)

file := NewCacheFile(&MemFile{Eds: eds}, rsmt2d.NewLeoRSCodec())

nd, err := file.SharesByNamespace(context.Background(), &dah, namespace)
require.NoError(t, err)
err = nd.Verify(&dah, namespace)
require.NoError(t, err)
}

// BenchmarkGetShareFromCacheMiss/16 16308 72295 ns/op
// BenchmarkGetShareFromCacheMiss/32 8216 141334 ns/op
// BenchmarkGetShareFromCacheMiss/64 3877 284171 ns/op
Expand Down

0 comments on commit d8425e3

Please sign in to comment.