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
|
2023-01-09 13:46:03 +01:00
|
|
|
const { AddressZero } = ethers.constants
|
2022-06-13 12:17:37 +02:00
|
|
|
const { exampleRequest } = require("./examples")
|
2023-01-09 15:15:11 +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")
|
2022-07-20 11:07:20 +02:00
|
|
|
const { price } = require("./price")
|
2022-09-21 19:57:26 +10:00
|
|
|
const {
|
|
|
|
|
waitUntilCancelled,
|
|
|
|
|
waitUntilStarted,
|
|
|
|
|
waitUntilFinished,
|
|
|
|
|
} = require("./marketplace")
|
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-09-22 12:21:49 +10:00
|
|
|
await deployments.fixture(["TestToken", "Storage"])
|
2022-02-09 14:17:23 +01:00
|
|
|
token = await ethers.getContract("TestToken")
|
2022-09-22 12:21:49 +10:00
|
|
|
storage = await ethers.getContract("Storage")
|
2022-02-22 09:25:42 +01:00
|
|
|
|
2022-07-20 11:07:20 +02:00
|
|
|
await token.mint(client.address, 1_000_000_000)
|
|
|
|
|
await token.mint(host.address, 1_000_000_000)
|
2022-02-22 09:25:42 +01:00
|
|
|
|
2023-01-09 15:52:27 +01:00
|
|
|
collateralAmount = await storage.collateral()
|
2021-11-16 14:26:28 +01:00
|
|
|
slashMisses = await storage.slashMisses()
|
|
|
|
|
slashPercentage = await storage.slashPercentage()
|
2022-09-13 17:18:55 +10:00
|
|
|
minCollateralThreshold = await storage.minCollateralThreshold()
|
2022-02-22 09:25:42 +01:00
|
|
|
|
2022-09-22 12:21:49 +10:00
|
|
|
request = await exampleRequest()
|
2022-02-22 09:25:42 +01:00
|
|
|
request.client = client.address
|
2022-07-20 10:05:36 +02:00
|
|
|
slot = {
|
|
|
|
|
request: requestId(request),
|
2022-07-20 10:44:22 +02:00
|
|
|
index: request.ask.slots / 2,
|
2022-07-20 10:05:36 +02:00
|
|
|
}
|
2022-02-22 09:25:42 +01:00
|
|
|
|
|
|
|
|
switchAccount(client)
|
2022-07-20 11:07:20 +02:00
|
|
|
await token.approve(storage.address, price(request))
|
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-07-20 10:05:36 +02:00
|
|
|
describe("ending the contract", function () {
|
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-10-25 14:49:37 +02:00
|
|
|
await waitUntilFinished(storage, slot.request)
|
2022-11-23 16:07:01 +01:00
|
|
|
await storage.freeSlot(slotId(slot))
|
2022-03-09 11:33:54 +01:00
|
|
|
await expect(storage.withdraw()).not.to.be.reverted
|
|
|
|
|
})
|
2022-02-22 09:25:42 +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
|