From 54cc2987df798e479b046830a659fb5ef2af207c Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 4 Nov 2021 11:18:05 +0100 Subject: [PATCH] Ensure that finishing a contract can only be done once --- contracts/Storage.sol | 4 ++++ test/Storage.test.js | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/contracts/Storage.sol b/contracts/Storage.sol index 9b8cafe..959f914 100644 --- a/contracts/Storage.sol +++ b/contracts/Storage.sol @@ -9,6 +9,8 @@ contract Storage is Contracts, Proofs, Stakes { uint private stakeAmount; + mapping(bytes32=>bool) private finished; + constructor(IERC20 token, uint _stakeAmount) Stakes(token) { stakeAmount = _stakeAmount; } @@ -56,7 +58,9 @@ contract Storage is Contracts, Proofs, Stakes { function finishContract(bytes32 id) public { require(block.number > proofEnd(id), "Contract has not ended yet"); + require(!finished[id], "Contract already finished"); _unlockStake(host(id)); + finished[id] = true; } function duration(bytes32 contractId) public view returns (uint) { diff --git a/test/Storage.test.js b/test/Storage.test.js index 8027b88..e9ac522 100644 --- a/test/Storage.test.js +++ b/test/Storage.test.js @@ -101,6 +101,17 @@ describe("Storage", function () { storage.finishContract(id) ).to.be.revertedWith("Contract has not ended yet") }) + + it("can only be done once", async function () { + const end = await storage.proofEnd(id) + while (await minedBlockNumber() < end) { + await mineBlock() + } + await storage.finishContract(id) + await expect( + storage.finishContract(id) + ).to.be.revertedWith("Contract already finished") + }) }) })