Wire up memrepo

This commit is contained in:
Łukasz Magiera 2019-07-10 17:38:35 +02:00
parent 0aacd4048c
commit 14d515da3b
5 changed files with 62 additions and 13 deletions

View File

@ -5,10 +5,10 @@ package daemon
import ( import (
"context" "context"
"github.com/filecoin-project/go-lotus/node"
"github.com/filecoin-project/go-lotus/node/config"
"gopkg.in/urfave/cli.v2" "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 // Cmd is the `go-lotus daemon` command
@ -23,13 +23,12 @@ var Cmd = &cli.Command{
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
ctx := context.Background() ctx := context.Background()
repo := repo.NewMemory(nil)
cfg, err := config.FromFile("./config.toml") api, err := node.New(ctx,
if err != nil { node.Online(),
return err node.Repo(repo),
} )
api, err := node.New(ctx, node.Online(), node.Config(cfg))
if err != nil { if err != nil {
return err return err
} }

View File

@ -27,6 +27,7 @@ import (
"github.com/filecoin-project/go-lotus/node/modules/helpers" "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/lp2p"
"github.com/filecoin-project/go-lotus/node/modules/testing" "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 // 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 // New builds and starts new Filecoin node
func New(ctx context.Context, opts ...Option) (api.API, error) { func New(ctx context.Context, opts ...Option) (api.API, error) {
resAPI := &API{} resAPI := &API{}

View File

@ -5,6 +5,7 @@ import (
"github.com/ipfs/go-bitswap" "github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network" "github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore" blockstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface" exchange "github.com/ipfs/go-ipfs-exchange-interface"
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
@ -16,6 +17,7 @@ import (
"github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/node/modules/helpers" "github.com/filecoin-project/go-lotus/node/modules/helpers"
"github.com/filecoin-project/go-lotus/node/repo"
) )
var log = logging.Logger("modules") 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 { func SetGenesis(cs *chain.ChainStore, g Genesis) error {
return cs.SetGenesis(g) 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
}

View File

@ -10,7 +10,7 @@ import (
) )
var ( var (
ErrNoAPIEndpoint = xerrors.New("no API Endpoint set") ErrNoAPIEndpoint = xerrors.New("API not running (no endpoint)")
ErrRepoAlreadyLocked = xerrors.New("repo is already locked") ErrRepoAlreadyLocked = xerrors.New("repo is already locked")
ErrClosedRepo = xerrors.New("repo is no longer open") ErrClosedRepo = xerrors.New("repo is no longer open")
) )
@ -28,7 +28,7 @@ type LockedRepo interface {
Close() error Close() error
// Returns datastore defined in this repo. // Returns datastore defined in this repo.
Datastore() (datastore.Datastore, error) Datastore(namespace string) (datastore.Batching, error)
// Returns config in this repo // Returns config in this repo
Config() (*config.Root, error) Config() (*config.Root, error)

View File

@ -5,6 +5,7 @@ import (
"sync" "sync"
"github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
dssync "github.com/ipfs/go-datastore/sync" dssync "github.com/ipfs/go-datastore/sync"
"github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/crypto"
"github.com/multiformats/go-multiaddr" "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 { if err := lmem.checkToken(); err != nil {
return nil, err 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) { func (lmem *lockedMemRepo) Config() (*config.Root, error) {