Unverified Commit 6f1e7bb7 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #73 from ipfs/fix/nil-bs

fix: handle missing session exchange in Session
parents 622c072d 39f3c34e
......@@ -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)
......@@ -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")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment