[sales] Pass along request and slot index in onStore callback
This commit is contained in:
parent
3a3d083345
commit
cb5427ca95
|
@ -313,11 +313,13 @@ proc start*(node: CodexNodeRef) {.async.} =
|
||||||
|
|
||||||
if contracts =? node.contracts:
|
if contracts =? node.contracts:
|
||||||
# TODO: remove Sales callbacks, pass BlockStore and StorageProofs instead
|
# 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
|
## store data in local storage
|
||||||
##
|
##
|
||||||
|
|
||||||
without cid =? Cid.init(cid):
|
without cid =? Cid.init(request.content.cid):
|
||||||
trace "Unable to parse Cid", cid
|
trace "Unable to parse Cid", cid
|
||||||
raise newException(CodexError, "Unable to parse Cid")
|
raise newException(CodexError, "Unable to parse Cid")
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,9 @@ type
|
||||||
running: ?Future[void]
|
running: ?Future[void]
|
||||||
waiting: ?Future[void]
|
waiting: ?Future[void]
|
||||||
finished: bool
|
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: [].}
|
OnProve = proc(cid: string): Future[seq[byte]] {.gcsafe, upraises: [].}
|
||||||
OnClear = proc(availability: Availability, request: StorageRequest) {.gcsafe, upraises: [].}
|
OnClear = proc(availability: Availability, request: StorageRequest) {.gcsafe, upraises: [].}
|
||||||
OnSale = proc(availability: Availability,
|
OnSale = proc(availability: Availability,
|
||||||
|
@ -184,7 +186,7 @@ proc start(agent: SalesAgent) {.async.} =
|
||||||
|
|
||||||
agent.waiting = some agent.waitForExpiry()
|
agent.waiting = some agent.waitForExpiry()
|
||||||
|
|
||||||
await onStore(request.content.cid, availability)
|
await onStore(request, slotIndex, availability)
|
||||||
let proof = await onProve(request.content.cid)
|
let proof = await onProve(request.content.cid)
|
||||||
await market.fillSlot(request.id, slotIndex, proof)
|
await market.fillSlot(request.id, slotIndex, proof)
|
||||||
except CancelledError:
|
except CancelledError:
|
||||||
|
|
|
@ -33,7 +33,9 @@ suite "Sales":
|
||||||
market = MockMarket.new()
|
market = MockMarket.new()
|
||||||
clock = MockClock.new()
|
clock = MockClock.new()
|
||||||
sales = Sales.new(market, clock)
|
sales = Sales.new(market, clock)
|
||||||
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
|
sales.onStore = proc(request: StorageRequest,
|
||||||
|
slot: UInt256,
|
||||||
|
availability: Availability) {.async.} =
|
||||||
discard
|
discard
|
||||||
sales.onProve = proc(cid: string): Future[seq[byte]] {.async.} =
|
sales.onProve = proc(cid: string): Future[seq[byte]] {.async.} =
|
||||||
return proof
|
return proof
|
||||||
|
@ -85,18 +87,26 @@ suite "Sales":
|
||||||
check sales.available == @[availability]
|
check sales.available == @[availability]
|
||||||
|
|
||||||
test "retrieves and stores data locally":
|
test "retrieves and stores data locally":
|
||||||
var storingCid: string
|
var storingRequest: StorageRequest
|
||||||
|
var storingSlot: UInt256
|
||||||
var storingAvailability: Availability
|
var storingAvailability: Availability
|
||||||
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
|
sales.onStore = proc(request: StorageRequest,
|
||||||
storingCid = cid
|
slot: UInt256,
|
||||||
|
availability: Availability) {.async.} =
|
||||||
|
storingRequest = request
|
||||||
|
storingSlot = slot
|
||||||
storingAvailability = availability
|
storingAvailability = availability
|
||||||
sales.add(availability)
|
sales.add(availability)
|
||||||
discard await market.requestStorage(request)
|
let requested = await market.requestStorage(request)
|
||||||
check storingCid == request.content.cid
|
check storingRequest == requested
|
||||||
|
check storingSlot < request.ask.slots.u256
|
||||||
|
check storingAvailability == availability
|
||||||
|
|
||||||
test "makes storage available again when data retrieval fails":
|
test "makes storage available again when data retrieval fails":
|
||||||
let error = newException(IOError, "data retrieval failed")
|
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
|
raise error
|
||||||
sales.add(availability)
|
sales.add(availability)
|
||||||
discard await market.requestStorage(request)
|
discard await market.requestStorage(request)
|
||||||
|
@ -149,7 +159,9 @@ suite "Sales":
|
||||||
|
|
||||||
test "makes storage available again when other host fills the slot":
|
test "makes storage available again when other host fills the slot":
|
||||||
let otherHost = Address.example
|
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)
|
await sleepAsync(1.hours)
|
||||||
sales.add(availability)
|
sales.add(availability)
|
||||||
discard await market.requestStorage(request)
|
discard await market.requestStorage(request)
|
||||||
|
@ -158,7 +170,9 @@ suite "Sales":
|
||||||
check sales.available == @[availability]
|
check sales.available == @[availability]
|
||||||
|
|
||||||
test "makes storage available again when request expires":
|
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)
|
await sleepAsync(1.hours)
|
||||||
sales.add(availability)
|
sales.add(availability)
|
||||||
discard await market.requestStorage(request)
|
discard await market.requestStorage(request)
|
||||||
|
|
Loading…
Reference in New Issue