diff --git a/node/builder.go b/node/builder.go index bc68d532e..56fcd8b43 100644 --- a/node/builder.go +++ b/node/builder.go @@ -3,6 +3,7 @@ package node import ( "context" "errors" + "fmt" "os" "time" @@ -216,14 +217,42 @@ func Base(r repo.Repo) Option { ApplyIf(func(s *Settings) bool { return s.Config }, Error(errors.New("the Base() option must be set before Config option")), ), - - ApplyIfEnableLibP2P(r, LibP2P), - + ApplyIf(func(s *Settings) bool { result, _ := enableLibp2pNode(s, r); return result }, + LibP2P, + ), ApplyIf(isFullOrLiteNode, ChainNode), ApplyIf(IsType(repo.StorageMiner), MinerNode), ) } +func enableLibp2pNode(s *Settings, r repo.Repo) (bool, error) { + lr, err := r.Lock(s.nodeType) + if err != nil { + return false, err + } + c, err := lr.Config() + if err != nil { + return false, err + } + defer lr.Close() //nolint:errcheck + + switch s.nodeType { + case repo.FullNode: + return true, nil + case repo.StorageMiner: + cfg, ok := c.(*config.StorageMiner) + if !ok { + return false, fmt.Errorf("invalid config for repo, got: %T", c) + } + + enableLibP2P := cfg.Subsystems.EnableStorageMarket + return enableLibP2P, nil + default: + // TODO: log error + return false, errors.New("unknown repo type") + } +} + // Config sets up constructors based on the provided Config func ConfigCommon(cfg *config.Common, enableLibp2pNode bool) Option { return Options( diff --git a/node/enable_libp2p.go b/node/enable_libp2p.go deleted file mode 100644 index c091fee92..000000000 --- a/node/enable_libp2p.go +++ /dev/null @@ -1,40 +0,0 @@ -package node - -import ( - "github.com/filecoin-project/lotus/node/config" - "github.com/filecoin-project/lotus/node/repo" -) - -func ApplyIfEnableLibP2P(r repo.Repo, opts ...Option) Option { - return ApplyIf(func(settings *Settings) bool { - lr, err := r.Lock(settings.nodeType) - if err != nil { - // TODO: log error - return false - } - c, err := lr.Config() - if err != nil { - // TODO: log error - return false - } - - defer lr.Close() //nolint:errcheck - - switch settings.nodeType { - case repo.FullNode: - return true - case repo.StorageMiner: - cfg, ok := c.(*config.StorageMiner) - if !ok { - // TODO: log error - return false - } - - enableLibP2P := cfg.Subsystems.EnableStorageMarket - return enableLibP2P - default: - // TODO: log error - return false - } - }, opts...) -}