[WIP sales] Retrieve data

This commit is contained in:
Mark Spanbroek 2022-07-05 09:39:59 +02:00 committed by markspanbroek
parent 3e6d51754d
commit 07dbb6ae18
2 changed files with 44 additions and 18 deletions

View File

@ -18,6 +18,7 @@ type
subscription: ?Subscription subscription: ?Subscription
available*: seq[Availability] available*: seq[Availability]
offerExpiryInterval*: UInt256 offerExpiryInterval*: UInt256
retrieve: ?Retrieve
onSale: ?OnSale onSale: ?OnSale
Availability* = object Availability* = object
id*: array[32, byte] id*: array[32, byte]
@ -33,6 +34,7 @@ type
subscription: ?Subscription subscription: ?Subscription
waiting: ?Future[void] waiting: ?Future[void]
finished: bool finished: bool
Retrieve = proc(cid: string): Future[void] {.gcsafe, upraises: [].}
OnSale = proc(offer: StorageOffer) {.gcsafe, upraises: [].} OnSale = proc(offer: StorageOffer) {.gcsafe, upraises: [].}
func new*(_: type Sales, market: Market, clock: Clock): Sales = func new*(_: type Sales, market: Market, clock: Clock): Sales =
@ -50,6 +52,9 @@ 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 `retrieve=`*(sales: Sales, retrieve: Retrieve) =
sales.retrieve = some retrieve
proc `onSale=`*(sales: Sales, callback: OnSale) = proc `onSale=`*(sales: Sales, callback: OnSale) =
sales.onSale = some callback sales.onSale = some callback
@ -118,10 +123,22 @@ proc waitForExpiry(negotiation: Negotiation) {.async.} =
negotiation.finish(success = false) negotiation.finish(success = false)
proc start(negotiation: Negotiation) {.async.} = proc start(negotiation: Negotiation) {.async.} =
let sales = negotiation.sales
let market = sales.market
let availability = negotiation.availability
without retrieve =? sales.retrieve:
raiseAssert "retrieve proc not set"
try: try:
let sales = negotiation.sales
let availability = negotiation.availability
sales.remove(availability) sales.remove(availability)
without request =? await market.getRequest(negotiation.requestId):
negotiation.finish(success = false)
return
await retrieve(request.content.cid)
await negotiation.sendOffer() await negotiation.sendOffer()
await negotiation.subscribeSelect() await negotiation.subscribeSelect()
negotiation.waiting = some negotiation.waitForExpiry() negotiation.waiting = some negotiation.waitForExpiry()

View File

@ -13,11 +13,16 @@ suite "Sales":
duration=60.u256, duration=60.u256,
minPrice=42.u256 minPrice=42.u256
) )
let request = StorageRequest(ask: StorageAsk( let request = StorageRequest(
duration: 60.u256, ask: StorageAsk(
size: 100.u256, duration: 60.u256,
maxPrice:42.u256 size: 100.u256,
)) maxPrice:42.u256
),
content: StorageContent(
cid: "some cid"
)
)
var sales: Sales var sales: Sales
var market: MockMarket var market: MockMarket
@ -27,6 +32,7 @@ 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.retrieve = proc(_: string) {.async.} = discard
await sales.start() await sales.start()
teardown: teardown:
@ -54,18 +60,21 @@ suite "Sales":
let availability2 = Availability.init(1.u256, 2.u256, 3.u256) let availability2 = Availability.init(1.u256, 2.u256, 3.u256)
check availability1.id != availability2.id check availability1.id != availability2.id
# test "offers available storage when matching request comes in": test "retrieves data when matching request comes in":
# sales.add(availability) var retrievingCid: string
# discard await market.requestStorage(request) sales.retrieve = proc(cid: string) {.async.} = retrievingCid = cid
# check market.offered.len == 1 sales.add(availability)
# check market.offered[0].price == 42.u256 discard await market.requestStorage(request)
check retrievingCid == request.content.cid
# test "ignores request when no matching storage is available": test "ignores request when no matching storage is available":
# sales.add(availability) var retrieveCalled = false
# var tooBig = request sales.retrieve = proc(cid: string) {.async.} = retrieveCalled = true
# tooBig.ask.size = request.ask.size + 1 sales.add(availability)
# discard await market.requestStorage(tooBig) var tooBig = request
# check market.offered.len == 0 tooBig.ask.size = request.ask.size + 1
discard await market.requestStorage(tooBig)
check not retrieveCalled
test "makes storage unavailable when offer is submitted": test "makes storage unavailable when offer is submitted":
sales.add(availability) sales.add(availability)