Cleanup node/modules

This commit is contained in:
Łukasz Magiera 2019-08-01 16:19:53 +02:00
parent 6a4b9a6515
commit 9ae450620a
7 changed files with 266 additions and 253 deletions

View File

@ -270,7 +270,7 @@ func Repo(r repo.Repo) Option {
Override(new(repo.LockedRepo), modules.LockedRepo(lr)), // module handles closing
Override(new(dtypes.MetadataDS), modules.Datastore),
Override(new(dtypes.ChainBlockstore), modules.Blockstore),
Override(new(dtypes.ChainBlockstore), modules.ChainBlockstore),
Override(new(dtypes.ClientFilestore), modules.ClientFstore),
Override(new(dtypes.ClientDAG), modules.ClientDAG),

108
node/modules/chain.go Normal file
View File

@ -0,0 +1,108 @@
package modules
import (
"bytes"
"context"
"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-car"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/routing"
"go.uber.org/fx"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/node/modules/dtypes"
"github.com/filecoin-project/go-lotus/node/modules/helpers"
"github.com/filecoin-project/go-lotus/node/repo"
)
func ChainExchange(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.Routing, bs dtypes.ChainGCBlockstore) dtypes.ChainExchange {
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 ChainBlockstore(r repo.LockedRepo) (dtypes.ChainBlockstore, error) {
blocks, err := r.Datastore("/blocks")
if err != nil {
return nil, err
}
bs := blockstore.NewBlockstore(blocks)
return blockstore.NewIdStore(bs), nil
}
func ChainGCBlockstore(bs dtypes.ChainBlockstore, gcl dtypes.ChainGCLocker) dtypes.ChainGCBlockstore {
return blockstore.NewGCBlockstore(bs, gcl)
}
func ChainBlockservice(bs dtypes.ChainBlockstore, rem dtypes.ChainExchange) dtypes.ChainBlockService {
return blockservice.New(bs, rem)
}
func ChainStore(lc fx.Lifecycle, bs dtypes.ChainBlockstore, ds dtypes.MetadataDS) *store.ChainStore {
chain := store.NewChainStore(bs, ds)
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return chain.Load()
},
})
return chain
}
func ErrorGenesis() Genesis {
return func() (header *types.BlockHeader, e error) {
return nil, xerrors.New("No genesis block provided, provide the file with 'lotus daemon --genesis=[genesis file]'")
}
}
func LoadGenesis(genBytes []byte) func(dtypes.ChainBlockstore) Genesis {
return func(bs dtypes.ChainBlockstore) Genesis {
return func() (header *types.BlockHeader, e error) {
c, err := car.LoadCar(bs, bytes.NewReader(genBytes))
if err != nil {
return nil, err
}
if len(c.Roots) != 1 {
return nil, xerrors.New("expected genesis file to have one root")
}
root, err := bs.Get(c.Roots[0])
if err != nil {
return &types.BlockHeader{}, err
}
return types.DecodeBlock(root.RawData())
}
}
}
func SetGenesis(cs *store.ChainStore, g Genesis) error {
_, err := cs.GetGenesis()
if err == nil {
return nil // already set, noop
}
if err != datastore.ErrNotFound {
return err
}
genesis, err := g()
if err != nil {
return err
}
return cs.SetGenesis(genesis)
}

47
node/modules/client.go Normal file
View File

@ -0,0 +1,47 @@
package modules
import (
"context"
"path/filepath"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
"github.com/ipfs/go-filestore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
offline "github.com/ipfs/go-ipfs-exchange-offline"
"github.com/ipfs/go-merkledag"
"go.uber.org/fx"
"github.com/filecoin-project/go-lotus/node/modules/dtypes"
"github.com/filecoin-project/go-lotus/node/repo"
)
func ClientFstore(r repo.LockedRepo) (dtypes.ClientFilestore, error) {
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()))
fm.AllowFiles = true
// TODO: fm.AllowUrls (needs more code in client import)
bs := blockstore.NewBlockstore(blocks)
return filestore.NewFilestore(bs, fm), nil
}
func ClientDAG(lc fx.Lifecycle, fstore dtypes.ClientFilestore) dtypes.ClientDAG {
ibs := blockstore.NewIdStore((*filestore.Filestore)(fstore))
bsvc := blockservice.New(ibs, offline.Exchange(ibs))
dag := merkledag.NewDAGService(bsvc)
lc.Append(fx.Hook{
OnStop: func(_ context.Context) error {
return bsvc.Close()
},
})
return dag
}

View File

@ -1,37 +1,19 @@
package modules
import (
"bytes"
"context"
"crypto/rand"
"io"
"io/ioutil"
"path/filepath"
"github.com/gbrlsnchs/jwt/v3"
"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-car"
"github.com/ipfs/go-datastore"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/routing"
record "github.com/libp2p/go-libp2p-record"
"github.com/mitchellh/go-homedir"
"go.uber.org/fx"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/chain/wallet"
"github.com/filecoin-project/go-lotus/lib/sectorbuilder"
"github.com/filecoin-project/go-lotus/node/modules/dtypes"
"github.com/filecoin-project/go-lotus/node/modules/helpers"
"github.com/filecoin-project/go-lotus/node/repo"
"github.com/filecoin-project/go-lotus/storage"
)
var log = logging.Logger("modules")
@ -45,51 +27,6 @@ func RecordValidator(ps peerstore.Peerstore) record.Validator {
}
}
func ChainExchange(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.Routing, bs dtypes.ChainGCBlockstore) dtypes.ChainExchange {
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 *store.ChainStore, g Genesis) error {
_, err := cs.GetGenesis()
if err == nil {
return nil // already set, noop
}
if err != datastore.ErrNotFound {
return err
}
genesis, err := g()
if err != nil {
return err
}
return cs.SetGenesis(genesis)
}
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()
}
const JWTSecretName = "auth-jwt-private"
type APIAlg jwt.HMACSHA
@ -135,114 +72,3 @@ func APISecret(keystore types.KeyStore, lr repo.LockedRepo) (*APIAlg, error) {
return (*APIAlg)(jwt.NewHS256(key.PrivateKey)), nil
}
func ChainStore(lc fx.Lifecycle, bs dtypes.ChainBlockstore, ds dtypes.MetadataDS) *store.ChainStore {
chain := store.NewChainStore(bs, ds)
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return chain.Load()
},
})
return chain
}
func ErrorGenesis() Genesis {
return func() (header *types.BlockHeader, e error) {
return nil, xerrors.New("No genesis block provided, provide the file with 'lotus daemon --genesis=[genesis file]'")
}
}
func LoadGenesis(genBytes []byte) func(dtypes.ChainBlockstore) Genesis {
return func(bs dtypes.ChainBlockstore) Genesis {
return func() (header *types.BlockHeader, e error) {
c, err := car.LoadCar(bs, bytes.NewReader(genBytes))
if err != nil {
return nil, err
}
if len(c.Roots) != 1 {
return nil, xerrors.New("expected genesis file to have one root")
}
root, err := bs.Get(c.Roots[0])
if err != nil {
return &types.BlockHeader{}, err
}
return types.DecodeBlock(root.RawData())
}
}
}
func SectorBuilderConfig(storagePath string) func() (*sectorbuilder.SectorBuilderConfig, error) {
return func() (*sectorbuilder.SectorBuilderConfig, error) {
sp, err := homedir.Expand(storagePath)
if err != nil {
return nil, err
}
metadata := filepath.Join(sp, "meta")
sealed := filepath.Join(sp, "sealed")
staging := filepath.Join(sp, "staging")
// TODO: get the address of the miner actor
minerAddr, err := address.NewIDAddress(42)
if err != nil {
return nil, err
}
sb := &sectorbuilder.SectorBuilderConfig{
Miner: minerAddr,
SectorSize: 1024,
MetadataDir: metadata,
SealedDir: sealed,
StagedDir: staging,
}
return sb, nil
}
}
func SectorBuilder(mctx helpers.MetricsCtx, lc fx.Lifecycle, sbc *sectorbuilder.SectorBuilderConfig) (*sectorbuilder.SectorBuilder, error) {
sb, err := sectorbuilder.New(sbc)
if err != nil {
return nil, err
}
ctx := helpers.LifecycleCtx(mctx, lc)
lc.Append(fx.Hook{
OnStart: func(context.Context) error {
sb.Run(ctx)
return nil
},
})
return sb, nil
}
func StorageMiner(mctx helpers.MetricsCtx, lc fx.Lifecycle, api api.FullNode, h host.Host, ds dtypes.MetadataDS, sb *sectorbuilder.SectorBuilder, w *wallet.Wallet) (*storage.Miner, error) {
maddrb, err := ds.Get(datastore.NewKey("miner-address"))
if err != nil {
return nil, err
}
maddr, err := address.NewFromBytes(maddrb)
if err != nil {
return nil, err
}
sm, err := storage.NewMiner(api, maddr, h, ds, sb, w)
if err != nil {
return nil, err
}
ctx := helpers.LifecycleCtx(mctx, lc)
lc.Append(fx.Hook{
OnStart: func(context.Context) error {
return sm.Run(ctx)
},
})
return sm, nil
}

View File

@ -2,68 +2,30 @@ package modules
import (
"context"
"path/filepath"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
"github.com/ipfs/go-filestore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
offline "github.com/ipfs/go-ipfs-exchange-offline"
"github.com/ipfs/go-merkledag"
"go.uber.org/fx"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/node/modules/dtypes"
"github.com/filecoin-project/go-lotus/node/repo"
)
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()
}
func Datastore(r repo.LockedRepo) (dtypes.MetadataDS, error) {
return r.Datastore("/metadata")
}
func Blockstore(r repo.LockedRepo) (dtypes.ChainBlockstore, error) {
blocks, err := r.Datastore("/blocks")
if err != nil {
return nil, err
}
bs := blockstore.NewBlockstore(blocks)
return blockstore.NewIdStore(bs), nil
}
func ClientFstore(r repo.LockedRepo) (dtypes.ClientFilestore, error) {
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()))
fm.AllowFiles = true
// TODO: fm.AllowUrls (needs more code in client import)
bs := blockstore.NewBlockstore(blocks)
return filestore.NewFilestore(bs, fm), nil
}
func ClientDAG(lc fx.Lifecycle, fstore dtypes.ClientFilestore) dtypes.ClientDAG {
ibs := blockstore.NewIdStore((*filestore.Filestore)(fstore))
bsvc := blockservice.New(ibs, offline.Exchange(ibs))
dag := merkledag.NewDAGService(bsvc)
lc.Append(fx.Hook{
OnStop: func(_ context.Context) error {
return bsvc.Close()
},
})
return dag
}
func ChainGCBlockstore(bs dtypes.ChainBlockstore, gcl dtypes.ChainGCLocker) dtypes.ChainGCBlockstore {
return blockstore.NewGCBlockstore(bs, gcl)
}
func ChainBlockservice(bs dtypes.ChainBlockstore, rem dtypes.ChainExchange) dtypes.ChainBlockService {
return blockservice.New(bs, rem)
}

View File

@ -0,0 +1,93 @@
package modules
import (
"context"
"path/filepath"
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p-core/host"
"github.com/mitchellh/go-homedir"
"go.uber.org/fx"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/wallet"
"github.com/filecoin-project/go-lotus/lib/sectorbuilder"
"github.com/filecoin-project/go-lotus/node/modules/dtypes"
"github.com/filecoin-project/go-lotus/node/modules/helpers"
"github.com/filecoin-project/go-lotus/storage"
)
func SectorBuilderConfig(storagePath string) func() (*sectorbuilder.SectorBuilderConfig, error) {
return func() (*sectorbuilder.SectorBuilderConfig, error) {
sp, err := homedir.Expand(storagePath)
if err != nil {
return nil, err
}
metadata := filepath.Join(sp, "meta")
sealed := filepath.Join(sp, "sealed")
staging := filepath.Join(sp, "staging")
// TODO: get the address of the miner actor
minerAddr, err := address.NewIDAddress(42)
if err != nil {
return nil, err
}
sb := &sectorbuilder.SectorBuilderConfig{
Miner: minerAddr,
SectorSize: 1024,
MetadataDir: metadata,
SealedDir: sealed,
StagedDir: staging,
}
return sb, nil
}
}
func SectorBuilder(mctx helpers.MetricsCtx, lc fx.Lifecycle, sbc *sectorbuilder.SectorBuilderConfig) (*sectorbuilder.SectorBuilder, error) {
sb, err := sectorbuilder.New(sbc)
if err != nil {
return nil, err
}
ctx := helpers.LifecycleCtx(mctx, lc)
lc.Append(fx.Hook{
OnStart: func(context.Context) error {
sb.Run(ctx)
return nil
},
})
return sb, nil
}
func StorageMiner(mctx helpers.MetricsCtx, lc fx.Lifecycle, api api.FullNode, h host.Host, ds dtypes.MetadataDS, sb *sectorbuilder.SectorBuilder, w *wallet.Wallet) (*storage.Miner, error) {
maddrb, err := ds.Get(datastore.NewKey("miner-address"))
if err != nil {
return nil, err
}
maddr, err := address.NewFromBytes(maddrb)
if err != nil {
return nil, err
}
sm, err := storage.NewMiner(api, maddr, h, ds, sb, w)
if err != nil {
return nil, err
}
ctx := helpers.LifecycleCtx(mctx, lc)
lc.Append(fx.Hook{
OnStart: func(context.Context) error {
return sm.Run(ctx)
},
})
return sm, nil
}

View File

@ -1,23 +0,0 @@
package modules
import (
"crypto/rand"
"io"
"io/ioutil"
"github.com/libp2p/go-libp2p-core/peer"
mh "github.com/multiformats/go-multihash"
)
// RandomPeerID generates random peer id
func RandomPeerID() (peer.ID, error) {
b, err := ioutil.ReadAll(io.LimitReader(rand.Reader, 32))
if err != nil {
return "", err
}
hash, err := mh.Sum(b, mh.SHA2_256, -1)
if err != nil {
return "", err
}
return peer.ID(hash), nil
}