Skip to content

Commit 81d7b86

Browse files
committed
fix: minor CreditAccountCompressor improvements
1 parent f342711 commit 81d7b86

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

contracts/compressors/CreditAccountCompressor.sol

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ contract CreditAccountCompressor is IVersion, SanityCheckTrait {
5959
}
6060

6161
/// @notice Returns data for credit accounts that match `caFilter` in credit managers matching `cmFilter`
62-
/// @dev The `false` value of `finished` return variable indicates either that gas supplied with a call was
63-
/// insufficient to process all the accounts and next iteration starting from `nextOffset` is needed
62+
/// @dev The non-zero value of `nextOffset` return variable indicates that gas supplied with a call was
63+
/// insufficient to process all the accounts and next iteration starting from this value is needed
6464
function getCreditAccounts(CreditManagerFilter memory cmFilter, CreditAccountFilter memory caFilter, uint256 offset)
6565
external
6666
view
67-
returns (CreditAccountData[] memory data, bool finished, uint256 nextOffset)
67+
returns (CreditAccountData[] memory data, uint256 nextOffset)
6868
{
6969
address[] memory creditManagers = _getCreditManagers(cmFilter);
7070
return _getCreditAccounts(creditManagers, caFilter, offset, type(uint256).max);
@@ -76,18 +76,18 @@ contract CreditAccountCompressor is IVersion, SanityCheckTrait {
7676
CreditAccountFilter memory caFilter,
7777
uint256 offset,
7878
uint256 limit
79-
) public view returns (CreditAccountData[] memory data, bool finished, uint256 nextOffset) {
79+
) public view returns (CreditAccountData[] memory data, uint256 nextOffset) {
8080
address[] memory creditManagers = _getCreditManagers(cmFilter);
8181
return _getCreditAccounts(creditManagers, caFilter, offset, limit);
8282
}
8383

8484
/// @notice Returns data for credit accounts that match `caFilter` in a given `creditManager`
85-
/// @dev The `false` value of `finished` return variable indicates either that gas supplied with a call was
86-
/// insufficient to process all the accounts and next iteration starting from `nextOffset` is needed
85+
/// @dev The non-zero value of `nextOffset` return variable indicates that gas supplied with a call was
86+
/// insufficient to process all the accounts and next iteration starting from this value is needed
8787
function getCreditAccounts(address creditManager, CreditAccountFilter memory caFilter, uint256 offset)
8888
external
8989
view
90-
returns (CreditAccountData[] memory data, bool finished, uint256 nextOffset)
90+
returns (CreditAccountData[] memory data, uint256 nextOffset)
9191
{
9292
address[] memory creditManagers = new address[](1);
9393
creditManagers[0] = creditManager;
@@ -100,7 +100,7 @@ contract CreditAccountCompressor is IVersion, SanityCheckTrait {
100100
CreditAccountFilter memory caFilter,
101101
uint256 offset,
102102
uint256 limit
103-
) external view returns (CreditAccountData[] memory data, bool finished, uint256 nextOffset) {
103+
) external view returns (CreditAccountData[] memory data, uint256 nextOffset) {
104104
address[] memory creditManagers = new address[](1);
105105
creditManagers[0] = creditManager;
106106
return _getCreditAccounts(creditManagers, caFilter, offset, limit);
@@ -141,9 +141,9 @@ contract CreditAccountCompressor is IVersion, SanityCheckTrait {
141141
CreditAccountFilter memory filter,
142142
uint256 offset,
143143
uint256 limit
144-
) internal view returns (CreditAccountData[] memory data, bool finished, uint256 nextOffset) {
144+
) internal view returns (CreditAccountData[] memory data, uint256 nextOffset) {
145145
uint256 num = _countCreditAccounts(creditManagers, filter, offset, limit);
146-
if (num == 0) return (data, true, 0);
146+
if (num == 0) return (data, 0);
147147

148148
// allocating the `CreditAccountData` array might consume most of the gas leaving no room for computations,
149149
// so we instead allocate and gradually fill the array of pointers to structs which takes much less space
@@ -185,15 +185,15 @@ contract CreditAccountCompressor is IVersion, SanityCheckTrait {
185185
}
186186

187187
// rough approximation of gas that will be needed to accommodate additional memory expansion cost
188-
gasReserve += gasBefore - gasleft();
188+
gasReserve += (gasBefore - gasleft()) / 2;
189189
}
190190
--count;
191191

192-
if (dataOffset == dataPointers.length || gasleft() < gasReserve) break;
192+
if (dataOffset == num || gasleft() < gasReserve) break;
193193
}
194194

195195
nextOffset += creditAccounts.length - count;
196-
if (dataOffset == dataPointers.length || count != 0) break;
196+
if (dataOffset == num || count != 0) break;
197197

198198
limit -= creditAccounts.length;
199199
if (limit == 0) break;
@@ -207,7 +207,8 @@ contract CreditAccountCompressor is IVersion, SanityCheckTrait {
207207
mstore(data, dataOffset)
208208
}
209209

210-
return (data, dataOffset == num, nextOffset);
210+
// set `nextOffset` to zero to indicate that scanning is finished
211+
if (dataOffset == num) nextOffset = 0;
211212
}
212213

213214
/// @dev Counting implementation

0 commit comments

Comments
 (0)