slotqueue: add repair reward to slot queue

This commit is contained in:
Mark Spanbroek 2025-03-13 14:42:08 +01:00
parent 5f36f384fa
commit 4e506ff6f3
No known key found for this signature in database
GPG Key ID: FBE3E9548D427C00
2 changed files with 29 additions and 14 deletions

View File

@ -332,16 +332,18 @@ proc onSlotFreed(sales: Sales, requestId: RequestId, slotIndex: uint64) =
return
let collateral = request.ask.collateralPerSlot.stuint(256)
let percentage = context.market.repairRewardPercentage
let repairReward = (collateral * percentage.u256) div 100.u256
if slotIndex > uint16.high.uint64:
error "Cannot cast slot index to uint16, value = ", slotIndex
return
without slotQueueItem =?
SlotQueueItem.init(request, slotIndex.uint16, collateral = collateral).catch,
err:
warn "Too many slots, cannot add to queue", error = err.msgDetail
return
SlotQueueItem.init(request, slotIndex.uint16, collateral, repairReward).catch,
err:
warn "Too many slots, cannot add to queue", error = err.msgDetail
return
if err =? queue.push(slotQueueItem).errorOption:
if err of SlotQueueItemExistsError:

View File

@ -34,7 +34,8 @@ type
slotSize: uint64
duration: uint64
pricePerBytePerSecond: UInt256
collateral: UInt256 # Collateral computed
repairReward: UInt256
collateral: UInt256
expiry: uint64
seen: bool
@ -70,11 +71,13 @@ const DefaultMaxWorkers = 3
const DefaultMaxSize = 128'u16
proc profitability(item: SlotQueueItem): UInt256 =
StorageAsk(
duration: item.duration,
pricePerBytePerSecond: item.pricePerBytePerSecond,
slotSize: item.slotSize,
).pricePerSlot
let price =
StorageAsk(
duration: item.duration,
pricePerBytePerSecond: item.pricePerBytePerSecond,
slotSize: item.slotSize,
).pricePerSlot
return price.stuint(256) + item.repairReward
proc `<`*(a, b: SlotQueueItem): bool =
# for A to have a higher priority than B (in a min queue), A must be less than
@ -145,6 +148,7 @@ proc init*(
ask: StorageAsk,
expiry: uint64,
collateral: UInt256,
repairReward = 0.u256,
seen = false,
): SlotQueueItem =
SlotQueueItem(
@ -163,8 +167,11 @@ proc init*(
request: StorageRequest,
slotIndex: uint16,
collateral: UInt256,
repairReward = 0.u256,
): SlotQueueItem =
SlotQueueItem.init(request.id, slotIndex, request.ask, request.expiry, collateral)
SlotQueueItem.init(
request.id, slotIndex, request.ask, request.expiry.u64, collateral, repairReward
)
proc init*(
_: type SlotQueueItem,
@ -172,13 +179,14 @@ proc init*(
ask: StorageAsk,
expiry: uint64,
collateral: UInt256,
repairReward = 0.u256,
): seq[SlotQueueItem] {.raises: [SlotsOutOfRangeError].} =
if not ask.slots.inRange:
raise newException(SlotsOutOfRangeError, "Too many slots")
var i = 0'u16
proc initSlotQueueItem(): SlotQueueItem =
let item = SlotQueueItem.init(requestId, i, ask, expiry, collateral)
let item = SlotQueueItem.init(requestId, i, ask, expiry, collateral, repairReward)
inc i
return item
@ -187,9 +195,14 @@ proc init*(
return items
proc init*(
_: type SlotQueueItem, request: StorageRequest, collateral: UInt256
_: type SlotQueueItem,
request: StorageRequest,
collateral: UInt256,
repairReward = 0.u256,
): seq[SlotQueueItem] =
return SlotQueueItem.init(request.id, request.ask, request.expiry, collateral)
return SlotQueueItem.init(
request.id, request.ask, request.expiry, collateral, repairReward
)
proc inRange*(val: SomeUnsignedInt): bool =
val.uint16 in SlotQueueSize.low .. SlotQueueSize.high