Skip to content

Commit

Permalink
added maxAge tests
Browse files Browse the repository at this point in the history
Signed-off-by: chrismaree <[email protected]>
  • Loading branch information
chrismaree committed May 20, 2024
1 parent 60db5cb commit 5d8409b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/adapters/source-adapters/SnapshotSource.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract contract SnapshotSource is DiamondRootOval {
Snapshot memory historicalData = _searchSnapshotAt(timestamp, maxTraversal);

// Validate returned data. If it is uninitialized or too old we fallback to returning the current latest round data.
if (historicalData.timestamp > block.timestamp - maxAge()) return historicalData;
if (historicalData.timestamp >= block.timestamp - maxAge()) return historicalData;
return latestData;
}

Expand Down
27 changes: 27 additions & 0 deletions test/fork/adapters/ChainlinkSourceAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,33 @@ contract ChainlinkSourceAdapterTest is CommonTest {
assertTrue(uint256(roundId) == lookBackRoundId);
}

function testCorrectlyBoundsMaxLooBackByMaxAge() public {
// Value returned at 2 days should be the same as the value returned at 1 day as the max age is 1 day.
assertTrue(sourceAdapter.maxAge() == 1 days);
(int256 lookBackPricePastWindow, uint256 lookBackTimestampPastWindow, uint256 lookBackRoundIdPastWindow) =
sourceAdapter.tryLatestDataAt(block.timestamp - 2 days, 50);

(int256 lookBackPriceAtLimit, uint256 lookBackTimestampAtLimit, uint256 lookBackRoundIdAtLimit) =
sourceAdapter.tryLatestDataAt(block.timestamp - 1 days, 50);

assertTrue(lookBackPricePastWindow == lookBackPriceAtLimit);
assertTrue(lookBackTimestampPastWindow == lookBackTimestampAtLimit);
assertTrue(lookBackRoundIdPastWindow == lookBackRoundIdAtLimit);
}

function testExtendingMaxAgeCorrectlyExtendsWindowOfReturnedValue() public {
sourceAdapter.setMaxAge(2 days);
(int256 lookBackPricePastWindow, uint256 lookBackTimestampPastWindow, uint256 lookBackRoundIdPastWindow) =
sourceAdapter.tryLatestDataAt(block.timestamp - 3 days, 50);

(int256 lookBackPriceAtLimit, uint256 lookBackTimestampAtLimit, uint256 lookBackRoundIdAtLimit) =
sourceAdapter.tryLatestDataAt(block.timestamp - 2 days, 50);

assertTrue(lookBackPricePastWindow == lookBackPriceAtLimit);
assertTrue(lookBackTimestampPastWindow == lookBackTimestampAtLimit);
assertTrue(lookBackRoundIdPastWindow == lookBackRoundIdAtLimit);
}

function testNonHistoricalData() public {
uint256 targetTime = block.timestamp - 1 hours;

Expand Down
38 changes: 38 additions & 0 deletions test/unit/SnapshotSource.SnapshotData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {CommonTest} from "../Common.sol";
import {MockSnapshotSourceAdapter} from "../mocks/MockSnapshotSourceAdapter.sol";
import {Oval} from "../../src/Oval.sol";
import {BaseController} from "../../src/controllers/BaseController.sol";
import "forge-std/console.sol";

contract TestSnapshotSource is MockSnapshotSourceAdapter, Oval, BaseController {}

Expand Down Expand Up @@ -74,4 +75,41 @@ contract SnapshotSourceSnapshotDataTest is CommonTest {
snapshot = snapshotSource.latestSnapshotData();
assertTrue(snapshot.answer == 100 && snapshot.timestamp == 1000);
}

function testMaxAgeIsRespected() public {
// Set maxAge to 2000 for testing
snapshotSource.setMaxAge(2000);

// Publish data at different timestamps
vm.warp(1000);
snapshotSource.publishSourceData(100, 1000);
snapshotSource.snapshotData();

vm.warp(2000);
snapshotSource.publishSourceData(200, 2000);
snapshotSource.snapshotData();

vm.warp(3000);
snapshotSource.publishSourceData(300, 3000);
snapshotSource.snapshotData();

vm.warp(4000);
snapshotSource.publishSourceData(400, 4000);
snapshotSource.snapshotData();

// Verify behavior when requesting data within the maxAge limit
(int256 answerAt4000, uint256 timestampAt4000,) = snapshotSource.tryLatestDataAt(4000, 10);
assertTrue(answerAt4000 == 400 && timestampAt4000 == 4000);

(int256 answerAt3000, uint256 timestampAt3000,) = snapshotSource.tryLatestDataAt(3000, 10);
assertTrue(answerAt3000 == 300 && timestampAt3000 == 3000);

// Request data at the limit of maxAge should still work.
(int256 answerAt2000, uint256 timestampAt2000,) = snapshotSource.tryLatestDataAt(2000, 10);
assertTrue(answerAt2000 == 200 && timestampAt2000 == 2000);

// Request data older than maxAge (1000), should get the latest available data at 4000.
(int256 answerAt1000, uint256 timestampAt1000,) = snapshotSource.tryLatestDataAt(1000, 10);
assertTrue(answerAt1000 == 400 && timestampAt1000 == 4000);
}
}

0 comments on commit 5d8409b

Please sign in to comment.