[WIP sales] call onSale()

This commit is contained in:
Mark Spanbroek 2022-07-05 10:51:01 +02:00 committed by markspanbroek
parent a2570f9380
commit 7f864570bd
2 changed files with 17 additions and 13 deletions

View File

@ -31,13 +31,14 @@ type
requestId: array[32, byte]
ask: StorageAsk
availability: Availability
request: ?StorageRequest
offer: ?StorageOffer
subscription: ?Subscription
waiting: ?Future[void]
finished: bool
Retrieve = proc(cid: string): Future[void] {.gcsafe, upraises: [].}
Prove = proc(cid: string): Future[seq[byte]] {.gcsafe, upraises: [].}
OnSale = proc(offer: StorageOffer) {.gcsafe, upraises: [].}
OnSale = proc(availability: Availability, request: StorageRequest) {.gcsafe, upraises: [].}
func new*(_: type Sales, market: Market, clock: Clock): Sales =
Sales(
@ -100,9 +101,9 @@ proc finish(negotiation: Negotiation, success: bool) =
if waiting =? negotiation.waiting:
waiting.cancel()
if success and offer =? negotiation.offer:
if success and request =? negotiation.request:
if onSale =? negotiation.sales.onSale:
onSale(offer)
onSale(negotiation.availability, request)
else:
negotiation.sales.add(negotiation.availability)
@ -141,13 +142,15 @@ proc start(negotiation: Negotiation) {.async.} =
try:
sales.remove(availability)
without request =? await market.getRequest(negotiation.requestId):
negotiation.request = await market.getRequest(negotiation.requestId)
without request =? negotiation.request:
negotiation.finish(success = false)
return
await retrieve(request.content.cid)
let proof = await prove(request.content.cid)
await market.fulfillRequest(request.id, proof)
negotiation.finish(success = true)
await negotiation.sendOffer()
await negotiation.subscribeSelect()

View File

@ -103,15 +103,16 @@ suite "Sales":
check market.fulfilled[0].proof == proof
check market.fulfilled[0].host == await market.getSigner()
# test "calls onSale when offer is selected":
# var sold: StorageOffer
# sales.onSale = proc(offer: StorageOffer) =
# sold = offer
# sales.add(availability)
# discard await market.requestStorage(request)
# let offer = market.offered[0]
# await market.selectOffer(offer.id)
# check sold == offer
test "calls onSale when request is fulfilled":
var soldAvailability: Availability
var soldRequest: StorageRequest
sales.onSale = proc(availability: Availability, request: StorageRequest) =
soldAvailability = availability
soldRequest = request
sales.add(availability)
discard await market.requestStorage(request)
check soldAvailability == availability
check soldRequest == request
# test "does not call onSale when a different offer is selected":
# var didSell: bool