[marketplace] requestIsKnown() modifier

Replaces _request() getter
This commit is contained in:
Mark Spanbroek 2023-01-17 08:57:31 +01:00 committed by markspanbroek
parent 6eab9fee9a
commit 944e9c9da2

View File

@ -72,8 +72,8 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
RequestId requestId, RequestId requestId,
uint256 slotIndex, uint256 slotIndex,
bytes calldata proof bytes calldata proof
) public { ) public requestIsKnown(requestId) {
Request storage request = _request(requestId); Request storage request = requests[requestId];
require(slotIndex < request.ask.slots, "Invalid slot"); require(slotIndex < request.ask.slots, "Invalid slot");
SlotId slotId = Requests.slotId(requestId, slotIndex); SlotId slotId = Requests.slotId(requestId, slotIndex);
@ -150,7 +150,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
context.slotsFilled -= 1; context.slotsFilled -= 1;
emit SlotFreed(requestId, slotId); emit SlotFreed(requestId, slotId);
Request storage request = _request(requestId); Request storage request = requests[requestId];
uint256 slotsLost = request.ask.slots - context.slotsFilled; uint256 slotsLost = request.ask.slots - context.slotsFilled;
if ( if (
slotsLost > request.ask.maxSlotLoss && slotsLost > request.ask.maxSlotLoss &&
@ -169,9 +169,9 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
function payoutSlot( function payoutSlot(
RequestId requestId, RequestId requestId,
SlotId slotId SlotId slotId
) private marketplaceInvariant { ) private requestIsKnown(requestId) marketplaceInvariant {
RequestContext storage context = requestContexts[requestId]; RequestContext storage context = requestContexts[requestId];
Request storage request = _request(requestId); Request storage request = requests[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);
@ -215,18 +215,15 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
return slots[slotId].host; return slots[slotId].host;
} }
function _request( modifier requestIsKnown(RequestId requestId) {
RequestId requestId require(requests[requestId].client != address(0), "Unknown request");
) internal view returns (Request storage) { _;
Request storage request = requests[requestId];
require(request.client != address(0), "Unknown request");
return request;
} }
function getRequest( function getRequest(
RequestId requestId RequestId requestId
) public view returns (Request memory) { ) public view requestIsKnown(requestId) returns (Request memory) {
return _request(requestId); return requests[requestId];
} }
function _slot(SlotId slotId) internal view returns (Slot storage) { function _slot(SlotId slotId) internal view returns (Slot storage) {
@ -271,11 +268,11 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
function requestState( function requestState(
RequestId requestId RequestId requestId
) public view returns (RequestState) { ) public view requestIsKnown(requestId) returns (RequestState) {
RequestContext storage context = requestContexts[requestId]; RequestContext storage context = requestContexts[requestId];
if ( if (
context.state == RequestState.New && context.state == RequestState.New &&
block.timestamp > _request(requestId).expiry block.timestamp > requests[requestId].expiry
) { ) {
return RequestState.Cancelled; return RequestState.Cancelled;
} else if ( } else if (