[purchasing] Ignore offers that are about to expire

This commit is contained in:
Mark Spanbroek 2022-03-28 15:16:39 +02:00 committed by markspanbroek
parent dcfd6be1c6
commit 5b5f3335d6
2 changed files with 28 additions and 3 deletions

View File

@ -13,15 +13,18 @@ type
market: Market market: Market
proofProbability*: UInt256 proofProbability*: UInt256
requestExpiryInterval*: UInt256 requestExpiryInterval*: UInt256
offerExpiryMargin*: UInt256
Purchase* = ref object Purchase* = ref object
future: Future[void] future: Future[void]
market: Market market: Market
offerExpiryMargin: UInt256
request*: StorageRequest request*: StorageRequest
offers*: seq[StorageOffer] offers*: seq[StorageOffer]
selected*: ?StorageOffer selected*: ?StorageOffer
const DefaultProofProbability = 100.u256 const DefaultProofProbability = 100.u256
const DefaultRequestExpiryInterval = (10 * 60).u256 const DefaultRequestExpiryInterval = (10 * 60).u256
const DefaultOfferExpiryMargin = (8 * 60).u256
proc start(purchase: Purchase) {.gcsafe.} proc start(purchase: Purchase) {.gcsafe.}
@ -29,7 +32,8 @@ proc new*(_: type Purchasing, market: Market): Purchasing =
Purchasing( Purchasing(
market: market, market: market,
proofProbability: DefaultProofProbability, proofProbability: DefaultProofProbability,
requestExpiryInterval: DefaultRequestExpiryInterval requestExpiryInterval: DefaultRequestExpiryInterval,
offerExpiryMargin: DefaultOfferExpiryMargin
) )
proc populate*(purchasing: Purchasing, request: StorageRequest): StorageRequest = proc populate*(purchasing: Purchasing, request: StorageRequest): StorageRequest =
@ -43,14 +47,18 @@ proc populate*(purchasing: Purchasing, request: StorageRequest): StorageRequest
proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase = proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
let request = purchasing.populate(request) let request = purchasing.populate(request)
let purchase = Purchase(request: request, market: purchasing.market) let purchase = Purchase(
request: request,
market: purchasing.market,
offerExpiryMargin: purchasing.offerExpiryMargin
)
purchase.start() purchase.start()
purchase 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:
without offer.expiry > getTime().toUnix().u256: without getTime().toUnix().u256 < offer.expiry - purchase.offerExpiryMargin:
continue continue
without current =? cheapest: without current =? cheapest:
cheapest = some offer cheapest = some offer

View File

@ -98,3 +98,20 @@ suite "Purchasing":
market.advanceTimeTo(request.expiry) market.advanceTimeTo(request.expiry)
await purchase.wait() await purchase.wait()
check market.selected[0] == offer1.id check market.selected[0] == offer1.id
test "has a default expiration margin for offers":
check purchasing.offerExpiryMargin != 0.u256
test "ignores offers that are about to expire":
let expiryMargin = purchasing.offerExpiryMargin
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 = getTime().toUnix().u256 + expiryMargin - 1
await market.offerStorage(offer1)
await market.offerStorage(offer2)
market.advanceTimeTo(request.expiry)
await purchase.wait()
check market.selected[0] == offer1.id