Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codex/sales/statemachine.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion codex/sales/states/preparing.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions codex/sales/states/unknown.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions tests/codex/sales/states/testpreparing.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions tests/codex/sales/states/testunknown.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading