Skip to content

Commit

Permalink
Reverted Observation back to uint32 timestamp. Added graceful shutdow…
Browse files Browse the repository at this point in the history
…n code
  • Loading branch information
asselstine committed Sep 29, 2023
1 parent 8ec3b52 commit 2f9d736
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 302 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 90% Test Coverage
name: 100% Test Coverage

on: ["push", "pull_request"]

Expand Down Expand Up @@ -35,12 +35,12 @@ jobs:
env:
MAINNET_RPC_URL: ${{ secrets.MAINNET_RPC_URL }}
run: |
forge coverage --report lcov && lcov --extract lcov.info -o lcov.info 'src/*'
forge coverage --nmc Invariant --report lcov && lcov --extract lcov.info -o lcov.info 'src/*'
id: coverage

- name: Report code coverage
uses: zgosalvez/[email protected]
with:
coverage-files: lcov.info
minimum-coverage: 90
minimum-coverage: 100
github-token: ${{ secrets.GITHUB_TOKEN }}
27 changes: 14 additions & 13 deletions src/TwabController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ contract TwabController {
function getBalanceAt(
address vault,
address user,
uint32 periodEndOnOrAfterTime
uint256 periodEndOnOrAfterTime
) external view returns (uint256) {
TwabLib.Account storage _account = userObservations[vault][user];
return
Expand All @@ -275,7 +275,7 @@ contract TwabController {
*/
function getTotalSupplyAt(
address vault,
uint32 periodEndOnOrAfterTime
uint256 periodEndOnOrAfterTime
) external view returns (uint256) {
TwabLib.Account storage _account = totalSupplyObservations[vault];
return
Expand All @@ -300,8 +300,8 @@ contract TwabController {
function getTwabBetween(
address vault,
address user,
uint32 startTime,
uint32 endTime
uint256 startTime,
uint256 endTime
) external view returns (uint256) {
TwabLib.Account storage _account = userObservations[vault][user];
// We snap the timestamps to the period end on or after the timestamp because the total supply records will be sparsely populated.
Expand All @@ -327,8 +327,8 @@ contract TwabController {
*/
function getTotalSupplyTwabBetween(
address vault,
uint32 startTime,
uint32 endTime
uint256 startTime,
uint256 endTime
) external view returns (uint256) {
TwabLib.Account storage _account = totalSupplyObservations[vault];
// We snap the timestamps to the period end on or after the timestamp because the total supply records will be sparsely populated.
Expand All @@ -349,7 +349,7 @@ contract TwabController {
* @param _timestamp The timestamp to check
* @return The end timestamp of the period that ends on or immediately after the given timestamp
*/
function periodEndOnOrAfter(uint32 _timestamp) external view returns (uint32) {
function periodEndOnOrAfter(uint256 _timestamp) external view returns (uint256) {
return _periodEndOnOrAfter(_timestamp);
}

Expand All @@ -358,18 +358,19 @@ contract TwabController {
* @param _timestamp The timestamp to compute the period end time for
* @return A period end time.
*/
function _periodEndOnOrAfter(uint32 _timestamp) internal view returns (uint32) {
function _periodEndOnOrAfter(uint256 _timestamp) internal view returns (uint256) {
if (_timestamp < PERIOD_OFFSET) {
return PERIOD_OFFSET;
}
if ((_timestamp - PERIOD_OFFSET) % PERIOD_LENGTH == 0) {
return _timestamp;
return uint32(_timestamp);
}
uint256 period = TwabLib.getTimestampPeriod(PERIOD_LENGTH, PERIOD_OFFSET, _timestamp);
return
TwabLib.getPeriodEndTime(
PERIOD_LENGTH,
PERIOD_OFFSET,
TwabLib.getTimestampPeriod(PERIOD_LENGTH, PERIOD_OFFSET, _timestamp)
period
);
}

Expand Down Expand Up @@ -434,7 +435,7 @@ contract TwabController {
* @param time The timestamp to check
* @return period The period the timestamp falls into
*/
function getTimestampPeriod(uint32 time) external view returns (uint32) {
function getTimestampPeriod(uint256 time) external view returns (uint256) {
return TwabLib.getTimestampPeriod(PERIOD_LENGTH, PERIOD_OFFSET, time);
}

Expand All @@ -443,7 +444,7 @@ contract TwabController {
* @param time The timestamp to check
* @return True if the given time is finalized, false if it's during the current overwrite period.
*/
function hasFinalized(uint32 time) external view returns (bool) {
function hasFinalized(uint256 time) external view returns (bool) {
return TwabLib.hasFinalized(PERIOD_LENGTH, PERIOD_OFFSET, time);
}

Expand All @@ -452,7 +453,7 @@ contract TwabController {
* @dev The overwrite period is the period during which observations are collated.
* @return period The timestamp at which the current overwrite period started.
*/
function currentOverwritePeriodStartedAt() external view returns (uint32) {
function currentOverwritePeriodStartedAt() external view returns (uint256) {
return TwabLib.currentOverwritePeriodStartedAt(PERIOD_LENGTH, PERIOD_OFFSET);
}

Expand Down
18 changes: 4 additions & 14 deletions src/libraries/ObservationLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.19;

import "ring-buffer-lib/RingBufferLib.sol";

import "./OverflowSafeComparatorLib.sol";
// import "./OverflowSafeComparatorLib.sol";

/**
* @dev Sets max ring buffer length in the Account.observations Observation list.
Expand All @@ -22,8 +22,6 @@ uint16 constant MAX_CARDINALITY = 9600; // with min period of 1 hour, this allow
* @author PoolTogether Inc.
*/
library ObservationLib {
using OverflowSafeComparatorLib for uint32;

/**
* @notice Observation, which includes an amount and timestamp.
* @param cumulativeBalance the cumulative time-weighted balance at `timestamp`.
Expand All @@ -48,7 +46,6 @@ library ObservationLib {
* @param _oldestObservationIndex Index of the oldest Observation. Left side of the circular buffer.
* @param _target Timestamp at which we are searching the Observation.
* @param _cardinality Cardinality of the circular buffer we are searching through.
* @param _currentTime Current timestamp.
* @return beforeOrAt Observation recorded before, or at, the target.
* @return beforeOrAtIndex Index of observation recorded before, or at, the target.
* @return afterOrAt Observation recorded at, or after, the target.
Expand All @@ -59,8 +56,7 @@ library ObservationLib {
uint24 _newestObservationIndex,
uint24 _oldestObservationIndex,
uint32 _target,
uint16 _cardinality,
uint32 _currentTime
uint16 _cardinality
)
internal
view
Expand All @@ -86,19 +82,13 @@ library ObservationLib {
beforeOrAt = _observations[beforeOrAtIndex];
uint32 beforeOrAtTimestamp = beforeOrAt.timestamp;

// We've landed on an uninitialized timestamp, keep searching higher (more recently).
if (beforeOrAtTimestamp == 0) {
leftSide = uint16(RingBufferLib.nextIndex(leftSide, _cardinality));
continue;
}

afterOrAtIndex = uint16(RingBufferLib.nextIndex(currentIndex, _cardinality));
afterOrAt = _observations[afterOrAtIndex];

bool targetAfterOrAt = beforeOrAtTimestamp.lte(_target, _currentTime);
bool targetAfterOrAt = beforeOrAtTimestamp <= _target;

// Check if we've found the corresponding Observation.
if (targetAfterOrAt && _target.lte(afterOrAt.timestamp, _currentTime)) {
if (targetAfterOrAt && _target <= afterOrAt.timestamp) {
break;
}

Expand Down
57 changes: 0 additions & 57 deletions src/libraries/OverflowSafeComparatorLib.sol

This file was deleted.

Loading

0 comments on commit 2f9d736

Please sign in to comment.