From fd06bc00b3017780aa539828aae44931e44146da Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 9 Mar 2022 11:11:01 +0100 Subject: [PATCH] Move periodOf(), periodStart(), periodEnd() into time module --- test/Proofs.test.js | 15 +++------------ test/Storage.test.js | 16 +++------------- test/time.js | 8 +++++++- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/test/Proofs.test.js b/test/Proofs.test.js index 24a0fb8..ebfb695 100644 --- a/test/Proofs.test.js +++ b/test/Proofs.test.js @@ -8,6 +8,7 @@ const { advanceTime, advanceTimeTo, } = require("./evm") +const { periodic } = require("./time") describe("Proofs", function () { const id = ethers.utils.randomBytes(32) @@ -103,6 +104,8 @@ describe("Proofs", function () { }) describe("when proofs are required", async function () { + const { periodOf, periodEnd } = periodic(period) + beforeEach(async function () { await proofs.expectProofs(id, probability, duration) }) @@ -113,18 +116,6 @@ describe("Proofs", function () { } } - function periodOf(timestamp) { - return Math.floor(timestamp / period) - } - - function periodStart(p) { - return period * p - } - - function periodEnd(p) { - return periodStart(p + 1) - } - it("submits a correct proof", async function () { await proofs.submitProof(id, true) }) diff --git a/test/Storage.test.js b/test/Storage.test.js index b4bbd08..9162a57 100644 --- a/test/Storage.test.js +++ b/test/Storage.test.js @@ -9,6 +9,7 @@ const { currentTime, } = require("./evm") const { requestId, offerId } = require("./ids") +const { periodic } = require("./time") describe("Storage", function () { let storage @@ -126,25 +127,14 @@ describe("Storage", function () { }) describe("slashing when missing proofs", function () { - let period + let period, periodOf, periodEnd beforeEach(async function () { switchAccount(host) period = (await storage.proofPeriod()).toNumber() + ;({ periodOf, periodEnd } = periodic(period)) }) - function periodOf(timestamp) { - return Math.floor(timestamp / period) - } - - function periodStart(p) { - return period * p - } - - function periodEnd(p) { - return periodStart(p + 1) - } - async function ensureProofIsMissing() { let currentPeriod = periodOf(await currentTime()) await advanceTimeTo(periodEnd(currentPeriod)) diff --git a/test/time.js b/test/time.js index 6144f12..1992f24 100644 --- a/test/time.js +++ b/test/time.js @@ -3,4 +3,10 @@ const hours = (amount) => amount * minutes(60) const minutes = (amount) => amount * seconds(60) const seconds = (amount) => amount -module.exports = { now, hours, minutes, seconds } +const periodic = (length) => ({ + periodOf: (timestamp) => Math.floor(timestamp / length), + periodStart: (period) => period * length, + periodEnd: (period) => (period + 1) * length, +}) + +module.exports = { now, hours, minutes, seconds, periodic }