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

refactor(shwap): Extract eds interface #3452

Merged
merged 11 commits into from
Jun 5, 2024
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package file
package eds

import (
"github.com/celestiaorg/celestia-node/share"
Expand Down
5 changes: 3 additions & 2 deletions share/store/file/eds_file.go → share/new_eds/eds.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package file
package eds

import (
"context"
Expand All @@ -10,7 +10,8 @@ import (
"github.com/celestiaorg/celestia-node/share/shwap"
)

type EdsFile interface {
// EDS is an interface for accessing extended data square data.
type EDS interface {
io.Closer
// Size returns square size of the file.
Size() int
Expand Down
33 changes: 17 additions & 16 deletions share/store/file/mem_file.go → share/new_eds/in_mem.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package file
package eds

import (
"context"
Expand All @@ -12,29 +12,30 @@ import (
"github.com/celestiaorg/celestia-node/share/shwap"
)

var _ EdsFile = (*MemFile)(nil)
var _ EDS = InMem{}

type MemFile struct {
Eds *rsmt2d.ExtendedDataSquare
// InMem is an in-memory implementation of EDS.
type InMem struct {
*rsmt2d.ExtendedDataSquare
}

func (f *MemFile) Close() error {
func (eds InMem) Close() error {
return nil
}

func (f *MemFile) Size() int {
return int(f.Eds.Width())
func (eds InMem) Size() int {
return int(eds.Width())
}

func (f *MemFile) Share(
func (eds InMem) Share(
_ context.Context,
rowIdx, colIdx int,
) (*shwap.Sample, error) {
axisType := rsmt2d.Row
axisIdx, shrIdx := rowIdx, colIdx

shares := getAxis(f.Eds, axisType, axisIdx)
tree := wrapper.NewErasuredNamespacedMerkleTree(uint64(f.Size()/2), uint(axisIdx))
shares := getAxis(eds.ExtendedDataSquare, axisType, axisIdx)
tree := wrapper.NewErasuredNamespacedMerkleTree(uint64(eds.Size()/2), uint(axisIdx))
for _, shr := range shares {
err := tree.Push(shr)
if err != nil {
Expand All @@ -54,20 +55,20 @@ func (f *MemFile) Share(
}, nil
}

func (f *MemFile) AxisHalf(_ context.Context, axisType rsmt2d.Axis, axisIdx int) (AxisHalf, error) {
func (eds InMem) AxisHalf(_ context.Context, axisType rsmt2d.Axis, axisIdx int) (AxisHalf, error) {
return AxisHalf{
Shares: getAxis(f.Eds, axisType, axisIdx)[:f.Size()/2],
Shares: getAxis(eds.ExtendedDataSquare, axisType, axisIdx)[:eds.Size()/2],
IsParity: false,
}, nil
}

func (f *MemFile) Data(_ context.Context, namespace share.Namespace, rowIdx int) (shwap.RowNamespaceData, error) {
shares := getAxis(f.Eds, rsmt2d.Row, rowIdx)
func (eds InMem) Data(_ context.Context, namespace share.Namespace, rowIdx int) (shwap.RowNamespaceData, error) {
shares := getAxis(eds.ExtendedDataSquare, rsmt2d.Row, rowIdx)
return ndDataFromShares(shares, namespace, rowIdx)
}

func (f *MemFile) EDS(_ context.Context) (*rsmt2d.ExtendedDataSquare, error) {
return f.Eds, nil
func (eds InMem) EDS(_ context.Context) (*rsmt2d.ExtendedDataSquare, error) {
return eds.ExtendedDataSquare, nil
}

func getAxis(eds *rsmt2d.ExtendedDataSquare, axisType rsmt2d.Axis, axisIdx int) []share.Share {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package file
package eds

import (
"context"
Expand All @@ -16,7 +16,7 @@ func TestMemFileShare(t *testing.T) {
eds := edstest.RandEDS(t, 32)
root, err := share.NewRoot(eds)
require.NoError(t, err)
fl := &MemFile{Eds: eds}
fl := &InMem{ExtendedDataSquare: eds}

width := int(eds.Width())
for rowIdx := 0; rowIdx < width; rowIdx++ {
Expand All @@ -33,12 +33,12 @@ func TestMemFileShare(t *testing.T) {
func TestMemFileDate(t *testing.T) {
size := 32

// generate EDS with random data and some shares with the same namespace
// generate InMem with random data and some shares with the same namespace
namespace := sharetest.RandV0Namespace()
amount := mrand.Intn(size*size-1) + 1
eds, dah := edstest.RandEDSWithNamespace(t, namespace, amount, size)

file := &MemFile{Eds: eds}
file := &InMem{ExtendedDataSquare: eds}

for i, root := range dah.RowRoots {
if !namespace.IsOutsideRange(root, root) {
Expand Down
Loading