Skip to content

Commit 6d5475f

Browse files
committed
blockservice: add WithProvider option
This allows to recreate the behavior of advertising added blocks the bitswap server used to do.
1 parent bbe356c commit 6d5475f

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The following emojis are used to highlight certain changes:
1818

1919
- `bitswap` & `bitswap/client` now have a `WithContentSearch` option, this pickup the content routing job from `bitswap/network`.
2020
It used to be a commun pattern for consumers which do not need external content routing to pass a [`routinghelpers.Null`](https://pkg.go.dev/github.com/libp2p/go-libp2p-routing-helpers#Null), now this can be ommited completely which is more efficient.
21+
- `blockservice` now have a `WithProvider` option, this allows to recreate the behavior of advertising added blocks the bitswap server used to do.
2122

2223
### Changed
2324

blockservice/blockservice.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/ipfs/boxo/blockstore"
1515
"github.com/ipfs/boxo/exchange"
16+
"github.com/ipfs/boxo/provider"
1617
"github.com/ipfs/boxo/verifcid"
1718
blocks "github.com/ipfs/go-block-format"
1819
"github.com/ipfs/go-cid"
@@ -75,6 +76,7 @@ type blockService struct {
7576
allowlist verifcid.Allowlist
7677
blockstore blockstore.Blockstore
7778
exchange exchange.Interface
79+
provider provider.Provider
7880
// If checkFirst is true then first check that a block doesn't
7981
// already exist to avoid republishing the block on the exchange.
8082
checkFirst bool
@@ -97,6 +99,13 @@ func WithAllowlist(allowlist verifcid.Allowlist) Option {
9799
}
98100
}
99101

102+
// WithProvider allows to advertise anything that is added through the blockservice.
103+
func WithProvider(prov provider.Provider) Option {
104+
return func(bs *blockService) {
105+
bs.provider = prov
106+
}
107+
}
108+
100109
// New creates a BlockService with given datastore instance.
101110
func New(bs blockstore.Blockstore, exchange exchange.Interface, opts ...Option) BlockService {
102111
if exchange == nil {
@@ -196,6 +205,11 @@ func (s *blockService) AddBlock(ctx context.Context, o blocks.Block) error {
196205
logger.Errorf("NotifyNewBlocks: %s", err.Error())
197206
}
198207
}
208+
if s.provider != nil {
209+
if err := s.provider.Provide(o.Cid()); err != nil {
210+
logger.Errorf("Provide: %s", err.Error())
211+
}
212+
}
199213

200214
return nil
201215
}
@@ -242,6 +256,14 @@ func (s *blockService) AddBlocks(ctx context.Context, bs []blocks.Block) error {
242256
logger.Errorf("NotifyNewBlocks: %s", err.Error())
243257
}
244258
}
259+
if s.provider != nil {
260+
for _, o := range toput {
261+
if err := s.provider.Provide(o.Cid()); err != nil {
262+
logger.Errorf("Provide: %s", err.Error())
263+
}
264+
}
265+
}
266+
245267
return nil
246268
}
247269

blockservice/blockservice_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,31 @@ func TestAllowlist(t *testing.T) {
288288
check(blockservice.GetBlock)
289289
check(NewSession(ctx, blockservice).GetBlock)
290290
}
291+
292+
type mockProvider []cid.Cid
293+
294+
func (p *mockProvider) Provide(c cid.Cid) error {
295+
*p = append(*p, c)
296+
return nil
297+
}
298+
299+
func TestProviding(t *testing.T) {
300+
t.Parallel()
301+
a := assert.New(t)
302+
303+
bgen := butil.NewBlockGenerator()
304+
blocks := bgen.Blocks(3)
305+
306+
prov := mockProvider{}
307+
blockservice := New(blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())), nil, WithProvider(&prov))
308+
var added []cid.Cid
309+
310+
a.NoError(blockservice.AddBlock(context.Background(), blocks[0]))
311+
added = append(added, blocks[0].Cid())
312+
313+
a.NoError(blockservice.AddBlocks(context.Background(), blocks[1:]))
314+
added = append(added, blocks[1].Cid())
315+
added = append(added, blocks[2].Cid())
316+
317+
a.ElementsMatch(added, []cid.Cid(prov))
318+
}

examples/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ require (
5858
github.com/huin/goupnp v1.3.0 // indirect
5959
github.com/ipfs/bbloom v0.0.4 // indirect
6060
github.com/ipfs/go-bitfield v1.1.0 // indirect
61+
github.com/ipfs/go-cidutil v0.1.0 // indirect
6162
github.com/ipfs/go-ipfs-delay v0.0.1 // indirect
6263
github.com/ipfs/go-ipfs-pq v0.0.3 // indirect
6364
github.com/ipfs/go-ipfs-redirects-file v0.1.1 // indirect

examples/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUP
251251
github.com/ipfs/go-cid v0.0.6/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
252252
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
253253
github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk=
254+
github.com/ipfs/go-cidutil v0.1.0 h1:RW5hO7Vcf16dplUU60Hs0AKDkQAVPVplr7lk97CFL+Q=
255+
github.com/ipfs/go-cidutil v0.1.0/go.mod h1:e7OEVBMIv9JaOxt9zaGEmAoSlXW9jdFZ5lP/0PwcfpA=
254256
github.com/ipfs/go-datastore v0.6.0 h1:JKyz+Gvz1QEZw0LsX1IBn+JFCJQH4SJVFtM4uWU0Myk=
255257
github.com/ipfs/go-datastore v0.6.0/go.mod h1:rt5M3nNbSO/8q1t4LNkLyUwRs8HupMeN/8O4Vn9YAT8=
256258
github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk=

0 commit comments

Comments
 (0)