Add handler for payment messages

This commit is contained in:
Mark Spanbroek 2021-04-08 10:09:59 +02:00 committed by markspanbroek
parent 5f9215a371
commit e02c40db80
3 changed files with 27 additions and 0 deletions

View File

@ -34,12 +34,14 @@ type
BlocksHandler* = proc(peer: PeerID, blocks: seq[bt.Block]) {.gcsafe.}
BlockPresenceHandler* = proc(peer: PeerID, precense: seq[BlockPresence]) {.gcsafe.}
PricingHandler* = proc(peer: PeerID, pricing: Pricing) {.gcsafe.}
PaymentHandler* = proc(peer: PeerID, payment: SignedState) {.gcsafe.}
BitswapHandlers* = object
onWantList*: WantListHandler
onBlocks*: BlocksHandler
onPresence*: BlockPresenceHandler
onPricing*: PricingHandler
onPayment*: PaymentHandler
WantListBroadcaster* = proc(
id: PeerID,
@ -206,6 +208,13 @@ proc handlePricing(network: BitswapNetwork,
return
network.handlers.onPricing(peer.id, pricing)
proc handlePayment(network: BitswapNetwork,
peer: NetworkPeer,
payment: SignedState) =
if network.handlers.onPayment.isNil:
return
network.handlers.onPayment(peer.id, payment)
proc rpcHandler(b: BitswapNetwork, peer: NetworkPeer, msg: Message) {.async.} =
try:
if msg.wantlist.entries.len > 0:
@ -223,6 +232,9 @@ proc rpcHandler(b: BitswapNetwork, peer: NetworkPeer, msg: Message) {.async.} =
if pricing =? Pricing.init(msg.pricing):
b.handlePricing(peer, pricing)
if payment =? SignedState.init(msg.payment):
b.handlePayment(peer, payment)
except CatchableError as exc:
trace "Exception in bitswap rpc handler", exc = exc.msg

View File

@ -1,4 +1,5 @@
import pkg/protobuf_serialization
import pkg/stew/byteutils
import pkg/nitro
import pkg/questionable
import pkg/upraises

View File

@ -118,6 +118,20 @@ suite "Bitswap network":
await done.wait(100.millis)
test "handles payment messages":
let payment = SignedState.example
proc handlePayment(peer: PeerID, received: SignedState) =
check received == payment
done.complete()
network.handlers.onPayment = handlePayment
let message = Message(payment: StateChannelUpdate.init(payment))
await buffer.pushData(lenPrefix(Protobuf.encode(message)))
await done.wait(100.millis)
suite "Bitswap Network - e2e":
let
chunker = newRandomChunker(Rng.instance(), size = 1024, chunkSize = 256)