Skip to content

Commit

Permalink
Exchange don't add blocks on their own anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure authored and Jorropo committed Jul 27, 2022
1 parent 4ec405c commit 931b053
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
11 changes: 4 additions & 7 deletions exchange/offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
16 changes: 10 additions & 6 deletions exchange/offline/offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand All @@ -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()
}
}
Expand Down

0 comments on commit 931b053

Please sign in to comment.