lotus/node/modules/core.go

115 lines
3.0 KiB
Go
Raw Normal View History

package modules
import (
2019-07-08 13:36:43 +00:00
"context"
2019-07-12 09:59:18 +00:00
"path/filepath"
2019-07-08 13:36:43 +00:00
"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
2019-07-16 16:02:51 +00:00
"github.com/ipfs/go-blockservice"
2019-07-10 15:38:35 +00:00
"github.com/ipfs/go-datastore"
2019-07-16 16:02:51 +00:00
"github.com/ipfs/go-datastore/namespace"
"github.com/ipfs/go-filestore"
2019-07-08 13:36:43 +00:00
blockstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
2019-07-12 09:59:18 +00:00
offline "github.com/ipfs/go-ipfs-exchange-offline"
ipld "github.com/ipfs/go-ipld-format"
2019-07-03 17:39:07 +00:00
logging "github.com/ipfs/go-log"
2019-07-16 16:02:51 +00:00
"github.com/ipfs/go-merkledag"
2019-07-08 13:36:43 +00:00
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peerstore"
2019-07-08 13:36:43 +00:00
"github.com/libp2p/go-libp2p-core/routing"
record "github.com/libp2p/go-libp2p-record"
2019-07-08 13:36:43 +00:00
"go.uber.org/fx"
"github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/node/modules/helpers"
2019-07-10 15:38:35 +00:00
"github.com/filecoin-project/go-lotus/node/repo"
)
2019-07-03 17:39:07 +00:00
var log = logging.Logger("modules")
2019-07-08 13:36:43 +00:00
type Genesis *chain.BlockHeader
// RecordValidator provides namesys compatible routing record validator
func RecordValidator(ps peerstore.Peerstore) record.Validator {
return record.NamespacedValidator{
"pk": record.PublicKeyValidator{},
}
}
2019-07-08 13:36:43 +00:00
func Bitswap(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.Routing, bs blockstore.GCBlockstore) exchange.Interface {
bitswapNetwork := network.NewFromIpfsHost(host, rt)
exch := bitswap.New(helpers.LifecycleCtx(mctx, lc), bitswapNetwork, bs)
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return exch.Close()
},
})
return exch
}
func SetGenesis(cs *chain.ChainStore, g Genesis) error {
return cs.SetGenesis(g)
}
2019-07-10 15:38:35 +00:00
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 KeyStore(lr repo.LockedRepo) (types.KeyStore, error) {
return lr.KeyStore()
}
2019-07-10 15:38:35 +00:00
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
}
2019-07-12 09:59:18 +00:00
2019-07-12 10:44:01 +00:00
func ClientFstore(r repo.LockedRepo) (*filestore.Filestore, error) {
2019-07-12 09:59:18 +00:00
clientds, err := r.Datastore("/client")
if err != nil {
return nil, err
}
blocks := namespace.Wrap(clientds, datastore.NewKey("blocks"))
fm := filestore.NewFileManager(clientds, filepath.Dir(r.Path()))
2019-07-12 10:17:44 +00:00
fm.AllowFiles = true
// TODO: fm.AllowUrls (needs more code in client import)
2019-07-12 09:59:18 +00:00
bs := blockstore.NewBlockstore(blocks)
2019-07-12 10:44:01 +00:00
return filestore.NewFilestore(bs, fm), nil
}
func ClientDAG(lc fx.Lifecycle, fstore *filestore.Filestore) ipld.DAGService {
2019-07-12 09:59:18 +00:00
ibs := blockstore.NewIdStore(fstore)
bsvc := blockservice.New(ibs, offline.Exchange(ibs))
dag := merkledag.NewDAGService(bsvc)
lc.Append(fx.Hook{
OnStop: func(_ context.Context) error {
return bsvc.Close()
},
})
2019-07-12 10:44:01 +00:00
return dag
2019-07-12 09:59:18 +00:00
}