From 2170d6bd1943485f04994588a3a5324cc7012ddd Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Wed, 21 Sep 2022 19:34:05 +1000 Subject: [PATCH] [marketplace] tests for Failed state --- contracts/AccountLocks.sol | 19 ------------------- contracts/Marketplace.sol | 17 ++++++++++++++--- contracts/TestCollateral.sol | 4 ---- test/Collateral.test.js | 5 ----- test/Marketplace.test.js | 30 +++++++++++++++++++----------- test/Storage.test.js | 14 ++++++-------- 6 files changed, 39 insertions(+), 50 deletions(-) diff --git a/contracts/AccountLocks.sol b/contracts/AccountLocks.sol index 5c9ed72..65fecab 100644 --- a/contracts/AccountLocks.sol +++ b/contracts/AccountLocks.sol @@ -64,25 +64,6 @@ contract AccountLocks { require(accountLocks.length == 0, "Account locked"); } - /// Removes an account lock - function _removeAccountLock(address account, bytes32 lockId) internal { - require(accounts[account].locks.length > 0, "Account lock doesn't exist"); - bytes32[] storage accountLocks = accounts[account].locks; - uint256 index = 0; - while (true) { - if (index >= accountLocks.length) { - return; - } - if (accountLocks[index] == lockId) { - accountLocks[index] = accountLocks[accountLocks.length - 1]; - accountLocks.pop(); - } else { - index++; - } - } - - } - function removeInactiveLocks(bytes32[] storage lockIds) private { uint256 index = 0; while (true) { diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index 1cd23d1..8d65687 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -55,10 +55,10 @@ contract Marketplace is Collateral, Proofs { RequestContext storage context = requestContexts[requestId]; require(context.state == RequestState.Started, "Invalid state"); - - _removeAccountLock(slot.host, requestId); - // TODO: burn host's collateral except for repair costs + mark proof + // TODO: burn host's slot collateral except for repair costs + mark proof // missing reward + // Slot collateral is not yet implemented as the design decision was + // not finalised. _unexpectProofs(slotId); @@ -66,6 +66,17 @@ contract Marketplace is Collateral, Proofs { slot.requestId = 0; context.slotsFilled -= 1; emit SlotFreed(requestId, slotId); + + Request memory request = _request(requestId); + uint256 slotsLost = request.ask.slots - context.slotsFilled; + if (slotsLost > request.ask.maxSlotLoss) { + context.state = RequestState.Failed; + emit RequestFailed(requestId); + + // TODO: burn all remaining slot collateral (note: slot collateral not + // yet implemented) + // TODO: send client remaining funds + } } function fillSlot( diff --git a/contracts/TestCollateral.sol b/contracts/TestCollateral.sol index efa2148..2524c76 100644 --- a/contracts/TestCollateral.sol +++ b/contracts/TestCollateral.sol @@ -23,8 +23,4 @@ contract TestCollateral is Collateral { function unlock(bytes32 id) public { _unlock(id); } - - function removeAccountLock(address account, bytes32 lockId) public { - _removeAccountLock(account, lockId); - } } diff --git a/test/Collateral.test.js b/test/Collateral.test.js index 96f7819..4ceea35 100644 --- a/test/Collateral.test.js +++ b/test/Collateral.test.js @@ -110,10 +110,5 @@ describe("Collateral", function () { await collateral.unlock(lock.id) await expect(collateral.withdraw()).not.to.be.reverted }) - - it("withdrawal succeeds when account lock has been removed", async function () { - await collateral.removeAccountLock(account0.address, lock.id) - await expect(collateral.withdraw()).not.to.be.reverted - }) }) }) diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index 955ce20..616e74f 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -119,13 +119,6 @@ describe("Marketplace", function () { // await marketplace.fillSlot(slot.request, slot.index, proof) }) - async function waitUntilAllSlotsFilled() { - const lastSlot = request.ask.slots - 1 - for (let i = 0; i <= lastSlot; i++) { - await marketplace.fillSlot(slot.request, i, proof) - } - } - it("fails to free slot when slot not filled", async function () { slot.index = 5 let nonExistentId = slotId(slot) @@ -140,19 +133,34 @@ describe("Marketplace", function () { }) it("successfully frees slot", async function () { - await waitUntilAllSlotsFilled() + await waitUntilAllSlotsFilled( + marketplace, + request.ask.slots, + slot.request, + proof + ) await expect(marketplace.freeSlot(id)).not.to.be.reverted }) it("emits event once slot is freed", async function () { - await waitUntilAllSlotsFilled() + await waitUntilAllSlotsFilled( + marketplace, + request.ask.slots, + slot.request, + proof + ) await expect(await marketplace.freeSlot(id)) .to.emit(marketplace, "SlotFreed") .withArgs(slot.request, id) }) it("cannot get slot once freed", async function () { - await waitUntilAllSlotsFilled() + await waitUntilAllSlotsFilled( + marketplace, + request.ask.slots, + slot.request, + proof + ) await marketplace.freeSlot(id) await expect(marketplace.slot(id)).to.be.revertedWith("Slot empty") }) @@ -477,7 +485,7 @@ describe("Marketplace", function () { ) }) - it("changes state to Failed once too many slots are freed", async function () { + // fill all slots, should change state to RequestState.Started await waitUntilAllSlotsFilled( marketplace, request.ask.slots, diff --git a/test/Storage.test.js b/test/Storage.test.js index bc12683..f6e2995 100644 --- a/test/Storage.test.js +++ b/test/Storage.test.js @@ -233,13 +233,6 @@ describe("Storage", function () { ;({ periodOf, periodEnd } = periodic(period)) }) - async function waitUntilAllSlotsFilled() { - const lastSlot = request.ask.slots - 1 - for (let i = 0; i <= lastSlot; i++) { - await storage.fillSlot(slot.request, i, proof) - } - } - async function waitUntilProofIsRequired(id) { await advanceTimeTo(periodEnd(periodOf(await currentTime()))) while ( @@ -266,7 +259,12 @@ describe("Storage", function () { it("frees slot when collateral slashed below minimum threshold", async function () { const id = slotId(slot) - await waitUntilAllSlotsFilled() + await waitUntilAllSlotsFilled( + storage, + request.ask.slots, + slot.request, + proof + ) while (true) { await markProofAsMissing(id)