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-09-08 07:56:01 +00:00
|
|
|
const { BigNumber } = ethers
|
2022-06-13 10:17:37 +00:00
|
|
|
const { hexlify, randomBytes } = ethers.utils
|
2022-07-04 09:16:55 +00:00
|
|
|
const { AddressZero } = ethers.constants
|
2022-06-13 10:17:37 +00:00
|
|
|
const { exampleRequest } = require("./examples")
|
2022-09-08 07:56:01 +00:00
|
|
|
const { advanceTime, advanceTimeTo, currentTime, mine } = require("./evm")
|
2022-07-20 08:05:36 +00:00
|
|
|
const { requestId, slotId } = require("./ids")
|
2022-03-09 10:11:01 +00:00
|
|
|
const { periodic } = require("./time")
|
2022-07-20 09:07:20 +00:00
|
|
|
const { price } = require("./price")
|
2022-09-21 09:57:26 +00:00
|
|
|
const {
|
|
|
|
waitUntilCancelled,
|
|
|
|
waitUntilStarted,
|
|
|
|
waitUntilFinished,
|
|
|
|
} = require("./marketplace")
|
2021-10-12 14:59:34 +00:00
|
|
|
|
2021-11-01 15:34:01 +00:00
|
|
|
describe("Storage", function () {
|
2022-06-13 10:17:37 +00:00
|
|
|
const proof = hexlify(randomBytes(42))
|
|
|
|
|
2021-11-02 10:19:52 +00:00
|
|
|
let storage
|
|
|
|
let token
|
|
|
|
let client, host
|
2022-06-13 10:17:37 +00:00
|
|
|
let request
|
2022-02-15 16:54:19 +00:00
|
|
|
let collateralAmount, slashMisses, slashPercentage
|
2022-07-20 08:05:36 +00:00
|
|
|
let slot
|
2022-02-22 08:25:42 +00:00
|
|
|
|
|
|
|
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-09-22 02:21:49 +00:00
|
|
|
await deployments.fixture(["TestToken", "Storage"])
|
2022-02-09 13:17:23 +00:00
|
|
|
token = await ethers.getContract("TestToken")
|
2022-09-22 02:21:49 +00:00
|
|
|
storage = await ethers.getContract("Storage")
|
2022-02-22 08:25:42 +00:00
|
|
|
|
2022-07-20 09:07:20 +00:00
|
|
|
await token.mint(client.address, 1_000_000_000)
|
|
|
|
await token.mint(host.address, 1_000_000_000)
|
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-09-13 07:18:55 +00:00
|
|
|
minCollateralThreshold = await storage.minCollateralThreshold()
|
2022-02-22 08:25:42 +00:00
|
|
|
|
2022-09-22 02:21:49 +00:00
|
|
|
request = await exampleRequest()
|
2022-02-22 08:25:42 +00:00
|
|
|
request.client = client.address
|
2022-07-20 08:05:36 +00:00
|
|
|
slot = {
|
|
|
|
request: requestId(request),
|
2022-07-20 08:44:22 +00:00
|
|
|
index: request.ask.slots / 2,
|
2022-07-20 08:05:36 +00:00
|
|
|
}
|
2022-02-22 08:25:42 +00:00
|
|
|
|
|
|
|
switchAccount(client)
|
2022-07-20 09:07:20 +00:00
|
|
|
await token.approve(storage.address, price(request))
|
2022-02-22 08:25:42 +00:00
|
|
|
await storage.requestStorage(request)
|
|
|
|
switchAccount(host)
|
|
|
|
await token.approve(storage.address, collateralAmount)
|
|
|
|
await storage.deposit(collateralAmount)
|
2021-11-02 10:19:52 +00:00
|
|
|
})
|
2021-10-12 14:59:34 +00:00
|
|
|
|
2022-04-06 12:52:02 +00:00
|
|
|
it("can retrieve storage requests", async function () {
|
|
|
|
const id = requestId(request)
|
|
|
|
const retrieved = await storage.getRequest(id)
|
|
|
|
expect(retrieved.client).to.equal(request.client)
|
|
|
|
expect(retrieved.expiry).to.equal(request.expiry)
|
|
|
|
expect(retrieved.nonce).to.equal(request.nonce)
|
|
|
|
})
|
|
|
|
|
2022-07-20 08:10:22 +00:00
|
|
|
it("can retrieve host that filled slot", async function () {
|
|
|
|
expect(await storage.getHost(slotId(slot))).to.equal(AddressZero)
|
|
|
|
await storage.fillSlot(slot.request, slot.index, proof)
|
|
|
|
expect(await storage.getHost(slotId(slot))).to.equal(host.address)
|
2022-07-04 09:16:55 +00:00
|
|
|
})
|
|
|
|
|
2022-07-20 08:05:36 +00:00
|
|
|
describe("ending the contract", function () {
|
2022-03-09 10:33:54 +00:00
|
|
|
it("unlocks the host collateral", async function () {
|
2022-07-20 08:05:36 +00:00
|
|
|
await storage.fillSlot(slot.request, slot.index, proof)
|
2022-09-21 09:57:26 +00:00
|
|
|
await waitUntilFinished(storage, slotId(slot))
|
2022-03-09 10:33:54 +00:00
|
|
|
await expect(storage.withdraw()).not.to.be.reverted
|
|
|
|
})
|
2022-02-22 08:25:42 +00:00
|
|
|
})
|
2021-11-04 10:55:47 +00:00
|
|
|
|
2022-09-16 04:58:51 +00:00
|
|
|
describe("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 () {
|
|
|
|
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-07-20 08:10:22 +00:00
|
|
|
async function waitUntilProofIsRequired(id) {
|
2022-03-15 15:53:35 +00:00
|
|
|
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-09-16 04:58:51 +00:00
|
|
|
describe("slashing when missing proofs", function () {
|
|
|
|
it("reduces collateral when too many proofs are missing", async function () {
|
|
|
|
const id = slotId(slot)
|
|
|
|
await storage.fillSlot(slot.request, slot.index, proof)
|
|
|
|
for (let i = 0; i < slashMisses; i++) {
|
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
let missedPeriod = periodOf(await currentTime())
|
|
|
|
await advanceTime(period)
|
|
|
|
await storage.markProofAsMissing(id, missedPeriod)
|
|
|
|
}
|
|
|
|
const expectedBalance =
|
|
|
|
(collateralAmount * (100 - slashPercentage)) / 100
|
|
|
|
expect(await storage.balanceOf(host.address)).to.equal(expectedBalance)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("freeing a slot", function () {
|
|
|
|
it("frees slot when collateral slashed below minimum threshold", async function () {
|
|
|
|
const id = slotId(slot)
|
|
|
|
|
2022-09-22 02:21:49 +00:00
|
|
|
await waitUntilStarted(storage, request.ask.slots, slot.request, proof)
|
2022-09-16 04:58:51 +00:00
|
|
|
|
|
|
|
// max slashes before dropping below collateral threshold
|
|
|
|
const maxSlashes = 10
|
|
|
|
for (let i = 0; i < maxSlashes; i++) {
|
|
|
|
for (let j = 0; j < slashMisses; j++) {
|
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
let missedPeriod = periodOf(await currentTime())
|
|
|
|
await advanceTime(period)
|
|
|
|
if (i === maxSlashes - 1 && j === slashMisses - 1) {
|
|
|
|
await expect(
|
|
|
|
await storage.markProofAsMissing(id, missedPeriod)
|
|
|
|
).to.emit(storage, "SlotFreed")
|
|
|
|
await expect(storage.getSlot(id)).to.be.revertedWith("Slot empty")
|
|
|
|
} else {
|
|
|
|
await storage.markProofAsMissing(id, missedPeriod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-11-04 13:19:58 +00:00
|
|
|
})
|
2022-09-08 07:56:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("contract state", function () {
|
|
|
|
let period, periodOf, periodEnd
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
period = (await storage.proofPeriod()).toNumber()
|
|
|
|
;({ periodOf, periodEnd } = periodic(period))
|
|
|
|
})
|
|
|
|
|
|
|
|
async function waitUntilProofWillBeRequired(id) {
|
|
|
|
while (!(await storage.willProofBeRequired(id))) {
|
|
|
|
await mine()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function waitUntilProofIsRequired(id) {
|
|
|
|
await advanceTimeTo(periodEnd(periodOf(await currentTime())))
|
|
|
|
while (
|
|
|
|
!(
|
|
|
|
(await storage.isProofRequired(id)) &&
|
|
|
|
(await storage.getPointer(id)) < 250
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
await advanceTime(period)
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 03:19:51 +00:00
|
|
|
|
|
|
|
it("fails to mark proof as missing when cancelled", async function () {
|
|
|
|
await storage.fillSlot(slot.request, slot.index, proof)
|
2022-09-21 09:57:26 +00:00
|
|
|
await waitUntilCancelled(request.expiry)
|
2022-08-16 03:19:51 +00:00
|
|
|
let missedPeriod = periodOf(await currentTime())
|
|
|
|
await expect(
|
|
|
|
storage.markProofAsMissing(slotId(slot), missedPeriod)
|
2022-09-08 07:56:01 +00:00
|
|
|
).to.be.revertedWith("Slot not accepting proofs")
|
2022-08-16 03:19:51 +00:00
|
|
|
})
|
2022-09-08 07:56:01 +00:00
|
|
|
|
|
|
|
it("will not require proofs once cancelled", async function () {
|
|
|
|
const id = slotId(slot)
|
|
|
|
await storage.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilProofWillBeRequired(id)
|
|
|
|
await expect(await storage.willProofBeRequired(id)).to.be.true
|
|
|
|
await advanceTimeTo(request.expiry + 1)
|
|
|
|
await expect(await storage.willProofBeRequired(id)).to.be.false
|
2022-08-22 06:16:45 +00:00
|
|
|
})
|
2022-09-21 09:29:40 +00:00
|
|
|
|
2022-09-08 07:56:01 +00:00
|
|
|
it("does not require proofs once cancelled", async function () {
|
|
|
|
const id = slotId(slot)
|
|
|
|
await storage.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
await expect(await storage.isProofRequired(id)).to.be.true
|
|
|
|
await advanceTimeTo(request.expiry + 1)
|
|
|
|
await expect(await storage.isProofRequired(id)).to.be.false
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not provide challenges once cancelled", async function () {
|
|
|
|
const id = slotId(slot)
|
|
|
|
await storage.fillSlot(slot.request, slot.index, proof)
|
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
const challenge1 = await storage.getChallenge(id)
|
|
|
|
expect(BigNumber.from(challenge1).gt(0))
|
|
|
|
await advanceTimeTo(request.expiry + 1)
|
|
|
|
const challenge2 = await storage.getChallenge(id)
|
|
|
|
expect(BigNumber.from(challenge2).isZero())
|
2022-08-22 06:16:45 +00:00
|
|
|
})
|
|
|
|
|
2022-09-08 07:56:01 +00:00
|
|
|
it("does not provide pointer once cancelled", async function () {
|
|
|
|
const id = slotId(slot)
|
2022-08-22 06:16:45 +00:00
|
|
|
await storage.fillSlot(slot.request, slot.index, proof)
|
2022-09-08 07:56:01 +00:00
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
const challenge1 = await storage.getChallenge(id)
|
|
|
|
expect(BigNumber.from(challenge1).gt(0))
|
|
|
|
await advanceTimeTo(request.expiry + 1)
|
|
|
|
const challenge2 = await storage.getChallenge(id)
|
|
|
|
expect(BigNumber.from(challenge2).isZero())
|
2022-08-22 06:16:45 +00:00
|
|
|
})
|
|
|
|
})
|
2021-10-12 14:59:34 +00:00
|
|
|
})
|
|
|
|
|
2021-11-02 10:19:52 +00:00
|
|
|
// 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
|