diff --git a/contracts/Storage.sol b/contracts/Storage.sol index caf495c..2ae9cc4 100644 --- a/contracts/Storage.sol +++ b/contracts/Storage.sol @@ -10,7 +10,7 @@ contract Storage is Collateral, Marketplace, Proofs { uint256 public slashMisses; uint256 public slashPercentage; - mapping(bytes32 => bool) private finished; + mapping(bytes32 => ContractState) private contractState; constructor( IERC20 token, @@ -33,14 +33,15 @@ contract Storage is Collateral, Marketplace, Proofs { Offer storage offer = _offer(id); require(msg.sender == offer.host, "Only host can call this function"); require(_selectedOffer(offer.requestId) == id, "Offer was not selected"); + contractState[id] = ContractState.started; Request storage request = _request(offer.requestId); _expectProofs(id, request.proofProbability, request.duration); } function finishContract(bytes32 id) public { + require(contractState[id] == ContractState.started, "Contract not started"); require(block.timestamp > proofEnd(id), "Contract has not ended yet"); - require(!finished[id], "Contract already finished"); - finished[id] = true; + contractState[id] = ContractState.finished; Offer storage offer = _offer(id); require(token.transfer(offer.host, offer.price), "Payment failed"); } @@ -84,4 +85,10 @@ contract Storage is Collateral, Marketplace, Proofs { _slash(offer.host, slashPercentage); } } + + enum ContractState { + none, + started, + finished + } } diff --git a/test/Storage.test.js b/test/Storage.test.js index 235ad1e..b0c9e5e 100644 --- a/test/Storage.test.js +++ b/test/Storage.test.js @@ -84,7 +84,6 @@ describe("Storage", function () { describe("finishing the contract", function () { beforeEach(async function () { switchAccount(host) - await storage.startContract(id) }) async function waitUntilEnd() { @@ -93,12 +92,14 @@ describe("Storage", function () { } it("unlocks the host collateral", async function () { + await storage.startContract(id) await waitUntilEnd() await storage.finishContract(id) await expect(storage.withdraw()).not.to.be.reverted }) it("pays the host", async function () { + await storage.startContract(id) await waitUntilEnd() const startBalance = await token.balanceOf(host.address) await storage.finishContract(id) @@ -106,18 +107,31 @@ describe("Storage", function () { expect(endBalance - startBalance).to.equal(offer.price) }) + it("is only allowed when the contract has started", async function () { + await expect(storage.finishContract(id)).to.be.revertedWith( + "Contract not started" + ) + }) + it("is only allowed when end time has passed", async function () { + await storage.startContract(id) await expect(storage.finishContract(id)).to.be.revertedWith( "Contract has not ended yet" ) }) it("can only be done once", async function () { + await storage.startContract(id) await waitUntilEnd() await storage.finishContract(id) - await expect(storage.finishContract(id)).to.be.revertedWith( - "Contract already finished" - ) + await expect(storage.finishContract(id)).to.be.reverted + }) + + it("can not be restarted", async function () { + await storage.startContract(id) + await waitUntilEnd() + await storage.finishContract(id) + await expect(storage.startContract(id)).to.be.reverted }) })