From dcb8f8a7f757073b674e3612c7d2d09792ca381a Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 5 Jul 2022 10:24:33 +0200 Subject: [PATCH] [WIP sales] Generate proof of storage --- codex/sales.nim | 9 +++++++++ tests/codex/testsales.nim | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/codex/sales.nim b/codex/sales.nim index 7f14ac24..dc1ecb55 100644 --- a/codex/sales.nim +++ b/codex/sales.nim @@ -19,6 +19,7 @@ type available*: seq[Availability] offerExpiryInterval*: UInt256 retrieve: ?Retrieve + prove: ?Prove onSale: ?OnSale Availability* = object id*: array[32, byte] @@ -35,6 +36,7 @@ type waiting: ?Future[void] finished: bool Retrieve = proc(cid: string): Future[void] {.gcsafe, upraises: [].} + Prove = proc(cid: string): Future[seq[byte]] {.gcsafe, upraises: [].} OnSale = proc(offer: StorageOffer) {.gcsafe, upraises: [].} func new*(_: type Sales, market: Market, clock: Clock): Sales = @@ -55,6 +57,9 @@ proc init*(_: type Availability, proc `retrieve=`*(sales: Sales, retrieve: Retrieve) = sales.retrieve = some retrieve +proc `prove=`*(sales: Sales, prove: Prove) = + sales.prove = some prove + proc `onSale=`*(sales: Sales, callback: OnSale) = sales.onSale = some callback @@ -130,6 +135,9 @@ proc start(negotiation: Negotiation) {.async.} = without retrieve =? sales.retrieve: raiseAssert "retrieve proc not set" + without prove =? sales.prove: + raiseAssert "prove proc not set" + try: sales.remove(availability) @@ -138,6 +146,7 @@ proc start(negotiation: Negotiation) {.async.} = return await retrieve(request.content.cid) + let proof = await prove(request.content.cid) await negotiation.sendOffer() await negotiation.subscribeSelect() diff --git a/tests/codex/testsales.nim b/tests/codex/testsales.nim index 1e36ae95..d09743f7 100644 --- a/tests/codex/testsales.nim +++ b/tests/codex/testsales.nim @@ -33,6 +33,7 @@ suite "Sales": clock = MockClock.new() sales = Sales.new(market, clock) sales.retrieve = proc(_: string) {.async.} = discard + sales.prove = proc(_: string): Future[seq[byte]] {.async.} = discard await sales.start() teardown: @@ -81,6 +82,13 @@ suite "Sales": discard await market.requestStorage(request) check sales.available.len == 0 + test "generates proof of storage": + var provingCid: string + sales.prove = proc(cid: string): Future[seq[byte]] {.async.} = provingCid = cid + sales.add(availability) + discard await market.requestStorage(request) + check provingCid == request.content.cid + # test "sets expiry time of offer": # sales.add(availability) # let now = clock.now().u256