2021-11-01 11:30:35 +00:00
|
|
|
const { expect } = require("chai")
|
|
|
|
const { ethers } = require("hardhat")
|
2022-04-12 06:43:47 +00:00
|
|
|
const { hexlify, randomBytes } = ethers.utils
|
2022-03-08 14:58:08 +00:00
|
|
|
const {
|
|
|
|
snapshot,
|
|
|
|
revert,
|
2022-04-05 08:11:30 +00:00
|
|
|
mine,
|
2022-03-09 10:21:19 +00:00
|
|
|
ensureMinimumBlockHeight,
|
2022-03-08 14:58:08 +00:00
|
|
|
currentTime,
|
|
|
|
advanceTime,
|
|
|
|
advanceTimeTo,
|
|
|
|
} = require("./evm")
|
2022-09-22 02:21:49 +00:00
|
|
|
const { periodic, hours, minutes } = require("./time")
|
2021-11-01 11:30:35 +00:00
|
|
|
|
|
|
|
describe("Proofs", function () {
|
2022-04-12 06:43:47 +00:00
|
|
|
const id = hexlify(randomBytes(32))
|
2022-09-29 10:07:55 +00:00
|
|
|
const endId = hexlify(randomBytes(32))
|
2022-04-05 08:11:30 +00:00
|
|
|
const period = 30 * 60
|
2021-11-01 11:30:35 +00:00
|
|
|
const timeout = 5
|
2022-03-10 09:19:21 +00:00
|
|
|
const downtime = 64
|
2022-04-05 08:11:30 +00:00
|
|
|
const duration = 1000 * period
|
2022-03-10 09:12:03 +00:00
|
|
|
const probability = 4 // require a proof roughly once every 4 periods
|
2022-04-05 08:11:30 +00:00
|
|
|
const { periodOf, periodEnd } = periodic(period)
|
2021-11-01 11:30:35 +00:00
|
|
|
|
|
|
|
let proofs
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
2022-03-08 14:58:08 +00:00
|
|
|
await snapshot()
|
2022-03-09 10:21:19 +00:00
|
|
|
await ensureMinimumBlockHeight(256)
|
2021-11-01 11:30:35 +00:00
|
|
|
const Proofs = await ethers.getContractFactory("TestProofs")
|
2022-03-10 09:19:21 +00:00
|
|
|
proofs = await Proofs.deploy(period, timeout, downtime)
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|
|
|
|
|
2022-03-08 14:58:08 +00:00
|
|
|
afterEach(async function () {
|
|
|
|
await revert()
|
|
|
|
})
|
|
|
|
|
2022-09-29 10:07:55 +00:00
|
|
|
describe("general", function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
await proofs.setProofEnd(endId, (await currentTime()) + duration)
|
|
|
|
})
|
2021-11-03 12:24:50 +00:00
|
|
|
|
2022-09-29 10:07:55 +00:00
|
|
|
it("does not allow ids to be reused", async function () {
|
|
|
|
await proofs.expectProofs(id, endId, probability)
|
|
|
|
await expect(
|
|
|
|
proofs.expectProofs(id, endId, probability)
|
|
|
|
).to.be.revertedWith("Proof id already in use")
|
|
|
|
})
|
2021-11-01 14:28:22 +00:00
|
|
|
|
2022-09-29 10:07:55 +00:00
|
|
|
it("requires proofs with an agreed upon probability", async function () {
|
|
|
|
await proofs.expectProofs(id, endId, probability)
|
|
|
|
let amount = 0
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
if (await proofs.isProofRequired(id)) {
|
|
|
|
amount += 1
|
|
|
|
}
|
|
|
|
await advanceTime(period)
|
2021-11-03 16:02:12 +00:00
|
|
|
}
|
2022-09-29 10:07:55 +00:00
|
|
|
let expected = 100 / probability
|
|
|
|
expect(amount).to.be.closeTo(expected, expected / 2)
|
|
|
|
})
|
2021-11-03 16:02:12 +00:00
|
|
|
|
2022-09-29 10:07:55 +00:00
|
|
|
it("requires no proofs in the start period", async function () {
|
|
|
|
const startPeriod = Math.floor((await currentTime()) / period)
|
|
|
|
const probability = 1
|
|
|
|
await proofs.expectProofs(id, endId, probability)
|
|
|
|
while (Math.floor((await currentTime()) / period) == startPeriod) {
|
|
|
|
expect(await proofs.isProofRequired(id)).to.be.false
|
|
|
|
await advanceTime(Math.floor(period / 10))
|
|
|
|
}
|
|
|
|
})
|
2022-03-08 14:58:08 +00:00
|
|
|
|
2022-09-29 10:07:55 +00:00
|
|
|
it("requires no proofs in the end period", async function () {
|
|
|
|
const probability = 1
|
|
|
|
await proofs.expectProofs(id, endId, probability)
|
|
|
|
await advanceTime(duration)
|
|
|
|
expect(await proofs.isProofRequired(id)).to.be.false
|
|
|
|
})
|
2022-03-08 14:58:08 +00:00
|
|
|
|
2022-09-29 10:07:55 +00:00
|
|
|
it("requires no proofs after the end time", async function () {
|
|
|
|
const probability = 1
|
|
|
|
await proofs.expectProofs(id, endId, probability)
|
|
|
|
await advanceTime(duration + timeout)
|
|
|
|
expect(await proofs.isProofRequired(id)).to.be.false
|
|
|
|
})
|
2022-03-08 14:58:08 +00:00
|
|
|
|
2022-09-29 10:07:55 +00:00
|
|
|
it("requires proofs for different ids at different times", async function () {
|
|
|
|
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, endId, probability)
|
|
|
|
}
|
|
|
|
let req1, req2, req3
|
|
|
|
while (req1 === req2 && req2 === req3) {
|
|
|
|
req1 = await proofs.isProofRequired(id1)
|
|
|
|
req2 = await proofs.isProofRequired(id2)
|
|
|
|
req3 = await proofs.isProofRequired(id3)
|
|
|
|
await advanceTime(period)
|
|
|
|
}
|
|
|
|
})
|
2021-11-03 16:15:03 +00:00
|
|
|
|
2022-09-29 10:07:55 +00:00
|
|
|
it("moves pointer one block at a time", async function () {
|
|
|
|
await advanceTimeTo(periodEnd(periodOf(await currentTime())))
|
|
|
|
for (let i = 0; i < 256; i++) {
|
|
|
|
let previous = await proofs.getPointer(id)
|
|
|
|
await mine()
|
|
|
|
let current = await proofs.getPointer(id)
|
|
|
|
expect(current).to.equal((previous + 1) % 256)
|
|
|
|
}
|
|
|
|
})
|
2022-04-05 08:11:30 +00:00
|
|
|
})
|
2022-04-05 09:27:02 +00:00
|
|
|
|
|
|
|
describe("when proof requirement is upcoming", function () {
|
|
|
|
async function waitUntilProofWillBeRequired() {
|
|
|
|
while (!(await proofs.willProofBeRequired(id))) {
|
|
|
|
await mine()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
2022-09-29 10:07:55 +00:00
|
|
|
await proofs.setProofEnd(endId, (await currentTime()) + duration)
|
|
|
|
await proofs.expectProofs(id, endId, probability)
|
2022-04-05 09:27:02 +00:00
|
|
|
await advanceTimeTo(periodEnd(periodOf(await currentTime())))
|
|
|
|
await waitUntilProofWillBeRequired()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("means the pointer is in downtime", async function () {
|
|
|
|
expect(await proofs.getPointer(id)).to.be.lt(downtime)
|
|
|
|
while ((await proofs.getPointer(id)) < downtime) {
|
|
|
|
expect(await proofs.willProofBeRequired(id)).to.be.true
|
|
|
|
await mine()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it("means that a proof is required after downtime", async function () {
|
|
|
|
while ((await proofs.getPointer(id)) < downtime) {
|
|
|
|
await mine()
|
|
|
|
}
|
|
|
|
expect(await proofs.willProofBeRequired(id)).to.be.false
|
|
|
|
expect(await proofs.isProofRequired(id)).to.be.true
|
|
|
|
})
|
2022-09-13 07:18:55 +00:00
|
|
|
|
2022-09-13 07:32:02 +00:00
|
|
|
it("will not require proofs when no longer expected", async function () {
|
2022-09-21 09:29:40 +00:00
|
|
|
expect(await proofs.getPointer(id)).to.be.lt(downtime)
|
|
|
|
expect(await proofs.willProofBeRequired(id)).to.be.true
|
2022-09-13 07:18:55 +00:00
|
|
|
await proofs.unexpectProofs(id)
|
|
|
|
expect(await proofs.willProofBeRequired(id)).to.be.false
|
|
|
|
})
|
2022-04-05 09:27:02 +00:00
|
|
|
})
|
2022-03-09 10:11:01 +00:00
|
|
|
|
2022-04-11 12:09:48 +00:00
|
|
|
describe("when proofs are required", function () {
|
2022-04-12 06:43:47 +00:00
|
|
|
const proof = hexlify(randomBytes(42))
|
|
|
|
|
2021-11-01 11:30:35 +00:00
|
|
|
beforeEach(async function () {
|
2022-09-29 10:07:55 +00:00
|
|
|
await proofs.setProofEnd(endId, (await currentTime()) + duration)
|
|
|
|
await proofs.expectProofs(id, endId, probability)
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|
|
|
|
|
2022-03-08 14:58:08 +00:00
|
|
|
async function waitUntilProofIsRequired(id) {
|
2022-03-09 10:29:53 +00:00
|
|
|
await advanceTimeTo(periodEnd(periodOf(await currentTime())))
|
2022-03-10 12:35:41 +00:00
|
|
|
while (
|
|
|
|
!(
|
|
|
|
(await proofs.isProofRequired(id)) &&
|
|
|
|
(await proofs.getPointer(id)) < 250
|
|
|
|
)
|
|
|
|
) {
|
2022-03-08 14:58:08 +00:00
|
|
|
await advanceTime(period)
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 12:04:46 +00:00
|
|
|
it("provides different challenges per period", async function () {
|
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
const challenge1 = await proofs.getChallenge(id)
|
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
const challenge2 = await proofs.getChallenge(id)
|
|
|
|
expect(challenge2).not.to.equal(challenge1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("provides different challenges per id", async function () {
|
2022-04-12 06:43:47 +00:00
|
|
|
const id2 = hexlify(randomBytes(32))
|
|
|
|
const id3 = hexlify(randomBytes(32))
|
2022-03-10 12:04:46 +00:00
|
|
|
const challenge1 = await proofs.getChallenge(id)
|
|
|
|
const challenge2 = await proofs.getChallenge(id2)
|
|
|
|
const challenge3 = await proofs.getChallenge(id3)
|
|
|
|
expect(challenge1 === challenge2 && challenge2 === challenge3).to.be.false
|
|
|
|
})
|
|
|
|
|
2021-11-01 11:30:35 +00:00
|
|
|
it("submits a correct proof", async function () {
|
2022-04-12 06:43:47 +00:00
|
|
|
await proofs.submitProof(id, proof)
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("fails proof submission when proof is incorrect", async function () {
|
2022-04-12 06:43:47 +00:00
|
|
|
await expect(proofs.submitProof(id, [])).to.be.revertedWith(
|
2022-03-08 14:58:08 +00:00
|
|
|
"Invalid proof"
|
|
|
|
)
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|
|
|
|
|
2022-04-12 06:43:47 +00:00
|
|
|
it("emits an event when proof was submitted", async function () {
|
|
|
|
await expect(proofs.submitProof(id, proof))
|
|
|
|
.to.emit(proofs, "ProofSubmitted")
|
|
|
|
.withArgs(id, proof)
|
|
|
|
})
|
|
|
|
|
2022-02-09 13:17:23 +00:00
|
|
|
it("fails proof submission when already submitted", async function () {
|
2022-03-08 14:58:08 +00:00
|
|
|
await advanceTimeTo(periodEnd(periodOf(await currentTime())))
|
2022-04-12 06:43:47 +00:00
|
|
|
await proofs.submitProof(id, proof)
|
|
|
|
await expect(proofs.submitProof(id, proof)).to.be.revertedWith(
|
2022-03-08 14:58:08 +00:00
|
|
|
"Proof already submitted"
|
|
|
|
)
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("marks a proof as missing", async function () {
|
|
|
|
expect(await proofs.missed(id)).to.equal(0)
|
2022-03-08 14:58:08 +00:00
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
let missedPeriod = periodOf(await currentTime())
|
|
|
|
await advanceTimeTo(periodEnd(missedPeriod))
|
|
|
|
await proofs.markProofAsMissing(id, missedPeriod)
|
2021-11-01 11:30:35 +00:00
|
|
|
expect(await proofs.missed(id)).to.equal(1)
|
|
|
|
})
|
|
|
|
|
2022-03-08 14:58:08 +00:00
|
|
|
it("does not mark a proof as missing before period end", async function () {
|
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
let currentPeriod = periodOf(await currentTime())
|
|
|
|
await expect(
|
|
|
|
proofs.markProofAsMissing(id, currentPeriod)
|
|
|
|
).to.be.revertedWith("Period has not ended yet")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not mark a proof as missing after timeout", async function () {
|
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
let currentPeriod = periodOf(await currentTime())
|
|
|
|
await advanceTimeTo(periodEnd(currentPeriod) + timeout)
|
2021-11-01 11:30:35 +00:00
|
|
|
await expect(
|
2022-03-08 14:58:08 +00:00
|
|
|
proofs.markProofAsMissing(id, currentPeriod)
|
|
|
|
).to.be.revertedWith("Validation timed out")
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("does not mark a submitted proof as missing", async function () {
|
2022-03-08 14:58:08 +00:00
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
let submittedPeriod = periodOf(await currentTime())
|
2022-04-12 06:43:47 +00:00
|
|
|
await proofs.submitProof(id, proof)
|
2022-03-08 14:58:08 +00:00
|
|
|
await advanceTimeTo(periodEnd(submittedPeriod))
|
2021-11-01 11:30:35 +00:00
|
|
|
await expect(
|
2022-03-08 14:58:08 +00:00
|
|
|
proofs.markProofAsMissing(id, submittedPeriod)
|
2021-11-01 11:30:35 +00:00
|
|
|
).to.be.revertedWith("Proof was submitted, not missing")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not mark proof as missing when not required", async function () {
|
2022-03-08 14:58:08 +00:00
|
|
|
while (await proofs.isProofRequired(id)) {
|
|
|
|
await advanceTime(period)
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
2022-03-08 14:58:08 +00:00
|
|
|
let currentPeriod = periodOf(await currentTime())
|
|
|
|
await advanceTimeTo(periodEnd(currentPeriod))
|
2021-11-01 11:30:35 +00:00
|
|
|
await expect(
|
2022-03-08 14:58:08 +00:00
|
|
|
proofs.markProofAsMissing(id, currentPeriod)
|
2021-11-01 11:30:35 +00:00
|
|
|
).to.be.revertedWith("Proof was not required")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not mark proof as missing twice", async function () {
|
2022-03-08 14:58:08 +00:00
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
let missedPeriod = periodOf(await currentTime())
|
|
|
|
await advanceTimeTo(periodEnd(missedPeriod))
|
|
|
|
await proofs.markProofAsMissing(id, missedPeriod)
|
2021-11-01 11:30:35 +00:00
|
|
|
await expect(
|
2022-03-08 14:58:08 +00:00
|
|
|
proofs.markProofAsMissing(id, missedPeriod)
|
2021-11-01 11:30:35 +00:00
|
|
|
).to.be.revertedWith("Proof already marked as missing")
|
|
|
|
})
|
2022-09-13 07:18:55 +00:00
|
|
|
|
|
|
|
it("requires no proofs when no longer expected", async function () {
|
|
|
|
await waitUntilProofIsRequired(id)
|
|
|
|
await proofs.unexpectProofs(id)
|
|
|
|
await expect(await proofs.isProofRequired(id)).to.be.false
|
|
|
|
})
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|
2022-09-29 10:07:55 +00:00
|
|
|
|
|
|
|
describe("set proof end", function () {
|
|
|
|
const proof = hexlify(randomBytes(42))
|
|
|
|
|
|
|
|
it("fails to get proof end when proof ending doesn't exist", async function () {
|
|
|
|
await expect(proofs.end(endId)).to.be.revertedWith(
|
|
|
|
"Proof ending doesn't exist"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("sets proof end when proof ending doesn't already exist", async function () {
|
|
|
|
let ending = (await currentTime()) + duration
|
|
|
|
await expect(proofs.setProofEnd(endId, ending)).not.to.be.reverted
|
|
|
|
await expect(await proofs.end(endId)).to.equal(ending)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("sets proof end when proof ending exists and ending set to past", async function () {
|
|
|
|
// let ending = (await currentTime()) + duration
|
|
|
|
// await proofs.setProofEnd(endId, ending)
|
|
|
|
let past = (await currentTime()) - 1
|
|
|
|
await expect(proofs.setProofEnd(endId, past)).not.to.be.reverted
|
|
|
|
})
|
|
|
|
|
|
|
|
it("fails when proof ending already exists and ending set to future", async function () {
|
|
|
|
let ending = (await currentTime()) + duration
|
|
|
|
await proofs.setProofEnd(endId, ending)
|
|
|
|
await expect(proofs.setProofEnd(endId, ending)).to.be.revertedWith(
|
|
|
|
"End exists or must be past"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|