From c77029b2c4a29e21ec92b45fd2664e9b7e16ebab Mon Sep 17 00:00:00 2001 From: rene <41963722+renaynay@users.noreply.github.com> Date: Wed, 10 Apr 2024 14:01:27 +0200 Subject: [PATCH] chore(core): dedup storeEDS --- core/eds.go | 25 +++++++++++++++++++++++-- core/exchange.go | 28 ++-------------------------- core/listener.go | 12 +++--------- 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/core/eds.go b/core/eds.go index eb93c249ba..c5f9ed6075 100644 --- a/core/eds.go +++ b/core/eds.go @@ -16,8 +16,11 @@ import ( "github.com/celestiaorg/nmt" "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/eds" + "github.com/celestiaorg/celestia-node/share/ipld" ) // extendBlock extends the given block data, returning the resulting @@ -51,14 +54,32 @@ func extendShares(s [][]byte, options ...nmt.Option) (*rsmt2d.ExtendedDataSquare } // storeEDS will only store extended block if it is not empty and doesn't already exist. -func storeEDS(ctx context.Context, hash share.DataHash, eds *rsmt2d.ExtendedDataSquare, store *eds.Store) error { +func storeEDS( + ctx context.Context, + eh *header.ExtendedHeader, + eds *rsmt2d.ExtendedDataSquare, + adder *ipld.ProofsAdder, + store *eds.Store, + window pruner.AvailabilityWindow, +) error { if eds == nil { return nil } - err := store.Put(ctx, hash, eds) + + if !pruner.IsWithinAvailabilityWindow(eh.Time(), window) { + log.Debugw("skipping storage of historic block", "height", eh.Height()) + return nil + } + + ctx = ipld.CtxWithProofsAdder(ctx, adder) + + err := store.Put(ctx, share.DataHash(eh.DataHash), eds) if errors.Is(err, dagstore.ErrShardExists) { // block with given root already exists, return nil return nil } + if err == nil { + log.Debugw("stored EDS for height", "height", eh.Height()) + } return err } diff --git a/core/exchange.go b/core/exchange.go index 9d31cf50df..61dd701bb4 100644 --- a/core/exchange.go +++ b/core/exchange.go @@ -10,7 +10,6 @@ import ( libhead "github.com/celestiaorg/go-header" "github.com/celestiaorg/nmt" - "github.com/celestiaorg/rsmt2d" "github.com/celestiaorg/celestia-node/header" "github.com/celestiaorg/celestia-node/pruner" @@ -155,7 +154,7 @@ func (ce *Exchange) Get(ctx context.Context, hash libhead.Hash) (*header.Extende &block.Height, hash, eh.Hash()) } - err = ce.storeEDS(ctx, eh, eds, adder) + err = storeEDS(ctx, eh, eds, adder, ce.store, ce.availabilityWindow) if err != nil { return nil, err } @@ -195,33 +194,10 @@ func (ce *Exchange) getExtendedHeaderByHeight(ctx context.Context, height *int64 panic(fmt.Errorf("constructing extended header for height %d: %w", b.Header.Height, err)) } - err = ce.storeEDS(ctx, eh, eds, adder) + err = storeEDS(ctx, eh, eds, adder, ce.store, ce.availabilityWindow) if err != nil { return nil, err } return eh, nil } - -func (ce *Exchange) storeEDS( - ctx context.Context, - eh *header.ExtendedHeader, - eds *rsmt2d.ExtendedDataSquare, - adder *ipld.ProofsAdder, -) error { - if !pruner.IsWithinAvailabilityWindow(eh.Time(), ce.availabilityWindow) { - log.Debugw("skipping storage of historic block", "height", eh.Height()) - return nil - } - - ctx = ipld.CtxWithProofsAdder(ctx, adder) // TODO @renaynay: should we short-circuit this if pruning enabled - // && historic? - - err := storeEDS(ctx, eh.DAH.Hash(), eds, ce.store) - if err != nil { - return fmt.Errorf("storing EDS to eds.Store for block height %d: %w", eh.Height(), err) - } - - log.Debugw("stored EDS for height", "height", eh.Height()) - return nil -} diff --git a/core/listener.go b/core/listener.go index 5dc8d6e976..08e36d941f 100644 --- a/core/listener.go +++ b/core/listener.go @@ -224,15 +224,9 @@ func (cl *Listener) handleNewSignedBlock(ctx context.Context, b types.EventDataS panic(fmt.Errorf("making extended header: %w", err)) } - // only store EDS if the header is within the availability window - if pruner.IsWithinAvailabilityWindow(eh.Time(), cl.availabilityWindow) { - // attempt to store block data if not empty - ctx = ipld.CtxWithProofsAdder(ctx, adder) - - err = storeEDS(ctx, b.Header.DataHash.Bytes(), eds, cl.store) - if err != nil { - return fmt.Errorf("storing EDS: %w", err) - } + err = storeEDS(ctx, eh, eds, adder, cl.store, cl.availabilityWindow) + if err != nil { + return fmt.Errorf("storing EDS: %w", err) } syncing, err := cl.fetcher.IsSyncing(ctx)