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")
|
2023-01-09 13:21:23 +00:00
|
|
|
const { periodic } = require("./time")
|
2021-11-01 11:30:35 +00:00
|
|
|
|
|
|
|
describe("Proofs", function () {
|
2023-01-09 13:20:59 +00:00
|
|
|
const slotId = hexlify(randomBytes(32))
|
|
|
|
const requestId = 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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.setProofEnd(requestId, (await currentTime()) + duration)
|
2022-09-29 10:07:55 +00:00
|
|
|
})
|
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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.expectProofs(slotId, requestId, probability)
|
2022-09-29 10:07:55 +00:00
|
|
|
await expect(
|
2023-01-09 13:20:59 +00:00
|
|
|
proofs.expectProofs(slotId, requestId, probability)
|
|
|
|
).to.be.revertedWith("Slot id already in use")
|
2022-09-29 10:07:55 +00:00
|
|
|
})
|
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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.expectProofs(slotId, requestId, probability)
|
2022-09-29 10:07:55 +00:00
|
|
|
let amount = 0
|
|
|
|
for (let i = 0; i < 100; i++) {
|
2023-01-09 13:20:59 +00:00
|
|
|
if (await proofs.isProofRequired(slotId)) {
|
2022-09-29 10:07:55 +00:00
|
|
|
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
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.expectProofs(slotId, requestId, probability)
|
2022-09-29 10:07:55 +00:00
|
|
|
while (Math.floor((await currentTime()) / period) == startPeriod) {
|
2023-01-09 13:20:59 +00:00
|
|
|
expect(await proofs.isProofRequired(slotId)).to.be.false
|
2022-09-29 10:07:55 +00:00
|
|
|
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
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.expectProofs(slotId, requestId, probability)
|
2022-09-29 10:07:55 +00:00
|
|
|
await advanceTime(duration)
|
2023-01-09 13:20:59 +00:00
|
|
|
expect(await proofs.isProofRequired(slotId)).to.be.false
|
2022-09-29 10:07:55 +00:00
|
|
|
})
|
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
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.expectProofs(slotId, requestId, probability)
|
2022-09-29 10:07:55 +00:00
|
|
|
await advanceTime(duration + timeout)
|
2023-01-09 13:20:59 +00:00
|
|
|
expect(await proofs.isProofRequired(slotId)).to.be.false
|
2022-09-29 10:07:55 +00:00
|
|
|
})
|
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))
|
2023-01-09 13:20:59 +00:00
|
|
|
for (let slotId of [id1, id2, id3]) {
|
|
|
|
await proofs.expectProofs(slotId, requestId, probability)
|
2022-09-29 10:07:55 +00:00
|
|
|
}
|
|
|
|
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++) {
|
2023-01-09 13:20:59 +00:00
|
|
|
let previous = await proofs.getPointer(slotId)
|
2022-09-29 10:07:55 +00:00
|
|
|
await mine()
|
2023-01-09 13:20:59 +00:00
|
|
|
let current = await proofs.getPointer(slotId)
|
2022-09-29 10:07:55 +00:00
|
|
|
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() {
|
2023-01-09 13:20:59 +00:00
|
|
|
while (!(await proofs.willProofBeRequired(slotId))) {
|
2022-04-05 09:27:02 +00:00
|
|
|
await mine()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.setProofEnd(requestId, (await currentTime()) + duration)
|
|
|
|
await proofs.expectProofs(slotId, requestId, 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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
expect(await proofs.getPointer(slotId)).to.be.lt(downtime)
|
|
|
|
while ((await proofs.getPointer(slotId)) < downtime) {
|
|
|
|
expect(await proofs.willProofBeRequired(slotId)).to.be.true
|
2022-04-05 09:27:02 +00:00
|
|
|
await mine()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it("means that a proof is required after downtime", async function () {
|
2023-01-09 13:20:59 +00:00
|
|
|
while ((await proofs.getPointer(slotId)) < downtime) {
|
2022-04-05 09:27:02 +00:00
|
|
|
await mine()
|
|
|
|
}
|
2023-01-09 13:20:59 +00:00
|
|
|
expect(await proofs.willProofBeRequired(slotId)).to.be.false
|
|
|
|
expect(await proofs.isProofRequired(slotId)).to.be.true
|
2022-04-05 09:27:02 +00:00
|
|
|
})
|
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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
expect(await proofs.getPointer(slotId)).to.be.lt(downtime)
|
|
|
|
expect(await proofs.willProofBeRequired(slotId)).to.be.true
|
|
|
|
await proofs.unexpectProofs(slotId)
|
|
|
|
expect(await proofs.willProofBeRequired(slotId)).to.be.false
|
2022-09-13 07:18:55 +00:00
|
|
|
})
|
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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.setProofEnd(requestId, (await currentTime()) + duration)
|
|
|
|
await proofs.expectProofs(slotId, requestId, probability)
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|
|
|
|
|
2023-01-09 13:20:59 +00:00
|
|
|
async function waitUntilProofIsRequired(slotId) {
|
2022-03-09 10:29:53 +00:00
|
|
|
await advanceTimeTo(periodEnd(periodOf(await currentTime())))
|
2022-03-10 12:35:41 +00:00
|
|
|
while (
|
|
|
|
!(
|
2023-01-09 13:20:59 +00:00
|
|
|
(await proofs.isProofRequired(slotId)) &&
|
|
|
|
(await proofs.getPointer(slotId)) < 250
|
2022-03-10 12:35:41 +00:00
|
|
|
)
|
|
|
|
) {
|
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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await waitUntilProofIsRequired(slotId)
|
|
|
|
const challenge1 = await proofs.getChallenge(slotId)
|
|
|
|
await waitUntilProofIsRequired(slotId)
|
|
|
|
const challenge2 = await proofs.getChallenge(slotId)
|
2022-03-10 12:04:46 +00:00
|
|
|
expect(challenge2).not.to.equal(challenge1)
|
|
|
|
})
|
|
|
|
|
2023-01-09 13:20:59 +00:00
|
|
|
it("provides different challenges per slotId", async function () {
|
2022-04-12 06:43:47 +00:00
|
|
|
const id2 = hexlify(randomBytes(32))
|
|
|
|
const id3 = hexlify(randomBytes(32))
|
2023-01-09 13:20:59 +00:00
|
|
|
const challenge1 = await proofs.getChallenge(slotId)
|
2022-03-10 12:04:46 +00:00
|
|
|
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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.submitProof(slotId, proof)
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("fails proof submission when proof is incorrect", async function () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await expect(proofs.submitProof(slotId, [])).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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await expect(proofs.submitProof(slotId, proof))
|
2022-04-12 06:43:47 +00:00
|
|
|
.to.emit(proofs, "ProofSubmitted")
|
2023-01-09 13:20:59 +00:00
|
|
|
.withArgs(slotId, proof)
|
2022-04-12 06:43:47 +00:00
|
|
|
})
|
|
|
|
|
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())))
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.submitProof(slotId, proof)
|
|
|
|
await expect(proofs.submitProof(slotId, 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 () {
|
2023-01-09 13:41:28 +00:00
|
|
|
expect(await proofs.missingProofs(slotId)).to.equal(0)
|
2023-01-09 13:20:59 +00:00
|
|
|
await waitUntilProofIsRequired(slotId)
|
2022-03-08 14:58:08 +00:00
|
|
|
let missedPeriod = periodOf(await currentTime())
|
|
|
|
await advanceTimeTo(periodEnd(missedPeriod))
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.markProofAsMissing(slotId, missedPeriod)
|
2023-01-09 13:41:28 +00:00
|
|
|
expect(await proofs.missingProofs(slotId)).to.equal(1)
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|
|
|
|
|
2022-03-08 14:58:08 +00:00
|
|
|
it("does not mark a proof as missing before period end", async function () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await waitUntilProofIsRequired(slotId)
|
2022-03-08 14:58:08 +00:00
|
|
|
let currentPeriod = periodOf(await currentTime())
|
|
|
|
await expect(
|
2023-01-09 13:20:59 +00:00
|
|
|
proofs.markProofAsMissing(slotId, currentPeriod)
|
2022-03-08 14:58:08 +00:00
|
|
|
).to.be.revertedWith("Period has not ended yet")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not mark a proof as missing after timeout", async function () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await waitUntilProofIsRequired(slotId)
|
2022-03-08 14:58:08 +00:00
|
|
|
let currentPeriod = periodOf(await currentTime())
|
|
|
|
await advanceTimeTo(periodEnd(currentPeriod) + timeout)
|
2021-11-01 11:30:35 +00:00
|
|
|
await expect(
|
2023-01-09 13:20:59 +00:00
|
|
|
proofs.markProofAsMissing(slotId, currentPeriod)
|
2022-03-08 14:58:08 +00:00
|
|
|
).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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await waitUntilProofIsRequired(slotId)
|
2022-03-08 14:58:08 +00:00
|
|
|
let submittedPeriod = periodOf(await currentTime())
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.submitProof(slotId, proof)
|
2022-03-08 14:58:08 +00:00
|
|
|
await advanceTimeTo(periodEnd(submittedPeriod))
|
2021-11-01 11:30:35 +00:00
|
|
|
await expect(
|
2023-01-09 13:20:59 +00:00
|
|
|
proofs.markProofAsMissing(slotId, 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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
while (await proofs.isProofRequired(slotId)) {
|
2022-03-08 14:58:08 +00:00
|
|
|
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(
|
2023-01-09 13:20:59 +00:00
|
|
|
proofs.markProofAsMissing(slotId, 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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await waitUntilProofIsRequired(slotId)
|
2022-03-08 14:58:08 +00:00
|
|
|
let missedPeriod = periodOf(await currentTime())
|
|
|
|
await advanceTimeTo(periodEnd(missedPeriod))
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.markProofAsMissing(slotId, missedPeriod)
|
2021-11-01 11:30:35 +00:00
|
|
|
await expect(
|
2023-01-09 13:20:59 +00:00
|
|
|
proofs.markProofAsMissing(slotId, 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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await waitUntilProofIsRequired(slotId)
|
|
|
|
await proofs.unexpectProofs(slotId)
|
|
|
|
await expect(await proofs.isProofRequired(slotId)).to.be.false
|
2022-09-13 07:18:55 +00:00
|
|
|
})
|
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 () {
|
2023-01-09 13:20:59 +00:00
|
|
|
await expect(proofs.end(requestId)).to.be.revertedWith(
|
2022-09-29 10:07:55 +00:00
|
|
|
"Proof ending doesn't exist"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("sets proof end when proof ending doesn't already exist", async function () {
|
|
|
|
let ending = (await currentTime()) + duration
|
2023-01-09 13:20:59 +00:00
|
|
|
await expect(proofs.setProofEnd(requestId, ending)).not.to.be.reverted
|
|
|
|
await expect(await proofs.end(requestId)).to.equal(ending)
|
2022-09-29 10:07:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("sets proof end when proof ending exists and ending set to past", async function () {
|
|
|
|
// let ending = (await currentTime()) + duration
|
2023-01-09 13:20:59 +00:00
|
|
|
// await proofs.setProofEnd(requestId, ending)
|
2022-09-29 10:07:55 +00:00
|
|
|
let past = (await currentTime()) - 1
|
2023-01-09 13:20:59 +00:00
|
|
|
await expect(proofs.setProofEnd(requestId, past)).not.to.be.reverted
|
2022-09-29 10:07:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("fails when proof ending already exists and ending set to future", async function () {
|
|
|
|
let ending = (await currentTime()) + duration
|
2023-01-09 13:20:59 +00:00
|
|
|
await proofs.setProofEnd(requestId, ending)
|
|
|
|
await expect(proofs.setProofEnd(requestId, ending)).to.be.revertedWith(
|
2022-09-29 10:07:55 +00:00
|
|
|
"End exists or must be past"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2021-11-01 11:30:35 +00:00
|
|
|
})
|