diff --git a/cmd/lotus-storage-miner/init.go b/cmd/lotus-storage-miner/init.go new file mode 100644 index 000000000..618e5de1e --- /dev/null +++ b/cmd/lotus-storage-miner/init.go @@ -0,0 +1,43 @@ +package main + +import ( + "golang.org/x/xerrors" + "gopkg.in/urfave/cli.v2" + + lcli "github.com/filecoin-project/go-lotus/cli" + "github.com/filecoin-project/go-lotus/node/repo" +) + +var initCmd = &cli.Command{ + Name: "init", + Usage: "Initialize a lotus storage miner repo", + Action: func(cctx *cli.Context) error { + log.Info("Initializing lotus storage miner") + log.Info("Checking if repo exists") + + r, err := repo.NewFS(cctx.String("storagerepo")) + if err != nil { + return err + } + + if r.Exists() { + return xerrors.Errorf("repo at '%s' is already initialized", cctx.String("storagerepo")) + } + + log.Info("Trying to connect to full node RPC") + + api, err := lcli.GetAPI(cctx) + if err != nil { + return err + } + ctx := lcli.ReqContext(cctx) + + log.Info("Checking full node version") + + if err := r.Init(); err != nil { + return err + } + + return nil + }, +} diff --git a/cmd/lotus-storage-miner/run.go b/cmd/lotus-storage-miner/run.go index d4b1a2ba3..be870378f 100644 --- a/cmd/lotus-storage-miner/run.go +++ b/cmd/lotus-storage-miner/run.go @@ -1,9 +1,16 @@ package main import ( + "net/http" + + "github.com/multiformats/go-multiaddr" + "golang.org/x/xerrors" "gopkg.in/urfave/cli.v2" lcli "github.com/filecoin-project/go-lotus/cli" + "github.com/filecoin-project/go-lotus/lib/jsonrpc" + "github.com/filecoin-project/go-lotus/node" + "github.com/filecoin-project/go-lotus/node/repo" ) var RunCmd = &cli.Command{ @@ -15,6 +22,10 @@ var RunCmd = &cli.Command{ EnvVars: []string{"LOTUS_STORAGE_PATH"}, Value: "~/.lotusstorage", // TODO: Consider XDG_DATA_HOME }, + &cli.StringFlag{ + Name: "api", + Value: "2345", + }, }, Action: func(cctx *cli.Context) error { api, err := lcli.GetAPI(cctx) @@ -25,9 +36,40 @@ var RunCmd = &cli.Command{ v, err := api.Version(ctx) + r, err := repo.NewFS(cctx.String("storagerepo")) + if err != nil { + return err + } + + if !r.Exists() { + return xerrors.Errorf("repo at '%s' is not initialized, run 'lotus-storage-miner init' to set it up", cctx.String("storagerepo")) + } + + minerapi, err := node.New(ctx, + node.StorageMiner(), + 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 + } + // TODO: libp2p node log.Infof("Remote version %s", v) - return nil + + + rpcServer := jsonrpc.NewServer() + rpcServer.Register("Filecoin", minerapi) + http.Handle("/rpc/v0", rpcServer) + return http.ListenAndServe("127.0.0.1:"+cctx.String("api"), http.DefaultServeMux) }, }