[purchasing] Load purchases from market

This commit is contained in:
Mark Spanbroek 2022-10-24 16:24:47 +02:00 committed by Eric Mastro
parent c02a89e4ac
commit b470799586
No known key found for this signature in database
GPG Key ID: 141E3048D95A4E63
6 changed files with 54 additions and 6 deletions

View File

@ -23,6 +23,9 @@ method requestStorage*(market: Market,
Future[StorageRequest] {.base, async.} =
raiseAssert("not implemented")
method myRequests*(market: Market): Future[seq[RequestId]] {.base, async.} =
raiseAssert("not implemented")
method getRequest*(market: Market,
id: RequestId):
Future[?StorageRequest] {.base, async.} =

View File

@ -8,6 +8,7 @@ import ./clock
import ./purchasing/purchase
export questionable
export chronos
export market
export purchase
@ -31,6 +32,15 @@ proc new*(_: type Purchasing, market: Market, clock: Clock): Purchasing =
requestExpiryInterval: DefaultRequestExpiryInterval,
)
proc load*(purchasing: Purchasing) {.async.} =
let market = purchasing.market
let requestIds = await market.myRequests()
for requestId in requestIds:
if request =? await market.getRequest(requestId):
let purchase = newPurchase(request, purchasing.market, purchasing.clock)
purchase.load()
purchasing.purchases[purchase.id] = purchase
proc populate*(purchasing: Purchasing, request: StorageRequest): StorageRequest =
result = request
if result.ask.proofProbability == 0.u256:

View File

@ -1,15 +1,24 @@
import ./statemachine
import ./states/pending
import ./states/unknown
import ./purchaseid
# Purchase is implemented as a state machine:
# Purchase is implemented as a state machine.
#
# pending ----> submitted ----------> started
# \ \ \
# \ \ -----------> cancelled
# \ \ \
# --------------------------------------> error
# It can either be a new (pending) purchase that still needs to be submitted
# on-chain, or it is a purchase that was previously submitted on-chain, and
# we're just restoring its (unknown) state after a node restart.
#
# |
# v
# ---------unknown
# | / / /
# v v / /
# pending ----> submitted ----> started / /
# \ \ \ / /
# \ \ ----> cancelled <--- /
# \ \ /
# -------------------> error <----------
export Purchase
export purchaseid
@ -27,6 +36,9 @@ func newPurchase*(request: StorageRequest,
proc start*(purchase: Purchase) =
purchase.switch(PurchasePending())
proc load*(purchase: Purchase) =
purchase.switch(PurchaseUnknown())
proc wait*(purchase: Purchase) {.async.} =
await purchase.future

View File

@ -0,0 +1,3 @@
import ../statemachine
type PurchaseUnknown* = ref object of PurchaseState

View File

@ -1,10 +1,14 @@
import std/sequtils
import std/tables
import std/hashes
import pkg/codex/market
export market
export tables
type
MockMarket* = ref object of Market
activeRequests*: Table[Address, seq[RequestId]]
requested*: seq[StorageRequest]
fulfilled*: seq[Fulfillment]
filled*: seq[Slot]
@ -42,6 +46,9 @@ type
requestId: RequestId
callback: OnRequestCancelled
proc hash*(address: Address): Hash =
hash(address.toArray)
proc new*(_: type MockMarket): MockMarket =
MockMarket(signer: Address.example)
@ -57,6 +64,9 @@ method requestStorage*(market: MockMarket,
subscription.callback(request.id, request.ask)
return request
method myRequests*(market: MockMarket): Future[seq[RequestId]] {.async.} =
return market.activeRequests[market.signer]
method getRequest(market: MockMarket,
id: RequestId): Future[?StorageRequest] {.async.} =
for request in market.requested:

View File

@ -96,3 +96,13 @@ suite "Purchasing":
expect PurchaseTimeout:
await purchase.wait()
check market.withdrawn == @[request.id]
test "loads active purchases from market":
let me = await market.getSigner()
let request1, request2, request3 = StorageRequest.example
market.requested = @[request1, request2, request3]
market.activeRequests[me] = @[request1.id, request2.id]
await purchasing.load()
check isSome purchasing.getPurchase(PurchaseId(request1.id))
check isSome purchasing.getPurchase(PurchaseId(request2.id))
check isNone purchasing.getPurchase(PurchaseId(request3.id))