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

View File

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

View File

@ -118,6 +118,20 @@ suite "Bitswap network":
await done.wait(100.millis) 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": suite "Bitswap Network - e2e":
let let
chunker = newRandomChunker(Rng.instance(), size = 1024, chunkSize = 256) chunker = newRandomChunker(Rng.instance(), size = 1024, chunkSize = 256)