From f224cb8eda74a166f0bb3020032026a8dec26466 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 10 Jan 2023 12:34:18 +0100 Subject: [PATCH] [Proofs] Remove double administration of request start The timestamp when proofs start to be required was stored in both the Marketplace and Proofs. It is now only stored in the Marketplace. --- contracts/Marketplace.sol | 8 ++++++++ contracts/Proofs.sol | 8 +++++--- contracts/TestProofs.sol | 9 +++++++++ test/Proofs.test.js | 4 ++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index 659102d..8e48671 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -320,10 +320,18 @@ contract Marketplace is Collateral, Proofs { return _timeout(); } + function proofStart(SlotId slotId) public view override returns (uint256) { + return requestStart(_slot(slotId).requestId); + } + function proofEnd(SlotId slotId) public view override returns (uint256) { return requestEnd(_slot(slotId).requestId); } + function requestStart(RequestId requestId) public view returns (uint256) { + return _context(requestId).startedAt; + } + function requestEnd(RequestId requestId) public view returns (uint256) { uint256 end = _context(requestId).endsAt; if (_requestAcceptsProofs(requestId)) { diff --git a/contracts/Proofs.sol b/contracts/Proofs.sol index c9bd1f5..6df43b9 100644 --- a/contracts/Proofs.sol +++ b/contracts/Proofs.sol @@ -16,7 +16,6 @@ abstract contract Proofs { } mapping(SlotId => bool) private slotIds; - mapping(SlotId => uint256) private starts; mapping(SlotId => uint256) private probabilities; mapping(SlotId => uint256) private missed; mapping(SlotId => mapping(uint256 => bool)) private received; @@ -30,6 +29,10 @@ abstract contract Proofs { return timeout; } + // Override this to let the proving system know when proofs for a + // slot are required. + function proofStart(SlotId id) public view virtual returns (uint256); + // Override this to let the proving system know when proofs for a // slot are no longer required. function proofEnd(SlotId id) public view virtual returns (uint256); @@ -52,7 +55,6 @@ abstract contract Proofs { function _expectProofs(SlotId id, uint256 probability) internal { require(!slotIds[id], "Slot id already in use"); slotIds[id] = true; - starts[id] = block.timestamp; probabilities[id] = probability; } @@ -97,7 +99,7 @@ abstract contract Proofs { SlotId id, uint256 proofPeriod ) internal view returns (bool isRequired, uint8 pointer) { - if (proofPeriod <= periodOf(starts[id])) { + if (proofPeriod <= periodOf(proofStart(id))) { return (false, 0); } uint256 end = proofEnd(id); diff --git a/contracts/TestProofs.sol b/contracts/TestProofs.sol index 634d3b6..fe05c8a 100644 --- a/contracts/TestProofs.sol +++ b/contracts/TestProofs.sol @@ -5,6 +5,7 @@ import "./Proofs.sol"; // exposes internal functions of Proofs for testing contract TestProofs is Proofs { + mapping(SlotId => uint256) private starts; mapping(SlotId => uint256) private ends; constructor( @@ -18,6 +19,10 @@ contract TestProofs is Proofs { } + function proofStart(SlotId slotId) public view override returns (uint256) { + return starts[slotId]; + } + function proofEnd(SlotId slotId) public view override returns (uint256) { return ends[slotId]; } @@ -58,6 +63,10 @@ contract TestProofs is Proofs { _markProofAsMissing(id, _period); } + function setProofStart(SlotId id, uint256 start) public { + starts[id] = start; + } + function setProofEnd(SlotId id, uint256 end) public { ends[id] = end; } diff --git a/test/Proofs.test.js b/test/Proofs.test.js index 3fda82d..978a2de 100644 --- a/test/Proofs.test.js +++ b/test/Proofs.test.js @@ -37,6 +37,7 @@ describe("Proofs", function () { describe("general", function () { beforeEach(async function () { + await proofs.setProofStart(slotId, await currentTime()) await proofs.setProofEnd(slotId, (await currentTime()) + duration) }) @@ -89,6 +90,7 @@ describe("Proofs", function () { let id2 = hexlify(randomBytes(32)) let id3 = hexlify(randomBytes(32)) for (let slotId of [id1, id2, id3]) { + await proofs.setProofStart(slotId, await currentTime()) await proofs.setProofEnd(slotId, (await currentTime()) + duration) await proofs.expectProofs(slotId, probability) } @@ -120,6 +122,7 @@ describe("Proofs", function () { } beforeEach(async function () { + await proofs.setProofStart(slotId, await currentTime()) await proofs.setProofEnd(slotId, (await currentTime()) + duration) await proofs.expectProofs(slotId, probability) await advanceTimeTo(periodEnd(periodOf(await currentTime()))) @@ -154,6 +157,7 @@ describe("Proofs", function () { const proof = hexlify(randomBytes(42)) beforeEach(async function () { + await proofs.setProofStart(slotId, await currentTime()) await proofs.setProofEnd(slotId, (await currentTime()) + duration) await proofs.expectProofs(slotId, probability) })