2021-10-12 14:59:34 +00:00
|
|
|
const { expect } = require("chai")
|
2021-11-16 13:26:28 +00:00
|
|
|
const { ethers, deployments } = require("hardhat")
|
2022-02-22 08:25:42 +00:00
|
|
|
const { exampleRequest, exampleOffer } = require("./examples")
|
2022-03-15 15:45:35 +00:00
|
|
|
const { advanceTime, advanceTimeTo, currentTime } = require("./evm")
|
2022-02-22 08:25:42 +00:00
|
|
|
const { requestId, offerId } = require("./ids")
|
2022-03-09 10:11:01 +00:00
|
|
|
const { periodic } = require("./time")
|
2021-10-12 14:59:34 +00:00
|
|
|
|
2021-11-01 15:34:01 +00:00
|
|
|
describe("Storage", function () {
|
2021-11-02 10:19:52 +00:00
|
|
|
let storage
|
|
|
|
let token
|
|
|
|
let client, host
|
2022-02-22 08:25:42 +00:00
|
|
|
let request, offer
|
2022-02-15 16:54:19 +00:00
|
|
|
let collateralAmount, slashMisses, slashPercentage
|
2022-02-22 08:25:42 +00:00
|
|
|
let id
|
|
|
|
|
|
|
|
function switchAccount(account) {
|
|
|
|
token = token.connect(account)
|
|
|
|
storage = storage.connect(account)
|
|
|
|
}
|
2021-11-02 10:19:52 +00:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
2022-02-09 13:17:23 +00:00
|
|
|
;[client, host] = await ethers.getSigners()
|
2022-02-22 08:25:42 +00:00
|
|
|
|
2022-02-09 13:17:23 +00:00
|
|
|
await deployments.fixture(["TestToken", "Storage"])
|
|
|
|
token = await ethers.getContract("TestToken")
|
|
|
|
storage = await ethers.getContract("Storage")
|
2022-02-22 08:25:42 +00:00
|
|
|
|
2021-11-23 13:34:27 +00:00
|
|
|
await token.mint(client.address, 1000)
|
|
|
|
await token.mint(host.address, 1000)
|
2022-02-22 08:25:42 +00:00
|
|
|
|
2022-02-15 16:54:19 +00:00
|
|
|
collateralAmount = await storage.collateralAmount()
|
2021-11-16 13:26:28 +00:00
|
|
|
slashMisses = await storage.slashMisses()
|
|
|
|
slashPercentage = await storage.slashPercentage()
|
2022-02-22 08:25:42 +00:00
|
|
|
|
|
|
|
request = exampleRequest()
|
|
|
|
request.client = client.address
|
|
|
|
|
|
|
|
offer = exampleOffer()
|
|
|
|
offer.host = host.address
|
|
|
|
offer.requestId = requestId(request)
|
|
|
|
|
|
|
|
switchAccount(client)
|
2022-04-06 12:26:56 +00:00
|
|
|
await token.approve(storage.address, request.ask.maxPrice)
|
2022-02-22 08:25:42 +00:00
|
|
|
await storage.requestStorage(request)
|
|
|
|
switchAccount(host)
|
|
|
|
await token.approve(storage.address, collateralAmount)
|
|
|
|
await storage.deposit(collateralAmount)
|
|
|
|
await storage.offerStorage(offer)
|
|
|
|
switchAccount(client)
|
|
|
|
await storage.selectOffer(offerId(offer))
|
|
|
|
id = offerId(offer)
|
2021-11-02 10:19:52 +00:00
|
|
|
})
|
2021-10-12 14:59:34 +00:00
|
|
|
|
2022-02-22 08:25:42 +00:00
|
|
|
describe("starting the contract", function () {
|
|
|
|
it("starts requiring storage proofs", async function () {
|
|
|
|
switchAccount(host)
|
|
|
|
await storage.startContract(id)
|
|
|
|
expect(await storage.proofEnd(id)).to.be.gt(0)
|
|
|
|
})
|
2021-10-12 14:59:34 +00:00
|
|
|
|
2022-02-22 08:25:42 +00:00
|
|
|
it("can only be done by the host", async function () {
|
|
|
|
switchAccount(client)
|
|
|
|
await expect(storage.startContract(id)).to.be.revertedWith(
|
|
|
|
"Only host can call this function"
|
2021-10-12 14:59:34 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-02-22 08:25:42 +00:00
|
|
|
it("can only be done once", async function () {
|
|
|
|
switchAccount(host)
|
|
|
|
await storage.startContract(id)
|
|
|
|
await expect(storage.startContract(id)).to.be.reverted
|
2022-02-09 13:17:23 +00:00
|
|
|
})
|
2022-03-22 10:39:45 +00:00
|
|
|
|
|
|
|
it("can only be done for a selected offer", async function () {
|
|
|
|
switchAccount(host)
|
|
|
|
const differentOffer = { ...offer, price: offer.price * 2 }
|
|
|
|
await storage.offerStorage(differentOffer)
|
|
|
|
await expect(
|
|
|
|
storage.startContract(offerId(differentOffer))
|
|
|
|
).to.be.revertedWith("Offer was not selected")
|
|
|
|
})
|
2022-02-22 08:25:42 +00:00
|
|
|
})
|
2021-11-02 11:50:06 +00:00
|
|
|
|
2022-02-22 08:25:42 +00:00
|
|
|
describe("finishing the contract", function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
switchAccount(host)
|
2021-11-02 11:50:06 +00:00
|
|
|
})
|
2021-11-04 08:49:07 +00:00
|
|
|
|
2022-03-08 14:58:08 +00:00
|
|
|
async function waitUntilEnd() {
|
|
|
|
const end = (await storage.proofEnd(id)).toNumber()
|
|
|
|
await advanceTimeTo(end)
|
2022-02-22 08:25:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 10:33:54 +00:00
|
|
|
it("unlocks the host collateral", async function () {
|
2022-03-22 11:32:14 +00:00
|
|
|
await storage.startContract(id)
|
2022-03-09 10:33:54 +00:00
|
|
|
await waitUntilEnd()
|
|
|
|
await storage.finishContract(id)
|
|
|
|
await expect(storage.withdraw()).not.to.be.reverted
|
|
|
|
})
|
2022-02-22 08:55:44 +00:00
|
|
|
|
2022-02-22 08:25:42 +00:00
|
|
|
it("pays the host", async function () {
|
2022-03-22 11:32:14 +00:00
|
|
|
await storage.startContract(id)
|
2022-03-08 14:58:08 +00:00
|
|
|
await waitUntilEnd()
|
2022-02-22 08:25:42 +00:00
|
|
|
const startBalance = await token.balanceOf(host.address)
|
|
|
|
await storage.finishContract(id)
|
|
|
|
const endBalance = await token.balanceOf(host.address)
|
|
|
|
expect(endBalance - startBalance).to.equal(offer.price)
|
2021-11-04 08:49:07 +00:00
|
|
|
})
|
2021-11-04 09:19:23 +00:00
|
|
|
|
2022-03-22 11:32:14 +00:00
|
|
|
it("is only allowed when the contract has started", async function () {
|
|
|
|
await expect(storage.finishContract(id)).to.be.revertedWith(
|
|
|
|
"Contract not started"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-02-22 08:25:42 +00:00
|
|
|
it("is only allowed when end time has passed", async function () {
|
2022-03-22 11:32:14 +00:00
|
|
|
await storage.startContract(id)
|
2022-02-22 08:25:42 +00:00
|
|
|
await expect(storage.finishContract(id)).to.be.revertedWith(
|
|
|
|
"Contract has not ended yet"
|
|
|
|
)
|
|
|
|
})
|
2021-11-04 09:19:23 +00:00
|
|
|
|
2022-02-22 08:25:42 +00:00
|
|
|
it("can only be done once", async function () {
|
2022-03-22 11:32:14 +00:00
|
|
|
await storage.startContract(id)
|
2022-03-08 14:58:08 +00:00
|
|
|
await waitUntilEnd()
|
2022-02-22 08:25:42 +00:00
|
|
|
await storage.finishContract(id)
|
2022-03-22 11:32:14 +00:00
|
|
|
await expect(storage.finishContract(id)).to.be.reverted
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can not be restarted", async function () {
|
|
|
|
await storage.startContract(id)
|
|
|
|
await waitUntilEnd()
|
|
|
|
await storage.finishContract(id)
|
|
|
|
await expect(storage.startContract(id)).to.be.reverted
|
2022-02-22 08:25:42 +00:00
|
|
|
})
|
|
|
|
})
|
2021-11-04 10:55:47 +00:00
|
|
|
|
2022-02-22 08:25:42 +00:00
|
|
|
describe("slashing when missing proofs", function () {
|
2022-03-09 10:11:01 +00:00
|
|
|
let period, periodOf, periodEnd
|
2022-03-08 14:58:08 +00:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
2022-02-22 08:25:42 +00:00
|
|
|
switchAccount(host)
|
2022-03-08 14:58:08 +00:00
|
|
|
period = (await storage.proofPeriod()).toNumber()
|
2022-03-09 10:11:01 +00:00
|
|
|
;({ periodOf, periodEnd } = periodic(period))
|
2021-11-04 09:19:23 +00:00
|
|
|
})
|
2021-11-04 13:19:58 +00:00
|
|
|
|
2022-03-15 15:53:35 +00:00
|
|
|
async function waitUntilProofIsRequired() {
|
|
|
|
await advanceTimeTo(periodEnd(periodOf(await currentTime())))
|
|
|
|
while (
|
|
|
|
!(
|
|
|
|
(await storage.isProofRequired(id)) &&
|
|
|
|
(await storage.getPointer(id)) < 250
|
|
|
|
)
|
|
|
|
) {
|
2022-03-08 14:58:08 +00:00
|
|
|
await advanceTime(period)
|
2021-11-04 13:19:58 +00:00
|
|
|
}
|
2022-02-22 08:25:42 +00:00
|
|
|
}
|
2021-11-04 13:19:58 +00:00
|
|
|
|
2022-02-22 08:25:42 +00:00
|
|
|
it("reduces collateral when too many proofs are missing", async function () {
|
|
|
|
await storage.connect(host).startContract(id)
|
|
|
|
for (let i = 0; i < slashMisses; i++) {
|
2022-03-15 15:53:35 +00:00
|
|
|
await waitUntilProofIsRequired()
|
|
|
|
let missedPeriod = periodOf(await currentTime())
|
|
|
|
await advanceTime(period)
|
|
|
|
await storage.markProofAsMissing(id, missedPeriod)
|
2022-02-22 08:25:42 +00:00
|
|
|
}
|
|
|
|
const expectedBalance = (collateralAmount * (100 - slashPercentage)) / 100
|
|
|
|
expect(await storage.balanceOf(host.address)).to.equal(expectedBalance)
|
2021-11-04 13:19:58 +00:00
|
|
|
})
|
2021-10-12 14:59:34 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-02 10:19:52 +00:00
|
|
|
// TODO: failure to start contract burns host and client
|
|
|
|
// TODO: implement checking of actual proofs of storage, instead of dummy bool
|
2021-11-04 10:55:47 +00:00
|
|
|
// TODO: allow other host to take over contract when too many missed proofs
|
|
|
|
// TODO: small partial payouts when proofs are being submitted
|
2021-11-04 13:28:02 +00:00
|
|
|
// TODO: reward caller of markProofAsMissing
|