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

View File

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