Skip to content

Commit ec3c5a2

Browse files
committed
optimised minipool filter loop
1 parent 68219dc commit ec3c5a2

1 file changed

Lines changed: 42 additions & 21 deletions

File tree

contracts/RocketPool.sol

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -251,66 +251,65 @@ contract RocketPool is RocketBase {
251251
/// @dev Get all pools that match this status (explicit method)
252252
/// @param _status Get pools with the current status
253253
function getPoolsFilterWithStatus(uint256 _status) public view returns(address[] memory) {
254-
return getPoolsFilter(false, _status, 0, 0, 0, false);
254+
return getPoolsFilter(1, _status, 0, 0, 0);
255255
}
256256

257257
/// @dev Get all pools that match this status and set staking duration (explicit method)
258258
/// @param _status Get pools with the current status
259259
/// @param _stakingDuration Get pools with the current staking duration
260260
function getPoolsFilterWithStatusAndDuration(uint256 _status, uint256 _stakingDuration) public view returns(address[] memory) {
261-
return getPoolsFilter(false, _status, 0, _stakingDuration, 0, false);
261+
return getPoolsFilter(2, _status, 0, _stakingDuration, 0);
262262
}
263263

264264
/// @dev Get all pools that are assigned to this node (explicit method)
265265
/// @param _nodeAddress Get pools with the current node
266266
function getPoolsFilterWithNode(address _nodeAddress) public view returns(address[] memory) {
267-
return getPoolsFilter(false, 99, _nodeAddress, 0, 0, false);
267+
return getPoolsFilter(3, 0, _nodeAddress, 0, 0);
268268
}
269269

270270
/// @dev Return count of all pools that are assigned to this node (explicit method)
271271
/// @param _nodeAddress Get pools with the current node
272272
function getPoolsFilterWithNodeCount(address _nodeAddress) public view returns(uint256) {
273-
return getPoolsFilter(false, 99, _nodeAddress, 0, 0, false).length;
273+
return getPoolsFilter(3, 0, _nodeAddress, 0, 0).length;
274274
}
275275

276276
/// @dev Return all pools that are assigned to this node and have the current status (explicit method)
277277
/// @param _nodeAddress Get pools with the current node
278278
/// @param _status Pool status to filter pools
279279
function getPoolsFilterWithNodeWithStatus(address _nodeAddress, uint256 _status) public view returns(address[]) {
280-
return getPoolsFilter(false, _status, _nodeAddress, 0, 0, false);
280+
return getPoolsFilter(4, _status, _nodeAddress, 0, 0);
281281
}
282282

283283
/// @dev Return count of all pools that are assigned to this node and have the current status (explicit method)
284284
/// @param _nodeAddress Get pools with the current node
285285
function getPoolsFilterWithNodeWithStatusCount(address _nodeAddress, uint256 _status) public view returns(uint256) {
286-
return getPoolsFilter(false, _status, _nodeAddress, 0, 0, false).length;
286+
return getPoolsFilter(4, _status, _nodeAddress, 0, 0).length;
287287
}
288288

289289
/// @dev Get all pools that match this user belongs too (explicit method)
290290
/// @param _userAddress Get pools with the current user
291291
function getPoolsFilterWithUser(address _userAddress) public view returns(address[] memory) {
292-
return getPoolsFilter(false, 99, 0, 0, _userAddress, false);
292+
return getPoolsFilter(5, 0, 0, 0, _userAddress);
293293
}
294294

295295
/// @dev Get all pools that match this user belongs too and has a deposit > 0 (explicit method)
296296
/// @param _userAddress Get pools with the current user
297297
function getPoolsFilterWithUserDeposit(address _userAddress) public view returns(address[] memory) {
298-
return getPoolsFilter(false, 99, 0, 0, _userAddress, true);
298+
return getPoolsFilter(6, 0, 0, 0, _userAddress);
299299
}
300300

301301
/// @dev Returns all current mini pools (explicit method)
302302
function getPools() view private returns(address[] memory) {
303-
return getPoolsFilter(true, 99, 0, 0, 0, false);
303+
return getPoolsFilter(0, 0, 0, 0, 0);
304304
}
305305

306306
/// @dev Get the address of any pools with the current set status or filter
307-
/// @param _returnAll Return all mini pools
307+
/// @param _type The type of minipool filter
308308
/// @param _status Get pools with the current status
309309
/// @param _nodeAddress Filter pools that are currently assigned to this node address
310310
/// @param _stakingDuration The duration that the pool with stake with Casper for
311311
/// @param _userAddress The address of a user account in the pool
312-
/// @param _userHasDeposit Filter pools on users that have a deposit > 0 in the pool
313-
function getPoolsFilter(bool _returnAll, uint256 _status, address _nodeAddress, uint256 _stakingDuration, address _userAddress, bool _userHasDeposit) view private returns(address[] memory) {
312+
function getPoolsFilter(uint8 _type, uint256 _status, address _nodeAddress, uint256 _stakingDuration, address _userAddress) view private returns(address[] memory) {
314313
// Get the mini pool count
315314
uint256 miniPoolCount = getPoolsCount();
316315
// Create an array at the length of the current pools, then populate it
@@ -323,15 +322,37 @@ contract RocketPool is RocketBase {
323322
// Get an instance of that pool contract
324323
RocketPoolMini pool = getPoolInstance(pools[i]);
325324
// Check the pool meets any supplied filters
326-
if (
327-
(_nodeAddress == 0 && _status < 10 && pool.getStatus() == _status && _stakingDuration == 0) ||
328-
(_nodeAddress == 0 && _status < 10 && pool.getStatus() == _status && _stakingDuration > 0 && _stakingDuration == pool.getStakingDuration()) ||
329-
(_userAddress != 0 && pool.getUserExists(_userAddress)) ||
330-
(_userAddress != 0 && _userHasDeposit == true && pool.getUserHasDeposit(_userAddress)) ||
331-
(_nodeAddress != 0 && _status > 10 && _nodeAddress == pool.getNodeAddress()) ||
332-
(_nodeAddress != 0 && pool.getStatus() == _status && _nodeAddress == pool.getNodeAddress()) ||
333-
_returnAll == true) {
334-
// Matched
325+
if(_type == 1) {
326+
if(pool.getStatus() == _status) {
327+
poolsFound[i] = pools[i];
328+
}
329+
}
330+
if(_type == 2) {
331+
if(pool.getStatus() == _status && _stakingDuration == pool.getStakingDuration()) {
332+
poolsFound[i] = pools[i];
333+
}
334+
}
335+
if(_type == 3) {
336+
if(_nodeAddress == pool.getNodeAddress()) {
337+
poolsFound[i] = pools[i];
338+
}
339+
}
340+
if(_type == 4) {
341+
if(_nodeAddress == pool.getNodeAddress() && pool.getStatus() == _status) {
342+
poolsFound[i] = pools[i];
343+
}
344+
}
345+
if(_type == 5) {
346+
if(pool.getUserExists(_userAddress)) {
347+
poolsFound[i] = pools[i];
348+
}
349+
}
350+
if(_type == 6) {
351+
if(pool.getUserHasDeposit(_userAddress)) {
352+
poolsFound[i] = pools[i];
353+
}
354+
}
355+
if(_type == 0) {
335356
poolsFound[i] = pools[i];
336357
}
337358
}

0 commit comments

Comments
 (0)