Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

Commit 8f605e7

Browse files
authored
Merge pull request #40 from morpho-labs/refactor/remove-bucket-dll-getters
Remove bucket DLL getters
2 parents e0e4745 + bf66f98 commit 8f605e7

7 files changed

Lines changed: 45 additions & 30 deletions

src/BucketDLL.sol

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,15 @@ library BucketDLL {
1919

2020
/* INTERNAL */
2121

22-
/// @notice Returns the address at the head of the `list`.
23-
/// @param list The list from which to get the head.
24-
/// @return The address of the head.
25-
function getHead(List storage list) internal view returns (address) {
26-
return list.accounts[address(0)].next;
27-
}
28-
29-
/// @notice Returns the address at the tail of the `list`.
30-
/// @param list The list from which to get the tail.
31-
/// @return The address of the tail.
32-
function getTail(List storage list) internal view returns (address) {
33-
return list.accounts[address(0)].prev;
34-
}
35-
3622
/// @notice Returns the next id address from the current `id`.
23+
/// @dev Pass the address 0 to get the head of the list.
3724
/// @param list The list to search in.
3825
/// @param id The address of the current account.
3926
/// @return The address of the next account.
4027
function getNext(List storage list, address id) internal view returns (address) {
4128
return list.accounts[id].next;
4229
}
4330

44-
/// @notice Returns the previous id address from the current `id`.
45-
/// @param list The list to search in.
46-
/// @param id The address of the current account.
47-
/// @return The address of the previous account.
48-
function getPrev(List storage list, address id) internal view returns (address) {
49-
return list.accounts[id].prev;
50-
}
51-
5231
/// @notice Removes an account of the `list`.
5332
/// @dev This function should not be called with `id` equal to address 0.
5433
/// @dev This function should not be called with an `_id` that is not in the list.
@@ -70,7 +49,7 @@ library BucketDLL {
7049

7150
/// @notice Inserts an account in the `list`.
7251
/// @dev This function should not be called with `id` equal to address 0.
73-
/// @dev This function should not be called with an `_id` that is already in the list.
52+
/// @dev This function should not be called with an `id` that is already in the list.
7453
/// @param list The list to search in.
7554
/// @param id The address of the account.
7655
/// @param atHead Tells whether to insert at the head or at the tail of the list.

src/LogarithmicBuckets.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ library LogarithmicBuckets {
7272
if (bucketsMask == 0) return address(0);
7373

7474
uint256 next = nextBucket(value, bucketsMask);
75-
if (next != 0) return buckets.buckets[next].getHead();
75+
if (next != 0) return buckets.buckets[next].getNext(address(0));
7676

7777
// `highestSetBit` is used to compute the highest non-empty bucket.
7878
// Knowing that `next` == 0, it is also the highest previous non-empty bucket.
7979
uint256 prev = highestSetBit(bucketsMask);
80-
return buckets.buckets[prev].getHead();
80+
return buckets.buckets[prev].getNext(address(0));
8181
}
8282

8383
/* PRIVATE */

test/TestBucketDLL.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
pragma solidity ^0.8.0;
33

44
import "forge-std/Test.sol";
5-
import "src/BucketDLL.sol";
5+
import "./mocks/BucketDLLMock.sol";
66

77
contract TestBucketDLL is Test {
8-
using BucketDLL for BucketDLL.List;
8+
using BucketDLLMock for BucketDLL.List;
99

1010
uint256 internal numberOfAccounts = 50;
1111
address[] public accounts;

test/TestLogarithmicBuckets.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "forge-std/Test.sol";
55
import "./mocks/LogarithmicBucketsMock.sol";
66

77
contract TestLogarithmicBuckets is LogarithmicBucketsMock, Test {
8-
using BucketDLL for BucketDLL.List;
8+
using BucketDLLMock for BucketDLL.List;
99
using LogarithmicBuckets for LogarithmicBuckets.Buckets;
1010

1111
uint256 public accountsLength = 50;

test/TestLogarithmicBucketsInvariant.t.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pragma solidity ^0.8.0;
33

44
import "./helpers/Random.sol";
55
import "./mocks/LogarithmicBucketsMock.sol";
6+
import "./mocks/BucketDLLMock.sol";
67
import "forge-std/Test.sol";
78

89
contract TestLogarithmicBucketsInvariant is Test, Random {
@@ -31,7 +32,7 @@ contract TestLogarithmicBucketsInvariant is Test, Random {
3132
}
3233

3334
contract LogarithmicBucketsSeenMock is LogarithmicBucketsMock {
34-
using BucketDLL for BucketDLL.List;
35+
using BucketDLLMock for BucketDLL.List;
3536
using LogarithmicBuckets for LogarithmicBuckets.Buckets;
3637

3738
address[] public seen;

test/mocks/BucketDLLMock.sol

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: AGPL-3.0-only
2+
pragma solidity ^0.8.0;
3+
4+
import "src/BucketDLL.sol";
5+
6+
library BucketDLLMock {
7+
function remove(BucketDLL.List storage _list, address _id) internal returns (bool) {
8+
return BucketDLL.remove(_list, _id);
9+
}
10+
11+
function insert(
12+
BucketDLL.List storage _list,
13+
address _id,
14+
bool _head
15+
) internal returns (bool) {
16+
return BucketDLL.insert(_list, _id, _head);
17+
}
18+
19+
function getNext(BucketDLL.List storage _list, address _id) internal view returns (address) {
20+
return BucketDLL.getNext(_list, _id);
21+
}
22+
23+
function getHead(BucketDLL.List storage _list) internal view returns (address) {
24+
return _list.accounts[address(0)].next;
25+
}
26+
27+
function getPrev(BucketDLL.List storage _list, address _id) internal view returns (address) {
28+
return _list.accounts[_id].prev;
29+
}
30+
31+
function getTail(BucketDLL.List storage _list) internal view returns (address) {
32+
return _list.accounts[address(0)].prev;
33+
}
34+
}

test/mocks/LogarithmicBucketsMock.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
pragma solidity ^0.8.0;
33

44
import "src/LogarithmicBuckets.sol";
5+
import "./BucketDLLMock.sol";
56

67
contract LogarithmicBucketsMock {
7-
using BucketDLL for BucketDLL.List;
8+
using BucketDLLMock for BucketDLL.List;
89
using LogarithmicBuckets for LogarithmicBuckets.Buckets;
910

1011
LogarithmicBuckets.Buckets public buckets;

0 commit comments

Comments
 (0)