[marketplace] freeSlot() can be called by host

Handles payout, slashing and removal from mySlots
This commit is contained in:
Mark Spanbroek 2022-11-23 15:29:58 +01:00 committed by markspanbroek
parent 6c52f60381
commit c46c268880
2 changed files with 34 additions and 39 deletions

View File

@ -118,6 +118,21 @@ contract Marketplace is Collateral, Proofs {
} }
} }
// TODO: test
function freeSlot(SlotId slotId) public {
Slot storage slot = _slot(slotId);
require(slot.host == msg.sender, "Slot filled by other host");
RequestState s = state(slot.requestId);
if (s == RequestState.Finished || s == RequestState.Cancelled) {
payoutSlot(slot.requestId, slotId);
slotsPerHost[msg.sender].remove(SlotId.unwrap(slotId));
} else if (s == RequestState.Failed) {
slotsPerHost[msg.sender].remove(SlotId.unwrap(slotId));
} else {
_forciblyFreeSlot(slotId);
}
}
function _forciblyFreeSlot(SlotId slotId) function _forciblyFreeSlot(SlotId slotId)
internal internal
slotMustAcceptProofs(slotId) slotMustAcceptProofs(slotId)
@ -159,8 +174,8 @@ contract Marketplace is Collateral, Proofs {
} }
} }
function payoutSlot(RequestId requestId, uint256 slotIndex) function payoutSlot(RequestId requestId, SlotId slotId)
public private
marketplaceInvariant marketplaceInvariant
{ {
require(_isFinished(requestId), "Contract not ended"); require(_isFinished(requestId), "Contract not ended");
@ -168,7 +183,6 @@ contract Marketplace is Collateral, Proofs {
Request storage request = _request(requestId); Request storage request = _request(requestId);
context.state = RequestState.Finished; context.state = RequestState.Finished;
requestsPerClient[request.client].remove(RequestId.unwrap(requestId)); requestsPerClient[request.client].remove(RequestId.unwrap(requestId));
SlotId slotId = _toSlotId(requestId, slotIndex);
Slot storage slot = _slot(slotId); Slot storage slot = _slot(slotId);
require(!slot.hostPaid, "Already paid"); require(!slot.hostPaid, "Already paid");

View File

@ -359,22 +359,6 @@ describe("Marketplace", function () {
) )
}) })
it("fails to free slot when cancelled", async function () {
await marketplace.fillSlot(slot.request, slot.index, proof)
await waitUntilCancelled(request)
await expect(marketplace.freeSlot(slotId(slot))).to.be.revertedWith(
"Slot not accepting proofs"
)
})
it("fails to free slot when finished", async function () {
await waitUntilStarted(marketplace, request, proof)
await waitUntilFinished(marketplace, requestId(request))
await expect(marketplace.freeSlot(slotId(slot))).to.be.revertedWith(
"Slot not accepting proofs"
)
})
it("successfully frees slot", async function () { it("successfully frees slot", async function () {
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
await expect(marketplace.freeSlot(id)).not.to.be.reverted await expect(marketplace.freeSlot(id)).not.to.be.reverted
@ -408,31 +392,32 @@ describe("Marketplace", function () {
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
await waitUntilFinished(marketplace, requestId(request)) await waitUntilFinished(marketplace, requestId(request))
const startBalance = await token.balanceOf(host.address) const startBalance = await token.balanceOf(host.address)
await marketplace.payoutSlot(slot.request, slot.index) await marketplace.freeSlot(slotId(slot))
const endBalance = await token.balanceOf(host.address) const endBalance = await token.balanceOf(host.address)
expect(endBalance - startBalance).to.equal(pricePerSlot(request)) expect(endBalance - startBalance).to.equal(pricePerSlot(request))
}) })
it("is only allowed when the contract has ended", async function () { it("only pays when the contract has ended", async function () {
await marketplace.fillSlot(slot.request, slot.index, proof) await marketplace.fillSlot(slot.request, slot.index, proof)
await expect( const startBalance = await token.balanceOf(host.address)
marketplace.payoutSlot(slot.request, slot.index) await marketplace.freeSlot(slotId(slot))
).to.be.revertedWith("Contract not ended") const endBalance = await token.balanceOf(host.address)
expect(endBalance).to.equal(startBalance)
}) })
it("can only be done once", async function () { it("can only be done once", async function () {
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
await waitUntilFinished(marketplace, requestId(request)) await waitUntilFinished(marketplace, requestId(request))
await marketplace.payoutSlot(slot.request, slot.index) await marketplace.freeSlot(slotId(slot))
await expect( await expect(marketplace.freeSlot(slotId(slot))).to.be.revertedWith(
marketplace.payoutSlot(slot.request, slot.index) "Already paid"
).to.be.revertedWith("Already paid") )
}) })
it("cannot be filled again", async function () { it("cannot be filled again", async function () {
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
await waitUntilFinished(marketplace, requestId(request)) await waitUntilFinished(marketplace, requestId(request))
await marketplace.payoutSlot(slot.request, slot.index) await marketplace.freeSlot(slotId(slot))
await expect(marketplace.fillSlot(slot.request, slot.index, proof)).to.be await expect(marketplace.fillSlot(slot.request, slot.index, proof)).to.be
.reverted .reverted
}) })
@ -575,7 +560,7 @@ describe("Marketplace", function () {
it("state is Finished once slot is paid out", async function () { it("state is Finished once slot is paid out", async function () {
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
await waitUntilFinished(marketplace, requestId(request)) await waitUntilFinished(marketplace, requestId(request))
await marketplace.payoutSlot(slot.request, slot.index) await marketplace.freeSlot(slotId(slot))
await expect(await marketplace.state(slot.request)).to.equal( await expect(await marketplace.state(slot.request)).to.equal(
RequestState.Finished RequestState.Finished
) )
@ -587,7 +572,7 @@ describe("Marketplace", function () {
for (let i = 0; i <= request.ask.maxSlotLoss; i++) { for (let i = 0; i <= request.ask.maxSlotLoss; i++) {
slot.index = i slot.index = i
let id = slotId(slot) let id = slotId(slot)
await marketplace.freeSlot(id) await marketplace.forciblyFreeSlot(id)
} }
await expect(await marketplace.state(slot.request)).to.equal( await expect(await marketplace.state(slot.request)).to.equal(
RequestState.New RequestState.New
@ -671,7 +656,7 @@ describe("Marketplace", function () {
it("fails when request Finished (state set to Finished)", async function () { it("fails when request Finished (state set to Finished)", async function () {
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
await waitUntilFinished(marketplace, requestId(request)) await waitUntilFinished(marketplace, requestId(request))
await marketplace.payoutSlot(slot.request, slot.index) await marketplace.freeSlot(slotId(slot))
await expect( await expect(
marketplace.testAcceptsProofs(slotId(slot)) marketplace.testAcceptsProofs(slotId(slot))
).to.be.revertedWith("Slot not accepting proofs") ).to.be.revertedWith("Slot not accepting proofs")
@ -679,11 +664,7 @@ describe("Marketplace", function () {
it("fails when request Failed", async function () { it("fails when request Failed", async function () {
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
for (let i = 0; i <= request.ask.maxSlotLoss; i++) { await waitUntilFailed(marketplace, request, slot)
slot.index = i
let id = slotId(slot)
await marketplace.freeSlot(id)
}
await expect( await expect(
marketplace.testAcceptsProofs(slotId(slot)) marketplace.testAcceptsProofs(slotId(slot))
).to.be.revertedWith("Slot empty") ).to.be.revertedWith("Slot empty")
@ -732,7 +713,7 @@ describe("Marketplace", function () {
switchAccount(host) switchAccount(host)
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
await waitUntilFinished(marketplace, requestId(request)) await waitUntilFinished(marketplace, requestId(request))
await marketplace.payoutSlot(slot.request, slot.index) await marketplace.freeSlot(slotId(slot))
switchAccount(client) switchAccount(client)
expect(await marketplace.myRequests()).to.deep.equal([]) expect(await marketplace.myRequests()).to.deep.equal([])
}) })
@ -829,7 +810,7 @@ describe("Marketplace", function () {
it("removes slot from list when slot is paid out", async function () { it("removes slot from list when slot is paid out", async function () {
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
await waitUntilFinished(marketplace, requestId(request)) await waitUntilFinished(marketplace, requestId(request))
await marketplace.payoutSlot(slot.request, slot.index) await marketplace.freeSlot(slotId(slot))
expect(await marketplace.mySlots()).to.not.contain(slotId(slot)) expect(await marketplace.mySlots()).to.not.contain(slotId(slot))
}) })
}) })