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

46 lines
1.2 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
contract Periods {
error Periods_InvalidSecondsPerPeriod();
type Period is uint64;
uint64 internal immutable _secondsPerPeriod;
constructor(uint64 secondsPerPeriod) {
if (secondsPerPeriod == 0) {
revert Periods_InvalidSecondsPerPeriod();
}
_secondsPerPeriod = secondsPerPeriod;
}
function _periodOf(uint64 timestamp) internal view returns (Period) {
return Period.wrap(timestamp / _secondsPerPeriod);
}
function _blockPeriod() internal view returns (Period) {
return _periodOf(uint64(block.timestamp));
}
function _nextPeriod(Period period) internal pure returns (Period) {
return Period.wrap(Period.unwrap(period) + 1);
}
function _periodStart(Period period) internal view returns (uint64) {
return Period.unwrap(period) * _secondsPerPeriod;
}
function _periodEnd(Period period) internal view returns (uint64) {
return _periodStart(_nextPeriod(period));
}
function _isBefore(Period a, Period b) internal pure returns (bool) {
return Period.unwrap(a) < Period.unwrap(b);
}
function _isAfter(Period a, Period b) internal pure returns (bool) {
return _isBefore(b, a);
}
}