Skip to content

Commit

Permalink
epoch time tests: 1 second off due to new warmup
Browse files Browse the repository at this point in the history
  • Loading branch information
jordaniza committed Sep 9, 2024
1 parent fd6d1a8 commit ce8b450
Show file tree
Hide file tree
Showing 12 changed files with 571 additions and 190 deletions.
2 changes: 1 addition & 1 deletion src/escrow/increasing/QuadraticIncreasingEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ contract QuadraticIncreasingEscrow is
}

function _isWarm(uint256 _tokenId, uint256 _userEpoch, uint256 t) public view returns (bool) {
return t > _userPointWarmup[_tokenId][_userEpoch];
return t >= _userPointWarmup[_tokenId][_userEpoch];
}

/*//////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/escrow/increasing/VotingEscrowIncreasing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ contract VotingEscrow is
if (_value == 0) revert ZeroAmount();

// query the duration lib to get the next time we can deposit
uint256 startTime = EpochDurationLib.epochNextDeposit(block.timestamp);
uint256 startTime = EpochDurationLib.epochNextDepositTs(block.timestamp);

// increment the total locked supply and mint the token
totalLocked += _value;
Expand Down
91 changes: 68 additions & 23 deletions src/libs/EpochDurationLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,103 @@ import {console2 as console} from "forge-std/console2.sol";
library EpochDurationLib {
uint256 internal constant EPOCH_DURATION = 2 weeks;
uint256 internal constant DEPOSIT_INTERVAL = 1 weeks;
uint256 internal constant MAXTIME = 4 * 365 * 86400;
uint256 internal constant VOTE_DURATION = 1 weeks;
uint256 internal constant VOTE_WINDOW_OFFSET = 1 hours;

function currentEpoch(uint256 timestamp) internal pure returns (uint256) {
unchecked {
return timestamp / EPOCH_DURATION;
}
}

function epochStart(uint256 timestamp) internal pure returns (uint256) {
function votingActive(uint256 timestamp) internal pure returns (bool) {
bool afterVoteStart = timestamp >= epochVoteStartTs(timestamp);
bool beforeVoteEnd = timestamp < epochVoteEndTs(timestamp);
return afterVoteStart && beforeVoteEnd;
}

function elapsedInEpoch(uint256 timestamp) internal pure returns (uint256) {
unchecked {
return timestamp - (timestamp % EPOCH_DURATION);
return timestamp % EPOCH_DURATION;
}
}

function epochNext(uint256 timestamp) internal pure returns (uint256) {
/// @notice Number of seconds until the start of the next epoch (relative)
function epochStartsIn(uint256 timestamp) internal pure returns (uint256) {
unchecked {
return epochStart(timestamp) + EPOCH_DURATION;
uint256 elapsed = elapsedInEpoch(timestamp);
return (elapsed == 0) ? 0 : EPOCH_DURATION - elapsed;
}
}

function epochVoteStart(uint256 timestamp) internal pure returns (uint256) {
/// @notice Timestamp of the start of the next epoch (absolute)
function epochStartTs(uint256 timestamp) internal pure returns (uint256) {
unchecked {
return epochStart(timestamp) + 1 hours;
return timestamp + epochStartsIn(timestamp);
}
}

function epochVoteEnd(uint256 timestamp) internal pure returns (uint256) {
/// @notice Number of seconds until voting starts.
/// @dev If voting is active, returns 0.
function epochVoteStartsIn(uint256 timestamp) internal pure returns (uint256) {
unchecked {
return epochStart(timestamp) + (EPOCH_DURATION / 2) - 1 hours;
uint256 elapsed = elapsedInEpoch(timestamp);

// if less than the offset has past, return the time until the offset
if (elapsed < VOTE_WINDOW_OFFSET) {
return VOTE_WINDOW_OFFSET - elapsed;
}
// if voting is active (we are in the voting period) return 0
else if (elapsed < VOTE_DURATION - VOTE_WINDOW_OFFSET) {
return 0;
}
// else return the time until the next epoch + the offset
else return epochStartsIn(timestamp) + VOTE_WINDOW_OFFSET;
}
}

/// aligns the timestamp to the next deposit interval
function epochNextDeposit(uint256 timestamp) internal pure returns (uint256) {
/// @notice Timestamp of the start of the next voting period (absolute)
function epochVoteStartTs(uint256 timestamp) internal pure returns (uint256) {
unchecked {
// Calculate the remaining time within the current interval
uint256 elapsed = timestamp % DEPOSIT_INTERVAL;
return timestamp + epochVoteStartsIn(timestamp);
}
}

// allow the user to deposit at the start of the interval
if (elapsed == 0) return timestamp;
/// @notice Number of seconds until the end of the current voting period (relative)
/// @dev If we are outside the voting period, returns 0
function epochVoteEndsIn(uint256 timestamp) internal pure returns (uint256) {
unchecked {
uint256 elapsed = elapsedInEpoch(timestamp);
uint VOTING_WINDOW = VOTE_DURATION - VOTE_WINDOW_OFFSET;
// if we are outside the voting period, return 0
if (elapsed >= VOTING_WINDOW) return 0;
// if we are in the voting period, return the remaining time
else return VOTING_WINDOW - elapsed;
}
}

// Calculate time left until the next interval
uint256 timeUntilNextInterval = DEPOSIT_INTERVAL - elapsed;
/// @notice Timestamp of the end of the current voting period (absolute)
function epochVoteEndTs(uint256 timestamp) internal pure returns (uint256) {
unchecked {
return timestamp + epochVoteEndsIn(timestamp);
}
}

// else the user must wait until the start of the next interval
return timestamp + timeUntilNextInterval;
/// @notice Number of seconds until the next deposit interval (relative)
function epochNextDepositIn(uint256 timestamp) internal pure returns (uint256) {
unchecked {
uint256 elapsed = elapsedInEpoch(timestamp);
// elapsed > deposit interval, then subtract the interval
if (elapsed > DEPOSIT_INTERVAL) elapsed -= DEPOSIT_INTERVAL;
if (elapsed == 0) return 0;
else return DEPOSIT_INTERVAL - elapsed;
}
}

function votingActive(uint256 timestamp) internal pure returns (bool) {
bool afterVoteStart = timestamp >= epochVoteStart(timestamp);
bool beforeVoteEnd = timestamp < epochVoteEnd(timestamp);
return afterVoteStart && beforeVoteEnd;
/// @notice Timestamp of the next deposit interval (absolute)
function epochNextDepositTs(uint256 timestamp) internal pure returns (uint256) {
unchecked {
return timestamp + epochNextDepositIn(timestamp);
}
}
}
15 changes: 8 additions & 7 deletions src/voting/ISimpleGaugeVoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ interface IGauge {
interface IGaugeVote {
/// @param votes gauge => votes cast at that time
/// @param gaugesVotedFor array of gauges we have active votes for
/// @param usedWeight total voting power used at the time of the vote
/// @param usedVotingPower total voting power used at the time of the vote
/// @dev this changes so we need an historic snapshot
/// @param lastVoted is the last time the user voted
struct TokenVoteData {
mapping(address => uint256) votes;
address[] gaugesVotedFor;
uint256 usedWeight;
uint256 usedVotingPower;
uint256 lastVoted;
}

Expand Down Expand Up @@ -69,17 +69,17 @@ interface IGaugeVoterEvents {
address indexed gauge,
uint256 indexed epoch,
uint256 tokenId,
uint256 weight,
uint256 totalWeight,
uint256 votingPower,
uint256 totalVotingPower,
uint256 timestamp
);
event Reset(
address indexed voter,
address indexed gauge,
uint256 indexed epoch,
uint256 tokenId,
uint256 weight,
uint256 totalWeight,
uint256 votingPower,
uint256 totalVotingPower,
uint256 timestamp
);
}
Expand All @@ -90,9 +90,10 @@ interface IGaugeVoterErrors {
error NotApprovedOrOwner();
error GaugeDoesNotExist(address _pool);
error GaugeInactive(address _gauge);
error MustReset();
error DoubleVote();
error NoVotes();
error NoVotingPower();
error NotCurrentlyVoting();
}

interface IGaugeVoter is IGaugeVoterEvents, IGaugeVoterErrors, IGaugeVote {
Expand Down
Loading

0 comments on commit ce8b450

Please sign in to comment.