lotus/node/builder.go

263 lines
7.5 KiB
Go
Raw Normal View History

package node
import (
"context"
2019-07-04 15:50:48 +00:00
"errors"
2019-07-09 16:27:07 +00:00
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
2019-07-04 20:06:02 +00:00
"reflect"
"time"
2019-07-05 10:06:28 +00:00
"github.com/ipfs/go-datastore"
2019-07-08 14:07:09 +00:00
blockstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
ci "github.com/libp2p/go-libp2p-core/crypto"
2019-07-05 10:06:28 +00:00
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
2019-07-05 10:06:28 +00:00
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/routing"
"github.com/libp2p/go-libp2p-peerstore/pstoremem"
2019-07-08 14:07:09 +00:00
pubsub "github.com/libp2p/go-libp2p-pubsub"
2019-07-05 10:06:28 +00:00
record "github.com/libp2p/go-libp2p-record"
"go.uber.org/fx"
"github.com/filecoin-project/go-lotus/api"
2019-07-08 13:36:43 +00:00
"github.com/filecoin-project/go-lotus/chain"
2019-07-05 10:06:28 +00:00
"github.com/filecoin-project/go-lotus/node/config"
2019-07-08 13:36:43 +00:00
"github.com/filecoin-project/go-lotus/node/hello"
"github.com/filecoin-project/go-lotus/node/modules"
"github.com/filecoin-project/go-lotus/node/modules/helpers"
2019-07-02 12:40:25 +00:00
"github.com/filecoin-project/go-lotus/node/modules/lp2p"
2019-07-08 14:07:09 +00:00
"github.com/filecoin-project/go-lotus/node/modules/testing"
)
2019-07-04 15:50:48 +00:00
// special is a type used to give keys to modules which
// can't really be identified by the returned type
type special struct{ id int }
//nolint:golint
2019-07-04 15:50:48 +00:00
var (
DefaultTransportsKey = special{0} // Libp2p option
PNetKey = special{1} // Option + multiret
DiscoveryHandlerKey = special{2} // Private type
AddrsFactoryKey = special{3} // Libp2p option
SmuxTransportKey = special{4} // Libp2p option
RelayKey = special{5} // Libp2p option
SecurityKey = special{6} // Libp2p option
BaseRoutingKey = special{7} // fx groups + multiret
NatPortMapKey = special{8} // Libp2p option
ConnectionManagerKey = special{9} // Libp2p option
)
type invoke int
2019-07-08 13:36:43 +00:00
//nolint:golint
2019-07-04 15:50:48 +00:00
const (
2019-07-08 13:36:43 +00:00
// libp2p
2019-07-04 20:06:02 +00:00
2019-07-08 13:36:43 +00:00
PstoreAddSelfKeysKey = invoke(iota)
2019-07-04 15:50:48 +00:00
StartListeningKey
2019-07-08 13:36:43 +00:00
// filecoin
SetGenisisKey
2019-07-08 14:07:09 +00:00
2019-07-08 13:36:43 +00:00
RunHelloKey
2019-07-08 14:07:09 +00:00
RunBlockSyncKey
HandleIncomingBlocksKey
HandleIncomingMessagesKey
2019-07-08 13:36:43 +00:00
2019-07-04 15:50:48 +00:00
_nInvokes // keep this last
)
type settings struct {
2019-07-04 20:06:02 +00:00
// modules is a map of constructors for DI
//
// In most cases the index will be a reflect. Type of element returned by
// the constructor, but for some 'constructors' it's hard to specify what's
// the return type should be (or the constructor returns fx group)
2019-07-04 15:50:48 +00:00
modules map[interface{}]fx.Option
// invokes are separate from modules as they can't be referenced by return
// type, and must be applied in correct order
invokes []fx.Option
online bool // Online option applied
config bool // Config option applied
}
2019-07-04 20:06:02 +00:00
// Override option changes constructor for a given type
func Override(typ, constructor interface{}) Option {
return func(s *settings) error {
if i, ok := typ.(invoke); ok {
s.invokes[i] = fx.Invoke(constructor)
return nil
}
if c, ok := typ.(special); ok {
s.modules[c] = fx.Provide(constructor)
return nil
}
ctor := as(constructor, typ)
rt := reflect.TypeOf(typ).Elem()
s.modules[rt] = fx.Provide(ctor)
return nil
}
}
2019-07-04 15:50:48 +00:00
var defConf = config.Default()
var defaults = []Option{
Override(new(helpers.MetricsCtx), context.Background),
randomIdentity(),
2019-07-08 13:36:43 +00:00
Override(new(datastore.Batching), testing.MapDatastore),
Override(new(blockstore.Blockstore), testing.MapBlockstore), // NOT on top of ds above
2019-07-04 15:50:48 +00:00
Override(new(record.Validator), modules.RecordValidator),
2019-07-08 13:36:43 +00:00
// Filecoin modules
Override(new(*chain.ChainStore), chain.NewChainStore),
2019-07-04 15:50:48 +00:00
}
2019-07-04 20:06:02 +00:00
// Online sets up basic libp2p node
2019-07-04 15:50:48 +00:00
func Online() Option {
return Options(
2019-07-04 20:06:02 +00:00
// make sure that online is applied before Config.
// This is important because Config overrides some of Online units
2019-07-04 15:50:48 +00:00
func(s *settings) error { s.online = true; return nil },
applyIf(func(s *settings) bool { return s.config },
Error(errors.New("the Online option must be set before Config option")),
),
Override(new(peerstore.Peerstore), pstoremem.NewPeerstore),
Override(DefaultTransportsKey, lp2p.DefaultTransports),
Override(PNetKey, lp2p.PNet),
Override(new(lp2p.RawHost), lp2p.Host),
Override(new(host.Host), lp2p.RoutedHost),
Override(new(lp2p.BaseIpfsRouting), lp2p.DHTRouting(false)),
Override(DiscoveryHandlerKey, lp2p.DiscoveryHandler),
Override(AddrsFactoryKey, lp2p.AddrsFactory(nil, nil)),
Override(SmuxTransportKey, lp2p.SmuxTransport(true)),
Override(RelayKey, lp2p.Relay(true, false)),
Override(SecurityKey, lp2p.Security(true, false)),
Override(BaseRoutingKey, lp2p.BaseRouting),
Override(new(routing.Routing), lp2p.Routing),
2019-07-09 14:30:54 +00:00
//Override(NatPortMapKey, lp2p.NatPortMap), //TODO: reenable when closing logic is actually there
2019-07-04 15:50:48 +00:00
Override(ConnectionManagerKey, lp2p.ConnectionManager(50, 200, 20*time.Second)),
2019-07-08 14:07:09 +00:00
Override(new(*pubsub.PubSub), lp2p.GossipSub()),
2019-07-04 15:50:48 +00:00
Override(PstoreAddSelfKeysKey, lp2p.PstoreAddSelfKeys),
Override(StartListeningKey, lp2p.StartListening(defConf.Libp2p.ListenAddresses)),
2019-07-08 13:36:43 +00:00
//
Override(new(blockstore.GCLocker), blockstore.NewGCLocker),
Override(new(blockstore.GCBlockstore), blockstore.NewGCBlockstore),
Override(new(exchange.Interface), modules.Bitswap),
// Filecoin services
Override(new(*chain.Syncer), chain.NewSyncer),
Override(new(*chain.BlockSync), chain.NewBlockSyncClient),
Override(new(*chain.Wallet), chain.NewWallet),
2019-07-08 14:07:09 +00:00
Override(new(*chain.MessagePool), chain.NewMessagePool),
2019-07-08 13:36:43 +00:00
Override(new(modules.Genesis), testing.MakeGenesis),
Override(SetGenisisKey, modules.SetGenesis),
Override(new(*hello.Service), hello.NewHelloService),
2019-07-08 14:07:09 +00:00
Override(new(*chain.BlockSyncService), chain.NewBlockSyncService),
Override(RunHelloKey, modules.RunHello),
Override(RunBlockSyncKey, modules.RunBlockSync),
Override(HandleIncomingBlocksKey, modules.HandleIncomingBlocks),
Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages),
2019-07-04 15:50:48 +00:00
)
}
2019-07-04 20:06:02 +00:00
// Config sets up constructors based on the provided config
2019-07-04 15:50:48 +00:00
func Config(cfg *config.Root) Option {
return Options(
func(s *settings) error { s.config = true; return nil },
applyIf(func(s *settings) bool { return s.online },
Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)),
),
)
}
2019-07-02 13:05:43 +00:00
// New builds and starts new Filecoin node
2019-07-04 15:50:48 +00:00
func New(ctx context.Context, opts ...Option) (api.API, error) {
2019-07-09 10:58:13 +00:00
resAPI := &API{}
2019-07-04 15:50:48 +00:00
settings := settings{
modules: map[interface{}]fx.Option{},
invokes: make([]fx.Option, _nInvokes),
}
2019-07-04 20:06:02 +00:00
// apply module options in the right order
2019-07-04 15:50:48 +00:00
if err := Options(Options(defaults...), Options(opts...))(&settings); err != nil {
return nil, err
}
2019-07-04 20:06:02 +00:00
// gather constructors for fx.Options
2019-07-04 15:50:48 +00:00
ctors := make([]fx.Option, 0, len(settings.modules))
for _, opt := range settings.modules {
ctors = append(ctors, opt)
}
2019-07-04 20:06:02 +00:00
// fill holes in invokes for use in fx.Options
2019-07-04 15:50:48 +00:00
for i, opt := range settings.invokes {
if opt == nil {
settings.invokes[i] = fx.Options()
}
}
app := fx.New(
2019-07-04 15:50:48 +00:00
fx.Options(ctors...),
fx.Options(settings.invokes...),
2019-07-09 10:58:13 +00:00
fx.Extract(resAPI),
)
2019-07-04 20:06:02 +00:00
// TODO: we probably should have a 'firewall' for Closing signal
// on this context, and implement closing logic through lifecycles
// correctly
if err := app.Start(ctx); err != nil {
return nil, err
}
2019-07-09 10:58:13 +00:00
return resAPI, nil
}
// In-memory / testing
2019-07-09 16:27:07 +00:00
func MockHost(mn mocknet.Mocknet) Option {
return Options(
applyIf(func(s *settings) bool { return !s.online },
Error(errors.New("MockHost must be specified after Online")),
),
Override(new(lp2p.RawHost), lp2p.MockHost),
Override(new(mocknet.Mocknet), mn),
)
}
2019-07-04 15:50:48 +00:00
func randomIdentity() Option {
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
if err != nil {
2019-07-04 15:50:48 +00:00
return Error(err)
}
2019-07-04 15:50:48 +00:00
return Options(
Override(new(ci.PrivKey), sk),
Override(new(ci.PubKey), pk),
Override(new(peer.ID), peer.IDFromPublicKey),
)
}