[purchasing] keep track of all purchases

This commit is contained in:
Mark Spanbroek 2022-05-11 09:14:43 +02:00 committed by markspanbroek
parent 466f109193
commit fb56f60d77
2 changed files with 16 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import std/times
import std/tables
import pkg/stint
import pkg/chronos
import pkg/questionable
@ -11,6 +12,7 @@ export market
type
Purchasing* = ref object
market: Market
purchases: Table[array[32, byte], Purchase]
proofProbability*: UInt256
requestExpiryInterval*: UInt256
offerExpiryMargin*: UInt256
@ -27,6 +29,7 @@ const DefaultRequestExpiryInterval = (10 * 60).u256
const DefaultOfferExpiryMargin = (8 * 60).u256
proc start(purchase: Purchase) {.gcsafe.}
func id*(purchase: Purchase): array[32, byte]
proc new*(_: type Purchasing, market: Market): Purchasing =
Purchasing(
@ -53,8 +56,15 @@ proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
offerExpiryMargin: purchasing.offerExpiryMargin
)
purchase.start()
purchasing.purchases[purchase.id] = purchase
purchase
func getPurchase*(purchasing: Purchasing, id: array[32, byte]): ?Purchase =
if purchasing.purchases.hasKey(id):
some purchasing.purchases[id]
else:
none Purchase
proc selectOffer(purchase: Purchase) {.async.} =
var cheapest: ?StorageOffer
for offer in purchase.offers:

View File

@ -34,6 +34,12 @@ suite "Purchasing":
check submitted.ask.size == request.ask.size
check submitted.ask.maxPrice == request.ask.maxPrice
test "remembers purchases":
let purchase1 = purchasing.purchase(request)
let purchase2 = purchasing.purchase(request)
check purchasing.getPurchase(purchase1.id) == some purchase1
check purchasing.getPurchase(purchase2.id) == some purchase2
test "has a default value for proof probability":
check purchasing.proofProbability != 0.u256