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-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-03-15 15:45:35 +00:00
|
|
|
const { advanceTime, advanceTimeTo, currentTime } = 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")
|
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
|
|
|
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
|
2022-06-13 10:17:37 +00:00
|
|
|
id = requestId(request)
|
2022-07-20 08:05:36 +00:00
|
|
|
slot = {
|
|
|
|
request: requestId(request),
|
|
|
|
index: request.content.erasure.totalNodes / 2,
|
|
|
|
}
|
2022-02-22 08:25:42 +00:00
|
|
|
|
|
|
|
switchAccount(client)
|
2022-06-13 11:19:33 +00:00
|
|
|
await token.approve(storage.address, request.ask.reward)
|
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-04 09:16:55 +00:00
|
|
|
it("can retrieve host that fulfilled request", async function () {
|
|
|
|
expect(await storage.getHost(requestId(request))).to.equal(AddressZero)
|
|
|
|
await storage.fulfillRequest(requestId(request), proof)
|
|
|
|
expect(await storage.getHost(requestId(request))).to.equal(host.address)
|
|
|
|
})
|
|
|
|
|
2022-07-20 08:05:36 +00:00
|
|
|
describe("ending the contract", function () {
|
2022-03-08 14:58:08 +00:00
|
|
|
async function waitUntilEnd() {
|
2022-07-20 08:05:36 +00:00
|
|
|
const end = (await storage.proofEnd(slotId(slot))).toNumber()
|
2022-03-08 14:58:08 +00:00
|
|
|
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-07-20 08:05:36 +00:00
|
|
|
await storage.fillSlot(slot.request, slot.index, proof)
|
2022-03-09 10:33:54 +00:00
|
|
|
await waitUntilEnd()
|
|
|
|
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-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 () {
|
|
|
|
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 () {
|
2022-06-13 10:17:37 +00:00
|
|
|
await storage.fulfillRequest(requestId(request), proof)
|
2022-02-22 08:25:42 +00:00
|
|
|
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: 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
|