Send block prices

This commit is contained in:
Mark Spanbroek 2021-05-10 12:47:45 +02:00 committed by markspanbroek
parent 98d8571ceb
commit f69827d582
2 changed files with 9 additions and 11 deletions

View File

@ -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)

View File

@ -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)
]