From 79f6f817668fb82960c88a4adbbcd9f1c9f8fd73 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 24 Oct 2022 16:43:26 +0200 Subject: [PATCH] [purchasing] load purchase states --- codex/contracts/requests.nim | 6 ++++++ codex/market.nim | 4 ++++ codex/purchasing/states/unknown.nim | 24 ++++++++++++++++++++++++ tests/codex/helpers/mockmarket.nim | 8 ++++++++ tests/codex/testpurchasing.nim | 13 +++++++++++++ 5 files changed, 55 insertions(+) diff --git a/codex/contracts/requests.nim b/codex/contracts/requests.nim index 10612765..a0f4a12d 100644 --- a/codex/contracts/requests.nim +++ b/codex/contracts/requests.nim @@ -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.} diff --git a/codex/market.nim b/codex/market.nim index 259729a1..7a710e42 100644 --- a/codex/market.nim +++ b/codex/market.nim @@ -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.} = diff --git a/codex/purchasing/states/unknown.nim b/codex/purchasing/states/unknown.nim index 1edc2093..b95c22e9 100644 --- a/codex/purchasing/states/unknown.nim +++ b/codex/purchasing/states/unknown.nim @@ -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)) diff --git a/tests/codex/helpers/mockmarket.nim b/tests/codex/helpers/mockmarket.nim index fe84f3e9..a1701904 100644 --- a/tests/codex/helpers/mockmarket.nim +++ b/tests/codex/helpers/mockmarket.nim @@ -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.} = diff --git a/tests/codex/testpurchasing.nim b/tests/codex/testpurchasing.nim index 3ac1bd03..98f58615 100644 --- a/tests/codex/testpurchasing.nim +++ b/tests/codex/testpurchasing.nim @@ -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