Skip to content

Commit

Permalink
refinements to test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
jordaniza committed Oct 25, 2024
1 parent 37c8de6 commit d684168
Show file tree
Hide file tree
Showing 9 changed files with 971 additions and 775 deletions.
28 changes: 24 additions & 4 deletions TOTAL_SUPPLY.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,30 @@ Getting ready for live

Minimum

time wise - priotise for now

1. change the token checkpoint to only take in a lock - done
2. add the remaining test functions to ensure we've covered ourselves
- Token unit - todo tonight
- elapsed thingy
- Warmup: big test -done
- Voting Power: this can be done in the big test as well done
3. Test the revert conditions with a second big test
4. check the manual checkpoint works as expected

Then optimise can come tomorrow

1. Fix all the tests
2. Add the binary search for total supply
- decide on implementation - leaving for now
- unit test some of the other functions - done
- adjust the token tests
2. Add the binary search for total supply - done
3. Run the regression test and adjust the escrow contract
4. Fix the bug with the warmup
5. Check all the reverts and fix the revert conditions
6. Test the manual checkpoint
4. Fix the bug with the warmup - needs testing
5. Check all the revert - done
6. fix the revert conditions
7. Test the manual checkpoint
8. Review all the comments

Optimise

Expand All @@ -125,3 +143,5 @@ Document

1. Visual Doc
2. README

Qus: New locked start passed to the update - what if that changes?
352 changes: 174 additions & 178 deletions src/escrow/increasing/LinearIncreasingEscrow.sol

Large diffs are not rendered by default.

65 changes: 49 additions & 16 deletions test/escrow/curve/linear/LinearApplyTokenToGlobal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ contract TestLinearIncreasingApplyTokenChange is LinearCurveBase {
return _old;
}

function testRevertsIfPointsArentInSync() public {
vm.warp(0);
TokenPoint memory oldPoint;
TokenPoint memory newPoint;
GlobalPoint memory globalPoint;
newPoint.checkpointTs = 1;

vm.expectRevert(TokenPointNotUpToDate.selector);
curve.applyTokenUpdateToGlobal(0, oldPoint, newPoint, globalPoint);

vm.warp(newPoint.checkpointTs);

vm.expectRevert(GlobalPointNotUpToDate.selector);
curve.applyTokenUpdateToGlobal(0, oldPoint, newPoint, globalPoint);
}

// when run on an empty global point, adds the user's own point
// deposit
function testEmptyGlobalDeposit() public {
Expand All @@ -32,9 +48,11 @@ contract TestLinearIncreasingApplyTokenChange is LinearCurveBase {

TokenPoint memory newPoint = curve.previewPoint(10 ether);
newPoint.checkpointTs = uint128(block.timestamp);
// write it

globalPoint = curve.applyTokenUpdateToGlobal(0, oldPoint, newPoint, globalPoint);
// assume the lock starts immediately
uint48 lockStart = uint48(block.timestamp);

globalPoint = curve.applyTokenUpdateToGlobal(lockStart, oldPoint, newPoint, globalPoint);

uint expectedBias = 10 ether;

Expand All @@ -53,7 +71,10 @@ contract TestLinearIncreasingApplyTokenChange is LinearCurveBase {

TokenPoint memory newPoint0 = curve.previewPoint(10 ether);
newPoint0.checkpointTs = uint128(block.timestamp);
globalPoint = curve.applyTokenUpdateToGlobal(0, oldPoint, newPoint0, globalPoint);
// assume the lock starts immediately
uint48 lockStart = uint48(block.timestamp);

globalPoint = curve.applyTokenUpdateToGlobal(lockStart, oldPoint, newPoint0, globalPoint);

// copy the new to old point and redefine the new point
oldPoint = _copyNewToOld(newPoint0, oldPoint);
Expand All @@ -62,7 +83,7 @@ contract TestLinearIncreasingApplyTokenChange is LinearCurveBase {
newPoint1.checkpointTs = uint128(block.timestamp);

GlobalPoint memory newGlobalPoint = curve.applyTokenUpdateToGlobal(
0,
lockStart,
oldPoint,
newPoint1,
globalPoint
Expand All @@ -86,13 +107,15 @@ contract TestLinearIncreasingApplyTokenChange is LinearCurveBase {
globalPoint.coefficients[1] = curve.previewPoint(100 ether).coefficients[1];

TokenPoint memory oldPoint; // 0

TokenPoint memory newPoint = curve.previewPoint(10 ether);
newPoint.checkpointTs = uint128(block.timestamp);

// again this is the first lock
uint48 lockStart = uint48(block.timestamp);

int cachedSlope = globalPoint.coefficients[1];

globalPoint = curve.applyTokenUpdateToGlobal(0, oldPoint, newPoint, globalPoint);
globalPoint = curve.applyTokenUpdateToGlobal(lockStart, oldPoint, newPoint, globalPoint);

// expectation: bias && slope incremented
assertEq(uint(globalPoint.coefficients[0]) / 1e18, 110 ether);
Expand All @@ -101,43 +124,53 @@ contract TestLinearIncreasingApplyTokenChange is LinearCurveBase {
}

// change - elapsed
// test that if we have existing state and some time elapses
// the change is correctly applied
function testChangeOnExistingGlobalStateElapsedTime() public {
vm.warp(100);

// imagine the state is set with 100 ether total
GlobalPoint memory globalPoint;
globalPoint.ts = block.timestamp;

// imagine the state is set with 100 ether total
globalPoint.coefficients[0] = curve.previewPoint(100 ether).coefficients[0];
globalPoint.coefficients[1] = curve.previewPoint(100 ether).coefficients[1];

// no existing point
TokenPoint memory oldPoint; // 0

// user makes a deposit at the same time as global for 10 eth
TokenPoint memory newPoint0 = curve.previewPoint(10 ether);
newPoint0.checkpointTs = uint128(block.timestamp);

globalPoint = curve.applyTokenUpdateToGlobal(0, oldPoint, newPoint0, globalPoint);
int cachedCoeff0 = globalPoint.coefficients[0];
uint48 lockStart = uint48(block.timestamp);

// apply the deposit of the user to the state
globalPoint = curve.applyTokenUpdateToGlobal(lockStart, oldPoint, newPoint0, globalPoint);

// should be a global state of 110 eth
assertEq(uint(globalPoint.coefficients[0]) / 1e18, 110 ether);

// copy the new to old point and redefine the new point
oldPoint = _copyNewToOld(newPoint0, oldPoint);

// warp into the future, we're gonna write a new point over the top
// representing a change in the deposit
vm.warp(200);

TokenPoint memory newPoint1 = curve.previewPoint(20 ether);
newPoint1.checkpointTs = uint128(block.timestamp);

// define a new global point by evaluating it over the elapsed time
// our existing global point should have accrued 110 ether's worth of bias for 100 seconds
globalPoint.coefficients[0] = curve.getBiasUnbound(100, globalPoint.coefficients);
globalPoint.ts = block.timestamp;

// an entirely fresh, new point is written which should overrwrite the old
TokenPoint memory newPoint1 = curve.previewPoint(20 ether);
newPoint1.checkpointTs = uint128(block.timestamp);

GlobalPoint memory newGlobalPoint = curve.applyTokenUpdateToGlobal(
0,
lockStart,
oldPoint,
newPoint1,
globalPoint
);

// we would now expect that the new global point is:
// 110 ether evaled over 100 seconds - (10 ether evaled over 100 seconds) + (20 ether)
uint expectedCoeff0 = curve.getBias(100, 110 ether) -
Expand Down
29 changes: 23 additions & 6 deletions test/escrow/curve/linear/LinearBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma solidity ^0.8.17;

import {console2 as console} from "forge-std/console2.sol";
import {TestHelpers} from "@helpers/TestHelpers.sol";
import {console2 as console} from "forge-std/console2.sol";
import {DaoUnauthorized} from "@aragon/osx/core/utils/auth.sol";

import {IDAO} from "@aragon/osx/core/dao/IDAO.sol";
Expand All @@ -20,6 +19,12 @@ contract MockEscrow {
address public token;
LinearIncreasingEscrow public curve;

mapping(uint256 => IVotingEscrow.LockedBalance) internal _locked;

function setLockedBalance(uint256 _tokenId, IVotingEscrow.LockedBalance memory lock) external {
_locked[_tokenId] = lock;
}

function setCurve(LinearIncreasingEscrow _curve) external {
curve = _curve;
}
Expand All @@ -31,16 +36,19 @@ contract MockEscrow {
) external {
return curve.checkpoint(_tokenId, _oldLocked, _newLocked);
}

function locked(uint256 _tokenId) external view returns (IVotingEscrow.LockedBalance memory) {
return _locked[_tokenId];
}
}

/// @dev expose internal functions for testing
contract MockLinearIncreasingEscrow is LinearIncreasingEscrow {
function tokenCheckpoint(
uint256 _tokenId,
IVotingEscrow.LockedBalance memory _oldLocked,
IVotingEscrow.LockedBalance memory _newLocked
) public returns (TokenPoint memory, TokenPoint memory) {
return _tokenCheckpoint(_tokenId, _oldLocked, _newLocked);
return _tokenCheckpoint(_tokenId, _newLocked);
}

function scheduleCurveChanges(
Expand All @@ -65,7 +73,16 @@ contract MockLinearIncreasingEscrow is LinearIncreasingEscrow {
_pointHistory[_index] = _latestPoint;
}

function getLatestGlobalPointOrWriteFirstPoint() external returns (GlobalPoint memory) {
function writeNewTokenPoint(
uint256 _tokenId,
TokenPoint memory _point,
uint _interval
) external {
tokenPointIntervals[_tokenId] = _interval;
_tokenPointHistory[_tokenId][_interval] = _point;
}

function getLatestGlobalPointOrWriteFirstPoint() external returns (GlobalPoint memory, uint) {
return _getLatestGlobalPointOrWriteFirstPoint();
}

Expand Down Expand Up @@ -94,11 +111,11 @@ contract MockLinearIncreasingEscrow is LinearIncreasingEscrow {
function getBiasUnbound(
uint elapsed,
int[3] memory coefficients
) external view returns (int256) {
) external pure returns (int256) {
return _getBiasUnbound(elapsed, coefficients);
}

function getBiasUnbound(uint elapsed, uint amount) external view returns (int256) {
function getBiasUnbound(uint elapsed, uint amount) external pure returns (int256) {
return _getBiasUnbound(elapsed, _getCoefficients(amount));
}

Expand Down
Loading

0 comments on commit d684168

Please sign in to comment.