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] 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) {