From 14d515da3bd582c35539803bc30b1a07ecd65887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 10 Jul 2019 17:38:35 +0200 Subject: [PATCH 1/8] Wire up memrepo --- daemon/cmd.go | 17 ++++++++--------- node/builder.go | 20 ++++++++++++++++++++ node/modules/core.go | 28 ++++++++++++++++++++++++++++ node/repo/interface.go | 4 ++-- node/repo/memrepo.go | 6 ++++-- 5 files changed, 62 insertions(+), 13 deletions(-) diff --git a/daemon/cmd.go b/daemon/cmd.go index b0192d938..7bac4bca4 100644 --- a/daemon/cmd.go +++ b/daemon/cmd.go @@ -5,10 +5,10 @@ package daemon import ( "context" - "github.com/filecoin-project/go-lotus/node" - "github.com/filecoin-project/go-lotus/node/config" - "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 @@ -23,13 +23,12 @@ var Cmd = &cli.Command{ }, Action: func(cctx *cli.Context) error { ctx := context.Background() + repo := repo.NewMemory(nil) - cfg, err := config.FromFile("./config.toml") - if err != nil { - return err - } - - api, err := node.New(ctx, node.Online(), node.Config(cfg)) + api, err := node.New(ctx, + node.Online(), + node.Repo(repo), + ) if err != nil { return err } diff --git a/node/builder.go b/node/builder.go index ec3bfb313..5603bae50 100644 --- a/node/builder.go +++ b/node/builder.go @@ -27,6 +27,7 @@ import ( "github.com/filecoin-project/go-lotus/node/modules/helpers" "github.com/filecoin-project/go-lotus/node/modules/lp2p" "github.com/filecoin-project/go-lotus/node/modules/testing" + "github.com/filecoin-project/go-lotus/node/repo" ) // special is a type used to give keys to modules which @@ -193,6 +194,25 @@ func Config(cfg *config.Root) Option { ) } +func Repo(r repo.Repo) Option { + lr, err := r.Lock() + if err != nil { + return Error(err) + } + cfg, err := lr.Config() + if err != nil { + return Error(err) + } + + return Options( + Config(cfg), + Override(new(repo.LockedRepo), modules.LockedRepo(lr)), // module handles closing + + Override(new(datastore.Batching), modules.Datastore), + Override(new(blockstore.Blockstore), modules.Blockstore), + ) +} + // New builds and starts new Filecoin node func New(ctx context.Context, opts ...Option) (api.API, error) { resAPI := &API{} diff --git a/node/modules/core.go b/node/modules/core.go index 91aadd659..a261d7bb3 100644 --- a/node/modules/core.go +++ b/node/modules/core.go @@ -5,6 +5,7 @@ import ( "github.com/ipfs/go-bitswap" "github.com/ipfs/go-bitswap/network" + "github.com/ipfs/go-datastore" blockstore "github.com/ipfs/go-ipfs-blockstore" exchange "github.com/ipfs/go-ipfs-exchange-interface" logging "github.com/ipfs/go-log" @@ -16,6 +17,7 @@ import ( "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/node/modules/helpers" + "github.com/filecoin-project/go-lotus/node/repo" ) var log = logging.Logger("modules") @@ -43,3 +45,29 @@ func Bitswap(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routin func SetGenesis(cs *chain.ChainStore, g Genesis) error { return cs.SetGenesis(g) } + +func LockedRepo(lr repo.LockedRepo) func(lc fx.Lifecycle) repo.LockedRepo { + return func(lc fx.Lifecycle) repo.LockedRepo { + lc.Append(fx.Hook{ + OnStop: func(_ context.Context) error { + return lr.Close() + }, + }) + + return lr + } +} + +func Datastore(r repo.LockedRepo) (datastore.Batching, error) { + return r.Datastore("/metadata") +} + +func Blockstore(r repo.LockedRepo) (blockstore.Blockstore, error) { + blocks, err := r.Datastore("/blocks") + if err != nil { + return nil, err + } + + bs := blockstore.NewBlockstore(blocks) + return blockstore.NewIdStore(bs), nil +} diff --git a/node/repo/interface.go b/node/repo/interface.go index 4c9823ab9..fdfc8deb5 100644 --- a/node/repo/interface.go +++ b/node/repo/interface.go @@ -10,7 +10,7 @@ import ( ) var ( - ErrNoAPIEndpoint = xerrors.New("no API Endpoint set") + ErrNoAPIEndpoint = xerrors.New("API not running (no endpoint)") ErrRepoAlreadyLocked = xerrors.New("repo is already locked") ErrClosedRepo = xerrors.New("repo is no longer open") ) @@ -28,7 +28,7 @@ type LockedRepo interface { Close() error // Returns datastore defined in this repo. - Datastore() (datastore.Datastore, error) + Datastore(namespace string) (datastore.Batching, error) // Returns config in this repo Config() (*config.Root, error) diff --git a/node/repo/memrepo.go b/node/repo/memrepo.go index de2ace933..19cd31949 100644 --- a/node/repo/memrepo.go +++ b/node/repo/memrepo.go @@ -5,6 +5,7 @@ import ( "sync" "github.com/ipfs/go-datastore" + "github.com/ipfs/go-datastore/namespace" dssync "github.com/ipfs/go-datastore/sync" "github.com/libp2p/go-libp2p-core/crypto" "github.com/multiformats/go-multiaddr" @@ -127,11 +128,12 @@ func (lmem *lockedMemRepo) Close() error { } -func (lmem *lockedMemRepo) Datastore() (datastore.Datastore, error) { +func (lmem *lockedMemRepo) Datastore(ns string) (datastore.Batching, error) { if err := lmem.checkToken(); err != nil { return nil, err } - return lmem.mem.datastore, nil + + return namespace.Wrap(lmem.mem.datastore, datastore.NewKey(ns)), nil } func (lmem *lockedMemRepo) Config() (*config.Root, error) { From 1f8c3f4145d96b9bfa67840ccb077374cea1277b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 10 Jul 2019 19:09:57 +0200 Subject: [PATCH 2/8] repo: update Datastore, Init --- go.mod | 1 + go.sum | 2 ++ node/repo/fsrepo.go | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index b471457b8..dc40b6df2 100644 --- a/go.mod +++ b/go.mod @@ -39,6 +39,7 @@ require ( github.com/libp2p/go-libp2p-yamux v0.2.1 github.com/libp2p/go-maddr-filter v0.0.4 github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 + github.com/mitchellh/go-homedir v1.1.0 github.com/multiformats/go-multiaddr v0.0.4 github.com/multiformats/go-multiaddr-dns v0.0.2 github.com/multiformats/go-multihash v0.0.5 diff --git a/go.sum b/go.sum index 286bf799b..b6d7f9c5e 100644 --- a/go.sum +++ b/go.sum @@ -350,6 +350,8 @@ github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+ github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.1.0 h1:U41/2erhAKcmSI14xh/ZTUdBPOzDOIfS93ibzUSl8KM= github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= github.com/mr-tron/base58 v1.1.2 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78= diff --git a/node/repo/fsrepo.go b/node/repo/fsrepo.go index 1d3f313ee..18e8bc8cc 100644 --- a/node/repo/fsrepo.go +++ b/node/repo/fsrepo.go @@ -6,12 +6,17 @@ import ( "os" "path/filepath" "strings" + "sync" "github.com/ipfs/go-datastore" + "github.com/ipfs/go-datastore/namespace" badger "github.com/ipfs/go-ds-badger" fslock "github.com/ipfs/go-fs-lock" + logging "github.com/ipfs/go-log" "github.com/libp2p/go-libp2p-core/crypto" + "github.com/mitchellh/go-homedir" "github.com/multiformats/go-multiaddr" + "github.com/pkg/errors" "golang.org/x/xerrors" "github.com/filecoin-project/go-lotus/node/config" @@ -25,6 +30,10 @@ const ( fsLock = "repo.lock" ) +var log = logging.Logger("repo") + +var ErrRepoExists = errors.New("repo exists") + // FsRepo is struct for repo, use NewFS to create type FsRepo struct { path string @@ -34,11 +43,28 @@ var _ Repo = &FsRepo{} // NewFS creates a repo instance based on a path on file system func NewFS(path string) (*FsRepo, error) { + path, err := homedir.Expand(path) + if err != nil { + return &FsRepo{}, err + } + return &FsRepo{ path: path, }, nil } +func (fsr *FsRepo) Init() error { + if _, err := os.Stat(fsr.path); err == nil { + return ErrRepoExists + } else if !os.IsNotExist(err) { + return err + } + + log.Infof("Initializing repo at '%s'", fsr.path) + + return os.Mkdir(fsr.path, 0755) +} + // APIEndpoint returns endpoint of API in this repo func (fsr *FsRepo) APIEndpoint() (multiaddr.Multiaddr, error) { p := filepath.Join(fsr.path, fsAPI) @@ -88,6 +114,10 @@ func (fsr *FsRepo) Lock() (LockedRepo, error) { type fsLockedRepo struct { path string closer io.Closer + + ds datastore.Batching + dsErr error + dsOnce sync.Once } func (fsr *fsLockedRepo) Close() error { @@ -114,8 +144,11 @@ func (fsr *fsLockedRepo) stillValid() error { return nil } -func (fsr *fsLockedRepo) Datastore() (datastore.Datastore, error) { - return badger.NewDatastore(fsr.join(fsDatastore), nil) +func (fsr *fsLockedRepo) Datastore(ns string) (datastore.Batching, error) { + fsr.dsOnce.Do(func() { + fsr.ds, fsr.dsErr = badger.NewDatastore(fsr.join(fsDatastore), nil) + }) + return namespace.Wrap(fsr.ds, datastore.NewKey(ns)), fsr.dsErr } func (fsr *fsLockedRepo) Config() (*config.Root, error) { From 2e8dfc759b9aa8850101f09fd0043e6ae200f891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 10 Jul 2019 19:10:19 +0200 Subject: [PATCH 3/8] daemon: wire up fsrepo --- cmd/lotus/main.go | 14 +++++++------- daemon/cmd.go | 11 +++++++++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/cmd/lotus/main.go b/cmd/lotus/main.go index 61978dc3c..06086f20d 100644 --- a/cmd/lotus/main.go +++ b/cmd/lotus/main.go @@ -6,8 +6,6 @@ import ( "gopkg.in/urfave/cli.v2" - "github.com/filecoin-project/go-lotus/api" - "github.com/filecoin-project/go-lotus/api/client" "github.com/filecoin-project/go-lotus/build" lcli "github.com/filecoin-project/go-lotus/cli" "github.com/filecoin-project/go-lotus/daemon" @@ -22,11 +20,13 @@ func main() { Name: "lotus", Usage: "Filecoin decentralized storage network client", Version: build.Version, - Metadata: map[string]interface{}{ - "api": lcli.ApiConnector(func() api.API { - // TODO: get this from repo - return client.NewRPC("http://127.0.0.1:1234/rpc/v0") - }), + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "repo", + EnvVars: []string{"LOTUS_PATH"}, + Hidden: true, + Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME + }, }, Commands: append(local, lcli.Commands...), diff --git a/daemon/cmd.go b/daemon/cmd.go index 7bac4bca4..889f493ac 100644 --- a/daemon/cmd.go +++ b/daemon/cmd.go @@ -23,11 +23,18 @@ var Cmd = &cli.Command{ }, Action: func(cctx *cli.Context) error { ctx := context.Background() - repo := repo.NewMemory(nil) + 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(repo), + node.Repo(r), ) if err != nil { return err From e6396701959e5d7a2a5c1e70ef9661037bf888c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 10 Jul 2019 19:28:49 +0200 Subject: [PATCH 4/8] cli: use repo api endpoint --- cli/chain.go | 5 ++++- cli/cmd.go | 21 +++++++++++++++++---- cli/net.go | 15 ++++++++++++--- daemon/cmd.go | 13 +++++++++++-- go.mod | 1 + node/builder.go | 2 ++ node/node_test.go | 3 ++- node/opts_test.go | 1 + node/repo/fsrepo.go | 4 ++-- 9 files changed, 52 insertions(+), 13 deletions(-) diff --git a/cli/chain.go b/cli/chain.go index 1e23a0c3e..a57a9a14a 100644 --- a/cli/chain.go +++ b/cli/chain.go @@ -18,7 +18,10 @@ var chainHeadCmd = &cli.Command{ Name: "head", Usage: "Print chain head", Action: func(cctx *cli.Context) error { - api := getApi(cctx) + api, err := getApi(cctx) + if err != nil { + return err + } ctx := reqContext(cctx) head, err := api.ChainHead(ctx) diff --git a/cli/cmd.go b/cli/cmd.go index e76669a4f..b987862a9 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -6,20 +6,33 @@ import ( "os/signal" "syscall" - "github.com/filecoin-project/go-lotus/api" + manet "github.com/multiformats/go-multiaddr-net" "gopkg.in/urfave/cli.v2" + + "github.com/filecoin-project/go-lotus/api" + "github.com/filecoin-project/go-lotus/api/client" + "github.com/filecoin-project/go-lotus/node/repo" ) const ( metadataContext = "context" - metadataAPI = "api" ) // ApiConnector returns API instance type ApiConnector func() api.API -func getApi(ctx *cli.Context) api.API { - return ctx.App.Metadata[metadataAPI].(ApiConnector)() +func getApi(ctx *cli.Context) (api.API, error) { + r, err := repo.NewFS(ctx.String("repo")) + if err != nil { + return nil, err + } + + ma, err := r.APIEndpoint() + if err != nil { + return nil, err + } + _, addr, err := manet.DialArgs(ma) + return client.NewRPC("http://" + addr + "/rpc/v0"), nil } // reqContext returns context for cli execution. Calling it for the first time diff --git a/cli/net.go b/cli/net.go index 1ccadc9eb..76044ec54 100644 --- a/cli/net.go +++ b/cli/net.go @@ -26,7 +26,10 @@ var netPeers = &cli.Command{ Name: "peers", Usage: "Print peers", Action: func(cctx *cli.Context) error { - api := getApi(cctx) + api, err := getApi(cctx) + if err != nil { + return err + } ctx := reqContext(cctx) peers, err := api.NetPeers(ctx) if err != nil { @@ -45,7 +48,10 @@ var netListen = &cli.Command{ Name: "listen", Usage: "List listen addresses", Action: func(cctx *cli.Context) error { - api := getApi(cctx) + api, err := getApi(cctx) + if err != nil { + return err + } ctx := reqContext(cctx) addrs, err := api.NetAddrsListen(ctx) @@ -64,7 +70,10 @@ var netConnect = &cli.Command{ Name: "connect", Usage: "Connect to a peer", Action: func(cctx *cli.Context) error { - api := getApi(cctx) + api, err := getApi(cctx) + if err != nil { + return err + } ctx := reqContext(cctx) pis, err := parseAddresses(ctx, cctx.Args().Slice()) diff --git a/daemon/cmd.go b/daemon/cmd.go index 889f493ac..bfb5e23be 100644 --- a/daemon/cmd.go +++ b/daemon/cmd.go @@ -5,6 +5,7 @@ package daemon import ( "context" + "github.com/multiformats/go-multiaddr" "gopkg.in/urfave/cli.v2" "github.com/filecoin-project/go-lotus/node" @@ -18,7 +19,7 @@ var Cmd = &cli.Command{ Flags: []cli.Flag{ &cli.StringFlag{ Name: "api", - Value: ":1234", + Value: "1234", }, }, Action: func(cctx *cli.Context) error { @@ -35,11 +36,19 @@ var Cmd = &cli.Command{ 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 } - return serveRPC(api, cctx.String("api")) + return serveRPC(api, ":"+cctx.String("api")) }, } diff --git a/go.mod b/go.mod index dc40b6df2..e61e84aa7 100644 --- a/go.mod +++ b/go.mod @@ -42,6 +42,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/multiformats/go-multiaddr v0.0.4 github.com/multiformats/go-multiaddr-dns v0.0.2 + github.com/multiformats/go-multiaddr-net v0.0.1 github.com/multiformats/go-multihash v0.0.5 github.com/pkg/errors v0.8.1 github.com/polydawn/refmt v0.0.0-20190408063855-01bf1e26dd14 diff --git a/node/builder.go b/node/builder.go index 5603bae50..4a81df2d8 100644 --- a/node/builder.go +++ b/node/builder.go @@ -66,6 +66,8 @@ const ( HandleIncomingBlocksKey HandleIncomingMessagesKey + SetApiEndpointKey + _nInvokes // keep this last ) diff --git a/node/node_test.go b/node/node_test.go index 496e0a4e9..6b36cf834 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -2,10 +2,11 @@ package node_test import ( "context" - "github.com/filecoin-project/go-lotus/node" "net/http/httptest" "testing" + "github.com/filecoin-project/go-lotus/node" + "github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/api/client" "github.com/filecoin-project/go-lotus/api/test" diff --git a/node/opts_test.go b/node/opts_test.go index c6cdf1bfa..60e024a84 100644 --- a/node/opts_test.go +++ b/node/opts_test.go @@ -2,6 +2,7 @@ package node_test import ( "errors" + "github.com/filecoin-project/go-lotus/node" "github.com/filecoin-project/go-lotus/node/modules/lp2p" diff --git a/node/repo/fsrepo.go b/node/repo/fsrepo.go index 18e8bc8cc..0aa66fcbf 100644 --- a/node/repo/fsrepo.go +++ b/node/repo/fsrepo.go @@ -115,8 +115,8 @@ type fsLockedRepo struct { path string closer io.Closer - ds datastore.Batching - dsErr error + ds datastore.Batching + dsErr error dsOnce sync.Once } From 1f97ad2c2edb7fa6262ef0c0647a45bdccf61972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 10 Jul 2019 19:36:17 +0200 Subject: [PATCH 5/8] Use repo libp2p identiny --- node/builder.go | 8 ++++++++ node/repo/fsrepo.go | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/node/builder.go b/node/builder.go index 4a81df2d8..0ba8e246c 100644 --- a/node/builder.go +++ b/node/builder.go @@ -205,6 +205,10 @@ func Repo(r repo.Repo) Option { if err != nil { return Error(err) } + pk, err := lr.Libp2pIdentity() + if err != nil { + return Error(err) + } return Options( Config(cfg), @@ -212,6 +216,10 @@ func Repo(r repo.Repo) Option { Override(new(datastore.Batching), modules.Datastore), Override(new(blockstore.Blockstore), modules.Blockstore), + + Override(new(ci.PrivKey), pk), + Override(new(ci.PubKey), ci.PrivKey.GetPublic), + Override(new(peer.ID), peer.IDFromPublicKey), ) } diff --git a/node/repo/fsrepo.go b/node/repo/fsrepo.go index 0aa66fcbf..27d9d24a4 100644 --- a/node/repo/fsrepo.go +++ b/node/repo/fsrepo.go @@ -45,7 +45,7 @@ var _ Repo = &FsRepo{} func NewFS(path string) (*FsRepo, error) { path, err := homedir.Expand(path) if err != nil { - return &FsRepo{}, err + return nil, err } return &FsRepo{ @@ -175,6 +175,13 @@ func (fsr *fsLockedRepo) Libp2pIdentity() (crypto.PrivKey, error) { if err != nil { return nil, xerrors.Errorf("could not write private key: %w", err) } + } else if err != nil { + return nil, err + } + + stat, err = os.Stat(kpath) + if err != nil { + return nil, err } if stat.Mode()&0066 != 0 { From ffafc056eefa69d2c7ace8772935184e7a7a0fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 11 Jul 2019 13:48:21 +0200 Subject: [PATCH 6/8] fsrepo: check dsErr before returning datastore --- node/repo/fsrepo.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/node/repo/fsrepo.go b/node/repo/fsrepo.go index 27d9d24a4..8c71150b7 100644 --- a/node/repo/fsrepo.go +++ b/node/repo/fsrepo.go @@ -148,7 +148,10 @@ func (fsr *fsLockedRepo) Datastore(ns string) (datastore.Batching, error) { fsr.dsOnce.Do(func() { fsr.ds, fsr.dsErr = badger.NewDatastore(fsr.join(fsDatastore), nil) }) - return namespace.Wrap(fsr.ds, datastore.NewKey(ns)), fsr.dsErr + if fsr.dsErr != nil { + return nil, fsr.dsErr + } + return namespace.Wrap(fsr.ds, datastore.NewKey(ns)), nil } func (fsr *fsLockedRepo) Config() (*config.Root, error) { From cd16e3e597f2e1075cf26049474482ac77f924af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 11 Jul 2019 13:52:07 +0200 Subject: [PATCH 7/8] Fix lint warnings --- cli/chain.go | 2 +- cli/cmd.go | 5 ++++- cli/net.go | 6 +++--- node/repo/fsrepo.go | 4 ++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cli/chain.go b/cli/chain.go index a57a9a14a..22a788f3b 100644 --- a/cli/chain.go +++ b/cli/chain.go @@ -18,7 +18,7 @@ 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 } diff --git a/cli/cmd.go b/cli/cmd.go index b987862a9..e6eb40d4b 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -21,7 +21,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 @@ -32,6 +32,9 @@ func getApi(ctx *cli.Context) (api.API, error) { return nil, err } _, addr, err := manet.DialArgs(ma) + if err != nil { + return nil, err + } return client.NewRPC("http://" + addr + "/rpc/v0"), nil } diff --git a/cli/net.go b/cli/net.go index 76044ec54..a46a48200 100644 --- a/cli/net.go +++ b/cli/net.go @@ -26,7 +26,7 @@ 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 } @@ -48,7 +48,7 @@ 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 } @@ -70,7 +70,7 @@ 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 } diff --git a/node/repo/fsrepo.go b/node/repo/fsrepo.go index 8c71150b7..a0d052371 100644 --- a/node/repo/fsrepo.go +++ b/node/repo/fsrepo.go @@ -62,7 +62,7 @@ func (fsr *FsRepo) Init() error { log.Infof("Initializing repo at '%s'", fsr.path) - return os.Mkdir(fsr.path, 0755) + return os.Mkdir(fsr.path, 0755) // nolint } // APIEndpoint returns endpoint of API in this repo @@ -214,7 +214,7 @@ func (fsr *fsLockedRepo) SetAPIEndpoint(ma multiaddr.Multiaddr) error { if err := fsr.stillValid(); err != nil { return err } - return ioutil.WriteFile(fsr.join(fsAPI), []byte(ma.String()), 0666) + return ioutil.WriteFile(fsr.join(fsAPI), []byte(ma.String()), 0644) } func (fsr *fsLockedRepo) Wallet() (interface{}, error) { From dd25f31e9a0d21038ae0244a10c531db110532e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 11 Jul 2019 13:57:04 +0200 Subject: [PATCH 8/8] Make API listen on localhost only --- daemon/cmd.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/daemon/cmd.go b/daemon/cmd.go index bfb5e23be..4029f581e 100644 --- a/daemon/cmd.go +++ b/daemon/cmd.go @@ -49,6 +49,7 @@ var Cmd = &cli.Command{ return err } - return serveRPC(api, ":"+cctx.String("api")) + // TODO: properly parse api endpoint (or make it a URL) + return serveRPC(api, "127.0.0.1:"+cctx.String("api")) }, }