Skip to content

Commit dae471e

Browse files
committed
chore: update boxo for bitswap providing refactor
Keeps in sync with: - ipfs/boxo#578 - ipfs/boxo#534 - ipfs/boxo#535 - ipfs/boxo#536
1 parent 1514785 commit dae471e

File tree

19 files changed

+103
-65
lines changed

19 files changed

+103
-65
lines changed

core/commands/bitswap.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ var bitswapStatCmd = &cmds.Command{
134134
human, _ := req.Options[bitswapHumanOptionName].(bool)
135135

136136
fmt.Fprintln(w, "bitswap status")
137-
fmt.Fprintf(w, "\tprovides buffer: %d / %d\n", s.ProvideBufLen, bitswap.HasBlockBufferSize)
138137
fmt.Fprintf(w, "\tblocks received: %d\n", s.BlocksReceived)
139138
fmt.Fprintf(w, "\tblocks sent: %d\n", s.BlocksSent)
140139
if human {

core/commands/files.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"github.com/ipfs/kubo/core"
1515
"github.com/ipfs/kubo/core/commands/cmdenv"
1616

17-
bservice "github.com/ipfs/boxo/blockservice"
18-
offline "github.com/ipfs/boxo/exchange/offline"
1917
dag "github.com/ipfs/boxo/ipld/merkledag"
2018
ft "github.com/ipfs/boxo/ipld/unixfs"
2119
mfs "github.com/ipfs/boxo/mfs"
@@ -162,11 +160,7 @@ var filesStatCmd = &cmds.Command{
162160

163161
var dagserv ipld.DAGService
164162
if withLocal {
165-
// an offline DAGService will not fetch from the network
166-
dagserv = dag.NewDAGService(bservice.New(
167-
node.Blockstore,
168-
offline.Exchange(node.Blockstore),
169-
))
163+
dagserv = node.OfflineDAG
170164
} else {
171165
dagserv = node.DAG
172166
}

core/commands/pin/pin.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"os"
99
"time"
1010

11-
bserv "github.com/ipfs/boxo/blockservice"
12-
offline "github.com/ipfs/boxo/exchange/offline"
1311
dag "github.com/ipfs/boxo/ipld/merkledag"
1412
verifcid "github.com/ipfs/boxo/verifcid"
1513
cid "github.com/ipfs/go-cid"
@@ -738,8 +736,7 @@ type pinVerifyOpts struct {
738736
func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts, enc cidenc.Encoder) (<-chan any, error) {
739737
visited := make(map[cid.Cid]PinStatus)
740738

741-
bs := n.Blocks.Blockstore()
742-
DAG := dag.NewDAGService(bserv.New(bs, offline.Exchange(bs)))
739+
DAG := n.OfflineDAG
743740
getLinks := dag.GetLinksWithDAG(DAG)
744741

745742
var checkPin func(root cid.Cid) PinStatus

core/core.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ type IpfsNode struct {
8181
BaseBlocks node.BaseBlocks // the raw blockstore, no filestore wrapping
8282
GCLocker bstore.GCLocker // the locker used to protect the blockstore during gc
8383
Blocks bserv.BlockService // the block service, get/add blocks.
84+
OfflineBlocks bserv.BlockService `name:"offlineBlockService"` // blockservice which doesn't try to fetch from the network
8485
DAG ipld.DAGService // the merkle dag service, get/add objects.
86+
OfflineDAG ipld.DAGService `name:"offlineDagService"` // merkle dag service which doesn't try to fetch from the network
8587
IPLDFetcherFactory fetcher.Factory `name:"ipldFetcher"` // fetcher that paths over the IPLD data model
8688
UnixFSFetcherFactory fetcher.Factory `name:"unixfsFetcher"` // fetcher that interprets UnixFS data
8789
OfflineIPLDFetcherFactory fetcher.Factory `name:"offlineIpldFetcher"` // fetcher that paths over the IPLD data model without fetching new blocks

core/coreapi/coreapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
250250

251251
if settings.Offline || !settings.FetchBlocks {
252252
subAPI.exchange = offlinexch.Exchange(subAPI.blockstore)
253-
subAPI.blocks = bserv.New(subAPI.blockstore, subAPI.exchange)
253+
subAPI.blocks = bserv.New(subAPI.blockstore, nil, bserv.WithProvider(subAPI.provider))
254254
subAPI.dag = dag.NewDAGService(subAPI.blocks)
255255
}
256256

core/coreapi/pin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66

77
bserv "github.com/ipfs/boxo/blockservice"
8-
offline "github.com/ipfs/boxo/exchange/offline"
98
"github.com/ipfs/boxo/ipld/merkledag"
109
"github.com/ipfs/boxo/path"
1110
pin "github.com/ipfs/boxo/pinning/pinner"
@@ -195,7 +194,8 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro
195194

196195
visited := make(map[cid.Cid]*pinStatus)
197196
bs := api.blockstore
198-
DAG := merkledag.NewDAGService(bserv.New(bs, offline.Exchange(bs)))
197+
// FIXME: we are recreating a dag and blockservice, maybe offline varients should be shared ?
198+
DAG := merkledag.NewDAGService(bserv.New(bs, nil, bserv.WithProvider(api.provider)))
199199
getLinks := merkledag.GetLinksWithDAG(DAG)
200200

201201
var checkPin func(root cid.Cid) *pinStatus

core/coreapi/unixfs.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
106106
}
107107
exch := api.exchange
108108
pinning := api.pinning
109+
prov := api.provider
109110

110111
if settings.OnlyHash {
111112
node, err := getOrCreateNilNode()
@@ -115,9 +116,10 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
115116
addblockstore = node.Blockstore
116117
exch = node.Exchange
117118
pinning = node.Pinning
119+
prov = nil
118120
}
119121

120-
bserv := blockservice.New(addblockstore, exch) // hash security 001
122+
bserv := blockservice.New(addblockstore, exch, blockservice.WithProvider(prov)) // hash security 001
121123
dserv := merkledag.NewDAGService(bserv)
122124

123125
// add a sync call to the DagService

core/corehttp/gateway.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"net/http"
1010
"time"
1111

12-
"github.com/ipfs/boxo/blockservice"
13-
"github.com/ipfs/boxo/exchange/offline"
1412
"github.com/ipfs/boxo/files"
1513
"github.com/ipfs/boxo/gateway"
1614
"github.com/ipfs/boxo/namesys"
@@ -86,7 +84,7 @@ func VersionOption() ServeOption {
8684

8785
func Libp2pGatewayOption() ServeOption {
8886
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
89-
bserv := blockservice.New(n.Blocks.Blockstore(), offline.Exchange(n.Blocks.Blockstore()))
87+
bserv := n.OfflineBlocks
9088

9189
backend, err := gateway.NewBlocksBackend(bserv,
9290
// GatewayOverLibp2p only returns things that are in local blockstore
@@ -125,7 +123,7 @@ func newGatewayBackend(n *core.IpfsNode) (gateway.IPFSBackend, error) {
125123
pathResolver := n.UnixFSPathResolver
126124

127125
if cfg.Gateway.NoFetch {
128-
bserv = blockservice.New(bserv.Blockstore(), offline.Exchange(bserv.Blockstore()))
126+
bserv = n.OfflineBlocks
129127

130128
cs := cfg.Ipns.ResolveCacheSize
131129
if cs == 0 {

core/node/bitswap.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,16 @@ type bitswapOptionsOut struct {
3131
BitswapOpts []bitswap.Option `group:"bitswap-options,flatten"`
3232
}
3333

34-
// BitswapOptions creates configuration options for Bitswap from the config file
35-
// and whether to provide data.
36-
func BitswapOptions(cfg *config.Config, provide bool) interface{} {
37-
return func() bitswapOptionsOut {
34+
// BitswapOptions creates configuration options for Bitswap from the config file.
35+
func BitswapOptions(cfg *config.Config) fx.Option {
36+
return fx.Provide(func(routing irouting.ProvideManyRouter) bitswapOptionsOut {
3837
var internalBsCfg config.InternalBitswap
3938
if cfg.Internal.Bitswap != nil {
4039
internalBsCfg = *cfg.Internal.Bitswap
4140
}
4241

4342
opts := []bitswap.Option{
44-
bitswap.ProvideEnabled(provide),
43+
bitswap.WithContentSearch(routing),
4544
bitswap.ProviderSearchDelay(internalBsCfg.ProviderSearchDelay.WithDefault(DefaultProviderSearchDelay)), // See https://github.com/ipfs/go-ipfs/issues/8807 for rationale
4645
bitswap.EngineBlockstoreWorkerCount(int(internalBsCfg.EngineBlockstoreWorkerCount.WithDefault(DefaultEngineBlockstoreWorkerCount))),
4746
bitswap.TaskWorkerCount(int(internalBsCfg.TaskWorkerCount.WithDefault(DefaultTaskWorkerCount))),
@@ -50,25 +49,24 @@ func BitswapOptions(cfg *config.Config, provide bool) interface{} {
5049
}
5150

5251
return bitswapOptionsOut{BitswapOpts: opts}
53-
}
52+
})
5453
}
5554

5655
type onlineExchangeIn struct {
5756
fx.In
5857

5958
Mctx helpers.MetricsCtx
6059
Host host.Host
61-
Rt irouting.ProvideManyRouter
6260
Bs blockstore.GCBlockstore
6361
BitswapOpts []bitswap.Option `group:"bitswap-options"`
6462
}
6563

6664
// OnlineExchange creates new LibP2P backed block exchange (BitSwap).
6765
// Additional options to bitswap.New can be provided via the "bitswap-options"
6866
// group.
69-
func OnlineExchange() interface{} {
70-
return func(in onlineExchangeIn, lc fx.Lifecycle) exchange.Interface {
71-
bitswapNetwork := network.NewFromIpfsHost(in.Host, in.Rt)
67+
func OnlineExchange() fx.Option {
68+
return fx.Provide(func(in onlineExchangeIn, lc fx.Lifecycle) exchange.Interface {
69+
bitswapNetwork := network.NewFromIpfsHost(in.Host)
7270

7371
exch := bitswap.New(helpers.LifecycleCtx(in.Mctx, lc), bitswapNetwork, in.Bs, in.BitswapOpts...)
7472
lc.Append(fx.Hook{
@@ -77,5 +75,5 @@ func OnlineExchange() interface{} {
7775
},
7876
})
7977
return exch
80-
}
78+
})
8179
}

core/node/core.go

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/ipfs/boxo/blockservice"
88
blockstore "github.com/ipfs/boxo/blockstore"
99
exchange "github.com/ipfs/boxo/exchange"
10-
offline "github.com/ipfs/boxo/exchange/offline"
1110
"github.com/ipfs/boxo/fetcher"
1211
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
1312
"github.com/ipfs/boxo/filestore"
@@ -17,6 +16,7 @@ import (
1716
pathresolver "github.com/ipfs/boxo/path/resolver"
1817
pin "github.com/ipfs/boxo/pinning/pinner"
1918
"github.com/ipfs/boxo/pinning/pinner/dspinner"
19+
"github.com/ipfs/boxo/provider"
2020
"github.com/ipfs/go-cid"
2121
"github.com/ipfs/go-datastore"
2222
format "github.com/ipfs/go-ipld-format"
@@ -29,8 +29,8 @@ import (
2929
)
3030

3131
// BlockService creates new blockservice which provides an interface to fetch content-addressable blocks
32-
func BlockService(lc fx.Lifecycle, bs blockstore.Blockstore, rem exchange.Interface) blockservice.BlockService {
33-
bsvc := blockservice.New(bs, rem)
32+
func BlockService(lc fx.Lifecycle, bs blockstore.Blockstore, rem exchange.Interface, prov provider.System) blockservice.BlockService {
33+
bsvc := blockservice.New(bs, rem, blockservice.WithProvider(prov))
3434

3535
lc.Append(fx.Hook{
3636
OnStop: func(ctx context.Context) error {
@@ -41,6 +41,32 @@ func BlockService(lc fx.Lifecycle, bs blockstore.Blockstore, rem exchange.Interf
4141
return bsvc
4242
}
4343

44+
type offlineIn struct {
45+
fx.In
46+
47+
Bs blockstore.Blockstore
48+
Prov provider.System `optional:"true"`
49+
}
50+
51+
type offlineOut struct {
52+
fx.Out
53+
54+
Bs blockservice.BlockService `name:"offlineBlockService"`
55+
}
56+
57+
// OfflineBlockservice is like [BlockService] but it makes an offline version.
58+
func OfflineBlockservice(lc fx.Lifecycle, in offlineIn) offlineOut {
59+
bsvc := blockservice.New(in.Bs, nil, blockservice.WithProvider(in.Prov))
60+
61+
lc.Append(fx.Hook{
62+
OnStop: func(ctx context.Context) error {
63+
return bsvc.Close()
64+
},
65+
})
66+
67+
return offlineOut{Bs: bsvc}
68+
}
69+
4470
// Pinning creates new pinner which tells GC which blocks should be kept
4571
func Pinning(bstore blockstore.Blockstore, ds format.DAGService, repo repo.Repo) (pin.Pinner, error) {
4672
rootDS := repo.Datastore()
@@ -82,38 +108,34 @@ func (s *syncDagService) Session(ctx context.Context) format.NodeGetter {
82108
return merkledag.NewSession(ctx, s.DAGService)
83109
}
84110

85-
// FetchersOut allows injection of fetchers.
86-
type FetchersOut struct {
111+
// fetchersOut allows injection of fetchers.
112+
type fetchersOut struct {
87113
fx.Out
88114
IPLDFetcher fetcher.Factory `name:"ipldFetcher"`
89115
UnixfsFetcher fetcher.Factory `name:"unixfsFetcher"`
90116
OfflineIPLDFetcher fetcher.Factory `name:"offlineIpldFetcher"`
91117
OfflineUnixfsFetcher fetcher.Factory `name:"offlineUnixfsFetcher"`
92118
}
93119

94-
// FetchersIn allows using fetchers for other dependencies.
95-
type FetchersIn struct {
120+
type fetcherIn struct {
96121
fx.In
97-
IPLDFetcher fetcher.Factory `name:"ipldFetcher"`
98-
UnixfsFetcher fetcher.Factory `name:"unixfsFetcher"`
99-
OfflineIPLDFetcher fetcher.Factory `name:"offlineIpldFetcher"`
100-
OfflineUnixfsFetcher fetcher.Factory `name:"offlineUnixfsFetcher"`
122+
Online blockservice.BlockService
123+
Offline blockservice.BlockService `name:"offlineBlockService"`
101124
}
102125

103126
// FetcherConfig returns a fetcher config that can build new fetcher instances
104-
func FetcherConfig(bs blockservice.BlockService) FetchersOut {
105-
ipldFetcher := bsfetcher.NewFetcherConfig(bs)
127+
func FetcherConfig(in fetcherIn) fetchersOut {
128+
ipldFetcher := bsfetcher.NewFetcherConfig(in.Online)
106129
ipldFetcher.PrototypeChooser = dagpb.AddSupportToChooser(bsfetcher.DefaultPrototypeChooser)
107130
unixFSFetcher := ipldFetcher.WithReifier(unixfsnode.Reify)
108131

109132
// Construct offline versions which we can safely use in contexts where
110133
// path resolution should not fetch new blocks via exchange.
111-
offlineBs := blockservice.New(bs.Blockstore(), offline.Exchange(bs.Blockstore()))
112-
offlineIpldFetcher := bsfetcher.NewFetcherConfig(offlineBs)
134+
offlineIpldFetcher := bsfetcher.NewFetcherConfig(in.Offline)
113135
offlineIpldFetcher.PrototypeChooser = dagpb.AddSupportToChooser(bsfetcher.DefaultPrototypeChooser)
114136
offlineUnixFSFetcher := offlineIpldFetcher.WithReifier(unixfsnode.Reify)
115137

116-
return FetchersOut{
138+
return fetchersOut{
117139
IPLDFetcher: ipldFetcher,
118140
UnixfsFetcher: unixFSFetcher,
119141
OfflineIPLDFetcher: offlineIpldFetcher,
@@ -130,8 +152,17 @@ type PathResolversOut struct {
130152
OfflineUnixFSPathResolver pathresolver.Resolver `name:"offlineUnixFSPathResolver"`
131153
}
132154

155+
// PathResolverIn allows using fetchers for other dependencies.
156+
type PathResolverIn struct {
157+
fx.In
158+
IPLDFetcher fetcher.Factory `name:"ipldFetcher"`
159+
UnixfsFetcher fetcher.Factory `name:"unixfsFetcher"`
160+
OfflineIPLDFetcher fetcher.Factory `name:"offlineIpldFetcher"`
161+
OfflineUnixfsFetcher fetcher.Factory `name:"offlineUnixfsFetcher"`
162+
}
163+
133164
// PathResolverConfig creates path resolvers with the given fetchers.
134-
func PathResolverConfig(fetchers FetchersIn) PathResolversOut {
165+
func PathResolverConfig(fetchers PathResolverIn) PathResolversOut {
135166
return PathResolversOut{
136167
IPLDPathResolver: pathresolver.NewBasicResolver(fetchers.IPLDFetcher),
137168
UnixFSPathResolver: pathresolver.NewBasicResolver(fetchers.UnixfsFetcher),
@@ -145,6 +176,23 @@ func Dag(bs blockservice.BlockService) format.DAGService {
145176
return merkledag.NewDAGService(bs)
146177
}
147178

179+
type offlineDagIn struct {
180+
fx.In
181+
182+
Bs blockservice.BlockService `name:"offlineBlockService"`
183+
}
184+
185+
type offlineDagOut struct {
186+
fx.Out
187+
188+
DAG format.DAGService `name:"offlineDagService"`
189+
}
190+
191+
// OfflineDag is like [Dag] but it makes an offline version.
192+
func OfflineDag(lc fx.Lifecycle, in offlineDagIn) offlineDagOut {
193+
return offlineDagOut{DAG: merkledag.NewDAGService(in.Bs)}
194+
}
195+
148196
// Files loads persisted MFS root
149197
func Files(mctx helpers.MetricsCtx, lc fx.Lifecycle, repo repo.Repo, dag format.DAGService) (*mfs.Root, error) {
150198
dsk := datastore.NewKey("/local/filesroot")

core/node/groups.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,9 @@ func Online(bcfg *BuildCfg, cfg *config.Config, userResourceOverrides rcmgr.Part
266266
recordLifetime = d
267267
}
268268

269-
/* don't provide from bitswap when the strategic provider service is active */
270-
shouldBitswapProvide := !cfg.Experimental.StrategicProviding
271-
272269
return fx.Options(
273-
fx.Provide(BitswapOptions(cfg, shouldBitswapProvide)),
274-
fx.Provide(OnlineExchange()),
270+
BitswapOptions(cfg),
271+
OnlineExchange(),
275272
fx.Provide(DNSResolver),
276273
fx.Provide(Namesys(ipnsCacheSize, cfg.Ipns.MaxCacheTTL.WithDefault(config.DefaultIpnsMaxCacheTTL))),
277274
fx.Provide(Peering),
@@ -307,7 +304,9 @@ func Offline(cfg *config.Config) fx.Option {
307304
// Core groups basic IPFS services
308305
var Core = fx.Options(
309306
fx.Provide(BlockService),
307+
fx.Provide(OfflineBlockservice),
310308
fx.Provide(Dag),
309+
fx.Provide(OfflineDag),
311310
fx.Provide(FetcherConfig),
312311
fx.Provide(PathResolverConfig),
313312
fx.Provide(Pinning),

docs/examples/kubo-as-a-library/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ go 1.20
77
replace github.com/ipfs/kubo => ./../../..
88

99
require (
10-
github.com/ipfs/boxo v0.17.1-0.20240206084652-79cb4e2886d7
10+
github.com/ipfs/boxo v0.17.1-0.20240216140830-2e813d83999c
1111
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
1212
github.com/libp2p/go-libp2p v0.32.2
1313
github.com/multiformats/go-multiaddr v0.12.2

docs/examples/kubo-as-a-library/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c h1:7Uy
260260
github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c/go.mod h1:6EekK/jo+TynwSE/ZOiOJd4eEvRXoavEC3vquKtv4yI=
261261
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
262262
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
263-
github.com/ipfs/boxo v0.17.1-0.20240206084652-79cb4e2886d7 h1:1xhvfhNpPSJZ6GavPT6MuR15HhN4azBQvu7wsziJph4=
264-
github.com/ipfs/boxo v0.17.1-0.20240206084652-79cb4e2886d7/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
263+
github.com/ipfs/boxo v0.17.1-0.20240216140830-2e813d83999c h1:4wRWKU3JeuX90CrKA2u6/VuN/eptDLJJZ6RgOwwGkBs=
264+
github.com/ipfs/boxo v0.17.1-0.20240216140830-2e813d83999c/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
265265
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
266266
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
267267
github.com/ipfs/go-block-format v0.0.3/go.mod h1:4LmD4ZUw0mhO+JSKdpWwrzATiEfM7WWgQ8H5l6P8MVk=

0 commit comments

Comments
 (0)