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

feat(share): Ensure empty CAR exists in eds.Store #1409

Merged
merged 7 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions nodebuilder/share/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package share

import (
"context"
"errors"

"github.com/filecoin-project/dagstore"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/routing"
routingdisc "github.com/libp2p/go-libp2p/p2p/discovery/routing"
"go.uber.org/fx"

"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/availability/cache"
disc "github.com/celestiaorg/celestia-node/share/availability/discovery"
"github.com/celestiaorg/celestia-node/share/eds"
"github.com/celestiaorg/celestia-node/share/service"
)

Expand Down Expand Up @@ -52,3 +56,15 @@ func newModule(lc fx.Lifecycle, bServ blockservice.BlockService, avail share.Ava
})
return &module{serv}
}

// ensureEmptyCARExists adds an empty EDS to the provided EDS store.
func ensureEmptyCARExists(ctx context.Context, store *eds.Store) error {
emptyEDS := share.EmptyExtendedDataSquare()
emptyDAH := da.NewDataAvailabilityHeader(emptyEDS)

err := store.Put(ctx, emptyDAH.Hash(), emptyEDS)
if errors.Is(err, dagstore.ErrShardExists) {
return nil
}
return err
}
15 changes: 10 additions & 5 deletions nodebuilder/share/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func ConstructModule(tp node.Type, cfg *Config, options ...fx.Option) fx.Option
fx.Supply(*cfg),
fx.Error(cfgErr),
fx.Options(options...),
fx.Invoke(share.EnsureEmptySquareExists),
fx.Provide(discovery(*cfg)),
fx.Provide(newModule),
fx.Invoke(share.EnsureEmptySquareExists),
)

switch tp {
Expand Down Expand Up @@ -52,11 +52,16 @@ func ConstructModule(tp node.Type, cfg *Config, options ...fx.Option) fx.Option
func(path node.StorePath, ds datastore.Batching) (*eds.Store, error) {
return eds.NewStore(string(path), ds)
},
fx.OnStart(func(ctx context.Context, eds *eds.Store) error {
return eds.Start(ctx)
fx.OnStart(func(ctx context.Context, store *eds.Store) error {
err := store.Start(ctx)
if err != nil {
return err
}

return ensureEmptyCARExists(ctx, store)
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
}),
fx.OnStop(func(ctx context.Context, eds *eds.Store) error {
return eds.Stop(ctx)
fx.OnStop(func(ctx context.Context, store *eds.Store) error {
return store.Stop(ctx)
}),
)),
fx.Provide(fx.Annotate(
Expand Down
44 changes: 44 additions & 0 deletions nodebuilder/share/share_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package share

import (
"context"
"testing"

"github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/eds"
)

func Test_EmptyCARExists(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

tmpDir := t.TempDir()
ds := ds_sync.MutexWrap(datastore.NewMapDatastore())
edsStore, err := eds.NewStore(tmpDir, ds)
require.NoError(t, err)
err = edsStore.Start(ctx)
require.NoError(t, err)

eds := share.EmptyExtendedDataSquare()
dah := da.NewDataAvailabilityHeader(eds)

// add empty EDS to store
err = ensureEmptyCARExists(ctx, edsStore)
assert.NoError(t, err)

// assert that the empty car exists
has, err := edsStore.Has(ctx, dah.Hash())
assert.True(t, has)
assert.NoError(t, err)

// assert that the empty car is, in fact, empty
emptyEds, err := edsStore.Get(ctx, dah.Hash())
assert.Equal(t, eds.Flattened(), emptyEds.Flattened())
assert.NoError(t, err)
}
33 changes: 26 additions & 7 deletions share/empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,33 @@ package share
import (
"bytes"
"context"
"fmt"
"math"

"github.com/ipfs/go-blockservice"

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

// EnsureEmptySquareExists checks if the given DAG contains an empty block data square.
// If it does not, it stores an empty block. This optimization exists to prevent
// redundant storing of empty block data so that it is only stored once and returned
// upon request for a block with an empty data square. Ref: header/constructors.go#L56
func EnsureEmptySquareExists(ctx context.Context, bServ blockservice.BlockService) error {
shares := make([][]byte, appconsts.MinShareCount)
for i := 0; i < appconsts.MinShareCount; i++ {
shares[i] = tailPaddingShare
}
func EnsureEmptySquareExists(ctx context.Context, bServ blockservice.BlockService) (*rsmt2d.ExtendedDataSquare, error) {
return AddShares(ctx, emptyDataSquare(), bServ)
}

_, err := AddShares(ctx, shares, bServ)
return err
// EmptyExtendedDataSquare returns the EDS of the empty block data square.
func EmptyExtendedDataSquare() *rsmt2d.ExtendedDataSquare {
shares := emptyDataSquare()
squareSize := uint64(math.Sqrt(float64(appconsts.MinSquareSize)))
eds, err := da.ExtendShares(squareSize, shares)
if err != nil {
panic(fmt.Errorf("failed to create empty EDS: %w", err))
}
distractedm1nd marked this conversation as resolved.
Show resolved Hide resolved
return eds
}

// tail is filler for all tail padded shares
Expand All @@ -29,3 +38,13 @@ var tailPaddingShare = append(
append(make([]byte, 0, appconsts.ShareSize), appconsts.TailPaddingNamespaceID...),
bytes.Repeat([]byte{0}, appconsts.ShareSize-appconsts.NamespaceSize)...,
)

// emptyDataSquare returns the minimum size data square filled with tail padding.
func emptyDataSquare() [][]byte {
shares := make([][]byte, appconsts.MinShareCount)
for i := 0; i < appconsts.MinShareCount; i++ {
shares[i] = tailPaddingShare
}

return shares
}