fix(sales): do not crash when retrieving request fails (#1248)

This commit is contained in:
markspanbroek 2025-06-04 13:22:14 +02:00 committed by GitHub
parent 2e1306ac2d
commit 2dd436bfb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 25 additions and 4 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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,

View File

@ -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