Skip to content

Commit b8407be

Browse files
authored
Merge pull request #231 from ampleforth/median-oracle-replace-expired-report-before-delay
Median oracle replace expired report before delay
2 parents 0de7c94 + b180e75 commit b8407be

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 Ownable, 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 = now.sub(reportExpirationTimeSec);
118+
uint256 maxValidTimestamp = now.sub(reportDelaySec);
119+
116120
// Check that the push is not too soon after the last one.
117-
require(timestamps[index_recent].add(reportDelaySec) <= now);
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 = now;
120128
reports[index_past].payload = payload;
@@ -127,6 +135,7 @@ contract MedianOracle is Ownable, 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
@@ -91,11 +91,55 @@ describe('MedianOracle:pushReport', async function () {
9191
it('should only push from authorized source', async function () {
9292
await expect(oracle.connect(A).pushReport(payload)).to.be.reverted
9393
})
94-
it('should fail if reportDelaySec did not pass since the previous push', async function () {
94+
95+
it('should pass if reportDelaySec did not pass since the previous push, but previous report non-existent', async function () {
96+
await oracle.addProvider(await A.getAddress())
97+
await oracle.connect(A).pushReport(payload)
98+
await oracle.connect(A).pushReport(payload)
99+
})
100+
101+
it('should pass if reportDelaySec did pass since the previous push, and previous report non-existent', async function () {
95102
await oracle.addProvider(await A.getAddress())
96103
await oracle.connect(A).pushReport(payload)
104+
await increaseTime(20)
105+
await oracle.connect(A).pushReport(payload)
106+
})
107+
108+
it('should pass if reportDelaySec did pass since the previous push, but previous report expired', async function () {
109+
await oracle.addProvider(await A.getAddress())
110+
await oracle.connect(A).pushReport(payload)
111+
await increaseTime(70)
112+
await oracle.connect(A).pushReport(payload)
113+
await oracle.connect(A).pushReport(payload)
114+
})
115+
116+
it('should pass if reportDelaySec did pass since the previous push, and previous report expired', async function () {
117+
await oracle.addProvider(await A.getAddress())
118+
await oracle.connect(A).pushReport(payload)
119+
await increaseTime(70)
120+
await oracle.connect(A).pushReport(payload)
121+
await increaseTime(20)
122+
await oracle.connect(A).pushReport(payload)
123+
})
124+
125+
it('should fail if reportDelaySec did not pass since the previous push, and previous report not expired', async function () {
126+
await oracle.addProvider(await A.getAddress())
127+
await oracle.connect(A).pushReport(payload)
128+
await increaseTime(20)
129+
await oracle.connect(A).pushReport(payload)
130+
await expect(oracle.connect(A).pushReport(payload)).to.be.reverted
131+
})
132+
133+
it('should pass if reportDelaySec did pass since the previous push, and previous report not expired', async function () {
134+
await oracle.addProvider(await A.getAddress())
135+
await oracle.connect(A).pushReport(payload)
136+
await increaseTime(20)
137+
await oracle.connect(A).pushReport(payload)
138+
await increaseTime(20)
139+
await oracle.connect(A).pushReport(payload)
97140
await expect(oracle.connect(A).pushReport(payload)).to.be.reverted
98141
})
142+
99143
it('should emit ProviderReportPushed message', async function () {
100144
oracle.addProvider(await A.getAddress())
101145
const tx = await oracle.connect(A).pushReport(payload)

0 commit comments

Comments
 (0)