2023-08-23 23:57:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2023-10-11 22:51:46 +00:00
|
|
|
"net/http"
|
2023-08-23 23:57:34 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2023-08-26 03:07:17 +00:00
|
|
|
"time"
|
2023-08-23 23:57:34 +00:00
|
|
|
|
2023-08-26 03:07:17 +00:00
|
|
|
"github.com/gin-contrib/pprof"
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-10-11 22:51:46 +00:00
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
2023-10-16 15:28:58 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-08-23 23:57:34 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"go.opencensus.io/stats"
|
|
|
|
"go.opencensus.io/tag"
|
2023-10-18 21:47:00 +00:00
|
|
|
"golang.org/x/xerrors"
|
2023-08-23 23:57:34 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-jsonrpc/auth"
|
2023-10-11 22:51:46 +00:00
|
|
|
"github.com/filecoin-project/go-statestore"
|
2023-08-26 03:25:19 +00:00
|
|
|
|
2023-08-23 23:57:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2023-10-11 22:51:46 +00:00
|
|
|
cliutil "github.com/filecoin-project/lotus/cli/util"
|
|
|
|
"github.com/filecoin-project/lotus/journal"
|
|
|
|
"github.com/filecoin-project/lotus/journal/fsjournal"
|
2023-08-23 23:57:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/harmony/harmonydb"
|
2023-08-26 03:07:17 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/harmony/harmonytask"
|
2023-08-23 23:57:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/ulimit"
|
|
|
|
"github.com/filecoin-project/lotus/metrics"
|
|
|
|
"github.com/filecoin-project/lotus/node"
|
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2023-10-11 22:51:46 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules"
|
2023-08-23 23:57:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
"github.com/filecoin-project/lotus/storage/paths"
|
2023-10-11 22:51:46 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/ffiwrapper"
|
2023-08-23 23:57:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
|
|
|
)
|
|
|
|
|
2023-10-16 15:28:58 +00:00
|
|
|
type stackTracer interface {
|
|
|
|
StackTrace() errors.StackTrace
|
|
|
|
}
|
|
|
|
|
2023-08-23 23:57:34 +00:00
|
|
|
var runCmd = &cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "Start a lotus provider process",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
2023-08-30 04:16:05 +00:00
|
|
|
Name: "listen",
|
|
|
|
Usage: "host address and port the worker api will listen on",
|
|
|
|
Value: "0.0.0.0:12300",
|
|
|
|
EnvVars: []string{"LOTUS_WORKER_LISTEN"},
|
2023-08-23 23:57:34 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "nosync",
|
|
|
|
Usage: "don't check full-node sync status",
|
|
|
|
},
|
2023-08-25 23:17:31 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "halt-after-init",
|
|
|
|
Usage: "only run init, then return",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
2023-08-23 23:57:34 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "manage-fdlimit",
|
|
|
|
Usage: "manage open file limit",
|
|
|
|
Value: true,
|
|
|
|
},
|
2023-10-18 21:47:00 +00:00
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "layers",
|
|
|
|
Usage: "list of layers to be interpreted (atop defaults). Default: base",
|
|
|
|
Value: cli.NewStringSlice("base"),
|
|
|
|
},
|
2023-08-23 23:57:34 +00:00
|
|
|
},
|
2023-10-16 15:28:58 +00:00
|
|
|
Action: func(cctx *cli.Context) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err, ok := err.(stackTracer); ok {
|
|
|
|
for _, f := range err.StackTrace() {
|
|
|
|
fmt.Printf("%+s:%d\n", f, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2023-08-23 23:57:34 +00:00
|
|
|
if !cctx.Bool("enable-gpu-proving") {
|
|
|
|
err := os.Setenv("BELLMAN_NO_GPU", "true")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, _ := tag.New(lcli.DaemonContext(cctx),
|
|
|
|
tag.Insert(metrics.Version, build.BuildVersion),
|
|
|
|
tag.Insert(metrics.Commit, build.CurrentCommit),
|
|
|
|
tag.Insert(metrics.NodeType, "provider"),
|
|
|
|
)
|
|
|
|
// Register all metric views
|
|
|
|
/*
|
|
|
|
if err := view.Register(
|
|
|
|
metrics.MinerNodeViews...,
|
|
|
|
); err != nil {
|
|
|
|
log.Fatalf("Cannot register the view: %v", err)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// Set the metric to one so it is published to the exporter
|
|
|
|
stats.Record(ctx, metrics.LotusInfo.M(1))
|
|
|
|
|
|
|
|
if cctx.Bool("manage-fdlimit") {
|
|
|
|
if _, _, err := ulimit.ManageFdLimit(); err != nil {
|
|
|
|
log.Errorf("setting file descriptor limit: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open repo
|
|
|
|
|
2023-09-27 03:06:00 +00:00
|
|
|
repoPath := cctx.String(FlagRepoPath)
|
2023-08-30 20:57:20 +00:00
|
|
|
fmt.Println("repopath", repoPath)
|
2023-08-23 23:57:34 +00:00
|
|
|
r, err := repo.NewFS(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := r.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
if err := r.Init(repo.Provider); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lr, err := r.Lock(repo.Provider)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var localPaths []storiface.LocalPath
|
|
|
|
|
|
|
|
if err := lr.SetStorage(func(sc *storiface.StorageConfig) {
|
|
|
|
sc.StoragePaths = append(sc.StoragePaths, localPaths...)
|
|
|
|
}); err != nil {
|
2023-08-26 03:07:17 +00:00
|
|
|
return fmt.Errorf("set storage config: %w", err)
|
2023-08-23 23:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// init datastore for r.Exists
|
|
|
|
_, err := lr.Datastore(context.Background(), "/metadata")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := lr.Close(); err != nil {
|
2023-08-26 03:07:17 +00:00
|
|
|
return fmt.Errorf("close repo: %w", err)
|
2023-08-23 23:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:48:39 +00:00
|
|
|
db, err := makeDB(cctx)
|
2023-08-23 23:57:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
shutdownChan := make(chan struct{})
|
|
|
|
|
2023-08-30 04:16:05 +00:00
|
|
|
/* defaults break lockedRepo (below)
|
2023-08-23 23:57:34 +00:00
|
|
|
stop, err := node.New(ctx,
|
|
|
|
node.Override(new(dtypes.ShutdownChan), shutdownChan),
|
|
|
|
node.Provider(r),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-08-26 03:07:17 +00:00
|
|
|
return fmt.Errorf("creating node: %w", err)
|
2023-08-23 23:57:34 +00:00
|
|
|
}
|
2023-08-30 04:16:05 +00:00
|
|
|
*/
|
2023-08-23 23:57:34 +00:00
|
|
|
|
|
|
|
const unspecifiedAddress = "0.0.0.0"
|
|
|
|
address := cctx.String("listen")
|
|
|
|
addressSlice := strings.Split(address, ":")
|
|
|
|
if ip := net.ParseIP(addressSlice[0]); ip != nil {
|
|
|
|
if ip.String() == unspecifiedAddress {
|
2023-08-25 23:17:31 +00:00
|
|
|
rip, err := db.GetRoutableIP()
|
2023-08-23 23:57:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
address = rip + ":" + addressSlice[1]
|
|
|
|
}
|
|
|
|
}
|
2023-08-30 04:16:05 +00:00
|
|
|
|
|
|
|
lr, err := r.Lock(repo.Provider)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := lr.Close(); err != nil {
|
|
|
|
log.Error("closing repo", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-10-18 21:47:00 +00:00
|
|
|
if err := lr.SetAPIToken([]byte(address)); err != nil { // our assigned listen address is our unique token
|
|
|
|
return xerrors.Errorf("setting api token: %w", err)
|
|
|
|
}
|
2023-08-23 23:57:34 +00:00
|
|
|
localStore, err := paths.NewLocal(ctx, lr, nil, []string{"http://" + address + "/remote"})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-16 15:28:58 +00:00
|
|
|
cfg, err := getConfig(cctx, db)
|
2023-09-20 03:48:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 22:51:46 +00:00
|
|
|
// The config feeds into task runners & their helpers
|
2023-08-23 23:57:34 +00:00
|
|
|
|
2023-10-11 22:51:46 +00:00
|
|
|
var activeTasks []harmonytask.TaskInterface
|
2023-09-05 16:29:39 +00:00
|
|
|
|
2023-10-16 15:28:58 +00:00
|
|
|
var verif storiface.Verifier = ffiwrapper.ProofVerifier
|
|
|
|
|
|
|
|
as, err := modules.AddressSelector(&cfg.Addresses)()
|
2023-10-11 22:51:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-16 15:28:58 +00:00
|
|
|
de, err := journal.ParseDisabledEvents(cfg.Journal.DisabledEvents)
|
2023-10-11 22:51:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-16 15:28:58 +00:00
|
|
|
j, err := fsjournal.OpenFSJournal(lr, de)
|
2023-10-11 22:51:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-16 15:28:58 +00:00
|
|
|
defer j.Close()
|
2023-10-11 22:51:46 +00:00
|
|
|
|
|
|
|
si := paths.NewIndexProxy( /*TODO Alerting*/ nil, db, true)
|
|
|
|
|
|
|
|
lstor, err := paths.NewLocal(ctx, lr, si, nil /*TODO URLs*/)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-19 23:19:42 +00:00
|
|
|
full, fullCloser, err := cliutil.GetFullNodeAPIV1LotusProvider(cctx, cfg.Apis.FULLNODE_API_INFO) // TODO switch this into DB entries.
|
2023-10-16 15:28:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fullCloser()
|
2023-10-18 21:47:00 +00:00
|
|
|
|
2023-10-11 22:51:46 +00:00
|
|
|
sa, err := modules.StorageAuth(ctx, full)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stor := paths.NewRemote(lstor, si, http.Header(sa), 10, &paths.DefaultPartialFileHandler{})
|
|
|
|
mds, err := lr.Datastore(ctx, "/metadata")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wsts := statestore.New(namespace.Wrap(mds, modules.WorkerCallsPrefix))
|
|
|
|
smsts := statestore.New(namespace.Wrap(mds, modules.ManagerWorkPrefix))
|
2023-10-16 15:28:58 +00:00
|
|
|
sealer, err := sealer.New(ctx, lstor, stor, lr, si, cfg.SealerConfig, config.ProvingConfig{}, wsts, smsts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ds, dsCloser, err := modules.DatastoreV2(ctx, false, lr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dsCloser()
|
|
|
|
maddr, err := modules.MinerAddress(ds)
|
2023-10-11 22:51:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-16 15:28:58 +00:00
|
|
|
if cfg.Subsystems.EnableWindowPost {
|
|
|
|
wdPostTask, err := modules.WindowPostSchedulerV2(ctx, cfg.Fees, cfg.Proving, full, sealer, verif, j,
|
|
|
|
as, maddr, db, cfg.Subsystems.WindowPostMaxTasks)
|
2023-10-11 22:51:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
activeTasks = append(activeTasks, wdPostTask)
|
|
|
|
}
|
|
|
|
taskEngine, err := harmonytask.New(db, activeTasks, address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-26 03:07:17 +00:00
|
|
|
handler := gin.New()
|
|
|
|
|
|
|
|
taskEngine.ApplyHttpHandlers(handler.Group("/"))
|
|
|
|
defer taskEngine.GracefullyTerminate(time.Hour)
|
|
|
|
|
2023-08-23 23:57:34 +00:00
|
|
|
fh := &paths.FetchHandler{Local: localStore, PfHandler: &paths.DefaultPartialFileHandler{}}
|
2023-08-26 03:07:17 +00:00
|
|
|
handler.NoRoute(gin.HandlerFunc(func(c *gin.Context) {
|
|
|
|
if !auth.HasPerm(c, nil, api.PermAdmin) {
|
|
|
|
c.JSON(401, struct{ Error string }{"unauthorized: missing admin permission"})
|
2023-08-23 23:57:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-26 03:07:17 +00:00
|
|
|
fh.ServeHTTP(c.Writer, c.Request)
|
|
|
|
}))
|
2023-08-23 23:57:34 +00:00
|
|
|
// local APIs
|
|
|
|
{
|
|
|
|
// debugging
|
2023-08-26 03:07:17 +00:00
|
|
|
handler.GET("/debug/metrics", gin.WrapH(metrics.Exporter()))
|
|
|
|
pprof.Register(handler)
|
2023-08-23 23:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serve the RPC.
|
2023-08-30 20:57:20 +00:00
|
|
|
/*
|
|
|
|
endpoint, err := r.APIEndpoint()
|
|
|
|
fmt.Println("Endpoint: ", endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting API endpoint: %w", err)
|
|
|
|
}
|
|
|
|
rpcStopper, err := node.ServeRPC(handler, "lotus-provider", endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to start json-rpc endpoint: %s", err)
|
|
|
|
}
|
|
|
|
*/
|
2023-08-23 23:57:34 +00:00
|
|
|
|
|
|
|
// Monitor for shutdown.
|
2023-08-30 20:57:20 +00:00
|
|
|
finishCh := node.MonitorShutdown(shutdownChan) //node.ShutdownHandler{Component: "rpc server", StopFunc: rpcStopper},
|
|
|
|
//node.ShutdownHandler{Component: "provider", StopFunc: stop},
|
2023-08-23 23:57:34 +00:00
|
|
|
|
|
|
|
<-finishCh
|
2023-10-11 22:51:46 +00:00
|
|
|
|
2023-08-23 23:57:34 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-09-20 03:48:39 +00:00
|
|
|
|
|
|
|
func makeDB(cctx *cli.Context) (*harmonydb.DB, error) {
|
|
|
|
dbConfig := config.HarmonyDB{
|
|
|
|
Username: cctx.String("db-user"),
|
|
|
|
Password: cctx.String("db-password"),
|
|
|
|
Hosts: strings.Split(cctx.String("db-host"), ","),
|
|
|
|
Database: cctx.String("db-name"),
|
|
|
|
Port: cctx.String("db-port"),
|
|
|
|
}
|
|
|
|
return harmonydb.NewFromConfig(dbConfig)
|
|
|
|
|
|
|
|
}
|