lotus/cmd/lotus-storage-miner/run.go

186 lines
4.4 KiB
Go
Raw Normal View History

2019-07-18 23:16:23 +00:00
package main
import (
2019-09-17 14:23:08 +00:00
"context"
2019-07-19 09:24:11 +00:00
"net/http"
2019-10-05 16:08:55 +00:00
_ "net/http/pprof"
"os"
2019-09-17 14:23:08 +00:00
"os/signal"
"syscall"
2019-07-19 09:24:11 +00:00
mux "github.com/gorilla/mux"
2019-07-19 09:24:11 +00:00
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/urfave/cli/v2"
2020-06-05 22:59:01 +00:00
"golang.org/x/xerrors"
2019-07-18 23:16:23 +00:00
2020-05-20 18:23:51 +00:00
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/go-jsonrpc/auth"
"github.com/filecoin-project/lotus/api"
2019-12-09 17:08:32 +00:00
"github.com/filecoin-project/lotus/api/apistruct"
"github.com/filecoin-project/lotus/build"
lcli "github.com/filecoin-project/lotus/cli"
2020-03-22 21:08:22 +00:00
"github.com/filecoin-project/lotus/lib/ulimit"
"github.com/filecoin-project/lotus/node"
"github.com/filecoin-project/lotus/node/impl"
2020-03-16 17:50:07 +00:00
"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/repo"
2019-07-18 23:16:23 +00:00
)
var runCmd = &cli.Command{
2019-07-18 23:16:23 +00:00
Name: "run",
Usage: "Start a lotus miner process",
Flags: []cli.Flag{
2020-03-27 05:19:29 +00:00
&cli.StringFlag{
Name: "api",
Usage: "2345",
},
2019-11-08 16:43:23 +00:00
&cli.BoolFlag{
Name: "enable-gpu-proving",
2019-12-04 16:53:32 +00:00
Usage: "enable use of GPU for mining operations",
Value: true,
2019-11-08 16:43:23 +00:00
},
2019-12-04 00:25:18 +00:00
&cli.BoolFlag{
Name: "nosync",
2019-12-04 16:53:32 +00:00
Usage: "don't check full-node sync status",
2019-12-04 00:25:18 +00:00
},
2020-03-24 18:00:08 +00:00
&cli.BoolFlag{
Name: "manage-fdlimit",
Usage: "manage open file limit",
Value: true,
},
},
2019-07-18 23:16:23 +00:00
Action: func(cctx *cli.Context) error {
2019-11-08 16:43:23 +00:00
if !cctx.Bool("enable-gpu-proving") {
err := os.Setenv("BELLMAN_NO_GPU", "true")
if err != nil {
return err
}
2019-11-08 16:43:23 +00:00
}
2019-10-03 18:12:30 +00:00
nodeApi, ncloser, err := lcli.GetFullNodeAPI(cctx)
2019-07-18 23:16:23 +00:00
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer ncloser()
2019-09-17 18:36:06 +00:00
ctx := lcli.DaemonContext(cctx)
2019-07-18 23:16:23 +00:00
2019-07-24 00:58:31 +00:00
v, err := nodeApi.Version(ctx)
2019-07-24 11:20:00 +00:00
if err != nil {
return err
}
2019-07-18 23:16:23 +00:00
2020-03-24 18:00:08 +00:00
if cctx.Bool("manage-fdlimit") {
if _, _, err := ulimit.ManageFdLimit(); err != nil {
log.Errorf("setting file descriptor limit: %s", err)
}
2020-03-22 21:08:22 +00:00
}
if v.APIVersion != build.FullAPIVersion {
return xerrors.Errorf("lotus-daemon API version doesn't match: expected: %s", api.Version{APIVersion: build.FullAPIVersion})
2019-11-20 19:44:38 +00:00
}
2019-12-04 00:25:18 +00:00
log.Info("Checking full node sync status")
if !cctx.Bool("nosync") {
if err := lcli.SyncWait(ctx, nodeApi); err != nil {
return xerrors.Errorf("sync wait: %w", err)
}
}
2020-07-08 10:38:59 +00:00
minerRepoPath := cctx.String(FlagMinerRepo)
r, err := repo.NewFS(minerRepoPath)
2019-07-19 09:24:11 +00:00
if err != nil {
return err
}
2019-07-23 21:54:54 +00:00
ok, err := r.Exists()
if err != nil {
return err
}
if !ok {
2020-07-08 10:38:59 +00:00
return xerrors.Errorf("repo at '%s' is not initialized, run 'lotus-miner init' to set it up", minerRepoPath)
2019-07-19 09:24:11 +00:00
}
shutdownChan := make(chan struct{})
2019-07-24 00:58:31 +00:00
var minerapi api.StorageMiner
2019-09-17 14:23:08 +00:00
stop, err := node.New(ctx,
2019-07-24 00:58:31 +00:00
node.StorageMiner(&minerapi),
node.Override(new(dtypes.ShutdownChan), shutdownChan),
2019-07-19 09:24:11 +00:00
node.Online(),
node.Repo(r),
node.ApplyIf(func(s *node.Settings) bool { return cctx.IsSet("api") },
2020-03-16 17:50:07 +00:00
node.Override(new(dtypes.APIEndpoint), func() (dtypes.APIEndpoint, error) {
return multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/" + cctx.String("api"))
})),
node.Override(new(api.FullNode), nodeApi),
2019-07-19 09:24:11 +00:00
)
if err != nil {
return err
}
endpoint, err := r.APIEndpoint()
if err != nil {
return err
}
2019-08-02 16:37:42 +00:00
// Bootstrap with full node
remoteAddrs, err := nodeApi.NetAddrsListen(ctx)
if err != nil {
return err
}
if err := minerapi.NetConnect(ctx, remoteAddrs); err != nil {
return err
}
2019-07-18 23:16:23 +00:00
log.Infof("Remote version %s", v)
2019-07-19 09:24:11 +00:00
lst, err := manet.Listen(endpoint)
if err != nil {
return xerrors.Errorf("could not listen: %w", err)
}
mux := mux.NewRouter()
2019-07-19 09:24:11 +00:00
rpcServer := jsonrpc.NewServer()
2019-12-09 17:08:32 +00:00
rpcServer.Register("Filecoin", apistruct.PermissionedStorMinerAPI(minerapi))
2019-07-24 17:09:00 +00:00
mux.Handle("/rpc/v0", rpcServer)
2019-11-21 18:38:43 +00:00
mux.PathPrefix("/remote").HandlerFunc(minerapi.(*impl.StorageMinerAPI).ServeRemote)
mux.PathPrefix("/").Handler(http.DefaultServeMux) // pprof
2019-07-24 17:09:00 +00:00
ah := &auth.Handler{
Verify: minerapi.AuthVerify,
Next: mux.ServeHTTP,
2019-07-24 17:09:00 +00:00
}
srv := &http.Server{Handler: ah}
2019-09-17 14:23:08 +00:00
sigChan := make(chan os.Signal, 2)
go func() {
select {
case sig := <-sigChan:
log.Warnw("received shutdown", "signal", sig)
case <-shutdownChan:
log.Warn("received shutdown")
}
log.Warn("Shutting down...")
2019-09-17 14:23:08 +00:00
if err := stop(context.TODO()); err != nil {
log.Errorf("graceful shutting down failed: %s", err)
}
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)
return srv.Serve(manet.NetListener(lst))
2019-07-18 23:16:23 +00:00
},
}