From 931b05363833d5de3b64e86a6c75f5c6cf5e7fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Thu, 14 Jul 2022 12:27:39 +0200 Subject: [PATCH] Exchange don't add blocks on their own anymore Follows: - https://github.com/ipfs/go-ipfs-exchange-interface/pull/23 - https://github.com/ipfs/go-bitswap/pull/571 This commit was moved from ipfs/go-ipfs-exchange-offline@678648ad69e71cacaee5caff28ce179f5aa96bfa --- exchange/offline/offline.go | 11 ++++------- exchange/offline/offline_test.go | 16 ++++++++++------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/exchange/offline/offline.go b/exchange/offline/offline.go index 7c5d7c5ea..430a8d744 100644 --- a/exchange/offline/offline.go +++ b/exchange/offline/offline.go @@ -34,9 +34,10 @@ func (e *offlineExchange) GetBlock(ctx context.Context, k cid.Cid) (blocks.Block return blk, err } -// HasBlock always returns nil. -func (e *offlineExchange) HasBlock(ctx context.Context, b blocks.Block) error { - return e.bs.Put(ctx, b) +// NotifyNewBlocks tells the exchange that new blocks are available and can be served. +func (e *offlineExchange) NotifyNewBlocks(ctx context.Context, blocks ...blocks.Block) error { + // as an offline exchange we have nothing to do + return nil } // Close always returns nil. @@ -71,7 +72,3 @@ func (e *offlineExchange) GetBlocks(ctx context.Context, ks []cid.Cid) (<-chan b }() return out, nil } - -func (e *offlineExchange) IsOnline() bool { - return false -} diff --git a/exchange/offline/offline_test.go b/exchange/offline/offline_test.go index 0ac95a6b6..e329372b7 100644 --- a/exchange/offline/offline_test.go +++ b/exchange/offline/offline_test.go @@ -28,13 +28,14 @@ func TestHasBlockReturnsNil(t *testing.T) { ex := Exchange(store) block := blocks.NewBlock([]byte("data")) - err := ex.HasBlock(context.Background(), block) - if err != nil { - t.Fail() + // we don't need to do that for the test, but that illustrate the normal workflow + if err := store.Put(context.Background(), block); err != nil { + t.Fatal(err) } - if _, err := store.Get(context.Background(), block.Cid()); err != nil { - t.Fatal(err) + err := ex.NotifyNewBlocks(context.Background(), block) + if err != nil { + t.Fail() } } @@ -46,7 +47,10 @@ func TestGetBlocks(t *testing.T) { expected := g.Blocks(2) for _, b := range expected { - if err := ex.HasBlock(context.Background(), b); err != nil { + if err := store.Put(context.Background(), b); err != nil { + t.Fatal(err) + } + if err := ex.NotifyNewBlocks(context.Background(), b); err != nil { t.Fail() } }