2022-03-30 10:51:28 +00:00
|
|
|
import std/sequtils
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/upraises
|
|
|
|
import pkg/stint
|
|
|
|
import pkg/nimcrypto
|
2022-04-20 10:39:40 +00:00
|
|
|
import pkg/chronicles
|
2022-08-01 12:12:05 +00:00
|
|
|
import ./rng
|
2022-03-30 10:51:28 +00:00
|
|
|
import ./market
|
2022-05-17 15:02:03 +00:00
|
|
|
import ./clock
|
2022-08-17 04:02:53 +00:00
|
|
|
import ./proving
|
|
|
|
import ./contracts/requests
|
2022-03-30 10:51:28 +00:00
|
|
|
|
2022-07-07 14:36:48 +00:00
|
|
|
## Sales holds a list of available storage that it may sell.
|
|
|
|
##
|
|
|
|
## When storage is requested on the market that matches availability, the Sales
|
|
|
|
## object will instruct the Codex node to persist the requested data. Once the
|
|
|
|
## data has been persisted, it uploads a proof of storage to the market in an
|
|
|
|
## attempt to win a storage contract.
|
|
|
|
##
|
|
|
|
## Node Sales Market
|
|
|
|
## | | |
|
|
|
|
## | -- add availability --> | |
|
|
|
|
## | | <-- storage request --- |
|
|
|
|
## | <----- store data ------ | |
|
|
|
|
## | -----------------------> | |
|
|
|
|
## | | |
|
|
|
|
## | <----- prove data ---- | |
|
|
|
|
## | -----------------------> | |
|
|
|
|
## | | ---- storage proof ---> |
|
|
|
|
|
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-08-17 04:02:53 +00:00
|
|
|
subscription: ?market.Subscription
|
2022-03-31 08:24:06 +00:00
|
|
|
available*: seq[Availability]
|
2022-07-07 14:18:45 +00:00
|
|
|
onStore: ?OnStore
|
|
|
|
onProve: ?OnProve
|
2022-07-07 14:14:19 +00:00
|
|
|
onClear: ?OnClear
|
2022-03-31 09:41:45 +00:00
|
|
|
onSale: ?OnSale
|
2022-08-17 04:02:53 +00:00
|
|
|
proving: Proving
|
2022-03-30 10:51:28 +00:00
|
|
|
Availability* = object
|
|
|
|
id*: array[32, byte]
|
2022-05-09 14:51:08 +00:00
|
|
|
size*: UInt256
|
|
|
|
duration*: UInt256
|
2022-03-30 10:51:28 +00:00
|
|
|
minPrice*: UInt256
|
2022-07-07 07:53:31 +00:00
|
|
|
SalesAgent = ref object
|
2022-03-31 12:35:53 +00:00
|
|
|
sales: Sales
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId
|
2022-04-11 18:03:55 +00:00
|
|
|
ask: StorageAsk
|
2022-03-31 12:35:53 +00:00
|
|
|
availability: Availability
|
2022-07-05 08:51:01 +00:00
|
|
|
request: ?StorageRequest
|
2022-08-01 12:12:05 +00:00
|
|
|
slotIndex: ?UInt256
|
2022-08-17 04:02:53 +00:00
|
|
|
subscription: ?market.Subscription
|
2022-07-05 12:19:01 +00:00
|
|
|
running: ?Future[void]
|
2022-03-31 12:35:53 +00:00
|
|
|
waiting: ?Future[void]
|
2022-03-31 13:08:26 +00:00
|
|
|
finished: bool
|
2022-08-02 15:23:12 +00:00
|
|
|
OnStore = proc(request: StorageRequest,
|
|
|
|
slot: UInt256,
|
|
|
|
availability: Availability): Future[void] {.gcsafe, upraises: [].}
|
2022-08-02 15:33:32 +00:00
|
|
|
OnProve = proc(request: StorageRequest,
|
|
|
|
slot: UInt256): Future[seq[byte]] {.gcsafe, upraises: [].}
|
2022-08-17 04:02:53 +00:00
|
|
|
OnClear = proc(availability: Availability,
|
|
|
|
request: StorageRequest,
|
|
|
|
slotIndex: UInt256) {.gcsafe, upraises: [].}
|
2022-07-21 12:32:38 +00:00
|
|
|
OnSale = proc(availability: Availability,
|
|
|
|
request: StorageRequest,
|
|
|
|
slotIndex: UInt256) {.gcsafe, upraises: [].}
|
2022-03-30 10:51:28 +00:00
|
|
|
|
2022-08-17 04:02:53 +00:00
|
|
|
func new*(_: type Sales,
|
|
|
|
market: Market,
|
|
|
|
clock: Clock,
|
|
|
|
proving: Proving): Sales =
|
2022-05-17 15:02:03 +00:00
|
|
|
Sales(
|
|
|
|
market: market,
|
|
|
|
clock: clock,
|
2022-08-17 04:02:53 +00:00
|
|
|
proving: proving
|
2022-05-17 15:02:03 +00:00
|
|
|
)
|
2022-03-30 10:51:28 +00:00
|
|
|
|
2022-03-30 15:07:03 +00:00
|
|
|
proc init*(_: type Availability,
|
2022-05-09 14:51:08 +00:00
|
|
|
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-07 14:18:45 +00:00
|
|
|
proc `onStore=`*(sales: Sales, onStore: OnStore) =
|
|
|
|
sales.onStore = some onStore
|
2022-07-05 07:39:59 +00:00
|
|
|
|
2022-07-07 14:18:45 +00:00
|
|
|
proc `onProve=`*(sales: Sales, onProve: OnProve) =
|
|
|
|
sales.onProve = some onProve
|
2022-07-05 08:24:33 +00:00
|
|
|
|
2022-07-07 14:14:19 +00:00
|
|
|
proc `onClear=`*(sales: Sales, onClear: OnClear) =
|
|
|
|
sales.onClear = some onClear
|
|
|
|
|
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)
|
|
|
|
|
2022-04-11 18:03:55 +00:00
|
|
|
func findAvailability(sales: Sales, ask: StorageAsk): ?Availability =
|
2022-03-30 10:51:28 +00:00
|
|
|
for availability in sales.available:
|
2022-08-02 12:21:12 +00:00
|
|
|
if ask.slotSize <= availability.size and
|
2022-05-09 14:51:08 +00:00
|
|
|
ask.duration <= availability.duration and
|
2022-08-01 12:25:32 +00:00
|
|
|
ask.pricePerSlot >= availability.minPrice:
|
2022-03-30 10:51:28 +00:00
|
|
|
return some availability
|
|
|
|
|
2022-07-07 07:53:31 +00:00
|
|
|
proc finish(agent: SalesAgent, success: bool) =
|
|
|
|
if agent.finished:
|
2022-03-31 13:08:26 +00:00
|
|
|
return
|
|
|
|
|
2022-07-07 07:53:31 +00:00
|
|
|
agent.finished = true
|
2022-03-31 13:08:26 +00:00
|
|
|
|
2022-07-07 07:53:31 +00:00
|
|
|
if subscription =? agent.subscription:
|
2022-03-31 12:35:53 +00:00
|
|
|
asyncSpawn subscription.unsubscribe()
|
2022-03-31 13:08:26 +00:00
|
|
|
|
2022-07-07 07:53:31 +00:00
|
|
|
if running =? agent.running:
|
2022-07-05 12:19:01 +00:00
|
|
|
running.cancel()
|
|
|
|
|
2022-07-07 07:53:31 +00:00
|
|
|
if waiting =? agent.waiting:
|
2022-03-31 13:08:26 +00:00
|
|
|
waiting.cancel()
|
|
|
|
|
2022-07-07 14:14:19 +00:00
|
|
|
if success:
|
2022-08-17 04:02:53 +00:00
|
|
|
if request =? agent.request and
|
2022-08-01 12:12:05 +00:00
|
|
|
slotIndex =? agent.slotIndex:
|
2022-08-17 04:02:53 +00:00
|
|
|
agent.sales.proving.add(request.slotId(slotIndex))
|
|
|
|
|
|
|
|
if onSale =? agent.sales.onSale:
|
|
|
|
onSale(agent.availability, request, slotIndex)
|
2022-03-31 12:35:53 +00:00
|
|
|
else:
|
2022-08-17 04:02:53 +00:00
|
|
|
if onClear =? agent.sales.onClear and
|
|
|
|
request =? agent.request and
|
|
|
|
slotIndex =? agent.slotIndex:
|
|
|
|
onClear(agent.availability, request, slotIndex)
|
2022-07-07 07:53:31 +00:00
|
|
|
agent.sales.add(agent.availability)
|
2022-03-31 12:35:53 +00:00
|
|
|
|
2022-08-01 12:12:05 +00:00
|
|
|
proc selectSlot(agent: SalesAgent) =
|
|
|
|
let rng = Rng.instance
|
|
|
|
let slotIndex = rng.rand(agent.ask.slots - 1)
|
|
|
|
agent.slotIndex = some slotIndex.u256
|
|
|
|
|
2022-07-21 12:32:38 +00:00
|
|
|
proc onSlotFilled(agent: SalesAgent,
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId,
|
2022-07-21 12:32:38 +00:00
|
|
|
slotIndex: UInt256) {.async.} =
|
2022-07-05 12:19:01 +00:00
|
|
|
try:
|
2022-07-07 07:53:31 +00:00
|
|
|
let market = agent.sales.market
|
2022-07-21 12:32:38 +00:00
|
|
|
let host = await market.getHost(requestId, slotIndex)
|
2022-07-05 12:19:01 +00:00
|
|
|
let me = await market.getSigner()
|
2022-07-07 07:53:31 +00:00
|
|
|
agent.finish(success = (host == me.some))
|
2022-07-05 12:19:01 +00:00
|
|
|
except CatchableError:
|
2022-07-07 07:53:31 +00:00
|
|
|
agent.finish(success = false)
|
2022-03-31 13:08:26 +00:00
|
|
|
|
2022-08-01 12:12:05 +00:00
|
|
|
proc subscribeSlotFilled(agent: SalesAgent, slotIndex: UInt256) {.async.} =
|
2022-08-17 02:29:44 +00:00
|
|
|
proc onSlotFilled(requestId: RequestId,
|
2022-07-21 12:32:38 +00:00
|
|
|
slotIndex: UInt256) {.gcsafe, upraises:[].} =
|
|
|
|
asyncSpawn agent.onSlotFilled(requestId, slotIndex)
|
2022-07-07 07:53:31 +00:00
|
|
|
let market = agent.sales.market
|
2022-07-21 12:32:38 +00:00
|
|
|
let subscription = await market.subscribeSlotFilled(agent.requestId,
|
2022-08-01 12:12:05 +00:00
|
|
|
slotIndex,
|
|
|
|
onSlotFilled)
|
2022-07-07 07:53:31 +00:00
|
|
|
agent.subscription = some subscription
|
2022-03-31 08:24:06 +00:00
|
|
|
|
2022-07-07 07:53:31 +00:00
|
|
|
proc waitForExpiry(agent: SalesAgent) {.async.} =
|
|
|
|
without request =? agent.request:
|
2022-03-31 12:35:53 +00:00
|
|
|
return
|
2022-07-07 07:53:31 +00:00
|
|
|
await agent.sales.clock.waitUntil(request.expiry.truncate(int64))
|
|
|
|
agent.finish(success = false)
|
2022-03-31 12:35:53 +00:00
|
|
|
|
2022-07-07 07:53:31 +00:00
|
|
|
proc start(agent: SalesAgent) {.async.} =
|
2022-07-05 12:19:01 +00:00
|
|
|
try:
|
2022-07-07 07:53:31 +00:00
|
|
|
let sales = agent.sales
|
2022-07-05 12:19:01 +00:00
|
|
|
let market = sales.market
|
2022-07-07 07:53:31 +00:00
|
|
|
let availability = agent.availability
|
2022-07-05 07:39:59 +00:00
|
|
|
|
2022-07-07 14:18:45 +00:00
|
|
|
without onStore =? sales.onStore:
|
|
|
|
raiseAssert "onStore callback not set"
|
2022-07-05 07:39:59 +00:00
|
|
|
|
2022-07-07 14:18:45 +00:00
|
|
|
without onProve =? sales.onProve:
|
|
|
|
raiseAssert "onProve callback not set"
|
2022-07-05 08:24:33 +00:00
|
|
|
|
2022-04-20 10:39:40 +00:00
|
|
|
sales.remove(availability)
|
2022-07-05 07:39:59 +00:00
|
|
|
|
2022-08-01 12:12:05 +00:00
|
|
|
agent.selectSlot()
|
|
|
|
without slotIndex =? agent.slotIndex:
|
|
|
|
raiseAssert "no slot selected"
|
|
|
|
|
|
|
|
await agent.subscribeSlotFilled(slotIndex)
|
2022-07-05 12:19:01 +00:00
|
|
|
|
2022-07-07 07:53:31 +00:00
|
|
|
agent.request = await market.getRequest(agent.requestId)
|
|
|
|
without request =? agent.request:
|
|
|
|
agent.finish(success = false)
|
2022-07-05 07:39:59 +00:00
|
|
|
return
|
|
|
|
|
2022-07-07 07:53:31 +00:00
|
|
|
agent.waiting = some agent.waitForExpiry()
|
2022-07-05 13:04:25 +00:00
|
|
|
|
2022-08-02 15:23:12 +00:00
|
|
|
await onStore(request, slotIndex, availability)
|
2022-08-02 15:33:32 +00:00
|
|
|
let proof = await onProve(request, slotIndex)
|
2022-08-01 12:12:05 +00:00
|
|
|
await market.fillSlot(request.id, slotIndex, proof)
|
2022-07-05 12:19:01 +00:00
|
|
|
except CancelledError:
|
|
|
|
raise
|
2022-04-20 10:39:40 +00:00
|
|
|
except CatchableError as e:
|
2022-07-07 07:53:31 +00:00
|
|
|
error "SalesAgent failed", msg = e.msg
|
|
|
|
agent.finish(success = false)
|
2022-04-20 10:39:40 +00:00
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
proc handleRequest(sales: Sales, requestId: RequestId, ask: StorageAsk) =
|
2022-04-11 18:03:55 +00:00
|
|
|
without availability =? sales.findAvailability(ask):
|
2022-03-31 12:35:53 +00:00
|
|
|
return
|
2022-03-31 08:24:06 +00:00
|
|
|
|
2022-07-07 07:53:31 +00:00
|
|
|
let agent = SalesAgent(
|
2022-03-31 12:35:53 +00:00
|
|
|
sales: sales,
|
2022-04-11 18:03:55 +00:00
|
|
|
requestId: requestId,
|
|
|
|
ask: ask,
|
2022-03-31 12:35:53 +00:00
|
|
|
availability: availability
|
|
|
|
)
|
|
|
|
|
2022-07-07 07:53:31 +00:00
|
|
|
agent.running = some agent.start()
|
2022-03-30 10:51:28 +00:00
|
|
|
|
2022-05-17 14:40:57 +00:00
|
|
|
proc start*(sales: Sales) {.async.} =
|
2022-03-30 10:51:28 +00:00
|
|
|
doAssert sales.subscription.isNone, "Sales already started"
|
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
proc onRequest(requestId: RequestId, ask: StorageAsk) {.gcsafe, upraises:[].} =
|
2022-04-20 10:39:40 +00:00
|
|
|
sales.handleRequest(requestId, ask)
|
2022-03-30 10:51:28 +00:00
|
|
|
|
2022-05-17 14:40:57 +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
|
|
|
|
2022-05-17 14:40:57 +00:00
|
|
|
proc stop*(sales: Sales) {.async.} =
|
2022-03-30 10:51:28 +00:00
|
|
|
if subscription =? sales.subscription:
|
2022-08-17 04:02:53 +00:00
|
|
|
sales.subscription = market.Subscription.none
|
2022-05-17 14:40:57 +00:00
|
|
|
try:
|
|
|
|
await subscription.unsubscribe()
|
|
|
|
except CatchableError as e:
|
|
|
|
warn "Unsubscribe failed", msg = e.msg
|