Move periodOf(), periodStart(), periodEnd() into time module

This commit is contained in:
Mark Spanbroek 2022-03-09 11:11:01 +01:00 committed by markspanbroek
parent 07d0e33789
commit fd06bc00b3
3 changed files with 13 additions and 26 deletions

View File

@ -8,6 +8,7 @@ const {
advanceTime, advanceTime,
advanceTimeTo, advanceTimeTo,
} = require("./evm") } = require("./evm")
const { periodic } = require("./time")
describe("Proofs", function () { describe("Proofs", function () {
const id = ethers.utils.randomBytes(32) const id = ethers.utils.randomBytes(32)
@ -103,6 +104,8 @@ describe("Proofs", function () {
}) })
describe("when proofs are required", async function () { describe("when proofs are required", async function () {
const { periodOf, periodEnd } = periodic(period)
beforeEach(async function () { beforeEach(async function () {
await proofs.expectProofs(id, probability, duration) 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 () { it("submits a correct proof", async function () {
await proofs.submitProof(id, true) await proofs.submitProof(id, true)
}) })

View File

@ -9,6 +9,7 @@ const {
currentTime, currentTime,
} = require("./evm") } = require("./evm")
const { requestId, offerId } = require("./ids") const { requestId, offerId } = require("./ids")
const { periodic } = require("./time")
describe("Storage", function () { describe("Storage", function () {
let storage let storage
@ -126,25 +127,14 @@ describe("Storage", function () {
}) })
describe("slashing when missing proofs", function () { describe("slashing when missing proofs", function () {
let period let period, periodOf, periodEnd
beforeEach(async function () { beforeEach(async function () {
switchAccount(host) switchAccount(host)
period = (await storage.proofPeriod()).toNumber() 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() { async function ensureProofIsMissing() {
let currentPeriod = periodOf(await currentTime()) let currentPeriod = periodOf(await currentTime())
await advanceTimeTo(periodEnd(currentPeriod)) await advanceTimeTo(periodEnd(currentPeriod))

View File

@ -3,4 +3,10 @@ const hours = (amount) => amount * minutes(60)
const minutes = (amount) => amount * seconds(60) const minutes = (amount) => amount * seconds(60)
const seconds = (amount) => amount 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 }