Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion contracts/MedianOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,21 @@ contract MedianOracle is Ownable, IOracle {
Report[2] storage reports = providerReports[providerAddress];
uint256[2] memory timestamps = [reports[0].timestamp, reports[1].timestamp];

// Checks that this providerAddress is already whitelisted
require(timestamps[0] > 0);

uint8 index_recent = timestamps[0] >= timestamps[1] ? 0 : 1;
uint8 index_past = 1 - index_recent;

uint256 minValidTimestamp = now.sub(reportExpirationTimeSec);
uint256 maxValidTimestamp = now.sub(reportDelaySec);

// Check that the push is not too soon after the last one.
require(timestamps[index_recent].add(reportDelaySec) <= now);
// unless past one is already expired
require(
timestamps[index_past] < minValidTimestamp ||
timestamps[index_recent] <= maxValidTimestamp
);

reports[index_past].timestamp = now;
reports[index_past].payload = payload;
Expand All @@ -127,6 +135,7 @@ contract MedianOracle is Ownable, IOracle {
*/
function purgeReports() external {
address providerAddress = msg.sender;
// Check that this providerAddress is already whitelisted
require(providerReports[providerAddress][0].timestamp > 0);
providerReports[providerAddress][0].timestamp = 1;
providerReports[providerAddress][1].timestamp = 1;
Expand Down
46 changes: 45 additions & 1 deletion test/unit/MedianOracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,55 @@ describe('MedianOracle:pushReport', async function () {
it('should only push from authorized source', async function () {
await expect(oracle.connect(A).pushReport(payload)).to.be.reverted
})
it('should fail if reportDelaySec did not pass since the previous push', async function () {

it('should pass if reportDelaySec did not pass since the previous push, but previous report non-existent', async function () {
await oracle.addProvider(await A.getAddress())
await oracle.connect(A).pushReport(payload)
await oracle.connect(A).pushReport(payload)
})

it('should pass if reportDelaySec did pass since the previous push, and previous report non-existent', async function () {
await oracle.addProvider(await A.getAddress())
await oracle.connect(A).pushReport(payload)
await increaseTime(20)
await oracle.connect(A).pushReport(payload)
})

it('should pass if reportDelaySec did pass since the previous push, but previous report expired', async function () {
await oracle.addProvider(await A.getAddress())
await oracle.connect(A).pushReport(payload)
await increaseTime(70)
await oracle.connect(A).pushReport(payload)
await oracle.connect(A).pushReport(payload)
})

it('should pass if reportDelaySec did pass since the previous push, and previous report expired', async function () {
await oracle.addProvider(await A.getAddress())
await oracle.connect(A).pushReport(payload)
await increaseTime(70)
await oracle.connect(A).pushReport(payload)
await increaseTime(20)
await oracle.connect(A).pushReport(payload)
})

it('should fail if reportDelaySec did not pass since the previous push, and previous report not expired', async function () {
await oracle.addProvider(await A.getAddress())
await oracle.connect(A).pushReport(payload)
await increaseTime(20)
await oracle.connect(A).pushReport(payload)
await expect(oracle.connect(A).pushReport(payload)).to.be.reverted
})

it('should pass if reportDelaySec did pass since the previous push, and previous report not expired', async function () {
await oracle.addProvider(await A.getAddress())
await oracle.connect(A).pushReport(payload)
await increaseTime(20)
await oracle.connect(A).pushReport(payload)
await increaseTime(20)
await oracle.connect(A).pushReport(payload)
await expect(oracle.connect(A).pushReport(payload)).to.be.reverted
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check that one more push here should fail? Since once is too new and the other one is in the valid time range? Otherwise LGTM thanks @nms-7

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added


it('should emit ProviderReportPushed message', async function () {
oracle.addProvider(await A.getAddress())
const tx = await oracle.connect(A).pushReport(payload)
Expand Down