Skip to content

Commit

Permalink
Merge pull request ipfs/go-blockservice#73 from ipfs/fix/nil-bs
Browse files Browse the repository at this point in the history
fix: handle missing session exchange in Session

This commit was moved from ipfs/go-blockservice@6f1e7bb
  • Loading branch information
Stebalien authored Apr 2, 2021
2 parents b261c29 + c06e355 commit d210966
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
12 changes: 10 additions & 2 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
28 changes: 28 additions & 0 deletions blockservice/blockservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit d210966

Please sign in to comment.