From 811b29fb5e593c4544692ab13a5aa17f098558ed Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 19 Apr 2021 16:47:47 +0200 Subject: [PATCH] Receive payments for blocks that were sent --- dagger/bitswap.nim | 6 +++++- dagger/bitswap/engine.nim | 13 +++++++++++++ dagger/bitswap/network.nim | 6 +++++- tests/dagger/bitswap/testbitswap.nim | 9 +++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/dagger/bitswap.nim b/dagger/bitswap.nim index bd6b43b8..d56179f7 100644 --- a/dagger/bitswap.nim +++ b/dagger/bitswap.nim @@ -161,11 +161,15 @@ proc new*( proc pricingHandler(peer: PeerId, pricing: Pricing) = engine.pricingHandler(peer, pricing) + proc paymentHandler(peer: PeerId, payment: SignedState) = + engine.paymentHandler(peer, payment) + network.handlers = BitswapHandlers( onWantList: blockWantListHandler, onBlocks: blocksHandler, onPresence: blockPresenceHandler, - onPricing: pricingHandler + onPricing: pricingHandler, + onPayment: paymentHandler ) return b diff --git a/dagger/bitswap/engine.nim b/dagger/bitswap/engine.nim index 31c63e70..78bd2af9 100644 --- a/dagger/bitswap/engine.nim +++ b/dagger/bitswap/engine.nim @@ -269,6 +269,19 @@ proc pricingHandler*(engine: BitswapEngine, peer: PeerID, pricing: Pricing) = 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) = ## Perform initial setup, such as want ## list exchange diff --git a/dagger/bitswap/network.nim b/dagger/bitswap/network.nim index 0f1fa1d9..2214b5c7 100644 --- a/dagger/bitswap/network.nim +++ b/dagger/bitswap/network.nim @@ -350,11 +350,15 @@ proc new*( proc sendPricing(id: PeerID, pricing: Pricing) = b.broadcastPricing(id, pricing) + proc sendPayment(id: PeerID, payment: SignedState) = + b.broadcastPayment(id, payment) + b.request = BitswapRequest( sendWantList: sendWantList, sendBlocks: sendBlocks, sendPresence: sendPresence, - sendPricing: sendPricing + sendPricing: sendPricing, + sendPayment: sendPayment ) b.init() diff --git a/tests/dagger/bitswap/testbitswap.nim b/tests/dagger/bitswap/testbitswap.nim index c228e1e7..52fb0c6e 100644 --- a/tests/dagger/bitswap/testbitswap.nim +++ b/tests/dagger/bitswap/testbitswap.nim @@ -69,6 +69,8 @@ suite "Bitswap engine - 2 nodes": bitswap1.engine.wantList = blocks2.mapIt( it.cid ) bitswap2.engine.wantList = blocks1.mapIt( it.cid ) + pricing1.address = wallet1.address + pricing2.address = wallet2.address bitswap1.engine.pricing = pricing1.some bitswap2.engine.pricing = pricing2.some @@ -147,6 +149,13 @@ suite "Bitswap engine - 2 nodes": 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": let chunker = newRandomChunker(Rng.instance(), size = 4096, chunkSize = 256)