Stop requiring proofs when contract has ended
This commit is contained in:
parent
2784800c3e
commit
8efa9fe162
|
@ -61,6 +61,9 @@ contract Proofs {
|
||||||
internal view
|
internal view
|
||||||
returns (bool)
|
returns (bool)
|
||||||
{
|
{
|
||||||
|
if (block.number >= ends[id]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
bytes32 hash = blockhash(blocknumber - 1);
|
bytes32 hash = blockhash(blocknumber - 1);
|
||||||
return hash != 0 && uint(hash) % periods[id] == markers[id];
|
return hash != 0 && uint(hash) % periods[id] == markers[id];
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ describe("Proofs", function () {
|
||||||
const id = ethers.utils.randomBytes(32)
|
const id = ethers.utils.randomBytes(32)
|
||||||
const period = 10
|
const period = 10
|
||||||
const timeout = 5
|
const timeout = 5
|
||||||
const duration = 100
|
const duration = 50
|
||||||
|
|
||||||
let proofs
|
let proofs
|
||||||
|
|
||||||
|
@ -50,6 +50,20 @@ describe("Proofs", function () {
|
||||||
).to.be.revertedWith("Invalid proof timeout")
|
).to.be.revertedWith("Invalid proof timeout")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("requires on average a proof every period", async function () {
|
||||||
|
let blocks = 500
|
||||||
|
let amount = 0
|
||||||
|
await proofs.expectProofs(id, period, timeout, blocks)
|
||||||
|
for (let i=0; i<blocks; i++) {
|
||||||
|
await mineBlock()
|
||||||
|
if (await proofs.isProofRequired(id, await minedBlockNumber())) {
|
||||||
|
amount += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let average = blocks / amount
|
||||||
|
expect(average).to.be.closeTo(period, period / 2)
|
||||||
|
})
|
||||||
|
|
||||||
describe("when proofs are required", async function () {
|
describe("when proofs are required", async function () {
|
||||||
|
|
||||||
beforeEach(async function () {
|
beforeEach(async function () {
|
||||||
|
@ -68,28 +82,31 @@ describe("Proofs", function () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
it("requires on average a proof every period", async function () {
|
async function mineUntilEnd() {
|
||||||
let blocks = 500
|
const end = await proofs.end(id)
|
||||||
let amount = 0
|
while (await minedBlockNumber() < end) {
|
||||||
for (i=0; i<blocks; i++) {
|
mineBlock()
|
||||||
await mineBlock()
|
|
||||||
if (await proofs.isProofRequired(id, await minedBlockNumber())) {
|
|
||||||
amount += 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let average = blocks / amount
|
|
||||||
expect(average).to.be.closeTo(period, period / 2)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("requires no proof for blocks that are unavailable", async function () {
|
it("requires no proof for blocks that are unavailable", async function () {
|
||||||
await mineUntilProofIsRequired(id)
|
await mineUntilProofIsRequired(id)
|
||||||
let blocknumber = await minedBlockNumber()
|
let blocknumber = await minedBlockNumber()
|
||||||
for (i=0; i<256; i++) { // only last 256 blocks are available in solidity
|
for (let i=0; i<256; i++) { // only last 256 blocks available in solidity
|
||||||
mineBlock()
|
mineBlock()
|
||||||
}
|
}
|
||||||
expect(await proofs.isProofRequired(id, blocknumber)).to.be.false
|
expect(await proofs.isProofRequired(id, blocknumber)).to.be.false
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("requires no proof after end time", async function () {
|
||||||
|
await mineUntilEnd()
|
||||||
|
for (let i=0; i<4*period; i++) {
|
||||||
|
const blocknumber = await minedBlockNumber()
|
||||||
|
expect(await proofs.isProofRequired(id, blocknumber)).to.be.false
|
||||||
|
mineBlock()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
it("submits a correct proof", async function () {
|
it("submits a correct proof", async function () {
|
||||||
await mineUntilProofIsRequired(id)
|
await mineUntilProofIsRequired(id)
|
||||||
let blocknumber = await minedBlockNumber()
|
let blocknumber = await minedBlockNumber()
|
||||||
|
|
Loading…
Reference in New Issue