Skip to content

Commit 17b3ed9

Browse files
committed
blockservice: remove inner fields access methods
We shouldn't expose inner implementation details of the blockservice to consumers. For example by using the blockstore for the .Has call, the gateway skipped verifcid and blocker checks. Probably fine but not we want to do.
1 parent 5e08a77 commit 17b3ed9

File tree

3 files changed

+44
-49
lines changed

3 files changed

+44
-49
lines changed

blockservice/blockservice.go

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,6 @@ func New(bs blockstore.Blockstore, exchange exchange.Interface, opts ...Option)
9999
return service
100100
}
101101

102-
// Blockstore returns the blockstore behind this blockservice.
103-
func (s *BlockService) Blockstore() blockstore.Blockstore {
104-
return s.blockstore
105-
}
106-
107-
// Exchange returns the exchange behind this blockservice.
108-
func (s *BlockService) Exchange() exchange.Interface {
109-
return s.exchange
110-
}
111-
112-
func (s *BlockService) Allowlist() verifcid.Allowlist {
113-
return s.allowlist
114-
}
115-
116-
func (s *BlockService) Blocker() Blocker {
117-
return s.blocker
118-
}
119-
120102
// NewSession creates a new session that allows for
121103
// controlled exchange of wantlists to decrease the bandwidth overhead.
122104
// If the current exchange is a SessionExchange, a new exchange
@@ -257,9 +239,7 @@ func (s *BlockService) getBlock(ctx context.Context, c cid.Cid, fetchFactory fun
257239
}
258240
}
259241

260-
blockstore := s.Blockstore()
261-
262-
block, err := blockstore.Get(ctx, c)
242+
block, err := s.blockstore.Get(ctx, c)
263243
switch {
264244
case err == nil:
265245
return block, nil
@@ -281,12 +261,12 @@ func (s *BlockService) getBlock(ctx context.Context, c cid.Cid, fetchFactory fun
281261
return nil, err
282262
}
283263
// also write in the blockstore for caching, inform the exchange that the block is available
284-
err = blockstore.Put(ctx, blk)
264+
err = s.blockstore.Put(ctx, blk)
285265
if err != nil {
286266
return nil, err
287267
}
288-
if ex := s.Exchange(); ex != nil {
289-
err = ex.NotifyNewBlocks(ctx, blk)
268+
if s.exchange != nil {
269+
err = s.exchange.NotifyNewBlocks(ctx, blk)
290270
if err != nil {
291271
return nil, err
292272
}
@@ -352,11 +332,9 @@ func (s *BlockService) getBlocks(ctx context.Context, ks []cid.Cid, fetchFactory
352332
ks = ks2
353333
}
354334

355-
bs := s.Blockstore()
356-
357335
var misses []cid.Cid
358336
for _, c := range ks {
359-
hit, err := bs.Get(ctx, c)
337+
hit, err := s.blockstore.Get(ctx, c)
360338
if err != nil {
361339
misses = append(misses, c)
362340
continue
@@ -379,7 +357,6 @@ func (s *BlockService) getBlocks(ctx context.Context, ks []cid.Cid, fetchFactory
379357
return
380358
}
381359

382-
ex := s.Exchange()
383360
var cache [1]blocks.Block // preallocate once for all iterations
384361
for {
385362
var b blocks.Block
@@ -394,16 +371,16 @@ func (s *BlockService) getBlocks(ctx context.Context, ks []cid.Cid, fetchFactory
394371
}
395372

396373
// write in the blockstore for caching
397-
err = bs.Put(ctx, b)
374+
err = s.blockstore.Put(ctx, b)
398375
if err != nil {
399376
logger.Errorf("could not write blocks from the network to the blockstore: %s", err)
400377
return
401378
}
402379

403-
if ex != nil {
380+
if s.exchange != nil {
404381
// inform the exchange that the blocks are available
405382
cache[0] = b
406-
err = ex.NotifyNewBlocks(ctx, cache[:]...)
383+
err = s.exchange.NotifyNewBlocks(ctx, cache[:]...)
407384
if err != nil {
408385
logger.Errorf("could not tell the exchange about new blocks: %s", err)
409386
return
@@ -456,14 +433,13 @@ func (s *Session) grabSession() exchange.Fetcher {
456433
s.sesctx = nil // early gc
457434
}()
458435

459-
ex := s.bs.Exchange()
460-
if ex == nil {
436+
if s.bs.exchange == nil {
461437
return
462438
}
463-
s.ses = ex // always fallback to non session fetches
464439

465-
sesEx, ok := ex.(exchange.SessionExchange)
440+
sesEx, ok := s.bs.exchange.(exchange.SessionExchange)
466441
if !ok {
442+
s.ses = s.bs.exchange // always fallback to non session fetches
467443
return
468444
}
469445
s.ses = sesEx.NewSession(s.sesctx)
@@ -526,3 +502,17 @@ func grabSessionFromContext(ctx context.Context, bs *BlockService) *Session {
526502

527503
return ss
528504
}
505+
506+
func (s *BlockService) Has(ctx context.Context, c cid.Cid) (bool, error) {
507+
if err := verifcid.ValidateCid(s.allowlist, c); err != nil {
508+
return false, err
509+
}
510+
511+
if s.blocker != nil {
512+
if err := s.blocker(c); err != nil {
513+
return false, err
514+
}
515+
}
516+
517+
return s.blockstore.Has(ctx, c)
518+
}

gateway/blocks_backend.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/ipfs/boxo/blockservice"
14-
blockstore "github.com/ipfs/boxo/blockstore"
1514
"github.com/ipfs/boxo/fetcher"
1615
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
1716
"github.com/ipfs/boxo/files"
@@ -51,7 +50,6 @@ import (
5150

5251
// BlocksBackend is an [IPFSBackend] implementation based on a [blockservice.BlockService].
5352
type BlocksBackend struct {
54-
blockStore blockstore.Blockstore
5553
blockService *blockservice.BlockService
5654
dagService format.DAGService
5755
resolver resolver.Resolver
@@ -143,7 +141,6 @@ func NewBlocksBackend(blockService *blockservice.BlockService, opts ...BlocksBac
143141
}
144142

145143
return &BlocksBackend{
146-
blockStore: blockService.Blockstore(),
147144
blockService: blockService,
148145
dagService: dagService,
149146
resolver: r,
@@ -680,7 +677,7 @@ func (bb *BlocksBackend) IsCached(ctx context.Context, p path.Path) bool {
680677
return false
681678
}
682679

683-
has, _ := bb.blockStore.Has(ctx, rp.RootCid())
680+
has, _ := bb.blockService.Has(ctx, rp.RootCid())
684681
return has
685682
}
686683

ipld/merkledag/merkledag_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ import (
1616
"testing"
1717
"time"
1818

19+
testinstance "github.com/ipfs/boxo/bitswap/testinstance"
20+
tn "github.com/ipfs/boxo/bitswap/testnet"
1921
. "github.com/ipfs/boxo/ipld/merkledag"
2022
mdpb "github.com/ipfs/boxo/ipld/merkledag/pb"
2123
dstest "github.com/ipfs/boxo/ipld/merkledag/test"
24+
mockrouting "github.com/ipfs/boxo/routing/mock"
25+
delay "github.com/ipfs/go-ipfs-delay"
2226

2327
bserv "github.com/ipfs/boxo/blockservice"
2428
bstest "github.com/ipfs/boxo/blockservice/test"
@@ -507,10 +511,12 @@ func TestCantGet(t *testing.T) {
507511
}
508512

509513
func TestFetchGraph(t *testing.T) {
510-
var dservs []ipld.DAGService
511-
bsis := bstest.Mocks(2)
512-
for _, bsi := range bsis {
513-
dservs = append(dservs, NewDAGService(bsi))
514+
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(0))
515+
sg := testinstance.NewTestInstanceGenerator(net, nil, nil)
516+
instances := sg.Instances(2)
517+
dservs := [2]ipld.DAGService{
518+
NewDAGService(bserv.New(instances[0].Blockstore(), instances[0].Exchange)),
519+
NewDAGService(bserv.New(instances[1].Blockstore(), instances[1].Exchange)),
514520
}
515521

516522
read := io.LimitReader(u.NewTimeSeededRand(), 1024*32)
@@ -522,7 +528,7 @@ func TestFetchGraph(t *testing.T) {
522528
}
523529

524530
// create an offline dagstore and ensure all blocks were fetched
525-
bs := bserv.New(bsis[1].Blockstore(), offline.Exchange(bsis[1].Blockstore()))
531+
bs := bserv.New(instances[1].Blockstore(), nil)
526532

527533
offlineDS := NewDAGService(bs)
528534

@@ -547,10 +553,12 @@ func TestFetchGraphWithDepthLimit(t *testing.T) {
547553
}
548554

549555
testF := func(t *testing.T, tc testcase) {
550-
var dservs []ipld.DAGService
551-
bsis := bstest.Mocks(2)
552-
for _, bsi := range bsis {
553-
dservs = append(dservs, NewDAGService(bsi))
556+
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(0))
557+
sg := testinstance.NewTestInstanceGenerator(net, nil, nil)
558+
instances := sg.Instances(2)
559+
dservs := [2]ipld.DAGService{
560+
NewDAGService(bserv.New(instances[0].Blockstore(), instances[0].Exchange)),
561+
NewDAGService(bserv.New(instances[1].Blockstore(), instances[1].Exchange)),
554562
}
555563

556564
root := makeDepthTestingGraph(t, dservs[0])
@@ -561,7 +569,7 @@ func TestFetchGraphWithDepthLimit(t *testing.T) {
561569
}
562570

563571
// create an offline dagstore and ensure all blocks were fetched
564-
bs := bserv.New(bsis[1].Blockstore(), offline.Exchange(bsis[1].Blockstore()))
572+
bs := bserv.New(instances[1].Blockstore(), offline.Exchange(instances[1].Blockstore()))
565573

566574
offlineDS := NewDAGService(bs)
567575

0 commit comments

Comments
 (0)