2022-03-23 12:57:48 +00:00
|
|
|
import std/times
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/stint
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/purchasing
|
2022-03-23 12:57:48 +00:00
|
|
|
import ./helpers/mockmarket
|
2022-05-17 14:51:29 +00:00
|
|
|
import ./helpers/mockclock
|
2022-03-23 12:57:48 +00:00
|
|
|
import ./examples
|
|
|
|
|
|
|
|
suite "Purchasing":
|
|
|
|
|
|
|
|
var purchasing: Purchasing
|
|
|
|
var market: MockMarket
|
2022-05-17 14:51:29 +00:00
|
|
|
var clock: MockClock
|
2022-03-24 09:00:40 +00:00
|
|
|
var request: StorageRequest
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
market = MockMarket.new()
|
2022-05-17 14:51:29 +00:00
|
|
|
clock = MockClock.new()
|
|
|
|
purchasing = Purchasing.new(market, clock)
|
2022-03-24 09:00:40 +00:00
|
|
|
request = StorageRequest(
|
2022-04-11 18:03:55 +00:00
|
|
|
ask: StorageAsk(
|
|
|
|
duration: uint16.example.u256,
|
|
|
|
size: uint32.example.u256,
|
|
|
|
)
|
2022-03-24 09:00:40 +00:00
|
|
|
)
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
test "submits a storage request when asked":
|
2022-06-14 13:25:48 +00:00
|
|
|
discard purchasing.purchase(request)
|
2022-03-24 12:48:18 +00:00
|
|
|
let submitted = market.requested[0]
|
2022-04-11 18:03:55 +00:00
|
|
|
check submitted.ask.duration == request.ask.duration
|
|
|
|
check submitted.ask.size == request.ask.size
|
2022-07-20 12:11:00 +00:00
|
|
|
check submitted.ask.reward == request.ask.reward
|
2022-03-23 12:57:48 +00:00
|
|
|
|
2022-05-11 07:14:43 +00:00
|
|
|
test "remembers purchases":
|
|
|
|
let purchase1 = purchasing.purchase(request)
|
|
|
|
let purchase2 = purchasing.purchase(request)
|
|
|
|
check purchasing.getPurchase(purchase1.id) == some purchase1
|
|
|
|
check purchasing.getPurchase(purchase2.id) == some purchase2
|
|
|
|
|
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-06-14 13:25:48 +00:00
|
|
|
discard purchasing.purchase(request)
|
2022-04-11 18:03:55 +00:00
|
|
|
check market.requested[0].ask.proofProbability == 42.u256
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
test "can override proof probability per request":
|
2022-04-11 18:03:55 +00:00
|
|
|
request.ask.proofProbability = 42.u256
|
2022-06-14 13:25:48 +00:00
|
|
|
discard purchasing.purchase(request)
|
2022-04-11 18:03:55 +00:00
|
|
|
check market.requested[0].ask.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-06-14 13:25:48 +00:00
|
|
|
discard purchasing.purchase(request)
|
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
|
2022-06-14 13:25:48 +00:00
|
|
|
discard purchasing.purchase(request)
|
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-06-14 13:25:48 +00:00
|
|
|
discard purchasing.purchase(request)
|
|
|
|
discard purchasing.purchase(request)
|
2022-03-24 12:48:18 +00:00
|
|
|
check market.requested[0].nonce != market.requested[1].nonce
|
2022-03-28 10:28:22 +00:00
|
|
|
|
2022-06-14 13:25:48 +00:00
|
|
|
test "succeeds when request is fulfilled":
|
2022-03-28 10:28:22 +00:00
|
|
|
let purchase = purchasing.purchase(request)
|
|
|
|
let request = market.requested[0]
|
2022-06-14 13:25:48 +00:00
|
|
|
let proof = seq[byte].example
|
|
|
|
await market.fulfillRequest(request.id, proof)
|
2022-03-28 10:28:22 +00:00
|
|
|
await purchase.wait()
|
2022-06-14 13:25:48 +00:00
|
|
|
check purchase.error.isNone
|
2022-03-28 12:40:41 +00:00
|
|
|
|
2022-06-14 13:25:48 +00:00
|
|
|
test "fails when request times out":
|
2022-03-28 12:40:41 +00:00
|
|
|
let purchase = purchasing.purchase(request)
|
|
|
|
let request = market.requested[0]
|
2022-05-18 08:19:32 +00:00
|
|
|
clock.set(request.expiry.truncate(int64))
|
2022-06-14 13:25:48 +00:00
|
|
|
expect PurchaseTimeout:
|
|
|
|
await purchase.wait()
|