github.com/celestiaorg/celestia-node@v0.15.0-beta.1/share/eds/adapters.go (about) 1 package eds 2 3 import ( 4 "context" 5 "sync" 6 7 "github.com/filecoin-project/dagstore" 8 "github.com/ipfs/boxo/blockservice" 9 blocks "github.com/ipfs/go-block-format" 10 "github.com/ipfs/go-cid" 11 ) 12 13 var _ blockservice.BlockGetter = (*BlockGetter)(nil) 14 15 // NewBlockGetter creates new blockservice.BlockGetter adapter from dagstore.ReadBlockstore 16 func NewBlockGetter(store dagstore.ReadBlockstore) *BlockGetter { 17 return &BlockGetter{store: store} 18 } 19 20 // BlockGetter is an adapter for dagstore.ReadBlockstore to implement blockservice.BlockGetter 21 // interface. 22 type BlockGetter struct { 23 store dagstore.ReadBlockstore 24 } 25 26 // GetBlock gets the requested block by the given CID. 27 func (bg *BlockGetter) GetBlock(ctx context.Context, cid cid.Cid) (blocks.Block, error) { 28 return bg.store.Get(ctx, cid) 29 } 30 31 // GetBlocks does a batch request for the given cids, returning blocks as 32 // they are found, in no particular order. 33 // 34 // It implements blockservice.BlockGetter interface, that requires: 35 // It may not be able to find all requested blocks (or the context may 36 // be canceled). In that case, it will close the channel early. It is up 37 // to the consumer to detect this situation and keep track which blocks 38 // it has received and which it hasn't. 39 func (bg *BlockGetter) GetBlocks(ctx context.Context, cids []cid.Cid) <-chan blocks.Block { 40 bCh := make(chan blocks.Block) 41 42 go func() { 43 var wg sync.WaitGroup 44 wg.Add(len(cids)) 45 for _, c := range cids { 46 go func(cid cid.Cid) { 47 defer wg.Done() 48 block, err := bg.store.Get(ctx, cid) 49 if err != nil { 50 log.Debugw("getblocks: error getting block by cid", "cid", cid, "error", err) 51 return 52 } 53 54 select { 55 case bCh <- block: 56 case <-ctx.Done(): 57 return 58 } 59 }(c) 60 } 61 wg.Wait() 62 close(bCh) 63 }() 64 65 return bCh 66 }