2019-06-29 09:19:06 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-04 15:50:48 +00:00
|
|
|
"errors"
|
2019-07-04 20:06:02 +00:00
|
|
|
"reflect"
|
2019-07-01 10:18:00 +00:00
|
|
|
"time"
|
2019-06-29 09:19:06 +00:00
|
|
|
|
2019-07-08 14:07:09 +00:00
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
2019-07-01 10:18:00 +00:00
|
|
|
ci "github.com/libp2p/go-libp2p-core/crypto"
|
2019-07-05 10:06:28 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2019-06-29 09:19:06 +00:00
|
|
|
"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"
|
2019-07-01 10:18:00 +00:00
|
|
|
"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"
|
2019-06-29 09:19:06 +00:00
|
|
|
"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-08-01 17:12:41 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/deals"
|
2019-10-10 11:07:00 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/metrics"
|
2019-09-06 06:26:02 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/stmgr"
|
2019-07-26 04:54:22 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/store"
|
2019-07-18 15:41:30 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-25 22:15:33 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/wallet"
|
2019-07-27 00:45:27 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/lib/sectorbuilder"
|
2019-08-20 16:50:17 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/miner"
|
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"
|
2019-07-24 01:13:56 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/node/impl"
|
2019-06-29 09:19:06 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/node/modules"
|
2019-08-01 14:14:16 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/node/modules/dtypes"
|
2019-07-01 10:18:00 +00:00
|
|
|
"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-10 15:38:35 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/node/repo"
|
2019-08-12 17:09:56 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/paych"
|
2019-08-29 15:09:34 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/retrieval"
|
2019-08-26 13:45:36 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/retrieval/discovery"
|
2019-07-30 00:46:56 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/storage"
|
2019-09-16 16:40:26 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/storage/commitment"
|
2019-08-14 20:27:10 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/storage/sector"
|
2019-08-26 13:45:36 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/storage/sectorblocks"
|
2019-06-29 09:19:06 +00:00
|
|
|
)
|
|
|
|
|
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 }
|
|
|
|
|
2019-07-05 11:21:54 +00:00
|
|
|
//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-10-11 00:31:06 +00:00
|
|
|
BootstrapKey
|
2019-07-04 15:50:48 +00:00
|
|
|
|
2019-07-08 13:36:43 +00:00
|
|
|
// filecoin
|
2019-07-09 22:58:51 +00:00
|
|
|
SetGenesisKey
|
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-08-01 17:12:41 +00:00
|
|
|
RunDealClientKey
|
2019-08-14 20:27:10 +00:00
|
|
|
|
|
|
|
// storage miner
|
2019-08-02 16:25:10 +00:00
|
|
|
HandleDealsKey
|
2019-08-26 13:45:36 +00:00
|
|
|
HandleRetrievalKey
|
2019-08-14 20:27:10 +00:00
|
|
|
RunSectorServiceKey
|
2019-08-20 17:19:24 +00:00
|
|
|
RegisterMinerKey
|
2019-08-01 17:12:41 +00:00
|
|
|
|
2019-07-23 15:33:25 +00:00
|
|
|
// daemon
|
2019-07-23 22:34:13 +00:00
|
|
|
ExtractApiKey
|
2019-10-10 11:07:00 +00:00
|
|
|
HeadMetricsKey
|
2019-07-23 22:34:13 +00:00
|
|
|
|
2019-07-10 17:28:49 +00:00
|
|
|
SetApiEndpointKey
|
|
|
|
|
2019-07-04 15:50:48 +00:00
|
|
|
_nInvokes // keep this last
|
|
|
|
)
|
|
|
|
|
2019-07-19 09:23:24 +00:00
|
|
|
const (
|
|
|
|
nodeFull = iota
|
|
|
|
nodeStorageMiner
|
|
|
|
)
|
|
|
|
|
2019-07-10 13:06:04 +00:00
|
|
|
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
|
|
|
|
|
2019-07-19 09:23:24 +00:00
|
|
|
nodeType int
|
|
|
|
|
2019-07-10 13:06:04 +00:00
|
|
|
Online bool // Online option applied
|
|
|
|
Config bool // Config option applied
|
2019-07-04 15:50:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 20:06:02 +00:00
|
|
|
// Override option changes constructor for a given type
|
|
|
|
func Override(typ, constructor interface{}) Option {
|
2019-07-10 13:06:04 +00:00
|
|
|
return func(s *Settings) error {
|
2019-07-04 20:06:02 +00:00
|
|
|
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()
|
|
|
|
|
2019-07-09 17:03:36 +00:00
|
|
|
func defaults() []Option {
|
|
|
|
return []Option{
|
|
|
|
Override(new(helpers.MetricsCtx), context.Background),
|
|
|
|
Override(new(record.Validator), modules.RecordValidator),
|
2019-07-24 21:10:27 +00:00
|
|
|
|
|
|
|
// Filecoin modules
|
|
|
|
|
2019-07-09 17:03:36 +00:00
|
|
|
}
|
2019-07-04 15:50:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 09:23:24 +00:00
|
|
|
func libp2p() Option {
|
2019-07-04 15:50:48 +00:00
|
|
|
return Options(
|
|
|
|
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-10-09 03:16:35 +00:00
|
|
|
Override(NatPortMapKey, lp2p.NatPortMap),
|
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-19 09:23:24 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Online sets up basic libp2p node
|
|
|
|
func Online() Option {
|
|
|
|
return Options(
|
|
|
|
// 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 },
|
|
|
|
ApplyIf(func(s *Settings) bool { return s.Config },
|
|
|
|
Error(errors.New("the Online option must be set before Config option")),
|
|
|
|
),
|
2019-07-08 13:36:43 +00:00
|
|
|
|
2019-07-19 09:23:24 +00:00
|
|
|
libp2p(),
|
2019-07-08 13:36:43 +00:00
|
|
|
|
2019-07-30 00:46:56 +00:00
|
|
|
// common
|
|
|
|
|
2019-07-19 09:23:24 +00:00
|
|
|
// Full node
|
2019-07-08 13:36:43 +00:00
|
|
|
|
2019-07-19 09:23:24 +00:00
|
|
|
ApplyIf(func(s *Settings) bool { return s.nodeType == nodeFull },
|
2019-07-23 22:34:13 +00:00
|
|
|
// TODO: Fix offline mode
|
|
|
|
|
2019-10-11 03:16:12 +00:00
|
|
|
Override(new(dtypes.BootstrapPeers), modules.BuiltinBootstrap),
|
|
|
|
|
2019-07-23 22:34:13 +00:00
|
|
|
Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages),
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
Override(new(*store.ChainStore), modules.ChainStore),
|
2019-09-06 06:26:02 +00:00
|
|
|
Override(new(*stmgr.StateManager), stmgr.NewStateManager),
|
2019-08-12 23:54:53 +00:00
|
|
|
Override(new(*wallet.Wallet), wallet.NewWallet),
|
2019-07-23 22:34:13 +00:00
|
|
|
|
2019-08-01 14:14:16 +00:00
|
|
|
Override(new(dtypes.ChainGCLocker), blockstore.NewGCLocker),
|
|
|
|
Override(new(dtypes.ChainGCBlockstore), modules.ChainGCBlockstore),
|
|
|
|
Override(new(dtypes.ChainExchange), modules.ChainExchange),
|
|
|
|
Override(new(dtypes.ChainBlockService), modules.ChainBlockservice),
|
|
|
|
Override(new(dtypes.ClientDAG), testing.MemoryClientDag),
|
2019-07-08 13:36:43 +00:00
|
|
|
|
2019-07-19 09:23:24 +00:00
|
|
|
// Filecoin services
|
|
|
|
Override(new(*chain.Syncer), chain.NewSyncer),
|
|
|
|
Override(new(*chain.BlockSync), chain.NewBlockSyncClient),
|
|
|
|
Override(new(*chain.MessagePool), chain.NewMessagePool),
|
2019-07-08 13:36:43 +00:00
|
|
|
|
2019-07-24 22:49:37 +00:00
|
|
|
Override(new(modules.Genesis), modules.ErrorGenesis),
|
2019-07-19 09:23:24 +00:00
|
|
|
Override(SetGenesisKey, modules.SetGenesis),
|
|
|
|
|
|
|
|
Override(new(*hello.Service), hello.NewHelloService),
|
|
|
|
Override(new(*chain.BlockSyncService), chain.NewBlockSyncService),
|
|
|
|
Override(RunHelloKey, modules.RunHello),
|
|
|
|
Override(RunBlockSyncKey, modules.RunBlockSync),
|
|
|
|
Override(HandleIncomingBlocksKey, modules.HandleIncomingBlocks),
|
2019-10-11 02:45:45 +00:00
|
|
|
Override(HeadMetricsKey, metrics.SendHeadNotifs("")),
|
2019-08-01 17:12:41 +00:00
|
|
|
|
2019-08-26 13:45:36 +00:00
|
|
|
Override(new(*discovery.Local), discovery.NewLocal),
|
|
|
|
Override(new(discovery.PeerResolver), modules.RetrievalResolver),
|
|
|
|
|
|
|
|
Override(new(*retrieval.Client), retrieval.NewClient),
|
2019-08-01 17:12:41 +00:00
|
|
|
Override(new(*deals.Client), deals.NewClient),
|
|
|
|
Override(RunDealClientKey, modules.RunDealClient),
|
2019-08-12 17:09:56 +00:00
|
|
|
|
2019-09-16 16:40:26 +00:00
|
|
|
Override(new(*paych.Store), paych.NewStore),
|
|
|
|
Override(new(*paych.Manager), paych.NewManager),
|
2019-08-20 16:50:17 +00:00
|
|
|
|
|
|
|
Override(new(*miner.Miner), miner.NewMiner),
|
2019-07-19 09:23:24 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
// Storage miner
|
2019-07-29 19:11:55 +00:00
|
|
|
ApplyIf(func(s *Settings) bool { return s.nodeType == nodeStorageMiner },
|
2019-08-14 21:33:52 +00:00
|
|
|
Override(new(*sectorbuilder.SectorBuilder), sectorbuilder.New),
|
2019-08-14 20:27:10 +00:00
|
|
|
Override(new(*sector.Store), sector.NewStore),
|
2019-08-26 10:04:57 +00:00
|
|
|
Override(new(*sectorblocks.SectorBlocks), sectorblocks.NewSectorBlocks),
|
2019-09-16 16:40:26 +00:00
|
|
|
Override(new(*commitment.Tracker), commitment.NewTracker),
|
2019-07-30 00:46:56 +00:00
|
|
|
Override(new(*storage.Miner), modules.StorageMiner),
|
2019-08-02 16:25:10 +00:00
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
Override(new(dtypes.StagingDAG), modules.StagingDAG),
|
|
|
|
|
2019-08-26 13:45:36 +00:00
|
|
|
Override(new(*retrieval.Miner), retrieval.NewMiner),
|
2019-08-02 16:25:10 +00:00
|
|
|
Override(new(*deals.Handler), deals.NewHandler),
|
2019-08-26 13:45:36 +00:00
|
|
|
Override(HandleRetrievalKey, modules.HandleRetrieval),
|
2019-08-02 16:25:10 +00:00
|
|
|
Override(HandleDealsKey, modules.HandleDeals),
|
2019-08-14 20:27:10 +00:00
|
|
|
Override(RunSectorServiceKey, modules.RunSectorService),
|
2019-08-20 17:19:24 +00:00
|
|
|
Override(RegisterMinerKey, modules.RegisterMiner),
|
2019-07-29 19:11:55 +00:00
|
|
|
),
|
2019-07-19 09:23:24 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:58:31 +00:00
|
|
|
func StorageMiner(out *api.StorageMiner) Option {
|
2019-07-19 09:23:24 +00:00
|
|
|
return Options(
|
|
|
|
ApplyIf(func(s *Settings) bool { return s.Config },
|
|
|
|
Error(errors.New("the StorageMiner option must be set before Config option")),
|
|
|
|
),
|
|
|
|
ApplyIf(func(s *Settings) bool { return s.Online },
|
|
|
|
Error(errors.New("the StorageMiner option must be set before Online option")),
|
|
|
|
),
|
|
|
|
|
|
|
|
func(s *Settings) error {
|
|
|
|
s.nodeType = nodeStorageMiner
|
|
|
|
return nil
|
|
|
|
},
|
2019-07-24 00:58:31 +00:00
|
|
|
|
|
|
|
func(s *Settings) error {
|
2019-07-24 01:13:56 +00:00
|
|
|
resAPI := &impl.StorageMinerAPI{}
|
2019-07-24 00:58:31 +00:00
|
|
|
s.invokes[ExtractApiKey] = fx.Extract(resAPI)
|
|
|
|
*out = resAPI
|
|
|
|
return nil
|
|
|
|
},
|
2019-07-04 15:50:48 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-10 13:06:04 +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(
|
2019-07-10 13:06:04 +00:00
|
|
|
func(s *Settings) error { s.Config = true; return nil },
|
2019-07-04 15:50:48 +00:00
|
|
|
|
2019-07-10 13:06:04 +00:00
|
|
|
ApplyIf(func(s *Settings) bool { return s.Online },
|
2019-07-04 15:50:48 +00:00
|
|
|
Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)),
|
2019-10-11 02:45:45 +00:00
|
|
|
|
2019-10-11 03:16:12 +00:00
|
|
|
ApplyIf(func(s *Settings) bool { return len(cfg.Libp2p.BootstrapPeers) > 0 },
|
|
|
|
Override(new(dtypes.BootstrapPeers), modules.ConfigBootstrap(cfg.Libp2p.BootstrapPeers)),
|
|
|
|
),
|
|
|
|
|
2019-10-11 02:45:45 +00:00
|
|
|
ApplyIf(func(s *Settings) bool { return s.nodeType == nodeFull },
|
|
|
|
Override(HeadMetricsKey, metrics.SendHeadNotifs(cfg.Metrics.Nickname)),
|
|
|
|
),
|
2019-07-04 15:50:48 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-10 15:38:35 +00:00
|
|
|
func Repo(r repo.Repo) Option {
|
|
|
|
lr, err := r.Lock()
|
|
|
|
if err != nil {
|
|
|
|
return Error(err)
|
|
|
|
}
|
|
|
|
cfg, err := lr.Config()
|
|
|
|
if err != nil {
|
|
|
|
return Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Options(
|
|
|
|
Config(cfg),
|
|
|
|
Override(new(repo.LockedRepo), modules.LockedRepo(lr)), // module handles closing
|
|
|
|
|
2019-08-01 14:14:16 +00:00
|
|
|
Override(new(dtypes.MetadataDS), modules.Datastore),
|
2019-08-01 14:19:53 +00:00
|
|
|
Override(new(dtypes.ChainBlockstore), modules.ChainBlockstore),
|
2019-07-10 17:36:17 +00:00
|
|
|
|
2019-08-01 14:14:16 +00:00
|
|
|
Override(new(dtypes.ClientFilestore), modules.ClientFstore),
|
2019-08-26 13:45:36 +00:00
|
|
|
Override(new(dtypes.ClientBlockstore), modules.ClientBlockstore),
|
2019-08-01 14:14:16 +00:00
|
|
|
Override(new(dtypes.ClientDAG), modules.ClientDAG),
|
2019-07-16 16:02:51 +00:00
|
|
|
|
2019-10-03 00:02:06 +00:00
|
|
|
Override(new(ci.PrivKey), lp2p.PrivKey),
|
2019-07-10 17:36:17 +00:00
|
|
|
Override(new(ci.PubKey), ci.PrivKey.GetPublic),
|
|
|
|
Override(new(peer.ID), peer.IDFromPublicKey),
|
2019-07-18 15:41:30 +00:00
|
|
|
|
2019-07-18 16:10:53 +00:00
|
|
|
Override(new(types.KeyStore), modules.KeyStore),
|
2019-07-23 20:23:44 +00:00
|
|
|
|
2019-08-20 17:19:24 +00:00
|
|
|
Override(new(*dtypes.APIAlg), modules.APISecret),
|
2019-07-10 15:38:35 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:09:34 +00:00
|
|
|
func FullAPI(out *api.FullNode) Option {
|
2019-07-23 22:34:13 +00:00
|
|
|
return func(s *Settings) error {
|
2019-08-20 17:19:24 +00:00
|
|
|
resAPI := &impl.FullNodeAPI{}
|
2019-07-23 22:34:13 +00:00
|
|
|
s.invokes[ExtractApiKey] = fx.Extract(resAPI)
|
|
|
|
*out = resAPI
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 14:23:08 +00:00
|
|
|
type StopFunc func(context.Context) error
|
|
|
|
|
2019-07-02 13:05:43 +00:00
|
|
|
// New builds and starts new Filecoin node
|
2019-09-17 14:23:08 +00:00
|
|
|
func New(ctx context.Context, opts ...Option) (StopFunc, error) {
|
2019-07-10 13:06:04 +00:00
|
|
|
settings := Settings{
|
2019-07-04 15:50:48 +00:00
|
|
|
modules: map[interface{}]fx.Option{},
|
|
|
|
invokes: make([]fx.Option, _nInvokes),
|
|
|
|
}
|
2019-06-29 09:19:06 +00:00
|
|
|
|
2019-07-04 20:06:02 +00:00
|
|
|
// apply module options in the right order
|
2019-07-09 17:03:36 +00:00
|
|
|
if err := Options(Options(defaults()...), Options(opts...))(&settings); err != nil {
|
2019-09-17 14:23:08 +00:00
|
|
|
return nil, err
|
2019-07-04 15:50:48 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2019-07-01 10:18:00 +00:00
|
|
|
|
2019-06-29 09:19:06 +00:00
|
|
|
app := fx.New(
|
2019-07-04 15:50:48 +00:00
|
|
|
fx.Options(ctors...),
|
|
|
|
fx.Options(settings.invokes...),
|
2019-06-29 09:19:06 +00:00
|
|
|
|
2019-07-09 17:03:36 +00:00
|
|
|
fx.NopLogger,
|
2019-06-29 09:19:06 +00:00
|
|
|
)
|
|
|
|
|
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
|
2019-06-29 09:19:06 +00:00
|
|
|
if err := app.Start(ctx); err != nil {
|
2019-07-30 11:45:48 +00:00
|
|
|
// comment fx.NopLogger few lines above for easier debugging
|
2019-09-17 14:23:08 +00:00
|
|
|
return nil, err
|
2019-06-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 14:23:08 +00:00
|
|
|
return app.Stop, nil
|
2019-06-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 10:18:00 +00:00
|
|
|
// In-memory / testing
|
|
|
|
|
2019-07-04 15:50:48 +00:00
|
|
|
func randomIdentity() Option {
|
2019-07-01 10:18:00 +00:00
|
|
|
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
|
|
|
|
if err != nil {
|
2019-07-04 15:50:48 +00:00
|
|
|
return Error(err)
|
2019-07-01 10:18:00 +00:00
|
|
|
}
|
|
|
|
|
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),
|
2019-07-01 10:18:00 +00:00
|
|
|
)
|
|
|
|
}
|