diff --git a/dagger/bitswap/engine.nim b/dagger/bitswap/engine.nim index ac862a52..05ef7ed9 100644 --- a/dagger/bitswap/engine.nim +++ b/dagger/bitswap/engine.nim @@ -338,15 +338,11 @@ proc taskHandler*(b: BitswapEngine, task: BitswapPeerCtx) {.gcsafe, async.} = # we send the block or get a cancel for e in task.peerWants: if e.wantType == WantType.wantHave: - wants.add( - BlockPresence( - cid: e.`block`, - `type`: if b.localStore.hasBlock(e.cid): - BlockPresenceType.presenceHave - else: - BlockPresenceType.presenceDontHave - )) - + var presence = Presence(cid: e.cid) + presence.have = b.localStore.hasblock(presence.cid) + if presence.have and price =? b.pricing.?price: + presence.price = price + wants.add(BlockPresence.init(presence)) if wants.len > 0: b.request.sendPresence(task.id, wants) diff --git a/tests/dagger/bitswap/testengine.nim b/tests/dagger/bitswap/testengine.nim index bb8eb5ad..80b7e8cd 100644 --- a/tests/dagger/bitswap/testengine.nim +++ b/tests/dagger/bitswap/testengine.nim @@ -303,6 +303,7 @@ suite "Task Handler": )) engine.peers = peersCtx + engine.pricing = Pricing.example.some test "Should send want-blocks in priority order": proc sendBlocks( @@ -341,11 +342,12 @@ suite "Task Handler": test "Should send presence": let present = blocks let missing = @[bt.Block.new("missing".toBytes)] + let price = (!engine.pricing).price proc sendPresence(id: PeerID, presence: seq[BlockPresence]) = check presence.mapIt(!Presence.init(it)) == @[ - Presence(cid: present[0].cid, have: true), - Presence(cid: present[1].cid, have: true), + Presence(cid: present[0].cid, have: true, price: price), + Presence(cid: present[1].cid, have: true, price: price), Presence(cid: missing[0].cid, have: false) ]