From 2bf01da728317fd1f3ae8424785f31b53eb4013f Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 12 Apr 2022 08:43:47 +0200 Subject: [PATCH] [proofs] Submitting proof emits event containing proof --- contracts/Proofs.sol | 7 +++++-- contracts/Storage.sol | 2 +- contracts/TestProofs.sol | 2 +- test/Proofs.test.js | 31 ++++++++++++++++++++----------- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/contracts/Proofs.sol b/contracts/Proofs.sol index 5627266..9aa1f70 100644 --- a/contracts/Proofs.sol +++ b/contracts/Proofs.sol @@ -136,10 +136,11 @@ contract Proofs { return isRequired && pointer < downtime; } - function _submitProof(bytes32 id, bool proof) internal { - require(proof, "Invalid proof"); // TODO: replace bool by actual proof + function _submitProof(bytes32 id, bytes calldata proof) internal { + require(proof.length > 0, "Invalid proof"); // TODO: replace by actual check require(!received[id][currentPeriod()], "Proof already submitted"); received[id][currentPeriod()] = true; + emit ProofSubmitted(id, proof); } function _markProofAsMissing(bytes32 id, uint256 missedPeriod) internal { @@ -152,4 +153,6 @@ contract Proofs { missing[id][missedPeriod] = true; missed[id] += 1; } + + event ProofSubmitted(bytes32 id, bytes proof); } diff --git a/contracts/Storage.sol b/contracts/Storage.sol index 5f68108..b28f69b 100644 --- a/contracts/Storage.sol +++ b/contracts/Storage.sol @@ -86,7 +86,7 @@ contract Storage is Collateral, Marketplace, Proofs { return _getPointer(id); } - function submitProof(bytes32 contractId, bool proof) public { + function submitProof(bytes32 contractId, bytes calldata proof) public { _submitProof(contractId, proof); } diff --git a/contracts/TestProofs.sol b/contracts/TestProofs.sol index 81aab4a..d844807 100644 --- a/contracts/TestProofs.sol +++ b/contracts/TestProofs.sol @@ -56,7 +56,7 @@ contract TestProofs is Proofs { return _getPointer(id); } - function submitProof(bytes32 id, bool proof) public { + function submitProof(bytes32 id, bytes calldata proof) public { _submitProof(id, proof); } diff --git a/test/Proofs.test.js b/test/Proofs.test.js index fc0fdb9..c36e5fc 100644 --- a/test/Proofs.test.js +++ b/test/Proofs.test.js @@ -1,5 +1,6 @@ const { expect } = require("chai") const { ethers } = require("hardhat") +const { hexlify, randomBytes } = ethers.utils const { snapshot, revert, @@ -12,7 +13,7 @@ const { const { periodic } = require("./time") describe("Proofs", function () { - const id = ethers.utils.randomBytes(32) + const id = hexlify(randomBytes(32)) const period = 30 * 60 const timeout = 5 const downtime = 64 @@ -84,9 +85,9 @@ describe("Proofs", function () { }) it("requires proofs for different ids at different times", async function () { - let id1 = ethers.utils.randomBytes(32) - let id2 = ethers.utils.randomBytes(32) - let id3 = ethers.utils.randomBytes(32) + let id1 = hexlify(randomBytes(32)) + let id2 = hexlify(randomBytes(32)) + let id3 = hexlify(randomBytes(32)) for (let id of [id1, id2, id3]) { await proofs.expectProofs(id, probability, duration) } @@ -140,6 +141,8 @@ describe("Proofs", function () { }) describe("when proofs are required", function () { + const proof = hexlify(randomBytes(42)) + beforeEach(async function () { await proofs.expectProofs(id, probability, duration) }) @@ -165,8 +168,8 @@ describe("Proofs", function () { }) it("provides different challenges per id", async function () { - const id2 = ethers.utils.randomBytes(32) - const id3 = ethers.utils.randomBytes(32) + const id2 = hexlify(randomBytes(32)) + const id3 = hexlify(randomBytes(32)) const challenge1 = await proofs.getChallenge(id) const challenge2 = await proofs.getChallenge(id2) const challenge3 = await proofs.getChallenge(id3) @@ -174,19 +177,25 @@ describe("Proofs", function () { }) it("submits a correct proof", async function () { - await proofs.submitProof(id, true) + await proofs.submitProof(id, proof) }) it("fails proof submission when proof is incorrect", async function () { - await expect(proofs.submitProof(id, false)).to.be.revertedWith( + await expect(proofs.submitProof(id, [])).to.be.revertedWith( "Invalid proof" ) }) + it("emits an event when proof was submitted", async function () { + await expect(proofs.submitProof(id, proof)) + .to.emit(proofs, "ProofSubmitted") + .withArgs(id, proof) + }) + it("fails proof submission when already submitted", async function () { await advanceTimeTo(periodEnd(periodOf(await currentTime()))) - await proofs.submitProof(id, true) - await expect(proofs.submitProof(id, true)).to.be.revertedWith( + await proofs.submitProof(id, proof) + await expect(proofs.submitProof(id, proof)).to.be.revertedWith( "Proof already submitted" ) }) @@ -220,7 +229,7 @@ describe("Proofs", function () { it("does not mark a submitted proof as missing", async function () { await waitUntilProofIsRequired(id) let submittedPeriod = periodOf(await currentTime()) - await proofs.submitProof(id, true) + await proofs.submitProof(id, proof) await advanceTimeTo(periodEnd(submittedPeriod)) await expect( proofs.markProofAsMissing(id, submittedPeriod)