From aa673ff71c38c946ef121262008697fa70ac7839 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 4 Nov 2021 10:19:23 +0100 Subject: [PATCH] Unlock host stake at end of contract --- contracts/Storage.sol | 5 +++++ test/Storage.test.js | 26 +++++++++++++++++++++++--- test/examples.js | 2 +- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/contracts/Storage.sol b/contracts/Storage.sol index 9e692c3..9b8cafe 100644 --- a/contracts/Storage.sol +++ b/contracts/Storage.sol @@ -54,6 +54,11 @@ contract Storage is Contracts, Proofs, Stakes { _expectProofs(id, proofPeriod(id), proofTimeout(id), duration(id)); } + function finishContract(bytes32 id) public { + require(block.number > proofEnd(id), "Contract has not ended yet"); + _unlockStake(host(id)); + } + function duration(bytes32 contractId) public view returns (uint) { return _duration(contractId); } diff --git a/test/Storage.test.js b/test/Storage.test.js index 6627e64..5d9bace 100644 --- a/test/Storage.test.js +++ b/test/Storage.test.js @@ -2,6 +2,7 @@ const { expect } = require("chai") const { ethers } = require("hardhat") const { hashRequest, hashBid, sign } = require("./marketplace") const { exampleRequest, exampleBid } = require("./examples") +const { mineBlock, minedBlockNumber } = require ("./mining") describe("Storage", function () { @@ -74,6 +75,28 @@ describe("Storage", function () { ).to.be.revertedWith("Only host can call this function") }) }) + + describe("finishing the contract", function () { + + beforeEach(async function () { + await storage.connect(host).startContract(id) + }) + + it("unlocks the host stake", async function () { + const end = await storage.proofEnd(id) + while (await minedBlockNumber() < end) { + await mineBlock() + } + await storage.finishContract(id) + await expect(storage.connect(host).withdrawStake()).not.to.be.reverted + }) + + it("is only allowed when end time has passed", async function () { + await expect( + storage.finishContract(id) + ).to.be.revertedWith("Contract has not ended yet") + }) + }) }) it("doesn't create contract with insufficient stake", async function () { @@ -97,11 +120,8 @@ describe("Storage", function () { }) }) -// TODO: unlock stake at end of contract // TODO: payment when new contract // TODO: contract start and timeout // TODO: failure to start contract burns host and client // TODO: implement checking of actual proofs of storage, instead of dummy bool -// TODO: only allow proofs after start of contract -// TODO: proofs no longer required after contract duration // TODO: payout diff --git a/test/examples.js b/test/examples.js index 4452164..ba99ced 100644 --- a/test/examples.js +++ b/test/examples.js @@ -1,7 +1,7 @@ const { ethers } = require("hardhat") const exampleRequest = () => ({ - duration: 200000, // 200,000 blocks ≈ 1 month + duration: 150, // 150 blocks ≈ half an hour size: 1 * 1024 * 1024 * 1024, // 1 Gigabyte contentHash: ethers.utils.sha256("0xdeadbeef"), proofPeriod: 8, // 8 blocks ≈ 2 minutes