Skip to content

Commit

Permalink
Merge branch 'master' into fix-erasure-tests
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitriy Ryajov <[email protected]>
  • Loading branch information
dryajov authored Oct 31, 2023
2 parents ad1b6c0 + 0014ffd commit a3ce00d
Show file tree
Hide file tree
Showing 32 changed files with 135 additions and 1,641 deletions.
1 change: 1 addition & 0 deletions codex/contracts/requests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type
Finished
Failed
Paid
Cancelled

proc `==`*(x, y: Nonce): bool {.borrow.}
proc `==`*(x, y: RequestId): bool {.borrow.}
Expand Down
2 changes: 1 addition & 1 deletion codex/purchasing/states/cancelled.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ method run*(state: PurchaseCancelled, machine: Machine): Future[?State] {.async.
await purchase.market.withdrawFunds(purchase.requestId)

let error = newException(Timeout, "Purchase cancelled due to timeout")
return some State(PurchaseErrored(error: error))
purchase.future.fail(error)
2 changes: 1 addition & 1 deletion codex/purchasing/states/submitted.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ method run*(state: PurchaseSubmitted, machine: Machine): Future[?State] {.async.
await subscription.unsubscribe()

proc withTimeout(future: Future[void]) {.async.} =
let expiry = request.expiry.truncate(int64)
let expiry = request.expiry.truncate(int64) + 1
await future.withTimeout(clock, expiry)

try:
Expand Down
21 changes: 19 additions & 2 deletions codex/sales/salesagent.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ proc retrieveRequest*(agent: SalesAgent) {.async.} =
if data.request.isNone:
data.request = await market.getRequest(data.requestId)

proc retrieveRequestState*(agent: SalesAgent): Future[?RequestState] {.async.} =
let data = agent.data
let market = agent.context.market
return await market.requestState(data.requestId)

proc subscribeCancellation(agent: SalesAgent) {.async.} =
let data = agent.data
let clock = agent.context.clock
Expand All @@ -62,8 +67,20 @@ proc subscribeCancellation(agent: SalesAgent) {.async.} =
without request =? data.request:
return

await clock.waitUntil(request.expiry.truncate(int64))
agent.schedule(cancelledEvent(request))
while true:
let deadline = max(clock.now, request.expiry.truncate(int64)) + 1
trace "Waiting for request to be cancelled", now=clock.now, expiry=deadline
await clock.waitUntil(deadline)

without state =? await agent.retrieveRequestState():
error "Uknown request", requestId = data.requestId
return

if state == RequestState.Cancelled:
agent.schedule(cancelledEvent(request))
break

debug "The request is not yet canceled, even though it should be. Waiting for some more time.", currentState = state, now=clock.now

data.cancelled = onCancelled()

Expand Down
28 changes: 24 additions & 4 deletions codex/sales/states/cancelled.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pkg/chronicles
import ../salesagent
import ../statemachine
import ./errorhandling
import ./errored
Expand All @@ -8,11 +9,30 @@ logScope:

type
SaleCancelled* = ref object of ErrorHandlingState
SaleCancelledError* = object of CatchableError
SaleTimeoutError* = object of SaleCancelledError

method `$`*(state: SaleCancelled): string = "SaleCancelled"

method run*(state: SaleCancelled, machine: Machine): Future[?State] {.async.} =
let error = newException(SaleTimeoutError, "Sale cancelled due to timeout")
return some State(SaleErrored(error: error))
let agent = SalesAgent(machine)
let data = agent.data
let market = agent.context.market

without request =? data.request:
raiseAssert "no sale request"

without slotIndex =? data.slotIndex:
raiseAssert("no slot index assigned")

let slot = Slot(request: request, slotIndex: slotIndex)
debug "Collecting collateral and partial payout", requestId = $data.requestId, slotIndex
await market.freeSlot(slot.id)

if onClear =? agent.context.onClear and
request =? data.request and
slotIndex =? data.slotIndex:
onClear(request, slotIndex)

if onCleanUp =? agent.onCleanUp:
await onCleanUp()

warn "Sale cancelled due to timeout", requestId = $data.requestId, slotIndex
14 changes: 14 additions & 0 deletions codex/sales/states/failed.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pkg/chronicles
import ../salesagent
import ../statemachine
import ./errorhandling
import ./errored
Expand All @@ -13,5 +14,18 @@ type
method `$`*(state: SaleFailed): string = "SaleFailed"

method run*(state: SaleFailed, machine: Machine): Future[?State] {.async.} =
let data = SalesAgent(machine).data
let market = SalesAgent(machine).context.market

without request =? data.request:
raiseAssert "no sale request"

without slotIndex =? data.slotIndex:
raiseAssert("no slot index assigned")

let slot = Slot(request: request, slotIndex: slotIndex)
debug "Removing slot from mySlots", requestId = $data.requestId, slotIndex
await market.freeSlot(slot.id)

let error = newException(SaleFailedError, "Sale failed")
return some State(SaleErrored(error: error))
2 changes: 2 additions & 0 deletions codex/sales/states/unknown.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ method run*(state: SaleUnknown, machine: Machine): Future[?State] {.async.} =
return some State(SaleFinished())
of SlotState.Failed:
return some State(SaleFailed())
of SlotState.Cancelled:
return some State(SaleCancelled())
4 changes: 0 additions & 4 deletions codex/storageproofs/por.nim

This file was deleted.

Loading

0 comments on commit a3ce00d

Please sign in to comment.