@@ -99,24 +99,6 @@ func New(bs blockstore.Blockstore, exchange exchange.Interface, opts ...Option)
99
99
return service
100
100
}
101
101
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
-
120
102
// NewSession creates a new session that allows for
121
103
// controlled exchange of wantlists to decrease the bandwidth overhead.
122
104
// 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
257
239
}
258
240
}
259
241
260
- blockstore := s .Blockstore ()
261
-
262
- block , err := blockstore .Get (ctx , c )
242
+ block , err := s .blockstore .Get (ctx , c )
263
243
switch {
264
244
case err == nil :
265
245
return block , nil
@@ -281,12 +261,12 @@ func (s *BlockService) getBlock(ctx context.Context, c cid.Cid, fetchFactory fun
281
261
return nil , err
282
262
}
283
263
// 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 )
285
265
if err != nil {
286
266
return nil , err
287
267
}
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 )
290
270
if err != nil {
291
271
return nil , err
292
272
}
@@ -352,11 +332,9 @@ func (s *BlockService) getBlocks(ctx context.Context, ks []cid.Cid, fetchFactory
352
332
ks = ks2
353
333
}
354
334
355
- bs := s .Blockstore ()
356
-
357
335
var misses []cid.Cid
358
336
for _ , c := range ks {
359
- hit , err := bs .Get (ctx , c )
337
+ hit , err := s . blockstore .Get (ctx , c )
360
338
if err != nil {
361
339
misses = append (misses , c )
362
340
continue
@@ -379,7 +357,6 @@ func (s *BlockService) getBlocks(ctx context.Context, ks []cid.Cid, fetchFactory
379
357
return
380
358
}
381
359
382
- ex := s .Exchange ()
383
360
var cache [1 ]blocks.Block // preallocate once for all iterations
384
361
for {
385
362
var b blocks.Block
@@ -394,16 +371,16 @@ func (s *BlockService) getBlocks(ctx context.Context, ks []cid.Cid, fetchFactory
394
371
}
395
372
396
373
// write in the blockstore for caching
397
- err = bs .Put (ctx , b )
374
+ err = s . blockstore .Put (ctx , b )
398
375
if err != nil {
399
376
logger .Errorf ("could not write blocks from the network to the blockstore: %s" , err )
400
377
return
401
378
}
402
379
403
- if ex != nil {
380
+ if s . exchange != nil {
404
381
// inform the exchange that the blocks are available
405
382
cache [0 ] = b
406
- err = ex .NotifyNewBlocks (ctx , cache [:]... )
383
+ err = s . exchange .NotifyNewBlocks (ctx , cache [:]... )
407
384
if err != nil {
408
385
logger .Errorf ("could not tell the exchange about new blocks: %s" , err )
409
386
return
@@ -456,14 +433,13 @@ func (s *Session) grabSession() exchange.Fetcher {
456
433
s .sesctx = nil // early gc
457
434
}()
458
435
459
- ex := s .bs .Exchange ()
460
- if ex == nil {
436
+ if s .bs .exchange == nil {
461
437
return
462
438
}
463
- s .ses = ex // always fallback to non session fetches
464
439
465
- sesEx , ok := ex .(exchange.SessionExchange )
440
+ sesEx , ok := s . bs . exchange .(exchange.SessionExchange )
466
441
if ! ok {
442
+ s .ses = s .bs .exchange // always fallback to non session fetches
467
443
return
468
444
}
469
445
s .ses = sesEx .NewSession (s .sesctx )
@@ -526,3 +502,17 @@ func grabSessionFromContext(ctx context.Context, bs *BlockService) *Session {
526
502
527
503
return ss
528
504
}
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
+ }
0 commit comments