2021-10-12 16:59:34 +02:00
|
|
|
const { expect } = require("chai")
|
2021-11-16 14:26:28 +01:00
|
|
|
const { ethers, deployments } = require("hardhat")
|
2022-06-13 12:17:37 +02:00
|
|
|
const { hexlify, randomBytes } = ethers.utils
|
2022-07-04 11:16:55 +02:00
|
|
|
const { AddressZero } = ethers.constants
|
2022-06-13 12:17:37 +02:00
|
|
|
const { exampleRequest } = require("./examples")
|
2022-03-15 16:45:35 +01:00
|
|
|
const { advanceTime, advanceTimeTo, currentTime } = require("./evm")
|
2022-07-20 10:05:36 +02:00
|
|
|
const { requestId, slotId } = require("./ids")
|
2022-03-09 11:11:01 +01:00
|
|
|
const { periodic } = require("./time")
|
2021-10-12 16:59:34 +02:00
|
|
|
|
2021-11-01 16:34:01 +01:00
|
|
|
describe("Storage", function () {
|
2022-06-13 12:17:37 +02:00
|
|
|
const proof = hexlify(randomBytes(42))
|
|
|
|
|
2021-11-02 11:19:52 +01:00
|
|
|
let storage
|
|
|
|
let token
|
|
|
|
let client, host
|
2022-06-13 12:17:37 +02:00
|
|
|
let request
|
2022-02-15 17:54:19 +01:00
|
|
|
let collateralAmount, slashMisses, slashPercentage
|
2022-07-20 10:05:36 +02:00
|
|
|
let slot
|
2022-02-22 09:25:42 +01:00
|
|
|
|
|
|
|
function switchAccount(account) {
|
|
|
|
token = token.connect(account)
|
|
|
|
storage = storage.connect(account)
|
|
|
|
}
|
2021-11-02 11:19:52 +01:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
2022-02-09 14:17:23 +01:00
|
|
|
;[client, host] = await ethers.getSigners()
|
2022-02-22 09:25:42 +01:00
|
|
|
|
2022-02-09 14:17:23 +01:00
|
|
|
await deployments.fixture(["TestToken", "Storage"])
|
|
|
|
token = await ethers.getContract("TestToken")
|
|
|
|
storage = await ethers.getContract("Storage")
|
2022-02-22 09:25:42 +01:00
|
|
|
|
2021-11-23 14:34:27 +01:00
|
|
|
await token.mint(client.address, 1000)
|
|
|
|
await token.mint(host.address, 1000)
|
2022-02-22 09:25:42 +01:00
|
|
|
|
2022-02-15 17:54:19 +01:00
|
|
|
collateralAmount = await storage.collateralAmount()
|
2021-11-16 14:26:28 +01:00
|
|
|
slashMisses = await storage.slashMisses()
|
|
|
|
slashPercentage = await storage.slashPercentage()
|
2022-02-22 09:25:42 +01:00
|
|
|
|
|
|
|
request = exampleRequest()
|
|
|
|
request.client = client.address
|
2022-07-20 10:05:36 +02:00
|
|
|
slot = {
|
|
|
|
request: requestId(request),
|
|
|
|
index: request.content.erasure.totalNodes / 2,
|
|
|
|
}
|
2022-02-22 09:25:42 +01:00
|
|
|
|
|
|
|
switchAccount(client)
|
2022-06-13 13:19:33 +02:00
|
|
|
await token.approve(storage.address, request.ask.reward)
|
2022-02-22 09:25:42 +01:00
|
|
|
await storage.requestStorage(request)
|
|
|
|
switchAccount(host)
|
|
|
|
await token.approve(storage.address, collateralAmount)
|
|
|
|
await storage.deposit(collateralAmount)
|
2021-11-02 11:19:52 +01:00
|
|
|
})
|
2021-10-12 16:59:34 +02:00
|
|
|
|
2022-04-06 14:52:02 +02: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 10:10:22 +02: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 11:16:55 +02:00
|
|
|
})
|
|
|
|
|
2022-07-20 10:05:36 +02:00
|
|
|
describe("ending the contract", function () {
|
2022-03-08 15:58:08 +01:00
|
|
|
async function waitUntilEnd() {
|
2022-07-20 10:05:36 +02:00
|
|
|
const end = (await storage.proofEnd(slotId(slot))).toNumber()
|
2022-03-08 15:58:08 +01:00
|
|
|
await advanceTimeTo(end)
|
2022-02-22 09:25:42 +01:00
|
|
|
}
|
|
|
|
|
2022-03-09 11:33:54 +01:00
|
|
|
it("unlocks the host collateral", async function () {
|
2022-07-20 10:05:36 +02:00
|
|
|
await storage.fillSlot(slot.request, slot.index, proof)
|
2022-03-09 11:33:54 +01:00
|
|
|
await waitUntilEnd()
|
|
|
|
await expect(storage.withdraw()).not.to.be.reverted
|
|
|
|
})
|
2022-02-22 09:25:42 +01:00
|
|
|
})
|
2021-11-04 11:55:47 +01:00
|
|
|
|
2022-02-22 09:25:42 +01:00
|
|
|
describe("slashing when missing proofs", function () {
|
2022-03-09 11:11:01 +01:00
|
|
|
let period, periodOf, periodEnd
|
2022-03-08 15:58:08 +01:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
period = (await storage.proofPeriod()).toNumber()
|
2022-03-09 11:11:01 +01:00
|
|
|
;({ periodOf, periodEnd } = periodic(period))
|
2021-11-04 10:19:23 +01:00
|
|
|
})
|
2021-11-04 14:19:58 +01:00
|
|
|
|
2022-07-20 10:10:22 +02:00
|
|
|
async function waitUntilProofIsRequired(id) {
|
2022-03-15 16:53:35 +01:00
|
|
|
await advanceTimeTo(periodEnd(periodOf(await currentTime())))
|
|
|
|
while (
|
|
|
|
!(
|
|
|
|
(await storage.isProofRequired(id)) &&
|
|
|
|
(await storage.getPointer(id)) < 250
|
|
|
|
)
|
|
|
|
) {
|
2022-03-08 15:58:08 +01:00
|
|
|
await advanceTime(period)
|
2021-11-04 14:19:58 +01:00
|
|
|
}
|
2022-02-22 09:25:42 +01:00
|
|
|
}
|
2021-11-04 14:19:58 +01:00
|
|
|
|
2022-02-22 09:25:42 +01:00
|
|
|
it("reduces collateral when too many proofs are missing", async function () {
|
2022-07-20 10:10:22 +02:00
|
|
|
const id = slotId(slot)
|
|
|
|
await storage.fillSlot(slot.request, slot.index, proof)
|
2022-02-22 09:25:42 +01:00
|
|
|
for (let i = 0; i < slashMisses; i++) {
|
2022-07-20 10:10:22 +02:00
|
|
|
await waitUntilProofIsRequired(id)
|
2022-03-15 16:53:35 +01:00
|
|
|
let missedPeriod = periodOf(await currentTime())
|
|
|
|
await advanceTime(period)
|
|
|
|
await storage.markProofAsMissing(id, missedPeriod)
|
2022-02-22 09:25:42 +01:00
|
|
|
}
|
|
|
|
const expectedBalance = (collateralAmount * (100 - slashPercentage)) / 100
|
|
|
|
expect(await storage.balanceOf(host.address)).to.equal(expectedBalance)
|
2021-11-04 14:19:58 +01:00
|
|
|
})
|
2021-10-12 16:59:34 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-02 11:19:52 +01:00
|
|
|
// TODO: implement checking of actual proofs of storage, instead of dummy bool
|
2021-11-04 11:55:47 +01: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 14:28:02 +01:00
|
|
|
// TODO: reward caller of markProofAsMissing
|