logos-storage-contracts-eth/contracts/SlotReservations.sol
Adam Uhlíř c00152e621
perf: optimizing parameters sizing (#207)
* perf: optimizing parameters sizing

* chore: feedback

Co-authored-by: markspanbroek <mark@spanbroek.net>

* style: formatting

* perf: more optimizations

* chore: fixes

* chore: fix certora spec

* chore: more fixes for certora spec

* chore: more and more fixes for certora spec

* fix: ends type

* test(certora): timestamp conversion

* test(certora): timestamp conversion again

* test(certora): timestamp conversion revert to assert_uint64

* test(certora): timestamp with mathint

* test(certora): timestamp back with uint64 with require

* Add missing configuration

* Fix previous merge

* Update StorageRequested to use int64 for expiry

* requestDurationLimit => uint64

---------

Co-authored-by: markspanbroek <mark@spanbroek.net>
Co-authored-by: Arnaud <arnaud@status.im>
Co-authored-by: Eric <5089238+emizzle@users.noreply.github.com>
2025-02-20 16:54:41 +11:00

48 lines
1.5 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;
error SlotReservations_ReservationNotAllowed();
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, uint64 slotIndex) public {
if (!canReserveSlot(requestId, slotIndex))
revert SlotReservations_ReservationNotAllowed();
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,
uint64 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, uint64 slotIndex);
}