From 7726db2a8d766081117e9816d1320cfe36525282 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Tue, 13 Sep 2022 17:13:11 +1000 Subject: [PATCH] modify collateral threshold test to remove TestStorage dep --- contracts/Marketplace.sol | 3 +-- contracts/TestStorage.sol | 36 ------------------------------ deploy/test/storage.js | 36 ------------------------------ test/Storage.test.js | 47 ++++++++++++++------------------------- 4 files changed, 18 insertions(+), 104 deletions(-) delete mode 100644 contracts/TestStorage.sol delete mode 100644 deploy/test/storage.js diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index a28a813..cf38276 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -87,7 +87,6 @@ contract Marketplace is Collateral, Proofs { Request storage request = _request(requestId); require(slotIndex < request.ask.slots, "Invalid slot"); RequestContext storage context = requestContexts[requestId]; - // TODO: change below to check !_isCancelled(requestId) instead? require(!_isCancelled(requestId), "Request cancelled"); // TODO: in the case of repair, update below require condition by adding // || context.state == RequestState.Started @@ -194,7 +193,7 @@ contract Marketplace is Collateral, Proofs { function _request(bytes32 id) internal view returns (Request storage) { Request storage request = requests[id]; require(request.client != address(0), "Unknown request"); - return requests[id]; + return request; } function _slot(bytes32 slotId) internal view returns (Slot storage) { diff --git a/contracts/TestStorage.sol b/contracts/TestStorage.sol deleted file mode 100644 index c4986c4..0000000 --- a/contracts/TestStorage.sol +++ /dev/null @@ -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); - } -} diff --git a/deploy/test/storage.js b/deploy/test/storage.js deleted file mode 100644 index 675d0de..0000000 --- a/deploy/test/storage.js +++ /dev/null @@ -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"] diff --git a/test/Storage.test.js b/test/Storage.test.js index 849a57d..124840e 100644 --- a/test/Storage.test.js +++ b/test/Storage.test.js @@ -28,9 +28,9 @@ describe("Storage", function () { beforeEach(async function () { ;[client, host] = await ethers.getSigners() - await deployments.fixture(["TestToken", "TestStorage"]) + await deployments.fixture(["TestToken", "Storage"]) 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(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 () { const id = slotId(slot) @@ -231,25 +220,23 @@ describe("Storage", function () { proof ) - while (true) { - await markProofAsMissing(id) - let balance = await storage.balanceOf(host.address) - let slashAmount = await storage.slashAmount( - host.address, - slashPercentage - ) - if (balance - slashAmount < minCollateralThreshold) { - break + // max slashes before dropping below collateral threshold + const maxSlashes = 10 + for (let i = 0; i < maxSlashes; i++) { + for (let j = 0; j < slashMisses; j++) { + await waitUntilProofIsRequired(id) + let missedPeriod = periodOf(await currentTime()) + await advanceTime(period) + if (i === maxSlashes - 1 && j === slashMisses - 1) { + 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") }) }) })