Liquid Staking

Stakelink
DeFiHardhatOracle
50,000 USDC
View results
Submission Details
Severity: low
Invalid

Inefficiency in Strategy Removal Process in the `removeStrategy` Function

Github

Summary

The removeStrategy function in the StakingPool contract currently implements a mechanism to remove a strategy by shifting all elements of the strategies array after the removal index. This approach is less efficient compared to a standard array manipulation technique that involves replacing the element to be removed with the last element in the array and then removing the last element.

Vulnerability Details

The existing implementation of the removeStrategy function shifts the elements in the array to the left by one position when a strategy is removed, as shown in the following code snippet:

for (uint256 i = _index; i < strategies.length - 1; i++) {
strategies[i] = strategies[i + 1];
}
strategies.pop();

This approach ensures that the order of strategies is preserved, but it has a time complexity of O(n), where n is the number of strategies in the array. The operation shifts every element after the removed strategy to fill the gap, which can be expensive in terms of gas costs, especially when the array is large.

The recommended approach is to replace the element at the _index with the last element of the array and then call the pop() function to remove the last element:

if (_index < strategies.length - 1) {
strategies[_index] = strategies[strategies.length - 1];
}
strategies.pop();

This technique has a constant time complexity, O(1), because it involves only a single replacement operation and one call to pop(). This makes the function significantly more efficient, even for large arrays.

Impact

The inefficiency in the current implementation can lead to increased gas costs, which directly affects the contract's performance. The longer the strategies array, the higher the gas fees required to remove a strategy. This could have a considerable impact on users or administrators trying to interact with the contract, especially if frequent updates to the strategy list are required.

Additionally, the higher gas costs could become a deterrent to scaling the number of strategies, limiting the flexibility and expandability of the staking pool.

Imagine a scenario where the strategies array has 100 entries, and the 10th strategy is to be removed. In the current implementation, the function would need to shift 90 elements to the left, resulting in multiple storage operations that increase gas costs. In contrast, using the optimized approach, only one element is moved, followed by a call to pop(), which is far more efficient.

Tools Used

Manual Review

Recommendations

Implement the suggested optimization by replacing the strategy to be removed with the last element in the array and then popping the last element. This change will significantly reduce gas costs for strategy removal operations, making it more efficient.

Updates

Lead Judging Commences

inallhonesty Lead Judge about 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.