[purchasing] Ignore expired offers

This commit is contained in:
Mark Spanbroek 2022-03-28 14:40:41 +02:00 committed by markspanbroek
parent fe23cb89d7
commit dcfd6be1c6
2 changed files with 29 additions and 6 deletions

View File

@ -50,10 +50,12 @@ proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
proc selectOffer(purchase: Purchase) {.async.} = proc selectOffer(purchase: Purchase) {.async.} =
var cheapest: ?StorageOffer var cheapest: ?StorageOffer
for offer in purchase.offers: for offer in purchase.offers:
if current =? cheapest: without offer.expiry > getTime().toUnix().u256:
if current.price > offer.price: continue
cheapest = some offer without current =? cheapest:
else: cheapest = some offer
continue
if current.price > offer.price:
cheapest = some offer cheapest = some offer
if cheapest =? cheapest: if cheapest =? cheapest:
await purchase.market.selectOffer(cheapest.id) await purchase.market.selectOffer(cheapest.id)

View File

@ -67,13 +67,34 @@ suite "Purchasing":
await purchaseAndWait(request) await purchaseAndWait(request)
check market.requested[0].nonce != market.requested[1].nonce 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": test "selects the cheapest offer":
let purchase = purchasing.purchase(request) let purchase = purchasing.purchase(request)
let request = market.requested[0] let request = market.requested[0]
let offer1 = StorageOffer(requestId: request.id, price: 20.u256) var offer1, offer2 = createOffer(request)
let offer2 = StorageOffer(requestId: request.id, price: 10.u256) offer1.price = 20.u256
offer2.price = 10.u256
await market.offerStorage(offer1) await market.offerStorage(offer1)
await market.offerStorage(offer2) await market.offerStorage(offer2)
market.advanceTimeTo(request.expiry) market.advanceTimeTo(request.expiry)
await purchase.wait() await purchase.wait()
check market.selected[0] == offer2.id 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