Skip to content

Commit

Permalink
coreapi/unixfs: don't create an additional IpfsNode for --only-hash
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure authored and hacdias committed Mar 19, 2024
1 parent 9a5f5e7 commit bd9c6cf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 66 deletions.
59 changes: 16 additions & 43 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ package coreapi
import (
"context"
"fmt"
"sync"

"github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/tracing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

"github.com/ipfs/kubo/core/coreunix"

blockservice "github.com/ipfs/boxo/blockservice"
bstore "github.com/ipfs/boxo/blockstore"
Expand All @@ -21,41 +13,23 @@ import (
ft "github.com/ipfs/boxo/ipld/unixfs"
unixfile "github.com/ipfs/boxo/ipld/unixfs/file"
uio "github.com/ipfs/boxo/ipld/unixfs/io"
mfs "github.com/ipfs/boxo/mfs"
"github.com/ipfs/boxo/mfs"
"github.com/ipfs/boxo/path"
cid "github.com/ipfs/go-cid"
cidutil "github.com/ipfs/go-cidutil"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
ipld "github.com/ipfs/go-ipld-format"
coreiface "github.com/ipfs/kubo/core/coreiface"
options "github.com/ipfs/kubo/core/coreiface/options"
"github.com/ipfs/kubo/core/coreunix"
"github.com/ipfs/kubo/tracing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

type UnixfsAPI CoreAPI

var (
nilNode *core.IpfsNode
once sync.Once
)

func getOrCreateNilNode() (*core.IpfsNode, error) {
once.Do(func() {
if nilNode != nil {
return
}
node, err := core.NewNode(context.Background(), &core.BuildCfg{
// TODO: need this to be true or all files
// hashed will be stored in memory!
NilRepo: true,
})
if err != nil {
panic(err)
}
nilNode = node
})

return nilNode, nil
}

// Add builds a merkledag node from a reader, adds it to the blockstore,
// and returns the key representing that node.
func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options.UnixfsAddOption) (path.ImmutablePath, error) {
Expand Down Expand Up @@ -108,13 +82,12 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
pinning := api.pinning

if settings.OnlyHash {
node, err := getOrCreateNilNode()
if err != nil {
return path.ImmutablePath{}, err
}
addblockstore = node.Blockstore
exch = node.Exchange
pinning = node.Pinning
// setup a /dev/null pipeline to simulate adding the data
dstore := dssync.MutexWrap(ds.NewNullDatastore())
bs := bstore.NewBlockstore(dstore, bstore.WriteThrough())
addblockstore = bstore.NewGCBlockstore(bs, nil) // gclocker will never be used
exch = nil // exchange will never be used
pinning = nil // pinner will never be used
}

bserv := blockservice.New(addblockstore, exch) // hash security 001
Expand All @@ -133,11 +106,11 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
syncDserv = &syncDagService{
DAGService: dserv,
syncFn: func() error {
ds := api.repo.Datastore()
if err := ds.Sync(ctx, bstore.BlockPrefix); err != nil {
rds := api.repo.Datastore()
if err := rds.Sync(ctx, bstore.BlockPrefix); err != nil {
return err
}
return ds.Sync(ctx, filestore.FilestorePrefix)
return rds.Sync(ctx, filestore.FilestorePrefix)
},
}
}
Expand Down
16 changes: 1 addition & 15 deletions core/node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/rand"
"encoding/base64"
"errors"

"go.uber.org/fx"

Expand Down Expand Up @@ -34,9 +33,6 @@ type BuildCfg struct {
// DO NOT SET THIS UNLESS YOU'RE TESTING.
DisableEncryptedConnections bool

// If NilRepo is set, a Repo backed by a nil datastore will be constructed
NilRepo bool

Routing libp2p.RoutingOption
Host libp2p.HostOption
Repo repo.Repo
Expand All @@ -51,18 +47,8 @@ func (cfg *BuildCfg) getOpt(key string) bool {
}

func (cfg *BuildCfg) fillDefaults() error {
if cfg.Repo != nil && cfg.NilRepo {
return errors.New("cannot set a Repo and specify nilrepo at the same time")
}

if cfg.Repo == nil {
var d ds.Datastore
if cfg.NilRepo {
d = ds.NewNullDatastore()
} else {
d = ds.NewMapDatastore()
}
r, err := defaultRepo(dsync.MutexWrap(d))
r, err := defaultRepo(dsync.MutexWrap(ds.NewMapDatastore()))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func Storage(bcfg *BuildCfg, cfg *config.Config) fx.Option {
return fx.Options(
fx.Provide(RepoConfig),
fx.Provide(Datastore),
fx.Provide(BaseBlockstoreCtor(cacheOpts, bcfg.NilRepo, cfg.Datastore.HashOnRead)),
fx.Provide(BaseBlockstoreCtor(cacheOpts, cfg.Datastore.HashOnRead)),
finalBstore,
)
}
Expand Down
11 changes: 4 additions & 7 deletions core/node/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@ func Datastore(repo repo.Repo) datastore.Datastore {
type BaseBlocks blockstore.Blockstore

// BaseBlockstoreCtor creates cached blockstore backed by the provided datastore
func BaseBlockstoreCtor(cacheOpts blockstore.CacheOpts, nilRepo bool, hashOnRead bool) func(mctx helpers.MetricsCtx, repo repo.Repo, lc fx.Lifecycle) (bs BaseBlocks, err error) {
func BaseBlockstoreCtor(cacheOpts blockstore.CacheOpts, hashOnRead bool) func(mctx helpers.MetricsCtx, repo repo.Repo, lc fx.Lifecycle) (bs BaseBlocks, err error) {
return func(mctx helpers.MetricsCtx, repo repo.Repo, lc fx.Lifecycle) (bs BaseBlocks, err error) {
// hash security
bs = blockstore.NewBlockstore(repo.Datastore())
bs = &verifbs.VerifBS{Blockstore: bs}

if !nilRepo {
bs, err = blockstore.CachedBlockstore(helpers.LifecycleCtx(mctx, lc), bs, cacheOpts)
if err != nil {
return nil, err
}
bs, err = blockstore.CachedBlockstore(helpers.LifecycleCtx(mctx, lc), bs, cacheOpts)
if err != nil {
return nil, err
}

bs = blockstore.NewIdStore(bs)
Expand Down

0 comments on commit bd9c6cf

Please sign in to comment.