2022-08-18 05:55:53 +00:00
|
|
|
import std/hashes
|
2022-05-11 07:14:43 +00:00
|
|
|
import std/tables
|
2022-03-23 12:57:48 +00:00
|
|
|
import pkg/stint
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/nimcrypto
|
|
|
|
import ./market
|
2022-05-17 14:51:29 +00:00
|
|
|
import ./clock
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
export questionable
|
2022-03-24 09:00:40 +00:00
|
|
|
export market
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Purchasing* = ref object
|
|
|
|
market: Market
|
2022-05-17 14:51:29 +00:00
|
|
|
clock: Clock
|
2022-08-17 02:29:44 +00:00
|
|
|
purchases: Table[PurchaseId, Purchase]
|
2022-03-23 12:57:48 +00:00
|
|
|
proofProbability*: UInt256
|
|
|
|
requestExpiryInterval*: UInt256
|
|
|
|
Purchase* = ref object
|
2022-03-24 13:22:45 +00:00
|
|
|
future: Future[void]
|
|
|
|
market: Market
|
2022-05-17 14:51:29 +00:00
|
|
|
clock: Clock
|
2022-03-24 13:22:45 +00:00
|
|
|
request*: StorageRequest
|
2022-06-14 13:25:48 +00:00
|
|
|
PurchaseTimeout* = Timeout
|
2022-08-18 06:01:47 +00:00
|
|
|
RequestState* = enum
|
|
|
|
New = 1, # [default] waiting to fill slots
|
|
|
|
Started = 2, # all slots filled, accepting regular proofs
|
|
|
|
Cancelled = 3, # not enough slots filled before expiry
|
|
|
|
Finished = 4, # successfully completed
|
|
|
|
Failed = 5 # too many nodes have failed to provide proofs, data lost
|
2022-08-18 05:55:53 +00:00
|
|
|
PurchaseId* = distinct array[32, byte]
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
const DefaultProofProbability = 100.u256
|
|
|
|
const DefaultRequestExpiryInterval = (10 * 60).u256
|
|
|
|
|
2022-03-24 13:22:45 +00:00
|
|
|
proc start(purchase: Purchase) {.gcsafe.}
|
2022-08-17 02:29:44 +00:00
|
|
|
func id*(purchase: Purchase): PurchaseId
|
2022-08-18 05:55:53 +00:00
|
|
|
proc `==`*(x, y: PurchaseId): bool {.borrow.}
|
|
|
|
proc hash*(x: PurchaseId): Hash {.borrow.}
|
2022-08-22 02:55:19 +00:00
|
|
|
# Using {.borrow.} for toHex does not borrow correctly and causes a
|
|
|
|
# C-compilation error, so we must do it long form
|
|
|
|
proc toHex*(x: PurchaseId): string = array[32, byte](x).toHex
|
2022-03-24 13:22:45 +00:00
|
|
|
|
2022-05-17 14:51:29 +00:00
|
|
|
proc new*(_: type Purchasing, market: Market, clock: Clock): Purchasing =
|
2022-03-23 12:57:48 +00:00
|
|
|
Purchasing(
|
|
|
|
market: market,
|
2022-05-17 14:51:29 +00:00
|
|
|
clock: clock,
|
2022-03-23 12:57:48 +00:00
|
|
|
proofProbability: DefaultProofProbability,
|
2022-03-28 13:16:39 +00:00
|
|
|
requestExpiryInterval: DefaultRequestExpiryInterval,
|
2022-03-23 12:57:48 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 13:22:45 +00:00
|
|
|
proc populate*(purchasing: Purchasing, request: StorageRequest): StorageRequest =
|
|
|
|
result = request
|
2022-04-11 18:03:55 +00:00
|
|
|
if result.ask.proofProbability == 0.u256:
|
|
|
|
result.ask.proofProbability = purchasing.proofProbability
|
2022-03-24 13:22:45 +00:00
|
|
|
if result.expiry == 0.u256:
|
2022-05-17 14:51:29 +00:00
|
|
|
result.expiry = (purchasing.clock.now().u256 + purchasing.requestExpiryInterval)
|
2022-08-17 02:29:44 +00:00
|
|
|
if result.nonce == Nonce.default:
|
2022-08-18 05:55:53 +00:00
|
|
|
var id = result.nonce.toArray
|
|
|
|
doAssert randomBytes(id) == 32
|
|
|
|
result.nonce = Nonce(id)
|
2022-03-24 13:22:45 +00:00
|
|
|
|
2022-03-24 09:00:40 +00:00
|
|
|
proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
|
2022-03-24 13:22:45 +00:00
|
|
|
let request = purchasing.populate(request)
|
2022-03-28 13:16:39 +00:00
|
|
|
let purchase = Purchase(
|
|
|
|
request: request,
|
|
|
|
market: purchasing.market,
|
2022-05-17 14:51:29 +00:00
|
|
|
clock: purchasing.clock,
|
2022-03-28 13:16:39 +00:00
|
|
|
)
|
2022-03-24 13:22:45 +00:00
|
|
|
purchase.start()
|
2022-05-11 07:14:43 +00:00
|
|
|
purchasing.purchases[purchase.id] = purchase
|
2022-03-24 13:22:45 +00:00
|
|
|
purchase
|
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
func getPurchase*(purchasing: Purchasing, id: PurchaseId): ?Purchase =
|
2022-05-11 07:14:43 +00:00
|
|
|
if purchasing.purchases.hasKey(id):
|
|
|
|
some purchasing.purchases[id]
|
|
|
|
else:
|
|
|
|
none Purchase
|
|
|
|
|
2022-03-24 13:22:45 +00:00
|
|
|
proc run(purchase: Purchase) {.async.} =
|
2022-03-28 10:28:22 +00:00
|
|
|
let market = purchase.market
|
2022-06-14 13:25:48 +00:00
|
|
|
let clock = purchase.clock
|
2022-08-18 06:01:47 +00:00
|
|
|
var state = RequestState.New
|
2022-06-14 13:25:48 +00:00
|
|
|
|
|
|
|
proc requestStorage {.async.} =
|
|
|
|
purchase.request = await market.requestStorage(purchase.request)
|
|
|
|
|
|
|
|
proc waitUntilFulfilled {.async.} =
|
|
|
|
let done = newFuture[void]()
|
2022-08-17 02:29:44 +00:00
|
|
|
proc callback(_: RequestId) =
|
2022-06-14 13:25:48 +00:00
|
|
|
done.complete()
|
|
|
|
let request = purchase.request
|
|
|
|
let subscription = await market.subscribeFulfillment(request.id, callback)
|
2022-07-27 14:51:46 +00:00
|
|
|
await done
|
|
|
|
await subscription.unsubscribe()
|
2022-08-18 06:01:47 +00:00
|
|
|
state = RequestState.Started
|
2022-06-14 13:25:48 +00:00
|
|
|
|
|
|
|
proc withTimeout(future: Future[void]) {.async.} =
|
|
|
|
let expiry = purchase.request.expiry.truncate(int64)
|
|
|
|
await future.withTimeout(clock, expiry)
|
|
|
|
|
|
|
|
await requestStorage()
|
2022-08-18 06:01:47 +00:00
|
|
|
try:
|
|
|
|
await waitUntilFulfilled().withTimeout()
|
|
|
|
except PurchaseTimeout as e:
|
|
|
|
if state != RequestState.Started:
|
|
|
|
# If contract was fulfilled, the state would be RequestState.Started.
|
|
|
|
# Otherwise, the request would have timed out and should be considered
|
|
|
|
# cancelled. However, the request state hasn't been updated to
|
|
|
|
# RequestState.Cancelled yet so we can't check for that state or listen for
|
|
|
|
# an event emission. Instead, the state will be updated when the client
|
|
|
|
# requests to withdraw funds from the storage request.
|
|
|
|
await market.withdrawFunds(purchase.request.id)
|
|
|
|
state = RequestState.Cancelled
|
|
|
|
raise e
|
2022-03-24 13:22:45 +00:00
|
|
|
|
|
|
|
proc start(purchase: Purchase) =
|
|
|
|
purchase.future = purchase.run()
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
proc wait*(purchase: Purchase) {.async.} =
|
2022-03-24 13:22:45 +00:00
|
|
|
await purchase.future
|
2022-05-11 07:01:31 +00:00
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
func id*(purchase: Purchase): PurchaseId =
|
2022-08-18 05:55:53 +00:00
|
|
|
PurchaseId(purchase.request.id)
|
2022-05-12 11:43:42 +00:00
|
|
|
|
2022-08-18 06:01:47 +00:00
|
|
|
func cancelled*(purchase: Purchase): bool =
|
|
|
|
purchase.future.cancelled
|
|
|
|
|
2022-05-12 11:43:42 +00:00
|
|
|
func finished*(purchase: Purchase): bool =
|
|
|
|
purchase.future.finished
|
|
|
|
|
|
|
|
func error*(purchase: Purchase): ?(ref CatchableError) =
|
|
|
|
if purchase.future.failed:
|
|
|
|
some purchase.future.error
|
|
|
|
else:
|
|
|
|
none (ref CatchableError)
|