dagger-contracts/contracts/SlotReservations.sol
Eric 1ce3d10fa2
fix(slot-reservations): ensure slot is free (#196)
Ensure that the slot state is free before allowing reservations
2024-10-30 15:48:37 +11:00

46 lines
1.4 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import "./Requests.sol";
import "./Configuration.sol";
abstract contract SlotReservations {
using EnumerableSet for EnumerableSet.AddressSet;
mapping(SlotId => EnumerableSet.AddressSet) internal _reservations;
SlotReservationsConfig private _config;
constructor(SlotReservationsConfig memory config) {
_config = config;
}
function _slotIsFree(SlotId slotId) internal view virtual returns (bool);
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);
if (_reservations[slotId].length() == _config.maxReservations) {
emit SlotReservationsFull(requestId, slotIndex);
}
}
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
_slotIsFree(slotId) &&
(_reservations[slotId].length() < _config.maxReservations) &&
(!_reservations[slotId].contains(host));
}
event SlotReservationsFull(RequestId indexed requestId, uint256 slotIndex);
}