diff --git a/contracts/Proofs.sol b/contracts/Proofs.sol index 7ca7e94..6a6c736 100644 --- a/contracts/Proofs.sol +++ b/contracts/Proofs.sol @@ -23,8 +23,16 @@ contract Proofs { return missed[id]; } + // Checks that proof timeout is <= 128. Only the latest 256 blocks can be + // checked in a smart contract, so that leaves a period of at least 128 blocks + // after timeout for a validator to signal the absence of a proof. + function _checkTimeout(uint timeout) private pure { + require(timeout <= 128, "Invalid proof timeout, needs to be <= 128"); + } + function _expectProofs(bytes32 id, uint period, uint timeout) internal { require(!ids[id], "Proof id already in use"); + _checkTimeout(timeout); ids[id] = true; periods[id] = period; timeouts[id] = timeout; diff --git a/contracts/StorageContracts.sol b/contracts/StorageContracts.sol index 5d57d5e..a8af7d8 100644 --- a/contracts/StorageContracts.sol +++ b/contracts/StorageContracts.sol @@ -76,7 +76,6 @@ contract StorageContracts is Proofs { bytes32 bidHash = hashBid(requestHash, _bidExpiry, _price); checkSignature(requestSignature, requestHash, msg.sender); checkSignature(bidSignature, bidHash, _host); - checkProofTimeout(_proofTimeout); checkBidExpiry(_bidExpiry); bytes32 contractId = bidHash; checkId(contractId); @@ -135,13 +134,6 @@ contract StorageContracts is Proofs { require(recovered == signer, "Invalid signature"); } - // Checks that proof timeout is <= 128. Only the latest 256 blocks can be - // checked in a smart contract, so that leaves a period of at least 128 blocks - // after timeout for a validator to signal the absence of a proof. - function checkProofTimeout(uint timeout) internal pure { - require(timeout <= 128, "Invalid proof timeout, needs to be <= 128"); - } - function checkBidExpiry(uint expiry) internal view { require(expiry > block.timestamp, "Bid expired"); } diff --git a/test/Proofs.test.js b/test/Proofs.test.js index 24e7c6b..c6a011f 100644 --- a/test/Proofs.test.js +++ b/test/Proofs.test.js @@ -27,6 +27,13 @@ describe("Proofs", function () { ).to.be.revertedWith("Proof id already in use") }) + it("does not allow a proof timeout that is too large", async function () { + let invalidTimeout = 129 // max proof timeout is 128 blocks + await expect( + proofs.expectProofs(id, period, invalidTimeout) + ).to.be.revertedWith("Invalid proof timeout") + }) + describe("when proofs are required", async function () { beforeEach(async function () { diff --git a/test/StorageContracts.test.js b/test/StorageContracts.test.js index a107905..4763dd9 100644 --- a/test/StorageContracts.test.js +++ b/test/StorageContracts.test.js @@ -154,32 +154,6 @@ describe("Storage Contracts", function () { )).to.be.revertedWith("Invalid signature") }) - it("cannot be created when proof timeout is too large", async function () { - let invalidTimeout = 129 // max proof timeout is 128 blocks - requestHash = hashRequest( - duration, - size, - contentHash, - proofPeriod, - invalidTimeout, - nonce - ) - bidHash = hashBid(requestHash, bidExpiry, price) - await expect(contracts.newContract( - duration, - size, - contentHash, - price, - proofPeriod, - invalidTimeout, - nonce, - bidExpiry, - await host.getAddress(), - await sign(client, requestHash), - await sign(host, bidHash), - )).to.be.revertedWith("Invalid proof timeout") - }) - it("cannot be created when bid has expired", async function () { let expired = Math.round(Date.now() / 1000) - 60 // 1 minute ago let bidHash = hashBid(requestHash, expired, price)