Move timeout validity check to Proofs contract

This commit is contained in:
Mark Spanbroek 2021-11-01 15:28:22 +01:00
parent d5dede6e6b
commit aa0def1127
4 changed files with 15 additions and 34 deletions

View File

@ -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;

View File

@ -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");
}

View File

@ -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 () {

View File

@ -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)