From 7c9c24483682a1cb227f9ce5dc636e520d99db69 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 24 Mar 2022 10:00:40 +0100 Subject: [PATCH] [purchasing] Remove PurchaseRequest; use StorageRequest --- dagger/purchasing.nim | 36 +++++++++------------------------ tests/dagger/examples.nim | 8 -------- tests/dagger/testpurchasing.nim | 36 ++++++++++++++++++--------------- 3 files changed, 29 insertions(+), 51 deletions(-) diff --git a/dagger/purchasing.nim b/dagger/purchasing.nim index 1ca842c3..c8f5cd15 100644 --- a/dagger/purchasing.nim +++ b/dagger/purchasing.nim @@ -6,19 +6,13 @@ import pkg/nimcrypto import ./market export questionable +export market type Purchasing* = ref object market: Market proofProbability*: UInt256 requestExpiryInterval*: UInt256 - PurchaseRequest* = object - duration*: UInt256 - size*: UInt256 - contentHash*: array[32, byte] - maxPrice*: UInt256 - proofProbability*: ?UInt256 - expiry*: ?UInt256 Purchase* = ref object const DefaultProofProbability = 100.u256 @@ -31,26 +25,14 @@ proc new*(_: type Purchasing, market: Market): Purchasing = requestExpiryInterval: DefaultRequestExpiryInterval ) -proc getProofProbability(purchasing: Purchasing, request: PurchaseRequest): UInt256 = - request.proofProbability |? purchasing.proofProbability - -proc getExpiry(purchasing: Purchasing, request: PurchaseRequest): UInt256 = - request.expiry |? (getTime().toUnix().u256 + purchasing.requestExpiryInterval) - -proc getNonce(): array[32, byte] = - doAssert randomBytes(result) == 32 - -proc purchase*(purchasing: Purchasing, request: PurchaseRequest): Purchase = - let request = StorageRequest( - client: Address.default, # TODO - duration: request.duration, - size: request.size, - contentHash: request.contentHash, - proofProbability: purchasing.getProofProbability(request), - maxPrice: request.maxPrice, - expiry: purchasing.getExpiry(request), - nonce: getNonce() - ) +proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase = + var request = request + if request.proofProbability == 0.u256: + request.proofProbability = purchasing.proofProbability + if request.expiry == 0.u256: + request.expiry = (getTime().toUnix().u256 + purchasing.requestExpiryInterval) + if request.nonce == array[32, byte].default: + doAssert randomBytes(request.nonce) == 32 asyncSpawn purchasing.market.requestStorage(request) proc wait*(purchase: Purchase) {.async.} = diff --git a/tests/dagger/examples.nim b/tests/dagger/examples.nim index 7034640f..dceb099e 100644 --- a/tests/dagger/examples.nim +++ b/tests/dagger/examples.nim @@ -6,7 +6,6 @@ import pkg/stint import pkg/dagger/rng import pkg/dagger/stores import pkg/dagger/blocktype -import pkg/dagger/purchasing proc example*(_: type EthAddress): EthAddress = EthPrivateKey.random().toPublicKey.toAddress @@ -62,10 +61,3 @@ proc example*(_: type BlockExcPeerCtx): BlockExcPeerCtx = proc example*(_: type Cid): Cid = Block.example.cid - -proc example*(_: type PurchaseRequest): PurchaseRequest = - PurchaseRequest( - duration: uint16.example.u256, - size: uint32.example.u256, - contentHash: array[32, byte].example - ) diff --git a/tests/dagger/testpurchasing.nim b/tests/dagger/testpurchasing.nim index 01ea9563..130af04a 100644 --- a/tests/dagger/testpurchasing.nim +++ b/tests/dagger/testpurchasing.nim @@ -10,32 +10,36 @@ suite "Purchasing": var purchasing: Purchasing var market: MockMarket - var purchaseRequest: PurchaseRequest + var request: StorageRequest setup: market = MockMarket.new() purchasing = Purchasing.new(market) - purchaseRequest = PurchaseRequest.example + request = StorageRequest( + duration: uint16.example.u256, + size: uint32.example.u256, + contentHash: array[32, byte].example + ) test "submits a storage request when asked": - await purchasing.purchase(purchaseRequest).wait() - let storageRequest = market.requests[0] - check storageRequest.duration == purchaseRequest.duration - check storageRequest.size == purchaseRequest.size - check storageRequest.contentHash == purchaseRequest.contentHash - check storageRequest.maxPrice == purchaseRequest.maxPrice + await purchasing.purchase(request).wait() + let submitted = market.requests[0] + check submitted.duration == request.duration + check submitted.size == request.size + check submitted.contentHash == request.contentHash + check submitted.maxPrice == request.maxPrice test "has a default value for proof probability": check purchasing.proofProbability != 0.u256 test "can change default value for proof probability": purchasing.proofProbability = 42.u256 - await purchasing.purchase(purchaseRequest).wait() + await purchasing.purchase(request).wait() check market.requests[0].proofProbability == 42.u256 test "can override proof probability per request": - purchaseRequest.proofProbability = some 42.u256 - await purchasing.purchase(purchaseRequest).wait() + request.proofProbability = 42.u256 + await purchasing.purchase(request).wait() check market.requests[0].proofProbability == 42.u256 test "has a default value for request expiration interval": @@ -44,16 +48,16 @@ suite "Purchasing": test "can change default value for request expiration interval": purchasing.requestExpiryInterval = 42.u256 let start = getTime().toUnix() - await purchasing.purchase(purchaseRequest).wait() + await purchasing.purchase(request).wait() check market.requests[0].expiry == (start + 42).u256 test "can override expiry time per request": let expiry = (getTime().toUnix() + 42).u256 - purchaseRequest.expiry = some expiry - await purchasing.purchase(purchaseRequest).wait() + request.expiry = expiry + await purchasing.purchase(request).wait() check market.requests[0].expiry == expiry test "includes a random nonce in every storage request": - await purchasing.purchase(purchaseRequest).wait() - await purchasing.purchase(purchaseRequest).wait() + await purchasing.purchase(request).wait() + await purchasing.purchase(request).wait() check market.requests[0].nonce != market.requests[1].nonce