Skip to content

Commit 1bfac42

Browse files
authored
Oracle v2 dev (#256)
* allows report to be pushed if past is expired (naguib's original change) * tests median oracle push report for all expiration/delay combinations * adds one more push report test to oracle * Apply suggestions from code review
1 parent 0b9b489 commit 1bfac42

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

contracts/MedianOracle.sol

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,21 @@ contract MedianOracle is OwnableUpgradeable, IOracle {
108108
Report[2] storage reports = providerReports[providerAddress];
109109
uint256[2] memory timestamps = [reports[0].timestamp, reports[1].timestamp];
110110

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

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

117+
uint256 minValidTimestamp = block.timestamp - reportExpirationTimeSec;
118+
uint256 maxValidTimestamp = block.timestamp - reportDelaySec;
119+
116120
// Check that the push is not too soon after the last one.
117-
require(timestamps[index_recent] + reportDelaySec <= block.timestamp);
121+
// unless past one is already expired
122+
require(
123+
timestamps[index_past] < minValidTimestamp ||
124+
timestamps[index_recent] <= maxValidTimestamp
125+
);
118126

119127
reports[index_past].timestamp = block.timestamp;
120128
reports[index_past].payload = payload;
@@ -127,6 +135,7 @@ contract MedianOracle is OwnableUpgradeable, IOracle {
127135
*/
128136
function purgeReports() external {
129137
address providerAddress = msg.sender;
138+
// Check that this providerAddress is already whitelisted
130139
require(providerReports[providerAddress][0].timestamp > 0);
131140
providerReports[providerAddress][0].timestamp = 1;
132141
providerReports[providerAddress][1].timestamp = 1;

test/unit/MedianOracle.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,55 @@ describe('MedianOracle:pushReport', async function () {
9292
it('should only push from authorized source', async function () {
9393
await expect(oracle.connect(A).pushReport(payload)).to.be.reverted
9494
})
95-
it('should fail if reportDelaySec did not pass since the previous push', async function () {
95+
96+
it('should pass if reportDelaySec did not pass since the previous push, but previous report non-existent', async function () {
97+
await oracle.addProvider(await A.getAddress())
98+
await oracle.connect(A).pushReport(payload)
99+
await oracle.connect(A).pushReport(payload)
100+
})
101+
102+
it('should pass if reportDelaySec did pass since the previous push, and previous report non-existent', async function () {
96103
await oracle.addProvider(await A.getAddress())
97104
await oracle.connect(A).pushReport(payload)
105+
await increaseTime(20)
106+
await oracle.connect(A).pushReport(payload)
107+
})
108+
109+
it('should pass if reportDelaySec did pass since the previous push, but previous report expired', async function () {
110+
await oracle.addProvider(await A.getAddress())
111+
await oracle.connect(A).pushReport(payload)
112+
await increaseTime(70)
113+
await oracle.connect(A).pushReport(payload)
114+
await oracle.connect(A).pushReport(payload)
115+
})
116+
117+
it('should pass if reportDelaySec did pass since the previous push, and previous report expired', async function () {
118+
await oracle.addProvider(await A.getAddress())
119+
await oracle.connect(A).pushReport(payload)
120+
await increaseTime(70)
121+
await oracle.connect(A).pushReport(payload)
122+
await increaseTime(20)
123+
await oracle.connect(A).pushReport(payload)
124+
})
125+
126+
it('should fail if reportDelaySec did not pass since the previous push, and previous report not expired', async function () {
127+
await oracle.addProvider(await A.getAddress())
128+
await oracle.connect(A).pushReport(payload)
129+
await increaseTime(20)
130+
await oracle.connect(A).pushReport(payload)
131+
await expect(oracle.connect(A).pushReport(payload)).to.be.reverted
132+
})
133+
134+
it('should pass if reportDelaySec did pass since the previous push, and previous report not expired', async function () {
135+
await oracle.addProvider(await A.getAddress())
136+
await oracle.connect(A).pushReport(payload)
137+
await increaseTime(20)
138+
await oracle.connect(A).pushReport(payload)
139+
await increaseTime(20)
140+
await oracle.connect(A).pushReport(payload)
98141
await expect(oracle.connect(A).pushReport(payload)).to.be.reverted
99142
})
143+
100144
it('should emit ProviderReportPushed message', async function () {
101145
oracle.addProvider(await A.getAddress())
102146
const tx = await oracle.connect(A).pushReport(payload)

0 commit comments

Comments
 (0)