Move periodOf(), periodStart(), periodEnd() into time module
This commit is contained in:
parent
07d0e33789
commit
fd06bc00b3
|
@ -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)
|
||||||
})
|
})
|
||||||
|
|
|
@ -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))
|
||||||
|
|
|
@ -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 }
|
||||||
|
|
Loading…
Reference in New Issue