[marketplace] add slot/request mapping, cancelled check

Add a requestId mapping to the Slot type. This allows the requestId to be obtained for a slot and the StorageRequest can be looked up from the id.

Add `isSlotCancelled` to check if the request that a slot belongs to has been cancelled (not enough slots filled before expiry).
This commit is contained in:
Eric Mastro 2022-08-17 15:26:44 +10:00 committed by Eric Mastro
parent 20938ab448
commit 34553ad7d9
2 changed files with 16 additions and 3 deletions

View File

@ -72,6 +72,7 @@ contract Marketplace is Collateral, Proofs {
_submitProof(slotId, proof);
slot.host = msg.sender;
slot.requestId = requestId;
context.slotsFilled += 1;
emit SlotFilled(requestId, slotIndex, slotId);
if (context.slotsFilled == request.ask.slots) {
@ -119,12 +120,12 @@ contract Marketplace is Collateral, Proofs {
emit RequestCancelled(requestId);
}
/// @notice Rseturn true if the request state is RequestState.Cancelled or if the request expiry time has elapsed and the request was never started.
/// @notice Return true if the request state is RequestState.Cancelled or if the request expiry time has elapsed and the request was never started.
/// @dev Handles the case when a request may have been cancelled, but the client has not withdrawn its funds yet, and therefore the state has not yet been updated.
/// @param requestId the id of the request
/// @return true if request is cancelled
function isCancelled(bytes32 requestId) public view returns (bool) {
RequestContext storage context = requestContexts[requestId];
RequestContext memory context = requestContexts[requestId];
return
context.state == RequestState.Cancelled ||
(
@ -133,6 +134,17 @@ contract Marketplace is Collateral, Proofs {
);
}
/// @notice Return true if the request state the slot belongs to is RequestState.Cancelled or if the request expiry time has elapsed and the request was never started.
/// @dev Handles the case when a request may have been cancelled, but the client has not withdrawn its funds yet, and therefore the state has not yet been updated.
/// @param slotId the id of the slot
/// @return true if request is cancelled
function isSlotCancelled(bytes32 slotId) public view returns (bool) {
Slot memory slot = slots[slotId];
require(slot.host != address(0), "Slot empty");
require(slot.requestId != 0, "Missing request id");
return isCancelled(slot.requestId);
}
function _host(bytes32 slotId) internal view returns (address) {
return slots[slotId].host;
}
@ -225,6 +237,7 @@ contract Marketplace is Collateral, Proofs {
struct Slot {
address host;
bool hostPaid;
bytes32 requestId;
}
event StorageRequested(bytes32 requestId, Ask ask);

View File

@ -65,7 +65,7 @@ contract Storage is Collateral, Marketplace {
}
function markProofAsMissing(bytes32 slotId, uint256 period) public {
require(!isCancelled(slotId), "Request was cancelled");
require(!isSlotCancelled(slotId), "Request was cancelled");
_markProofAsMissing(slotId, period);
if (_missed(slotId) % slashMisses == 0) {
_slash(_host(slotId), slashPercentage);