Skip to content

Commit

Permalink
fix(shwap/bitswap): close acccessor in Blockstore
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Jun 27, 2024
1 parent 218bdc7 commit 683bd10
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 8 additions & 4 deletions share/shwap/p2p/bitswap/block_fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestFetch_Duplicates(t *testing.T) {
func newExchangeOverEDS(ctx context.Context, t *testing.T, rsmt2d *rsmt2d.ExtendedDataSquare) exchange.SessionExchange {
bstore := &Blockstore{
Getter: testAccessorGetter{
Accessor: eds.Rsmt2D{ExtendedDataSquare: rsmt2d},
AccessorCloser: eds.WithCloser(eds.Rsmt2D{ExtendedDataSquare: rsmt2d}, nopCloser{}),
},
}
return newExchange(ctx, t, bstore)
Expand Down Expand Up @@ -163,11 +163,11 @@ func newClient(ctx context.Context, host host.Host, store blockstore.Blockstore)
}

type testAccessorGetter struct {
eds.Accessor
eds.AccessorCloser
}

func (t testAccessorGetter) GetByHeight(context.Context, uint64) (eds.Accessor, error) {
return t.Accessor, nil
func (t testAccessorGetter) GetByHeight(context.Context, uint64) (eds.AccessorCloser, error) {
return t.AccessorCloser, nil
}

type testFetcher struct {
Expand All @@ -184,3 +184,7 @@ func (t *testFetcher) GetBlocks(ctx context.Context, cids []cid.Cid) (<-chan blo
t.Fetched += len(cids)
return t.Embedded.GetBlocks(ctx, cids)
}

type nopCloser struct{}

func (nopCloser) Close() error { return nil }
7 changes: 6 additions & 1 deletion share/shwap/p2p/bitswap/block_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// AccessorGetter abstracts storage system that indexes and manages multiple eds.AccessorGetter by network height.
type AccessorGetter interface {
// GetByHeight returns an Accessor by its height.
GetByHeight(ctx context.Context, height uint64) (eds.Accessor, error)
GetByHeight(ctx context.Context, height uint64) (eds.AccessorCloser, error)
}

// Blockstore implements generalized Bitswap compatible storage over Shwap containers
Expand All @@ -37,6 +37,11 @@ func (b *Blockstore) getBlock(ctx context.Context, cid cid.Cid) (blocks.Block, e
if err != nil {
return nil, fmt.Errorf("getting EDS Accessor for height %v: %w", blk.Height(), err)
}
defer func() {
if err := eds.Close(); err != nil {
log.Warnf("failed to close EDS accessor for height %v: %s", blk.Height(), err)
}
}()

if err = blk.Populate(ctx, eds); err != nil {
return nil, fmt.Errorf("failed to populate Shwap Block on height %v for %s: %w", blk.Height(), spec.String(), err)
Expand Down

0 comments on commit 683bd10

Please sign in to comment.