Only expect proofs when the contract is started

This commit is contained in:
Mark Spanbroek 2021-11-04 09:49:07 +01:00
parent 7bb949a5cc
commit 376962322d
3 changed files with 33 additions and 10 deletions

View File

@ -9,6 +9,8 @@ contract Contracts {
mapping(bytes32=>uint) private durations; // contract duration in blocks
mapping(bytes32=>uint) private sizes; // storage size in bytes
mapping(bytes32=>bytes32) private contentHashes; // hash of data to be stored
mapping(bytes32=>uint) private proofPeriods; // period between proofs
mapping(bytes32=>uint) private proofTimeouts; // timeout for proof submission
mapping(bytes32=>uint) private prices; // price in coins
mapping(bytes32=>address) private hosts; // host that provides storage
@ -24,6 +26,14 @@ contract Contracts {
return contentHashes[id];
}
function _proofPeriod(bytes32 id) internal view returns (uint) {
return proofPeriods[id];
}
function _proofTimeout(bytes32 id) internal view returns (uint) {
return proofTimeouts[id];
}
function _price(bytes32 id) internal view returns (uint) {
return prices[id];
}
@ -66,6 +76,8 @@ contract Contracts {
durations[id] = duration;
sizes[id] = size;
contentHashes[id] = contentHash;
proofPeriods[id] = proofPeriod;
proofTimeouts[id] = proofTimeout;
prices[id] = price;
hosts[id] = host;
}

View File

@ -30,7 +30,7 @@ contract Storage is Contracts, Proofs, Stakes {
{
require(_stake(_host) >= stakeAmount, "Insufficient stake");
_lockStake(_host);
bytes32 id = _newContract(
_newContract(
_duration,
_size,
_contentHash,
@ -43,7 +43,10 @@ contract Storage is Contracts, Proofs, Stakes {
requestSignature,
bidSignature
);
_expectProofs(id, _proofPeriod, _proofTimeout, _duration);
}
function startContract(bytes32 id) public {
_expectProofs(id, proofPeriod(id), proofTimeout(id), duration(id));
}
function duration(bytes32 contractId) public view returns (uint) {
@ -67,11 +70,15 @@ contract Storage is Contracts, Proofs, Stakes {
}
function proofPeriod(bytes32 contractId) public view returns (uint) {
return _period(contractId);
return _proofPeriod(contractId);
}
function proofTimeout(bytes32 contractId) public view returns (uint) {
return _timeout(contractId);
return _proofTimeout(contractId);
}
function proofEnd(bytes32 contractId) public view returns (uint) {
return _end(contractId);
}
function missingProofs(bytes32 contractId) public view returns (uint) {

View File

@ -50,20 +50,24 @@ describe("Storage", function () {
expect(await storage.duration(id)).to.equal(request.duration)
expect(await storage.size(id)).to.equal(request.size)
expect(await storage.contentHash(id)).to.equal(request.contentHash)
expect(await storage.price(id)).to.equal(bid.price)
expect(await storage.host(id)).to.equal(await host.getAddress())
})
it("requires storage proofs", async function (){
expect(await storage.proofPeriod(id)).to.equal(request.proofPeriod)
expect(await storage.proofTimeout(id)).to.equal(request.proofTimeout)
})
expect(await storage.price(id)).to.equal(bid.price)
expect(await storage.host(id)).to.equal(await host.getAddress())
})
it("locks up host stake", async function () {
await expect(
storage.connect(host).withdrawStake()
).to.be.revertedWith("Stake locked")
})
describe("starting the contract", function () {
it("starts requiring storage proofs", async function (){
await storage.connect(host).startContract(id)
expect(await storage.proofEnd(id)).to.be.gt(0)
})
})
})
it("doesn't create contract with insufficient stake", async function () {