2024-10-03 11:01:21 +10:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity 0.8.23;
|
|
|
|
|
|
|
|
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
|
|
|
|
import "./Requests.sol";
|
|
|
|
import "./Configuration.sol";
|
|
|
|
|
2024-10-30 15:48:37 +11:00
|
|
|
abstract contract SlotReservations {
|
2024-10-03 11:01:21 +10:00
|
|
|
using EnumerableSet for EnumerableSet.AddressSet;
|
|
|
|
|
|
|
|
mapping(SlotId => EnumerableSet.AddressSet) internal _reservations;
|
|
|
|
SlotReservationsConfig private _config;
|
|
|
|
|
|
|
|
constructor(SlotReservationsConfig memory config) {
|
|
|
|
_config = config;
|
|
|
|
}
|
|
|
|
|
2024-10-30 15:48:37 +11:00
|
|
|
function _slotIsFree(SlotId slotId) internal view virtual returns (bool);
|
|
|
|
|
2024-10-03 11:01:21 +10:00
|
|
|
function reserveSlot(RequestId requestId, uint256 slotIndex) public {
|
|
|
|
require(canReserveSlot(requestId, slotIndex), "Reservation not allowed");
|
|
|
|
|
|
|
|
SlotId slotId = Requests.slotId(requestId, slotIndex);
|
|
|
|
_reservations[slotId].add(msg.sender);
|
2024-10-04 13:28:39 +10:00
|
|
|
|
|
|
|
if (_reservations[slotId].length() == _config.maxReservations) {
|
|
|
|
emit SlotReservationsFull(requestId, slotIndex);
|
|
|
|
}
|
2024-10-03 11:01:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
function canReserveSlot(
|
|
|
|
RequestId requestId,
|
|
|
|
uint256 slotIndex
|
|
|
|
) public view returns (bool) {
|
|
|
|
address host = msg.sender;
|
|
|
|
SlotId slotId = Requests.slotId(requestId, slotIndex);
|
|
|
|
return
|
|
|
|
// TODO: add in check for address inside of expanding window
|
2024-10-30 15:48:37 +11:00
|
|
|
_slotIsFree(slotId) &&
|
2024-10-03 11:01:21 +10:00
|
|
|
(_reservations[slotId].length() < _config.maxReservations) &&
|
|
|
|
(!_reservations[slotId].contains(host));
|
|
|
|
}
|
2024-10-04 13:28:39 +10:00
|
|
|
|
|
|
|
event SlotReservationsFull(RequestId indexed requestId, uint256 slotIndex);
|
2024-10-03 11:01:21 +10:00
|
|
|
}
|