Engine sends payments for received blocks
This commit is contained in:
parent
9512bbc50b
commit
ee978f354e
|
@ -23,6 +23,7 @@ import ../utils/asyncheapqueue
|
||||||
import ./network
|
import ./network
|
||||||
import ./pendingblocks
|
import ./pendingblocks
|
||||||
import ./peercontext
|
import ./peercontext
|
||||||
|
import ./engine/payments
|
||||||
|
|
||||||
export peercontext
|
export peercontext
|
||||||
|
|
||||||
|
@ -187,6 +188,20 @@ proc resolveBlocks*(b: BitswapEngine, blocks: seq[bt.Block]) =
|
||||||
b.pendingBlocks.resolve(blocks)
|
b.pendingBlocks.resolve(blocks)
|
||||||
b.scheduleTasks(blocks)
|
b.scheduleTasks(blocks)
|
||||||
|
|
||||||
|
proc payForBlocks(engine: BitswapEngine,
|
||||||
|
peer: BitswapPeerCtx,
|
||||||
|
blocks: seq[bt.Block]) =
|
||||||
|
let sendPayment = engine.request.sendPayment
|
||||||
|
if sendPayment.isNil:
|
||||||
|
return
|
||||||
|
|
||||||
|
var amountOfBytes = 0
|
||||||
|
for blck in blocks:
|
||||||
|
amountOfBytes += blck.data.len
|
||||||
|
|
||||||
|
if payment =? engine.wallet.pay(peer, amountOfBytes):
|
||||||
|
sendPayment(peer.id, payment)
|
||||||
|
|
||||||
proc blocksHandler*(
|
proc blocksHandler*(
|
||||||
b: BitswapEngine,
|
b: BitswapEngine,
|
||||||
peer: PeerID,
|
peer: PeerID,
|
||||||
|
@ -198,6 +213,10 @@ proc blocksHandler*(
|
||||||
b.localStore.putBlocks(blocks)
|
b.localStore.putBlocks(blocks)
|
||||||
b.resolveBlocks(blocks)
|
b.resolveBlocks(blocks)
|
||||||
|
|
||||||
|
let peerCtx = b.getPeerCtx(peer)
|
||||||
|
if peerCtx != nil:
|
||||||
|
b.payForBlocks(peerCtx, blocks)
|
||||||
|
|
||||||
proc wantListHandler*(
|
proc wantListHandler*(
|
||||||
b: BitswapEngine,
|
b: BitswapEngine,
|
||||||
peer: PeerID,
|
peer: PeerID,
|
||||||
|
|
|
@ -53,12 +53,14 @@ type
|
||||||
BlocksBroadcaster* = proc(peer: PeerID, presence: seq[bt.Block]) {.gcsafe.}
|
BlocksBroadcaster* = proc(peer: PeerID, presence: seq[bt.Block]) {.gcsafe.}
|
||||||
PresenceBroadcaster* = proc(peer: PeerID, presence: seq[BlockPresence]) {.gcsafe.}
|
PresenceBroadcaster* = proc(peer: PeerID, presence: seq[BlockPresence]) {.gcsafe.}
|
||||||
PricingBroadcaster* = proc(peer: PeerID, pricing: Pricing) {.gcsafe.}
|
PricingBroadcaster* = proc(peer: PeerID, pricing: Pricing) {.gcsafe.}
|
||||||
|
PaymentBroadcaster* = proc(peer: PeerID, payment: SignedState) {.gcsafe.}
|
||||||
|
|
||||||
BitswapRequest* = object
|
BitswapRequest* = object
|
||||||
sendWantList*: WantListBroadcaster
|
sendWantList*: WantListBroadcaster
|
||||||
sendBlocks*: BlocksBroadcaster
|
sendBlocks*: BlocksBroadcaster
|
||||||
sendPresence*: PresenceBroadcaster
|
sendPresence*: PresenceBroadcaster
|
||||||
sendPricing*: PricingBroadcaster
|
sendPricing*: PricingBroadcaster
|
||||||
|
sendPayment*: PaymentBroadcaster
|
||||||
|
|
||||||
BitswapNetwork* = ref object of LPProtocol
|
BitswapNetwork* = ref object of LPProtocol
|
||||||
peers*: Table[PeerID, NetworkPeer]
|
peers*: Table[PeerID, NetworkPeer]
|
||||||
|
|
|
@ -138,7 +138,7 @@ suite "Bitswap engine handlers":
|
||||||
|
|
||||||
await done
|
await done
|
||||||
|
|
||||||
test "should handle blocks":
|
test "stores blocks in local store":
|
||||||
let pending = blocks.mapIt(
|
let pending = blocks.mapIt(
|
||||||
engine.pendingBlocks.addOrAwait( it.cid )
|
engine.pendingBlocks.addOrAwait( it.cid )
|
||||||
)
|
)
|
||||||
|
@ -149,6 +149,21 @@ suite "Bitswap engine handlers":
|
||||||
for b in blocks:
|
for b in blocks:
|
||||||
check engine.localStore.hasBlock(b.cid)
|
check engine.localStore.hasBlock(b.cid)
|
||||||
|
|
||||||
|
test "sends payments for received blocks":
|
||||||
|
let pricing = Pricing.example
|
||||||
|
engine.getPeerCtx(peerId).pricing = pricing.some
|
||||||
|
|
||||||
|
engine.request.sendPayment = proc(receiver: PeerID, payment: SignedState) =
|
||||||
|
let amount = blocks.mapIt(it.data.len).foldl(a+b).u256 * pricing.price
|
||||||
|
let balances = payment.state.outcome.balances(pricing.asset).get
|
||||||
|
check receiver == peerId
|
||||||
|
check balances[pricing.address.toDestination] == amount
|
||||||
|
done.complete()
|
||||||
|
|
||||||
|
engine.blocksHandler(peerId, blocks)
|
||||||
|
|
||||||
|
await done.wait(100.millis)
|
||||||
|
|
||||||
test "should handle block presence":
|
test "should handle block presence":
|
||||||
engine.blockPresenceHandler(
|
engine.blockPresenceHandler(
|
||||||
peerId,
|
peerId,
|
||||||
|
|
Loading…
Reference in New Issue