mirror of
https://github.com/codex-storage/nim-codex.git
synced 2025-01-10 21:15:59 +00:00
4175689745
* [purchasing] Simplify test * [utils] Move StorageRequest.example up one level * [purchasing] Load purchases from market * [purchasing] load purchase states * Implement myRequest() and getState() methods for OnChainMarket * [proofs] Fix intermittently failing tests Ensures that examples of proofs in tests are never of length 0; these are considered invalid proofs by the smart contract logic. * [contracts] Fix failing test With the new solidity contracts update, a contract can only be paid out after it started. * [market] Add method to get request end time * [purchasing] wait until purchase is finished Purchase.wait() would previously wait until purchase was started, now we wait until it is finished. * [purchasing] Handle 'finished' and 'failed' states * [marketplace] move to failed state once request fails - Add support for subscribing to request failure events. - Add supporting contract tests for subscribing to request failure events. - Allow the PurchaseStarted state to move to PurchaseFailure once a request failure event is emitted - Add supporting tests for moving from PurchaseStarted to PurchaseFailure - Add state transition tests for PurchaseUnknown. * [marketplace] Fix test with longer sleepAsync * [integration] Add function to restart a codex node * [purchasing] Set client address before requesting storage To prevent the purchase id (which equals the request id) from changing once it's been submitted. * [contracts] Fix: OnChainMarket.getState() Had the wrong method signature before * [purchasing] Load purchases on node start * [purchasing] Rename state 'PurchaseError' to 'PurchaseErrored' Allows for an exception type called 'PurchaseError' * [purchasing] Load purchases in background No longer calls market.getRequest() for every purchase on node start. * [contracts] Add `$` for RequestId, SlotId and Nonce To aid with debugging * [purchasing] Add Purchasing.stop() To ensure that all contract interactions have both a start() and a stop() for * [tests] Remove sleepAsync where possible Use `eventually` loop instead, to make sure that we're not waiting unnecessarily. * [integration] Fix: handle non-json response in test * [purchasing] Add purchase state to json * [integration] Ensure that purchase is submitted before restart Fixes test failure on slower CI * [purchasing] re-implement `description` as method Allows description to be set in the same module where the state type is defined. Co-authored-by: Eric Mastro <eric.mastro@gmail.com> * [contracts] fix typo Co-authored-by: Eric Mastro <eric.mastro@gmail.com> * [market] Use more generic error type Should we decide to change the provider type later Co-authored-by: Eric Mastro <eric.mastro@gmail.com> Co-authored-by: Eric Mastro <eric.mastro@gmail.com>
75 lines
2.2 KiB
Nim
75 lines
2.2 KiB
Nim
import std/tables
|
|
import pkg/stint
|
|
import pkg/chronos
|
|
import pkg/questionable
|
|
import pkg/nimcrypto
|
|
import ./market
|
|
import ./clock
|
|
import ./purchasing/purchase
|
|
|
|
export questionable
|
|
export chronos
|
|
export market
|
|
export purchase
|
|
|
|
type
|
|
Purchasing* = ref object
|
|
market: Market
|
|
clock: Clock
|
|
purchases: Table[PurchaseId, Purchase]
|
|
proofProbability*: UInt256
|
|
requestExpiryInterval*: UInt256
|
|
PurchaseTimeout* = Timeout
|
|
|
|
const DefaultProofProbability = 100.u256
|
|
const DefaultRequestExpiryInterval = (10 * 60).u256
|
|
|
|
proc new*(_: type Purchasing, market: Market, clock: Clock): Purchasing =
|
|
Purchasing(
|
|
market: market,
|
|
clock: clock,
|
|
proofProbability: DefaultProofProbability,
|
|
requestExpiryInterval: DefaultRequestExpiryInterval,
|
|
)
|
|
|
|
proc load*(purchasing: Purchasing) {.async.} =
|
|
let market = purchasing.market
|
|
let requestIds = await market.myRequests()
|
|
for requestId in requestIds:
|
|
let purchase = Purchase.new(requestId, purchasing.market, purchasing.clock)
|
|
purchase.load()
|
|
purchasing.purchases[purchase.id] = purchase
|
|
|
|
proc start*(purchasing: Purchasing) {.async.} =
|
|
await purchasing.load()
|
|
|
|
proc stop*(purchasing: Purchasing) {.async.} =
|
|
discard
|
|
|
|
proc populate*(purchasing: Purchasing,
|
|
request: StorageRequest): Future[StorageRequest] {.async.} =
|
|
result = request
|
|
if result.ask.proofProbability == 0.u256:
|
|
result.ask.proofProbability = purchasing.proofProbability
|
|
if result.expiry == 0.u256:
|
|
result.expiry = (purchasing.clock.now().u256 + purchasing.requestExpiryInterval)
|
|
if result.nonce == Nonce.default:
|
|
var id = result.nonce.toArray
|
|
doAssert randomBytes(id) == 32
|
|
result.nonce = Nonce(id)
|
|
result.client = await purchasing.market.getSigner()
|
|
|
|
proc purchase*(purchasing: Purchasing,
|
|
request: StorageRequest): Future[Purchase] {.async.} =
|
|
let request = await purchasing.populate(request)
|
|
let purchase = Purchase.new(request, purchasing.market, purchasing.clock)
|
|
purchase.start()
|
|
purchasing.purchases[purchase.id] = purchase
|
|
return purchase
|
|
|
|
func getPurchase*(purchasing: Purchasing, id: PurchaseId): ?Purchase =
|
|
if purchasing.purchases.hasKey(id):
|
|
some purchasing.purchases[id]
|
|
else:
|
|
none Purchase
|