ipbsbstore: offlinemode, and Has() fix

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
This commit is contained in:
Ignacio Hagopian 2020-07-17 18:02:12 -03:00
parent ee6269756c
commit fd2262f51f
No known key found for this signature in database
GPG Key ID: 4DC349E20B2AD1BE

View File

@ -25,10 +25,14 @@ type IpfsBstore struct {
}
func NewIpfsBstore(ctx context.Context) (*IpfsBstore, error) {
api, err := httpapi.NewLocalApi()
localApi, err := httpapi.NewLocalApi()
if err != nil {
return nil, xerrors.Errorf("getting local ipfs api: %w", err)
}
api, err := localApi.WithOptions(options.Api.Offline(true))
if err != nil {
return nil, xerrors.Errorf("setting offline mode: %s", err)
}
return &IpfsBstore{
ctx: ctx,
@ -37,9 +41,13 @@ func NewIpfsBstore(ctx context.Context) (*IpfsBstore, error) {
}
func NewRemoteIpfsBstore(ctx context.Context, maddr multiaddr.Multiaddr) (*IpfsBstore, error) {
api, err := httpapi.NewApi(maddr)
httpApi, err := httpapi.NewApi(maddr)
if err != nil {
return nil, xerrors.Errorf("getting remote ipfs api: %w", err)
return nil, xerrors.Errorf("setting remote ipfs api: %w", err)
}
api, err := httpApi.WithOptions(options.Api.Offline(true))
if err != nil {
return nil, xerrors.Errorf("applying offline mode: %s", err)
}
return &IpfsBstore{
@ -55,6 +63,13 @@ func (i *IpfsBstore) DeleteBlock(cid cid.Cid) error {
func (i *IpfsBstore) Has(cid cid.Cid) (bool, error) {
_, err := i.api.Block().Stat(i.ctx, path.IpldPath(cid))
if err != nil {
// 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
}
return false, xerrors.Errorf("getting ipfs block: %w", err)
}