nim-dagger/codex/sales.nim

182 lines
5.3 KiB
Nim
Raw Normal View History

2022-03-30 10:51:28 +00:00
import std/sequtils
import pkg/questionable
import pkg/upraises
import pkg/stint
import pkg/nimcrypto
import pkg/chronicles
2022-03-30 10:51:28 +00:00
import ./market
2022-05-17 15:02:03 +00:00
import ./clock
2022-03-30 10:51:28 +00:00
export stint
type
Sales* = ref object
market: Market
2022-05-17 15:02:03 +00:00
clock: Clock
2022-03-30 10:51:28 +00:00
subscription: ?Subscription
available*: seq[Availability]
2022-07-05 07:39:59 +00:00
retrieve: ?Retrieve
2022-07-05 08:24:33 +00:00
prove: ?Prove
2022-03-31 09:41:45 +00:00
onSale: ?OnSale
2022-03-30 10:51:28 +00:00
Availability* = object
id*: array[32, byte]
size*: UInt256
duration*: UInt256
2022-03-30 10:51:28 +00:00
minPrice*: UInt256
2022-03-31 12:35:53 +00:00
Negotiation = ref object
sales: Sales
requestId: array[32, byte]
ask: StorageAsk
2022-03-31 12:35:53 +00:00
availability: Availability
2022-07-05 08:51:01 +00:00
request: ?StorageRequest
2022-03-31 12:35:53 +00:00
subscription: ?Subscription
running: ?Future[void]
2022-03-31 12:35:53 +00:00
waiting: ?Future[void]
finished: bool
2022-07-05 07:39:59 +00:00
Retrieve = proc(cid: string): Future[void] {.gcsafe, upraises: [].}
2022-07-05 08:24:33 +00:00
Prove = proc(cid: string): Future[seq[byte]] {.gcsafe, upraises: [].}
2022-07-05 08:51:01 +00:00
OnSale = proc(availability: Availability, request: StorageRequest) {.gcsafe, upraises: [].}
2022-03-30 10:51:28 +00:00
2022-05-17 15:02:03 +00:00
func new*(_: type Sales, market: Market, clock: Clock): Sales =
Sales(
market: market,
clock: clock,
)
2022-03-30 10:51:28 +00:00
proc init*(_: type Availability,
size: UInt256,
duration: UInt256,
2022-03-30 10:51:28 +00:00
minPrice: UInt256): Availability =
var id: array[32, byte]
doAssert randomBytes(id) == 32
Availability(id: id, size: size, duration: duration, minPrice: minPrice)
2022-07-05 07:39:59 +00:00
proc `retrieve=`*(sales: Sales, retrieve: Retrieve) =
sales.retrieve = some retrieve
2022-07-05 08:24:33 +00:00
proc `prove=`*(sales: Sales, prove: Prove) =
sales.prove = some prove
2022-03-31 09:41:45 +00:00
proc `onSale=`*(sales: Sales, callback: OnSale) =
sales.onSale = some callback
2022-03-30 10:51:28 +00:00
func add*(sales: Sales, availability: Availability) =
sales.available.add(availability)
func remove*(sales: Sales, availability: Availability) =
sales.available.keepItIf(it != availability)
func findAvailability(sales: Sales, ask: StorageAsk): ?Availability =
2022-03-30 10:51:28 +00:00
for availability in sales.available:
if ask.size <= availability.size and
ask.duration <= availability.duration and
ask.maxPrice >= availability.minPrice:
2022-03-30 10:51:28 +00:00
return some availability
proc finish(negotiation: Negotiation, success: bool) =
if negotiation.finished:
return
negotiation.finished = true
2022-03-31 12:35:53 +00:00
if subscription =? negotiation.subscription:
asyncSpawn subscription.unsubscribe()
if running =? negotiation.running:
running.cancel()
if waiting =? negotiation.waiting:
waiting.cancel()
2022-07-05 08:51:01 +00:00
if success and request =? negotiation.request:
2022-03-31 12:35:53 +00:00
if onSale =? negotiation.sales.onSale:
2022-07-05 08:51:01 +00:00
onSale(negotiation.availability, request)
2022-03-31 12:35:53 +00:00
else:
negotiation.sales.add(negotiation.availability)
proc onFulfill(negotiation: Negotiation, requestId: array[32, byte]) {.async.} =
try:
let market = negotiation.sales.market
let host = await market.getHost(requestId)
let me = await market.getSigner()
negotiation.finish(success = (host == me.some))
except CatchableError:
negotiation.finish(success = false)
proc subscribeFulfill(negotiation: Negotiation) {.async.} =
proc onFulfill(requestId: array[32, byte]) {.gcsafe, upraises:[].} =
asyncSpawn negotiation.onFulfill(requestId)
2022-03-31 12:35:53 +00:00
let market = negotiation.sales.market
let subscription = await market.subscribeFulfillment(negotiation.requestId, onFulfill)
negotiation.subscription = some subscription
2022-03-31 12:35:53 +00:00
proc waitForExpiry(negotiation: Negotiation) {.async.} =
2022-07-05 13:04:25 +00:00
without request =? negotiation.request:
2022-03-31 12:35:53 +00:00
return
2022-07-05 13:04:25 +00:00
await negotiation.sales.clock.waitUntil(request.expiry.truncate(int64))
negotiation.finish(success = false)
2022-03-31 12:35:53 +00:00
proc start(negotiation: Negotiation) {.async.} =
try:
let sales = negotiation.sales
let market = sales.market
let availability = negotiation.availability
2022-07-05 07:39:59 +00:00
without retrieve =? sales.retrieve:
raiseAssert "retrieve proc not set"
2022-07-05 07:39:59 +00:00
without prove =? sales.prove:
raiseAssert "prove proc not set"
2022-07-05 08:24:33 +00:00
sales.remove(availability)
2022-07-05 07:39:59 +00:00
await negotiation.subscribeFulfill()
2022-07-05 08:51:01 +00:00
negotiation.request = await market.getRequest(negotiation.requestId)
without request =? negotiation.request:
2022-07-05 07:39:59 +00:00
negotiation.finish(success = false)
return
2022-07-05 13:04:25 +00:00
negotiation.waiting = some negotiation.waitForExpiry()
2022-07-05 07:39:59 +00:00
await retrieve(request.content.cid)
2022-07-05 08:24:33 +00:00
let proof = await prove(request.content.cid)
2022-07-05 08:37:55 +00:00
await market.fulfillRequest(request.id, proof)
except CancelledError:
raise
except CatchableError as e:
error "Negotiation failed", msg = e.msg
negotiation.finish(success = false)
proc handleRequest(sales: Sales, requestId: array[32, byte], ask: StorageAsk) =
without availability =? sales.findAvailability(ask):
2022-03-31 12:35:53 +00:00
return
2022-03-31 12:35:53 +00:00
let negotiation = Negotiation(
sales: sales,
requestId: requestId,
ask: ask,
2022-03-31 12:35:53 +00:00
availability: availability
)
negotiation.running = some negotiation.start()
2022-03-30 10:51:28 +00:00
proc start*(sales: Sales) {.async.} =
2022-03-30 10:51:28 +00:00
doAssert sales.subscription.isNone, "Sales already started"
proc onRequest(requestId: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].} =
sales.handleRequest(requestId, ask)
2022-03-30 10:51:28 +00:00
try:
sales.subscription = some await sales.market.subscribeRequests(onRequest)
except CatchableError as e:
error "Unable to start sales", msg = e.msg
2022-03-30 10:51:28 +00:00
proc stop*(sales: Sales) {.async.} =
2022-03-30 10:51:28 +00:00
if subscription =? sales.subscription:
sales.subscription = Subscription.none
try:
await subscription.unsubscribe()
except CatchableError as e:
warn "Unsubscribe failed", msg = e.msg