[marketplace] replace Slot.hostPaid with SlotState.Paid

This commit is contained in:
Mark Spanbroek 2023-01-16 17:18:17 +01:00 committed by markspanbroek
parent 5eeac8a782
commit dd65133576
2 changed files with 19 additions and 17 deletions

View File

@ -107,10 +107,11 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
function freeSlot(SlotId slotId) public { function freeSlot(SlotId slotId) public {
Slot storage slot = _slot(slotId); Slot storage slot = _slot(slotId);
require(slot.host == msg.sender, "Slot filled by other host"); require(slot.host == msg.sender, "Slot filled by other host");
RequestState state = requestState(slot.requestId); SlotState state = slotState(slotId);
if (state == RequestState.Finished || state == RequestState.Cancelled) { require(state != SlotState.Paid, "Already paid");
if (state == SlotState.Finished) {
payoutSlot(slot.requestId, slotId); payoutSlot(slot.requestId, slotId);
} else if (state == RequestState.Failed) { } else if (state == SlotState.Failed) {
removeFromMySlots(msg.sender, slotId); removeFromMySlots(msg.sender, slotId);
} else { } else {
_forciblyFreeSlot(slotId); _forciblyFreeSlot(slotId);
@ -180,24 +181,18 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
RequestId requestId, RequestId requestId,
SlotId slotId SlotId slotId
) private marketplaceInvariant { ) private marketplaceInvariant {
RequestState state = requestState(requestId);
require(
state == RequestState.Finished || state == RequestState.Cancelled,
"Contract not ended"
);
RequestContext storage context = _context(requestId); RequestContext storage context = _context(requestId);
Request storage request = _request(requestId); Request storage request = _request(requestId);
context.state = RequestState.Finished; context.state = RequestState.Finished;
removeFromMyRequests(request.client, requestId); removeFromMyRequests(request.client, requestId);
Slot storage slot = _slot(slotId); Slot storage slot = _slot(slotId);
require(!slot.hostPaid, "Already paid");
removeFromMySlots(slot.host, slotId); removeFromMySlots(slot.host, slotId);
uint256 amount = pricePerSlot(requests[requestId]); uint256 amount = pricePerSlot(requests[requestId]);
funds.sent += amount; funds.sent += amount;
funds.balance -= amount; funds.balance -= amount;
slot.hostPaid = true; slot.state = SlotState.Paid;
require(token.transfer(slot.host, amount), "Payment failed"); require(token.transfer(slot.host, amount), "Payment failed");
} }
@ -259,7 +254,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
function _slot(SlotId slotId) internal view returns (Slot storage) { function _slot(SlotId slotId) internal view returns (Slot storage) {
Slot storage slot = slots[slotId]; Slot storage slot = slots[slotId];
require(slot.state == SlotState.Filled, "Slot empty"); require(slot.state != SlotState.Free, "Slot empty");
return slot; return slot;
} }
@ -348,10 +343,16 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
function slotState(SlotId slotId) internal view override returns (SlotState) { function slotState(SlotId slotId) internal view override returns (SlotState) {
Slot storage slot = slots[slotId]; Slot storage slot = slots[slotId];
RequestState reqState = requestState(slot.requestId); RequestState reqState = requestState(slot.requestId);
if ( if (slot.state == SlotState.Paid) {
return SlotState.Paid;
} else if (
slot.state == SlotState.Failed || reqState == RequestState.Failed
) {
return SlotState.Failed;
} else if (
slot.state == SlotState.Finished ||
reqState == RequestState.Finished || reqState == RequestState.Finished ||
reqState == RequestState.Cancelled || reqState == RequestState.Cancelled
reqState == RequestState.Failed
) { ) {
return SlotState.Finished; return SlotState.Finished;
} else { } else {
@ -378,9 +379,8 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
struct Slot { struct Slot {
SlotState state; SlotState state;
address host;
bool hostPaid;
RequestId requestId; RequestId requestId;
address host;
} }
event StorageRequested(RequestId requestId, Ask ask); event StorageRequested(RequestId requestId, Ask ask);

View File

@ -48,7 +48,9 @@ enum RequestState {
enum SlotState { enum SlotState {
Free, // [default] not filled yet, or host has freed slot Free, // [default] not filled yet, or host has freed slot
Filled, // host has filled slot Filled, // host has filled slot
Finished // successfully or unsuccessfully completed Finished, // successfully completed
Failed, // host has missed too many proofs
Paid // host has been paid
} }
library Requests { library Requests {