Skip to content

fix(sales): replaces then with asyncSpawn #1036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2024
Merged
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/contracts/market.nim
Original file line number Diff line number Diff line change
@@ -116,7 +116,7 @@
await market.approveFunds(request.price())
discard await market.contract.requestStorage(request).confirm(1)

method getRequest(market: OnChainMarket,
method getRequest*(market: OnChainMarket,

Check warning on line 119 in codex/contracts/market.nim

Codecov / codecov/patch

codex/contracts/market.nim#L119

Added line #L119 was not covered by tests
id: RequestId): Future[?StorageRequest] {.async.} =
convertEthersError:
try:
37 changes: 17 additions & 20 deletions codex/sales.nim
Original file line number Diff line number Diff line change
@@ -16,8 +16,8 @@
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.
##
@@ -325,7 +325,7 @@

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
@@ -336,25 +336,22 @@
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:

Check warning on line 345 in codex/sales.nim

Codecov / codecov/patch

codex/sales.nim#L345

Added line #L345 was not covered by tests
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

Check warning on line 349 in codex/sales.nim

Codecov / codecov/patch

codex/sales.nim#L347-L349

Added lines #L347 - L349 were not covered by tests

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
@@ -482,7 +479,7 @@
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

@@ -518,7 +515,7 @@

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

2 changes: 1 addition & 1 deletion tests/codex/helpers/mockmarket.nim
Original file line number Diff line number Diff line change
@@ -145,7 +145,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:
1 change: 1 addition & 0 deletions tests/codex/sales/testsales.nim
Original file line number Diff line number Diff line change
@@ -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()