|  | 
|  | 1 | +package remoteblockstore | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"context" | 
|  | 5 | +	"errors" | 
|  | 6 | + | 
|  | 7 | +	blocks "github.com/ipfs/go-block-format" | 
|  | 8 | +	logging "github.com/ipfs/go-log/v2" | 
|  | 9 | + | 
|  | 10 | +	"github.com/ipfs/go-cid" | 
|  | 11 | +	blockstore "github.com/ipfs/go-ipfs-blockstore" | 
|  | 12 | +) | 
|  | 13 | + | 
|  | 14 | +var log = logging.Logger("remote-blockstore") | 
|  | 15 | + | 
|  | 16 | +var _ blockstore.Blockstore = (*RemoteBlockstore)(nil) | 
|  | 17 | + | 
|  | 18 | +type RemoteBlockstoreAPI interface { | 
|  | 19 | +	BlockstoreGet(ctx context.Context, c cid.Cid) ([]byte, error) | 
|  | 20 | +	BlockstoreHas(ctx context.Context, c cid.Cid) (bool, error) | 
|  | 21 | +	BlockstoreGetSize(ctx context.Context, c cid.Cid) (int, error) | 
|  | 22 | +} | 
|  | 23 | + | 
|  | 24 | +// RemoteBlockstore is a read-only blockstore over all cids across all pieces on a provider. | 
|  | 25 | +type RemoteBlockstore struct { | 
|  | 26 | +	api RemoteBlockstoreAPI | 
|  | 27 | +} | 
|  | 28 | + | 
|  | 29 | +func NewRemoteBlockstore(api RemoteBlockstoreAPI) blockstore.Blockstore { | 
|  | 30 | +	return &RemoteBlockstore{ | 
|  | 31 | +		api: api, | 
|  | 32 | +	} | 
|  | 33 | +} | 
|  | 34 | + | 
|  | 35 | +func (ro *RemoteBlockstore) Get(ctx context.Context, c cid.Cid) (b blocks.Block, err error) { | 
|  | 36 | +	log.Debugw("Get", "cid", c) | 
|  | 37 | +	data, err := ro.api.BlockstoreGet(ctx, c) | 
|  | 38 | +	log.Debugw("Get response", "cid", c, "error", err) | 
|  | 39 | +	if err != nil { | 
|  | 40 | +		return nil, err | 
|  | 41 | +	} | 
|  | 42 | +	return blocks.NewBlockWithCid(data, c) | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +func (ro *RemoteBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) { | 
|  | 46 | +	log.Debugw("Has", "cid", c) | 
|  | 47 | +	has, err := ro.api.BlockstoreHas(ctx, c) | 
|  | 48 | +	log.Debugw("Has response", "cid", c, "has", has, "error", err) | 
|  | 49 | +	return has, err | 
|  | 50 | +} | 
|  | 51 | + | 
|  | 52 | +func (ro *RemoteBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) { | 
|  | 53 | +	log.Debugw("GetSize", "cid", c) | 
|  | 54 | +	size, err := ro.api.BlockstoreGetSize(ctx, c) | 
|  | 55 | +	log.Debugw("GetSize response", "cid", c, "size", size, "error", err) | 
|  | 56 | +	return size, err | 
|  | 57 | +} | 
|  | 58 | + | 
|  | 59 | +// --- UNSUPPORTED BLOCKSTORE METHODS ------- | 
|  | 60 | +func (ro *RemoteBlockstore) DeleteBlock(context.Context, cid.Cid) error { | 
|  | 61 | +	return errors.New("unsupported operation DeleteBlock") | 
|  | 62 | +} | 
|  | 63 | +func (ro *RemoteBlockstore) HashOnRead(_ bool) {} | 
|  | 64 | +func (ro *RemoteBlockstore) Put(context.Context, blocks.Block) error { | 
|  | 65 | +	return errors.New("unsupported operation Put") | 
|  | 66 | +} | 
|  | 67 | +func (ro *RemoteBlockstore) PutMany(context.Context, []blocks.Block) error { | 
|  | 68 | +	return errors.New("unsupported operation PutMany") | 
|  | 69 | +} | 
|  | 70 | +func (ro *RemoteBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) { | 
|  | 71 | +	return nil, errors.New("unsupported operation AllKeysChan") | 
|  | 72 | +} | 
0 commit comments