From befebcf3251711eb9fbe64be190637ca22d7d929 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 31 Mar 2022 15:08:26 +0200 Subject: [PATCH] [sales] Make storage available again when offer expires --- dagger/sales.nim | 25 ++++++++++++++++++++----- tests/dagger/testsales.nim | 8 ++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/dagger/sales.nim b/dagger/sales.nim index 82e22a09..abd9b719 100644 --- a/dagger/sales.nim +++ b/dagger/sales.nim @@ -29,6 +29,7 @@ type offer: ?StorageOffer subscription: ?Subscription waiting: ?Future[void] + finished: bool OnSale = proc(offer: StorageOffer) {.gcsafe, upraises: [].} func new*(_: type Sales, market: Market): Sales = @@ -69,17 +70,30 @@ proc sendOffer(negotiation: Negotiation) {.async.} = let offer = negotiation.createOffer() negotiation.offer = some await negotiation.sales.market.offerStorage(offer) -proc onSelect(negotiation: Negotiation, offerId: array[32, byte]) = +proc finish(negotiation: Negotiation, success: bool) = + if negotiation.finished: + return + + negotiation.finished = true + if subscription =? negotiation.subscription: asyncSpawn subscription.unsubscribe() - without offer =? negotiation.offer: - return - if offer.id == offerId: + + if waiting =? negotiation.waiting: + waiting.cancel() + + if success and offer =? negotiation.offer: if onSale =? negotiation.sales.onSale: onSale(offer) else: negotiation.sales.add(negotiation.availability) +proc onSelect(negotiation: Negotiation, offerId: array[32, byte]) = + if offer =? negotiation.offer and offer.id == offerId: + negotiation.finish(success = true) + else: + negotiation.finish(success = false) + proc subscribeSelect(negotiation: Negotiation) {.async.} = without offer =? negotiation.offer: return @@ -93,6 +107,7 @@ proc waitForExpiry(negotiation: Negotiation) {.async.} = without offer =? negotiation.offer: return await negotiation.sales.market.waitUntil(offer.expiry) + negotiation.finish(success = false) proc start(negotiation: Negotiation) {.async.} = let sales = negotiation.sales @@ -100,7 +115,7 @@ proc start(negotiation: Negotiation) {.async.} = sales.remove(availability) await negotiation.sendOffer() await negotiation.subscribeSelect() - await negotiation.waitForExpiry() + negotiation.waiting = some negotiation.waitForExpiry() proc handleRequest(sales: Sales, request: StorageRequest) {.async.} = without availability =? sales.findAvailability(request): diff --git a/tests/dagger/testsales.nim b/tests/dagger/testsales.nim index a6890a99..653f75df 100644 --- a/tests/dagger/testsales.nim +++ b/tests/dagger/testsales.nim @@ -95,3 +95,11 @@ suite "Sales": otherOffer = await market.offerStorage(otherOffer) await market.selectOffer(otherOffer.id) check sales.available.contains(availability) + + test "makes storage available again when offer expires": + sales.add(availability) + discard await market.requestStorage(request) + let offer = market.offered[0] + market.advanceTimeTo(offer.expiry) + await sleepAsync(chronos.seconds(1)) + check sales.available.contains(availability)