Skip to content

Commit

Permalink
test: round id in unlock tests
Browse files Browse the repository at this point in the history
Signed-off-by: Reinis Martinsons <[email protected]>
  • Loading branch information
Reinis-FRP committed May 13, 2024
1 parent 4029077 commit 540c3fe
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions test/unit/Oval.UnlockLatestValue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ contract OvalUnlockLatestValue is CommonTest {

TestOval oval;

uint256 latestPublishedRound;

function setUp() public {
vm.warp(initialTimestamp);

Expand All @@ -28,12 +30,18 @@ contract OvalUnlockLatestValue is CommonTest {
oval.setUnlocker(permissionedUnlocker, true);
vm.stopPrank();

oval.publishRoundData(initialPrice, initialTimestamp);
publishRoundData(initialPrice, initialTimestamp);
}

function publishRoundData(int256 answer, uint256 timestamp) public {
oval.publishRoundData(answer, timestamp);
++latestPublishedRound;
}

function verifyOvalMatchesOval() public {
(int256 latestAnswer, uint256 latestTimestamp,) = oval.internalLatestData();
(int256 latestAnswer, uint256 latestTimestamp, uint256 latestRoundId) = oval.internalLatestData();
assertTrue(latestAnswer == oval.latestAnswer() && latestTimestamp == oval.latestTimestamp());
assertTrue(latestRoundId == latestPublishedRound);
}

function syncOvalWithOval() public {
Expand Down Expand Up @@ -61,7 +69,7 @@ contract OvalUnlockLatestValue is CommonTest {
function testUnlockerCanUnlockLatestValue() public {
syncOvalWithOval();

oval.publishRoundData(newAnswer, newTimestamp);
publishRoundData(newAnswer, newTimestamp);
vm.warp(newTimestamp);

vm.prank(permissionedUnlocker);
Expand All @@ -72,7 +80,7 @@ contract OvalUnlockLatestValue is CommonTest {

// Advance time. Add a diff to the source adapter and verify that it is applied.
vm.warp(newTimestamp + 2);
oval.publishRoundData(newAnswer + 1, newTimestamp + 2);
publishRoundData(newAnswer + 1, newTimestamp + 2);
vm.prank(permissionedUnlocker);
oval.unlockLatestValue();
verifyOvalMatchesOval();
Expand All @@ -81,15 +89,16 @@ contract OvalUnlockLatestValue is CommonTest {
function testNonUnlockerCannotUnlockLatestValue() public {
syncOvalWithOval();

oval.publishRoundData(newAnswer, newTimestamp);
publishRoundData(newAnswer, newTimestamp);
vm.warp(newTimestamp);

vm.expectRevert("Controller blocked: canUnlock");
vm.prank(random);
oval.unlockLatestValue();

(int256 latestAnswer, uint256 latestTimestamp,) = oval.internalLatestData();
(int256 latestAnswer, uint256 latestTimestamp, uint256 latestRoundId) = oval.internalLatestData();
assertTrue(latestAnswer == initialPrice && latestTimestamp == initialTimestamp);
assertTrue(latestRoundId == latestPublishedRound - 1);
}

function testUpdatesWithinLockWindow() public {
Expand All @@ -98,11 +107,12 @@ contract OvalUnlockLatestValue is CommonTest {
// Advance time to within the lock window and update the source.
uint256 beforeLockWindow = block.timestamp + oval.lockWindow() - 1;
vm.warp(beforeLockWindow);
oval.publishRoundData(newAnswer, beforeLockWindow);
publishRoundData(newAnswer, beforeLockWindow);

// Before updating, initial values from cache would be returned.
(int256 latestAnswer, uint256 latestTimestamp,) = oval.internalLatestData();
(int256 latestAnswer, uint256 latestTimestamp, uint256 latestRoundId) = oval.internalLatestData();
assertTrue(latestAnswer == initialPrice && latestTimestamp == initialTimestamp);
assertTrue(latestRoundId == latestPublishedRound - 1);

// After updating we should return the new values.
vm.prank(permissionedUnlocker);
Expand All @@ -116,18 +126,20 @@ contract OvalUnlockLatestValue is CommonTest {

uint256 beforeOEVLockWindow = unlockTimestamp + 59; // Default lock window is 10 minutes.
vm.warp(beforeOEVLockWindow); // Advance before the end of the lock window.
oval.publishRoundData(newAnswer, beforeOEVLockWindow); // Update the source.
publishRoundData(newAnswer, beforeOEVLockWindow); // Update the source.

// Within original lock window (after OEV unlock), initial values from cache would be returned.
(int256 latestAnswer, uint256 latestTimestamp,) = oval.internalLatestData();
(int256 latestAnswer, uint256 latestTimestamp, uint256 latestRoundId) = oval.internalLatestData();
assertTrue(latestAnswer == initialPrice && latestTimestamp == initialTimestamp, "1");
assertTrue(latestRoundId == latestPublishedRound - 1);

// Advancing time past the original lock window but before new lock window since source update
// should not yet pass through source values.
uint256 pastOEVLockWindow = beforeOEVLockWindow + 2;
vm.warp(pastOEVLockWindow);
(latestAnswer, latestTimestamp,) = oval.internalLatestData();
(latestAnswer, latestTimestamp, latestRoundId) = oval.internalLatestData();
assertTrue(latestAnswer == initialPrice && latestTimestamp == initialTimestamp);
assertTrue(latestRoundId == latestPublishedRound - 1);

// Advancing time past the new lock window should pass through source values.
uint256 pastSourceLockWindow = beforeOEVLockWindow + 69;
Expand All @@ -141,11 +153,12 @@ contract OvalUnlockLatestValue is CommonTest {
// Advance time to within the lock window and update the source.
uint256 beforeLockWindow = block.timestamp + oval.lockWindow() - 1;
vm.warp(beforeLockWindow);
oval.publishRoundData(newAnswer, beforeLockWindow);
publishRoundData(newAnswer, beforeLockWindow);

// Before updating, initial values from cache would be returned.
(int256 latestAnswer, uint256 latestTimestamp,) = oval.internalLatestData();
(int256 latestAnswer, uint256 latestTimestamp, uint256 latestRoundId) = oval.internalLatestData();
assertTrue(latestAnswer == initialPrice && latestTimestamp == initialTimestamp);
assertTrue(latestRoundId == latestPublishedRound - 1);

// Sync and verify updated values.
syncOvalWithOval();
Expand All @@ -154,10 +167,11 @@ contract OvalUnlockLatestValue is CommonTest {
uint256 nextBeforeLockWindow = block.timestamp + oval.lockWindow() - 1;
vm.warp(nextBeforeLockWindow);
int256 nextNewAnswer = newAnswer + 1e18;
oval.publishRoundData(nextNewAnswer, nextBeforeLockWindow);
publishRoundData(nextNewAnswer, nextBeforeLockWindow);

// Within lock window, values from previous update would be returned.
(latestAnswer, latestTimestamp,) = oval.internalLatestData();
(latestAnswer, latestTimestamp, latestRoundId) = oval.internalLatestData();
assertTrue(latestAnswer == newAnswer && latestTimestamp == beforeLockWindow);
assertTrue(latestRoundId == latestPublishedRound - 1);
}
}

0 comments on commit 540c3fe

Please sign in to comment.