lotus/cmd/lotus-seal-worker/main.go

203 lines
4.6 KiB
Go
Raw Normal View History

package main
2019-11-21 00:52:59 +00:00
import (
2020-03-13 01:37:38 +00:00
"context"
"net/http"
2019-11-21 00:52:59 +00:00
"os"
2020-03-13 01:37:38 +00:00
"os/signal"
"syscall"
2020-03-13 01:37:38 +00:00
"github.com/gorilla/mux"
logging "github.com/ipfs/go-log/v2"
2020-03-11 01:57:52 +00:00
"golang.org/x/xerrors"
2019-11-21 00:52:59 +00:00
"gopkg.in/urfave/cli.v2"
2020-03-11 01:57:52 +00:00
paramfetch "github.com/filecoin-project/go-paramfetch"
2020-03-13 01:37:38 +00:00
manet "github.com/multiformats/go-multiaddr-net"
2020-03-11 01:57:52 +00:00
"github.com/filecoin-project/lotus/api"
2020-03-13 01:37:38 +00:00
"github.com/filecoin-project/lotus/api/apistruct"
2019-11-21 00:52:59 +00:00
"github.com/filecoin-project/lotus/build"
2020-03-11 01:57:52 +00:00
lcli "github.com/filecoin-project/lotus/cli"
2020-03-13 01:37:38 +00:00
"github.com/filecoin-project/lotus/lib/auth"
"github.com/filecoin-project/lotus/lib/jsonrpc"
2020-01-08 13:49:34 +00:00
"github.com/filecoin-project/lotus/lib/lotuslog"
"github.com/filecoin-project/lotus/node/repo"
2020-03-13 01:37:38 +00:00
"github.com/filecoin-project/lotus/storage/sealmgr/advmgr"
"github.com/filecoin-project/lotus/storage/sealmgr/stores"
2019-11-21 00:52:59 +00:00
)
var log = logging.Logger("main")
2020-03-13 01:37:38 +00:00
const FlagStorageRepo = "workerrepo"
2020-02-04 19:04:49 +00:00
const (
workers = 1 // TODO: Configurability
transfers = 1
)
2019-11-21 00:52:59 +00:00
func main() {
2020-01-08 13:49:34 +00:00
lotuslog.SetupLogLevels()
2019-11-21 00:52:59 +00:00
log.Info("Starting lotus worker")
local := []*cli.Command{
runCmd,
}
app := &cli.App{
2019-11-22 16:25:56 +00:00
Name: "lotus-seal-worker",
2019-11-21 00:52:59 +00:00
Usage: "Remote storage miner worker",
Version: build.UserVersion,
2019-11-21 00:52:59 +00:00
Flags: []cli.Flag{
&cli.StringFlag{
2020-03-13 01:37:38 +00:00
Name: "workerrepo",
2019-11-21 00:52:59 +00:00
EnvVars: []string{"WORKER_PATH"},
Value: "~/.lotusworker", // TODO: Consider XDG_DATA_HOME
},
&cli.StringFlag{
2019-11-21 16:10:04 +00:00
Name: "storagerepo",
EnvVars: []string{"LOTUS_STORAGE_PATH"},
2019-11-21 00:52:59 +00:00
Value: "~/.lotusstorage", // TODO: Consider XDG_DATA_HOME
},
2019-12-07 14:19:46 +00:00
&cli.BoolFlag{
Name: "enable-gpu-proving",
Usage: "enable use of GPU for mining operations",
Value: true,
},
2019-11-21 00:52:59 +00:00
},
Commands: local,
}
app.Setup()
app.Metadata["repoType"] = repo.StorageMiner
2019-11-21 00:52:59 +00:00
if err := app.Run(os.Args); err != nil {
2019-11-21 18:38:43 +00:00
log.Warnf("%+v", err)
2019-11-21 00:52:59 +00:00
return
}
}
var runCmd = &cli.Command{
Name: "run",
Usage: "Start lotus worker",
2019-11-21 00:52:59 +00:00
Action: func(cctx *cli.Context) error {
2020-03-11 01:57:52 +00:00
if !cctx.Bool("enable-gpu-proving") {
os.Setenv("BELLMAN_NO_GPU", "true")
}
nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return xerrors.Errorf("getting miner api: %w", err)
}
defer closer()
ctx := lcli.ReqContext(cctx)
v, err := nodeApi.Version(ctx)
if err != nil {
return err
}
if v.APIVersion != build.APIVersion {
return xerrors.Errorf("lotus-storage-miner API version doesn't match: local: ", api.Version{APIVersion: build.APIVersion})
}
2020-03-13 01:37:38 +00:00
log.Infof("Remote version %s", v)
2020-03-11 01:57:52 +00:00
go func() {
<-ctx.Done()
log.Warn("Shutting down..")
}()
act, err := nodeApi.ActorAddress(ctx)
if err != nil {
return err
}
ssize, err := nodeApi.ActorSectorSize(ctx, act)
if err != nil {
return err
}
if err := paramfetch.GetParams(build.ParametersJson(), uint64(ssize)); err != nil {
return xerrors.Errorf("get params: %w", err)
}
2020-03-13 01:37:38 +00:00
repoPath := cctx.String(FlagStorageRepo)
r, err := repo.NewFS(repoPath)
if err != nil {
return err
}
ok, err := r.Exists()
if err != nil {
return err
}
if !ok {
return xerrors.Errorf("repo at '%s' is not initialized, run 'lotus-seal-worker init' to set it up", repoPath)
}
lr, err := r.Lock(repo.Worker)
if err != nil {
return err
}
localStore, err := stores.NewLocal(lr)
if err != nil {
return err
}
endpoint, err := r.APIEndpoint()
if err != nil {
return err
}
lst, err := manet.Listen(endpoint)
if err != nil {
return xerrors.Errorf("could not listen: %w", err)
}
_, spt, err := api.ProofTypeFromSectorSize(ssize)
if err != nil {
return xerrors.Errorf("getting proof type: %w", err)
}
sminfo, err := lcli.GetAPIInfo(cctx, repo.StorageMiner)
if err != nil {
return xerrors.Errorf("could not get api info: %w", err)
}
remote := stores.NewRemote(localStore, nodeApi, sminfo.AuthHeader())
workerApi := &worker{
LocalWorker: advmgr.NewLocalWorker(act, spt, remote, localStore, stores.NewIndex()),
2020-03-13 01:37:38 +00:00
}
mux := mux.NewRouter()
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", apistruct.PermissionedWorkerAPI(workerApi))
mux.Handle("/rpc/v0", rpcServer)
mux.PathPrefix("/remote").HandlerFunc((&stores.FetchHandler{Store: localStore}).ServeHTTP)
mux.PathPrefix("/").Handler(http.DefaultServeMux) // pprof
ah := &auth.Handler{
Verify: nodeApi.AuthVerify,
Next: mux.ServeHTTP,
}
srv := &http.Server{Handler: ah}
sigChan := make(chan os.Signal, 2)
go func() {
<-sigChan
log.Warn("Shutting down..")
if err := srv.Shutdown(context.TODO()); err != nil {
log.Errorf("shutting down RPC server failed: %s", err)
}
log.Warn("Graceful shutdown successful")
}()
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
2020-03-13 00:23:05 +00:00
2020-03-13 01:37:38 +00:00
return srv.Serve(manet.NetListener(lst))
2019-11-21 00:52:59 +00:00
},
}