2020-04-29 23:51:55 +00:00
|
|
|
package ipfsbstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-05-22 14:51:18 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2020-04-29 23:51:55 +00:00
|
|
|
"github.com/multiformats/go-multihash"
|
|
|
|
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
httpapi "github.com/ipfs/go-ipfs-http-client"
|
|
|
|
iface "github.com/ipfs/interface-go-ipfs-core"
|
|
|
|
"github.com/ipfs/interface-go-ipfs-core/options"
|
|
|
|
"github.com/ipfs/interface-go-ipfs-core/path"
|
2020-07-23 02:05:11 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/lib/blockstore"
|
2020-04-29 23:51:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IpfsBstore 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
|
|
|
}
|
|
|
|
|
2020-10-29 18:16:45 +00:00
|
|
|
func NewIpfsBstore(ctx context.Context, onlineMode bool) (*IpfsBstore, error) {
|
2020-07-17 21:02:12 +00:00
|
|
|
localApi, err := httpapi.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-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)
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 23:51:55 +00:00
|
|
|
|
|
|
|
return &IpfsBstore{
|
2021-02-01 13:22:32 +00:00
|
|
|
ctx: ctx,
|
|
|
|
api: api,
|
2021-01-31 21:24:20 +00:00
|
|
|
offlineAPI: offlineAPI,
|
2020-04-29 23:51:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:16:45 +00:00
|
|
|
func NewRemoteIpfsBstore(ctx context.Context, maddr multiaddr.Multiaddr, onlineMode bool) (*IpfsBstore, error) {
|
2020-07-17 21:02:12 +00:00
|
|
|
httpApi, err := httpapi.NewApi(maddr)
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 14:51:18 +00:00
|
|
|
|
|
|
|
return &IpfsBstore{
|
2021-02-01 13:22:32 +00:00
|
|
|
ctx: ctx,
|
|
|
|
api: api,
|
2021-01-31 21:24:20 +00:00
|
|
|
offlineAPI: offlineAPI,
|
2020-05-22 14:51:18 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:51:55 +00:00
|
|
|
func (i *IpfsBstore) DeleteBlock(cid cid.Cid) error {
|
|
|
|
return xerrors.Errorf("not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IpfsBstore) Has(cid cid.Cid) (bool, error) {
|
2021-01-31 21:24:20 +00:00
|
|
|
_, err := i.offlineAPI.Block().Stat(i.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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IpfsBstore) Get(cid cid.Cid) (blocks.Block, error) {
|
|
|
|
rd, err := i.api.Block().Get(i.ctx, path.IpldPath(cid))
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting ipfs block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(rd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocks.NewBlockWithCid(data, cid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IpfsBstore) GetSize(cid cid.Cid) (int, error) {
|
|
|
|
st, err := i.api.Block().Stat(i.ctx, path.IpldPath(cid))
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("getting ipfs block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return st.Size(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IpfsBstore) Put(block blocks.Block) error {
|
|
|
|
mhd, err := multihash.Decode(block.Cid().Hash())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = i.api.Block().Put(i.ctx, bytes.NewReader(block.RawData()),
|
|
|
|
options.Block.Hash(mhd.Code, mhd.Length),
|
|
|
|
options.Block.Format(cid.CodecToStr[block.Cid().Type()]))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IpfsBstore) PutMany(blocks []blocks.Block) error {
|
|
|
|
// TODO: could be done in parallel
|
|
|
|
|
|
|
|
for _, block := range blocks {
|
|
|
|
if err := i.Put(block); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IpfsBstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
|
|
|
return nil, xerrors.Errorf("not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IpfsBstore) HashOnRead(enabled bool) {
|
|
|
|
return // TODO: We could technically support this, but..
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ blockstore.Blockstore = &IpfsBstore{}
|