2021-02-28 22:48:36 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2021-02-26 13:45:30 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-02-28 22:48:36 +00:00
|
|
|
|
|
|
|
bstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
2021-03-01 13:01:11 +00:00
|
|
|
badgerbs "github.com/filecoin-project/lotus/blockstore/badger"
|
2021-03-01 16:15:39 +00:00
|
|
|
"github.com/filecoin-project/lotus/blockstore/splitstore"
|
2021-02-27 10:10:10 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2021-02-28 22:48:36 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UniversalBlockstore returns a single universal blockstore that stores both
|
2021-03-01 17:38:02 +00:00
|
|
|
// chain data and state data. It can be backed by a blockstore directly
|
|
|
|
// (e.g. Badger), or by a Splitstore.
|
2021-02-28 22:48:36 +00:00
|
|
|
func UniversalBlockstore(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRepo) (dtypes.UniversalBlockstore, error) {
|
|
|
|
bs, err := r.Blockstore(helpers.LifecycleCtx(mctx, lc), repo.UniversalBlockstore)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if c, ok := bs.(io.Closer); ok {
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(_ context.Context) error {
|
|
|
|
return c.Close()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return bs, err
|
|
|
|
}
|
|
|
|
|
2021-07-09 16:19:37 +00:00
|
|
|
func DiscardColdBlockstore(lc fx.Lifecycle, bs dtypes.UniversalBlockstore) (dtypes.ColdBlockstore, error) {
|
|
|
|
return blockstore.NewDiscardStore(bs), nil
|
2021-06-11 20:22:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 13:45:30 +00:00
|
|
|
func BadgerHotBlockstore(lc fx.Lifecycle, r repo.LockedRepo) (dtypes.HotBlockstore, error) {
|
|
|
|
path, err := r.SplitstorePath()
|
2020-12-01 15:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-26 13:45:30 +00:00
|
|
|
|
2021-02-26 18:53:54 +00:00
|
|
|
path = filepath.Join(path, "hot.badger")
|
2021-02-26 13:45:30 +00:00
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := repo.BadgerBlockstoreOptions(repo.HotBlockstore, path, r.Readonly())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-12-01 15:35:58 +00:00
|
|
|
}
|
2021-02-26 13:45:30 +00:00
|
|
|
|
|
|
|
bs, err := badgerbs.Open(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(_ context.Context) error {
|
|
|
|
return bs.Close()
|
|
|
|
}})
|
|
|
|
|
2021-03-02 18:36:17 +00:00
|
|
|
return bs, nil
|
2021-02-28 22:48:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 20:22:20 +00:00
|
|
|
func SplitBlockstore(cfg *config.Chainstore) func(lc fx.Lifecycle, r repo.LockedRepo, ds dtypes.MetadataDS, cold dtypes.ColdBlockstore, hot dtypes.HotBlockstore) (dtypes.SplitBlockstore, error) {
|
|
|
|
return func(lc fx.Lifecycle, r repo.LockedRepo, ds dtypes.MetadataDS, cold dtypes.ColdBlockstore, hot dtypes.HotBlockstore) (dtypes.SplitBlockstore, error) {
|
2021-02-27 10:10:10 +00:00
|
|
|
path, err := r.SplitstorePath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-01 17:39:00 +00:00
|
|
|
cfg := &splitstore.Config{
|
2021-07-17 05:35:35 +00:00
|
|
|
MarkSetType: cfg.Splitstore.MarkSetType,
|
|
|
|
DiscardColdBlocks: cfg.Splitstore.ColdStoreType == "discard",
|
|
|
|
HotStoreMessageRetention: cfg.Splitstore.HotStoreMessageRetention,
|
2021-03-01 17:39:00 +00:00
|
|
|
}
|
|
|
|
ss, err := splitstore.Open(path, ds, hot, cold, cfg)
|
2021-02-27 10:10:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(context.Context) error {
|
|
|
|
return ss.Close()
|
|
|
|
},
|
|
|
|
})
|
2021-02-26 13:45:30 +00:00
|
|
|
|
2021-02-27 10:10:10 +00:00
|
|
|
return ss, err
|
2020-12-01 15:35:58 +00:00
|
|
|
}
|
2021-02-26 13:45:30 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 10:12:10 +00:00
|
|
|
func ExposedSplitBlockstore(_ fx.Lifecycle, s dtypes.SplitBlockstore) dtypes.ExposedBlockstore {
|
|
|
|
return s.(*splitstore.SplitStore).Expose()
|
|
|
|
}
|
|
|
|
|
2021-04-09 13:29:16 +00:00
|
|
|
func StateFlatBlockstore(_ fx.Lifecycle, _ helpers.MetricsCtx, bs dtypes.UniversalBlockstore) (dtypes.BasicStateBlockstore, error) {
|
2021-02-26 13:45:30 +00:00
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 13:29:16 +00:00
|
|
|
func StateSplitBlockstore(_ fx.Lifecycle, _ helpers.MetricsCtx, bs dtypes.SplitBlockstore) (dtypes.BasicStateBlockstore, error) {
|
2021-02-26 13:45:30 +00:00
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 17:38:02 +00:00
|
|
|
func ChainFlatBlockstore(_ fx.Lifecycle, _ helpers.MetricsCtx, bs dtypes.UniversalBlockstore) (dtypes.ChainBlockstore, error) {
|
2021-02-26 13:45:30 +00:00
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 17:38:02 +00:00
|
|
|
func ChainSplitBlockstore(_ fx.Lifecycle, _ helpers.MetricsCtx, bs dtypes.SplitBlockstore) (dtypes.ChainBlockstore, error) {
|
2021-02-26 13:45:30 +00:00
|
|
|
return bs, nil
|
2021-02-28 22:48:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 13:29:16 +00:00
|
|
|
func FallbackChainBlockstore(cbs dtypes.BasicChainBlockstore) dtypes.ChainBlockstore {
|
2021-02-28 22:48:36 +00:00
|
|
|
return &blockstore.FallbackStore{Blockstore: cbs}
|
|
|
|
}
|
|
|
|
|
2021-04-09 13:29:16 +00:00
|
|
|
func FallbackStateBlockstore(sbs dtypes.BasicStateBlockstore) dtypes.StateBlockstore {
|
2021-02-28 22:48:36 +00:00
|
|
|
return &blockstore.FallbackStore{Blockstore: sbs}
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitFallbackBlockstores(cbs dtypes.ChainBlockstore, sbs dtypes.StateBlockstore, rem dtypes.ChainBitswap) error {
|
|
|
|
for _, bs := range []bstore.Blockstore{cbs, sbs} {
|
|
|
|
if fbs, ok := bs.(*blockstore.FallbackStore); ok {
|
|
|
|
fbs.SetFallback(rem.GetBlock)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return xerrors.Errorf("expected a FallbackStore")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|