2021-01-29 20:01:00 +00:00
|
|
|
package blockstore
|
2020-04-29 23:51:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-03-29 19:24:07 +00:00
|
|
|
"io"
|
2020-04-29 23:51:55 +00:00
|
|
|
|
2023-05-25 14:31:53 +00:00
|
|
|
iface "github.com/ipfs/boxo/coreiface"
|
|
|
|
"github.com/ipfs/boxo/coreiface/options"
|
|
|
|
"github.com/ipfs/boxo/coreiface/path"
|
2023-03-25 07:33:05 +00:00
|
|
|
blocks "github.com/ipfs/go-block-format"
|
2020-04-29 23:51:55 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2023-06-02 16:45:59 +00:00
|
|
|
"github.com/ipfs/kubo/client/rpc"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
"github.com/multiformats/go-multihash"
|
|
|
|
"golang.org/x/xerrors"
|
2020-04-29 23:51:55 +00:00
|
|
|
)
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
type IPFSBlockstore struct {
|
2021-02-01 13:22:32 +00:00
|
|
|
ctx context.Context
|
2021-01-31 21:24:20 +00:00
|
|
|
api, offlineAPI iface.CoreAPI
|
2020-04-29 23:51:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
var _ BasicBlockstore = (*IPFSBlockstore)(nil)
|
2021-01-29 20:01:00 +00:00
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
func NewLocalIPFSBlockstore(ctx context.Context, onlineMode bool) (Blockstore, error) {
|
2023-06-02 16:45:59 +00:00
|
|
|
localApi, err := rpc.NewLocalApi()
|
2020-04-29 23:51:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting local ipfs api: %w", err)
|
|
|
|
}
|
2020-10-29 18:16:45 +00:00
|
|
|
api, err := localApi.WithOptions(options.Api.Offline(!onlineMode))
|
2020-07-17 21:02:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setting offline mode: %s", err)
|
|
|
|
}
|
2021-02-28 19:55:23 +00:00
|
|
|
|
2021-01-31 21:24:20 +00:00
|
|
|
offlineAPI := api
|
|
|
|
if onlineMode {
|
|
|
|
offlineAPI, err = localApi.WithOptions(options.Api.Offline(true))
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("applying offline mode: %s", err)
|
|
|
|
}
|
2021-01-29 23:17:25 +00:00
|
|
|
}
|
2020-04-29 23:51:55 +00:00
|
|
|
|
2021-02-28 19:55:23 +00:00
|
|
|
bs := &IPFSBlockstore{
|
2021-02-01 13:22:32 +00:00
|
|
|
ctx: ctx,
|
|
|
|
api: api,
|
2021-01-31 21:24:20 +00:00
|
|
|
offlineAPI: offlineAPI,
|
2021-02-28 19:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Adapt(bs), nil
|
2020-04-29 23:51:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
func NewRemoteIPFSBlockstore(ctx context.Context, maddr multiaddr.Multiaddr, onlineMode bool) (Blockstore, error) {
|
2023-06-02 16:45:59 +00:00
|
|
|
httpApi, err := rpc.NewApi(maddr)
|
2020-07-17 21:02:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setting remote ipfs api: %w", err)
|
|
|
|
}
|
2020-10-29 18:16:45 +00:00
|
|
|
api, err := httpApi.WithOptions(options.Api.Offline(!onlineMode))
|
2020-05-22 14:51:18 +00:00
|
|
|
if err != nil {
|
2020-07-17 21:02:12 +00:00
|
|
|
return nil, xerrors.Errorf("applying offline mode: %s", err)
|
2020-05-22 14:51:18 +00:00
|
|
|
}
|
2021-02-28 19:55:23 +00:00
|
|
|
|
2021-01-31 21:24:20 +00:00
|
|
|
offlineAPI := api
|
|
|
|
if onlineMode {
|
|
|
|
offlineAPI, err = httpApi.WithOptions(options.Api.Offline(true))
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("applying offline mode: %s", err)
|
|
|
|
}
|
2021-01-29 23:17:25 +00:00
|
|
|
}
|
2020-05-22 14:51:18 +00:00
|
|
|
|
2021-02-28 19:55:23 +00:00
|
|
|
bs := &IPFSBlockstore{
|
2021-02-01 13:22:32 +00:00
|
|
|
ctx: ctx,
|
|
|
|
api: api,
|
2021-01-31 21:24:20 +00:00
|
|
|
offlineAPI: offlineAPI,
|
2021-02-28 19:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Adapt(bs), nil
|
2020-05-22 14:51:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (i *IPFSBlockstore) DeleteBlock(ctx context.Context, cid cid.Cid) error {
|
2020-04-29 23:51:55 +00:00
|
|
|
return xerrors.Errorf("not supported")
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (i *IPFSBlockstore) Has(ctx context.Context, cid cid.Cid) (bool, error) {
|
|
|
|
_, err := i.offlineAPI.Block().Stat(ctx, path.IpldPath(cid))
|
2020-04-29 23:51:55 +00:00
|
|
|
if err != nil {
|
2020-07-17 21:02:12 +00:00
|
|
|
// The underlying client is running in Offline mode.
|
|
|
|
// Stat() will fail with an err if the block isn't in the
|
|
|
|
// blockstore. If that's the case, return false without
|
|
|
|
// an error since that's the original intention of this method.
|
|
|
|
if err.Error() == "blockservice: key not found" {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-04-29 23:51:55 +00:00
|
|
|
return false, xerrors.Errorf("getting ipfs block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (i *IPFSBlockstore) Get(ctx context.Context, cid cid.Cid) (blocks.Block, error) {
|
|
|
|
rd, err := i.api.Block().Get(ctx, path.IpldPath(cid))
|
2020-04-29 23:51:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting ipfs block: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
data, err := io.ReadAll(rd)
|
2020-04-29 23:51:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocks.NewBlockWithCid(data, cid)
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (i *IPFSBlockstore) GetSize(ctx context.Context, cid cid.Cid) (int, error) {
|
|
|
|
st, err := i.api.Block().Stat(ctx, path.IpldPath(cid))
|
2020-04-29 23:51:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("getting ipfs block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return st.Size(), nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (i *IPFSBlockstore) Put(ctx context.Context, block blocks.Block) error {
|
2020-04-29 23:51:55 +00:00
|
|
|
mhd, err := multihash.Decode(block.Cid().Hash())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
_, err = i.api.Block().Put(ctx, bytes.NewReader(block.RawData()),
|
2020-04-29 23:51:55 +00:00
|
|
|
options.Block.Hash(mhd.Code, mhd.Length),
|
2022-06-28 11:09:59 +00:00
|
|
|
options.Block.Format(multihash.Codes[block.Cid().Type()]))
|
2020-04-29 23:51:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (i *IPFSBlockstore) PutMany(ctx context.Context, blocks []blocks.Block) error {
|
2020-04-29 23:51:55 +00:00
|
|
|
// TODO: could be done in parallel
|
|
|
|
|
|
|
|
for _, block := range blocks {
|
2021-11-19 01:50:25 +00:00
|
|
|
if err := i.Put(ctx, block); err != nil {
|
2020-04-29 23:51:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
func (i *IPFSBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
2020-04-29 23:51:55 +00:00
|
|
|
return nil, xerrors.Errorf("not supported")
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (i *IPFSBlockstore) HashOnRead(enabled bool) {
|
2020-04-29 23:51:55 +00:00
|
|
|
return // TODO: We could technically support this, but..
|
|
|
|
}
|