Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas in LimboDAO.seed(): Avoiding a 2N for-loop for a N one #309

Open
code423n4 opened this issue Feb 2, 2022 · 0 comments
Open

Gas in LimboDAO.seed(): Avoiding a 2N for-loop for a N one #309

code423n4 opened this issue Feb 2, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons

Comments

@code423n4
Copy link
Contributor

Handle

Dravee

Vulnerability details

Impact

Looping twice is expensive. It's possible to iterate only once from 0 to maxLength.

Proof of Concept

Original code iterating between N and 2N times here: https://github.com/code-423n4/2022-01-behodler/blob/71d8e0cfd9388f975d6a90dffba9b502b222bdfe/contracts/DAO/LimboDAO.sol#L195-L222

It's possible to iterate only N times on sushiLPs and uniLPs.
Here's the refacto I suggest, it starts from the comment //@audit refacto starts here and ends with //@audit refacto ends here:

File: LimboDAO.sol
213:     //@audit refacto starts here
214:     bool isSushiBigger = sushiLPs.length >= uniLPs.length;
215:     (uint256 shortLength, uint256 longLength) = isSushiBigger ? (uniLPs.length, sushiLPs.length) : (sushiLPs.length, uniLPs.length);
216: 
217:     for (uint256 i; i < shortLength; ++i) {
218:       sushiLoop(sushiLPs[i]);
219:       uniLoop(uniLPs[i]);
220:     }
221:     
222:     if (isSushiBigger) {
223:       for (uint256 i = shortLength; i < longLength; ++i) {
224:         sushiLoop(sushiLPs[i]);
225:       }
226:     } else {
227:       for (uint256 i = shortLength; i < longLength; ++i) {
228:         uniLoop(uniLPs[i]);
229:       }
230:     }
231:   }
232: 
233:   function sushiLoop(
234:     address sushiLp
235:   ) private {
236:     require(UniPairLike(sushiLp).factory() == sushiFactory, "LimboDAO: invalid Sushi LP");
237:     if (IERC20(eye).balanceOf(sushiLp) > 1000) assetApproved[sushiLp] = true;
238:     fateGrowthStrategy[sushiLp] = FateGrowthStrategy.indirectTwoRootEye;
239:   }
240: 
241:   function uniLoop(
242:     address uniLP
243:   ) private {
244:     require(UniPairLike(uniLP).factory() == uniFactory, "LimboDAO: invalid Uni LP"); //@audit-info Dravee: I corrected the comment
245:     if (IERC20(eye).balanceOf(uniLP) > 1000) assetApproved[uniLP] = true;
246:     fateGrowthStrategy[uniLP] = FateGrowthStrategy.indirectTwoRootEye;
247:   }
248:   //@audit refacto ends here

Tools Used

VS Code

Recommended Mitigation Steps

Apply the suggested refacto

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Feb 2, 2022
code423n4 added a commit that referenced this issue Feb 2, 2022
@gititGoro gititGoro added duplicate This issue or pull request already exists sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons and removed duplicate This issue or pull request already exists labels Feb 5, 2022
@gititGoro gititGoro added unresolved indicate confirmed issues that haven't been resolved with a PR and removed unresolved indicate confirmed issues that haven't been resolved with a PR labels May 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Projects
None yet
Development

No branches or pull requests

2 participants