Receive payments for blocks that were sent

This commit is contained in:
Mark Spanbroek 2021-04-19 16:47:47 +02:00 committed by markspanbroek
parent 5160b267b6
commit 811b29fb5e
4 changed files with 32 additions and 2 deletions

View File

@ -161,11 +161,15 @@ proc new*(
proc pricingHandler(peer: PeerId, pricing: Pricing) = proc pricingHandler(peer: PeerId, pricing: Pricing) =
engine.pricingHandler(peer, pricing) engine.pricingHandler(peer, pricing)
proc paymentHandler(peer: PeerId, payment: SignedState) =
engine.paymentHandler(peer, payment)
network.handlers = BitswapHandlers( network.handlers = BitswapHandlers(
onWantList: blockWantListHandler, onWantList: blockWantListHandler,
onBlocks: blocksHandler, onBlocks: blocksHandler,
onPresence: blockPresenceHandler, onPresence: blockPresenceHandler,
onPricing: pricingHandler onPricing: pricingHandler,
onPayment: paymentHandler
) )
return b return b

View File

@ -269,6 +269,19 @@ proc pricingHandler*(engine: BitswapEngine, peer: PeerID, pricing: Pricing) =
context.pricing = pricing.some context.pricing = pricing.some
proc paymentHandler*(engine: BitswapEngine, peer: PeerId, payment: SignedState) =
without context =? engine.getPeerCtx(peer).option and
contextPricing =? context.pricing and
enginePricing =? engine.pricing:
return
if channel =? context.paymentChannel:
let asset = enginePricing.asset
let sender = contextPricing.address
discard engine.wallet.acceptPayment(channel, asset, sender, payment)
else:
context.paymentChannel = engine.wallet.acceptChannel(payment).option
proc setupPeer*(b: BitswapEngine, peer: PeerID) = proc setupPeer*(b: BitswapEngine, peer: PeerID) =
## Perform initial setup, such as want ## Perform initial setup, such as want
## list exchange ## list exchange

View File

@ -350,11 +350,15 @@ proc new*(
proc sendPricing(id: PeerID, pricing: Pricing) = proc sendPricing(id: PeerID, pricing: Pricing) =
b.broadcastPricing(id, pricing) b.broadcastPricing(id, pricing)
proc sendPayment(id: PeerID, payment: SignedState) =
b.broadcastPayment(id, payment)
b.request = BitswapRequest( b.request = BitswapRequest(
sendWantList: sendWantList, sendWantList: sendWantList,
sendBlocks: sendBlocks, sendBlocks: sendBlocks,
sendPresence: sendPresence, sendPresence: sendPresence,
sendPricing: sendPricing sendPricing: sendPricing,
sendPayment: sendPayment
) )
b.init() b.init()

View File

@ -69,6 +69,8 @@ suite "Bitswap engine - 2 nodes":
bitswap1.engine.wantList = blocks2.mapIt( it.cid ) bitswap1.engine.wantList = blocks2.mapIt( it.cid )
bitswap2.engine.wantList = blocks1.mapIt( it.cid ) bitswap2.engine.wantList = blocks1.mapIt( it.cid )
pricing1.address = wallet1.address
pricing2.address = wallet2.address
bitswap1.engine.pricing = pricing1.some bitswap1.engine.pricing = pricing1.some
bitswap2.engine.pricing = pricing2.some bitswap2.engine.pricing = pricing2.some
@ -147,6 +149,13 @@ suite "Bitswap engine - 2 nodes":
await done await done
test "receives payments for blocks that were sent":
let blocks = await bitswap1.getBlocks(blocks2.mapIt(it.cid))
let pricing = !bitswap2.engine.pricing
await sleepAsync(100.millis)
let channel = !peerCtx1.paymentChannel
check wallet2.balance(channel, pricing.asset) > 0
suite "Bitswap - multiple nodes": suite "Bitswap - multiple nodes":
let let
chunker = newRandomChunker(Rng.instance(), size = 4096, chunkSize = 256) chunker = newRandomChunker(Rng.instance(), size = 4096, chunkSize = 256)