From 571aa6f694026c95d7cebb565f3d72f78cc940f1 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 7 Jul 2022 16:18:45 +0200 Subject: [PATCH] [sales] Consistent names for callbacks --- codex/node.nim | 4 ++-- codex/sales.nim | 28 ++++++++++++++-------------- tests/codex/testsales.nim | 16 ++++++++-------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/codex/node.nim b/codex/node.nim index c89d0dad..bc23fe7f 100644 --- a/codex/node.nim +++ b/codex/node.nim @@ -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() diff --git a/codex/sales.nim b/codex/sales.nim index 39fa6ea4..2a9e51c3 100644 --- a/codex/sales.nim +++ b/codex/sales.nim @@ -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 diff --git a/tests/codex/testsales.nim b/tests/codex/testsales.nim index a1fd1f4c..b38248e4 100644 --- a/tests/codex/testsales.nim +++ b/tests/codex/testsales.nim @@ -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)