2022-03-23 12:57:48 +00:00
|
|
|
import std/times
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/stint
|
|
|
|
import pkg/dagger/purchasing
|
|
|
|
import ./helpers/mockmarket
|
|
|
|
import ./examples
|
|
|
|
|
|
|
|
suite "Purchasing":
|
|
|
|
|
|
|
|
var purchasing: Purchasing
|
|
|
|
var market: MockMarket
|
2022-03-24 09:00:40 +00:00
|
|
|
var request: StorageRequest
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
market = MockMarket.new()
|
|
|
|
purchasing = Purchasing.new(market)
|
2022-03-24 09:00:40 +00:00
|
|
|
request = StorageRequest(
|
|
|
|
duration: uint16.example.u256,
|
|
|
|
size: uint32.example.u256,
|
|
|
|
contentHash: array[32, byte].example
|
|
|
|
)
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
test "submits a storage request when asked":
|
2022-03-24 09:00:40 +00:00
|
|
|
await purchasing.purchase(request).wait()
|
2022-03-24 12:48:18 +00:00
|
|
|
let submitted = market.requested[0]
|
2022-03-24 09:00:40 +00:00
|
|
|
check submitted.duration == request.duration
|
|
|
|
check submitted.size == request.size
|
|
|
|
check submitted.contentHash == request.contentHash
|
|
|
|
check submitted.maxPrice == request.maxPrice
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
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
|
2022-03-24 09:00:40 +00:00
|
|
|
await purchasing.purchase(request).wait()
|
2022-03-24 12:48:18 +00:00
|
|
|
check market.requested[0].proofProbability == 42.u256
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
test "can override proof probability per request":
|
2022-03-24 09:00:40 +00:00
|
|
|
request.proofProbability = 42.u256
|
|
|
|
await purchasing.purchase(request).wait()
|
2022-03-24 12:48:18 +00:00
|
|
|
check market.requested[0].proofProbability == 42.u256
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
test "has a default value for request expiration interval":
|
|
|
|
check purchasing.requestExpiryInterval != 0.u256
|
|
|
|
|
|
|
|
test "can change default value for request expiration interval":
|
|
|
|
purchasing.requestExpiryInterval = 42.u256
|
|
|
|
let start = getTime().toUnix()
|
2022-03-24 09:00:40 +00:00
|
|
|
await purchasing.purchase(request).wait()
|
2022-03-24 12:48:18 +00:00
|
|
|
check market.requested[0].expiry == (start + 42).u256
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
test "can override expiry time per request":
|
|
|
|
let expiry = (getTime().toUnix() + 42).u256
|
2022-03-24 09:00:40 +00:00
|
|
|
request.expiry = expiry
|
|
|
|
await purchasing.purchase(request).wait()
|
2022-03-24 12:48:18 +00:00
|
|
|
check market.requested[0].expiry == expiry
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
test "includes a random nonce in every storage request":
|
2022-03-24 09:00:40 +00:00
|
|
|
await purchasing.purchase(request).wait()
|
|
|
|
await purchasing.purchase(request).wait()
|
2022-03-24 12:48:18 +00:00
|
|
|
check market.requested[0].nonce != market.requested[1].nonce
|