Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt round lookup in case of empty history ringbuffer #21

Merged
merged 1 commit into from
Mar 10, 2023
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
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);
}
});
})
})
});