[purchasing] Remove PurchaseRequest; use StorageRequest
This commit is contained in:
parent
9ade3fdd37
commit
7c9c244836
|
@ -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.} =
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue