Skip to content

Commit

Permalink
Adapt round lookup in case of empty history ringbuffer (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
sponomarev authored Mar 10, 2023
1 parent c4c0c21 commit e7b8f47
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/libraries/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ library Utils {
{
uint32 liveStartRoundId = saturatingSub(latestRoundId, liveLength - 1);
uint32 historicalEndRoundId = latestRoundId - (latestRoundId % granularity);
uint32 historicalStartRoundId = saturatingSub(historicalEndRoundId, granularity * (historicalLength - 1));
uint32 historicalStartRoundId = saturatingSub(historicalEndRoundId, granularity * saturatingSub(historicalLength, 1));

// If withing the live range, fetch from it. Otherwise, fetch from the closest previous in history.
if (roundId >= liveStartRoundId && roundId <= latestRoundId) {
Expand Down
28 changes: 28 additions & 0 deletions test/libraries/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,33 @@ contract("Utils", () => {
assert.equal(position, 7);
});
})

describe('when historical data does not present', async () => {
const liveCursor = 0
const liveLength = 1
const latestRoundId = 38
const historicalCursor = 0
const historicalLength = 0
const granularity = 1

it('reverts', async () => {
try {
await utils.locateRound(
42,
liveCursor,
liveLength,
latestRoundId,
historicalCursor,
historicalLength,
granularity
);
throw null;
}
catch (error) {
assert(error, "Expected an error but did not get one");
assert(error.message.endsWith("No data present"), "Expected no data present error, but received " + error);
}
});
})
})
});

0 comments on commit e7b8f47

Please sign in to comment.