Skip to content

Commit a3483e3

Browse files
MichaelMurehacdias
authored andcommitted
coreapi/unixfs: don't create an additional IpfsNode for --only-hash
1 parent 9a5f5e7 commit a3483e3

File tree

4 files changed

+22
-66
lines changed

4 files changed

+22
-66
lines changed

core/coreapi/unixfs.go

+16-43
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@ package coreapi
33
import (
44
"context"
55
"fmt"
6-
"sync"
7-
8-
"github.com/ipfs/kubo/core"
9-
"github.com/ipfs/kubo/tracing"
10-
"go.opentelemetry.io/otel/attribute"
11-
"go.opentelemetry.io/otel/trace"
12-
13-
"github.com/ipfs/kubo/core/coreunix"
146

157
blockservice "github.com/ipfs/boxo/blockservice"
168
bstore "github.com/ipfs/boxo/blockstore"
@@ -21,41 +13,23 @@ import (
2113
ft "github.com/ipfs/boxo/ipld/unixfs"
2214
unixfile "github.com/ipfs/boxo/ipld/unixfs/file"
2315
uio "github.com/ipfs/boxo/ipld/unixfs/io"
24-
mfs "github.com/ipfs/boxo/mfs"
16+
"github.com/ipfs/boxo/mfs"
2517
"github.com/ipfs/boxo/path"
2618
cid "github.com/ipfs/go-cid"
2719
cidutil "github.com/ipfs/go-cidutil"
20+
ds "github.com/ipfs/go-datastore"
21+
dssync "github.com/ipfs/go-datastore/sync"
2822
ipld "github.com/ipfs/go-ipld-format"
2923
coreiface "github.com/ipfs/kubo/core/coreiface"
3024
options "github.com/ipfs/kubo/core/coreiface/options"
25+
"github.com/ipfs/kubo/core/coreunix"
26+
"github.com/ipfs/kubo/tracing"
27+
"go.opentelemetry.io/otel/attribute"
28+
"go.opentelemetry.io/otel/trace"
3129
)
3230

3331
type UnixfsAPI CoreAPI
3432

35-
var (
36-
nilNode *core.IpfsNode
37-
once sync.Once
38-
)
39-
40-
func getOrCreateNilNode() (*core.IpfsNode, error) {
41-
once.Do(func() {
42-
if nilNode != nil {
43-
return
44-
}
45-
node, err := core.NewNode(context.Background(), &core.BuildCfg{
46-
// TODO: need this to be true or all files
47-
// hashed will be stored in memory!
48-
NilRepo: true,
49-
})
50-
if err != nil {
51-
panic(err)
52-
}
53-
nilNode = node
54-
})
55-
56-
return nilNode, nil
57-
}
58-
5933
// Add builds a merkledag node from a reader, adds it to the blockstore,
6034
// and returns the key representing that node.
6135
func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options.UnixfsAddOption) (path.ImmutablePath, error) {
@@ -108,13 +82,12 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
10882
pinning := api.pinning
10983

11084
if settings.OnlyHash {
111-
node, err := getOrCreateNilNode()
112-
if err != nil {
113-
return path.ImmutablePath{}, err
114-
}
115-
addblockstore = node.Blockstore
116-
exch = node.Exchange
117-
pinning = node.Pinning
85+
// setup a /dev/null pipeline to simulate adding the data
86+
dstore := dssync.MutexWrap(ds.NewNullDatastore())
87+
bs := bstore.NewBlockstore(dstore, bstore.WriteThrough())
88+
addblockstore = bstore.NewGCBlockstore(bs, nil) // gclocker will never be used
89+
exch = nil // exchange will never be used
90+
pinning = nil // pinner will never be used
11891
}
11992

12093
bserv := blockservice.New(addblockstore, exch) // hash security 001
@@ -133,11 +106,11 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
133106
syncDserv = &syncDagService{
134107
DAGService: dserv,
135108
syncFn: func() error {
136-
ds := api.repo.Datastore()
137-
if err := ds.Sync(ctx, bstore.BlockPrefix); err != nil {
109+
rds := api.repo.Datastore()
110+
if err := rds.Sync(ctx, bstore.BlockPrefix); err != nil {
138111
return err
139112
}
140-
return ds.Sync(ctx, filestore.FilestorePrefix)
113+
return rds.Sync(ctx, filestore.FilestorePrefix)
141114
},
142115
}
143116
}

core/node/builder.go

+1-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"crypto/rand"
66
"encoding/base64"
7-
"errors"
87

98
"go.uber.org/fx"
109

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

37-
// If NilRepo is set, a Repo backed by a nil datastore will be constructed
38-
NilRepo bool
39-
4036
Routing libp2p.RoutingOption
4137
Host libp2p.HostOption
4238
Repo repo.Repo
@@ -51,18 +47,8 @@ func (cfg *BuildCfg) getOpt(key string) bool {
5147
}
5248

5349
func (cfg *BuildCfg) fillDefaults() error {
54-
if cfg.Repo != nil && cfg.NilRepo {
55-
return errors.New("cannot set a Repo and specify nilrepo at the same time")
56-
}
57-
5850
if cfg.Repo == nil {
59-
var d ds.Datastore
60-
if cfg.NilRepo {
61-
d = ds.NewNullDatastore()
62-
} else {
63-
d = ds.NewMapDatastore()
64-
}
65-
r, err := defaultRepo(dsync.MutexWrap(d))
51+
r, err := defaultRepo(dsync.MutexWrap(ds.NewMapDatastore()))
6652
if err != nil {
6753
return err
6854
}

core/node/groups.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func Storage(bcfg *BuildCfg, cfg *config.Config) fx.Option {
178178
return fx.Options(
179179
fx.Provide(RepoConfig),
180180
fx.Provide(Datastore),
181-
fx.Provide(BaseBlockstoreCtor(cacheOpts, bcfg.NilRepo, cfg.Datastore.HashOnRead)),
181+
fx.Provide(BaseBlockstoreCtor(cacheOpts, cfg.Datastore.HashOnRead)),
182182
finalBstore,
183183
)
184184
}

core/node/storage.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,14 @@ func Datastore(repo repo.Repo) datastore.Datastore {
2727
type BaseBlocks blockstore.Blockstore
2828

2929
// BaseBlockstoreCtor creates cached blockstore backed by the provided datastore
30-
func BaseBlockstoreCtor(cacheOpts blockstore.CacheOpts, nilRepo bool, hashOnRead bool) func(mctx helpers.MetricsCtx, repo repo.Repo, lc fx.Lifecycle) (bs BaseBlocks, err error) {
30+
func BaseBlockstoreCtor(cacheOpts blockstore.CacheOpts, hashOnRead bool) func(mctx helpers.MetricsCtx, repo repo.Repo, lc fx.Lifecycle) (bs BaseBlocks, err error) {
3131
return func(mctx helpers.MetricsCtx, repo repo.Repo, lc fx.Lifecycle) (bs BaseBlocks, err error) {
3232
// hash security
3333
bs = blockstore.NewBlockstore(repo.Datastore())
3434
bs = &verifbs.VerifBS{Blockstore: bs}
35-
36-
if !nilRepo {
37-
bs, err = blockstore.CachedBlockstore(helpers.LifecycleCtx(mctx, lc), bs, cacheOpts)
38-
if err != nil {
39-
return nil, err
40-
}
35+
bs, err = blockstore.CachedBlockstore(helpers.LifecycleCtx(mctx, lc), bs, cacheOpts)
36+
if err != nil {
37+
return nil, err
4138
}
4239

4340
bs = blockstore.NewIdStore(bs)

0 commit comments

Comments
 (0)