Skip to content

Commit 01fb685

Browse files
authored
fix(sales): replaces then with asyncSpawn (#1036)
- ensures `addSlotToQueue` does not raise exceptions as it is now asyncSpawned
1 parent 92a0eda commit 01fb685

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

Diff for: codex/contracts/market.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ method requestStorage(market: OnChainMarket, request: StorageRequest){.async.} =
115115
await market.approveFunds(request.price())
116116
discard await market.contract.requestStorage(request).confirm(1)
117117

118-
method getRequest(market: OnChainMarket,
118+
method getRequest*(market: OnChainMarket,
119119
id: RequestId): Future[?StorageRequest] {.async.} =
120120
convertEthersError:
121121
try:

Diff for: codex/sales.nim

+17-20
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import ./sales/statemachine
1616
import ./sales/slotqueue
1717
import ./sales/states/preparing
1818
import ./sales/states/unknown
19-
import ./utils/then
2019
import ./utils/trackedfutures
20+
import ./utils/exceptions
2121

2222
## Sales holds a list of available storage that it may sell.
2323
##
@@ -325,7 +325,7 @@ proc onSlotFreed(sales: Sales,
325325

326326
trace "slot freed, adding to queue"
327327

328-
proc addSlotToQueue() {.async.} =
328+
proc addSlotToQueue() {.async: (raises: []).} =
329329
let context = sales.context
330330
let market = context.market
331331
let queue = context.slotQueue
@@ -336,25 +336,22 @@ proc onSlotFreed(sales: Sales,
336336
trace "no existing request metadata, getting request info from contract"
337337
# if there's no existing slot for that request, retrieve the request
338338
# from the contract.
339-
without request =? await market.getRequest(requestId):
340-
error "unknown request in contract"
341-
return
342-
343-
found = SlotQueueItem.init(request, slotIndex.truncate(uint16))
339+
try:
340+
without request =? await market.getRequest(requestId):
341+
error "unknown request in contract"
342+
return
343+
344+
found = SlotQueueItem.init(request, slotIndex.truncate(uint16))
345+
except CancelledError:
346+
discard # do not propagate as addSlotToQueue was asyncSpawned
347+
except CatchableError as e:
348+
error "failed to get request from contract and add slots to queue",
349+
error = e.msgDetail
344350

345351
if err =? queue.push(found).errorOption:
346-
raise err
352+
error "failed to push slot items to queue", error = err.msgDetail
347353

348-
addSlotToQueue()
349-
.track(sales)
350-
.catch(proc(err: ref CatchableError) =
351-
if err of SlotQueueItemExistsError:
352-
error "Failed to push item to queue becaue it already exists"
353-
elif err of QueueNotRunningError:
354-
warn "Failed to push item to queue becaue queue is not running"
355-
else:
356-
warn "Error adding request to SlotQueue", error = err.msg
357-
)
354+
asyncSpawn addSlotToQueue().track(sales)
358355

359356
proc subscribeRequested(sales: Sales) {.async.} =
360357
let context = sales.context
@@ -482,7 +479,7 @@ proc subscribeSlotReservationsFull(sales: Sales) {.async.} =
482479
except CatchableError as e:
483480
error "Unable to subscribe to slot filled events", msg = e.msg
484481

485-
proc startSlotQueue(sales: Sales) {.async.} =
482+
proc startSlotQueue(sales: Sales) =
486483
let slotQueue = sales.context.slotQueue
487484
let reservations = sales.context.reservations
488485

@@ -518,7 +515,7 @@ proc unsubscribe(sales: Sales) {.async.} =
518515

519516
proc start*(sales: Sales) {.async.} =
520517
await sales.load()
521-
await sales.startSlotQueue()
518+
sales.startSlotQueue()
522519
await sales.subscribe()
523520
sales.running = true
524521

Diff for: tests/codex/helpers/mockmarket.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ method myRequests*(market: MockMarket): Future[seq[RequestId]] {.async.} =
155155
method mySlots*(market: MockMarket): Future[seq[SlotId]] {.async.} =
156156
return market.activeSlots[market.signer]
157157

158-
method getRequest(market: MockMarket,
158+
method getRequest*(market: MockMarket,
159159
id: RequestId): Future[?StorageRequest] {.async.} =
160160
for request in market.requested:
161161
if request.id == id:

Diff for: tests/codex/sales/testsales.nim

+1
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ asyncchecksuite "Sales":
566566
request.ask.slots = 2
567567
market.requested = @[request]
568568
market.requestState[request.id] = RequestState.New
569+
market.requestEnds[request.id] = request.expiry.toSecondsSince1970
569570

570571
proc fillSlot(slotIdx: UInt256 = 0.u256) {.async.} =
571572
let address = await market.getSigner()

0 commit comments

Comments
 (0)