diff --git a/dagger/purchasing.nim b/dagger/purchasing.nim index 9149172a..5d45db6b 100644 --- a/dagger/purchasing.nim +++ b/dagger/purchasing.nim @@ -50,10 +50,12 @@ proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase = proc selectOffer(purchase: Purchase) {.async.} = var cheapest: ?StorageOffer for offer in purchase.offers: - if current =? cheapest: - if current.price > offer.price: - cheapest = some offer - else: + without offer.expiry > getTime().toUnix().u256: + continue + without current =? cheapest: + cheapest = some offer + continue + if current.price > offer.price: cheapest = some offer if cheapest =? cheapest: await purchase.market.selectOffer(cheapest.id) diff --git a/tests/dagger/testpurchasing.nim b/tests/dagger/testpurchasing.nim index 9d80a210..5569ba81 100644 --- a/tests/dagger/testpurchasing.nim +++ b/tests/dagger/testpurchasing.nim @@ -67,13 +67,34 @@ suite "Purchasing": await purchaseAndWait(request) check market.requested[0].nonce != market.requested[1].nonce + proc createOffer(request: StorageRequest): StorageOffer = + StorageOffer( + requestId: request.id, + expiry: (getTime() + initDuration(hours = 1)).toUnix().u256 + ) + test "selects the cheapest offer": let purchase = purchasing.purchase(request) let request = market.requested[0] - let offer1 = StorageOffer(requestId: request.id, price: 20.u256) - let offer2 = StorageOffer(requestId: request.id, price: 10.u256) + var offer1, offer2 = createOffer(request) + offer1.price = 20.u256 + offer2.price = 10.u256 await market.offerStorage(offer1) await market.offerStorage(offer2) market.advanceTimeTo(request.expiry) await purchase.wait() check market.selected[0] == offer2.id + + test "ignores offers that expired": + let expired = (getTime() - initTimeInterval(hours = 1)).toUnix().u256 + let purchase = purchasing.purchase(request) + let request = market.requested[0] + var offer1, offer2 = request.createOffer() + offer1.price = 20.u256 + offer2.price = 10.u256 + offer2.expiry = expired + await market.offerStorage(offer1) + await market.offerStorage(offer2) + market.advanceTimeTo(request.expiry) + await purchase.wait() + check market.selected[0] == offer1.id