[WIP sales] call onSale()
This commit is contained in:
parent
a2570f9380
commit
7f864570bd
|
@ -31,13 +31,14 @@ type
|
||||||
requestId: array[32, byte]
|
requestId: array[32, byte]
|
||||||
ask: StorageAsk
|
ask: StorageAsk
|
||||||
availability: Availability
|
availability: Availability
|
||||||
|
request: ?StorageRequest
|
||||||
offer: ?StorageOffer
|
offer: ?StorageOffer
|
||||||
subscription: ?Subscription
|
subscription: ?Subscription
|
||||||
waiting: ?Future[void]
|
waiting: ?Future[void]
|
||||||
finished: bool
|
finished: bool
|
||||||
Retrieve = proc(cid: string): Future[void] {.gcsafe, upraises: [].}
|
Retrieve = proc(cid: string): Future[void] {.gcsafe, upraises: [].}
|
||||||
Prove = proc(cid: string): Future[seq[byte]] {.gcsafe, upraises: [].}
|
Prove = proc(cid: string): Future[seq[byte]] {.gcsafe, upraises: [].}
|
||||||
OnSale = proc(offer: StorageOffer) {.gcsafe, upraises: [].}
|
OnSale = proc(availability: Availability, request: StorageRequest) {.gcsafe, upraises: [].}
|
||||||
|
|
||||||
func new*(_: type Sales, market: Market, clock: Clock): Sales =
|
func new*(_: type Sales, market: Market, clock: Clock): Sales =
|
||||||
Sales(
|
Sales(
|
||||||
|
@ -100,9 +101,9 @@ proc finish(negotiation: Negotiation, success: bool) =
|
||||||
if waiting =? negotiation.waiting:
|
if waiting =? negotiation.waiting:
|
||||||
waiting.cancel()
|
waiting.cancel()
|
||||||
|
|
||||||
if success and offer =? negotiation.offer:
|
if success and request =? negotiation.request:
|
||||||
if onSale =? negotiation.sales.onSale:
|
if onSale =? negotiation.sales.onSale:
|
||||||
onSale(offer)
|
onSale(negotiation.availability, request)
|
||||||
else:
|
else:
|
||||||
negotiation.sales.add(negotiation.availability)
|
negotiation.sales.add(negotiation.availability)
|
||||||
|
|
||||||
|
@ -141,13 +142,15 @@ proc start(negotiation: Negotiation) {.async.} =
|
||||||
try:
|
try:
|
||||||
sales.remove(availability)
|
sales.remove(availability)
|
||||||
|
|
||||||
without request =? await market.getRequest(negotiation.requestId):
|
negotiation.request = await market.getRequest(negotiation.requestId)
|
||||||
|
without request =? negotiation.request:
|
||||||
negotiation.finish(success = false)
|
negotiation.finish(success = false)
|
||||||
return
|
return
|
||||||
|
|
||||||
await retrieve(request.content.cid)
|
await retrieve(request.content.cid)
|
||||||
let proof = await prove(request.content.cid)
|
let proof = await prove(request.content.cid)
|
||||||
await market.fulfillRequest(request.id, proof)
|
await market.fulfillRequest(request.id, proof)
|
||||||
|
negotiation.finish(success = true)
|
||||||
|
|
||||||
await negotiation.sendOffer()
|
await negotiation.sendOffer()
|
||||||
await negotiation.subscribeSelect()
|
await negotiation.subscribeSelect()
|
||||||
|
|
|
@ -103,15 +103,16 @@ suite "Sales":
|
||||||
check market.fulfilled[0].proof == proof
|
check market.fulfilled[0].proof == proof
|
||||||
check market.fulfilled[0].host == await market.getSigner()
|
check market.fulfilled[0].host == await market.getSigner()
|
||||||
|
|
||||||
# test "calls onSale when offer is selected":
|
test "calls onSale when request is fulfilled":
|
||||||
# var sold: StorageOffer
|
var soldAvailability: Availability
|
||||||
# sales.onSale = proc(offer: StorageOffer) =
|
var soldRequest: StorageRequest
|
||||||
# sold = offer
|
sales.onSale = proc(availability: Availability, request: StorageRequest) =
|
||||||
# sales.add(availability)
|
soldAvailability = availability
|
||||||
# discard await market.requestStorage(request)
|
soldRequest = request
|
||||||
# let offer = market.offered[0]
|
sales.add(availability)
|
||||||
# await market.selectOffer(offer.id)
|
discard await market.requestStorage(request)
|
||||||
# check sold == offer
|
check soldAvailability == availability
|
||||||
|
check soldRequest == request
|
||||||
|
|
||||||
# test "does not call onSale when a different offer is selected":
|
# test "does not call onSale when a different offer is selected":
|
||||||
# var didSell: bool
|
# var didSell: bool
|
||||||
|
|
Loading…
Reference in New Issue