Begin implementing storage miner

This commit is contained in:
Łukasz Magiera 2019-07-19 01:16:23 +02:00
parent c394e3c2aa
commit b73f29286b
17 changed files with 109 additions and 142 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
lotus
lotus-storage-miner
**/*.h
**/*.a
**/*.pc

View File

@ -44,6 +44,7 @@ deps: $(BUILD_DEPS)
build: $(BUILD_DEPS)
go build -o lotus ./cmd/lotus
go build -o lotus-storage-miner ./cmd/lotus-storage-miner
.PHONY: build
clean:

View File

@ -23,11 +23,11 @@ var chainHeadCmd = &cli.Command{
Name: "head",
Usage: "Print chain head",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
head, err := api.ChainHead(ctx)
if err != nil {
@ -51,11 +51,11 @@ var chainGetBlock = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
if !cctx.Args().Present() {
return fmt.Errorf("must pass cid of block to print")

View File

@ -19,11 +19,11 @@ var clientImportCmd = &cli.Command{
Name: "import",
Usage: "Import data",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
c, err := api.ClientImport(ctx, cctx.Args().First())
if err != nil {
@ -38,11 +38,11 @@ var clientLocalCmd = &cli.Command{
Name: "local",
Usage: "List locally imported data",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
list, err := api.ClientListImports(ctx)
if err != nil {

View File

@ -25,7 +25,7 @@ const (
// ApiConnector returns API instance
type ApiConnector func() api.API
func getAPI(ctx *cli.Context) (api.API, error) {
func GetAPI(ctx *cli.Context) (api.API, error) {
r, err := repo.NewFS(ctx.String("repo"))
if err != nil {
return nil, err
@ -51,10 +51,10 @@ func getAPI(ctx *cli.Context) (api.API, error) {
return client.NewRPC("ws://"+addr+"/rpc/v0", headers)
}
// reqContext returns context for cli execution. Calling it for the first time
// ReqContext returns context for cli execution. Calling it for the first time
// installs SIGTERM handler that will close returned context.
// Not safe for concurrent execution.
func reqContext(cctx *cli.Context) context.Context {
func ReqContext(cctx *cli.Context) context.Context {
if uctx, ok := cctx.App.Metadata[metadataContext]; ok {
// unchecked cast as if something else is in there
// it is crash worthy either way

View File

@ -23,7 +23,7 @@ var createMinerCmd = &cli.Command{
return fmt.Errorf("must pass four arguments: worker address, owner address, sector size, peer ID")
}
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
@ -57,7 +57,7 @@ var createMinerCmd = &cli.Command{
PeerID: pid,
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
addr, err := api.WalletDefaultAddress(ctx)
if err != nil {
return xerrors.Errorf("failed to get default address: %w", err)

View File

@ -21,12 +21,12 @@ var minerStart = &cli.Command{
Name: "start",
Usage: "start mining",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
// TODO: this address needs to be the address of an actual miner
maddr, err := address.NewIDAddress(523423423)

View File

@ -18,12 +18,12 @@ var mpoolPending = &cli.Command{
Name: "pending",
Usage: "Get pending messages",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
msgs, err := api.MpoolPending(ctx, nil)
if err != nil {

View File

@ -27,11 +27,11 @@ var netPeers = &cli.Command{
Name: "peers",
Usage: "Print peers",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
peers, err := api.NetPeers(ctx)
if err != nil {
return err
@ -49,11 +49,11 @@ var netListen = &cli.Command{
Name: "listen",
Usage: "List listen addresses",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
addrs, err := api.NetAddrsListen(ctx)
if err != nil {
@ -71,11 +71,11 @@ var netConnect = &cli.Command{
Name: "connect",
Usage: "Connect to a peer",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
pis, err := parseAddresses(ctx, cctx.Args().Slice())
if err != nil {
@ -100,12 +100,12 @@ var netId = &cli.Command{
Name: "id",
Usage: "Get node identity",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
pid, err := api.ID(ctx)
if err != nil {

View File

@ -10,12 +10,12 @@ var versionCmd = &cli.Command{
Name: "version",
Usage: "Print version",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
// TODO: print more useful things
fmt.Println(api.Version(ctx))

View File

@ -21,11 +21,11 @@ var walletNew = &cli.Command{
Name: "new",
Usage: "Generate a new key of the given type (bls or secp256k1)",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
t := cctx.Args().First()
if t == "" {
@ -47,11 +47,11 @@ var walletList = &cli.Command{
Name: "list",
Usage: "List wallet address",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
addrs, err := api.WalletList(ctx)
if err != nil {
@ -69,11 +69,11 @@ var walletBalance = &cli.Command{
Name: "balance",
Usage: "get account balance",
Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx)
api, err := GetAPI(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
ctx := ReqContext(cctx)
addr, err := address.NewFromString(cctx.Args().First())
if err != nil {

View File

@ -0,0 +1,47 @@
package main
import (
"os"
logging "github.com/ipfs/go-log"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/go-lotus/build"
lcli "github.com/filecoin-project/go-lotus/cli"
)
var log = logging.Logger("main")
func main() {
logging.SetLogLevel("*", "INFO")
local := []*cli.Command{
RunCmd,
}
app := &cli.App{
Name: "lotus-storage-miner",
Usage: "Filecoin decentralized storage network storage miner",
Version: build.Version,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
EnvVars: []string{"LOTUS_PATH"},
Hidden: true,
Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME
},
&cli.StringFlag{
Name: "storagerepo",
EnvVars: []string{"LOTUS_PATH"},
Hidden: true,
Value: "~/.lotusstorage", // TODO: Consider XDG_DATA_HOME
},
},
Commands: append(local, lcli.Commands...),
}
if err := app.Run(os.Args); err != nil {
log.Error(err)
return
}
}

View File

@ -0,0 +1,26 @@
package main
import (
"gopkg.in/urfave/cli.v2"
lcli "github.com/filecoin-project/go-lotus/cli"
)
var RunCmd = &cli.Command{
Name: "run",
Usage: "Start a lotus storage miner process",
Action: func(cctx *cli.Context) error {
api, err := lcli.GetAPI(cctx)
if err != nil {
return err
}
ctx := lcli.ReqContext(cctx)
v, err := api.Version(ctx)
// TODO: libp2p node
log.Infof("Remote version %s", v)
return nil
},
}

View File

@ -9,13 +9,12 @@ import (
"github.com/filecoin-project/go-lotus/build"
lcli "github.com/filecoin-project/go-lotus/cli"
"github.com/filecoin-project/go-lotus/daemon"
)
func main() {
logging.SetLogLevel("*", "INFO")
local := []*cli.Command{
daemon.Cmd,
DaemonCmd,
}
app := &cli.App{

View File

@ -1,60 +0,0 @@
// +build !nodaemon
package daemon
import (
"context"
"github.com/multiformats/go-multiaddr"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/go-lotus/node"
"github.com/filecoin-project/go-lotus/node/repo"
)
// Cmd is the `go-lotus daemon` command
var Cmd = &cli.Command{
Name: "daemon",
Usage: "Start a lotus daemon process",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "api",
Value: "1234",
},
},
Action: func(cctx *cli.Context) error {
ctx := context.Background()
r, err := repo.NewFS(cctx.String("repo"))
if err != nil {
return err
}
if err := r.Init(); err != nil && err != repo.ErrRepoExists {
return err
}
api, err := node.New(ctx,
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
}
// Write cli token to the repo if not there yet
if _, err := api.AuthNew(ctx, nil); err != nil {
return err
}
// TODO: properly parse api endpoint (or make it a URL)
return serveRPC(api, "127.0.0.1:"+cctx.String("api"), api.AuthVerify)
},
}

View File

@ -1,24 +0,0 @@
// +build nodaemon
package daemon
import (
"errors"
"gopkg.in/urfave/cli.v2"
)
// Cmd is the `go-lotus daemon` command
var Cmd = &cli.Command{
Name: "daemon",
Usage: "Start a lotus daemon process",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "api",
Value: ":1234",
},
},
Action: func(cctx *cli.Context) error {
return errors.New("daemon support not included in this binary")
},
}

View File

@ -1,23 +0,0 @@
package daemon
import (
"context"
"github.com/filecoin-project/go-lotus/lib/auth"
"net/http"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/lib/jsonrpc"
)
func serveRPC(a api.API, addr string, verify func(ctx context.Context, token string) ([]string, error)) error {
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", api.Permissioned(a))
authHandler := &auth.Handler{
Verify: verify,
Next: rpcServer.ServeHTTP,
}
http.Handle("/rpc/v0", authHandler)
return http.ListenAndServe(addr, http.DefaultServeMux)
}