From 376962322d98f804348745fbf75bc4725a1802bf Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 4 Nov 2021 09:49:07 +0100 Subject: [PATCH] Only expect proofs when the contract is started --- contracts/Contracts.sol | 12 ++++++++++++ contracts/Storage.sol | 15 +++++++++++---- test/Storage.test.js | 16 ++++++++++------ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/contracts/Contracts.sol b/contracts/Contracts.sol index c01a7d3..45f9c79 100644 --- a/contracts/Contracts.sol +++ b/contracts/Contracts.sol @@ -9,6 +9,8 @@ contract Contracts { mapping(bytes32=>uint) private durations; // contract duration in blocks mapping(bytes32=>uint) private sizes; // storage size in bytes mapping(bytes32=>bytes32) private contentHashes; // hash of data to be stored + mapping(bytes32=>uint) private proofPeriods; // period between proofs + mapping(bytes32=>uint) private proofTimeouts; // timeout for proof submission mapping(bytes32=>uint) private prices; // price in coins mapping(bytes32=>address) private hosts; // host that provides storage @@ -24,6 +26,14 @@ contract Contracts { return contentHashes[id]; } + function _proofPeriod(bytes32 id) internal view returns (uint) { + return proofPeriods[id]; + } + + function _proofTimeout(bytes32 id) internal view returns (uint) { + return proofTimeouts[id]; + } + function _price(bytes32 id) internal view returns (uint) { return prices[id]; } @@ -66,6 +76,8 @@ contract Contracts { durations[id] = duration; sizes[id] = size; contentHashes[id] = contentHash; + proofPeriods[id] = proofPeriod; + proofTimeouts[id] = proofTimeout; prices[id] = price; hosts[id] = host; } diff --git a/contracts/Storage.sol b/contracts/Storage.sol index 1b42779..04ba9a6 100644 --- a/contracts/Storage.sol +++ b/contracts/Storage.sol @@ -30,7 +30,7 @@ contract Storage is Contracts, Proofs, Stakes { { require(_stake(_host) >= stakeAmount, "Insufficient stake"); _lockStake(_host); - bytes32 id = _newContract( + _newContract( _duration, _size, _contentHash, @@ -43,7 +43,10 @@ contract Storage is Contracts, Proofs, Stakes { requestSignature, bidSignature ); - _expectProofs(id, _proofPeriod, _proofTimeout, _duration); + } + + function startContract(bytes32 id) public { + _expectProofs(id, proofPeriod(id), proofTimeout(id), duration(id)); } function duration(bytes32 contractId) public view returns (uint) { @@ -67,11 +70,15 @@ contract Storage is Contracts, Proofs, Stakes { } function proofPeriod(bytes32 contractId) public view returns (uint) { - return _period(contractId); + return _proofPeriod(contractId); } function proofTimeout(bytes32 contractId) public view returns (uint) { - return _timeout(contractId); + return _proofTimeout(contractId); + } + + function proofEnd(bytes32 contractId) public view returns (uint) { + return _end(contractId); } function missingProofs(bytes32 contractId) public view returns (uint) { diff --git a/test/Storage.test.js b/test/Storage.test.js index 4f26f8a..e7850d0 100644 --- a/test/Storage.test.js +++ b/test/Storage.test.js @@ -50,20 +50,24 @@ describe("Storage", function () { expect(await storage.duration(id)).to.equal(request.duration) expect(await storage.size(id)).to.equal(request.size) expect(await storage.contentHash(id)).to.equal(request.contentHash) - expect(await storage.price(id)).to.equal(bid.price) - expect(await storage.host(id)).to.equal(await host.getAddress()) - }) - - it("requires storage proofs", async function (){ expect(await storage.proofPeriod(id)).to.equal(request.proofPeriod) expect(await storage.proofTimeout(id)).to.equal(request.proofTimeout) - }) + expect(await storage.price(id)).to.equal(bid.price) + expect(await storage.host(id)).to.equal(await host.getAddress()) + }) it("locks up host stake", async function () { await expect( storage.connect(host).withdrawStake() ).to.be.revertedWith("Stake locked") }) + + describe("starting the contract", function () { + it("starts requiring storage proofs", async function (){ + await storage.connect(host).startContract(id) + expect(await storage.proofEnd(id)).to.be.gt(0) + }) + }) }) it("doesn't create contract with insufficient stake", async function () {