diff --git a/blockservice.go b/blockservice.go index 33f69141c615e461ee3e9a97cd95584f4e036efb..2f320b139bf1e5d8a1ab5f179c56d2b444888abb 100644 --- a/blockservice.go +++ b/blockservice.go @@ -366,12 +366,20 @@ func (s *Session) getSession() exchange.Fetcher { // GetBlock gets a block in the context of a request session func (s *Session) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) { - return getBlock(ctx, c, s.bs, s.getSession) // hash security + var f func() exchange.Fetcher + if s.sessEx != nil { + f = s.getSession + } + return getBlock(ctx, c, s.bs, f) // hash security } // GetBlocks gets blocks in the context of a request session func (s *Session) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan blocks.Block { - return getBlocks(ctx, ks, s.bs, s.getSession) // hash security + var f func() exchange.Fetcher + if s.sessEx != nil { + f = s.getSession + } + return getBlocks(ctx, ks, s.bs, f) // hash security } var _ BlockGetter = (*Session)(nil) diff --git a/blockservice_test.go b/blockservice_test.go index dfd12fc437b8c32883d99d3e8932ba3e40d5a79e..36cdf0330c7be1aca7417827aaee1ddb95b85ea5 100644 --- a/blockservice_test.go +++ b/blockservice_test.go @@ -119,3 +119,31 @@ func (fe *fakeSessionExchange) NewSession(ctx context.Context) exchange.Fetcher } return fe.session } + +func TestNilExchange(t *testing.T) { + ctx := context.Background() + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + bgen := butil.NewBlockGenerator() + block := bgen.Next() + + bs := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())) + bserv := NewWriteThrough(bs, nil) + sess := NewSession(ctx, bserv) + _, err := sess.GetBlock(ctx, block.Cid()) + if err != ErrNotFound { + t.Fatal("expected block to not be found") + } + err = bs.Put(block) + if err != nil { + t.Fatal(err) + } + b, err := sess.GetBlock(ctx, block.Cid()) + if err != nil { + t.Fatal(err) + } + if b.Cid() != block.Cid() { + t.Fatal("got the wrong block") + } +}