mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-02 13:33:10 +00:00
* cleanup imports and logs * add BlockHandle type * revert deps * refactor: async error handling and future tracking improvements - Update async procedures to use explicit raises annotation - Modify TrackedFutures to handle futures with no raised exceptions - Replace `asyncSpawn` with explicit future tracking - Update test suites to use `unittest2` - Standardize error handling across network and async components - Remove deprecated error handling patterns This commit introduces a more robust approach to async error handling and future management, improving type safety and reducing potential runtime errors. * bump nim-serde * remove asyncSpawn * rework background downloads and prefetch * imporove logging * refactor: enhance async procedures with error handling and raise annotations * misc cleanup * misc * refactor: implement allFinishedFailed to aggregate future results with success and failure tracking * refactor: update error handling in reader procedures to raise ChunkerError and CancelledError * refactor: improve error handling in wantListHandler and accountHandler procedures * refactor: simplify LPStreamReadError creation by consolidating parameters * refactor: enhance error handling in AsyncStreamWrapper to catch unexpected errors * refactor: enhance error handling in advertiser and discovery loops to improve resilience * misc * refactor: improve code structure and readability * remove cancellation from addSlotToQueue * refactor: add assertion for unexpected errors in local store checks * refactor: prevent tracking of finished futures and improve test assertions * refactor: improve error handling in local store checks * remove usage of msgDetail * feat: add initial implementation of discovery engine and related components * refactor: improve task scheduling logic by removing unnecessary break statement * break after scheduling a task * make taskHandler cancelable * refactor: update async handlers to raise CancelledError * refactor(advertiser): streamline error handling and improve task flow in advertise loops * fix: correct spelling of "divisible" in error messages and comments * refactor(discovery): simplify discovery task loop and improve error handling * refactor(engine): filter peers before processing in cancelBlocks procedure
149 lines
4.3 KiB
Nim
149 lines
4.3 KiB
Nim
import pkg/chronos
|
|
import pkg/questionable
|
|
import pkg/questionable/results
|
|
import pkg/stint
|
|
import pkg/upraises
|
|
import ../contracts/requests
|
|
import ../errors
|
|
import ../logutils
|
|
import ../utils/exceptions
|
|
import ./statemachine
|
|
import ./salescontext
|
|
import ./salesdata
|
|
import ./reservations
|
|
|
|
export reservations
|
|
|
|
logScope:
|
|
topics = "marketplace sales"
|
|
|
|
type
|
|
SalesAgent* = ref object of Machine
|
|
context*: SalesContext
|
|
data*: SalesData
|
|
subscribed: bool
|
|
# Slot-level callbacks.
|
|
onCleanUp*: OnCleanUp
|
|
onFilled*: ?OnFilled
|
|
|
|
OnCleanUp* = proc(
|
|
returnBytes = false, reprocessSlot = false, returnedCollateral = UInt256.none
|
|
): Future[void] {.gcsafe, upraises: [].}
|
|
OnFilled* = proc(request: StorageRequest, slotIndex: uint64) {.gcsafe, upraises: [].}
|
|
|
|
SalesAgentError = object of CodexError
|
|
AllSlotsFilledError* = object of SalesAgentError
|
|
|
|
func `==`*(a, b: SalesAgent): bool =
|
|
a.data.requestId == b.data.requestId and a.data.slotIndex == b.data.slotIndex
|
|
|
|
proc newSalesAgent*(
|
|
context: SalesContext,
|
|
requestId: RequestId,
|
|
slotIndex: uint64,
|
|
request: ?StorageRequest,
|
|
): SalesAgent =
|
|
var agent = SalesAgent.new()
|
|
agent.context = context
|
|
agent.data = SalesData(requestId: requestId, slotIndex: slotIndex, request: request)
|
|
return agent
|
|
|
|
proc retrieveRequest*(agent: SalesAgent) {.async.} =
|
|
let data = agent.data
|
|
let market = agent.context.market
|
|
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)
|
|
|
|
func state*(agent: SalesAgent): ?string =
|
|
proc description(state: State): string =
|
|
$state
|
|
|
|
agent.query(description)
|
|
|
|
proc subscribeCancellation(agent: SalesAgent) {.async.} =
|
|
let data = agent.data
|
|
let clock = agent.context.clock
|
|
|
|
proc onCancelled() {.async: (raises: []).} =
|
|
without request =? data.request:
|
|
return
|
|
|
|
try:
|
|
let market = agent.context.market
|
|
let expiry = await market.requestExpiresAt(data.requestId)
|
|
|
|
while true:
|
|
let deadline = max(clock.now, expiry) + 1
|
|
trace "Waiting for request to be cancelled", now = clock.now, expiry = deadline
|
|
await clock.waitUntil(deadline)
|
|
|
|
without state =? await agent.retrieveRequestState():
|
|
error "Unknown request", requestId = data.requestId
|
|
return
|
|
|
|
case state
|
|
of New:
|
|
discard
|
|
of RequestState.Cancelled:
|
|
agent.schedule(cancelledEvent(request))
|
|
break
|
|
of RequestState.Started, RequestState.Finished, RequestState.Failed:
|
|
break
|
|
|
|
debug "The request is not yet canceled, even though it should be. Waiting for some more time.",
|
|
currentState = state, now = clock.now
|
|
except CancelledError:
|
|
trace "Waiting for expiry to lapse was cancelled", requestId = data.requestId
|
|
except CatchableError as e:
|
|
error "Error while waiting for expiry to lapse", error = e.msgDetail
|
|
|
|
data.cancelled = onCancelled()
|
|
|
|
method onFulfilled*(
|
|
agent: SalesAgent, requestId: RequestId
|
|
) {.base, gcsafe, upraises: [].} =
|
|
let cancelled = agent.data.cancelled
|
|
if agent.data.requestId == requestId and not cancelled.isNil and not cancelled.finished:
|
|
cancelled.cancelSoon()
|
|
|
|
method onFailed*(
|
|
agent: SalesAgent, requestId: RequestId
|
|
) {.base, gcsafe, upraises: [].} =
|
|
without request =? agent.data.request:
|
|
return
|
|
if agent.data.requestId == requestId:
|
|
agent.schedule(failedEvent(request))
|
|
|
|
method onSlotFilled*(
|
|
agent: SalesAgent, requestId: RequestId, slotIndex: uint64
|
|
) {.base, gcsafe, upraises: [].} =
|
|
if agent.data.requestId == requestId and agent.data.slotIndex == slotIndex:
|
|
agent.schedule(slotFilledEvent(requestId, slotIndex))
|
|
|
|
proc subscribe*(agent: SalesAgent) {.async.} =
|
|
if agent.subscribed:
|
|
return
|
|
|
|
await agent.subscribeCancellation()
|
|
agent.subscribed = true
|
|
|
|
proc unsubscribe*(agent: SalesAgent) {.async.} =
|
|
if not agent.subscribed:
|
|
return
|
|
|
|
let data = agent.data
|
|
if not data.cancelled.isNil and not data.cancelled.finished:
|
|
await data.cancelled.cancelAndWait()
|
|
data.cancelled = nil
|
|
|
|
agent.subscribed = false
|
|
|
|
proc stop*(agent: SalesAgent) {.async.} =
|
|
await Machine(agent).stop()
|
|
await agent.unsubscribe()
|