[purchasing] Ignore expired offers
This commit is contained in:
parent
fe23cb89d7
commit
dcfd6be1c6
|
@ -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
|
||||||
|
without current =? cheapest:
|
||||||
cheapest = some offer
|
cheapest = some offer
|
||||||
else:
|
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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue