From fe4c9e2521e1e8903de7bd149c9670538749a1b2 Mon Sep 17 00:00:00 2001 From: Wondertan Date: Fri, 20 Sep 2024 14:10:19 +0200 Subject: [PATCH] feat(shwap/bitswap): use new bitswap option --- share/shwap/p2p/bitswap/bitswap.go | 4 ++++ share/shwap/p2p/bitswap/block_fetch_test.go | 4 ++++ share/shwap/p2p/bitswap/block_store.go | 16 +++++++++++----- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/share/shwap/p2p/bitswap/bitswap.go b/share/shwap/p2p/bitswap/bitswap.go index f63efe4012..d81e61a135 100644 --- a/share/shwap/p2p/bitswap/bitswap.go +++ b/share/shwap/p2p/bitswap/bitswap.go @@ -64,6 +64,9 @@ const ( // We set it to be equal to targetMessageSize * N, so there can max N messages being prepared for // a peer at once. outstandingBytesPerPeer = targetMessageSize * 4 + // replaceHasWithBlockMaxSize configures Bitswap to use Has method instead of GetSize to check existence + // of a CID in Blockstore. + replaceHasWithBlockMaxSize = 0 ) // NewNetwork constructs Bitswap network for Shwap protocol composition. @@ -114,6 +117,7 @@ func NewServer( server.MaxQueuedWantlistEntriesPerPeer(maxServerWantListsPerPeer), server.WithTargetMessageSize(targetMessageSize), server.MaxOutstandingBytesPerPeer(outstandingBytesPerPeer), + server.WithWantHaveReplaceSize(replaceHasWithBlockMaxSize), } return server.New(ctx, net, bstore, opts...) } diff --git a/share/shwap/p2p/bitswap/block_fetch_test.go b/share/shwap/p2p/bitswap/block_fetch_test.go index 6642801efe..05b0c78b19 100644 --- a/share/shwap/p2p/bitswap/block_fetch_test.go +++ b/share/shwap/p2p/bitswap/block_fetch_test.go @@ -159,6 +159,10 @@ func (t testAccessorGetter) GetByHeight(context.Context, uint64) (eds.AccessorSt return t.AccessorStreamer, nil } +func (t testAccessorGetter) HasByHeight(context.Context, uint64) (bool, error) { + return true, nil +} + type testFetcher struct { Fetched int diff --git a/share/shwap/p2p/bitswap/block_store.go b/share/shwap/p2p/bitswap/block_store.go index b1a5f1554e..910a95c204 100644 --- a/share/shwap/p2p/bitswap/block_store.go +++ b/share/shwap/p2p/bitswap/block_store.go @@ -18,6 +18,8 @@ import ( type AccessorGetter interface { // GetByHeight returns an Accessor by its height. GetByHeight(ctx context.Context, height uint64) (eds.AccessorStreamer, error) + // HasByHeight reports whether an Accessor for the height exists. + HasByHeight(ctx context.Context, height uint64) (bool, error) } // Blockstore implements generalized Bitswap compatible storage over Shwap containers @@ -63,10 +65,9 @@ func (b *Blockstore) Get(ctx context.Context, cid cid.Cid) (blocks.Block, error) } func (b *Blockstore) GetSize(ctx context.Context, cid cid.Cid) (int, error) { - // TODO(@Wondertan): There must be a way to derive size without reading, proving, serializing and - // allocating Sample's block.Block or we could do hashing - // NOTE:Bitswap uses GetSize also to determine if we have content stored or not - // so simply returning constant size is not an option + // TODO(@Wondertan): Bitswap checks the size of the data(GetSize) before serving it via Get. This means + // GetSize may do an unnecessary read from disk which we can avoid by either caching on Blockstore level + // or returning constant size(we know at that point that we have requested data) blk, err := b.Get(ctx, cid) if err != nil { return 0, err @@ -75,10 +76,15 @@ func (b *Blockstore) GetSize(ctx context.Context, cid cid.Cid) (int, error) { } func (b *Blockstore) Has(ctx context.Context, cid cid.Cid) (bool, error) { - _, err := b.Get(ctx, cid) + blk, err := EmptyBlock(cid) if err != nil { return false, err } + + _, err = b.Getter.HasByHeight(ctx, blk.Height()) + if err != nil { + return false, fmt.Errorf("checking EDS Accessor for height %v: %w", blk.Height(), err) + } return true, nil }