Don't build full API in storage miner

This commit is contained in:
Łukasz Magiera 2019-07-24 00:34:13 +02:00
parent 8d529d1ae7
commit d0cbf02d36
4 changed files with 31 additions and 16 deletions

View File

@ -44,7 +44,7 @@ var runCmd = &cli.Command{
return xerrors.Errorf("repo at '%s' is not initialized, run 'lotus-storage-miner init' to set it up", cctx.String(FlagStorageRepo))
}
minerapi, err := node.New(ctx,
err = node.New(ctx,
node.StorageMiner(),
node.Online(),
node.Repo(r),
@ -66,7 +66,7 @@ var runCmd = &cli.Command{
log.Infof("Remote version %s", v)
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", minerapi)
//rpcServer.Register("Filecoin", minerapi)
http.Handle("/rpc/v0", rpcServer)
return http.ListenAndServe("127.0.0.1:"+cctx.String("api"), http.DefaultServeMux)
},

View File

@ -4,6 +4,8 @@ package main
import (
"context"
"github.com/filecoin-project/go-lotus/api"
"github.com/multiformats/go-multiaddr"
"gopkg.in/urfave/cli.v2"
@ -32,7 +34,10 @@ var DaemonCmd = &cli.Command{
return err
}
api, err := node.New(ctx,
var api api.API
err = node.New(ctx,
node.FullAPI(&api),
node.Online(),
node.Repo(r),

View File

@ -71,6 +71,8 @@ const (
HandleIncomingMessagesKey
// daemon
ExtractApiKey
SetApiEndpointKey
_nInvokes // keep this last
@ -125,10 +127,6 @@ func defaults() []Option {
return []Option{
Override(new(helpers.MetricsCtx), context.Background),
Override(new(record.Validator), modules.RecordValidator),
// Filecoin modules
Override(new(*chain.ChainStore), chain.NewChainStore),
}
}
@ -165,7 +163,6 @@ func libp2p() Option {
// Online sets up basic libp2p node
func Online() Option {
return Options(
Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages),
// make sure that online is applied before Config.
// This is important because Config overrides some of Online units
func(s *Settings) error { s.Online = true; return nil },
@ -178,6 +175,12 @@ func Online() Option {
// Full node
ApplyIf(func(s *Settings) bool { return s.nodeType == nodeFull },
// TODO: Fix offline mode
Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages),
Override(new(*chain.ChainStore), chain.NewChainStore),
Override(new(blockstore.GCLocker), blockstore.NewGCLocker),
Override(new(blockstore.GCBlockstore), blockstore.NewGCBlockstore),
Override(new(exchange.Interface), modules.Bitswap),
@ -265,9 +268,17 @@ func Repo(r repo.Repo) Option {
)
}
func FullAPI(out *api.API) Option {
return func(s *Settings) error {
resAPI := &API{}
s.invokes[ExtractApiKey] = fx.Extract(resAPI)
*out = resAPI
return nil
}
}
// New builds and starts new Filecoin node
func New(ctx context.Context, opts ...Option) (api.API, error) {
resAPI := &API{}
func New(ctx context.Context, opts ...Option) error {
settings := Settings{
modules: map[interface{}]fx.Option{},
invokes: make([]fx.Option, _nInvokes),
@ -275,7 +286,7 @@ func New(ctx context.Context, opts ...Option) (api.API, error) {
// apply module options in the right order
if err := Options(Options(defaults()...), Options(opts...))(&settings); err != nil {
return nil, err
return err
}
// gather constructors for fx.Options
@ -295,8 +306,6 @@ func New(ctx context.Context, opts ...Option) (api.API, error) {
fx.Options(ctors...),
fx.Options(settings.invokes...),
fx.Extract(resAPI),
fx.NopLogger,
)
@ -304,10 +313,10 @@ func New(ctx context.Context, opts ...Option) (api.API, error) {
// on this context, and implement closing logic through lifecycles
// correctly
if err := app.Start(ctx); err != nil {
return nil, err
return err
}
return resAPI, nil
return nil
}
// In-memory / testing

View File

@ -24,7 +24,8 @@ func builder(t *testing.T, n int) []api.API {
for i := 0; i < n; i++ {
var err error
out[i], err = node.New(ctx,
err = node.New(ctx,
node.FullAPI(&out[i]),
node.Online(),
node.Repo(repo.NewMemory(nil)),
MockHost(mn),