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]; 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 { function _expectProofs(bytes32 id, uint period, uint timeout) internal {
require(!ids[id], "Proof id already in use"); require(!ids[id], "Proof id already in use");
_checkTimeout(timeout);
ids[id] = true; ids[id] = true;
periods[id] = period; periods[id] = period;
timeouts[id] = timeout; timeouts[id] = timeout;

View File

@ -76,7 +76,6 @@ contract StorageContracts is Proofs {
bytes32 bidHash = hashBid(requestHash, _bidExpiry, _price); bytes32 bidHash = hashBid(requestHash, _bidExpiry, _price);
checkSignature(requestSignature, requestHash, msg.sender); checkSignature(requestSignature, requestHash, msg.sender);
checkSignature(bidSignature, bidHash, _host); checkSignature(bidSignature, bidHash, _host);
checkProofTimeout(_proofTimeout);
checkBidExpiry(_bidExpiry); checkBidExpiry(_bidExpiry);
bytes32 contractId = bidHash; bytes32 contractId = bidHash;
checkId(contractId); checkId(contractId);
@ -135,13 +134,6 @@ contract StorageContracts is Proofs {
require(recovered == signer, "Invalid signature"); 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 { function checkBidExpiry(uint expiry) internal view {
require(expiry > block.timestamp, "Bid expired"); require(expiry > block.timestamp, "Bid expired");
} }

View File

@ -27,6 +27,13 @@ describe("Proofs", function () {
).to.be.revertedWith("Proof id already in use") ).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 () { describe("when proofs are required", async function () {
beforeEach(async function () { beforeEach(async function () {

View File

@ -154,32 +154,6 @@ describe("Storage Contracts", function () {
)).to.be.revertedWith("Invalid signature") )).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 () { it("cannot be created when bid has expired", async function () {
let expired = Math.round(Date.now() / 1000) - 60 // 1 minute ago let expired = Math.round(Date.now() / 1000) - 60 // 1 minute ago
let bidHash = hashBid(requestHash, expired, price) let bidHash = hashBid(requestHash, expired, price)