diff --git a/contracts/StorageContract.sol b/contracts/StorageContract.sol index dc0b755..8212c5d 100644 --- a/contracts/StorageContract.sol +++ b/contracts/StorageContract.sol @@ -13,6 +13,8 @@ contract StorageContract { uint public immutable proofTimeout; // proof has to be submitted before this uint public immutable proofMarker; // indicates when a proof is required + mapping(uint => bool) proofReceived; // whether proof for a block was received + constructor(uint _duration, uint _size, bytes32 _contentHash, @@ -101,4 +103,15 @@ contract StorageContract { bytes32 hash = blockhash(blocknumber); return hash != 0 && uint(hash) % proofPeriod == proofMarker; } + + function submitProof(uint blocknumber, bool proof) public { + require(proof, "Invalid proof"); // TODO: replace bool by actual proof + require(isProofRequired(blocknumber), "No proof required for this block"); + require( + block.number < blocknumber + proofTimeout, + "Proof not allowed after timeout" + ); + require(!proofReceived[blocknumber], "Proof already submitted"); + proofReceived[blocknumber] = true; + } } diff --git a/test/StorageContract.test.js b/test/StorageContract.test.js index 511672c..18f84cb 100644 --- a/test/StorageContract.test.js +++ b/test/StorageContract.test.js @@ -186,13 +186,56 @@ describe("Storage Contract", function () { expect(await contract.isProofRequired(blocknumber)).to.be.false }) + it("submits a correct proof", async function () { + await mineUntilProofIsRequired() + let blocknumber = await minedBlockNumber() + await contract.submitProof(blocknumber, true) + }) + + it("fails proof submission when proof is incorrect", async function () { + await mineUntilProofIsRequired() + let blocknumber = await minedBlockNumber() + await expect( + contract.submitProof(blocknumber, false) + ).to.be.revertedWith("Invalid proof") + }) + + it("fails proof submission when proof was not required", async function () { + while (await contract.isProofRequired(await minedBlockNumber())) { + await mineBlock() + } + let blocknumber = await minedBlockNumber() + await expect( + contract.submitProof(blocknumber, true) + ).to.be.revertedWith("No proof required") + }) + + it("fails proof submission when proof is too late", async function () { + await mineUntilProofIsRequired() + let blocknumber = await minedBlockNumber() + for (let i=0; i