lotus/cmd/lotus/daemon.go

132 lines
3.3 KiB
Go
Raw Normal View History

2019-07-18 23:18:26 +00:00
// +build !nodaemon
package main
import (
"context"
2019-07-24 23:23:06 +00:00
"io/ioutil"
2019-07-23 22:34:13 +00:00
2020-01-02 19:08:49 +00:00
paramfetch "github.com/filecoin-project/go-paramfetch"
2019-07-18 23:18:26 +00:00
"github.com/multiformats/go-multiaddr"
2019-10-02 20:29:40 +00:00
"golang.org/x/xerrors"
2019-07-18 23:18:26 +00:00
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/node"
"github.com/filecoin-project/lotus/node/modules"
"github.com/filecoin-project/lotus/node/modules/testing"
"github.com/filecoin-project/lotus/node/repo"
2020-01-02 19:08:49 +00:00
"github.com/filecoin-project/lotus/peermgr"
2019-07-18 23:18:26 +00:00
)
2019-07-24 22:49:37 +00:00
const (
2019-11-25 04:45:13 +00:00
makeGenFlag = "lotus-make-random-genesis"
preSealedSectorsFlag = "genesis-presealed-sectors"
2019-07-24 22:49:37 +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{
Name: preSealedSectorsFlag,
2019-11-25 04:45:13 +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.StringFlag{
Name: "genesis-timestamp",
Hidden: true,
Usage: "set the timestamp for the genesis block that will be created",
},
&cli.BoolFlag{
Name: "bootstrap",
Value: true,
},
2019-07-18 23:18:26 +00:00
},
Action: func(cctx *cli.Context) error {
ctx := context.Background()
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
}
2020-01-02 19:08:49 +00:00
if err := paramfetch.GetParams(0); err != nil {
2019-10-02 17:20:30 +00:00
return xerrors.Errorf("fetching proof parameters: %w", err)
}
2019-10-02 20:29:40 +00:00
genBytes := build.MaybeGenesis()
2019-10-02 17:20:30 +00:00
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
}
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) != "" {
2019-11-25 04:45:13 +00:00
if cctx.String(preSealedSectorsFlag) == "" {
return xerrors.Errorf("must also pass file with miner preseal info to `--%s`", preSealedSectorsFlag)
}
genesis = node.Override(new(modules.Genesis), testing.MakeGenesis(cctx.String(makeGenFlag), cctx.String(preSealedSectorsFlag), cctx.String("genesis-timestamp")))
2019-10-02 17:20:30 +00:00
}
2019-07-24 22:49:37 +00:00
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),
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
}
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)
2019-07-18 23:18:26 +00:00
},
}