mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-04 06:23:06 +00:00
fix(sales): do not crash when retrieving request fails (#1248)
This commit is contained in:
parent
2e1306ac2d
commit
2dd436bfb7
@ -12,7 +12,7 @@ export asyncstatemachine
|
|||||||
|
|
||||||
type
|
type
|
||||||
SaleState* = ref object of State
|
SaleState* = ref object of State
|
||||||
SaleError* = ref object of CodexError
|
SaleError* = object of CodexError
|
||||||
|
|
||||||
method onCancelled*(
|
method onCancelled*(
|
||||||
state: SaleState, request: StorageRequest
|
state: SaleState, request: StorageRequest
|
||||||
|
|||||||
@ -51,7 +51,9 @@ method run*(
|
|||||||
await agent.subscribe()
|
await agent.subscribe()
|
||||||
|
|
||||||
without request =? data.request:
|
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 slotId = slotId(data.requestId, data.slotIndex)
|
||||||
let state = await market.slotState(slotId)
|
let state = await market.slotState(slotId)
|
||||||
|
|||||||
@ -38,6 +38,11 @@ method run*(
|
|||||||
await agent.retrieveRequest()
|
await agent.retrieveRequest()
|
||||||
await agent.subscribe()
|
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 slotId = slotId(data.requestId, data.slotIndex)
|
||||||
let slotState = await market.slotState(slotId)
|
let slotState = await market.slotState(slotId)
|
||||||
|
|
||||||
|
|||||||
@ -72,6 +72,12 @@ asyncchecksuite "sales state 'preparing'":
|
|||||||
let next = state.onSlotFilled(request.id, slotIndex)
|
let next = state.onSlotFilled(request.id, slotIndex)
|
||||||
check !next of SaleFilled
|
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.} =
|
proc createAvailability(enabled = true) {.async.} =
|
||||||
let a = await reservations.createAvailability(
|
let a = await reservations.createAvailability(
|
||||||
availability.totalSize,
|
availability.totalSize,
|
||||||
|
|||||||
@ -20,15 +20,22 @@ suite "sales state 'unknown'":
|
|||||||
let slotId = slotId(request.id, slotIndex)
|
let slotId = slotId(request.id, slotIndex)
|
||||||
|
|
||||||
var market: MockMarket
|
var market: MockMarket
|
||||||
|
var context: SalesContext
|
||||||
var agent: SalesAgent
|
var agent: SalesAgent
|
||||||
var state: SaleUnknown
|
var state: SaleUnknown
|
||||||
|
|
||||||
setup:
|
setup:
|
||||||
market = MockMarket.new()
|
market = MockMarket.new()
|
||||||
let context = SalesContext(market: market)
|
context = SalesContext(market: market)
|
||||||
agent = newSalesAgent(context, request.id, slotIndex, StorageRequest.none)
|
agent = newSalesAgent(context, request.id, slotIndex, request.some)
|
||||||
state = SaleUnknown.new()
|
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":
|
test "switches to error state when on chain state cannot be fetched":
|
||||||
let next = await state.run(agent)
|
let next = await state.run(agent)
|
||||||
check !next of SaleErrored
|
check !next of SaleErrored
|
||||||
@ -37,6 +44,7 @@ suite "sales state 'unknown'":
|
|||||||
market.slotState[slotId] = SlotState.Free
|
market.slotState[slotId] = SlotState.Free
|
||||||
let next = await state.run(agent)
|
let next = await state.run(agent)
|
||||||
check !next of SaleErrored
|
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'":
|
test "switches to filled state when on chain state is 'filled'":
|
||||||
market.slotState[slotId] = SlotState.Filled
|
market.slotState[slotId] = SlotState.Filled
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user