From d4da53dda8ab1e65743f2e2e70c4d7d68667cdae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sun, 31 Jan 2021 22:24:20 +0100 Subject: [PATCH 1/2] ipfsbstore: Fix has for non-existing blocks --- lib/ipfsbstore/ipfsbstore.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/ipfsbstore/ipfsbstore.go b/lib/ipfsbstore/ipfsbstore.go index e66f3da893a..2b34d37dbf3 100644 --- a/lib/ipfsbstore/ipfsbstore.go +++ b/lib/ipfsbstore/ipfsbstore.go @@ -22,7 +22,7 @@ import ( type IpfsBstore struct { ctx context.Context - api iface.CoreAPI + api, offlineAPI iface.CoreAPI } func NewIpfsBstore(ctx context.Context, onlineMode bool) (*IpfsBstore, error) { @@ -34,10 +34,18 @@ func NewIpfsBstore(ctx context.Context, onlineMode bool) (*IpfsBstore, error) { if err != nil { return nil, xerrors.Errorf("setting offline mode: %s", err) } + offlineAPI := api + if onlineMode { + offlineAPI, err = localApi.WithOptions(options.Api.Offline(true)) + if err != nil { + return nil, xerrors.Errorf("applying offline mode: %s", err) + } + } return &IpfsBstore{ ctx: ctx, api: api, + offlineAPI: offlineAPI, }, nil } @@ -50,10 +58,18 @@ func NewRemoteIpfsBstore(ctx context.Context, maddr multiaddr.Multiaddr, onlineM if err != nil { return nil, xerrors.Errorf("applying offline mode: %s", err) } + offlineAPI := api + if onlineMode { + offlineAPI, err = httpApi.WithOptions(options.Api.Offline(true)) + if err != nil { + return nil, xerrors.Errorf("applying offline mode: %s", err) + } + } return &IpfsBstore{ ctx: ctx, api: api, + offlineAPI: offlineAPI, }, nil } @@ -62,7 +78,7 @@ 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)) + _, err := i.offlineAPI.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 From b2eb014900ae9d3951deb5080f1e308507bfbd6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 1 Feb 2021 14:22:32 +0100 Subject: [PATCH 2/2] gofmt --- lib/ipfsbstore/ipfsbstore.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/ipfsbstore/ipfsbstore.go b/lib/ipfsbstore/ipfsbstore.go index 2b34d37dbf3..5f1c63f36ac 100644 --- a/lib/ipfsbstore/ipfsbstore.go +++ b/lib/ipfsbstore/ipfsbstore.go @@ -21,7 +21,7 @@ import ( ) type IpfsBstore struct { - ctx context.Context + ctx context.Context api, offlineAPI iface.CoreAPI } @@ -43,8 +43,8 @@ func NewIpfsBstore(ctx context.Context, onlineMode bool) (*IpfsBstore, error) { } return &IpfsBstore{ - ctx: ctx, - api: api, + ctx: ctx, + api: api, offlineAPI: offlineAPI, }, nil } @@ -67,8 +67,8 @@ func NewRemoteIpfsBstore(ctx context.Context, maddr multiaddr.Multiaddr, onlineM } return &IpfsBstore{ - ctx: ctx, - api: api, + ctx: ctx, + api: api, offlineAPI: offlineAPI, }, nil }