2022-03-30 15:28:56 +00:00
|
|
|
import std/times
|
2022-03-30 10:51:28 +00:00
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/sales
|
2022-03-30 10:51:28 +00:00
|
|
|
import ./helpers/mockmarket
|
2022-05-17 15:02:03 +00:00
|
|
|
import ./helpers/mockclock
|
2022-03-30 10:51:28 +00:00
|
|
|
import ./examples
|
|
|
|
|
|
|
|
suite "Sales":
|
|
|
|
|
2022-05-09 14:51:08 +00:00
|
|
|
let availability = Availability.init(
|
|
|
|
size=100.u256,
|
|
|
|
duration=60.u256,
|
|
|
|
minPrice=42.u256
|
2022-04-11 18:03:55 +00:00
|
|
|
)
|
2022-07-05 07:39:59 +00:00
|
|
|
let request = StorageRequest(
|
|
|
|
ask: StorageAsk(
|
|
|
|
duration: 60.u256,
|
|
|
|
size: 100.u256,
|
|
|
|
maxPrice:42.u256
|
|
|
|
),
|
|
|
|
content: StorageContent(
|
|
|
|
cid: "some cid"
|
|
|
|
)
|
|
|
|
)
|
2022-07-05 08:37:55 +00:00
|
|
|
let proof = seq[byte].example
|
2022-03-31 09:34:33 +00:00
|
|
|
|
2022-03-30 10:51:28 +00:00
|
|
|
var sales: Sales
|
|
|
|
var market: MockMarket
|
2022-05-17 15:02:03 +00:00
|
|
|
var clock: MockClock
|
2022-03-30 10:51:28 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
market = MockMarket.new()
|
2022-05-17 15:02:03 +00:00
|
|
|
clock = MockClock.new()
|
|
|
|
sales = Sales.new(market, clock)
|
2022-07-05 07:39:59 +00:00
|
|
|
sales.retrieve = proc(_: string) {.async.} = discard
|
2022-07-05 08:37:55 +00:00
|
|
|
sales.prove = proc(_: string): Future[seq[byte]] {.async.} = return proof
|
2022-05-17 14:40:57 +00:00
|
|
|
await sales.start()
|
2022-03-31 09:34:33 +00:00
|
|
|
|
|
|
|
teardown:
|
2022-05-17 14:40:57 +00:00
|
|
|
await sales.stop()
|
2022-03-30 10:51:28 +00:00
|
|
|
|
|
|
|
test "has no availability initially":
|
|
|
|
check sales.available.len == 0
|
|
|
|
|
|
|
|
test "can add available storage":
|
|
|
|
let availability1 = Availability.example
|
|
|
|
let availability2 = Availability.example
|
|
|
|
sales.add(availability1)
|
|
|
|
check sales.available.contains(availability1)
|
|
|
|
sales.add(availability2)
|
|
|
|
check sales.available.contains(availability1)
|
|
|
|
check sales.available.contains(availability2)
|
|
|
|
|
|
|
|
test "can remove available storage":
|
|
|
|
sales.add(availability)
|
|
|
|
sales.remove(availability)
|
|
|
|
check sales.available.len == 0
|
|
|
|
|
|
|
|
test "generates unique ids for storage availability":
|
2022-05-09 14:51:08 +00:00
|
|
|
let availability1 = Availability.init(1.u256, 2.u256, 3.u256)
|
|
|
|
let availability2 = Availability.init(1.u256, 2.u256, 3.u256)
|
2022-03-30 10:51:28 +00:00
|
|
|
check availability1.id != availability2.id
|
|
|
|
|
2022-07-05 08:29:02 +00:00
|
|
|
test "makes storage unavailable when matching request comes in":
|
2022-07-05 07:39:59 +00:00
|
|
|
sales.add(availability)
|
|
|
|
discard await market.requestStorage(request)
|
2022-07-05 08:29:02 +00:00
|
|
|
check sales.available.len == 0
|
2022-07-04 15:45:53 +00:00
|
|
|
|
2022-07-05 07:39:59 +00:00
|
|
|
test "ignores request when no matching storage is available":
|
|
|
|
sales.add(availability)
|
|
|
|
var tooBig = request
|
|
|
|
tooBig.ask.size = request.ask.size + 1
|
|
|
|
discard await market.requestStorage(tooBig)
|
2022-07-05 08:29:02 +00:00
|
|
|
check sales.available == @[availability]
|
2022-03-30 10:51:28 +00:00
|
|
|
|
2022-07-05 08:29:02 +00:00
|
|
|
test "retrieves data":
|
|
|
|
var retrievingCid: string
|
|
|
|
sales.retrieve = proc(cid: string) {.async.} = retrievingCid = cid
|
2022-03-30 10:51:28 +00:00
|
|
|
sales.add(availability)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-07-05 08:29:02 +00:00
|
|
|
check retrievingCid == request.content.cid
|
2022-03-30 15:28:56 +00:00
|
|
|
|
2022-07-05 08:32:09 +00:00
|
|
|
test "makes storage available again when data retrieval fails":
|
|
|
|
let error = newException(IOError, "data retrieval failed")
|
|
|
|
sales.retrieve = proc(cid: string) {.async.} = raise error
|
|
|
|
sales.add(availability)
|
|
|
|
discard await market.requestStorage(request)
|
|
|
|
check sales.available == @[availability]
|
|
|
|
|
2022-07-05 08:24:33 +00:00
|
|
|
test "generates proof of storage":
|
|
|
|
var provingCid: string
|
|
|
|
sales.prove = proc(cid: string): Future[seq[byte]] {.async.} = provingCid = cid
|
|
|
|
sales.add(availability)
|
|
|
|
discard await market.requestStorage(request)
|
|
|
|
check provingCid == request.content.cid
|
|
|
|
|
2022-07-05 08:37:55 +00:00
|
|
|
test "fulfills request":
|
|
|
|
sales.add(availability)
|
|
|
|
discard await market.requestStorage(request)
|
|
|
|
check market.fulfilled.len == 1
|
|
|
|
check market.fulfilled[0].requestId == request.id
|
|
|
|
check market.fulfilled[0].proof == proof
|
|
|
|
check market.fulfilled[0].host == await market.getSigner()
|
|
|
|
|
2022-07-05 08:51:01 +00:00
|
|
|
test "calls onSale when request is fulfilled":
|
|
|
|
var soldAvailability: Availability
|
|
|
|
var soldRequest: StorageRequest
|
|
|
|
sales.onSale = proc(availability: Availability, request: StorageRequest) =
|
|
|
|
soldAvailability = availability
|
|
|
|
soldRequest = request
|
|
|
|
sales.add(availability)
|
|
|
|
discard await market.requestStorage(request)
|
|
|
|
check soldAvailability == availability
|
|
|
|
check soldRequest == request
|
2022-07-04 15:45:53 +00:00
|
|
|
|
|
|
|
# test "does not call onSale when a different offer is selected":
|
|
|
|
# var didSell: bool
|
|
|
|
# sales.onSale = proc(offer: StorageOffer) =
|
|
|
|
# didSell = true
|
|
|
|
# sales.add(availability)
|
|
|
|
# let request = await market.requestStorage(request)
|
|
|
|
# var otherOffer = StorageOffer(requestId: request.id, price: 1.u256)
|
|
|
|
# otherOffer = await market.offerStorage(otherOffer)
|
|
|
|
# await market.selectOffer(otherOffer.id)
|
|
|
|
# check not didSell
|
|
|
|
|
|
|
|
# test "makes storage available again when different offer is selected":
|
|
|
|
# sales.add(availability)
|
|
|
|
# let request = await market.requestStorage(request)
|
|
|
|
# var otherOffer = StorageOffer(requestId: request.id, price: 1.u256)
|
|
|
|
# otherOffer = await market.offerStorage(otherOffer)
|
|
|
|
# await market.selectOffer(otherOffer.id)
|
|
|
|
# check sales.available.contains(availability)
|
|
|
|
|
|
|
|
# test "makes storage available again when offer expires":
|
|
|
|
# sales.add(availability)
|
|
|
|
# discard await market.requestStorage(request)
|
|
|
|
# let offer = market.offered[0]
|
|
|
|
# clock.set(offer.expiry.truncate(int64))
|
|
|
|
# await sleepAsync(chronos.seconds(2))
|
|
|
|
# check sales.available.contains(availability)
|