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
rename internal vars to avoid name conflicts
  • Loading branch information
Amxx committed Mar 25, 2024
commit 72a37944a07984022d42db973c4d00cc8ff59341
20 changes: 10 additions & 10 deletions contracts/utils/structs/CircularBuffer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ library CircularBuffer {
*
* If the CircularBuffer was already setup and used, calling that function again will reset it to a blank state.
*/
function setup(Bytes32CircularBuffer storage self, uint256 length) internal {
function setup(Bytes32CircularBuffer storage self, uint256 size) internal {
clear(self);
Arrays.unsafeSetLength(self._data, length);
Arrays.unsafeSetLength(self._data, size);
}

/**
Expand All @@ -62,8 +62,8 @@ library CircularBuffer {
*/
function push(Bytes32CircularBuffer storage self, bytes32 value) internal {
uint256 index = self._count++;
uint256 length = self._data.length;
Arrays.unsafeAccess(self._data, index % length).value = value;
uint256 module = self._data.length;
Arrays.unsafeAccess(self._data, index % module).value = value;
}

/**
Expand All @@ -89,23 +89,23 @@ library CircularBuffer {
*/
function last(Bytes32CircularBuffer storage self, uint256 i) internal view returns (bytes32) {
uint256 index = self._count;
uint256 length = self._data.length;
uint256 total = Math.min(index, length); // count(self)
uint256 module = self._data.length;
uint256 total = Math.min(index, module); // count(self)
if (i >= total) {
Panic.panic(Panic.ARRAY_OUT_OF_BOUNDS);
}
return Arrays.unsafeAccess(self._data, (index - i - 1) % length).value;
return Arrays.unsafeAccess(self._data, (index - i - 1) % module).value;
}

/**
* @dev Check if a given value is in the buffer.
*/
function includes(Bytes32CircularBuffer storage self, bytes32 value) internal view returns (bool) {
uint256 index = self._count;
uint256 length = self._data.length;
uint256 total = Math.min(index, length); // count(self)
uint256 module = self._data.length;
uint256 total = Math.min(index, module); // count(self)
for (uint256 i = 0; i < total; ++i) {
if (Arrays.unsafeAccess(self._data, (index - i - 1) % length).value == value) {
if (Arrays.unsafeAccess(self._data, (index - i - 1) % module).value == value) {
return true;
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/utils/structs/CircularBuffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('CircularBuffer', function () {

it('starts empty', async function () {
expect(await this.mock.$count(0)).to.equal(0n);
expect(await this.mock.$size(0)).to.equal(LENGTH);
expect(await this.mock.$length(0)).to.equal(LENGTH);
expect(await this.mock.$includes(0, ethers.ZeroHash)).to.be.false;
await expect(this.mock.$last(0, 0)).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
});
Expand All @@ -38,7 +38,7 @@ describe('CircularBuffer', function () {
const dropped = pushed.slice(0, -LENGTH);

// check count
expect(await this.mock.$size(0)).to.equal(LENGTH);
expect(await this.mock.$length(0)).to.equal(LENGTH);
expect(await this.mock.$count(0)).to.equal(stored.length);

// check last
Expand All @@ -65,14 +65,14 @@ describe('CircularBuffer', function () {
await this.mock.$push(0, value);

expect(await this.mock.$count(0)).to.equal(1n);
expect(await this.mock.$size(0)).to.equal(LENGTH);
expect(await this.mock.$length(0)).to.equal(LENGTH);
expect(await this.mock.$includes(0, value)).to.be.true;
await this.mock.$last(0, 0); // not revert

await this.mock.$clear(0);

expect(await this.mock.$count(0)).to.equal(0n);
expect(await this.mock.$size(0)).to.equal(LENGTH);
expect(await this.mock.$length(0)).to.equal(LENGTH);
expect(await this.mock.$includes(0, value)).to.be.false;
await expect(this.mock.$last(0, 0)).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
});
Expand Down