2022-02-16 09:50:00 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-09-29 10:18:02 +00:00
|
|
|
pragma solidity ^0.8.8;
|
2022-02-16 09:50:00 +00:00
|
|
|
|
|
|
|
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
2022-09-23 02:33:39 +00:00
|
|
|
import "@openzeppelin/contracts/utils/math/Math.sol";
|
2022-10-25 02:06:52 +00:00
|
|
|
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
|
2023-01-09 12:56:29 +00:00
|
|
|
import "./Requests.sol";
|
2022-02-16 13:38:19 +00:00
|
|
|
import "./Collateral.sol";
|
2022-06-13 08:40:18 +00:00
|
|
|
import "./Proofs.sol";
|
2023-01-10 14:04:16 +00:00
|
|
|
import "./StateRetrieval.sol";
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
contract Marketplace is Collateral, Proofs, StateRetrieval {
|
2022-10-25 02:06:52 +00:00
|
|
|
using EnumerableSet for EnumerableSet.Bytes32Set;
|
2023-01-10 14:04:16 +00:00
|
|
|
using Requests for Request;
|
2022-10-25 02:06:52 +00:00
|
|
|
|
2022-02-16 13:38:19 +00:00
|
|
|
uint256 public immutable collateral;
|
2023-01-09 14:52:27 +00:00
|
|
|
uint256 public immutable minCollateralThreshold;
|
|
|
|
uint256 public immutable slashMisses;
|
|
|
|
uint256 public immutable slashPercentage;
|
|
|
|
|
2022-02-16 13:15:43 +00:00
|
|
|
MarketplaceFunds private funds;
|
2022-09-29 10:18:02 +00:00
|
|
|
mapping(RequestId => Request) private requests;
|
|
|
|
mapping(RequestId => RequestContext) private requestContexts;
|
|
|
|
mapping(SlotId => Slot) private slots;
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2022-06-13 08:40:18 +00:00
|
|
|
constructor(
|
|
|
|
IERC20 _token,
|
|
|
|
uint256 _collateral,
|
2023-01-09 14:52:27 +00:00
|
|
|
uint256 _minCollateralThreshold,
|
|
|
|
uint256 _slashMisses,
|
|
|
|
uint256 _slashPercentage,
|
2022-06-13 08:40:18 +00:00
|
|
|
uint256 _proofPeriod,
|
|
|
|
uint256 _proofTimeout,
|
|
|
|
uint8 _proofDowntime
|
|
|
|
)
|
2022-02-16 13:38:19 +00:00
|
|
|
Collateral(_token)
|
2022-06-13 08:40:18 +00:00
|
|
|
Proofs(_proofPeriod, _proofTimeout, _proofDowntime)
|
2022-02-16 13:38:19 +00:00
|
|
|
marketplaceInvariant
|
|
|
|
{
|
|
|
|
collateral = _collateral;
|
2023-01-09 14:52:27 +00:00
|
|
|
minCollateralThreshold = _minCollateralThreshold;
|
|
|
|
slashMisses = _slashMisses;
|
|
|
|
slashPercentage = _slashPercentage;
|
2022-02-16 09:50:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 15:07:01 +00:00
|
|
|
function isWithdrawAllowed() internal view override returns (bool) {
|
2023-01-10 14:04:16 +00:00
|
|
|
return !hasSlots(msg.sender);
|
2022-11-23 15:07:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:04:04 +00:00
|
|
|
function requestStorage(
|
|
|
|
Request calldata request
|
|
|
|
) public marketplaceInvariant {
|
2022-02-17 10:00:18 +00:00
|
|
|
require(request.client == msg.sender, "Invalid client address");
|
2022-02-21 11:55:00 +00:00
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
RequestId id = request.id();
|
2022-02-17 10:09:35 +00:00
|
|
|
require(requests[id].client == address(0), "Request already exists");
|
2022-02-21 11:55:00 +00:00
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
requests[id] = request;
|
2022-09-29 10:07:55 +00:00
|
|
|
RequestContext storage context = _context(id);
|
|
|
|
// set contract end time to `duration` from now (time request was created)
|
|
|
|
context.endsAt = block.timestamp + request.ask.duration;
|
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
addToMyRequests(request.client, id);
|
2022-02-21 11:55:00 +00:00
|
|
|
|
2022-07-20 09:07:20 +00:00
|
|
|
uint256 amount = price(request);
|
|
|
|
funds.received += amount;
|
|
|
|
funds.balance += amount;
|
|
|
|
transferFrom(msg.sender, amount);
|
2022-02-21 11:55:00 +00:00
|
|
|
|
2022-04-06 12:26:56 +00:00
|
|
|
emit StorageRequested(id, request.ask);
|
2022-02-16 09:50:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 09:33:54 +00:00
|
|
|
function fillSlot(
|
2022-09-29 10:18:02 +00:00
|
|
|
RequestId requestId,
|
2022-07-19 09:33:54 +00:00
|
|
|
uint256 slotIndex,
|
|
|
|
bytes calldata proof
|
2022-09-21 09:13:12 +00:00
|
|
|
) public requestMustAcceptProofs(requestId) marketplaceInvariant {
|
2022-09-13 07:14:57 +00:00
|
|
|
Request storage request = _request(requestId);
|
2022-07-20 08:44:22 +00:00
|
|
|
require(slotIndex < request.ask.slots, "Invalid slot");
|
2022-07-19 09:33:54 +00:00
|
|
|
|
2023-01-11 09:52:08 +00:00
|
|
|
SlotId slotId = Requests.slotId(requestId, slotIndex);
|
2022-07-19 09:33:54 +00:00
|
|
|
Slot storage slot = slots[slotId];
|
2023-01-16 15:31:04 +00:00
|
|
|
slot.requestId = requestId;
|
|
|
|
|
|
|
|
require(slotState(slotId) == SlotState.Free, "Slot already filled");
|
2022-07-19 09:33:54 +00:00
|
|
|
|
|
|
|
require(balanceOf(msg.sender) >= collateral, "Insufficient collateral");
|
|
|
|
|
2023-01-10 11:51:26 +00:00
|
|
|
_startRequiringProofs(slotId, request.ask.proofProbability);
|
2023-01-09 14:19:00 +00:00
|
|
|
submitProof(slotId, proof);
|
2022-07-19 09:33:54 +00:00
|
|
|
|
|
|
|
slot.host = msg.sender;
|
2023-01-16 15:31:04 +00:00
|
|
|
slot.state = SlotState.Filled;
|
2022-09-21 09:57:26 +00:00
|
|
|
RequestContext storage context = _context(requestId);
|
2022-08-03 09:54:29 +00:00
|
|
|
context.slotsFilled += 1;
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
addToMySlots(slot.host, slotId);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2022-07-19 09:33:54 +00:00
|
|
|
emit SlotFilled(requestId, slotIndex, slotId);
|
2022-08-03 09:54:29 +00:00
|
|
|
if (context.slotsFilled == request.ask.slots) {
|
|
|
|
context.state = RequestState.Started;
|
2022-09-21 09:38:42 +00:00
|
|
|
context.startedAt = block.timestamp;
|
2022-07-20 08:29:24 +00:00
|
|
|
emit RequestFulfilled(requestId);
|
|
|
|
}
|
2022-07-19 09:33:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 14:29:58 +00:00
|
|
|
function freeSlot(SlotId slotId) public {
|
|
|
|
Slot storage slot = _slot(slotId);
|
|
|
|
require(slot.host == msg.sender, "Slot filled by other host");
|
2023-01-16 13:28:42 +00:00
|
|
|
RequestState state = requestState(slot.requestId);
|
|
|
|
if (state == RequestState.Finished || state == RequestState.Cancelled) {
|
2022-11-23 14:29:58 +00:00
|
|
|
payoutSlot(slot.requestId, slotId);
|
2023-01-16 13:28:42 +00:00
|
|
|
} else if (state == RequestState.Failed) {
|
2023-01-10 14:04:16 +00:00
|
|
|
removeFromMySlots(msg.sender, slotId);
|
2022-11-23 14:29:58 +00:00
|
|
|
} else {
|
|
|
|
_forciblyFreeSlot(slotId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:04:04 +00:00
|
|
|
function markProofAsMissing(
|
|
|
|
SlotId slotId,
|
2023-01-10 13:16:32 +00:00
|
|
|
Period period
|
2023-01-10 10:04:04 +00:00
|
|
|
) public slotMustAcceptProofs(slotId) {
|
2023-01-09 14:52:27 +00:00
|
|
|
_markProofAsMissing(slotId, period);
|
2023-01-10 10:33:18 +00:00
|
|
|
address host = getHost(slotId);
|
2023-01-09 14:52:27 +00:00
|
|
|
if (missingProofs(slotId) % slashMisses == 0) {
|
|
|
|
_slash(host, slashPercentage);
|
|
|
|
|
|
|
|
if (balanceOf(host) < minCollateralThreshold) {
|
|
|
|
// When the collateral drops below the minimum threshold, the slot
|
|
|
|
// needs to be freed so that there is enough remaining collateral to be
|
|
|
|
// distributed for repairs and rewards (with any leftover to be burnt).
|
|
|
|
_forciblyFreeSlot(slotId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:04:04 +00:00
|
|
|
function _forciblyFreeSlot(
|
|
|
|
SlotId slotId
|
|
|
|
)
|
2022-10-25 02:04:16 +00:00
|
|
|
internal
|
|
|
|
slotMustAcceptProofs(slotId)
|
|
|
|
marketplaceInvariant
|
2022-11-23 11:25:47 +00:00
|
|
|
// TODO: restrict senders that can call this function
|
2022-10-25 02:04:16 +00:00
|
|
|
{
|
2022-09-16 05:00:54 +00:00
|
|
|
Slot storage slot = _slot(slotId);
|
2022-09-29 10:18:02 +00:00
|
|
|
RequestId requestId = slot.requestId;
|
2022-09-16 05:00:54 +00:00
|
|
|
RequestContext storage context = requestContexts[requestId];
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
removeFromMySlots(slot.host, slotId);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-01-16 15:31:04 +00:00
|
|
|
slot.state = SlotState.Free;
|
2022-09-16 05:00:54 +00:00
|
|
|
slot.host = address(0);
|
2022-09-29 10:18:02 +00:00
|
|
|
slot.requestId = RequestId.wrap(0);
|
2022-09-16 05:00:54 +00:00
|
|
|
context.slotsFilled -= 1;
|
|
|
|
emit SlotFreed(requestId, slotId);
|
|
|
|
|
2022-09-21 09:13:12 +00:00
|
|
|
Request storage request = _request(requestId);
|
2022-09-16 05:00:54 +00:00
|
|
|
uint256 slotsLost = request.ask.slots - context.slotsFilled;
|
2022-10-26 08:58:14 +00:00
|
|
|
if (
|
|
|
|
slotsLost > request.ask.maxSlotLoss &&
|
|
|
|
context.state == RequestState.Started
|
|
|
|
) {
|
2022-09-16 05:00:54 +00:00
|
|
|
context.state = RequestState.Failed;
|
2022-09-23 02:33:39 +00:00
|
|
|
context.endsAt = block.timestamp - 1;
|
2022-09-16 05:00:54 +00:00
|
|
|
emit RequestFailed(requestId);
|
|
|
|
|
|
|
|
// TODO: burn all remaining slot collateral (note: slot collateral not
|
|
|
|
// yet implemented)
|
|
|
|
// TODO: send client remaining funds
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 02:04:16 +00:00
|
|
|
|
2023-01-10 10:04:04 +00:00
|
|
|
function payoutSlot(
|
|
|
|
RequestId requestId,
|
|
|
|
SlotId slotId
|
|
|
|
) private marketplaceInvariant {
|
2023-01-16 13:28:42 +00:00
|
|
|
RequestState state = requestState(requestId);
|
2022-11-23 15:07:01 +00:00
|
|
|
require(
|
2023-01-16 13:28:42 +00:00
|
|
|
state == RequestState.Finished || state == RequestState.Cancelled,
|
2022-11-23 15:07:01 +00:00
|
|
|
"Contract not ended"
|
|
|
|
);
|
2022-09-21 09:57:26 +00:00
|
|
|
RequestContext storage context = _context(requestId);
|
2022-10-25 02:06:52 +00:00
|
|
|
Request storage request = _request(requestId);
|
2022-09-21 09:57:26 +00:00
|
|
|
context.state = RequestState.Finished;
|
2023-01-10 14:04:16 +00:00
|
|
|
removeFromMyRequests(request.client, requestId);
|
2022-09-21 09:38:42 +00:00
|
|
|
Slot storage slot = _slot(slotId);
|
2022-07-19 15:09:35 +00:00
|
|
|
require(!slot.hostPaid, "Already paid");
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
removeFromMySlots(slot.host, slotId);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2022-07-20 09:07:20 +00:00
|
|
|
uint256 amount = pricePerSlot(requests[requestId]);
|
2022-07-19 15:09:35 +00:00
|
|
|
funds.sent += amount;
|
|
|
|
funds.balance -= amount;
|
|
|
|
slot.hostPaid = true;
|
|
|
|
require(token.transfer(slot.host, amount), "Payment failed");
|
|
|
|
}
|
|
|
|
|
2022-08-17 05:26:04 +00:00
|
|
|
/// @notice Withdraws storage request funds back to the client that deposited them.
|
|
|
|
/// @dev Request must be expired, must be in RequestState.New, and the transaction must originate from the depositer address.
|
|
|
|
/// @param requestId the id of the request
|
2022-09-29 10:18:02 +00:00
|
|
|
function withdrawFunds(RequestId requestId) public marketplaceInvariant {
|
2022-09-07 05:25:01 +00:00
|
|
|
Request storage request = requests[requestId];
|
2022-08-04 02:14:36 +00:00
|
|
|
require(block.timestamp > request.expiry, "Request not yet timed out");
|
|
|
|
require(request.client == msg.sender, "Invalid client address");
|
2022-09-21 09:57:26 +00:00
|
|
|
RequestContext storage context = _context(requestId);
|
2022-08-04 02:14:36 +00:00
|
|
|
require(context.state == RequestState.New, "Invalid state");
|
|
|
|
|
|
|
|
// Update request state to Cancelled. Handle in the withdraw transaction
|
|
|
|
// as there needs to be someone to pay for the gas to update the state
|
|
|
|
context.state = RequestState.Cancelled;
|
2023-01-10 14:04:16 +00:00
|
|
|
removeFromMyRequests(request.client, requestId);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2022-08-04 02:14:36 +00:00
|
|
|
emit RequestCancelled(requestId);
|
2022-09-07 05:25:01 +00:00
|
|
|
|
|
|
|
// TODO: To be changed once we start paying out hosts for the time they
|
|
|
|
// fill a slot. The amount that we paid to hosts will then have to be
|
|
|
|
// deducted from the price.
|
|
|
|
uint256 amount = _price(request);
|
|
|
|
funds.sent += amount;
|
|
|
|
funds.balance -= amount;
|
|
|
|
require(token.transfer(msg.sender, amount), "Withdraw failed");
|
2022-08-04 02:14:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 06:16:45 +00:00
|
|
|
/// @notice Return id of request that slot belongs to
|
|
|
|
/// @dev Returns requestId that is mapped to the slotId
|
|
|
|
/// @param slotId id of the slot
|
|
|
|
/// @return if of the request the slot belongs to
|
2023-01-10 10:04:04 +00:00
|
|
|
function _getRequestIdForSlot(
|
|
|
|
SlotId slotId
|
|
|
|
) internal view returns (RequestId) {
|
2022-08-22 06:16:45 +00:00
|
|
|
Slot memory slot = _slot(slotId);
|
2023-01-16 13:21:20 +00:00
|
|
|
require(RequestId.unwrap(slot.requestId) != 0, "Missing request id");
|
2022-08-22 06:16:45 +00:00
|
|
|
return slot.requestId;
|
|
|
|
}
|
|
|
|
|
2023-01-09 12:38:12 +00:00
|
|
|
function getHost(SlotId slotId) public view returns (address) {
|
2023-01-10 10:33:18 +00:00
|
|
|
return slots[slotId].host;
|
2023-01-09 12:38:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:04:04 +00:00
|
|
|
function _request(
|
|
|
|
RequestId requestId
|
|
|
|
) internal view returns (Request storage) {
|
2022-09-21 09:57:26 +00:00
|
|
|
Request storage request = requests[requestId];
|
2022-09-13 07:14:57 +00:00
|
|
|
require(request.client != address(0), "Unknown request");
|
2022-09-13 07:13:11 +00:00
|
|
|
return request;
|
2022-02-22 08:25:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:04:04 +00:00
|
|
|
function getRequest(
|
|
|
|
RequestId requestId
|
|
|
|
) public view returns (Request memory) {
|
2023-01-09 12:38:12 +00:00
|
|
|
return _request(requestId);
|
|
|
|
}
|
|
|
|
|
2022-09-29 10:18:02 +00:00
|
|
|
function _slot(SlotId slotId) internal view returns (Slot storage) {
|
2022-09-13 07:14:57 +00:00
|
|
|
Slot storage slot = slots[slotId];
|
2023-01-16 15:31:04 +00:00
|
|
|
require(slot.state == SlotState.Filled, "Slot empty");
|
2022-08-22 06:16:45 +00:00
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:04:04 +00:00
|
|
|
function _context(
|
|
|
|
RequestId requestId
|
|
|
|
) internal view returns (RequestContext storage) {
|
2022-09-08 07:56:01 +00:00
|
|
|
return requestContexts[requestId];
|
|
|
|
}
|
|
|
|
|
2022-06-13 09:49:25 +00:00
|
|
|
function proofPeriod() public view returns (uint256) {
|
2023-01-10 13:16:32 +00:00
|
|
|
return secondsPerPeriod;
|
2022-06-13 09:49:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:49:37 +00:00
|
|
|
function requestEnd(RequestId requestId) public view returns (uint256) {
|
2023-01-10 11:11:20 +00:00
|
|
|
uint256 end = _context(requestId).endsAt;
|
2022-10-26 08:58:14 +00:00
|
|
|
if (_requestAcceptsProofs(requestId)) {
|
2022-10-26 02:15:42 +00:00
|
|
|
return end;
|
|
|
|
} else {
|
|
|
|
return Math.min(end, block.timestamp - 1);
|
|
|
|
}
|
2022-10-25 12:49:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 14:15:11 +00:00
|
|
|
function isProofRequired(SlotId slotId) public view returns (bool) {
|
2023-01-16 15:31:04 +00:00
|
|
|
if (slotState(slotId) != SlotState.Filled) {
|
2023-01-09 14:15:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _isProofRequired(slotId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function willProofBeRequired(SlotId slotId) public view returns (bool) {
|
2023-01-16 15:31:04 +00:00
|
|
|
if (slotState(slotId) != SlotState.Filled) {
|
2023-01-09 14:15:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _willProofBeRequired(slotId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getChallenge(SlotId slotId) public view returns (bytes32) {
|
2023-01-16 15:31:04 +00:00
|
|
|
if (slotState(slotId) != SlotState.Filled) {
|
2023-01-09 14:15:11 +00:00
|
|
|
return bytes32(0);
|
|
|
|
}
|
|
|
|
return _getChallenge(slotId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPointer(SlotId slotId) public view returns (uint8) {
|
|
|
|
return _getPointer(slotId);
|
|
|
|
}
|
|
|
|
|
2022-08-04 02:14:36 +00:00
|
|
|
function _price(
|
|
|
|
uint64 numSlots,
|
|
|
|
uint256 duration,
|
2022-10-25 02:04:16 +00:00
|
|
|
uint256 reward
|
|
|
|
) internal pure returns (uint256) {
|
2022-08-04 02:14:36 +00:00
|
|
|
return numSlots * duration * reward;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _price(Request memory request) internal pure returns (uint256) {
|
|
|
|
return _price(request.ask.slots, request.ask.duration, request.ask.reward);
|
|
|
|
}
|
|
|
|
|
2022-07-20 09:07:20 +00:00
|
|
|
function price(Request calldata request) private pure returns (uint256) {
|
2022-08-04 02:14:36 +00:00
|
|
|
return _price(request.ask.slots, request.ask.duration, request.ask.reward);
|
2022-07-20 09:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function pricePerSlot(Request memory request) private pure returns (uint256) {
|
|
|
|
return request.ask.duration * request.ask.reward;
|
|
|
|
}
|
|
|
|
|
2023-01-16 13:28:42 +00:00
|
|
|
function requestState(
|
|
|
|
RequestId requestId
|
|
|
|
) public view returns (RequestState) {
|
2023-01-16 13:14:15 +00:00
|
|
|
RequestContext storage context = _context(requestId);
|
|
|
|
if (
|
|
|
|
context.state == RequestState.New &&
|
|
|
|
block.timestamp > _request(requestId).expiry
|
|
|
|
) {
|
2022-09-08 07:56:01 +00:00
|
|
|
return RequestState.Cancelled;
|
2023-01-16 13:14:15 +00:00
|
|
|
} else if (
|
|
|
|
context.state == RequestState.Started && block.timestamp > context.endsAt
|
|
|
|
) {
|
2022-09-21 09:57:26 +00:00
|
|
|
return RequestState.Finished;
|
2022-09-08 07:56:01 +00:00
|
|
|
} else {
|
|
|
|
return context.state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 15:31:04 +00:00
|
|
|
function slotState(SlotId slotId) internal view override returns (SlotState) {
|
|
|
|
Slot storage slot = slots[slotId];
|
|
|
|
RequestState reqState = requestState(slot.requestId);
|
|
|
|
if (
|
|
|
|
reqState == RequestState.Finished ||
|
|
|
|
reqState == RequestState.Cancelled ||
|
|
|
|
reqState == RequestState.Failed
|
|
|
|
) {
|
|
|
|
return SlotState.Finished;
|
|
|
|
} else {
|
|
|
|
return slot.state;
|
|
|
|
}
|
2022-09-29 10:18:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 07:56:01 +00:00
|
|
|
/// @notice returns true when the request is accepting proof submissions from hosts occupying slots.
|
|
|
|
/// @dev Request state must be new or started, and must not be cancelled, finished, or failed.
|
|
|
|
/// @param requestId id of the request for which to obtain state info
|
2023-01-10 10:04:04 +00:00
|
|
|
function _requestAcceptsProofs(
|
|
|
|
RequestId requestId
|
|
|
|
) internal view returns (bool) {
|
2023-01-16 13:28:42 +00:00
|
|
|
RequestState state = requestState(requestId);
|
|
|
|
return state == RequestState.New || state == RequestState.Started;
|
2022-09-08 07:56:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 09:54:29 +00:00
|
|
|
struct RequestContext {
|
|
|
|
RequestState state;
|
2023-01-16 15:31:04 +00:00
|
|
|
uint256 slotsFilled;
|
2022-09-21 09:38:42 +00:00
|
|
|
uint256 startedAt;
|
2022-09-21 09:57:26 +00:00
|
|
|
uint256 endsAt;
|
2022-07-20 08:29:24 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 09:33:54 +00:00
|
|
|
struct Slot {
|
2023-01-16 15:31:04 +00:00
|
|
|
SlotState state;
|
2022-07-19 09:33:54 +00:00
|
|
|
address host;
|
2022-07-19 15:09:35 +00:00
|
|
|
bool hostPaid;
|
2022-09-29 10:18:02 +00:00
|
|
|
RequestId requestId;
|
2022-07-19 09:33:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 10:18:02 +00:00
|
|
|
event StorageRequested(RequestId requestId, Ask ask);
|
|
|
|
event RequestFulfilled(RequestId indexed requestId);
|
|
|
|
event RequestFailed(RequestId indexed requestId);
|
2022-07-19 09:33:54 +00:00
|
|
|
event SlotFilled(
|
2022-09-29 10:18:02 +00:00
|
|
|
RequestId indexed requestId,
|
2022-07-19 09:33:54 +00:00
|
|
|
uint256 indexed slotIndex,
|
2022-09-29 10:18:02 +00:00
|
|
|
SlotId slotId
|
2022-07-19 09:33:54 +00:00
|
|
|
);
|
2022-09-29 10:18:02 +00:00
|
|
|
event SlotFreed(RequestId indexed requestId, SlotId slotId);
|
|
|
|
event RequestCancelled(RequestId indexed requestId);
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2022-02-16 13:15:43 +00:00
|
|
|
modifier marketplaceInvariant() {
|
|
|
|
MarketplaceFunds memory oldFunds = funds;
|
2022-02-16 09:50:00 +00:00
|
|
|
_;
|
2022-02-16 13:15:43 +00:00
|
|
|
assert(funds.received >= oldFunds.received);
|
|
|
|
assert(funds.sent >= oldFunds.sent);
|
|
|
|
assert(funds.received == funds.balance + funds.sent);
|
2022-02-16 09:50:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 07:56:01 +00:00
|
|
|
/// @notice Modifier that requires the request state to be that which is accepting proof submissions from hosts occupying slots.
|
|
|
|
/// @dev Request state must be new or started, and must not be cancelled, finished, or failed.
|
|
|
|
/// @param slotId id of the slot, that is mapped to a request, for which to obtain state info
|
2022-09-29 10:18:02 +00:00
|
|
|
modifier slotMustAcceptProofs(SlotId slotId) {
|
|
|
|
RequestId requestId = _getRequestIdForSlot(slotId);
|
2022-09-08 07:56:01 +00:00
|
|
|
require(_requestAcceptsProofs(requestId), "Slot not accepting proofs");
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2022-09-21 09:13:12 +00:00
|
|
|
/// @notice Modifier that requires the request state to be that which is accepting proof submissions from hosts occupying slots.
|
|
|
|
/// @dev Request state must be new or started, and must not be cancelled, finished, or failed.
|
|
|
|
/// @param requestId id of the request, for which to obtain state info
|
2022-09-29 10:18:02 +00:00
|
|
|
modifier requestMustAcceptProofs(RequestId requestId) {
|
2022-09-21 09:13:12 +00:00
|
|
|
require(_requestAcceptsProofs(requestId), "Request not accepting proofs");
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2022-02-16 13:15:43 +00:00
|
|
|
struct MarketplaceFunds {
|
2022-02-16 09:50:00 +00:00
|
|
|
uint256 balance;
|
|
|
|
uint256 received;
|
|
|
|
uint256 sent;
|
|
|
|
}
|
|
|
|
}
|