mirror of
https://github.com/status-im/dagger-contracts.git
synced 2025-01-17 01:51:42 +00:00
Add end time to proofs based on contract duration
This commit is contained in:
parent
3326c4fe74
commit
2784800c3e
@ -6,6 +6,7 @@ contract Proofs {
|
||||
mapping(bytes32=>bool) private ids;
|
||||
mapping(bytes32=>uint) private periods;
|
||||
mapping(bytes32=>uint) private timeouts;
|
||||
mapping(bytes32=>uint) private ends;
|
||||
mapping(bytes32=>uint) private markers;
|
||||
mapping(bytes32=>uint) private missed;
|
||||
mapping(bytes32=>mapping(uint=>bool)) private received;
|
||||
@ -19,6 +20,10 @@ contract Proofs {
|
||||
return timeouts[id];
|
||||
}
|
||||
|
||||
function _end(bytes32 id) internal view returns (uint) {
|
||||
return ends[id];
|
||||
}
|
||||
|
||||
function _missed(bytes32 id) internal view returns (uint) {
|
||||
return missed[id];
|
||||
}
|
||||
@ -30,12 +35,18 @@ contract Proofs {
|
||||
require(timeout <= 128, "Invalid proof timeout, needs to be <= 128");
|
||||
}
|
||||
|
||||
function _expectProofs(bytes32 id, uint period, uint timeout) internal {
|
||||
function _expectProofs(
|
||||
bytes32 id,
|
||||
uint period,
|
||||
uint timeout,
|
||||
uint duration
|
||||
) internal {
|
||||
require(!ids[id], "Proof id already in use");
|
||||
_checkTimeout(timeout);
|
||||
ids[id] = true;
|
||||
periods[id] = period;
|
||||
timeouts[id] = timeout;
|
||||
ends[id] = block.number + duration + 2 * timeout;
|
||||
markers[id] = uint(blockhash(block.number - 1)) % period;
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ contract Storage is Contracts, Proofs, Stakes {
|
||||
requestSignature,
|
||||
bidSignature
|
||||
);
|
||||
_expectProofs(id, _proofPeriod, _proofTimeout);
|
||||
_expectProofs(id, _proofPeriod, _proofTimeout, _duration);
|
||||
}
|
||||
|
||||
function duration(bytes32 contractId) public view returns (uint) {
|
||||
|
@ -14,12 +14,21 @@ contract TestProofs is Proofs {
|
||||
return _timeout(id);
|
||||
}
|
||||
|
||||
function end(bytes32 id) public view returns (uint) {
|
||||
return _end(id);
|
||||
}
|
||||
|
||||
function missed(bytes32 id) public view returns (uint) {
|
||||
return _missed(id);
|
||||
}
|
||||
|
||||
function expectProofs(bytes32 id, uint _period, uint _timeout) public {
|
||||
_expectProofs(id, _period, _timeout);
|
||||
function expectProofs(
|
||||
bytes32 id,
|
||||
uint _period,
|
||||
uint _timeout,
|
||||
uint _duration
|
||||
) public {
|
||||
_expectProofs(id, _period, _timeout, _duration);
|
||||
}
|
||||
|
||||
function isProofRequired(
|
||||
|
@ -6,6 +6,7 @@ describe("Proofs", function () {
|
||||
const id = ethers.utils.randomBytes(32)
|
||||
const period = 10
|
||||
const timeout = 5
|
||||
const duration = 100
|
||||
|
||||
let proofs
|
||||
|
||||
@ -14,32 +15,6 @@ describe("Proofs", function () {
|
||||
proofs = await Proofs.deploy()
|
||||
})
|
||||
|
||||
it("indicates that proofs are required", async function() {
|
||||
await proofs.expectProofs(id, period, timeout)
|
||||
expect(await proofs.period(id)).to.equal(period)
|
||||
expect(await proofs.timeout(id)).to.equal(timeout)
|
||||
})
|
||||
|
||||
it("does not allow ids to be reused", async function() {
|
||||
await proofs.expectProofs(id, period, timeout)
|
||||
await expect(
|
||||
proofs.expectProofs(id, period, timeout)
|
||||
).to.be.revertedWith("Proof id already in use")
|
||||
})
|
||||
|
||||
it("does not allow a proof timeout that is too large", async function () {
|
||||
let invalidTimeout = 129 // max proof timeout is 128 blocks
|
||||
await expect(
|
||||
proofs.expectProofs(id, period, invalidTimeout)
|
||||
).to.be.revertedWith("Invalid proof timeout")
|
||||
})
|
||||
|
||||
describe("when proofs are required", async function () {
|
||||
|
||||
beforeEach(async function () {
|
||||
await proofs.expectProofs(id, period, timeout)
|
||||
})
|
||||
|
||||
async function mineBlock() {
|
||||
await ethers.provider.send("evm_mine")
|
||||
}
|
||||
@ -48,6 +23,39 @@ describe("Proofs", function () {
|
||||
return await ethers.provider.getBlockNumber()
|
||||
}
|
||||
|
||||
it("indicates that proofs are required", async function() {
|
||||
await proofs.expectProofs(id, period, timeout, duration)
|
||||
expect(await proofs.period(id)).to.equal(period)
|
||||
expect(await proofs.timeout(id)).to.equal(timeout)
|
||||
})
|
||||
|
||||
it("calculates an endtime based on duration and timeout", async function() {
|
||||
await proofs.expectProofs(id, period, timeout, duration)
|
||||
let start = await minedBlockNumber()
|
||||
let end = start + duration + 2 * timeout
|
||||
expect(await proofs.end(id)).to.equal(end)
|
||||
})
|
||||
|
||||
it("does not allow ids to be reused", async function() {
|
||||
await proofs.expectProofs(id, period, timeout, duration)
|
||||
await expect(
|
||||
proofs.expectProofs(id, period, timeout, duration)
|
||||
).to.be.revertedWith("Proof id already in use")
|
||||
})
|
||||
|
||||
it("does not allow a proof timeout that is too large", async function () {
|
||||
let invalidTimeout = 129 // max proof timeout is 128 blocks
|
||||
await expect(
|
||||
proofs.expectProofs(id, period, invalidTimeout, duration)
|
||||
).to.be.revertedWith("Invalid proof timeout")
|
||||
})
|
||||
|
||||
describe("when proofs are required", async function () {
|
||||
|
||||
beforeEach(async function () {
|
||||
await proofs.expectProofs(id, period, timeout, duration)
|
||||
})
|
||||
|
||||
async function mineUntilProofIsRequired(id) {
|
||||
while (!await proofs.isProofRequired(id, await minedBlockNumber())) {
|
||||
mineBlock()
|
||||
|
Loading…
x
Reference in New Issue
Block a user