2019-07-18 23:16:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-19 09:24:11 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
"golang.org/x/xerrors"
|
2019-07-18 23:16:23 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
|
|
|
|
2019-07-24 00:58:31 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/api"
|
2019-07-18 23:16:23 +00:00
|
|
|
lcli "github.com/filecoin-project/go-lotus/cli"
|
2019-07-24 17:09:00 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/lib/auth"
|
2019-07-19 09:24:11 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/lib/jsonrpc"
|
|
|
|
"github.com/filecoin-project/go-lotus/node"
|
|
|
|
"github.com/filecoin-project/go-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",
|
|
|
|
Usage: "Start a lotus storage miner process",
|
2019-07-18 23:44:59 +00:00
|
|
|
Flags: []cli.Flag{
|
2019-07-19 09:24:11 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "api",
|
|
|
|
Value: "2345",
|
|
|
|
},
|
2019-07-18 23:44:59 +00:00
|
|
|
},
|
2019-07-18 23:16:23 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2019-07-24 00:58:31 +00:00
|
|
|
nodeApi, err := lcli.GetAPI(cctx)
|
2019-07-18 23:16:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
|
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
|
|
|
|
2019-07-23 21:50:52 +00:00
|
|
|
r, err := repo.NewFS(cctx.String(FlagStorageRepo))
|
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 {
|
2019-07-23 21:50:52 +00:00
|
|
|
return xerrors.Errorf("repo at '%s' is not initialized, run 'lotus-storage-miner init' to set it up", cctx.String(FlagStorageRepo))
|
2019-07-19 09:24:11 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 00:58:31 +00:00
|
|
|
var minerapi api.StorageMiner
|
2019-07-23 22:34:13 +00:00
|
|
|
err = node.New(ctx,
|
2019-07-24 00:58:31 +00:00
|
|
|
node.StorageMiner(&minerapi),
|
2019-07-19 09:24:11 +00:00
|
|
|
node.Online(),
|
|
|
|
node.Repo(r),
|
|
|
|
|
|
|
|
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)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-18 23:16:23 +00:00
|
|
|
// TODO: libp2p node
|
|
|
|
|
|
|
|
log.Infof("Remote version %s", v)
|
2019-07-19 09:24:11 +00:00
|
|
|
|
|
|
|
rpcServer := jsonrpc.NewServer()
|
2019-07-24 01:10:26 +00:00
|
|
|
rpcServer.Register("Filecoin", api.PermissionedStorMinerAPI(minerapi))
|
2019-07-24 17:09:00 +00:00
|
|
|
|
|
|
|
ah := &auth.Handler{
|
|
|
|
Verify: minerapi.AuthVerify,
|
|
|
|
Next: rpcServer.ServeHTTP,
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Handle("/rpc/v0", ah)
|
2019-07-19 09:24:11 +00:00
|
|
|
return http.ListenAndServe("127.0.0.1:"+cctx.String("api"), http.DefaultServeMux)
|
2019-07-18 23:16:23 +00:00
|
|
|
},
|
|
|
|
}
|