2022-08-17 04:02:53 +00:00
|
|
|
import std/sets
|
2022-03-30 10:51:28 +00:00
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
2022-08-17 04:02:53 +00:00
|
|
|
import pkg/codex/contracts/requests
|
|
|
|
import pkg/codex/proving
|
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,
|
2022-08-01 12:25:32 +00:00
|
|
|
minPrice=600.u256
|
2022-04-11 18:03:55 +00:00
|
|
|
)
|
2022-07-05 13:04:25 +00:00
|
|
|
var request = StorageRequest(
|
2022-07-05 07:39:59 +00:00
|
|
|
ask: StorageAsk(
|
2022-08-02 12:21:12 +00:00
|
|
|
slots: 4,
|
|
|
|
slotSize: 100.u256,
|
2022-07-05 07:39:59 +00:00
|
|
|
duration: 60.u256,
|
2022-08-01 12:25:32 +00:00
|
|
|
reward: 10.u256,
|
2022-07-05 07:39:59 +00:00
|
|
|
),
|
|
|
|
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-08-17 04:02:53 +00:00
|
|
|
var proving: Proving
|
2022-03-30 10:51:28 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
market = MockMarket.new()
|
2022-05-17 15:02:03 +00:00
|
|
|
clock = MockClock.new()
|
2022-08-17 04:02:53 +00:00
|
|
|
proving = Proving.new()
|
|
|
|
sales = Sales.new(market, clock, proving)
|
2022-08-02 15:23:12 +00:00
|
|
|
sales.onStore = proc(request: StorageRequest,
|
|
|
|
slot: UInt256,
|
|
|
|
availability: Availability) {.async.} =
|
2022-07-07 14:14:19 +00:00
|
|
|
discard
|
2022-08-02 15:33:32 +00:00
|
|
|
sales.onProve = proc(request: StorageRequest,
|
|
|
|
slot: UInt256): Future[seq[byte]] {.async.} =
|
2022-07-07 14:14:19 +00:00
|
|
|
return proof
|
2022-05-17 14:40:57 +00:00
|
|
|
await sales.start()
|
2022-07-05 13:04:25 +00:00
|
|
|
request.expiry = (clock.now() + 42).u256
|
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
|
2022-08-02 12:21:12 +00:00
|
|
|
tooBig.ask.slotSize = request.ask.slotSize + 1
|
2022-07-05 07:39:59 +00:00
|
|
|
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-08-01 12:25:32 +00:00
|
|
|
test "ignores request when reward is too low":
|
|
|
|
sales.add(availability)
|
|
|
|
var tooCheap = request
|
|
|
|
tooCheap.ask.reward = request.ask.reward - 1
|
|
|
|
discard await market.requestStorage(tooCheap)
|
|
|
|
check sales.available == @[availability]
|
|
|
|
|
2022-07-07 13:10:53 +00:00
|
|
|
test "retrieves and stores data locally":
|
2022-08-02 15:23:12 +00:00
|
|
|
var storingRequest: StorageRequest
|
|
|
|
var storingSlot: UInt256
|
2022-07-07 14:14:19 +00:00
|
|
|
var storingAvailability: Availability
|
2022-08-02 15:23:12 +00:00
|
|
|
sales.onStore = proc(request: StorageRequest,
|
|
|
|
slot: UInt256,
|
|
|
|
availability: Availability) {.async.} =
|
|
|
|
storingRequest = request
|
|
|
|
storingSlot = slot
|
2022-07-07 14:14:19 +00:00
|
|
|
storingAvailability = availability
|
2022-03-30 10:51:28 +00:00
|
|
|
sales.add(availability)
|
2022-08-02 15:23:12 +00:00
|
|
|
let requested = await market.requestStorage(request)
|
|
|
|
check storingRequest == requested
|
|
|
|
check storingSlot < request.ask.slots.u256
|
|
|
|
check storingAvailability == availability
|
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")
|
2022-08-02 15:23:12 +00:00
|
|
|
sales.onStore = proc(request: StorageRequest,
|
|
|
|
slot: UInt256,
|
|
|
|
availability: Availability) {.async.} =
|
2022-07-07 14:14:19 +00:00
|
|
|
raise error
|
2022-07-05 08:32:09 +00:00
|
|
|
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":
|
2022-08-02 15:33:32 +00:00
|
|
|
var provingRequest: StorageRequest
|
|
|
|
var provingSlot: UInt256
|
|
|
|
sales.onProve = proc(request: StorageRequest,
|
|
|
|
slot: UInt256): Future[seq[byte]] {.async.} =
|
|
|
|
provingRequest = request
|
|
|
|
provingSlot = slot
|
2022-07-05 08:24:33 +00:00
|
|
|
sales.add(availability)
|
2022-08-02 15:33:32 +00:00
|
|
|
let requested = await market.requestStorage(request)
|
|
|
|
check provingRequest == requested
|
|
|
|
check provingSlot < request.ask.slots.u256
|
2022-07-05 08:24:33 +00:00
|
|
|
|
2022-07-21 12:32:38 +00:00
|
|
|
test "fills a slot":
|
2022-07-05 08:37:55 +00:00
|
|
|
sales.add(availability)
|
|
|
|
discard await market.requestStorage(request)
|
2022-07-21 12:32:38 +00:00
|
|
|
check market.filled.len == 1
|
|
|
|
check market.filled[0].requestId == request.id
|
|
|
|
check market.filled[0].slotIndex < request.ask.slots.u256
|
|
|
|
check market.filled[0].proof == proof
|
|
|
|
check market.filled[0].host == await market.getSigner()
|
2022-07-05 08:37:55 +00:00
|
|
|
|
2022-07-21 12:32:38 +00:00
|
|
|
test "calls onSale when slot is filled":
|
2022-07-05 08:51:01 +00:00
|
|
|
var soldAvailability: Availability
|
|
|
|
var soldRequest: StorageRequest
|
2022-07-21 12:32:38 +00:00
|
|
|
var soldSlotIndex: UInt256
|
|
|
|
sales.onSale = proc(availability: Availability,
|
|
|
|
request: StorageRequest,
|
|
|
|
slotIndex: UInt256) =
|
2022-07-05 08:51:01 +00:00
|
|
|
soldAvailability = availability
|
|
|
|
soldRequest = request
|
2022-07-21 12:32:38 +00:00
|
|
|
soldSlotIndex = slotIndex
|
2022-07-05 08:51:01 +00:00
|
|
|
sales.add(availability)
|
|
|
|
discard await market.requestStorage(request)
|
|
|
|
check soldAvailability == availability
|
|
|
|
check soldRequest == request
|
2022-07-21 12:32:38 +00:00
|
|
|
check soldSlotIndex < request.ask.slots.u256
|
2022-07-04 15:45:53 +00:00
|
|
|
|
2022-07-07 14:14:19 +00:00
|
|
|
test "calls onClear when storage becomes available again":
|
2022-08-17 04:02:53 +00:00
|
|
|
# fail the proof intentionally to trigger `agent.finish(success=false)`,
|
|
|
|
# which then calls the onClear callback
|
2022-08-02 15:33:32 +00:00
|
|
|
sales.onProve = proc(request: StorageRequest,
|
|
|
|
slot: UInt256): Future[seq[byte]] {.async.} =
|
2022-07-07 14:14:19 +00:00
|
|
|
raise newException(IOError, "proof failed")
|
|
|
|
var clearedAvailability: Availability
|
|
|
|
var clearedRequest: StorageRequest
|
2022-08-17 04:02:53 +00:00
|
|
|
var clearedSlotIndex: UInt256
|
|
|
|
sales.onClear = proc(availability: Availability,
|
|
|
|
request: StorageRequest,
|
|
|
|
slotIndex: UInt256) =
|
2022-07-07 14:14:19 +00:00
|
|
|
clearedAvailability = availability
|
|
|
|
clearedRequest = request
|
2022-08-17 04:02:53 +00:00
|
|
|
clearedSlotIndex = slotIndex
|
2022-07-07 14:14:19 +00:00
|
|
|
sales.add(availability)
|
|
|
|
discard await market.requestStorage(request)
|
|
|
|
check clearedAvailability == availability
|
|
|
|
check clearedRequest == request
|
2022-08-17 04:02:53 +00:00
|
|
|
check clearedSlotIndex < request.ask.slots.u256
|
2022-07-07 14:14:19 +00:00
|
|
|
|
2022-07-21 12:32:38 +00:00
|
|
|
test "makes storage available again when other host fills the slot":
|
2022-07-05 12:19:01 +00:00
|
|
|
let otherHost = Address.example
|
2022-08-02 15:23:12 +00:00
|
|
|
sales.onStore = proc(request: StorageRequest,
|
|
|
|
slot: UInt256,
|
|
|
|
availability: Availability) {.async.} =
|
2022-07-07 14:14:19 +00:00
|
|
|
await sleepAsync(1.hours)
|
2022-07-05 12:19:01 +00:00
|
|
|
sales.add(availability)
|
|
|
|
discard await market.requestStorage(request)
|
2022-07-21 12:32:38 +00:00
|
|
|
for slotIndex in 0..<request.ask.slots:
|
|
|
|
market.fillSlot(request.id, slotIndex.u256, proof, otherHost)
|
2022-07-05 12:19:01 +00:00
|
|
|
check sales.available == @[availability]
|
2022-07-04 15:45:53 +00:00
|
|
|
|
2022-07-05 13:04:25 +00:00
|
|
|
test "makes storage available again when request expires":
|
2022-08-02 15:23:12 +00:00
|
|
|
sales.onStore = proc(request: StorageRequest,
|
|
|
|
slot: UInt256,
|
|
|
|
availability: Availability) {.async.} =
|
2022-07-07 14:14:19 +00:00
|
|
|
await sleepAsync(1.hours)
|
2022-07-05 13:04:25 +00:00
|
|
|
sales.add(availability)
|
|
|
|
discard await market.requestStorage(request)
|
|
|
|
clock.set(request.expiry.truncate(int64))
|
|
|
|
await sleepAsync(2.seconds)
|
|
|
|
check sales.available == @[availability]
|
2022-08-17 04:02:53 +00:00
|
|
|
|
|
|
|
test "adds proving for slot when slot is filled":
|
|
|
|
var soldSlotIndex: UInt256
|
|
|
|
sales.onSale = proc(availability: Availability,
|
|
|
|
request: StorageRequest,
|
|
|
|
slotIndex: UInt256) =
|
|
|
|
soldSlotIndex = slotIndex
|
2022-08-17 04:18:30 +00:00
|
|
|
check proving.slots.len == 0
|
2022-08-17 04:02:53 +00:00
|
|
|
sales.add(availability)
|
|
|
|
discard await market.requestStorage(request)
|
2022-08-17 04:18:30 +00:00
|
|
|
check proving.slots.len == 1
|
|
|
|
check proving.slots.contains(request.slotId(soldSlotIndex))
|