modify collateral threshold test to remove TestStorage dep

This commit is contained in:
Eric Mastro 2022-09-13 17:13:11 +10:00 committed by Eric Mastro
parent 1ab6d700af
commit 7726db2a8d
4 changed files with 18 additions and 104 deletions

View File

@ -87,7 +87,6 @@ contract Marketplace is Collateral, Proofs {
Request storage request = _request(requestId); Request storage request = _request(requestId);
require(slotIndex < request.ask.slots, "Invalid slot"); require(slotIndex < request.ask.slots, "Invalid slot");
RequestContext storage context = requestContexts[requestId]; RequestContext storage context = requestContexts[requestId];
// TODO: change below to check !_isCancelled(requestId) instead?
require(!_isCancelled(requestId), "Request cancelled"); require(!_isCancelled(requestId), "Request cancelled");
// TODO: in the case of repair, update below require condition by adding // TODO: in the case of repair, update below require condition by adding
// || context.state == RequestState.Started // || context.state == RequestState.Started
@ -194,7 +193,7 @@ contract Marketplace is Collateral, Proofs {
function _request(bytes32 id) internal view returns (Request storage) { function _request(bytes32 id) internal view returns (Request storage) {
Request storage request = requests[id]; Request storage request = requests[id];
require(request.client != address(0), "Unknown request"); require(request.client != address(0), "Unknown request");
return requests[id]; return request;
} }
function _slot(bytes32 slotId) internal view returns (Slot storage) { function _slot(bytes32 slotId) internal view returns (Slot storage) {

View File

@ -1,36 +0,0 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./Storage.sol";
// exposes internal functions of Storage for testing
contract TestStorage is Storage {
constructor(
IERC20 token,
uint256 _proofPeriod,
uint256 _proofTimeout,
uint8 _proofDowntime,
uint256 _collateralAmount,
uint256 _slashMisses,
uint256 _slashPercentage,
uint256 _minCollateralThreshold
)
Storage(
token,
_proofPeriod,
_proofTimeout,
_proofDowntime,
_collateralAmount,
_slashMisses,
_slashPercentage,
_minCollateralThreshold
)
// solhint-disable-next-line no-empty-blocks
{
}
function slashAmount(address account, uint256 percentage) public view returns (uint256) {
return _slashAmount(account, percentage);
}
}

View File

@ -1,36 +0,0 @@
async function deployStorage({ deployments, getNamedAccounts }) {
const token = await deployments.get("TestToken")
const proofPeriod = 10
const proofTimeout = 5
const proofDowntime = 64
const collateralAmount = 100
const slashMisses = 3
const slashPercentage = 10
const minCollateralThreshold = 40
const args = [
token.address,
proofPeriod,
proofTimeout,
proofDowntime,
collateralAmount,
slashMisses,
slashPercentage,
minCollateralThreshold,
]
const { deployer } = await getNamedAccounts()
await deployments.deploy("TestStorage", { args, from: deployer })
}
async function mine256blocks({ network, ethers }) {
if (network.tags.local) {
await ethers.provider.send("hardhat_mine", ["0x100"])
}
}
module.exports = async (environment) => {
await mine256blocks(environment)
await deployStorage(environment)
}
module.exports.tags = ["TestStorage"]
module.exports.dependencies = ["TestToken"]

View File

@ -28,9 +28,9 @@ describe("Storage", function () {
beforeEach(async function () { beforeEach(async function () {
;[client, host] = await ethers.getSigners() ;[client, host] = await ethers.getSigners()
await deployments.fixture(["TestToken", "TestStorage"]) await deployments.fixture(["TestToken", "Storage"])
token = await ethers.getContract("TestToken") token = await ethers.getContract("TestToken")
storage = await ethers.getContract("TestStorage") storage = await ethers.getContract("Storage")
await token.mint(client.address, 1_000_000_000) await token.mint(client.address, 1_000_000_000)
await token.mint(host.address, 1_000_000_000) await token.mint(host.address, 1_000_000_000)
@ -210,17 +210,6 @@ describe("Storage", function () {
} }
} }
async function markProofAsMissing(slotId, onMarkAsMissing) {
for (let i = 0; i < slashMisses; i++) {
await waitUntilProofIsRequired(slotId)
let missedPeriod = periodOf(await currentTime())
await advanceTime(period)
if (i === slashMisses - 1 && typeof onMarkAsMissing === "function") {
onMarkAsMissing(missedPeriod)
} else await storage.markProofAsMissing(slotId, missedPeriod)
}
}
it("frees slot when collateral slashed below minimum threshold", async function () { it("frees slot when collateral slashed below minimum threshold", async function () {
const id = slotId(slot) const id = slotId(slot)
@ -231,25 +220,23 @@ describe("Storage", function () {
proof proof
) )
while (true) { // max slashes before dropping below collateral threshold
await markProofAsMissing(id) const maxSlashes = 10
let balance = await storage.balanceOf(host.address) for (let i = 0; i < maxSlashes; i++) {
let slashAmount = await storage.slashAmount( for (let j = 0; j < slashMisses; j++) {
host.address, await waitUntilProofIsRequired(id)
slashPercentage let missedPeriod = periodOf(await currentTime())
) await advanceTime(period)
if (balance - slashAmount < minCollateralThreshold) { if (i === maxSlashes - 1 && j === slashMisses - 1) {
break await expect(
await storage.markProofAsMissing(id, missedPeriod)
).to.emit(storage, "SlotFreed")
await expect(storage.getSlot(id)).to.be.revertedWith("Slot empty")
} else {
await storage.markProofAsMissing(id, missedPeriod)
}
} }
} }
let onMarkAsMissing = async function (missedPeriod) {
await expect(
await storage.markProofAsMissing(id, missedPeriod)
).to.emit(storage, "SlotFreed")
}
await markProofAsMissing(id, onMarkAsMissing)
await expect(storage.getSlot(id)).to.be.revertedWith("Slot empty")
}) })
}) })
}) })