Merge remote-tracking branch 'origin/devnet/7' into feat/interactive-porep

This commit is contained in:
Łukasz Magiera
2019-11-05 18:53:19 +01:00
43 changed files with 1961 additions and 1151 deletions
+71 -46
View File
@@ -15,6 +15,7 @@ import (
pubsub "github.com/libp2p/go-libp2p-pubsub"
record "github.com/libp2p/go-libp2p-record"
"go.uber.org/fx"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain"
@@ -99,11 +100,6 @@ const (
_nInvokes // keep this last
)
const (
nodeFull = iota
nodeStorageMiner
)
type Settings struct {
// modules is a map of constructors for DI
//
@@ -116,14 +112,12 @@ type Settings struct {
// type, and must be applied in correct order
invokes []fx.Option
nodeType int
nodeType repo.RepoType
Online bool // Online option applied
Config bool // Config option applied
}
var defConf = config.Default()
func defaults() []Option {
return []Option{
Override(new(helpers.MetricsCtx), context.Background),
@@ -160,10 +154,14 @@ func libp2p() Option {
Override(new(*pubsub.PubSub), lp2p.GossipSub()),
Override(PstoreAddSelfKeysKey, lp2p.PstoreAddSelfKeys),
Override(StartListeningKey, lp2p.StartListening(defConf.Libp2p.ListenAddresses)),
Override(StartListeningKey, lp2p.StartListening(config.DefaultFullNode().Libp2p.ListenAddresses)),
)
}
func isType(t repo.RepoType) func(s *Settings) bool {
return func(s *Settings) bool { return s.nodeType == t }
}
// Online sets up basic libp2p node
func Online() Option {
return Options(
@@ -180,7 +178,7 @@ func Online() Option {
// Full node
ApplyIf(func(s *Settings) bool { return s.nodeType == nodeFull },
ApplyIf(isType(repo.RepoFullNode),
// TODO: Fix offline mode
Override(new(dtypes.BootstrapPeers), modules.BuiltinBootstrap),
@@ -229,7 +227,7 @@ func Online() Option {
),
// Storage miner
ApplyIf(func(s *Settings) bool { return s.nodeType == nodeStorageMiner },
ApplyIf(func(s *Settings) bool { return s.nodeType == repo.RepoStorageMiner },
Override(new(*sectorbuilder.SectorBuilder), sectorbuilder.New),
Override(new(*sector.Store), sector.NewStore),
Override(new(*sectorblocks.SectorBlocks), sectorblocks.NewSectorBlocks),
@@ -257,7 +255,7 @@ func StorageMiner(out *api.StorageMiner) Option {
),
func(s *Settings) error {
s.nodeType = nodeStorageMiner
s.nodeType = repo.RepoStorageMiner
return nil
},
@@ -271,7 +269,7 @@ func StorageMiner(out *api.StorageMiner) Option {
}
// Config sets up constructors based on the provided Config
func Config(cfg *config.Root) Option {
func ConfigCommon(cfg *config.Common) Option {
return Options(
func(s *Settings) error { s.Config = true; return nil },
@@ -281,45 +279,71 @@ func Config(cfg *config.Root) Option {
ApplyIf(func(s *Settings) bool { return len(cfg.Libp2p.BootstrapPeers) > 0 },
Override(new(dtypes.BootstrapPeers), modules.ConfigBootstrap(cfg.Libp2p.BootstrapPeers)),
),
ApplyIf(func(s *Settings) bool { return s.nodeType == nodeFull },
Override(HeadMetricsKey, metrics.SendHeadNotifs(cfg.Metrics.Nickname)),
),
),
)
}
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)
}
func ConfigFullNode(cfg *config.FullNode) Option {
//ApplyIf(func(s *Settings) bool { return s.nodeType == repo.RepoFullNode }),
return Options(
Config(cfg),
Override(new(repo.LockedRepo), modules.LockedRepo(lr)), // module handles closing
Override(new(dtypes.MetadataDS), modules.Datastore),
Override(new(dtypes.ChainBlockstore), modules.ChainBlockstore),
Override(new(dtypes.ClientFilestore), modules.ClientFstore),
Override(new(dtypes.ClientBlockstore), modules.ClientBlockstore),
Override(new(dtypes.ClientDAG), modules.ClientDAG),
Override(new(ci.PrivKey), lp2p.PrivKey),
Override(new(ci.PubKey), ci.PrivKey.GetPublic),
Override(new(peer.ID), peer.IDFromPublicKey),
Override(new(types.KeyStore), modules.KeyStore),
Override(new(*dtypes.APIAlg), modules.APISecret),
ConfigCommon(&cfg.Common),
Override(HeadMetricsKey, metrics.SendHeadNotifs(cfg.Metrics.Nickname)),
)
}
func configFull(c interface{}) Option {
cfg, ok := c.(*config.FullNode)
if !ok {
return Error(xerrors.Errorf("invalid config from repo, got: %T", c))
}
return ConfigFullNode(cfg)
}
func configMiner(c interface{}) Option {
cfg, ok := c.(*config.StorageMiner)
if !ok {
return Error(xerrors.Errorf("invalid config from repo, got: %T", c))
}
return ConfigCommon(&cfg.Common)
}
func Repo(r repo.Repo) Option {
return func(settings *Settings) error {
lr, err := r.Lock(settings.nodeType)
if err != nil {
return err
}
c, err := lr.Config()
if err != nil {
return err
}
return Options(
Override(new(repo.LockedRepo), modules.LockedRepo(lr)), // module handles closing
ApplyIf(isType(repo.RepoFullNode), configFull(c)),
ApplyIf(isType(repo.RepoStorageMiner), configMiner(c)),
Override(new(dtypes.MetadataDS), modules.Datastore),
Override(new(dtypes.ChainBlockstore), modules.ChainBlockstore),
Override(new(dtypes.ClientFilestore), modules.ClientFstore),
Override(new(dtypes.ClientBlockstore), modules.ClientBlockstore),
Override(new(dtypes.ClientDAG), modules.ClientDAG),
Override(new(ci.PrivKey), lp2p.PrivKey),
Override(new(ci.PubKey), ci.PrivKey.GetPublic),
Override(new(peer.ID), peer.IDFromPublicKey),
Override(new(types.KeyStore), modules.KeyStore),
Override(new(*dtypes.APIAlg), modules.APISecret),
)(settings)
}
}
func FullAPI(out *api.FullNode) Option {
return func(s *Settings) error {
resAPI := &impl.FullNodeAPI{}
@@ -334,8 +358,9 @@ type StopFunc func(context.Context) error
// New builds and starts new Filecoin node
func New(ctx context.Context, opts ...Option) (StopFunc, error) {
settings := Settings{
modules: map[interface{}]fx.Option{},
invokes: make([]fx.Option, _nInvokes),
modules: map[interface{}]fx.Option{},
invokes: make([]fx.Option, _nInvokes),
nodeType: repo.RepoFullNode,
}
// apply module options in the right order