[sales] Pass along request and slot index in onStore callback

This commit is contained in:
Mark Spanbroek 2022-08-02 17:23:12 +02:00 committed by markspanbroek
parent 3a3d083345
commit cb5427ca95
3 changed files with 31 additions and 13 deletions

View File

@ -313,11 +313,13 @@ proc start*(node: CodexNodeRef) {.async.} =
if contracts =? node.contracts:
# TODO: remove Sales callbacks, pass BlockStore and StorageProofs instead
contracts.sales.onStore = proc(cid: string, _: Availability) {.async.} =
contracts.sales.onStore = proc(request: StorageRequest,
slot: UInt256,
availability: Availability) {.async.} =
## store data in local storage
##
without cid =? Cid.init(cid):
without cid =? Cid.init(request.content.cid):
trace "Unable to parse Cid", cid
raise newException(CodexError, "Unable to parse Cid")

View File

@ -54,7 +54,9 @@ type
running: ?Future[void]
waiting: ?Future[void]
finished: bool
OnStore = proc(cid: string, availability: Availability): Future[void] {.gcsafe, upraises: [].}
OnStore = proc(request: StorageRequest,
slot: UInt256,
availability: Availability): Future[void] {.gcsafe, upraises: [].}
OnProve = proc(cid: string): Future[seq[byte]] {.gcsafe, upraises: [].}
OnClear = proc(availability: Availability, request: StorageRequest) {.gcsafe, upraises: [].}
OnSale = proc(availability: Availability,
@ -184,7 +186,7 @@ proc start(agent: SalesAgent) {.async.} =
agent.waiting = some agent.waitForExpiry()
await onStore(request.content.cid, availability)
await onStore(request, slotIndex, availability)
let proof = await onProve(request.content.cid)
await market.fillSlot(request.id, slotIndex, proof)
except CancelledError:

View File

@ -33,7 +33,9 @@ suite "Sales":
market = MockMarket.new()
clock = MockClock.new()
sales = Sales.new(market, clock)
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
sales.onStore = proc(request: StorageRequest,
slot: UInt256,
availability: Availability) {.async.} =
discard
sales.onProve = proc(cid: string): Future[seq[byte]] {.async.} =
return proof
@ -85,18 +87,26 @@ suite "Sales":
check sales.available == @[availability]
test "retrieves and stores data locally":
var storingCid: string
var storingRequest: StorageRequest
var storingSlot: UInt256
var storingAvailability: Availability
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
storingCid = cid
sales.onStore = proc(request: StorageRequest,
slot: UInt256,
availability: Availability) {.async.} =
storingRequest = request
storingSlot = slot
storingAvailability = availability
sales.add(availability)
discard await market.requestStorage(request)
check storingCid == request.content.cid
let requested = await market.requestStorage(request)
check storingRequest == requested
check storingSlot < request.ask.slots.u256
check storingAvailability == availability
test "makes storage available again when data retrieval fails":
let error = newException(IOError, "data retrieval failed")
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
sales.onStore = proc(request: StorageRequest,
slot: UInt256,
availability: Availability) {.async.} =
raise error
sales.add(availability)
discard await market.requestStorage(request)
@ -149,7 +159,9 @@ suite "Sales":
test "makes storage available again when other host fills the slot":
let otherHost = Address.example
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
sales.onStore = proc(request: StorageRequest,
slot: UInt256,
availability: Availability) {.async.} =
await sleepAsync(1.hours)
sales.add(availability)
discard await market.requestStorage(request)
@ -158,7 +170,9 @@ suite "Sales":
check sales.available == @[availability]
test "makes storage available again when request expires":
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
sales.onStore = proc(request: StorageRequest,
slot: UInt256,
availability: Availability) {.async.} =
await sleepAsync(1.hours)
sales.add(availability)
discard await market.requestStorage(request)