[purchasing] Add state to track purchase progress
This commit is contained in:
parent
04c291b05c
commit
03140fdf49
|
@ -1,8 +1,10 @@
|
||||||
import pkg/chronos
|
import pkg/chronos
|
||||||
import ./contracts/requests
|
import ./contracts/requests
|
||||||
|
import ./contracts/offers
|
||||||
|
|
||||||
export chronos
|
export chronos
|
||||||
export requests
|
export requests
|
||||||
|
export offers
|
||||||
|
|
||||||
type
|
type
|
||||||
Market* = ref object of RootObj
|
Market* = ref object of RootObj
|
||||||
|
|
|
@ -14,10 +14,17 @@ type
|
||||||
proofProbability*: UInt256
|
proofProbability*: UInt256
|
||||||
requestExpiryInterval*: UInt256
|
requestExpiryInterval*: UInt256
|
||||||
Purchase* = ref object
|
Purchase* = ref object
|
||||||
|
future: Future[void]
|
||||||
|
market: Market
|
||||||
|
request*: StorageRequest
|
||||||
|
offers*: seq[StorageOffer]
|
||||||
|
selected*: ?StorageOffer
|
||||||
|
|
||||||
const DefaultProofProbability = 100.u256
|
const DefaultProofProbability = 100.u256
|
||||||
const DefaultRequestExpiryInterval = (10 * 60).u256
|
const DefaultRequestExpiryInterval = (10 * 60).u256
|
||||||
|
|
||||||
|
proc start(purchase: Purchase) {.gcsafe.}
|
||||||
|
|
||||||
proc new*(_: type Purchasing, market: Market): Purchasing =
|
proc new*(_: type Purchasing, market: Market): Purchasing =
|
||||||
Purchasing(
|
Purchasing(
|
||||||
market: market,
|
market: market,
|
||||||
|
@ -25,15 +32,27 @@ proc new*(_: type Purchasing, market: Market): Purchasing =
|
||||||
requestExpiryInterval: DefaultRequestExpiryInterval
|
requestExpiryInterval: DefaultRequestExpiryInterval
|
||||||
)
|
)
|
||||||
|
|
||||||
|
proc populate*(purchasing: Purchasing, request: StorageRequest): StorageRequest =
|
||||||
|
result = request
|
||||||
|
if result.proofProbability == 0.u256:
|
||||||
|
result.proofProbability = purchasing.proofProbability
|
||||||
|
if result.expiry == 0.u256:
|
||||||
|
result.expiry = (getTime().toUnix().u256 + purchasing.requestExpiryInterval)
|
||||||
|
if result.nonce == array[32, byte].default:
|
||||||
|
doAssert randomBytes(result.nonce) == 32
|
||||||
|
|
||||||
proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
|
proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
|
||||||
var request = request
|
let request = purchasing.populate(request)
|
||||||
if request.proofProbability == 0.u256:
|
let purchase = Purchase(request: request, market: purchasing.market)
|
||||||
request.proofProbability = purchasing.proofProbability
|
purchase.start()
|
||||||
if request.expiry == 0.u256:
|
purchase
|
||||||
request.expiry = (getTime().toUnix().u256 + purchasing.requestExpiryInterval)
|
|
||||||
if request.nonce == array[32, byte].default:
|
proc run(purchase: Purchase) {.async.} =
|
||||||
doAssert randomBytes(request.nonce) == 32
|
await purchase.market.requestStorage(purchase.request)
|
||||||
asyncSpawn purchasing.market.requestStorage(request)
|
|
||||||
|
proc start(purchase: Purchase) =
|
||||||
|
purchase.future = purchase.run()
|
||||||
|
asyncSpawn purchase.future
|
||||||
|
|
||||||
proc wait*(purchase: Purchase) {.async.} =
|
proc wait*(purchase: Purchase) {.async.} =
|
||||||
discard
|
await purchase.future
|
||||||
|
|
Loading…
Reference in New Issue