lotus/cmd/lotus/daemon.go

348 lines
8.2 KiB
Go
Raw Normal View History

2019-07-18 23:18:26 +00:00
// +build !nodaemon
package main
import (
"context"
2020-02-23 23:39:45 +00:00
"encoding/hex"
"encoding/json"
"fmt"
2019-07-24 23:23:06 +00:00
"io/ioutil"
2020-01-21 01:53:55 +00:00
"os"
"runtime/pprof"
2020-02-23 23:39:45 +00:00
"strings"
2019-07-23 22:34:13 +00:00
"github.com/filecoin-project/lotus/chain/types"
2020-01-02 19:08:49 +00:00
paramfetch "github.com/filecoin-project/go-paramfetch"
2020-01-21 01:53:55 +00:00
blockstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/mitchellh/go-homedir"
2019-07-18 23:18:26 +00:00
"github.com/multiformats/go-multiaddr"
2020-06-05 22:59:01 +00:00
"github.com/urfave/cli/v2"
"go.opencensus.io/plugin/runmetrics"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
2019-10-02 20:29:40 +00:00
"golang.org/x/xerrors"
2019-07-18 23:18:26 +00:00
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
2020-01-21 01:53:55 +00:00
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/vm"
lcli "github.com/filecoin-project/lotus/cli"
2020-02-22 11:36:22 +00:00
"github.com/filecoin-project/lotus/lib/peermgr"
"github.com/filecoin-project/lotus/metrics"
"github.com/filecoin-project/lotus/node"
"github.com/filecoin-project/lotus/node/modules"
2020-05-14 01:10:49 +00:00
"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/modules/testing"
"github.com/filecoin-project/lotus/node/repo"
2020-03-27 23:00:21 +00:00
"github.com/filecoin-project/sector-storage/ffiwrapper"
2019-07-18 23:18:26 +00:00
)
2019-07-24 22:49:37 +00:00
const (
2020-02-21 20:56:30 +00:00
makeGenFlag = "lotus-make-genesis"
2020-02-21 18:00:10 +00:00
preTemplateFlag = "genesis-template"
2019-07-24 22:49:37 +00:00
)
2020-06-02 15:09:18 +00:00
var daemonStopCmd = &cli.Command{
Name: "stop",
Usage: "Stop a running lotus daemon",
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
api, closer, err := lcli.GetAPI(cctx)
if err != nil {
return err
}
defer closer()
err = api.Shutdown(lcli.ReqContext(cctx))
if err != nil {
return err
}
return nil
2020-06-02 15:09:18 +00:00
},
}
2019-07-18 23:18:26 +00:00
// DaemonCmd is the `go-lotus daemon` command
var DaemonCmd = &cli.Command{
Name: "daemon",
Usage: "Start a lotus daemon process",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "api",
Value: "1234",
},
2019-07-24 22:49:37 +00:00
&cli.StringFlag{
Name: makeGenFlag,
Value: "",
Hidden: true,
},
2019-11-25 04:45:13 +00:00
&cli.StringFlag{
2020-02-21 18:00:10 +00:00
Name: preTemplateFlag,
2019-11-25 04:45:13 +00:00
Hidden: true,
},
2020-02-23 23:39:45 +00:00
&cli.StringFlag{
2020-02-24 17:45:42 +00:00
Name: "import-key",
Usage: "on first run, import a default key from a given file",
2020-02-23 23:39:45 +00:00
Hidden: true,
},
2019-07-24 22:49:37 +00:00
&cli.StringFlag{
Name: "genesis",
Usage: "genesis file to use for first node run",
},
&cli.BoolFlag{
Name: "bootstrap",
Value: true,
},
2020-01-21 01:53:55 +00:00
&cli.StringFlag{
Name: "import-chain",
Usage: "on first run, load chain from given file",
},
2020-01-21 01:58:58 +00:00
&cli.BoolFlag{
Name: "halt-after-import",
Usage: "halt the process after importing chain from file",
},
&cli.StringFlag{
Name: "pprof",
Usage: "specify name of file for writing cpu profile to",
},
2020-05-14 01:10:49 +00:00
&cli.StringFlag{
Name: "profile",
Usage: "specify type of node",
},
2019-07-18 23:18:26 +00:00
},
Action: func(cctx *cli.Context) error {
err := runmetrics.Enable(runmetrics.RunMetricOptions{
EnableCPU: true,
EnableMemory: true,
})
if err != nil {
return xerrors.Errorf("enabling runtime metrics: %w", err)
}
if prof := cctx.String("pprof"); prof != "" {
profile, err := os.Create(prof)
if err != nil {
return err
}
if err := pprof.StartCPUProfile(profile); err != nil {
return err
}
defer pprof.StopCPUProfile()
}
2020-05-14 01:10:49 +00:00
var isBootstrapper dtypes.Bootstrapper
switch profile := cctx.String("profile"); profile {
case "bootstrapper":
isBootstrapper = true
case "":
// do nothing
default:
return fmt.Errorf("unrecognized profile type: %q", profile)
}
ctx, _ := tag.New(context.Background(), tag.Insert(metrics.Version, build.BuildVersion), tag.Insert(metrics.Commit, build.CurrentCommit))
{
dir, err := homedir.Expand(cctx.String("repo"))
if err != nil {
log.Warnw("could not expand repo location", "error", err)
} else {
log.Infof("lotus repo: %s", dir)
}
}
2019-07-18 23:18:26 +00:00
r, err := repo.NewFS(cctx.String("repo"))
if err != nil {
2019-11-27 17:10:34 +00:00
return xerrors.Errorf("opening fs repo: %w", err)
2019-07-18 23:18:26 +00:00
}
2019-11-12 17:59:38 +00:00
if err := r.Init(repo.FullNode); err != nil && err != repo.ErrRepoExists {
2019-11-27 17:10:34 +00:00
return xerrors.Errorf("repo init error: %w", err)
2019-07-18 23:18:26 +00:00
}
if err := paramfetch.GetParams(lcli.ReqContext(cctx), build.ParametersJSON(), 0); err != nil {
2019-10-02 17:20:30 +00:00
return xerrors.Errorf("fetching proof parameters: %w", err)
}
2020-04-21 19:46:50 +00:00
var genBytes []byte
2019-07-24 22:49:37 +00:00
if cctx.String("genesis") != "" {
2019-10-02 17:20:30 +00:00
genBytes, err = ioutil.ReadFile(cctx.String("genesis"))
2019-07-24 23:23:06 +00:00
if err != nil {
2019-11-27 17:10:34 +00:00
return xerrors.Errorf("reading genesis: %w", err)
2019-07-24 23:23:06 +00:00
}
2020-04-21 19:46:50 +00:00
} else {
genBytes = build.MaybeGenesis()
2019-10-02 17:20:30 +00:00
}
2020-01-21 01:53:55 +00:00
chainfile := cctx.String("import-chain")
if chainfile != "" {
chainfile, err := homedir.Expand(chainfile)
if err != nil {
return err
}
2020-01-21 01:53:55 +00:00
if err := ImportChain(r, chainfile); err != nil {
return err
}
2020-01-21 01:58:58 +00:00
if cctx.Bool("halt-after-import") {
fmt.Println("Chain import complete, halting as requested...")
2020-01-21 01:58:58 +00:00
return nil
}
2020-01-21 01:53:55 +00:00
}
2019-10-02 17:20:30 +00:00
genesis := node.Options()
if len(genBytes) > 0 {
2019-07-24 23:23:06 +00:00
genesis = node.Override(new(modules.Genesis), modules.LoadGenesis(genBytes))
2019-07-24 22:49:37 +00:00
}
2019-10-02 17:20:30 +00:00
if cctx.String(makeGenFlag) != "" {
2020-02-21 18:00:10 +00:00
if cctx.String(preTemplateFlag) == "" {
return xerrors.Errorf("must also pass file with genesis template to `--%s`", preTemplateFlag)
2019-11-25 04:45:13 +00:00
}
2020-02-21 18:00:10 +00:00
genesis = node.Override(new(modules.Genesis), testing.MakeGenesis(cctx.String(makeGenFlag), cctx.String(preTemplateFlag)))
2019-10-02 17:20:30 +00:00
}
2019-07-24 22:49:37 +00:00
shutdownChan := make(chan struct{})
2019-07-24 00:09:34 +00:00
var api api.FullNode
2019-09-17 14:23:08 +00:00
stop, err := node.New(ctx,
2019-07-23 22:34:13 +00:00
node.FullAPI(&api),
2020-05-14 01:10:49 +00:00
node.Override(new(dtypes.Bootstrapper), isBootstrapper),
node.Override(new(dtypes.ShutdownChan), shutdownChan),
2019-07-18 23:18:26 +00:00
node.Online(),
node.Repo(r),
2019-07-24 22:49:37 +00:00
genesis,
node.ApplyIf(func(s *node.Settings) bool { return cctx.IsSet("api") },
node.Override(node.SetApiEndpointKey, func(lr repo.LockedRepo) error {
apima, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/" +
cctx.String("api"))
if err != nil {
return err
}
return lr.SetAPIEndpoint(apima)
})),
node.ApplyIf(func(s *node.Settings) bool { return !cctx.Bool("bootstrap") },
node.Unset(node.RunPeerMgrKey),
node.Unset(new(*peermgr.PeerMgr)),
2019-10-11 00:31:06 +00:00
),
2019-07-18 23:18:26 +00:00
)
if err != nil {
2019-11-27 17:10:34 +00:00
return xerrors.Errorf("initializing node: %w", err)
2019-07-18 23:18:26 +00:00
}
2020-02-23 23:39:45 +00:00
if cctx.String("import-key") != "" {
if err := importKey(ctx, api, cctx.String("import-key")); err != nil {
log.Errorf("importing key failed: %+v", err)
}
}
// Register all metric views
if err = view.Register(
metrics.DefaultViews...,
); 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))
endpoint, err := r.APIEndpoint()
if err != nil {
2019-11-27 17:10:34 +00:00
return xerrors.Errorf("getting api endpoint: %w", err)
}
2019-07-18 23:18:26 +00:00
// TODO: properly parse api endpoint (or make it a URL)
return serveRPC(api, stop, endpoint, shutdownChan)
2019-07-18 23:18:26 +00:00
},
2020-06-02 15:09:18 +00:00
Subcommands: []*cli.Command{
daemonStopCmd,
2019-07-18 23:18:26 +00:00
},
}
2020-01-21 01:53:55 +00:00
2020-02-23 23:39:45 +00:00
func importKey(ctx context.Context, api api.FullNode, f string) error {
f, err := homedir.Expand(f)
if err != nil {
return err
}
hexdata, err := ioutil.ReadFile(f)
if err != nil {
return err
}
data, err := hex.DecodeString(strings.TrimSpace(string(hexdata)))
if err != nil {
return err
}
var ki types.KeyInfo
if err := json.Unmarshal(data, &ki); err != nil {
return err
}
addr, err := api.WalletImport(ctx, &ki)
if err != nil {
return err
}
if err := api.WalletSetDefault(ctx, addr); err != nil {
return err
}
log.Info("successfully imported key for %s", addr)
return nil
}
2020-01-21 01:53:55 +00:00
func ImportChain(r repo.Repo, fname string) error {
fi, err := os.Open(fname)
if err != nil {
return err
}
lr, err := r.Lock(repo.FullNode)
if err != nil {
return err
}
defer lr.Close() //nolint:errcheck
2020-01-21 01:53:55 +00:00
ds, err := lr.Datastore("/chain")
2020-01-21 01:53:55 +00:00
if err != nil {
return err
}
mds, err := lr.Datastore("/metadata")
if err != nil {
return err
}
bs := blockstore.NewBlockstore(ds)
2020-03-26 02:50:56 +00:00
cst := store.NewChainStore(bs, mds, vm.Syscalls(ffiwrapper.ProofVerifier))
2020-01-21 01:53:55 +00:00
log.Info("importing chain from file...")
ts, err := cst.Import(fi)
if err != nil {
return xerrors.Errorf("importing chain failed: %w", err)
}
stm := stmgr.NewStateManager(cst)
log.Infof("validating imported chain...")
if err := stm.ValidateChain(context.TODO(), ts); err != nil {
return xerrors.Errorf("chain validation failed: %w", err)
}
log.Info("accepting %s as new head", ts.Cids())
if err := cst.SetHead(ts); err != nil {
return err
}
return nil
}