2022-02-16 09:50:00 +00:00
|
|
|
const { ethers } = require("hardhat")
|
2022-06-13 08:40:18 +00:00
|
|
|
const { hexlify, randomBytes } = ethers.utils
|
2023-01-09 12:38:12 +00:00
|
|
|
const { AddressZero } = ethers.constants
|
2023-01-09 14:15:11 +00:00
|
|
|
const { BigNumber } = ethers
|
2022-02-16 09:50:00 +00:00
|
|
|
const { expect } = require("chai")
|
2023-01-17 12:55:58 +00:00
|
|
|
const { exampleConfiguration, exampleRequest } = require("./examples")
|
2023-01-16 15:31:24 +00:00
|
|
|
const { periodic, hours } = require("./time")
|
2022-07-19 09:33:54 +00:00
|
|
|
const { requestId, slotId, askToArray } = require("./ids")
|
2023-01-24 04:59:56 +00:00
|
|
|
const {
|
|
|
|
RequestState,
|
|
|
|
SlotState,
|
|
|
|
enableRequestAssertions,
|
|
|
|
} = require("./requests")
|
2022-09-22 02:21:49 +00:00
|
|
|
const {
|
2022-09-21 09:57:26 +00:00
|
|
|
waitUntilCancelled,
|
|
|
|
waitUntilStarted,
|
|
|
|
waitUntilFinished,
|
|
|
|
waitUntilFailed,
|
2022-12-19 10:50:37 +00:00
|
|
|
waitUntilSlotFailed,
|
2022-09-21 09:57:26 +00:00
|
|
|
} = require("./marketplace")
|
2022-07-20 09:07:20 +00:00
|
|
|
const { price, pricePerSlot } = require("./price")
|
2022-07-19 15:09:35 +00:00
|
|
|
const {
|
|
|
|
snapshot,
|
|
|
|
revert,
|
2023-01-09 14:15:11 +00:00
|
|
|
mine,
|
2022-07-19 15:09:35 +00:00
|
|
|
ensureMinimumBlockHeight,
|
2023-10-16 09:14:02 +00:00
|
|
|
advanceTimeForNextBlock,
|
|
|
|
advanceTimeToForNextBlock,
|
2022-09-22 02:21:49 +00:00
|
|
|
currentTime,
|
2022-07-19 15:09:35 +00:00
|
|
|
} = require("./evm")
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2023-10-16 09:14:02 +00:00
|
|
|
const ACCOUNT_STARTING_BALANCE = 1_000_000_000
|
|
|
|
|
2023-05-30 12:42:59 +00:00
|
|
|
describe("Marketplace constructor", function () {
|
2024-01-10 14:12:06 +00:00
|
|
|
let Marketplace, token, verifier, config
|
2023-03-30 09:11:21 +00:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
await snapshot()
|
|
|
|
await ensureMinimumBlockHeight(256)
|
|
|
|
|
|
|
|
const TestToken = await ethers.getContractFactory("TestToken")
|
|
|
|
token = await TestToken.deploy()
|
|
|
|
|
2024-01-10 14:12:06 +00:00
|
|
|
const TestVerifier = await ethers.getContractFactory("TestVerifier")
|
|
|
|
verifier = await TestVerifier.deploy()
|
|
|
|
|
2023-03-30 09:11:21 +00:00
|
|
|
Marketplace = await ethers.getContractFactory("TestMarketplace")
|
|
|
|
config = exampleConfiguration()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(async function () {
|
|
|
|
await revert()
|
|
|
|
})
|
|
|
|
|
|
|
|
function testPercentageOverflow(property) {
|
|
|
|
it(`should reject for ${property} overflowing percentage values`, async () => {
|
|
|
|
config.collateral[property] = 101
|
|
|
|
|
2023-05-30 12:42:59 +00:00
|
|
|
await expect(
|
2024-01-10 14:12:06 +00:00
|
|
|
Marketplace.deploy(config, token.address, verifier.address)
|
2023-05-30 12:42:59 +00:00
|
|
|
).to.be.revertedWith("Must be less than 100")
|
2023-03-30 09:11:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-30 12:42:59 +00:00
|
|
|
testPercentageOverflow("repairRewardPercentage")
|
|
|
|
testPercentageOverflow("slashPercentage")
|
2023-03-30 09:11:21 +00:00
|
|
|
|
2023-05-30 12:42:59 +00:00
|
|
|
it("should reject when total slash percentage exceeds 100%", async () => {
|
2023-03-30 09:11:21 +00:00
|
|
|
config.collateral.slashPercentage = 1
|
|
|
|
config.collateral.maxNumberOfSlashes = 101
|
|
|
|
|
2024-01-10 14:12:06 +00:00
|
|
|
await expect(
|
|
|
|
Marketplace.deploy(config, token.address, verifier.address)
|
|
|
|
).to.be.revertedWith("Maximum slashing exceeds 100%")
|
2023-03-30 09:11:21 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
describe("Marketplace", function () {
|
2022-07-19 15:09:35 +00:00
|
|
|
const proof = hexlify(randomBytes(42))
|
2023-01-17 12:55:58 +00:00
|
|
|
const config = exampleConfiguration()
|
2022-02-16 09:50:00 +00:00
|
|
|
|
|
|
|
let marketplace
|
|
|
|
let token
|
2024-01-10 14:12:06 +00:00
|
|
|
let verifier
|
2022-02-21 10:31:37 +00:00
|
|
|
let client, host, host1, host2, host3
|
2022-06-13 10:17:37 +00:00
|
|
|
let request
|
2022-07-19 15:09:35 +00:00
|
|
|
let slot
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2023-01-24 04:59:56 +00:00
|
|
|
enableRequestAssertions()
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
beforeEach(async function () {
|
2022-06-13 08:40:18 +00:00
|
|
|
await snapshot()
|
|
|
|
await ensureMinimumBlockHeight(256)
|
2022-02-21 10:31:37 +00:00
|
|
|
;[client, host1, host2, host3] = await ethers.getSigners()
|
|
|
|
host = host1
|
2022-02-17 10:00:18 +00:00
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
const TestToken = await ethers.getContractFactory("TestToken")
|
|
|
|
token = await TestToken.deploy()
|
2023-10-16 09:14:02 +00:00
|
|
|
for (let account of [client, host1, host2, host3]) {
|
|
|
|
await token.mint(account.address, ACCOUNT_STARTING_BALANCE)
|
2022-02-21 10:31:37 +00:00
|
|
|
}
|
2022-02-17 10:00:18 +00:00
|
|
|
|
2024-01-10 14:12:06 +00:00
|
|
|
const TestVerifier = await ethers.getContractFactory("TestVerifier")
|
|
|
|
verifier = await TestVerifier.deploy()
|
|
|
|
|
2022-09-08 07:56:01 +00:00
|
|
|
const Marketplace = await ethers.getContractFactory("TestMarketplace")
|
2024-01-10 14:12:06 +00:00
|
|
|
marketplace = await Marketplace.deploy(
|
|
|
|
config,
|
|
|
|
token.address,
|
|
|
|
verifier.address
|
|
|
|
)
|
2022-02-17 10:00:18 +00:00
|
|
|
|
2022-09-22 02:21:49 +00:00
|
|
|
request = await exampleRequest()
|
2022-02-17 10:00:18 +00:00
|
|
|
request.client = client.address
|
2022-07-19 15:09:35 +00:00
|
|
|
|
|
|
|
slot = {
|
|
|
|
request: requestId(request),
|
2022-07-20 08:44:22 +00:00
|
|
|
index: request.ask.slots / 2,
|
2022-07-19 15:09:35 +00:00
|
|
|
}
|
2022-02-16 09:50:00 +00:00
|
|
|
})
|
|
|
|
|
2022-06-13 08:40:18 +00:00
|
|
|
afterEach(async function () {
|
|
|
|
await revert()
|
|
|
|
})
|
|
|
|
|
2022-02-17 10:00:18 +00:00
|
|
|
function switchAccount(account) {
|
|
|
|
token = token.connect(account)
|
|
|
|
marketplace = marketplace.connect(account)
|
|
|
|
}
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
describe("requesting storage", function () {
|
2022-02-17 10:00:18 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
switchAccount(client)
|
|
|
|
})
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
it("emits event when storage is requested", async function () {
|
2022-07-20 09:07:20 +00:00
|
|
|
await token.approve(marketplace.address, price(request))
|
2022-02-16 09:50:00 +00:00
|
|
|
await expect(marketplace.requestStorage(request))
|
|
|
|
.to.emit(marketplace, "StorageRequested")
|
2023-06-21 20:18:33 +00:00
|
|
|
.withArgs(requestId(request), askToArray(request.ask), request.expiry)
|
2022-02-16 09:50:00 +00:00
|
|
|
})
|
|
|
|
|
2023-01-09 12:38:12 +00:00
|
|
|
it("allows retrieval of request details", async function () {
|
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
const id = requestId(request)
|
2023-01-24 04:59:56 +00:00
|
|
|
expect(await marketplace.getRequest(id)).to.be.request(request)
|
2023-01-09 12:38:12 +00:00
|
|
|
})
|
|
|
|
|
2022-02-17 10:00:18 +00:00
|
|
|
it("rejects request with invalid client address", async function () {
|
|
|
|
let invalid = { ...request, client: host.address }
|
2022-07-20 09:07:20 +00:00
|
|
|
await token.approve(marketplace.address, price(invalid))
|
2022-02-17 10:00:18 +00:00
|
|
|
await expect(marketplace.requestStorage(invalid)).to.be.revertedWith(
|
|
|
|
"Invalid client address"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
it("rejects request with insufficient payment", async function () {
|
2022-07-20 09:07:20 +00:00
|
|
|
let insufficient = price(request) - 1
|
2022-02-16 09:50:00 +00:00
|
|
|
await token.approve(marketplace.address, insufficient)
|
|
|
|
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
2022-03-15 15:18:44 +00:00
|
|
|
"ERC20: insufficient allowance"
|
2022-02-16 09:50:00 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-12-05 10:18:43 +00:00
|
|
|
it("rejects request when expiry is after request end", async function () {
|
2024-01-10 14:12:18 +00:00
|
|
|
request.expiry = (await currentTime()) + request.ask.duration + hours(1)
|
2023-12-05 10:18:43 +00:00
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
|
|
|
"Request end before expiry"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
it("rejects resubmission of request", async function () {
|
2022-07-20 09:07:20 +00:00
|
|
|
await token.approve(marketplace.address, price(request) * 2)
|
2022-02-16 09:50:00 +00:00
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
|
|
|
"Request already exists"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2022-02-16 13:38:19 +00:00
|
|
|
|
2023-03-08 11:02:34 +00:00
|
|
|
describe("filling a slot with collateral", function () {
|
2022-07-19 09:33:54 +00:00
|
|
|
beforeEach(async function () {
|
|
|
|
switchAccount(client)
|
2022-07-20 09:07:20 +00:00
|
|
|
await token.approve(marketplace.address, price(request))
|
2022-07-19 09:33:54 +00:00
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-07-19 09:33:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("emits event when slot is filled", async function () {
|
|
|
|
await expect(marketplace.fillSlot(slot.request, slot.index, proof))
|
|
|
|
.to.emit(marketplace, "SlotFilled")
|
2023-06-21 20:18:33 +00:00
|
|
|
.withArgs(slot.request, slot.index)
|
2022-07-19 09:33:54 +00:00
|
|
|
})
|
|
|
|
|
2023-01-09 12:38:12 +00:00
|
|
|
it("allows retrieval of host that filled slot", async function () {
|
|
|
|
expect(await marketplace.getHost(slotId(slot))).to.equal(AddressZero)
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
expect(await marketplace.getHost(slotId(slot))).to.equal(host.address)
|
|
|
|
})
|
|
|
|
|
2023-01-24 04:59:56 +00:00
|
|
|
it("fails to retrieve a request of an empty slot", async function () {
|
2023-04-13 23:28:39 +00:00
|
|
|
expect(marketplace.getActiveSlot(slotId(slot))).to.be.revertedWith(
|
2023-01-24 04:59:56 +00:00
|
|
|
"Slot is free"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("allows retrieval of request of a filled slot", async function () {
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
2023-04-13 23:28:39 +00:00
|
|
|
let activeSlot = await marketplace.getActiveSlot(slotId(slot))
|
|
|
|
expect(activeSlot.request).to.be.request(request)
|
|
|
|
expect(activeSlot.slotIndex).to.equal(slot.index)
|
2023-01-24 04:59:56 +00:00
|
|
|
})
|
|
|
|
|
2022-07-19 09:33:54 +00:00
|
|
|
it("is rejected when proof is incorrect", async function () {
|
|
|
|
let invalid = hexlify([])
|
|
|
|
await expect(
|
|
|
|
marketplace.fillSlot(slot.request, slot.index, invalid)
|
|
|
|
).to.be.revertedWith("Invalid proof")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("is rejected when slot already filled", async function () {
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await expect(
|
|
|
|
marketplace.fillSlot(slot.request, slot.index, proof)
|
2023-01-16 16:26:11 +00:00
|
|
|
).to.be.revertedWith("Slot is not free")
|
2022-07-19 09:33:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("is rejected when request is unknown", async function () {
|
2022-09-22 02:21:49 +00:00
|
|
|
let unknown = await exampleRequest()
|
2022-07-19 09:33:54 +00:00
|
|
|
await expect(
|
|
|
|
marketplace.fillSlot(requestId(unknown), 0, proof)
|
|
|
|
).to.be.revertedWith("Unknown request")
|
|
|
|
})
|
|
|
|
|
2022-09-21 09:13:12 +00:00
|
|
|
it("is rejected when request is cancelled", async function () {
|
2022-07-19 09:33:54 +00:00
|
|
|
switchAccount(client)
|
2022-09-22 02:21:49 +00:00
|
|
|
let expired = { ...request, expiry: (await currentTime()) - hours(1) }
|
2022-07-20 09:07:20 +00:00
|
|
|
await token.approve(marketplace.address, price(request))
|
2022-07-19 09:33:54 +00:00
|
|
|
await marketplace.requestStorage(expired)
|
|
|
|
switchAccount(host)
|
|
|
|
await expect(
|
|
|
|
marketplace.fillSlot(requestId(expired), slot.index, proof)
|
2023-01-16 16:26:11 +00:00
|
|
|
).to.be.revertedWith("Slot is not free")
|
2022-07-19 09:33:54 +00:00
|
|
|
})
|
|
|
|
|
2022-09-21 09:57:26 +00:00
|
|
|
it("is rejected when request is finished", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
|
|
|
await waitUntilFinished(marketplace, slot.request)
|
2022-09-21 09:57:26 +00:00
|
|
|
await expect(
|
|
|
|
marketplace.fillSlot(slot.request, slot.index, proof)
|
2023-01-16 16:26:11 +00:00
|
|
|
).to.be.revertedWith("Slot is not free")
|
2022-09-21 09:57:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("is rejected when request is failed", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-12-19 10:50:37 +00:00
|
|
|
await waitUntilFailed(marketplace, request)
|
2022-09-21 09:57:26 +00:00
|
|
|
await expect(
|
|
|
|
marketplace.fillSlot(slot.request, slot.index, proof)
|
2023-01-16 16:26:11 +00:00
|
|
|
).to.be.revertedWith("Slot is not free")
|
2022-09-21 09:57:26 +00:00
|
|
|
})
|
|
|
|
|
2022-07-19 09:33:54 +00:00
|
|
|
it("is rejected when slot index not in range", async function () {
|
2022-07-20 08:44:22 +00:00
|
|
|
const invalid = request.ask.slots
|
2022-07-19 09:33:54 +00:00
|
|
|
await expect(
|
|
|
|
marketplace.fillSlot(slot.request, invalid, proof)
|
|
|
|
).to.be.revertedWith("Invalid slot")
|
|
|
|
})
|
2022-09-07 05:25:01 +00:00
|
|
|
|
|
|
|
it("fails when all slots are already filled", async function () {
|
|
|
|
const lastSlot = request.ask.slots - 1
|
2023-05-30 12:42:59 +00:00
|
|
|
await token.approve(
|
|
|
|
marketplace.address,
|
|
|
|
request.ask.collateral * lastSlot
|
|
|
|
)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, price(request) * lastSlot)
|
2022-09-07 05:25:01 +00:00
|
|
|
for (let i = 0; i <= lastSlot; i++) {
|
|
|
|
await marketplace.fillSlot(slot.request, i, proof)
|
|
|
|
}
|
|
|
|
await expect(
|
|
|
|
marketplace.fillSlot(slot.request, lastSlot, proof)
|
2023-01-16 16:26:11 +00:00
|
|
|
).to.be.revertedWith("Slot is not free")
|
2022-09-07 05:25:01 +00:00
|
|
|
})
|
2022-09-23 02:33:39 +00:00
|
|
|
})
|
|
|
|
|
2023-03-08 11:02:34 +00:00
|
|
|
describe("filling slot without collateral", function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
switchAccount(client)
|
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("is rejected when approved collateral is insufficient", async function () {
|
|
|
|
let insufficient = request.ask.collateral - 1
|
|
|
|
await token.approve(marketplace.address, insufficient)
|
|
|
|
await expect(
|
|
|
|
marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
).to.be.revertedWith("ERC20: insufficient allowance")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("collects only requested collateral and not more", async function () {
|
2023-05-30 12:42:59 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral * 2)
|
2023-03-08 11:02:34 +00:00
|
|
|
const startBalanace = await token.balanceOf(host.address)
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
const endBalance = await token.balanceOf(host.address)
|
2023-05-30 12:42:59 +00:00
|
|
|
expect(startBalanace - endBalance).to.eq(request.ask.collateral)
|
2023-03-08 11:02:34 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-10-26 02:15:42 +00:00
|
|
|
describe("request end", function () {
|
|
|
|
var requestTime
|
|
|
|
beforeEach(async function () {
|
|
|
|
switchAccount(client)
|
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
requestTime = await currentTime()
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-10-26 02:15:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("sets the request end time to now + duration", async function () {
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await expect(
|
|
|
|
(await marketplace.requestEnd(requestId(request))).toNumber()
|
|
|
|
).to.be.closeTo(requestTime + request.ask.duration, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("sets request end time to the past once failed", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-12-19 10:50:37 +00:00
|
|
|
await waitUntilFailed(marketplace, request)
|
2022-10-26 02:15:42 +00:00
|
|
|
let slot0 = { ...slot, index: request.ask.maxSlotLoss + 1 }
|
|
|
|
const now = await currentTime()
|
|
|
|
await expect(await marketplace.requestEnd(requestId(request))).to.be.eq(
|
|
|
|
now - 1
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("sets request end time to the past once cancelled", async function () {
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilCancelled(request)
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2022-10-26 02:15:42 +00:00
|
|
|
const now = await currentTime()
|
|
|
|
await expect(await marketplace.requestEnd(requestId(request))).to.be.eq(
|
|
|
|
now - 1
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("checks that request end time is in the past once finished", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-10-26 02:15:42 +00:00
|
|
|
await waitUntilFinished(marketplace, requestId(request))
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2022-10-26 02:15:42 +00:00
|
|
|
const now = await currentTime()
|
2023-01-16 15:31:04 +00:00
|
|
|
// in the process of calling currentTime and requestEnd,
|
2022-10-26 02:15:42 +00:00
|
|
|
// block.timestamp has advanced by 1, so the expected proof end time will
|
|
|
|
// be block.timestamp - 1.
|
|
|
|
await expect(await marketplace.requestEnd(requestId(request))).to.be.eq(
|
|
|
|
now - 1
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-09-13 07:32:02 +00:00
|
|
|
describe("freeing a slot", function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
let id
|
|
|
|
|
2022-09-13 07:32:02 +00:00
|
|
|
beforeEach(async function () {
|
|
|
|
slot.index = 0
|
|
|
|
id = slotId(slot)
|
|
|
|
|
|
|
|
switchAccount(client)
|
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-09-13 07:32:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("fails to free slot when slot not filled", async function () {
|
|
|
|
slot.index = 5
|
|
|
|
let nonExistentId = slotId(slot)
|
|
|
|
await expect(marketplace.freeSlot(nonExistentId)).to.be.revertedWith(
|
2023-01-17 08:05:44 +00:00
|
|
|
"Slot is free"
|
2022-09-13 07:32:02 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-12-19 10:50:37 +00:00
|
|
|
it("can only be freed by the host occupying the slot", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-12-19 10:50:37 +00:00
|
|
|
switchAccount(client)
|
|
|
|
await expect(marketplace.freeSlot(id)).to.be.revertedWith(
|
|
|
|
"Slot filled by other host"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-09-13 07:32:02 +00:00
|
|
|
it("successfully frees slot", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-09-13 07:32:02 +00:00
|
|
|
await expect(marketplace.freeSlot(id)).not.to.be.reverted
|
|
|
|
})
|
|
|
|
|
|
|
|
it("emits event once slot is freed", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-09-13 07:32:02 +00:00
|
|
|
await expect(await marketplace.freeSlot(id))
|
|
|
|
.to.emit(marketplace, "SlotFreed")
|
2023-06-21 20:18:33 +00:00
|
|
|
.withArgs(slot.request, slot.index)
|
2022-09-13 07:32:02 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-07-19 15:09:35 +00:00
|
|
|
describe("paying out a slot", function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
switchAccount(client)
|
2022-07-20 09:07:20 +00:00
|
|
|
await token.approve(marketplace.address, price(request))
|
2022-07-19 15:09:35 +00:00
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-07-19 15:09:35 +00:00
|
|
|
})
|
|
|
|
|
2023-03-08 11:02:34 +00:00
|
|
|
it("pays the host when contract has finished and returns collateral", async function () {
|
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-10-25 12:49:37 +00:00
|
|
|
await waitUntilFinished(marketplace, requestId(request))
|
2022-07-19 15:09:35 +00:00
|
|
|
const startBalance = await token.balanceOf(host.address)
|
2022-11-23 14:29:58 +00:00
|
|
|
await marketplace.freeSlot(slotId(slot))
|
2022-07-19 15:09:35 +00:00
|
|
|
const endBalance = await token.balanceOf(host.address)
|
2023-05-30 12:42:59 +00:00
|
|
|
expect(endBalance - startBalance).to.equal(
|
|
|
|
pricePerSlot(request) + request.ask.collateral
|
|
|
|
)
|
2022-07-19 15:09:35 +00:00
|
|
|
})
|
|
|
|
|
2022-12-19 10:50:37 +00:00
|
|
|
it("pays the host when contract was cancelled", async function () {
|
2023-10-16 09:14:02 +00:00
|
|
|
// Lets move the time into middle of the expiry window
|
2024-01-10 14:12:18 +00:00
|
|
|
const fillTimestamp =
|
|
|
|
(await currentTime()) +
|
|
|
|
Math.floor((request.expiry - (await currentTime())) / 2) -
|
|
|
|
1
|
2023-10-16 09:14:02 +00:00
|
|
|
await advanceTimeToForNextBlock(fillTimestamp)
|
|
|
|
|
2022-12-19 10:50:37 +00:00
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilCancelled(request)
|
|
|
|
await marketplace.freeSlot(slotId(slot))
|
2023-10-16 09:14:02 +00:00
|
|
|
|
2022-12-19 10:50:37 +00:00
|
|
|
const endBalance = await token.balanceOf(host.address)
|
2024-01-10 14:12:18 +00:00
|
|
|
const expectedPartialPayout =
|
|
|
|
(request.expiry - fillTimestamp) * request.ask.reward
|
|
|
|
expect(endBalance - ACCOUNT_STARTING_BALANCE).to.be.equal(
|
|
|
|
expectedPartialPayout
|
|
|
|
)
|
2022-12-19 10:50:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("does not pay when the contract hasn't ended", async function () {
|
2022-07-19 15:09:35 +00:00
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
2022-11-23 14:29:58 +00:00
|
|
|
const startBalance = await token.balanceOf(host.address)
|
|
|
|
await marketplace.freeSlot(slotId(slot))
|
|
|
|
const endBalance = await token.balanceOf(host.address)
|
|
|
|
expect(endBalance).to.equal(startBalance)
|
2022-07-19 15:09:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("can only be done once", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-10-25 12:49:37 +00:00
|
|
|
await waitUntilFinished(marketplace, requestId(request))
|
2022-11-23 14:29:58 +00:00
|
|
|
await marketplace.freeSlot(slotId(slot))
|
|
|
|
await expect(marketplace.freeSlot(slotId(slot))).to.be.revertedWith(
|
|
|
|
"Already paid"
|
|
|
|
)
|
2022-07-19 15:09:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot be filled again", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-10-25 12:49:37 +00:00
|
|
|
await waitUntilFinished(marketplace, requestId(request))
|
2022-11-23 14:29:58 +00:00
|
|
|
await marketplace.freeSlot(slotId(slot))
|
2022-07-19 15:09:35 +00:00
|
|
|
await expect(marketplace.fillSlot(slot.request, slot.index, proof)).to.be
|
|
|
|
.reverted
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-07-20 08:29:24 +00:00
|
|
|
describe("fulfilling a request", function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
switchAccount(client)
|
2022-07-20 09:07:20 +00:00
|
|
|
await token.approve(marketplace.address, price(request))
|
2022-07-20 08:29:24 +00:00
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-07-20 08:29:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("emits event when all slots are filled", async function () {
|
2022-07-20 08:44:22 +00:00
|
|
|
const lastSlot = request.ask.slots - 1
|
2023-05-30 12:42:59 +00:00
|
|
|
await token.approve(
|
|
|
|
marketplace.address,
|
|
|
|
request.ask.collateral * lastSlot
|
|
|
|
)
|
2022-07-20 08:29:24 +00:00
|
|
|
for (let i = 0; i < lastSlot; i++) {
|
|
|
|
await marketplace.fillSlot(slot.request, i, proof)
|
|
|
|
}
|
2023-03-08 11:02:34 +00:00
|
|
|
|
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-07-20 08:29:24 +00:00
|
|
|
await expect(marketplace.fillSlot(slot.request, lastSlot, proof))
|
|
|
|
.to.emit(marketplace, "RequestFulfilled")
|
|
|
|
.withArgs(requestId(request))
|
|
|
|
})
|
2022-08-03 09:54:29 +00:00
|
|
|
it("sets state when all slots are filled", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
const slots = request.ask.slots
|
|
|
|
await token.approve(marketplace.address, request.ask.collateral * slots)
|
|
|
|
for (let i = 0; i < slots; i++) {
|
2022-08-03 09:54:29 +00:00
|
|
|
await marketplace.fillSlot(slot.request, i, proof)
|
|
|
|
}
|
2023-01-16 13:28:42 +00:00
|
|
|
await expect(await marketplace.requestState(slot.request)).to.equal(
|
2022-09-22 02:21:49 +00:00
|
|
|
RequestState.Started
|
|
|
|
)
|
2022-09-21 09:57:26 +00:00
|
|
|
})
|
|
|
|
it("fails when all slots are already filled", async function () {
|
|
|
|
const lastSlot = request.ask.slots - 1
|
2023-05-30 12:42:59 +00:00
|
|
|
await token.approve(
|
|
|
|
marketplace.address,
|
|
|
|
request.ask.collateral * (lastSlot + 1)
|
|
|
|
)
|
2022-09-21 09:57:26 +00:00
|
|
|
for (let i = 0; i <= lastSlot; i++) {
|
|
|
|
await marketplace.fillSlot(slot.request, i, proof)
|
|
|
|
}
|
|
|
|
await expect(
|
|
|
|
marketplace.fillSlot(slot.request, lastSlot, proof)
|
2023-01-16 16:26:11 +00:00
|
|
|
).to.be.revertedWith("Slot is not free")
|
2022-08-04 02:14:36 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("withdrawing funds", function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
switchAccount(client)
|
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-08-04 02:14:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("rejects withdraw when request not yet timed out", async function () {
|
|
|
|
switchAccount(client)
|
|
|
|
await expect(marketplace.withdrawFunds(slot.request)).to.be.revertedWith(
|
|
|
|
"Request not yet timed out"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("rejects withdraw when wrong account used", async function () {
|
2022-09-23 02:33:39 +00:00
|
|
|
await waitUntilCancelled(request)
|
2022-08-04 02:14:36 +00:00
|
|
|
await expect(marketplace.withdrawFunds(slot.request)).to.be.revertedWith(
|
|
|
|
"Invalid client address"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("rejects withdraw when in wrong state", async function () {
|
|
|
|
// fill all slots, should change state to RequestState.Started
|
|
|
|
const lastSlot = request.ask.slots - 1
|
2023-05-30 12:42:59 +00:00
|
|
|
await token.approve(
|
|
|
|
marketplace.address,
|
|
|
|
request.ask.collateral * (lastSlot + 1)
|
|
|
|
)
|
2022-08-04 02:14:36 +00:00
|
|
|
for (let i = 0; i <= lastSlot; i++) {
|
|
|
|
await marketplace.fillSlot(slot.request, i, proof)
|
|
|
|
}
|
2022-09-23 02:33:39 +00:00
|
|
|
await waitUntilCancelled(request)
|
2022-08-04 02:14:36 +00:00
|
|
|
switchAccount(client)
|
|
|
|
await expect(marketplace.withdrawFunds(slot.request)).to.be.revertedWith(
|
|
|
|
"Invalid state"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("emits event once request is cancelled", async function () {
|
2022-09-23 02:33:39 +00:00
|
|
|
await waitUntilCancelled(request)
|
2022-08-04 02:14:36 +00:00
|
|
|
switchAccount(client)
|
|
|
|
await expect(marketplace.withdrawFunds(slot.request))
|
|
|
|
.to.emit(marketplace, "RequestCancelled")
|
|
|
|
.withArgs(requestId(request))
|
|
|
|
})
|
|
|
|
|
|
|
|
it("withdraws to the client", async function () {
|
2022-09-23 02:33:39 +00:00
|
|
|
await waitUntilCancelled(request)
|
2022-08-04 02:14:36 +00:00
|
|
|
switchAccount(client)
|
|
|
|
const startBalance = await token.balanceOf(client.address)
|
|
|
|
await marketplace.withdrawFunds(slot.request)
|
|
|
|
const endBalance = await token.balanceOf(client.address)
|
|
|
|
expect(endBalance - startBalance).to.equal(price(request))
|
|
|
|
})
|
2023-10-16 09:14:02 +00:00
|
|
|
|
|
|
|
it("withdraws to the client for cancelled requests lowered by hosts payout", async function () {
|
2024-01-10 14:12:18 +00:00
|
|
|
const fillTimestamp =
|
|
|
|
(await currentTime()) +
|
|
|
|
Math.floor((request.expiry - (await currentTime())) / 2)
|
2023-10-16 09:14:02 +00:00
|
|
|
await advanceTimeToForNextBlock(fillTimestamp)
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilCancelled(request)
|
2024-01-10 14:12:18 +00:00
|
|
|
const expectedPartialHostPayout =
|
|
|
|
(request.expiry - fillTimestamp) * request.ask.reward
|
2023-10-16 09:14:02 +00:00
|
|
|
|
|
|
|
switchAccount(client)
|
|
|
|
await marketplace.withdrawFunds(slot.request)
|
|
|
|
const endBalance = await token.balanceOf(client.address)
|
2024-01-10 14:12:18 +00:00
|
|
|
expect(ACCOUNT_STARTING_BALANCE - endBalance).to.equal(
|
|
|
|
expectedPartialHostPayout
|
|
|
|
)
|
2023-10-16 09:14:02 +00:00
|
|
|
})
|
2022-07-20 08:29:24 +00:00
|
|
|
})
|
2022-08-12 01:40:04 +00:00
|
|
|
|
2023-01-17 13:07:48 +00:00
|
|
|
describe("request state", function () {
|
|
|
|
const { New, Cancelled, Started, Failed, Finished } = RequestState
|
|
|
|
|
2022-08-12 01:40:04 +00:00
|
|
|
beforeEach(async function () {
|
|
|
|
switchAccount(client)
|
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-08-12 01:40:04 +00:00
|
|
|
})
|
|
|
|
|
2023-01-17 13:07:48 +00:00
|
|
|
it("is 'New' initially", async function () {
|
|
|
|
expect(await marketplace.requestState(slot.request)).to.equal(New)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("changes to 'Cancelled' once request is cancelled", async function () {
|
|
|
|
await waitUntilCancelled(request)
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2023-01-17 13:07:48 +00:00
|
|
|
expect(await marketplace.requestState(slot.request)).to.equal(Cancelled)
|
2022-09-21 09:57:26 +00:00
|
|
|
})
|
|
|
|
|
2023-01-17 13:07:48 +00:00
|
|
|
it("remains 'Cancelled' when client withdraws funds", async function () {
|
2022-09-23 02:33:39 +00:00
|
|
|
await waitUntilCancelled(request)
|
2022-08-12 01:40:04 +00:00
|
|
|
switchAccount(client)
|
|
|
|
await marketplace.withdrawFunds(slot.request)
|
2023-01-17 13:07:48 +00:00
|
|
|
expect(await marketplace.requestState(slot.request)).to.equal(Cancelled)
|
2022-08-12 01:40:04 +00:00
|
|
|
})
|
|
|
|
|
2023-01-17 13:07:48 +00:00
|
|
|
it("changes to 'Started' once all slots are filled", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2023-01-17 13:07:48 +00:00
|
|
|
expect(await marketplace.requestState(slot.request)).to.equal(Started)
|
2022-08-12 01:40:04 +00:00
|
|
|
})
|
2022-09-08 07:56:01 +00:00
|
|
|
|
2023-01-17 13:07:48 +00:00
|
|
|
it("changes to 'Failed' once too many slots are freed", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-12-19 10:50:37 +00:00
|
|
|
await waitUntilFailed(marketplace, request)
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2023-01-17 13:07:48 +00:00
|
|
|
expect(await marketplace.requestState(slot.request)).to.equal(Failed)
|
2022-09-13 07:20:07 +00:00
|
|
|
})
|
|
|
|
|
2023-01-17 13:07:48 +00:00
|
|
|
it("does not change to 'Failed' before it is started", async function () {
|
2023-05-30 12:42:59 +00:00
|
|
|
await token.approve(
|
|
|
|
marketplace.address,
|
|
|
|
request.ask.collateral * (request.ask.maxSlotLoss + 1)
|
|
|
|
)
|
2022-09-21 09:13:12 +00:00
|
|
|
for (let i = 0; i <= request.ask.maxSlotLoss; i++) {
|
|
|
|
await marketplace.fillSlot(slot.request, i, proof)
|
|
|
|
}
|
|
|
|
for (let i = 0; i <= request.ask.maxSlotLoss; i++) {
|
|
|
|
slot.index = i
|
|
|
|
let id = slotId(slot)
|
2022-11-23 14:29:58 +00:00
|
|
|
await marketplace.forciblyFreeSlot(id)
|
2022-09-21 09:13:12 +00:00
|
|
|
}
|
2023-01-17 13:07:48 +00:00
|
|
|
expect(await marketplace.requestState(slot.request)).to.equal(New)
|
2022-09-21 09:13:12 +00:00
|
|
|
})
|
|
|
|
|
2023-01-19 15:49:10 +00:00
|
|
|
it("changes to 'Finished' when the request ends", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2023-01-17 13:07:48 +00:00
|
|
|
await waitUntilFinished(marketplace, requestId(request))
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2023-01-17 13:07:48 +00:00
|
|
|
expect(await marketplace.requestState(slot.request)).to.equal(Finished)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("remains 'Finished' once a slot is paid out", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2023-01-17 13:07:48 +00:00
|
|
|
await waitUntilFinished(marketplace, requestId(request))
|
|
|
|
await marketplace.freeSlot(slotId(slot))
|
|
|
|
expect(await marketplace.requestState(slot.request)).to.equal(Finished)
|
2022-09-08 07:56:01 +00:00
|
|
|
})
|
2022-08-12 01:40:04 +00:00
|
|
|
})
|
2023-01-09 14:15:11 +00:00
|
|
|
|
2023-01-18 14:26:21 +00:00
|
|
|
describe("slot state", function () {
|
2023-10-16 09:14:02 +00:00
|
|
|
const { Free, Filled, Finished, Failed, Paid, Cancelled } = SlotState
|
2023-01-18 14:26:21 +00:00
|
|
|
let period, periodEnd
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
period = config.proofs.period
|
|
|
|
;({ periodOf, periodEnd } = periodic(period))
|
|
|
|
|
|
|
|
switchAccount(client)
|
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2023-01-18 14:26:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
async function waitUntilProofIsRequired(id) {
|
2023-10-16 09:14:02 +00:00
|
|
|
await advanceTimeToForNextBlock(periodEnd(periodOf(await currentTime())))
|
|
|
|
await mine()
|
2023-01-18 14:26:21 +00:00
|
|
|
while (
|
|
|
|
!(
|
|
|
|
(await marketplace.isProofRequired(id)) &&
|
|
|
|
(await marketplace.getPointer(id)) < 250
|
|
|
|
)
|
|
|
|
) {
|
2023-10-16 09:14:02 +00:00
|
|
|
await advanceTimeForNextBlock(period)
|
|
|
|
await mine()
|
2023-01-18 14:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it("is 'Free' initially", async function () {
|
|
|
|
expect(await marketplace.slotState(slotId(slot))).to.equal(Free)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("changes to 'Filled' when slot is filled", async function () {
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
expect(await marketplace.slotState(slotId(slot))).to.equal(Filled)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("changes to 'Finished' when request finishes", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2023-01-18 14:26:21 +00:00
|
|
|
await waitUntilFinished(marketplace, slot.request)
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2023-01-18 14:26:21 +00:00
|
|
|
expect(await marketplace.slotState(slotId(slot))).to.equal(Finished)
|
|
|
|
})
|
|
|
|
|
2023-10-16 09:14:02 +00:00
|
|
|
it("changes to 'Cancelled' when request is cancelled", async function () {
|
2023-01-18 14:26:21 +00:00
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilCancelled(request)
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
|
|
|
expect(await marketplace.slotState(slotId(slot))).to.equal(Cancelled)
|
2023-01-18 14:26:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("changes to 'Free' when host frees the slot", async function () {
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await marketplace.freeSlot(slotId(slot))
|
|
|
|
expect(await marketplace.slotState(slotId(slot))).to.equal(Free)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("changes to 'Free' when too many proofs are missed", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2023-01-18 14:26:21 +00:00
|
|
|
while ((await marketplace.slotState(slotId(slot))) === Filled) {
|
|
|
|
await waitUntilProofIsRequired(slotId(slot))
|
|
|
|
const missedPeriod = periodOf(await currentTime())
|
2023-10-16 09:14:02 +00:00
|
|
|
await advanceTimeForNextBlock(period)
|
|
|
|
await mine()
|
2023-01-18 14:26:21 +00:00
|
|
|
await marketplace.markProofAsMissing(slotId(slot), missedPeriod)
|
|
|
|
}
|
|
|
|
expect(await marketplace.slotState(slotId(slot))).to.equal(Free)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("changes to 'Failed' when request fails", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2023-01-18 14:26:21 +00:00
|
|
|
await waitUntilSlotFailed(marketplace, request, slot)
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2023-01-18 14:26:21 +00:00
|
|
|
expect(await marketplace.slotState(slotId(slot))).to.equal(Failed)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("changes to 'Paid' when host has been paid", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2023-01-18 14:26:21 +00:00
|
|
|
await waitUntilFinished(marketplace, slot.request)
|
|
|
|
await marketplace.freeSlot(slotId(slot))
|
|
|
|
expect(await marketplace.slotState(slotId(slot))).to.equal(Paid)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-01-09 14:15:11 +00:00
|
|
|
describe("proof requirements", function () {
|
|
|
|
let period, periodOf, periodEnd
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
2023-01-17 12:55:58 +00:00
|
|
|
period = config.proofs.period
|
2023-01-09 14:15:11 +00:00
|
|
|
;({ periodOf, periodEnd } = periodic(period))
|
|
|
|
|
|
|
|
switchAccount(client)
|
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2023-01-09 14:15:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
async function waitUntilProofWillBeRequired(id) {
|
|
|
|
while (!(await marketplace.willProofBeRequired(id))) {
|
|
|
|
await mine()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function waitUntilProofIsRequired(id) {
|
2023-10-16 09:14:02 +00:00
|
|
|
await advanceTimeToForNextBlock(periodEnd(periodOf(await currentTime())))
|
2023-01-09 14:15:11 +00:00
|
|
|
while (
|
|
|
|
!(
|
|
|
|
(await marketplace.isProofRequired(id)) &&
|
|
|
|
(await marketplace.getPointer(id)) < 250
|
|
|
|
)
|
2023-05-30 12:42:59 +00:00
|
|
|
) {
|
2023-10-16 09:14:02 +00:00
|
|
|
await advanceTimeForNextBlock(period)
|
|
|
|
await mine()
|
2023-01-09 14:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 15:31:04 +00:00
|
|
|
it("requires proofs when slot is filled", async function () {
|
|
|
|
const id = slotId(slot)
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilProofWillBeRequired(id)
|
|
|
|
})
|
|
|
|
|
2023-01-09 14:15:11 +00:00
|
|
|
it("will not require proofs once cancelled", async function () {
|
|
|
|
const id = slotId(slot)
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilProofWillBeRequired(id)
|
|
|
|
await expect(await marketplace.willProofBeRequired(id)).to.be.true
|
2023-10-16 09:14:02 +00:00
|
|
|
await waitUntilCancelled(request)
|
|
|
|
await mine()
|
2023-01-09 14:15:11 +00:00
|
|
|
await expect(await marketplace.willProofBeRequired(id)).to.be.false
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not require proofs once cancelled", async function () {
|
|
|
|
const id = slotId(slot)
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
await expect(await marketplace.isProofRequired(id)).to.be.true
|
2023-10-16 09:14:02 +00:00
|
|
|
await waitUntilCancelled(request)
|
|
|
|
await mine()
|
2023-01-09 14:15:11 +00:00
|
|
|
await expect(await marketplace.isProofRequired(id)).to.be.false
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not provide challenges once cancelled", async function () {
|
|
|
|
const id = slotId(slot)
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilProofIsRequired(id)
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2023-01-09 14:15:11 +00:00
|
|
|
const challenge1 = await marketplace.getChallenge(id)
|
|
|
|
expect(BigNumber.from(challenge1).gt(0))
|
2023-10-16 09:14:02 +00:00
|
|
|
await waitUntilCancelled(request)
|
|
|
|
await mine()
|
2023-01-09 14:15:11 +00:00
|
|
|
const challenge2 = await marketplace.getChallenge(id)
|
|
|
|
expect(BigNumber.from(challenge2).isZero())
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not provide pointer once cancelled", async function () {
|
|
|
|
const id = slotId(slot)
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilProofIsRequired(id)
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2023-01-09 14:15:11 +00:00
|
|
|
const challenge1 = await marketplace.getChallenge(id)
|
|
|
|
expect(BigNumber.from(challenge1).gt(0))
|
2023-10-16 09:14:02 +00:00
|
|
|
await waitUntilCancelled(request)
|
|
|
|
await mine()
|
2023-01-09 14:15:11 +00:00
|
|
|
const challenge2 = await marketplace.getChallenge(id)
|
|
|
|
expect(BigNumber.from(challenge2).isZero())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-01-09 14:52:27 +00:00
|
|
|
describe("missing proofs", function () {
|
|
|
|
let period, periodOf, periodEnd
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
2023-01-17 12:55:58 +00:00
|
|
|
period = config.proofs.period
|
2023-01-09 14:52:27 +00:00
|
|
|
;({ periodOf, periodEnd } = periodic(period))
|
|
|
|
|
|
|
|
switchAccount(client)
|
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2023-01-09 14:52:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
async function waitUntilProofIsRequired(id) {
|
2023-10-16 09:14:02 +00:00
|
|
|
await advanceTimeToForNextBlock(periodEnd(periodOf(await currentTime())))
|
|
|
|
await mine()
|
2023-01-09 14:52:27 +00:00
|
|
|
while (
|
|
|
|
!(
|
|
|
|
(await marketplace.isProofRequired(id)) &&
|
|
|
|
(await marketplace.getPointer(id)) < 250
|
|
|
|
)
|
2023-05-30 12:42:59 +00:00
|
|
|
) {
|
2023-10-16 09:14:02 +00:00
|
|
|
await advanceTimeForNextBlock(period)
|
|
|
|
await mine()
|
2023-01-09 14:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it("fails to mark proof as missing when cancelled", async function () {
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilCancelled(request)
|
|
|
|
let missedPeriod = periodOf(await currentTime())
|
|
|
|
await expect(
|
|
|
|
marketplace.markProofAsMissing(slotId(slot), missedPeriod)
|
|
|
|
).to.be.revertedWith("Slot not accepting proofs")
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("slashing when missing proofs", function () {
|
|
|
|
it("reduces collateral when too many proofs are missing", async function () {
|
|
|
|
const id = slotId(slot)
|
2023-03-08 11:02:34 +00:00
|
|
|
const { slashCriterion, slashPercentage } = config.collateral
|
2023-01-09 14:52:27 +00:00
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
2023-01-17 12:55:58 +00:00
|
|
|
for (let i = 0; i < slashCriterion; i++) {
|
2023-01-09 14:52:27 +00:00
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
let missedPeriod = periodOf(await currentTime())
|
2024-01-10 14:12:18 +00:00
|
|
|
await advanceTimeForNextBlock(period + 1)
|
2023-01-09 14:52:27 +00:00
|
|
|
await marketplace.markProofAsMissing(id, missedPeriod)
|
|
|
|
}
|
2023-05-30 12:42:59 +00:00
|
|
|
const expectedBalance =
|
|
|
|
(request.ask.collateral * (100 - slashPercentage)) / 100
|
2023-03-08 11:02:34 +00:00
|
|
|
|
2023-05-30 12:42:59 +00:00
|
|
|
expect(
|
|
|
|
BigNumber.from(expectedBalance).eq(
|
|
|
|
await marketplace.getSlotCollateral(id)
|
|
|
|
)
|
|
|
|
)
|
2023-01-09 14:52:27 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-01-18 14:35:01 +00:00
|
|
|
it("frees slot when collateral slashed below minimum threshold", async function () {
|
2023-05-30 12:42:59 +00:00
|
|
|
const minimum =
|
|
|
|
request.ask.collateral -
|
|
|
|
(request.ask.collateral *
|
|
|
|
config.collateral.maxNumberOfSlashes *
|
|
|
|
config.collateral.slashPercentage) /
|
|
|
|
100
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2023-01-18 14:35:01 +00:00
|
|
|
while ((await marketplace.slotState(slotId(slot))) === SlotState.Filled) {
|
2023-05-30 12:42:59 +00:00
|
|
|
expect(await marketplace.getSlotCollateral(slotId(slot))).to.be.gt(
|
|
|
|
minimum
|
|
|
|
)
|
2023-01-18 14:35:01 +00:00
|
|
|
await waitUntilProofIsRequired(slotId(slot))
|
|
|
|
const missedPeriod = periodOf(await currentTime())
|
2024-01-10 14:12:18 +00:00
|
|
|
await advanceTimeForNextBlock(period + 1)
|
2023-01-18 14:35:01 +00:00
|
|
|
await marketplace.markProofAsMissing(slotId(slot), missedPeriod)
|
|
|
|
}
|
|
|
|
expect(await marketplace.slotState(slotId(slot))).to.equal(SlotState.Free)
|
2023-05-30 12:42:59 +00:00
|
|
|
expect(await marketplace.getSlotCollateral(slotId(slot))).to.be.lte(
|
|
|
|
minimum
|
|
|
|
)
|
2023-01-09 14:52:27 +00:00
|
|
|
})
|
2023-03-08 16:19:49 +00:00
|
|
|
|
|
|
|
it("free slot when minimum reached and resets missed proof counter", async function () {
|
2023-05-30 12:42:59 +00:00
|
|
|
const minimum =
|
|
|
|
request.ask.collateral -
|
|
|
|
(request.ask.collateral *
|
|
|
|
config.collateral.maxNumberOfSlashes *
|
|
|
|
config.collateral.slashPercentage) /
|
|
|
|
100
|
2023-03-08 16:19:49 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
|
|
|
let missedProofs = 0
|
|
|
|
while ((await marketplace.slotState(slotId(slot))) === SlotState.Filled) {
|
2023-05-30 12:42:59 +00:00
|
|
|
expect(await marketplace.getSlotCollateral(slotId(slot))).to.be.gt(
|
|
|
|
minimum
|
|
|
|
)
|
2023-03-08 16:19:49 +00:00
|
|
|
await waitUntilProofIsRequired(slotId(slot))
|
|
|
|
const missedPeriod = periodOf(await currentTime())
|
2024-01-10 14:12:18 +00:00
|
|
|
await advanceTimeForNextBlock(period + 1)
|
2023-05-30 12:42:59 +00:00
|
|
|
expect(await marketplace.missingProofs(slotId(slot))).to.equal(
|
|
|
|
missedProofs
|
|
|
|
)
|
2023-03-08 16:19:49 +00:00
|
|
|
await marketplace.markProofAsMissing(slotId(slot), missedPeriod)
|
|
|
|
missedProofs += 1
|
|
|
|
}
|
|
|
|
expect(await marketplace.slotState(slotId(slot))).to.equal(SlotState.Free)
|
|
|
|
expect(await marketplace.missingProofs(slotId(slot))).to.equal(0)
|
2023-05-30 12:42:59 +00:00
|
|
|
expect(await marketplace.getSlotCollateral(slotId(slot))).to.be.lte(
|
|
|
|
minimum
|
|
|
|
)
|
2023-03-08 16:19:49 +00:00
|
|
|
})
|
2023-01-09 14:52:27 +00:00
|
|
|
})
|
|
|
|
|
2022-10-25 02:06:52 +00:00
|
|
|
describe("list of active requests", function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-10-25 02:06:52 +00:00
|
|
|
switchAccount(client)
|
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
})
|
|
|
|
|
|
|
|
it("adds request to list when requesting storage", async function () {
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
expect(await marketplace.myRequests()).to.deep.equal([requestId(request)])
|
|
|
|
})
|
|
|
|
|
2022-11-23 11:39:49 +00:00
|
|
|
it("keeps request in list when cancelled", async function () {
|
2022-11-22 13:14:39 +00:00
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
await waitUntilCancelled(request)
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2022-11-23 11:39:49 +00:00
|
|
|
expect(await marketplace.myRequests()).to.deep.equal([requestId(request)])
|
2022-11-22 13:14:39 +00:00
|
|
|
})
|
|
|
|
|
2022-10-25 02:06:52 +00:00
|
|
|
it("removes request from list when funds are withdrawn", async function () {
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
await waitUntilCancelled(request)
|
|
|
|
await marketplace.withdrawFunds(requestId(request))
|
|
|
|
expect(await marketplace.myRequests()).to.deep.equal([])
|
|
|
|
})
|
|
|
|
|
2022-11-23 11:39:49 +00:00
|
|
|
it("keeps request in list when request fails", async function () {
|
2022-10-25 02:06:52 +00:00
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-12-19 10:50:37 +00:00
|
|
|
await waitUntilFailed(marketplace, request)
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2022-10-25 02:06:52 +00:00
|
|
|
switchAccount(client)
|
2022-11-23 11:39:49 +00:00
|
|
|
expect(await marketplace.myRequests()).to.deep.equal([requestId(request)])
|
2022-10-25 02:06:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("removes request from list when request finishes", async function () {
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-10-25 12:49:37 +00:00
|
|
|
await waitUntilFinished(marketplace, requestId(request))
|
2022-11-23 14:29:58 +00:00
|
|
|
await marketplace.freeSlot(slotId(slot))
|
2022-10-25 02:06:52 +00:00
|
|
|
switchAccount(client)
|
|
|
|
expect(await marketplace.myRequests()).to.deep.equal([])
|
|
|
|
})
|
|
|
|
})
|
2022-11-11 02:33:02 +00:00
|
|
|
|
|
|
|
describe("list of active slots", function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
switchAccount(client)
|
|
|
|
await token.approve(marketplace.address, price(request))
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
switchAccount(host)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-11-11 02:33:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("adds slot to list when filling slot", async function () {
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
let slot1 = { ...slot, index: slot.index + 1 }
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-11-11 02:33:02 +00:00
|
|
|
await marketplace.fillSlot(slot.request, slot1.index, proof)
|
2022-11-23 11:25:47 +00:00
|
|
|
expect(await marketplace.mySlots()).to.have.members([
|
2022-11-11 02:33:02 +00:00
|
|
|
slotId(slot),
|
|
|
|
slotId(slot1),
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2022-11-23 13:18:42 +00:00
|
|
|
it("removes slot from list when slot is freed", async function () {
|
2022-11-11 02:33:02 +00:00
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
let slot1 = { ...slot, index: slot.index + 1 }
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-11-11 02:33:02 +00:00
|
|
|
await marketplace.fillSlot(slot.request, slot1.index, proof)
|
2023-03-08 11:02:34 +00:00
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-11-11 02:33:02 +00:00
|
|
|
await marketplace.freeSlot(slotId(slot))
|
2022-11-23 11:25:47 +00:00
|
|
|
expect(await marketplace.mySlots()).to.have.members([slotId(slot1)])
|
2022-11-11 02:33:02 +00:00
|
|
|
})
|
|
|
|
|
2022-11-23 13:18:42 +00:00
|
|
|
it("keeps slots when cancelled", async function () {
|
2022-11-22 13:14:39 +00:00
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
let slot1 = { ...slot, index: slot.index + 1 }
|
2023-03-08 11:02:34 +00:00
|
|
|
|
|
|
|
await token.approve(marketplace.address, request.ask.collateral)
|
2022-11-22 13:14:39 +00:00
|
|
|
await marketplace.fillSlot(slot.request, slot1.index, proof)
|
|
|
|
await waitUntilCancelled(request)
|
2023-10-16 09:14:02 +00:00
|
|
|
await mine()
|
2022-11-23 13:18:42 +00:00
|
|
|
expect(await marketplace.mySlots()).to.have.members([
|
|
|
|
slotId(slot),
|
|
|
|
slotId(slot1),
|
|
|
|
])
|
2022-11-22 13:14:39 +00:00
|
|
|
})
|
|
|
|
|
2022-12-19 10:50:37 +00:00
|
|
|
it("removes slot when finished slot is freed", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-11-11 02:33:02 +00:00
|
|
|
await waitUntilFinished(marketplace, requestId(request))
|
2022-11-23 14:29:58 +00:00
|
|
|
await marketplace.freeSlot(slotId(slot))
|
2022-11-23 11:25:47 +00:00
|
|
|
expect(await marketplace.mySlots()).to.not.contain(slotId(slot))
|
2022-11-11 02:33:02 +00:00
|
|
|
})
|
2022-11-28 10:51:19 +00:00
|
|
|
|
2022-12-19 10:50:37 +00:00
|
|
|
it("removes slot when cancelled slot is freed", async function () {
|
|
|
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilCancelled(request)
|
|
|
|
await marketplace.freeSlot(slotId(slot))
|
|
|
|
expect(await marketplace.mySlots()).to.not.contain(slotId(slot))
|
|
|
|
})
|
|
|
|
|
|
|
|
it("removes slot when failed slot is freed", async function () {
|
2023-03-08 11:02:34 +00:00
|
|
|
await waitUntilStarted(marketplace, request, proof, token)
|
2022-12-19 10:50:37 +00:00
|
|
|
await waitUntilSlotFailed(marketplace, request, slot)
|
|
|
|
await marketplace.freeSlot(slotId(slot))
|
2022-11-28 10:51:19 +00:00
|
|
|
expect(await marketplace.mySlots()).to.not.contain(slotId(slot))
|
|
|
|
})
|
2022-11-11 02:33:02 +00:00
|
|
|
})
|
2022-02-16 09:50:00 +00:00
|
|
|
})
|