Skip to content

Commit

Permalink
Merge pull request #8086 from filecoin-project/nonsense/refactor-node…
Browse files Browse the repository at this point in the history
…type

refactor: convert RepoType from int to interface
  • Loading branch information
magik6k authored Mar 21, 2022
2 parents 1eb101d + 0d6493e commit fc34d9b
Show file tree
Hide file tree
Showing 14 changed files with 424 additions and 281 deletions.
3 changes: 1 addition & 2 deletions cli/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/filecoin-project/go-jsonrpc/auth"

"github.com/filecoin-project/lotus/api"
cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/filecoin-project/lotus/node/repo"
)

Expand Down Expand Up @@ -128,7 +127,7 @@ var AuthApiInfoToken = &cli.Command{

// TODO: Log in audit log when it is implemented

currentEnv, _, _ := cliutil.EnvsForAPIInfos(t)
currentEnv, _, _ := t.APIInfoEnvVars()
fmt.Printf("%s=%s:%s\n", currentEnv, string(token), ainfo.Addr)
return nil
},
Expand Down
69 changes: 5 additions & 64 deletions cli/util/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,63 +28,6 @@ const (
metadataTraceContext = "traceContext"
)

// flagsForAPI returns flags passed on the command line with the listen address
// of the API server (only used by the tests), in the order of precedence they
// should be applied for the requested kind of node.
func flagsForAPI(t repo.RepoType) []string {
switch t {
case repo.FullNode:
return []string{"api-url"}
case repo.StorageMiner:
return []string{"miner-api-url"}
case repo.Worker:
return []string{"worker-api-url"}
case repo.Markets:
// support split markets-miner and monolith deployments.
return []string{"markets-api-url", "miner-api-url"}
default:
panic(fmt.Sprintf("Unknown repo type: %v", t))
}
}

func flagsForRepo(t repo.RepoType) []string {
switch t {
case repo.FullNode:
return []string{"repo"}
case repo.StorageMiner:
return []string{"miner-repo"}
case repo.Worker:
return []string{"worker-repo"}
case repo.Markets:
// support split markets-miner and monolith deployments.
return []string{"markets-repo", "miner-repo"}
default:
panic(fmt.Sprintf("Unknown repo type: %v", t))
}
}

// EnvsForAPIInfos returns the environment variables to use in order of precedence
// to determine the API endpoint of the specified node type.
//
// It returns the current variables and deprecated ones separately, so that
// the user can log a warning when deprecated ones are found to be in use.
func EnvsForAPIInfos(t repo.RepoType) (primary string, fallbacks []string, deprecated []string) {
switch t {
case repo.FullNode:
return "FULLNODE_API_INFO", nil, nil
case repo.StorageMiner:
// TODO remove deprecated deprecation period
return "MINER_API_INFO", nil, []string{"STORAGE_API_INFO"}
case repo.Worker:
return "WORKER_API_INFO", nil, nil
case repo.Markets:
// support split markets-miner and monolith deployments.
return "MARKETS_API_INFO", []string{"MINER_API_INFO"}, nil
default:
panic(fmt.Sprintf("Unknown repo type: %v", t))
}
}

// GetAPIInfo returns the API endpoint to use for the specified kind of repo.
//
// The order of precedence is as follows:
Expand All @@ -96,8 +39,7 @@ func EnvsForAPIInfos(t repo.RepoType) (primary string, fallbacks []string, depre
func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
// Check if there was a flag passed with the listen address of the API
// server (only used by the tests)
apiFlags := flagsForAPI(t)
for _, f := range apiFlags {
for _, f := range t.APIFlags() {
if !ctx.IsSet(f) {
continue
}
Expand All @@ -111,7 +53,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
// Note: it is not correct/intuitive to prefer environment variables over
// CLI flags (repo flags below).
//
primaryEnv, fallbacksEnvs, deprecatedEnvs := EnvsForAPIInfos(t)
primaryEnv, fallbacksEnvs, deprecatedEnvs := t.APIInfoEnvVars()
env, ok := os.LookupEnv(primaryEnv)
if ok {
return ParseApiInfo(env), nil
Expand All @@ -125,8 +67,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
}
}

repoFlags := flagsForRepo(t)
for _, f := range repoFlags {
for _, f := range t.RepoFlags() {
// cannot use ctx.IsSet because it ignores default values
path := ctx.String(f)
if path == "" {
Expand Down Expand Up @@ -175,13 +116,13 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
}
}

return APIInfo{}, fmt.Errorf("could not determine API endpoint for node type: %v", t)
return APIInfo{}, fmt.Errorf("could not determine API endpoint for node type: %v", t.Type())
}

func GetRawAPI(ctx *cli.Context, t repo.RepoType, version string) (string, http.Header, error) {
ainfo, err := GetAPIInfo(ctx, t)
if err != nil {
return "", nil, xerrors.Errorf("could not get API info for %s: %w", t, err)
return "", nil, xerrors.Errorf("could not get API info for %s: %w", t.Type(), err)
}

addr, err := ainfo.DialArgs(version)
Expand Down
16 changes: 8 additions & 8 deletions cmd/lotus-shed/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ var datastoreListCmd = &cli.Command{
Name: "list",
Description: "list datastore keys",
Flags: []cli.Flag{
&cli.IntFlag{
&cli.StringFlag{
Name: "repo-type",
Usage: "node type (1 - full, 2 - storage, 3 - worker)",
Value: 1,
Usage: "node type (FullNode, StorageMiner, Worker, Wallet)",
Value: "FullNode",
},
&cli.BoolFlag{
Name: "top-level",
Expand Down Expand Up @@ -71,7 +71,7 @@ var datastoreListCmd = &cli.Command{
return xerrors.Errorf("lotus repo doesn't exist")
}

lr, err := r.Lock(repo.RepoType(cctx.Int("repo-type")))
lr, err := r.Lock(repo.NewRepoTypeFromString(cctx.String("repo-type")))
if err != nil {
return err
}
Expand Down Expand Up @@ -109,10 +109,10 @@ var datastoreGetCmd = &cli.Command{
Name: "get",
Description: "list datastore keys",
Flags: []cli.Flag{
&cli.IntFlag{
&cli.StringFlag{
Name: "repo-type",
Usage: "node type (1 - full, 2 - storage, 3 - worker)",
Value: 1,
Usage: "node type (FullNode, StorageMiner, Worker, Wallet)",
Value: "FullNode",
},
&cli.StringFlag{
Name: "enc",
Expand All @@ -137,7 +137,7 @@ var datastoreGetCmd = &cli.Command{
return xerrors.Errorf("lotus repo doesn't exist")
}

lr, err := r.Lock(repo.RepoType(cctx.Int("repo-type")))
lr, err := r.Lock(repo.NewRepoTypeFromString(cctx.String("repo-type")))
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/lotus-shed/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ var rpcCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
rt := repo.FullNode
var rt repo.RepoType
if cctx.Bool("miner") {
rt = repo.StorageMiner
} else {
rt = repo.FullNode
}

addr, headers, err := lcli.GetRawAPI(cctx, rt, cctx.String("version"))
Expand Down
3 changes: 2 additions & 1 deletion itests/batch_deal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func TestBatchDealInput(t *testing.T) {
})),
node.Override(new(dtypes.GetSealingConfigFunc), func() (dtypes.GetSealingConfigFunc, error) {
return func() (sealiface.Config, error) {
sc := modules.ToSealingConfig(config.DefaultStorageMiner())
cfg := config.DefaultStorageMiner()
sc := modules.ToSealingConfig(cfg.Dealmaking, cfg.Sealing)
sc.MaxWaitDealsSectors = 2
sc.MaxSealingSectors = 1
sc.MaxSealingSectorsForDeals = 3
Expand Down
2 changes: 1 addition & 1 deletion itests/sector_finalize_early_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestDealsWithFinalizeEarly(t *testing.T) {
return func() (sealiface.Config, error) {
cf := config.DefaultStorageMiner()
cf.Sealing.FinalizeEarly = true
return modules.ToSealingConfig(cf), nil
return modules.ToSealingConfig(cf.Dealmaking, cf.Sealing), nil
}, nil
})))) // no mock proofs.
ens.InterconnectAll().BeginMining(blockTime)
Expand Down
3 changes: 2 additions & 1 deletion itests/sector_miner_collateral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func TestMinerBalanceCollateral(t *testing.T) {
opts := kit.ConstructorOpts(
node.ApplyIf(node.IsType(repo.StorageMiner), node.Override(new(dtypes.GetSealingConfigFunc), func() (dtypes.GetSealingConfigFunc, error) {
return func() (sealiface.Config, error) {
sc := modules.ToSealingConfig(config.DefaultStorageMiner())
cfg := config.DefaultStorageMiner()
sc := modules.ToSealingConfig(cfg.Dealmaking, cfg.Sealing)

sc.MaxWaitDealsSectors = 4
sc.MaxSealingSectors = 4
Expand Down
8 changes: 6 additions & 2 deletions node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,12 @@ func IsType(t repo.RepoType) func(s *Settings) bool {
}

func isFullOrLiteNode(s *Settings) bool { return s.nodeType == repo.FullNode }
func isFullNode(s *Settings) bool { return s.nodeType == repo.FullNode && !s.Lite }
func isLiteNode(s *Settings) bool { return s.nodeType == repo.FullNode && s.Lite }
func isFullNode(s *Settings) bool {
return s.nodeType == repo.FullNode && !s.Lite
}
func isLiteNode(s *Settings) bool {
return s.nodeType == repo.FullNode && s.Lite
}

func Base() Option {
return Options(
Expand Down
4 changes: 2 additions & 2 deletions node/builder_miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ func ConfigStorageMiner(c interface{}) Option {
Override(new(dtypes.RetrievalPricingFunc), modules.RetrievalPricingFunc(cfg.Dealmaking)),

// DAG Store
Override(new(dagstore.MinerAPI), modules.NewMinerAPI),
Override(DAGStoreKey, modules.DAGStore),
Override(new(dagstore.MinerAPI), modules.NewMinerAPI(cfg.DAGStore)),
Override(DAGStoreKey, modules.DAGStore(cfg.DAGStore)),

// Markets (retrieval)
Override(new(dagstore.SectorAccessor), sectoraccessor.NewSectorAccessor),
Expand Down
27 changes: 27 additions & 0 deletions node/config/dynamic_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package config

type DealmakingConfiger interface {
GetDealmakingConfig() DealmakingConfig
SetDealmakingConfig(DealmakingConfig)
}

func (c *StorageMiner) GetDealmakingConfig() DealmakingConfig {
return c.Dealmaking
}

func (c *StorageMiner) SetDealmakingConfig(other DealmakingConfig) {
c.Dealmaking = other
}

type SealingConfiger interface {
GetSealingConfig() SealingConfig
SetSealingConfig(SealingConfig)
}

func (c *StorageMiner) GetSealingConfig() SealingConfig {
return c.Sealing
}

func (c *StorageMiner) SetSealingConfig(other SealingConfig) {
c.Sealing = other
}
Loading

0 comments on commit fc34d9b

Please sign in to comment.