[marketplace] tests for Failed state

This commit is contained in:
Eric Mastro 2022-09-21 19:34:05 +10:00 committed by Eric Mastro
parent 08e73d9348
commit 2170d6bd19
6 changed files with 39 additions and 50 deletions

View File

@ -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) {

View File

@ -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(

View File

@ -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);
}
}

View File

@ -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
})
})
})

View File

@ -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,

View File

@ -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)