Skip to content

Commit

Permalink
refactor: config & builder daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
ta0li committed Jun 21, 2022
1 parent 5263612 commit 7afae7a
Show file tree
Hide file tree
Showing 43 changed files with 365 additions and 1,269 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:
description: gotestsum format. https://github.com/gotestyourself/gotestsum#format
coverage:
type: string
default: -coverprofile=coverage.txt -coverpkg=github.com/filecoin-project/venus-miner/...
default: -coverprofile=coverage.txt -coverpkg=./...
description: Coverage flag. Set to the empty string to disable.
codecov-upload:
type: boolean
Expand All @@ -131,7 +131,7 @@ jobs:
- install-deps
- prepare
- run:
command: make deps miner
command: make deps
no_output_timeout: 30m
- run:
name: go test
Expand Down
5 changes: 0 additions & 5 deletions api/api_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ type APIVersion struct {
//
// See APIVersion in build/version.go
APIVersion api.Version

// TODO: git commit / os / genesis cid?

// Seconds
BlockDelay uint64
}

func (v APIVersion) String() string {
Expand Down
16 changes: 7 additions & 9 deletions api/api_miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ package api

import (
"context"
"github.com/filecoin-project/venus-miner/types"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"

"github.com/filecoin-project/venus-miner/node/modules/dtypes"
)

type MinerAPI interface {
Common

UpdateAddress(context.Context, int64, int64) ([]dtypes.MinerInfo, error) //perm:write
ListAddress(context.Context) ([]dtypes.MinerInfo, error) //perm:read
StatesForMining(context.Context, []address.Address) ([]dtypes.MinerState, error) //perm:read
CountWinners(context.Context, []address.Address, abi.ChainEpoch, abi.ChainEpoch) ([]dtypes.CountWinners, error) //perm:read
Start(context.Context, []address.Address) error //perm:admin
Stop(context.Context, []address.Address) error //perm:admin
AddAddress(context.Context, dtypes.MinerInfo) error //perm:admin
UpdateAddress(context.Context, int64, int64) ([]types.MinerInfo, error) //perm:write
ListAddress(context.Context) ([]types.MinerInfo, error) //perm:read
StatesForMining(context.Context, []address.Address) ([]types.MinerState, error) //perm:read
CountWinners(context.Context, []address.Address, abi.ChainEpoch, abi.ChainEpoch) ([]types.CountWinners, error) //perm:read
Start(context.Context, []address.Address) error //perm:admin
Stop(context.Context, []address.Address) error //perm:admin
}
55 changes: 21 additions & 34 deletions api/proxy_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions build/params.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build !testground
// +build !testground

package build

var (
Expand Down
78 changes: 17 additions & 61 deletions cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"os"
"os/signal"
"strings"
"syscall"

logging "github.com/ipfs/go-log/v2"
Expand All @@ -17,11 +16,10 @@ import (

"github.com/filecoin-project/venus-miner/api"
"github.com/filecoin-project/venus-miner/api/client"
cliutil "github.com/filecoin-project/venus-miner/cli/util"
"github.com/filecoin-project/venus-miner/node/config"
"github.com/filecoin-project/venus-miner/node/repo"

"github.com/filecoin-project/venus/venus-shared/api/chain/v1"
v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
)

var log = logging.Logger("cli")
Expand All @@ -30,77 +28,35 @@ const (
metadataTraceContext = "traceContext"
)

// custom CLI error

type ErrCmdFailed struct {
msg string
}

func (e *ErrCmdFailed) Error() string {
return e.msg
}

// The flag passed on the command line with the listen address of the API
// server (only used by the tests)
func flagForAPI(t repo.RepoType) string {
switch t {
case repo.Miner:
return "miner-api-url"
default:
panic(fmt.Sprintf("Unknown repo type: %v", t))
}
}

func flagForRepo(t repo.RepoType) string {
switch t {
case repo.Miner:
return "miner-repo"
default:
panic(fmt.Sprintf("Unknown repo type: %v", t))
}
}

func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (cliutil.APIInfo, error) {
// Check if there was a flag passed with the listen address of the API
// server (only used by the tests)
apiFlag := flagForAPI(t)
if ctx.IsSet(apiFlag) {
strma := ctx.String(apiFlag)
strma = strings.TrimSpace(strma)

return cliutil.APIInfo{Addr: strma}, nil
}

repoFlag := flagForRepo(t)

p, err := homedir.Expand(ctx.String(repoFlag))
func GetAPIInfo(ctx *cli.Context) (config.APIInfo, error) {
p, err := homedir.Expand(ctx.String("miner-repo"))
if err != nil {
return cliutil.APIInfo{}, fmt.Errorf("could not expand home dir (%s): %w", repoFlag, err)
return config.APIInfo{}, fmt.Errorf("could not expand home dir: %w", err)
}

r, err := repo.NewFS(p)
if err != nil {
return cliutil.APIInfo{}, fmt.Errorf("could not open repo at path: %s; %w", p, err)
return config.APIInfo{}, fmt.Errorf("could not open repo at path: %s; %w", p, err)
}

ma, err := r.APIEndpoint()
if err != nil {
return cliutil.APIInfo{}, fmt.Errorf("could not get api endpoint: %w", err)
return config.APIInfo{}, fmt.Errorf("could not get api endpoint: %w", err)
}

token, err := r.APIToken()
if err != nil {
log.Warnf("Couldn't load CLI token, capabilities may be limited: %v", err)
}

return cliutil.APIInfo{
return config.APIInfo{
Addr: ma.String(),
Token: token,
}, nil
}

func GetRawAPI(ctx *cli.Context, t repo.RepoType, version string) (string, http.Header, error) {
ainfo, err := GetAPIInfo(ctx, t)
func GetRawAPI(ctx *cli.Context, version string) (string, http.Header, error) {
ainfo, err := GetAPIInfo(ctx)
if err != nil {
return "", nil, fmt.Errorf("could not get API info: %w", err)
}
Expand All @@ -113,22 +69,22 @@ func GetRawAPI(ctx *cli.Context, t repo.RepoType, version string) (string, http.
return addr, ainfo.AuthHeader(), nil
}

func GetFullNodeAPI(ctx *cli.Context, fn config.FullNode, version string) (v1.FullNode, jsonrpc.ClientCloser, error) {
addr, err := fn.DialArgs(version)
func GetMinerAPI(ctx *cli.Context) (api.MinerAPI, jsonrpc.ClientCloser, error) {
addr, headers, err := GetRawAPI(ctx, "v0")
if err != nil {
return nil, nil, fmt.Errorf("could not get DialArgs: %w", err)
return nil, nil, err
}

return v1.NewFullNodeRPC(ctx.Context, addr, fn.AuthHeader())
return client.NewMinerRPC(ctx.Context, addr, headers)
}

func GetMinerAPI(ctx *cli.Context) (api.MinerAPI, jsonrpc.ClientCloser, error) {
addr, headers, err := GetRawAPI(ctx, repo.Miner, "v0")
func GetFullNodeAPI(ctx *cli.Context, fn config.FullNode, version string) (v1.FullNode, jsonrpc.ClientCloser, error) {
addr, err := fn.DialArgs(version)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("could not get DialArgs: %w", err)
}

return client.NewMinerRPC(ctx.Context, addr, headers)
return v1.NewFullNodeRPC(ctx.Context, addr, fn.AuthHeader())
}

func DaemonContext(cctx *cli.Context) context.Context {
Expand Down
44 changes: 0 additions & 44 deletions cmd/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var addressCmd = &cli.Command{
stateCmd,
startMiningCmd,
stopMiningCmd,
addCmd,
},
}

Expand Down Expand Up @@ -62,49 +61,6 @@ var updateCmd = &cli.Command{
},
}

var addCmd = &cli.Command{
Name: "add",
Usage: "add a miner",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "miner",
Required: true,
},
&cli.StringFlag{
Name: "id",
Required: false,
},
&cli.StringFlag{
Name: "name",
Required: false,
},
},
Action: func(cctx *cli.Context) error {
mi := dtypes.MinerInfo{Id: cctx.String("id"), Name: cctx.String("name")}

addr, err := address.NewFromString(cctx.String("miner"))
if err != nil {
return nil
}
mi.Addr = addr

postApi, closer, err := lcli.GetMinerAPI(cctx)
if err != nil {
return err
}
defer closer()

err = postApi.AddAddress(cctx.Context, mi)
if err != nil {
return err
}

fmt.Println("add miner success.")
return nil

},
}

var listCmd = &cli.Command{
Name: "list",
Usage: "print miners",
Expand Down
Loading

0 comments on commit 7afae7a

Please sign in to comment.