Skip to content

Commit

Permalink
refactor(nodebuilder/header): get rid of InitStore (celestiaorg#2678)
Browse files Browse the repository at this point in the history
Not needed anymore
  • Loading branch information
renaynay authored Sep 11, 2023
1 parent a770ca1 commit 6cc54e7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
25 changes: 17 additions & 8 deletions nodebuilder/header/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package header
import (
"context"

"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
Expand Down Expand Up @@ -63,7 +64,7 @@ func newP2PExchange[H libhead.Header[H]](
func newSyncer[H libhead.Header[H]](
ex libhead.Exchange[H],
fservice libfraud.Service[H],
store InitStore[H],
store libhead.Store[H],
sub libhead.Subscriber[H],
cfg Config,
) (*sync.Syncer[H], *modfraud.ServiceBreaker[*sync.Syncer[H], H], error) {
Expand All @@ -82,26 +83,34 @@ func newSyncer[H libhead.Header[H]](
}, nil
}

// InitStore is a type representing initialized header store.
// NOTE: It is needed to ensure that Store is always initialized before Syncer is started.
type InitStore[H libhead.Header[H]] libhead.Store[H]

// newInitStore constructs an initialized store
func newInitStore[H libhead.Header[H]](
lc fx.Lifecycle,
cfg Config,
net modp2p.Network,
s libhead.Store[H],
ds datastore.Batching,
ex libhead.Exchange[H],
) (InitStore[H], error) {
) (libhead.Store[H], error) {
s, err := store.NewStore[H](ds, store.WithParams(cfg.Store))
if err != nil {
return nil, err
}

trustedHash, err := cfg.trustedHash(net)
if err != nil {
return nil, err
}

lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return store.Init(ctx, s, ex, trustedHash)
err = store.Init[H](ctx, s, ex, trustedHash)
if err != nil {
return err
}
return s.Start(ctx)
},
OnStop: func(ctx context.Context) error {
return s.Stop(ctx)
},
})

Expand Down
14 changes: 0 additions & 14 deletions nodebuilder/header/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

libhead "github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-header/p2p"
"github.com/celestiaorg/go-header/store"
"github.com/celestiaorg/go-header/sync"

"github.com/celestiaorg/celestia-node/header"
Expand All @@ -31,19 +30,6 @@ func ConstructModule[H libhead.Header[H]](tp node.Type, cfg *Config) fx.Option {
fx.Supply(*cfg),
fx.Error(cfgErr),
fx.Provide(newHeaderService),
fx.Provide(fx.Annotate(
func(ds datastore.Batching) (libhead.Store[H], error) {
return store.NewStore[H](ds, store.WithParams(cfg.Store))
},
fx.OnStart(func(ctx context.Context, str libhead.Store[H]) error {
s := str.(*store.Store[H])
return s.Start(ctx)
}),
fx.OnStop(func(ctx context.Context, str libhead.Store[H]) error {
s := str.(*store.Store[H])
return s.Stop(ctx)
}),
)),
fx.Provide(newInitStore[H]),
fx.Provide(func(subscriber *p2p.Subscriber[H]) libhead.Subscriber[H] {
return subscriber
Expand Down
9 changes: 7 additions & 2 deletions nodebuilder/header/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ func TestConstructModule_StoreParams(t *testing.T) {

app := fxtest.New(t,
fx.Supply(modp2p.Private),
fx.Provide(func() datastore.Batching {
return datastore.NewMapDatastore()
fx.Supply(modp2p.Bootstrappers{}),
fx.Provide(context.Background),
fx.Provide(libp2p.New),
fx.Provide(conngater.NewBasicConnectionGater),
fx.Provide(func() (datastore.Batching, datastore.Datastore) {
ds := datastore.NewMapDatastore()
return ds, ds
}),
ConstructModule[*header.ExtendedHeader](node.Light, &cfg),
fx.Invoke(
Expand Down
4 changes: 2 additions & 2 deletions nodebuilder/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"go.uber.org/fx"

apptypes "github.com/celestiaorg/celestia-app/x/blob/types"
libhead "github.com/celestiaorg/go-header"

"github.com/celestiaorg/celestia-node/core"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/header/headertest"
"github.com/celestiaorg/celestia-node/libs/fxutil"
modhead "github.com/celestiaorg/celestia-node/nodebuilder/header"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
"github.com/celestiaorg/celestia-node/nodebuilder/state"
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestNodeWithConfig(t *testing.T, tp node.Type, cfg *Config, opts ...fx.Opti
// temp dir for the eds store FIXME: Should be in mem
fx.Replace(node.StorePath(t.TempDir())),
// avoid requesting trustedPeer during initialization
fxutil.ReplaceAs(headertest.NewStore(t), new(modhead.InitStore[*header.ExtendedHeader])),
fxutil.ReplaceAs(headertest.NewStore(t), new(libhead.Store[*header.ExtendedHeader])),
)

// in fact, we don't need core.Client in tests, but Bridge requires is a valid one
Expand Down

0 comments on commit 6cc54e7

Please sign in to comment.