Return bytes to availability when finished

This commit is contained in:
Arnaud 2025-02-04 19:29:07 +01:00
parent b80b1f85be
commit 7257f1768f
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
3 changed files with 13 additions and 6 deletions

View File

@ -32,5 +32,8 @@ method run*(state: SaleFinished, machine: Machine): Future[?State] {.async.} =
info "Slot finished and paid out",
requestId = data.requestId, slotIndex = data.slotIndex
if onClear =? agent.context.onClear:
onClear(request, data.slotIndex)
if onCleanUp =? agent.onCleanUp:
await onCleanUp(returnedCollateral = state.returnedCollateral)
await onCleanUp(returnedCollateral = state.returnedCollateral, returnBytes = true)

View File

@ -26,6 +26,7 @@ asyncchecksuite "sales state 'finished'":
var returnBytesWas = bool.none
var reprocessSlotWas = bool.none
var returnedCollateralValue = UInt256.none
var saleCleared = bool.none
setup:
market = MockMarket.new()
@ -39,6 +40,8 @@ asyncchecksuite "sales state 'finished'":
let context = SalesContext(market: market, clock: clock)
agent = newSalesAgent(context, request.id, slotIndex, request.some)
agent.onCleanUp = onCleanUp
agent.context.onClear = some proc(request: StorageRequest, idx: UInt256) =
saleCleared = some true
state = SaleFinished(returnedCollateral: some currentCollateral)
test "switches to cancelled state when request expires":
@ -49,8 +52,9 @@ asyncchecksuite "sales state 'finished'":
let next = state.onFailed(request)
check !next of SaleFailed
test "calls onCleanUp with returnBytes = false, reprocessSlot = true, and returnedCollateral = currentCollateral":
test "calls onCleanUp with returnBytes = true, reprocessSlot = true, and returnedCollateral = currentCollateral":
discard await state.run(agent)
check eventually returnBytesWas == some false
check eventually returnBytesWas == some true
check eventually reprocessSlotWas == some false
check eventually returnedCollateralValue == some currentCollateral
check eventually saleCleared == some true

View File

@ -354,14 +354,14 @@ asyncchecksuite "Sales":
check eventually getAvailability().freeSize ==
availability.freeSize - request.ask.slotSize
test "non-downloaded bytes are returned to availability once finished":
test "bytes are returned to availability once finished":
var slotIndex = 0.u256
sales.onStore = proc(
request: StorageRequest, slot: UInt256, onBatch: BatchProc
): Future[?!void] {.async.} =
slotIndex = slot
let blk = bt.Block.new(@[1.byte]).get
await onBatch(@[blk])
await onBatch(blk.repeat(request.ask.slotSize.truncate(int)))
let sold = newFuture[void]()
sales.onSale = proc(request: StorageRequest, slotIndex: UInt256) =
@ -377,7 +377,7 @@ asyncchecksuite "Sales":
market.slotState[request.slotId(slotIndex)] = SlotState.Finished
clock.advance(request.ask.duration.truncate(int64))
check eventually getAvailability().freeSize == origSize - 1
check eventually getAvailability().freeSize == origSize
test "ignores download when duration not long enough":
availability.duration = request.ask.duration - 1