lotus/cmd/lotus/daemon.go

203 lines
4.9 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"
2020-01-21 01:53:55 +00:00
"os"
2019-07-23 22:34:13 +00:00
2020-01-02 19:08:49 +00:00
paramfetch "github.com/filecoin-project/go-paramfetch"
2020-01-21 01:53:55 +00:00
"github.com/filecoin-project/go-sectorbuilder"
blockstore "github.com/ipfs/go-ipfs-blockstore"
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"
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"
"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,
},
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",
},
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
}
if err := paramfetch.GetParams(build.ParametersJson, 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
}
2020-01-21 01:53:55 +00:00
chainfile := cctx.String("import-chain")
if chainfile != "" {
if err := ImportChain(r, chainfile); err != nil {
return err
}
2020-01-21 01:58:58 +00:00
if cctx.Bool("halt-after-import") {
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) != "" {
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
},
}
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()
ds, err := lr.Datastore("/blocks")
if err != nil {
return err
}
mds, err := lr.Datastore("/metadata")
if err != nil {
return err
}
bs := blockstore.NewBlockstore(ds)
cst := store.NewChainStore(bs, mds, vm.Syscalls(sectorbuilder.ProofVerifier))
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
}