Skip to content

Commit

Permalink
fix(sales): replaces then with asyncSpawn (#1036)
Browse files Browse the repository at this point in the history
- ensures `addSlotToQueue` does not raise exceptions as it is now asyncSpawned
  • Loading branch information
emizzle authored Dec 15, 2024
1 parent 92a0eda commit 01fb685
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion codex/contracts/market.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ method requestStorage(market: OnChainMarket, request: StorageRequest){.async.} =
await market.approveFunds(request.price())
discard await market.contract.requestStorage(request).confirm(1)

method getRequest(market: OnChainMarket,
method getRequest*(market: OnChainMarket,
id: RequestId): Future[?StorageRequest] {.async.} =
convertEthersError:
try:
Expand Down
37 changes: 17 additions & 20 deletions codex/sales.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import ./sales/statemachine
import ./sales/slotqueue
import ./sales/states/preparing
import ./sales/states/unknown
import ./utils/then
import ./utils/trackedfutures
import ./utils/exceptions

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

trace "slot freed, adding to queue"

proc addSlotToQueue() {.async.} =
proc addSlotToQueue() {.async: (raises: []).} =
let context = sales.context
let market = context.market
let queue = context.slotQueue
Expand All @@ -336,25 +336,22 @@ proc onSlotFreed(sales: Sales,
trace "no existing request metadata, getting request info from contract"
# if there's no existing slot for that request, retrieve the request
# from the contract.
without request =? await market.getRequest(requestId):
error "unknown request in contract"
return

found = SlotQueueItem.init(request, slotIndex.truncate(uint16))
try:
without request =? await market.getRequest(requestId):
error "unknown request in contract"
return

found = SlotQueueItem.init(request, slotIndex.truncate(uint16))
except CancelledError:
discard # do not propagate as addSlotToQueue was asyncSpawned
except CatchableError as e:
error "failed to get request from contract and add slots to queue",
error = e.msgDetail

if err =? queue.push(found).errorOption:
raise err
error "failed to push slot items to queue", error = err.msgDetail

addSlotToQueue()
.track(sales)
.catch(proc(err: ref CatchableError) =
if err of SlotQueueItemExistsError:
error "Failed to push item to queue becaue it already exists"
elif err of QueueNotRunningError:
warn "Failed to push item to queue becaue queue is not running"
else:
warn "Error adding request to SlotQueue", error = err.msg
)
asyncSpawn addSlotToQueue().track(sales)

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

proc startSlotQueue(sales: Sales) {.async.} =
proc startSlotQueue(sales: Sales) =
let slotQueue = sales.context.slotQueue
let reservations = sales.context.reservations

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

proc start*(sales: Sales) {.async.} =
await sales.load()
await sales.startSlotQueue()
sales.startSlotQueue()
await sales.subscribe()
sales.running = true

Expand Down
2 changes: 1 addition & 1 deletion tests/codex/helpers/mockmarket.nim
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ method myRequests*(market: MockMarket): Future[seq[RequestId]] {.async.} =
method mySlots*(market: MockMarket): Future[seq[SlotId]] {.async.} =
return market.activeSlots[market.signer]

method getRequest(market: MockMarket,
method getRequest*(market: MockMarket,
id: RequestId): Future[?StorageRequest] {.async.} =
for request in market.requested:
if request.id == id:
Expand Down
1 change: 1 addition & 0 deletions tests/codex/sales/testsales.nim
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ asyncchecksuite "Sales":
request.ask.slots = 2
market.requested = @[request]
market.requestState[request.id] = RequestState.New
market.requestEnds[request.id] = request.expiry.toSecondsSince1970

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

0 comments on commit 01fb685

Please sign in to comment.