2021-10-12 14:59:34 +00:00
|
|
|
const { expect } = require("chai")
|
|
|
|
const { ethers } = require("hardhat")
|
|
|
|
const { hashRequest, hashBid, sign } = require("./marketplace")
|
2021-11-02 08:45:49 +00:00
|
|
|
const { exampleRequest, exampleBid } = require("./examples")
|
2021-10-12 14:59:34 +00:00
|
|
|
|
2021-11-01 15:34:01 +00:00
|
|
|
describe("Storage", function () {
|
2021-10-12 14:59:34 +00:00
|
|
|
|
2021-11-02 11:45:09 +00:00
|
|
|
const stakeAmount = 100
|
2021-11-02 10:19:52 +00:00
|
|
|
const request = exampleRequest()
|
|
|
|
const bid = exampleBid()
|
|
|
|
|
|
|
|
let storage
|
|
|
|
let token
|
|
|
|
let client, host
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
[client, host] = await ethers.getSigners()
|
|
|
|
let Token = await ethers.getContractFactory("TestToken")
|
|
|
|
let StorageContracts = await ethers.getContractFactory("Storage")
|
|
|
|
token = await Token.connect(host).deploy()
|
2021-11-02 11:45:09 +00:00
|
|
|
storage = await StorageContracts.deploy(token.address, stakeAmount)
|
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-10-12 14:59:34 +00:00
|
|
|
|
2021-11-01 15:34:01 +00:00
|
|
|
let id
|
2021-10-12 14:59:34 +00:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
2021-11-02 11:45:09 +00:00
|
|
|
await token.approve(storage.address, stakeAmount)
|
|
|
|
await storage.connect(host).increaseStake(stakeAmount)
|
2021-11-02 08:45:49 +00:00
|
|
|
let requestHash = hashRequest(request)
|
|
|
|
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())
|
|
|
|
})
|
2021-11-02 11:50:06 +00:00
|
|
|
|
|
|
|
it("locks up host stake", async function () {
|
|
|
|
await expect(
|
|
|
|
storage.connect(host).withdrawStake()
|
|
|
|
).to.be.revertedWith("Stake locked")
|
|
|
|
})
|
2021-11-04 08:49:07 +00:00
|
|
|
|
|
|
|
describe("starting the contract", function () {
|
|
|
|
it("starts requiring storage proofs", async function (){
|
|
|
|
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 08:49:07 +00:00
|
|
|
})
|
2021-10-12 14:59:34 +00:00
|
|
|
})
|
2021-11-02 10:19:52 +00:00
|
|
|
|
2021-11-03 16:20:33 +00:00
|
|
|
it("doesn't create contract with insufficient stake", async function () {
|
2021-11-02 11:45:09 +00:00
|
|
|
await token.approve(storage.address, stakeAmount - 1)
|
|
|
|
await storage.connect(host).increaseStake(stakeAmount - 1)
|
2021-11-02 10:19:52 +00:00
|
|
|
let requestHash = hashRequest(request)
|
|
|
|
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("Insufficient stake")
|
|
|
|
})
|
2021-10-12 14:59:34 +00:00
|
|
|
})
|
|
|
|
|
2021-11-02 10:19:52 +00:00
|
|
|
// TODO: unlock stake at end of contract
|
|
|
|
// TODO: payment when new contract
|
2021-10-12 14:59:34 +00:00
|
|
|
// TODO: contract start and timeout
|
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-10-14 10:37:14 +00:00
|
|
|
// TODO: only allow proofs after start of contract
|
2021-11-02 10:19:52 +00:00
|
|
|
// TODO: proofs no longer required after contract duration
|
2021-10-12 14:59:34 +00:00
|
|
|
// TODO: payout
|