[purchasing] load purchase states

This commit is contained in:
Mark Spanbroek 2022-10-24 16:43:26 +02:00 committed by Eric Mastro
parent b470799586
commit 79f6f81766
No known key found for this signature in database
GPG Key ID: 141E3048D95A4E63
5 changed files with 55 additions and 0 deletions

View File

@ -33,6 +33,12 @@ type
SlotId* = distinct array[32, byte]
RequestId* = distinct array[32, byte]
Nonce* = distinct array[32, byte]
RequestState* {.pure.} = enum
New
Started
Cancelled
Finished
Failed
proc `==`*(x, y: Nonce): bool {.borrow.}
proc `==`*(x, y: RequestId): bool {.borrow.}

View File

@ -31,6 +31,10 @@ method getRequest*(market: Market,
Future[?StorageRequest] {.base, async.} =
raiseAssert("not implemented")
method getState*(market: Market,
requestId: RequestId): Future[?RequestState] {.base, async.} =
raiseAssert("not implemented")
method getHost*(market: Market,
requestId: RequestId,
slotIndex: UInt256): Future[?Address] {.base, async.} =

View File

@ -1,3 +1,27 @@
import ../statemachine
import ./submitted
import ./started
import ./cancelled
import ./error
type PurchaseUnknown* = ref object of PurchaseState
method enterAsync(state: PurchaseUnknown) {.async.} =
without purchase =? (state.context as Purchase):
raiseAssert "invalid state"
try:
if requestState =? await purchase.market.getState(purchase.request.id):
case requestState
of RequestState.New:
state.switch(PurchaseSubmitted())
of RequestState.Started:
state.switch(PurchaseStarted())
of RequestState.Cancelled:
state.switch(PurchaseCancelled())
of RequestState.Finished:
discard # TODO
of RequestState.Failed:
discard # TODO
except CatchableError as error:
state.switch(PurchaseError(error: error))

View File

@ -10,6 +10,7 @@ type
MockMarket* = ref object of Market
activeRequests*: Table[Address, seq[RequestId]]
requested*: seq[StorageRequest]
state*: Table[RequestId, RequestState]
fulfilled*: seq[Fulfillment]
filled*: seq[Slot]
withdrawn*: seq[RequestId]
@ -49,6 +50,9 @@ type
proc hash*(address: Address): Hash =
hash(address.toArray)
proc hash*(requestId: RequestId): Hash =
hash(requestId.toArray)
proc new*(_: type MockMarket): MockMarket =
MockMarket(signer: Address.example)
@ -74,6 +78,10 @@ method getRequest(market: MockMarket,
return some request
return none StorageRequest
method getState*(market: MockMarket,
requestId: RequestId): Future[?RequestState] {.async.} =
return market.state.?[requestId]
method getHost(market: MockMarket,
requestId: RequestId,
slotIndex: UInt256): Future[?Address] {.async.} =

View File

@ -106,3 +106,16 @@ suite "Purchasing":
check isSome purchasing.getPurchase(PurchaseId(request1.id))
check isSome purchasing.getPurchase(PurchaseId(request2.id))
check isNone purchasing.getPurchase(PurchaseId(request3.id))
test "loads correct state for 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, request3.id]
market.state[request1.id] = RequestState.New
market.state[request2.id] = RequestState.Started
market.state[request3.id] = RequestState.Cancelled
await purchasing.load()
check purchasing.getPurchase(PurchaseId(request1.id)).?finished == false.some
check purchasing.getPurchase(PurchaseId(request2.id)).?finished == true.some
check purchasing.getPurchase(PurchaseId(request3.id)).?finished == true.some