diff --git a/codex/sales.nim b/codex/sales.nim index 7bacb0a6..d47cb965 100644 --- a/codex/sales.nim +++ b/codex/sales.nim @@ -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() diff --git a/tests/codex/testsales.nim b/tests/codex/testsales.nim index 212986a4..09808b2d 100644 --- a/tests/codex/testsales.nim +++ b/tests/codex/testsales.nim @@ -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