mirror of
https://github.com/status-im/dagger-contracts.git
synced 2025-02-10 05:36:47 +00:00
[proofs] Test that pointer increases by one every block
This commit is contained in:
parent
81c7dcd25a
commit
111dce58e8
@ -3,6 +3,7 @@ const { ethers } = require("hardhat")
|
|||||||
const {
|
const {
|
||||||
snapshot,
|
snapshot,
|
||||||
revert,
|
revert,
|
||||||
|
mine,
|
||||||
ensureMinimumBlockHeight,
|
ensureMinimumBlockHeight,
|
||||||
currentTime,
|
currentTime,
|
||||||
advanceTime,
|
advanceTime,
|
||||||
@ -12,11 +13,12 @@ const { periodic } = require("./time")
|
|||||||
|
|
||||||
describe("Proofs", function () {
|
describe("Proofs", function () {
|
||||||
const id = ethers.utils.randomBytes(32)
|
const id = ethers.utils.randomBytes(32)
|
||||||
const period = 10
|
const period = 30 * 60
|
||||||
const timeout = 5
|
const timeout = 5
|
||||||
const downtime = 64
|
const downtime = 64
|
||||||
const duration = 1000
|
const duration = 1000 * period
|
||||||
const probability = 4 // require a proof roughly once every 4 periods
|
const probability = 4 // require a proof roughly once every 4 periods
|
||||||
|
const { periodOf, periodEnd } = periodic(period)
|
||||||
|
|
||||||
let proofs
|
let proofs
|
||||||
|
|
||||||
@ -45,7 +47,6 @@ describe("Proofs", function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("requires proofs with an agreed upon probability", async function () {
|
it("requires proofs with an agreed upon probability", async function () {
|
||||||
const duration = 100_000
|
|
||||||
await proofs.expectProofs(id, probability, duration)
|
await proofs.expectProofs(id, probability, duration)
|
||||||
let amount = 0
|
let amount = 0
|
||||||
for (let i = 0; i < 100; i++) {
|
for (let i = 0; i < 100; i++) {
|
||||||
@ -64,7 +65,7 @@ describe("Proofs", function () {
|
|||||||
await proofs.expectProofs(id, probability, duration)
|
await proofs.expectProofs(id, probability, duration)
|
||||||
while (Math.floor((await currentTime()) / period) == startPeriod) {
|
while (Math.floor((await currentTime()) / period) == startPeriod) {
|
||||||
expect(await proofs.isProofRequired(id)).to.be.false
|
expect(await proofs.isProofRequired(id)).to.be.false
|
||||||
await advanceTime(1)
|
await advanceTime(Math.floor(period / 10))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -98,9 +99,17 @@ describe("Proofs", function () {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("when proofs are required", async function () {
|
it("moves pointer one block at a time", async function () {
|
||||||
const { periodOf, periodEnd } = periodic(period)
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("when proofs are required", async function () {
|
||||||
beforeEach(async function () {
|
beforeEach(async function () {
|
||||||
await proofs.expectProofs(id, probability, duration)
|
await proofs.expectProofs(id, probability, duration)
|
||||||
})
|
})
|
||||||
|
11
test/evm.js
11
test/evm.js
@ -14,9 +14,13 @@ async function revert() {
|
|||||||
await ethers.provider.send("evm_setNextBlockTimestamp", [time + 1])
|
await ethers.provider.send("evm_setNextBlockTimestamp", [time + 1])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function mine() {
|
||||||
|
await ethers.provider.send("evm_mine")
|
||||||
|
}
|
||||||
|
|
||||||
async function ensureMinimumBlockHeight(height) {
|
async function ensureMinimumBlockHeight(height) {
|
||||||
while ((await ethers.provider.getBlockNumber()) < height) {
|
while ((await ethers.provider.getBlockNumber()) < height) {
|
||||||
await ethers.provider.send("evm_mine")
|
await mine()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,19 +31,20 @@ async function currentTime() {
|
|||||||
|
|
||||||
async function advanceTime(seconds) {
|
async function advanceTime(seconds) {
|
||||||
await ethers.provider.send("evm_increaseTime", [seconds])
|
await ethers.provider.send("evm_increaseTime", [seconds])
|
||||||
await ethers.provider.send("evm_mine")
|
await mine()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function advanceTimeTo(timestamp) {
|
async function advanceTimeTo(timestamp) {
|
||||||
if ((await currentTime()) !== timestamp) {
|
if ((await currentTime()) !== timestamp) {
|
||||||
await ethers.provider.send("evm_setNextBlockTimestamp", [timestamp])
|
await ethers.provider.send("evm_setNextBlockTimestamp", [timestamp])
|
||||||
await ethers.provider.send("evm_mine")
|
await mine()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
snapshot,
|
snapshot,
|
||||||
revert,
|
revert,
|
||||||
|
mine,
|
||||||
ensureMinimumBlockHeight,
|
ensureMinimumBlockHeight,
|
||||||
currentTime,
|
currentTime,
|
||||||
advanceTime,
|
advanceTime,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user