2019-07-18 23:16:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-09-17 14:23:08 +00:00
|
|
|
"context"
|
2020-10-21 08:37:50 +00:00
|
|
|
"net"
|
2019-07-19 09:24:11 +00:00
|
|
|
"net/http"
|
2019-10-05 16:08:55 +00:00
|
|
|
_ "net/http/pprof"
|
2019-07-27 12:18:36 +00:00
|
|
|
"os"
|
2019-09-17 14:23:08 +00:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2019-07-19 09:24:11 +00:00
|
|
|
|
2021-04-06 11:36:16 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v1api"
|
|
|
|
|
2021-04-05 17:56:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
|
|
|
|
2019-11-21 14:10:51 +00:00
|
|
|
mux "github.com/gorilla/mux"
|
2019-07-19 09:24:11 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2020-08-25 23:46:31 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2020-06-02 18:12:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-02-21 10:03:00 +00:00
|
|
|
"go.opencensus.io/stats"
|
2020-10-21 08:37:50 +00:00
|
|
|
"go.opencensus.io/stats/view"
|
|
|
|
"go.opencensus.io/tag"
|
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"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-11-06 06:26:50 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-18 04:47:41 +00:00
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2021-05-20 10:36:24 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/rpcenc"
|
2020-03-22 21:08:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/ulimit"
|
2020-10-21 08:37:50 +00:00
|
|
|
"github.com/filecoin-project/lotus/metrics"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/node"
|
2019-11-21 14:10:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/impl"
|
2020-03-16 17:50:07 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2019-07-18 23:16:23 +00:00
|
|
|
)
|
|
|
|
|
2019-07-19 10:15:22 +00:00
|
|
|
var runCmd = &cli.Command{
|
2019-07-18 23:16:23 +00:00
|
|
|
Name: "run",
|
2020-07-11 08:55:13 +00:00
|
|
|
Usage: "Start a lotus miner process",
|
2019-07-18 23:44:59 +00:00
|
|
|
Flags: []cli.Flag{
|
2020-03-27 05:19:29 +00:00
|
|
|
&cli.StringFlag{
|
2020-10-06 14:29:16 +00:00
|
|
|
Name: "miner-api",
|
2020-03-27 05:19:29 +00:00
|
|
|
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",
|
2019-11-29 19:11:01 +00:00
|
|
|
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:44:59 +00:00
|
|
|
},
|
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") {
|
2020-08-20 04:49:10 +00:00
|
|
|
err := os.Setenv("BELLMAN_NO_GPU", "true")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-08 16:43:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 10:03:00 +00:00
|
|
|
ctx, _ := tag.New(lcli.DaemonContext(cctx),
|
|
|
|
tag.Insert(metrics.Version, build.BuildVersion),
|
|
|
|
tag.Insert(metrics.Commit, build.CurrentCommit),
|
|
|
|
tag.Insert(metrics.NodeType, "miner"),
|
|
|
|
)
|
2020-10-21 08:37:50 +00:00
|
|
|
// Register all metric views
|
2021-04-15 22:19:26 +00:00
|
|
|
if err := view.Register(
|
2021-02-21 10:03:00 +00:00
|
|
|
metrics.MinerNodeViews...,
|
2020-10-21 08:37:50 +00:00
|
|
|
); err != nil {
|
|
|
|
log.Fatalf("Cannot register the view: %v", err)
|
|
|
|
}
|
2021-02-21 10:03:00 +00:00
|
|
|
// Set the metric to one so it is published to the exporter
|
|
|
|
stats.Record(ctx, metrics.LotusInfo.M(1))
|
2020-10-21 08:37:50 +00:00
|
|
|
|
2021-04-15 22:19:26 +00:00
|
|
|
if err := checkV1ApiSupport(ctx, cctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeApi, ncloser, err := lcli.GetFullNodeAPIV1(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting full node api: %w", err)
|
|
|
|
}
|
|
|
|
defer ncloser()
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-15 22:19:26 +00:00
|
|
|
if v.APIVersion != api.FullAPIVersion1 {
|
|
|
|
return xerrors.Errorf("lotus-daemon API version doesn't match: expected: %s", api.APIVersion{APIVersion: api.FullAPIVersion1})
|
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") {
|
2021-04-05 17:56:53 +00:00
|
|
|
if err := lcli.SyncWait(ctx, &v0api.WrapperV1Full{FullNode: nodeApi}, false); err != nil {
|
2019-12-04 00:25:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-02 19:31:21 +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),
|
2020-06-02 19:35:11 +00:00
|
|
|
node.Override(new(dtypes.ShutdownChan), shutdownChan),
|
2019-07-19 09:24:11 +00:00
|
|
|
node.Online(),
|
|
|
|
node.Repo(r),
|
|
|
|
|
2020-10-06 14:29:16 +00:00
|
|
|
node.ApplyIf(func(s *node.Settings) bool { return cctx.IsSet("miner-api") },
|
2020-03-16 17:50:07 +00:00
|
|
|
node.Override(new(dtypes.APIEndpoint), func() (dtypes.APIEndpoint, error) {
|
2020-10-06 14:29:16 +00:00
|
|
|
return multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/" + cctx.String("miner-api"))
|
2019-12-09 12:36:56 +00:00
|
|
|
})),
|
2021-04-05 17:56:53 +00:00
|
|
|
node.Override(new(v1api.FullNode), nodeApi),
|
2019-07-19 09:24:11 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-10-06 14:29:16 +00:00
|
|
|
return xerrors.Errorf("creating node: %w", err)
|
2019-07-19 09:24:11 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 18:31:17 +00:00
|
|
|
endpoint, err := r.APIEndpoint()
|
|
|
|
if err != nil {
|
2020-10-06 14:29:16 +00:00
|
|
|
return xerrors.Errorf("getting API endpoint: %w", err)
|
2019-11-12 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 16:37:42 +00:00
|
|
|
// Bootstrap with full node
|
|
|
|
remoteAddrs, err := nodeApi.NetAddrsListen(ctx)
|
|
|
|
if err != nil {
|
2020-10-06 14:29:16 +00:00
|
|
|
return xerrors.Errorf("getting full node libp2p address: %w", err)
|
2019-08-02 16:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := minerapi.NetConnect(ctx, remoteAddrs); err != nil {
|
2020-10-06 14:29:16 +00:00
|
|
|
return xerrors.Errorf("connecting to full node (libp2p): %w", err)
|
2019-08-02 16:37:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 23:16:23 +00:00
|
|
|
log.Infof("Remote version %s", v)
|
2019-07-19 09:24:11 +00:00
|
|
|
|
2019-11-12 18:31:17 +00:00
|
|
|
lst, err := manet.Listen(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("could not listen: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:10:51 +00:00
|
|
|
mux := mux.NewRouter()
|
|
|
|
|
2021-05-20 10:36:24 +00:00
|
|
|
readerHandler, readerServerOpt := rpcenc.ReaderParamDecoder()
|
|
|
|
rpcServer := jsonrpc.NewServer(readerServerOpt)
|
2021-03-25 14:09:50 +00:00
|
|
|
rpcServer.Register("Filecoin", api.PermissionedStorMinerAPI(metrics.MetricedStorMinerAPI(minerapi)))
|
2019-07-24 17:09:00 +00:00
|
|
|
|
2019-11-21 14:10:51 +00:00
|
|
|
mux.Handle("/rpc/v0", rpcServer)
|
2021-05-20 10:36:24 +00:00
|
|
|
mux.Handle("/rpc/streams/v0/push/{uuid}", readerHandler)
|
2019-11-21 18:38:43 +00:00
|
|
|
mux.PathPrefix("/remote").HandlerFunc(minerapi.(*impl.StorageMinerAPI).ServeRemote)
|
2021-02-21 10:03:00 +00:00
|
|
|
mux.Handle("/debug/metrics", metrics.Exporter())
|
2019-11-21 18:38:43 +00:00
|
|
|
mux.PathPrefix("/").Handler(http.DefaultServeMux) // pprof
|
2019-11-21 14:10:51 +00:00
|
|
|
|
2019-07-24 17:09:00 +00:00
|
|
|
ah := &auth.Handler{
|
|
|
|
Verify: minerapi.AuthVerify,
|
2019-11-21 14:10:51 +00:00
|
|
|
Next: mux.ServeHTTP,
|
2019-07-24 17:09:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 08:37:50 +00:00
|
|
|
srv := &http.Server{
|
|
|
|
Handler: ah,
|
|
|
|
BaseContext: func(listener net.Listener) context.Context {
|
|
|
|
ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-miner"))
|
|
|
|
return ctx
|
|
|
|
},
|
|
|
|
}
|
2019-09-17 14:23:08 +00:00
|
|
|
|
|
|
|
sigChan := make(chan os.Signal, 2)
|
|
|
|
go func() {
|
2020-06-02 19:31:21 +00:00
|
|
|
select {
|
2020-09-18 18:07:58 +00:00
|
|
|
case sig := <-sigChan:
|
|
|
|
log.Warnw("received shutdown", "signal", sig)
|
2020-06-02 19:31:21 +00:00
|
|
|
case <-shutdownChan:
|
2020-09-18 18:07:58 +00:00
|
|
|
log.Warn("received shutdown")
|
2020-06-02 19:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2019-11-12 18:31:17 +00:00
|
|
|
return srv.Serve(manet.NetListener(lst))
|
2019-07-18 23:16:23 +00:00
|
|
|
},
|
|
|
|
}
|