Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix lint
  • Loading branch information
Amxx committed Feb 20, 2024
commit b56796fc7409eed573dd9bd4ecdd04cddfa93f5b
9 changes: 5 additions & 4 deletions contracts/utils/structs/CircularBuffer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ library CircularBuffer {
function last(Bytes32CircularBuffer storage self, uint256 i) internal view returns (bytes32) {
uint256 index = self._count;
uint256 length = self._data.length;
if (index <= i || length <= i) {
uint256 total = Math.min(index, length); // count(self)
if (i >= total) {
Panic.panic(Panic.ARRAY_OUT_OF_BOUNDS);
}
return Arrays.unsafeAccess(self._data, (index - i - 1) % self._data.length).value;
Expand All @@ -101,10 +102,10 @@ library CircularBuffer {
*/
function includes(Bytes32CircularBuffer storage self, bytes32 value) internal view returns (bool) {
uint256 index = self._count;
uint256 total = count(self);
uint256 length = self._data.length;
for (uint256 i = 1; i <= total; ++i) {
if (Arrays.unsafeAccess(self._data, (index - i) % length).value == value) {
uint256 total = Math.min(index, length); // count(self)
for (uint256 i = 0; i < total; ++i) {
if (Arrays.unsafeAccess(self._data, (index - i - 1) % length).value == value) {
return true;
}
}
Expand Down
5 changes: 3 additions & 2 deletions test/utils/structs/CircularBuffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ describe('CircularBuffer', function () {
for (const j in stored) {
expect(await this.mock.$last(0, j)).to.equal(stored.at(-j - 1));
}
await expect(this.mock.$last(0, stored.length + 1)).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
await expect(this.mock.$last(0, stored.length + 1)).to.be.revertedWithPanic(
PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS,
);

// check included and non-included values
for (const v of stored) {
Expand All @@ -56,7 +58,6 @@ describe('CircularBuffer', function () {
}
expect(await this.mock.$includes(0, ethers.ZeroHash)).to.be.false;
}

});

it('clear', async function () {
Expand Down