stake.link

stake.link
DeFiHardhatBridge
27,500 USDC
View results
Submission Details
Severity: low
Invalid

Redundant Loop in `_mintQueuedNewLocks`: Unnecessary Computations Leading to Extra Gas Consumption

Summary

The SDLPoolSecondary contract _mintQueuedNewLocks function has an optimization opportunity. The current implementation uses two loops to process and clean up an array of new locks. By introducing a minor adjustment, the need for the second loop is eliminated, resulting in improved efficiency and gas cost savings.

Vulnerability Details

The original code contains a while loop that iterates through an array of new locks. Within the loop, certain elements are skipped using a break statement based on a condition (newLockPointer.updateBatchIndex > finalizedBatchIndex). The skipped elements were previously handled in a second loop, copying the remaining elements to the beginning of the array. Which can introduce many gas issues and redundant code.

Impact

The impact of this issue lies in the efficiency and gas cost of the _mintQueuedNewLocks function. The redundant second loop for cleanup introduces unnecessary computational steps and increases gas consumption.

Tools Used

Manual review.

Recommendations

The proposed optimization involves moving the ++i increment operation above the if condition and adding continue instead of break. This modification eliminates the need for the second loop, streamlining the code execution. The revised code snippet is as follows:

while (i < numNewLocks) {
NewLockPointer memory newLockPointer = newLocksByOwner[_owner][i];
++i; // Move increment operation above the continue statement
if (newLockPointer.updateBatchIndex > finalizedBatchIndex) {
continue; // Skip processing elements that meet the condition
}
// ... (rest of the loop remains unchanged)
// Adjustments to update the necessary state are retained here
}
-for (uint256 j = 0; j < numNewLocks; ++j) {
- if (i == numNewLocks) {
- newLocksByOwner[_owner].pop();
- } else {
- newLocksByOwner[_owner][j] = newLocksByOwner[_owner][i];
- ++i;
- }
-}

This adjustment enhances the efficiency of the function and reduces gas consumption by eliminating the need for a separate loop to clean up the array.

NOTE: The same issue is present in _executeQueuedLockUpdates function also, and the recommendation can be applied to this function.

Updates

Lead Judging Commences

0kage Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.