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")
|
2021-10-12 14:59:34 +00:00
|
|
|
const { hashRequest, hashBid, sign } = require("./marketplace")
|
2021-11-02 08:45:49 +00:00
|
|
|
const { exampleRequest, exampleBid } = require("./examples")
|
2022-02-09 13:17:23 +00:00
|
|
|
const { mineBlock, minedBlockNumber } = require("./mining")
|
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
|
|
|
const request = exampleRequest()
|
|
|
|
const bid = exampleBid()
|
|
|
|
|
|
|
|
let storage
|
|
|
|
let token
|
|
|
|
let client, host
|
2022-02-15 16:54:19 +00:00
|
|
|
let collateralAmount, slashMisses, slashPercentage
|
2021-11-02 10:19:52 +00:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
2022-02-09 13:17:23 +00:00
|
|
|
;[client, host] = await ethers.getSigners()
|
|
|
|
await deployments.fixture(["TestToken", "Storage"])
|
|
|
|
token = await ethers.getContract("TestToken")
|
|
|
|
storage = await ethers.getContract("Storage")
|
2021-11-23 13:34:27 +00:00
|
|
|
await token.mint(client.address, 1000)
|
|
|
|
await token.mint(host.address, 1000)
|
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()
|
2021-11-02 10:19:52 +00:00
|
|
|
})
|
2021-10-12 14:59:34 +00:00
|
|
|
|
2021-11-02 10:19:52 +00:00
|
|
|
describe("creating a new storage contract", function () {
|
2021-11-01 15:34:01 +00:00
|
|
|
let id
|
2021-10-12 14:59:34 +00:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
2022-02-15 16:54:19 +00:00
|
|
|
await token.connect(host).approve(storage.address, collateralAmount)
|
2021-11-04 10:40:03 +00:00
|
|
|
await token.connect(client).approve(storage.address, bid.price)
|
2022-02-15 16:54:19 +00:00
|
|
|
await storage.connect(host).deposit(collateralAmount)
|
2021-11-02 08:45:49 +00:00
|
|
|
let requestHash = hashRequest(request)
|
2022-02-09 13:17:23 +00:00
|
|
|
let bidHash = hashBid({ ...bid, requestHash })
|
2021-11-02 10:19:52 +00:00
|
|
|
await storage.newContract(
|
2021-11-02 08:45:49 +00:00
|
|
|
request.duration,
|
|
|
|
request.size,
|
|
|
|
request.contentHash,
|
|
|
|
request.proofPeriod,
|
|
|
|
request.proofTimeout,
|
|
|
|
request.nonce,
|
|
|
|
bid.price,
|
2021-10-12 14:59:34 +00:00
|
|
|
await host.getAddress(),
|
2021-11-02 08:45:49 +00:00
|
|
|
bid.bidExpiry,
|
2021-10-12 14:59:34 +00:00
|
|
|
await sign(client, requestHash),
|
|
|
|
await sign(host, bidHash)
|
|
|
|
)
|
2021-11-02 10:19:52 +00:00
|
|
|
id = bidHash
|
2021-10-12 14:59:34 +00:00
|
|
|
})
|
|
|
|
|
2021-11-01 15:34:01 +00:00
|
|
|
it("created the contract", async function () {
|
2021-11-02 10:19:52 +00:00
|
|
|
expect(await storage.duration(id)).to.equal(request.duration)
|
|
|
|
expect(await storage.size(id)).to.equal(request.size)
|
|
|
|
expect(await storage.contentHash(id)).to.equal(request.contentHash)
|
|
|
|
expect(await storage.proofPeriod(id)).to.equal(request.proofPeriod)
|
|
|
|
expect(await storage.proofTimeout(id)).to.equal(request.proofTimeout)
|
2021-11-04 08:49:07 +00:00
|
|
|
expect(await storage.price(id)).to.equal(bid.price)
|
|
|
|
expect(await storage.host(id)).to.equal(await host.getAddress())
|
2022-02-09 13:17:23 +00:00
|
|
|
})
|
2021-11-02 11:50:06 +00:00
|
|
|
|
2022-02-15 16:54:19 +00:00
|
|
|
it("locks up host collateral", async function () {
|
|
|
|
await expect(storage.connect(host).withdraw()).to.be.revertedWith(
|
|
|
|
"Account locked"
|
2022-02-09 13:17:23 +00:00
|
|
|
)
|
2021-11-02 11:50:06 +00:00
|
|
|
})
|
2021-11-04 08:49:07 +00:00
|
|
|
|
|
|
|
describe("starting the contract", function () {
|
2022-02-09 13:17:23 +00:00
|
|
|
it("starts requiring storage proofs", async function () {
|
2021-11-04 08:49:07 +00:00
|
|
|
await storage.connect(host).startContract(id)
|
|
|
|
expect(await storage.proofEnd(id)).to.be.gt(0)
|
|
|
|
})
|
2021-11-04 08:53:01 +00:00
|
|
|
|
|
|
|
it("can only be done by the host", async function () {
|
|
|
|
await expect(
|
|
|
|
storage.connect(client).startContract(id)
|
|
|
|
).to.be.revertedWith("Only host can call this function")
|
|
|
|
})
|
2021-11-04 09:23:00 +00:00
|
|
|
|
|
|
|
it("can only be done once", async function () {
|
|
|
|
await storage.connect(host).startContract(id)
|
|
|
|
await expect(storage.connect(host).startContract(id)).to.be.reverted
|
|
|
|
})
|
2021-11-04 08:49:07 +00:00
|
|
|
})
|
2021-11-04 09:19:23 +00:00
|
|
|
|
|
|
|
describe("finishing the contract", function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
await storage.connect(host).startContract(id)
|
|
|
|
})
|
|
|
|
|
2021-11-04 10:55:47 +00:00
|
|
|
async function mineUntilEnd() {
|
2021-11-04 09:19:23 +00:00
|
|
|
const end = await storage.proofEnd(id)
|
2022-02-09 13:17:23 +00:00
|
|
|
while ((await minedBlockNumber()) < end) {
|
2021-11-04 09:19:23 +00:00
|
|
|
await mineBlock()
|
|
|
|
}
|
2021-11-04 10:55:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 16:54:19 +00:00
|
|
|
it("unlocks the host collateral", async function () {
|
2021-11-04 10:55:47 +00:00
|
|
|
await mineUntilEnd()
|
2021-11-04 09:19:23 +00:00
|
|
|
await storage.finishContract(id)
|
2022-02-15 16:54:19 +00:00
|
|
|
await expect(storage.connect(host).withdraw()).not.to.be.reverted
|
2021-11-04 09:19:23 +00:00
|
|
|
})
|
|
|
|
|
2021-11-04 10:55:47 +00:00
|
|
|
it("pays the host", async function () {
|
|
|
|
await mineUntilEnd()
|
|
|
|
const startBalance = await token.balanceOf(host.address)
|
|
|
|
await storage.finishContract(id)
|
|
|
|
const endBalance = await token.balanceOf(host.address)
|
|
|
|
expect(endBalance - startBalance).to.equal(bid.price)
|
|
|
|
})
|
|
|
|
|
2021-11-04 09:19:23 +00:00
|
|
|
it("is only allowed when end time has passed", async function () {
|
2022-02-09 13:17:23 +00:00
|
|
|
await expect(storage.finishContract(id)).to.be.revertedWith(
|
|
|
|
"Contract has not ended yet"
|
|
|
|
)
|
2021-11-04 09:19:23 +00:00
|
|
|
})
|
2021-11-04 10:18:05 +00:00
|
|
|
|
|
|
|
it("can only be done once", async function () {
|
2021-11-04 10:55:47 +00:00
|
|
|
await mineUntilEnd()
|
2021-11-04 10:18:05 +00:00
|
|
|
await storage.finishContract(id)
|
2022-02-09 13:17:23 +00:00
|
|
|
await expect(storage.finishContract(id)).to.be.revertedWith(
|
|
|
|
"Contract already finished"
|
|
|
|
)
|
2021-11-04 10:18:05 +00:00
|
|
|
})
|
2021-11-04 09:19:23 +00:00
|
|
|
})
|
2021-11-04 13:19:58 +00:00
|
|
|
|
|
|
|
describe("slashing when missing proofs", function () {
|
|
|
|
async function ensureProofIsMissing() {
|
2022-02-09 13:17:23 +00:00
|
|
|
while (!(await storage.isProofRequired(id, await minedBlockNumber()))) {
|
2021-11-04 13:19:58 +00:00
|
|
|
mineBlock()
|
|
|
|
}
|
|
|
|
const blocknumber = await minedBlockNumber()
|
2022-02-09 13:17:23 +00:00
|
|
|
for (let i = 0; i < request.proofTimeout; i++) {
|
2021-11-04 13:19:58 +00:00
|
|
|
mineBlock()
|
|
|
|
}
|
|
|
|
await storage.markProofAsMissing(id, blocknumber)
|
|
|
|
}
|
|
|
|
|
2022-02-15 16:54:19 +00:00
|
|
|
it("reduces collateral when too many proofs are missing", async function () {
|
2021-11-04 13:19:58 +00:00
|
|
|
await storage.connect(host).startContract(id)
|
2022-02-09 13:17:23 +00:00
|
|
|
for (let i = 0; i < slashMisses; i++) {
|
2021-11-04 13:19:58 +00:00
|
|
|
await ensureProofIsMissing()
|
|
|
|
}
|
2022-02-15 16:54:19 +00:00
|
|
|
const expectedBalance =
|
|
|
|
(collateralAmount * (100 - slashPercentage)) / 100
|
|
|
|
expect(await storage.balanceOf(host.address)).to.equal(expectedBalance)
|
2022-02-09 13:17:23 +00:00
|
|
|
})
|
2021-11-04 13:19:58 +00:00
|
|
|
})
|
2021-10-12 14:59:34 +00:00
|
|
|
})
|
2021-11-02 10:19:52 +00:00
|
|
|
|
2022-02-15 16:54:19 +00:00
|
|
|
it("doesn't create contract with insufficient collateral", async function () {
|
|
|
|
await token.connect(host).approve(storage.address, collateralAmount - 1)
|
2021-11-04 10:40:03 +00:00
|
|
|
await token.connect(client).approve(storage.address, bid.price)
|
2022-02-15 16:54:19 +00:00
|
|
|
await storage.connect(host).deposit(collateralAmount - 1)
|
2021-11-02 10:19:52 +00:00
|
|
|
let requestHash = hashRequest(request)
|
2022-02-09 13:17:23 +00:00
|
|
|
let bidHash = hashBid({ ...bid, requestHash })
|
|
|
|
await expect(
|
|
|
|
storage.newContract(
|
|
|
|
request.duration,
|
|
|
|
request.size,
|
|
|
|
request.contentHash,
|
|
|
|
request.proofPeriod,
|
|
|
|
request.proofTimeout,
|
|
|
|
request.nonce,
|
|
|
|
bid.price,
|
|
|
|
await host.getAddress(),
|
|
|
|
bid.bidExpiry,
|
|
|
|
await sign(client, requestHash),
|
|
|
|
await sign(host, bidHash)
|
|
|
|
)
|
2022-02-15 16:54:19 +00:00
|
|
|
).to.be.revertedWith("Insufficient collateral")
|
2021-11-02 10:19:52 +00:00
|
|
|
})
|
2021-11-04 10:40:03 +00:00
|
|
|
|
|
|
|
it("doesn't create contract without payment of price", async function () {
|
2022-02-15 16:54:19 +00:00
|
|
|
await token.connect(host).approve(storage.address, collateralAmount)
|
2021-11-04 10:40:03 +00:00
|
|
|
await token.connect(client).approve(storage.address, bid.price - 1)
|
2022-02-15 16:54:19 +00:00
|
|
|
await storage.connect(host).deposit(collateralAmount)
|
2021-11-04 10:40:03 +00:00
|
|
|
let requestHash = hashRequest(request)
|
2022-02-09 13:17:23 +00:00
|
|
|
let bidHash = hashBid({ ...bid, requestHash })
|
|
|
|
await expect(
|
|
|
|
storage.newContract(
|
|
|
|
request.duration,
|
|
|
|
request.size,
|
|
|
|
request.contentHash,
|
|
|
|
request.proofPeriod,
|
|
|
|
request.proofTimeout,
|
|
|
|
request.nonce,
|
|
|
|
bid.price,
|
|
|
|
await host.getAddress(),
|
|
|
|
bid.bidExpiry,
|
|
|
|
await sign(client, requestHash),
|
|
|
|
await sign(host, bidHash)
|
|
|
|
)
|
|
|
|
).to.be.revertedWith("ERC20: transfer amount exceeds allowance")
|
2021-11-04 10:40:03 +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
|