github.com/celestiaorg/celestia-node@v0.15.0-beta.1/nodebuilder/core/module.go (about)

     1  package core
     2  
     3  import (
     4  	"context"
     5  
     6  	"go.uber.org/fx"
     7  
     8  	libhead "github.com/celestiaorg/go-header"
     9  
    10  	"github.com/celestiaorg/celestia-node/core"
    11  	"github.com/celestiaorg/celestia-node/header"
    12  	"github.com/celestiaorg/celestia-node/libs/fxutil"
    13  	"github.com/celestiaorg/celestia-node/nodebuilder/node"
    14  	"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
    15  	"github.com/celestiaorg/celestia-node/share/eds"
    16  	"github.com/celestiaorg/celestia-node/share/p2p/shrexsub"
    17  )
    18  
    19  // ConstructModule collects all the components and services related to managing the relationship
    20  // with the Core node.
    21  func ConstructModule(tp node.Type, cfg *Config, options ...fx.Option) fx.Option {
    22  	// sanitize config values before constructing module
    23  	cfgErr := cfg.Validate()
    24  
    25  	baseComponents := fx.Options(
    26  		fx.Supply(*cfg),
    27  		fx.Error(cfgErr),
    28  		fx.Options(options...),
    29  	)
    30  
    31  	switch tp {
    32  	case node.Light, node.Full:
    33  		return fx.Module("core", baseComponents)
    34  	case node.Bridge:
    35  		return fx.Module("core",
    36  			baseComponents,
    37  			fx.Provide(core.NewBlockFetcher),
    38  			fxutil.ProvideAs(
    39  				func(
    40  					fetcher *core.BlockFetcher,
    41  					store *eds.Store,
    42  					construct header.ConstructFn,
    43  				) (*core.Exchange, error) {
    44  					var opts []core.Option
    45  					if MetricsEnabled {
    46  						opts = append(opts, core.WithMetrics())
    47  					}
    48  
    49  					return core.NewExchange(fetcher, store, construct, opts...)
    50  				},
    51  				new(libhead.Exchange[*header.ExtendedHeader])),
    52  			fx.Invoke(fx.Annotate(
    53  				func(
    54  					bcast libhead.Broadcaster[*header.ExtendedHeader],
    55  					fetcher *core.BlockFetcher,
    56  					pubsub *shrexsub.PubSub,
    57  					construct header.ConstructFn,
    58  					store *eds.Store,
    59  					chainID p2p.Network,
    60  				) (*core.Listener, error) {
    61  					opts := []core.Option{core.WithChainID(chainID)}
    62  					if MetricsEnabled {
    63  						opts = append(opts, core.WithMetrics())
    64  					}
    65  
    66  					return core.NewListener(bcast, fetcher, pubsub.Broadcast, construct, store, p2p.BlockTime, opts...)
    67  				},
    68  				fx.OnStart(func(ctx context.Context, listener *core.Listener) error {
    69  					return listener.Start(ctx)
    70  				}),
    71  				fx.OnStop(func(ctx context.Context, listener *core.Listener) error {
    72  					return listener.Stop(ctx)
    73  				}),
    74  			)),
    75  			fx.Provide(fx.Annotate(
    76  				remote,
    77  				fx.OnStart(func(ctx context.Context, client core.Client) error {
    78  					return client.Start()
    79  				}),
    80  				fx.OnStop(func(ctx context.Context, client core.Client) error {
    81  					return client.Stop()
    82  				}),
    83  			)),
    84  		)
    85  	default:
    86  		panic("invalid node type")
    87  	}
    88  }