[sales] Consistent names for callbacks

This commit is contained in:
Mark Spanbroek 2022-07-07 16:18:45 +02:00 committed by markspanbroek
parent 592a3dce4d
commit 571aa6f694
3 changed files with 24 additions and 24 deletions

View File

@ -312,13 +312,13 @@ proc start*(node: CodexNodeRef) {.async.} =
await node.discovery.start()
if contracts =? node.contracts:
contracts.sales.store = proc(cid: string, _: Availability) {.async.} =
contracts.sales.onStore = proc(cid: string, _: Availability) {.async.} =
# store data in local storage
(await node.store(Cid.init(cid).tryGet())).tryGet()
contracts.sales.onClear = proc(availability: Availability, request: StorageRequest) =
# TODO: remove data from local storage
discard
contracts.sales.prove = proc(cid: string): Future[seq[byte]] {.async.} =
contracts.sales.onProve = proc(cid: string): Future[seq[byte]] {.async.} =
# TODO: generate proof
return @[42'u8]
await contracts.start()

View File

@ -15,8 +15,8 @@ type
clock: Clock
subscription: ?Subscription
available*: seq[Availability]
store: ?Store
prove: ?Prove
onStore: ?OnStore
onProve: ?OnProve
onClear: ?OnClear
onSale: ?OnSale
Availability* = object
@ -34,8 +34,8 @@ type
running: ?Future[void]
waiting: ?Future[void]
finished: bool
Store = proc(cid: string, availability: Availability): Future[void] {.gcsafe, upraises: [].}
Prove = proc(cid: string): Future[seq[byte]] {.gcsafe, upraises: [].}
OnStore = proc(cid: string, 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, request: StorageRequest) {.gcsafe, upraises: [].}
@ -53,11 +53,11 @@ proc init*(_: type Availability,
doAssert randomBytes(id) == 32
Availability(id: id, size: size, duration: duration, minPrice: minPrice)
proc `store=`*(sales: Sales, store: Store) =
sales.store = some store
proc `onStore=`*(sales: Sales, onStore: OnStore) =
sales.onStore = some onStore
proc `prove=`*(sales: Sales, prove: Prove) =
sales.prove = some prove
proc `onProve=`*(sales: Sales, onProve: OnProve) =
sales.onProve = some onProve
proc `onClear=`*(sales: Sales, onClear: OnClear) =
sales.onClear = some onClear
@ -129,11 +129,11 @@ proc start(agent: SalesAgent) {.async.} =
let market = sales.market
let availability = agent.availability
without store =? sales.store:
raiseAssert "store proc not set"
without onStore =? sales.onStore:
raiseAssert "onStore callback not set"
without prove =? sales.prove:
raiseAssert "prove proc not set"
without onProve =? sales.onProve:
raiseAssert "onProve callback not set"
sales.remove(availability)
@ -146,8 +146,8 @@ proc start(agent: SalesAgent) {.async.} =
agent.waiting = some agent.waitForExpiry()
await store(request.content.cid, availability)
let proof = await prove(request.content.cid)
await onStore(request.content.cid, availability)
let proof = await onProve(request.content.cid)
await market.fulfillRequest(request.id, proof)
except CancelledError:
raise

View File

@ -32,9 +32,9 @@ suite "Sales":
market = MockMarket.new()
clock = MockClock.new()
sales = Sales.new(market, clock)
sales.store = proc(cid: string, availability: Availability) {.async.} =
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
discard
sales.prove = proc(cid: string): Future[seq[byte]] {.async.} =
sales.onProve = proc(cid: string): Future[seq[byte]] {.async.} =
return proof
await sales.start()
request.expiry = (clock.now() + 42).u256
@ -79,7 +79,7 @@ suite "Sales":
test "retrieves and stores data locally":
var storingCid: string
var storingAvailability: Availability
sales.store = proc(cid: string, availability: Availability) {.async.} =
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
storingCid = cid
storingAvailability = availability
sales.add(availability)
@ -88,7 +88,7 @@ suite "Sales":
test "makes storage available again when data retrieval fails":
let error = newException(IOError, "data retrieval failed")
sales.store = proc(cid: string, availability: Availability) {.async.} =
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
raise error
sales.add(availability)
discard await market.requestStorage(request)
@ -96,7 +96,7 @@ suite "Sales":
test "generates proof of storage":
var provingCid: string
sales.prove = proc(cid: string): Future[seq[byte]] {.async.} = provingCid = cid
sales.onProve = proc(cid: string): Future[seq[byte]] {.async.} = provingCid = cid
sales.add(availability)
discard await market.requestStorage(request)
check provingCid == request.content.cid
@ -121,7 +121,7 @@ suite "Sales":
check soldRequest == request
test "calls onClear when storage becomes available again":
sales.prove = proc(cid: string): Future[seq[byte]] {.async.} =
sales.onProve = proc(cid: string): Future[seq[byte]] {.async.} =
raise newException(IOError, "proof failed")
var clearedAvailability: Availability
var clearedRequest: StorageRequest
@ -135,7 +135,7 @@ suite "Sales":
test "makes storage available again when other host fulfills request":
let otherHost = Address.example
sales.store = proc(cid: string, availability: Availability) {.async.} =
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
await sleepAsync(1.hours)
sales.add(availability)
discard await market.requestStorage(request)
@ -143,7 +143,7 @@ suite "Sales":
check sales.available == @[availability]
test "makes storage available again when request expires":
sales.store = proc(cid: string, availability: Availability) {.async.} =
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
await sleepAsync(1.hours)
sales.add(availability)
discard await market.requestStorage(request)