diff --git a/codex/sales/statemachine.nim b/codex/sales/statemachine.nim index ec770ece0..d17325491 100644 --- a/codex/sales/statemachine.nim +++ b/codex/sales/statemachine.nim @@ -12,7 +12,7 @@ export asyncstatemachine type SaleState* = ref object of State - SaleError* = ref object of CodexError + SaleError* = object of CodexError method onCancelled*( state: SaleState, request: StorageRequest diff --git a/codex/sales/states/preparing.nim b/codex/sales/states/preparing.nim index 807bb196c..dba249de5 100644 --- a/codex/sales/states/preparing.nim +++ b/codex/sales/states/preparing.nim @@ -51,7 +51,9 @@ method run*( await agent.subscribe() without request =? data.request: - raiseAssert "no sale request" + error "request could not be retrieved", id = data.requestId + let error = newException(SaleError, "request could not be retrieved") + return some State(SaleErrored(error: error)) let slotId = slotId(data.requestId, data.slotIndex) let state = await market.slotState(slotId) diff --git a/codex/sales/states/unknown.nim b/codex/sales/states/unknown.nim index d182d7442..b714a4b98 100644 --- a/codex/sales/states/unknown.nim +++ b/codex/sales/states/unknown.nim @@ -38,6 +38,11 @@ method run*( await agent.retrieveRequest() await agent.subscribe() + without request =? data.request: + error "request could not be retrieved", id = data.requestId + let error = newException(SaleError, "request could not be retrieved") + return some State(SaleErrored(error: error)) + let slotId = slotId(data.requestId, data.slotIndex) let slotState = await market.slotState(slotId) diff --git a/tests/codex/sales/states/testpreparing.nim b/tests/codex/sales/states/testpreparing.nim index 802489a13..747544117 100644 --- a/tests/codex/sales/states/testpreparing.nim +++ b/tests/codex/sales/states/testpreparing.nim @@ -72,6 +72,12 @@ asyncchecksuite "sales state 'preparing'": let next = state.onSlotFilled(request.id, slotIndex) check !next of SaleFilled + test "run switches to errored when the request cannot be retrieved": + agent = newSalesAgent(context, request.id, slotIndex, StorageRequest.none) + let next = !(await state.run(agent)) + check next of SaleErrored + check SaleErrored(next).error.msg == "request could not be retrieved" + proc createAvailability(enabled = true) {.async.} = let a = await reservations.createAvailability( availability.totalSize, diff --git a/tests/codex/sales/states/testunknown.nim b/tests/codex/sales/states/testunknown.nim index 98b23224a..4806122f7 100644 --- a/tests/codex/sales/states/testunknown.nim +++ b/tests/codex/sales/states/testunknown.nim @@ -20,15 +20,22 @@ suite "sales state 'unknown'": let slotId = slotId(request.id, slotIndex) var market: MockMarket + var context: SalesContext var agent: SalesAgent var state: SaleUnknown setup: market = MockMarket.new() - let context = SalesContext(market: market) - agent = newSalesAgent(context, request.id, slotIndex, StorageRequest.none) + context = SalesContext(market: market) + agent = newSalesAgent(context, request.id, slotIndex, request.some) state = SaleUnknown.new() + test "switches to error state when the request cannot be retrieved": + agent = newSalesAgent(context, request.id, slotIndex, StorageRequest.none) + let next = await state.run(agent) + check !next of SaleErrored + check SaleErrored(!next).error.msg == "request could not be retrieved" + test "switches to error state when on chain state cannot be fetched": let next = await state.run(agent) check !next of SaleErrored @@ -37,6 +44,7 @@ suite "sales state 'unknown'": market.slotState[slotId] = SlotState.Free let next = await state.run(agent) check !next of SaleErrored + check SaleErrored(!next).error.msg == "Slot state on chain should not be 'free'" test "switches to filled state when on chain state is 'filled'": market.slotState[slotId] = SlotState.Filled