coreapi/unixfs: don't create an additional IpfsNode for --only-hash
Michael Muré committed
Oct 20, 2023 at 16:24 UTC
a3483e352e336cfa7cfa714ecdcd7e6a0a82ee03
4 files changed
+22
-66
core/coreapi/unixfs.go
+16
-43
@@ -3,14 +3,6 @@ package coreapi
3
import (
4
"context"
5
"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"
6
7
blockservice "github.com/ipfs/boxo/blockservice"
8
bstore "github.com/ipfs/boxo/blockstore"
@@ -21,41 +13,23 @@ import (
13
ft "github.com/ipfs/boxo/ipld/unixfs"
14
unixfile "github.com/ipfs/boxo/ipld/unixfs/file"
15
uio "github.com/ipfs/boxo/ipld/unixfs/io"
24
- mfs "github.com/ipfs/boxo/mfs"
16
+ "github.com/ipfs/boxo/mfs"
17
"github.com/ipfs/boxo/path"
18
cid "github.com/ipfs/go-cid"
19
cidutil "github.com/ipfs/go-cidutil"
20
+ ds "github.com/ipfs/go-datastore"
21
+ dssync "github.com/ipfs/go-datastore/sync"
22
ipld "github.com/ipfs/go-ipld-format"
23
coreiface "github.com/ipfs/kubo/core/coreiface"
24
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"
29
)
30
31
type UnixfsAPI CoreAPI
32
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
-
33
// Add builds a merkledag node from a reader, adds it to the blockstore,
34
// and returns the key representing that node.
35
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
82
pinning := api.pinning
83
84
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
91
}
92
93
bserv := blockservice.New(addblockstore, exch) // hash security 001
@@ -133,11 +106,11 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
106
syncDserv = &syncDagService{
107
DAGService: dserv,
108
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 {
111
return err
112
}
140
- return ds.Sync(ctx, filestore.FilestorePrefix)
113
+ return rds.Sync(ctx, filestore.FilestorePrefix)
114
},
115
}
116
}
core/node/builder.go
+1
-15
@@ -4,7 +4,6 @@ import (
4
"context"
5
"crypto/rand"
6
"encoding/base64"
7
- "errors"
7
8
"go.uber.org/fx"
9
@@ -34,9 +33,6 @@ type BuildCfg struct {
33
// DO NOT SET THIS UNLESS YOU'RE TESTING.
34
DisableEncryptedConnections bool
35
37
- // If NilRepo is set, a Repo backed by a nil datastore will be constructed
38
- NilRepo bool
39
-
36
Routing libp2p.RoutingOption
37
Host libp2p.HostOption
38
Repo repo.Repo
@@ -51,18 +47,8 @@ func (cfg *BuildCfg) getOpt(key string) bool {
47
}
48
49
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
-
50
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()))
52
if err != nil {
53
return err
54
}
core/node/groups.go
+1
-1
@@ -178,7 +178,7 @@ func Storage(bcfg *BuildCfg, cfg *config.Config) fx.Option {
178
return fx.Options(
179
fx.Provide(RepoConfig),
180
fx.Provide(Datastore),
181
- fx.Provide(BaseBlockstoreCtor(cacheOpts, bcfg.NilRepo, cfg.Datastore.HashOnRead)),
181
+ fx.Provide(BaseBlockstoreCtor(cacheOpts, cfg.Datastore.HashOnRead)),
182
finalBstore,
183
)
184
}
core/node/storage.go
+4
-7
@@ -27,17 +27,14 @@ func Datastore(repo repo.Repo) datastore.Datastore {
27
type BaseBlocks blockstore.Blockstore
28
29
// 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) {
31
return func(mctx helpers.MetricsCtx, repo repo.Repo, lc fx.Lifecycle) (bs BaseBlocks, err error) {
32
// hash security
33
bs = blockstore.NewBlockstore(repo.Datastore())
34
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
38
}
39
40
bs = blockstore.NewIdStore(bs)