[sales] Consistent names for callbacks
This commit is contained in:
parent
592a3dce4d
commit
571aa6f694
|
@ -312,13 +312,13 @@ proc start*(node: CodexNodeRef) {.async.} =
|
||||||
await node.discovery.start()
|
await node.discovery.start()
|
||||||
|
|
||||||
if contracts =? node.contracts:
|
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
|
# store data in local storage
|
||||||
(await node.store(Cid.init(cid).tryGet())).tryGet()
|
(await node.store(Cid.init(cid).tryGet())).tryGet()
|
||||||
contracts.sales.onClear = proc(availability: Availability, request: StorageRequest) =
|
contracts.sales.onClear = proc(availability: Availability, request: StorageRequest) =
|
||||||
# TODO: remove data from local storage
|
# TODO: remove data from local storage
|
||||||
discard
|
discard
|
||||||
contracts.sales.prove = proc(cid: string): Future[seq[byte]] {.async.} =
|
contracts.sales.onProve = proc(cid: string): Future[seq[byte]] {.async.} =
|
||||||
# TODO: generate proof
|
# TODO: generate proof
|
||||||
return @[42'u8]
|
return @[42'u8]
|
||||||
await contracts.start()
|
await contracts.start()
|
||||||
|
|
|
@ -15,8 +15,8 @@ type
|
||||||
clock: Clock
|
clock: Clock
|
||||||
subscription: ?Subscription
|
subscription: ?Subscription
|
||||||
available*: seq[Availability]
|
available*: seq[Availability]
|
||||||
store: ?Store
|
onStore: ?OnStore
|
||||||
prove: ?Prove
|
onProve: ?OnProve
|
||||||
onClear: ?OnClear
|
onClear: ?OnClear
|
||||||
onSale: ?OnSale
|
onSale: ?OnSale
|
||||||
Availability* = object
|
Availability* = object
|
||||||
|
@ -34,8 +34,8 @@ type
|
||||||
running: ?Future[void]
|
running: ?Future[void]
|
||||||
waiting: ?Future[void]
|
waiting: ?Future[void]
|
||||||
finished: bool
|
finished: bool
|
||||||
Store = proc(cid: string, availability: Availability): Future[void] {.gcsafe, upraises: [].}
|
OnStore = proc(cid: string, availability: Availability): Future[void] {.gcsafe, upraises: [].}
|
||||||
Prove = 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, request: StorageRequest) {.gcsafe, upraises: [].}
|
OnSale = proc(availability: Availability, request: StorageRequest) {.gcsafe, upraises: [].}
|
||||||
|
|
||||||
|
@ -53,11 +53,11 @@ proc init*(_: type Availability,
|
||||||
doAssert randomBytes(id) == 32
|
doAssert randomBytes(id) == 32
|
||||||
Availability(id: id, size: size, duration: duration, minPrice: minPrice)
|
Availability(id: id, size: size, duration: duration, minPrice: minPrice)
|
||||||
|
|
||||||
proc `store=`*(sales: Sales, store: Store) =
|
proc `onStore=`*(sales: Sales, onStore: OnStore) =
|
||||||
sales.store = some store
|
sales.onStore = some onStore
|
||||||
|
|
||||||
proc `prove=`*(sales: Sales, prove: Prove) =
|
proc `onProve=`*(sales: Sales, onProve: OnProve) =
|
||||||
sales.prove = some prove
|
sales.onProve = some onProve
|
||||||
|
|
||||||
proc `onClear=`*(sales: Sales, onClear: OnClear) =
|
proc `onClear=`*(sales: Sales, onClear: OnClear) =
|
||||||
sales.onClear = some onClear
|
sales.onClear = some onClear
|
||||||
|
@ -129,11 +129,11 @@ proc start(agent: SalesAgent) {.async.} =
|
||||||
let market = sales.market
|
let market = sales.market
|
||||||
let availability = agent.availability
|
let availability = agent.availability
|
||||||
|
|
||||||
without store =? sales.store:
|
without onStore =? sales.onStore:
|
||||||
raiseAssert "store proc not set"
|
raiseAssert "onStore callback not set"
|
||||||
|
|
||||||
without prove =? sales.prove:
|
without onProve =? sales.onProve:
|
||||||
raiseAssert "prove proc not set"
|
raiseAssert "onProve callback not set"
|
||||||
|
|
||||||
sales.remove(availability)
|
sales.remove(availability)
|
||||||
|
|
||||||
|
@ -146,8 +146,8 @@ proc start(agent: SalesAgent) {.async.} =
|
||||||
|
|
||||||
agent.waiting = some agent.waitForExpiry()
|
agent.waiting = some agent.waitForExpiry()
|
||||||
|
|
||||||
await store(request.content.cid, availability)
|
await onStore(request.content.cid, availability)
|
||||||
let proof = await prove(request.content.cid)
|
let proof = await onProve(request.content.cid)
|
||||||
await market.fulfillRequest(request.id, proof)
|
await market.fulfillRequest(request.id, proof)
|
||||||
except CancelledError:
|
except CancelledError:
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -32,9 +32,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.store = proc(cid: string, availability: Availability) {.async.} =
|
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
|
||||||
discard
|
discard
|
||||||
sales.prove = proc(cid: string): Future[seq[byte]] {.async.} =
|
sales.onProve = proc(cid: string): Future[seq[byte]] {.async.} =
|
||||||
return proof
|
return proof
|
||||||
await sales.start()
|
await sales.start()
|
||||||
request.expiry = (clock.now() + 42).u256
|
request.expiry = (clock.now() + 42).u256
|
||||||
|
@ -79,7 +79,7 @@ suite "Sales":
|
||||||
test "retrieves and stores data locally":
|
test "retrieves and stores data locally":
|
||||||
var storingCid: string
|
var storingCid: string
|
||||||
var storingAvailability: Availability
|
var storingAvailability: Availability
|
||||||
sales.store = proc(cid: string, availability: Availability) {.async.} =
|
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
|
||||||
storingCid = cid
|
storingCid = cid
|
||||||
storingAvailability = availability
|
storingAvailability = availability
|
||||||
sales.add(availability)
|
sales.add(availability)
|
||||||
|
@ -88,7 +88,7 @@ suite "Sales":
|
||||||
|
|
||||||
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.store = proc(cid: string, availability: Availability) {.async.} =
|
sales.onStore = proc(cid: string, availability: Availability) {.async.} =
|
||||||
raise error
|
raise error
|
||||||
sales.add(availability)
|
sales.add(availability)
|
||||||
discard await market.requestStorage(request)
|
discard await market.requestStorage(request)
|
||||||
|
@ -96,7 +96,7 @@ suite "Sales":
|
||||||
|
|
||||||
test "generates proof of storage":
|
test "generates proof of storage":
|
||||||
var provingCid: string
|
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)
|
sales.add(availability)
|
||||||
discard await market.requestStorage(request)
|
discard await market.requestStorage(request)
|
||||||
check provingCid == request.content.cid
|
check provingCid == request.content.cid
|
||||||
|
@ -121,7 +121,7 @@ suite "Sales":
|
||||||
check soldRequest == request
|
check soldRequest == request
|
||||||
|
|
||||||
test "calls onClear when storage becomes available again":
|
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")
|
raise newException(IOError, "proof failed")
|
||||||
var clearedAvailability: Availability
|
var clearedAvailability: Availability
|
||||||
var clearedRequest: StorageRequest
|
var clearedRequest: StorageRequest
|
||||||
|
@ -135,7 +135,7 @@ suite "Sales":
|
||||||
|
|
||||||
test "makes storage available again when other host fulfills request":
|
test "makes storage available again when other host fulfills request":
|
||||||
let otherHost = Address.example
|
let otherHost = Address.example
|
||||||
sales.store = proc(cid: string, availability: Availability) {.async.} =
|
sales.onStore = proc(cid: string, 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)
|
||||||
|
@ -143,7 +143,7 @@ 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.store = proc(cid: string, availability: Availability) {.async.} =
|
sales.onStore = proc(cid: string, 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