[WIP sales] Generate proof of storage

This commit is contained in:
Mark Spanbroek 2022-07-05 10:24:33 +02:00 committed by markspanbroek
parent 07dbb6ae18
commit dcb8f8a7f7
2 changed files with 17 additions and 0 deletions

View File

@ -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()

View File

@ -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