2021-10-12 14:59:34 +00:00
|
|
|
const { expect } = require("chai")
|
|
|
|
const { ethers } = require("hardhat")
|
|
|
|
const { hashRequest, hashBid, sign } = require("./marketplace")
|
|
|
|
|
|
|
|
describe("Storage Contract", function () {
|
|
|
|
|
2021-10-14 07:10:57 +00:00
|
|
|
const duration = 31 * 24 * 60 * 60 // 31 days
|
|
|
|
const size = 1 * 1024 * 1024 * 1024 // 1 Gigabyte
|
2021-10-14 12:49:29 +00:00
|
|
|
const contentHash = ethers.utils.sha256("0xdeadbeef") // hash of content
|
2021-10-14 10:37:14 +00:00
|
|
|
const proofPeriod = 8 // 8 blocks ≈ 2 minutes
|
|
|
|
const proofTimeout = 4 // 4 blocks ≈ 1 minute
|
2021-10-12 14:59:34 +00:00
|
|
|
const price = 42
|
|
|
|
|
|
|
|
var StorageContract
|
|
|
|
var client, host
|
2021-10-18 13:29:58 +00:00
|
|
|
var bidExpiry
|
2021-10-12 14:59:34 +00:00
|
|
|
var requestHash, bidHash
|
|
|
|
var contract
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
[client, host] = await ethers.getSigners()
|
|
|
|
StorageContract = await ethers.getContractFactory("StorageContract")
|
2021-10-14 12:49:29 +00:00
|
|
|
requestHash = hashRequest(
|
|
|
|
duration,
|
|
|
|
size,
|
|
|
|
contentHash,
|
|
|
|
proofPeriod,
|
|
|
|
proofTimeout
|
|
|
|
)
|
2021-10-18 13:29:58 +00:00
|
|
|
bidExpiry = Math.round(Date.now() / 1000) + 60 * 60 // 1 hour from now
|
|
|
|
bidHash = hashBid(requestHash, bidExpiry, price)
|
2021-10-12 14:59:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("when properly instantiated", function () {
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
contract = await StorageContract.deploy(
|
|
|
|
duration,
|
|
|
|
size,
|
2021-10-14 12:49:29 +00:00
|
|
|
contentHash,
|
2021-10-12 14:59:34 +00:00
|
|
|
price,
|
2021-10-14 07:10:57 +00:00
|
|
|
proofPeriod,
|
|
|
|
proofTimeout,
|
2021-10-18 13:29:58 +00:00
|
|
|
bidExpiry,
|
2021-10-12 14:59:34 +00:00
|
|
|
await host.getAddress(),
|
|
|
|
await sign(client, requestHash),
|
|
|
|
await sign(host, bidHash)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("has a duration", async function () {
|
|
|
|
expect(await contract.duration()).to.equal(duration)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("contains the size of the data that is to be stored", async function () {
|
|
|
|
expect(await contract.size()).to.equal(size)
|
|
|
|
})
|
|
|
|
|
2021-10-14 12:49:29 +00:00
|
|
|
it("contains the hash of the data that is to be stored", async function () {
|
|
|
|
expect(await contract.contentHash()).to.equal(contentHash)
|
|
|
|
})
|
|
|
|
|
2021-10-12 14:59:34 +00:00
|
|
|
it("has a price", async function () {
|
|
|
|
expect(await contract.price()).to.equal(price)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("knows the host that provides the storage", async function () {
|
|
|
|
expect(await contract.host()).to.equal(await host.getAddress())
|
|
|
|
})
|
|
|
|
|
2021-10-14 07:10:57 +00:00
|
|
|
it("has an average time between proofs (in blocks)", async function (){
|
|
|
|
expect(await contract.proofPeriod()).to.equal(proofPeriod)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("has a proof timeout (in blocks)", async function (){
|
|
|
|
expect(await contract.proofTimeout()).to.equal(proofTimeout)
|
|
|
|
})
|
2021-10-12 14:59:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot be created when client signature is invalid", async function () {
|
2021-10-14 12:49:29 +00:00
|
|
|
let invalidHash = hashRequest(
|
|
|
|
duration + 1,
|
|
|
|
size,
|
|
|
|
contentHash,
|
|
|
|
proofPeriod,
|
|
|
|
proofTimeout
|
|
|
|
)
|
2021-10-14 12:01:28 +00:00
|
|
|
let invalidSignature = await sign(client, invalidHash)
|
2021-10-12 14:59:34 +00:00
|
|
|
await expect(StorageContract.deploy(
|
|
|
|
duration,
|
|
|
|
size,
|
2021-10-14 12:49:29 +00:00
|
|
|
contentHash,
|
2021-10-12 14:59:34 +00:00
|
|
|
price,
|
2021-10-14 07:10:57 +00:00
|
|
|
proofPeriod,
|
|
|
|
proofTimeout,
|
2021-10-18 13:29:58 +00:00
|
|
|
bidExpiry,
|
2021-10-12 14:59:34 +00:00
|
|
|
await host.getAddress(),
|
|
|
|
invalidSignature,
|
|
|
|
await sign(host, bidHash)
|
|
|
|
)).to.be.revertedWith("Invalid signature")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot be created when host signature is invalid", async function () {
|
2021-10-18 13:29:58 +00:00
|
|
|
let invalidBid = hashBid(requestHash, bidExpiry, price - 1)
|
|
|
|
let invalidSignature = await sign(host, invalidBid)
|
2021-10-12 14:59:34 +00:00
|
|
|
await expect(StorageContract.deploy(
|
|
|
|
duration,
|
|
|
|
size,
|
2021-10-14 12:49:29 +00:00
|
|
|
contentHash,
|
2021-10-12 14:59:34 +00:00
|
|
|
price,
|
2021-10-14 07:10:57 +00:00
|
|
|
proofPeriod,
|
|
|
|
proofTimeout,
|
2021-10-18 13:29:58 +00:00
|
|
|
bidExpiry,
|
2021-10-12 14:59:34 +00:00
|
|
|
await host.getAddress(),
|
|
|
|
await sign(client, requestHash),
|
|
|
|
invalidSignature
|
|
|
|
)).to.be.revertedWith("Invalid signature")
|
|
|
|
})
|
2021-10-14 10:37:14 +00:00
|
|
|
|
|
|
|
it("cannot be created when proof timeout is too large", async function () {
|
|
|
|
let invalidTimeout = 129 // max proof timeout is 128 blocks
|
2021-10-14 12:49:29 +00:00
|
|
|
requestHash = hashRequest(
|
|
|
|
duration,
|
|
|
|
size,
|
|
|
|
contentHash,
|
|
|
|
proofPeriod,
|
|
|
|
invalidTimeout
|
|
|
|
)
|
2021-10-18 13:29:58 +00:00
|
|
|
bidHash = hashBid(requestHash, bidExpiry, price)
|
2021-10-14 10:37:14 +00:00
|
|
|
await expect(StorageContract.deploy(
|
|
|
|
duration,
|
|
|
|
size,
|
2021-10-14 12:49:29 +00:00
|
|
|
contentHash,
|
2021-10-14 10:37:14 +00:00
|
|
|
price,
|
|
|
|
proofPeriod,
|
|
|
|
invalidTimeout,
|
2021-10-18 13:29:58 +00:00
|
|
|
bidExpiry,
|
2021-10-14 10:37:14 +00:00
|
|
|
await host.getAddress(),
|
|
|
|
await sign(client, requestHash),
|
|
|
|
await sign(host, bidHash),
|
|
|
|
)).to.be.revertedWith("Invalid proof timeout")
|
|
|
|
})
|
|
|
|
|
2021-10-18 13:29:58 +00:00
|
|
|
it("cannot be created when bid has expired", async function () {
|
|
|
|
let expired = Math.round(Date.now() / 1000) - 1 // 1 second ago
|
|
|
|
let bidHash = hashBid(requestHash, expired, price)
|
|
|
|
await expect(StorageContract.deploy(
|
|
|
|
duration,
|
|
|
|
size,
|
|
|
|
contentHash,
|
|
|
|
price,
|
|
|
|
proofPeriod,
|
|
|
|
proofTimeout,
|
|
|
|
expired,
|
|
|
|
await host.getAddress(),
|
|
|
|
await sign(client, requestHash),
|
|
|
|
await sign(host, bidHash),
|
|
|
|
)).to.be.revertedWith("Bid expired")
|
|
|
|
})
|
|
|
|
|
2021-10-14 10:37:14 +00:00
|
|
|
describe("proofs", function () {
|
|
|
|
|
|
|
|
async function mineBlock() {
|
|
|
|
await ethers.provider.send("evm_mine")
|
|
|
|
}
|
|
|
|
|
|
|
|
async function minedBlockNumber() {
|
|
|
|
return await ethers.provider.getBlockNumber() - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
async function mineUntilProofIsRequired() {
|
|
|
|
while (!await contract.isProofRequired(await minedBlockNumber())) {
|
|
|
|
mineBlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
contract = await StorageContract.deploy(
|
|
|
|
duration,
|
|
|
|
size,
|
2021-10-14 12:49:29 +00:00
|
|
|
contentHash,
|
2021-10-14 10:37:14 +00:00
|
|
|
price,
|
|
|
|
proofPeriod,
|
|
|
|
proofTimeout,
|
2021-10-18 13:29:58 +00:00
|
|
|
bidExpiry,
|
2021-10-14 10:37:14 +00:00
|
|
|
await host.getAddress(),
|
|
|
|
await sign(client, requestHash),
|
|
|
|
await sign(host, bidHash)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("requires on average a proof every period", async function () {
|
|
|
|
let blocks = 400
|
|
|
|
let proofs = 0
|
|
|
|
for (i=0; i<blocks; i++) {
|
|
|
|
await mineBlock()
|
|
|
|
if (await contract.isProofRequired(await minedBlockNumber())) {
|
|
|
|
proofs += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let average = blocks / proofs
|
|
|
|
expect(average).to.be.closeTo(proofPeriod, proofPeriod / 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("requires no proof for blocks that are unavailable", async function () {
|
|
|
|
await mineUntilProofIsRequired()
|
|
|
|
let blocknumber = await minedBlockNumber()
|
|
|
|
for (i=0; i<256; i++) { // only last 256 blocks are available in solidity
|
|
|
|
mineBlock()
|
|
|
|
}
|
|
|
|
expect(await contract.isProofRequired(blocknumber)).to.be.false
|
|
|
|
})
|
|
|
|
|
2021-10-18 12:55:59 +00:00
|
|
|
it("submits a correct proof", async function () {
|
|
|
|
await mineUntilProofIsRequired()
|
|
|
|
let blocknumber = await minedBlockNumber()
|
|
|
|
await contract.submitProof(blocknumber, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("fails proof submission when proof is incorrect", async function () {
|
|
|
|
await mineUntilProofIsRequired()
|
|
|
|
let blocknumber = await minedBlockNumber()
|
|
|
|
await expect(
|
|
|
|
contract.submitProof(blocknumber, false)
|
|
|
|
).to.be.revertedWith("Invalid proof")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("fails proof submission when proof was not required", async function () {
|
|
|
|
while (await contract.isProofRequired(await minedBlockNumber())) {
|
|
|
|
await mineBlock()
|
|
|
|
}
|
|
|
|
let blocknumber = await minedBlockNumber()
|
|
|
|
await expect(
|
|
|
|
contract.submitProof(blocknumber, true)
|
|
|
|
).to.be.revertedWith("No proof required")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("fails proof submission when proof is too late", async function () {
|
|
|
|
await mineUntilProofIsRequired()
|
|
|
|
let blocknumber = await minedBlockNumber()
|
|
|
|
for (let i=0; i<proofTimeout; i++) {
|
|
|
|
mineBlock()
|
|
|
|
}
|
|
|
|
await expect(
|
|
|
|
contract.submitProof(blocknumber, true)
|
|
|
|
).to.be.revertedWith("Proof not allowed after timeout")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("fails proof submission when already submitted", async function() {
|
|
|
|
await mineUntilProofIsRequired()
|
|
|
|
let blocknumber = await minedBlockNumber()
|
|
|
|
await contract.submitProof(blocknumber, true)
|
|
|
|
await expect(
|
|
|
|
contract.submitProof(blocknumber, true)
|
|
|
|
).to.be.revertedWith("Proof already submitted")
|
|
|
|
})
|
2021-10-14 10:37:14 +00:00
|
|
|
})
|
2021-10-12 14:59:34 +00:00
|
|
|
})
|
|
|
|
|
2021-10-18 12:55:59 +00:00
|
|
|
// TODO: implement checking of actual proofs of storage, instead of dummy bool
|
2021-10-12 14:59:34 +00:00
|
|
|
// TODO: payment on constructor
|
|
|
|
// TODO: contract start and timeout
|
|
|
|
// TODO: missed proofs
|
2021-10-14 10:37:14 +00:00
|
|
|
// TODO: only allow proofs after start of contract
|
2021-10-12 14:59:34 +00:00
|
|
|
// TODO: payout
|
|
|
|
// TODO: stake
|
2021-10-13 08:21:03 +00:00
|
|
|
// TODO: multiple hosts in single contract
|